1 /* Copyright (c) 1998-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 /* The declarations for the request and response types are in the file
28 "nscd-client.h", which should contain everything needed by client
30 #include "nscd-client.h"
33 /* Handle databases. */
45 /* Default limit on the number of times a value gets reloaded without
46 being used in the meantime. NSCD does not throw a value out as
47 soon as it times out. It tries to reload the value from the
48 server. Only if the value has not been used for so many rounds it
50 #define DEFAULT_RELOAD_LIMIT 5
53 /* Time before restarting the process in paranoia mode. */
54 #define RESTART_INTERVAL (60 * 60)
57 /* Stack size for worker threads. */
58 #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4)
60 /* Maximum size of stack frames we allow the thread to use. We use
61 80% of the thread stack size. */
62 #define MAX_STACK_USE ((8 * NSCD_THREAD_STACKSIZE) / 10)
64 /* Records the file registered per database that when changed
65 or modified requires invalidating the database. */
68 /* Tracks the last modified time of the traced file. */
70 /* Support multiple registered files per database. */
71 struct traced_file
*next
;
73 /* Requires Inotify support to do anything useful. */
78 # define PATH_MAX 1024
80 /* The parent directory is used to scan for creation/deletion. */
82 /* Just the name of the file with no directory component. */
84 /* The full-path name of the registered file. */
88 /* Initialize a `struct traced_file`. As input we need the name
89 of the file, and if invalidation requires calling res_init.
90 If CRINIT is 1 then res_init will be called after invalidation
91 or if the traced file is changed in any way, otherwise it will
94 init_traced_file(struct traced_file
*file
, const char *fname
, int crinit
)
98 file
->inotify_descr
[TRACED_FILE
] = -1;
99 file
->inotify_descr
[TRACED_DIR
] = -1;
100 strcpy (file
->fname
, fname
);
101 /* Compute the parent directory name and store a copy. The copy makes
102 it much faster to add/remove watches while nscd is running instead
103 of computing this over and over again in a temp buffer. */
104 file
->dname
[0] = '\0';
105 dname
= strrchr (fname
, '/');
108 size_t len
= (size_t)(dname
- fname
);
109 if (len
> sizeof (file
->dname
))
111 memcpy (file
->dname
, file
->fname
, len
);
112 file
->dname
[len
] = '\0';
114 /* The basename is the name just after the last forward slash. */
115 file
->sfname
= &dname
[1];
116 file
->call_res_init
= crinit
;
119 #define define_traced_file(id, filename) \
122 struct traced_file file; \
123 char buf[sizeof (struct traced_file) + sizeof (filename)]; \
126 /* Structure describing dynamic part of one database. */
129 pthread_rwlock_t lock
;
130 pthread_cond_t prune_cond
;
131 pthread_mutex_t prune_lock
;
132 pthread_mutex_t prune_run_lock
;
141 struct traced_file
*traced_files
;
142 const char *db_filename
;
143 size_t suggested_module
;
146 unsigned long int postimeout
; /* In seconds. */
147 unsigned long int negtimeout
; /* In seconds. */
149 int wr_fd
; /* Writable file descriptor. */
150 int ro_fd
; /* Unwritable file descriptor. */
152 const struct iovec
*disabled_iov
;
154 struct database_pers_head
*head
;
157 pthread_mutex_t memlock
;
159 bool last_alloc_failed
;
163 /* Paths of the file for the persistent storage. */
164 #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd"
165 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
166 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
167 #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services"
168 #define _PATH_NSCD_NETGROUP_DB "/var/db/nscd/netgroup"
170 /* Path used when not using persistent storage. */
171 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
173 /* Maximum alignment requirement we will encounter. */
174 #define BLOCK_ALIGN_LOG 3
175 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
176 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
178 /* Default value for the maximum size of the database files. */
179 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
181 /* Number of bytes of data we initially reserve for each hash table bucket. */
182 #define DEFAULT_DATASIZE_PER_BUCKET 1024
184 /* Default module of hash table. */
185 #define DEFAULT_SUGGESTED_MODULE 211
188 /* Number of seconds between two cache pruning runs if we do not have
189 better information when it is really needed. */
190 #define CACHE_PRUNE_INTERVAL 15
193 /* Global variables. */
194 extern struct database_dyn dbs
[lastdb
] attribute_hidden
;
195 extern const char *const dbnames
[lastdb
];
196 extern const char *const serv2str
[LASTREQ
];
198 extern const struct iovec pwd_iov_disabled
;
199 extern const struct iovec grp_iov_disabled
;
200 extern const struct iovec hst_iov_disabled
;
201 extern const struct iovec serv_iov_disabled
;
202 extern const struct iovec netgroup_iov_disabled
;
205 /* Initial number of threads to run. */
207 /* Maximum number of threads to use. */
208 extern int max_nthreads
;
210 /* Inotify descriptor. */
211 extern int inotify_fd
;
213 /* User name to run server processes as. */
214 extern const char *server_user
;
216 /* Name and UID of user who is allowed to request statistics. */
217 extern const char *stat_user
;
218 extern uid_t stat_uid
;
220 /* Time the server was started. */
221 extern time_t start_time
;
223 /* Number of times clients had to wait. */
224 extern unsigned long int client_queued
;
226 /* Maximum needed alignment. */
227 extern const size_t block_align
;
229 /* Number of times a value is reloaded without being used. UINT_MAX
231 extern unsigned int reload_count
;
233 /* Pagesize minus one. */
234 extern uintptr_t pagesize_m1
;
236 /* Nonzero if paranoia mode is enabled. */
238 /* Time after which the process restarts. */
239 extern time_t restart_time
;
240 /* How much time between restarts. */
241 extern time_t restart_interval
;
242 /* Old current working directory. */
243 extern const char *oldcwd
;
244 /* Old user and group ID. */
245 extern uid_t old_uid
;
246 extern gid_t old_gid
;
249 /* Prototypes for global functions. */
251 /* Wrapper functions with error checking for standard functions. */
252 #include <programs/xmalloc.h>
255 extern void termination_handler (int signum
) __attribute__ ((__noreturn__
));
256 extern int nscd_open_socket (void);
257 void notify_parent (int child_ret
);
258 void do_exit (int child_ret
, int errnum
, const char *format
, ...);
261 extern void nscd_init (void);
262 extern void register_traced_file (size_t dbidx
, struct traced_file
*finfo
);
264 extern void install_watches (struct traced_file
*finfo
);
266 extern void close_sockets (void);
267 extern void start_threads (void) __attribute__ ((__noreturn__
));
270 extern int nscd_parse_file (const char *fname
,
271 struct database_dyn dbs
[lastdb
]);
274 extern void send_stats (int fd
, struct database_dyn dbs
[lastdb
]);
275 extern int receive_print_stats (void) __attribute__ ((__noreturn__
));
278 extern struct datahead
*cache_search (request_type
, const void *key
,
279 size_t len
, struct database_dyn
*table
,
281 extern int cache_add (int type
, const void *key
, size_t len
,
282 struct datahead
*packet
, bool first
,
283 struct database_dyn
*table
, uid_t owner
,
285 extern time_t prune_cache (struct database_dyn
*table
, time_t now
, int fd
);
288 extern void addpwbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
289 void *key
, uid_t uid
);
290 extern void addpwbyuid (struct database_dyn
*db
, int fd
, request_header
*req
,
291 void *key
, uid_t uid
);
292 extern time_t readdpwbyname (struct database_dyn
*db
, struct hashentry
*he
,
293 struct datahead
*dh
);
294 extern time_t readdpwbyuid (struct database_dyn
*db
, struct hashentry
*he
,
295 struct datahead
*dh
);
298 extern void addgrbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
299 void *key
, uid_t uid
);
300 extern void addgrbygid (struct database_dyn
*db
, int fd
, request_header
*req
,
301 void *key
, uid_t uid
);
302 extern time_t readdgrbyname (struct database_dyn
*db
, struct hashentry
*he
,
303 struct datahead
*dh
);
304 extern time_t readdgrbygid (struct database_dyn
*db
, struct hashentry
*he
,
305 struct datahead
*dh
);
308 extern void addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
309 void *key
, uid_t uid
);
310 extern void addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
311 void *key
, uid_t uid
);
312 extern void addhstbynamev6 (struct database_dyn
*db
, int fd
,
313 request_header
*req
, void *key
, uid_t uid
);
314 extern void addhstbyaddrv6 (struct database_dyn
*db
, int fd
,
315 request_header
*req
, void *key
, uid_t uid
);
316 extern time_t readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
317 struct datahead
*dh
);
318 extern time_t readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
319 struct datahead
*dh
);
320 extern time_t readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
321 struct datahead
*dh
);
322 extern time_t readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
323 struct datahead
*dh
);
326 extern void addhstai (struct database_dyn
*db
, int fd
, request_header
*req
,
327 void *key
, uid_t uid
);
328 extern time_t readdhstai (struct database_dyn
*db
, struct hashentry
*he
,
329 struct datahead
*dh
);
333 extern void addinitgroups (struct database_dyn
*db
, int fd
,
334 request_header
*req
, void *key
, uid_t uid
);
335 extern time_t readdinitgroups (struct database_dyn
*db
, struct hashentry
*he
,
336 struct datahead
*dh
);
339 extern void addservbyname (struct database_dyn
*db
, int fd
,
340 request_header
*req
, void *key
, uid_t uid
);
341 extern time_t readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
342 struct datahead
*dh
);
343 extern void addservbyport (struct database_dyn
*db
, int fd
,
344 request_header
*req
, void *key
, uid_t uid
);
345 extern time_t readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
346 struct datahead
*dh
);
348 /* netgroupcache.c */
349 extern void addinnetgr (struct database_dyn
*db
, int fd
, request_header
*req
,
350 void *key
, uid_t uid
);
351 extern time_t readdinnetgr (struct database_dyn
*db
, struct hashentry
*he
,
352 struct datahead
*dh
);
353 extern void addgetnetgrent (struct database_dyn
*db
, int fd
,
354 request_header
*req
, void *key
, uid_t uid
);
355 extern time_t readdgetnetgrent (struct database_dyn
*db
, struct hashentry
*he
,
356 struct datahead
*dh
);
359 extern void *mempool_alloc (struct database_dyn
*db
, size_t len
,
361 extern void gc (struct database_dyn
*db
);
364 /* nscd_setup_thread.c */
365 extern int setup_thread (struct database_dyn
*db
);
368 extern void nscd_print_cache (const char *name
);
370 /* Special version of TEMP_FAILURE_RETRY for functions returning error
372 #define TEMP_FAILURE_RETRY_VAL(expression) \
374 ({ long int __result; \
375 do __result = (long int) (expression); \
376 while (__result == EINTR); \