1 /* Copyright (c) 1998-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
26 /* The declarations for the request and response types are in the file
27 "nscd-client.h", which should contain everything needed by client
29 #include "nscd-client.h"
32 /* Handle databases. */
44 /* Default limit on the number of times a value gets reloaded without
45 being used in the meantime. NSCD does not throw a value out as
46 soon as it times out. It tries to reload the value from the
47 server. Only if the value has not been used for so many rounds it
49 #define DEFAULT_RELOAD_LIMIT 5
52 /* Time before restarting the process in paranoia mode. */
53 #define RESTART_INTERVAL (60 * 60)
56 /* Stack size for worker threads. */
57 #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4)
59 /* Maximum size of stack frames we allow the thread to use. We use
60 80% of the thread stack size. */
61 #define MAX_STACK_USE ((8 * NSCD_THREAD_STACKSIZE) / 10)
63 /* Records the file registered per database that when changed
64 or modified requires invalidating the database. */
67 /* Tracks the last modified time of the traced file. */
69 /* Support multiple registered files per database. */
70 struct traced_file
*next
;
72 /* Requires Inotify support to do anything useful. */
77 # define PATH_MAX 1024
79 /* The parent directory is used to scan for creation/deletion. */
81 /* Just the name of the file with no directory component. */
83 /* The full-path name of the registered file. */
87 /* Initialize a `struct traced_file`. As input we need the name
88 of the file, and if invalidation requires calling res_init.
89 If CRINIT is 1 then res_init will be called after invalidation
90 or if the traced file is changed in any way, otherwise it will
93 init_traced_file(struct traced_file
*file
, const char *fname
, int crinit
)
97 file
->inotify_descr
[TRACED_FILE
] = -1;
98 file
->inotify_descr
[TRACED_DIR
] = -1;
99 strcpy (file
->fname
, fname
);
100 /* Compute the parent directory name and store a copy. The copy makes
101 it much faster to add/remove watches while nscd is running instead
102 of computing this over and over again in a temp buffer. */
103 file
->dname
[0] = '\0';
104 dname
= strrchr (fname
, '/');
107 size_t len
= (size_t)(dname
- fname
);
108 if (len
> sizeof (file
->dname
))
110 memcpy (file
->dname
, file
->fname
, len
);
111 file
->dname
[len
] = '\0';
113 /* The basename is the name just after the last forward slash. */
114 file
->sfname
= &dname
[1];
115 file
->call_res_init
= crinit
;
118 #define define_traced_file(id, filename) \
121 struct traced_file file; \
122 char buf[sizeof (struct traced_file) + sizeof (filename)]; \
125 /* Structure describing dynamic part of one database. */
128 pthread_rwlock_t lock
;
129 pthread_cond_t prune_cond
;
130 pthread_mutex_t prune_lock
;
131 pthread_mutex_t prune_run_lock
;
140 struct traced_file
*traced_files
;
141 const char *db_filename
;
142 size_t suggested_module
;
145 unsigned long int postimeout
; /* In seconds. */
146 unsigned long int negtimeout
; /* In seconds. */
148 int wr_fd
; /* Writable file descriptor. */
149 int ro_fd
; /* Unwritable file descriptor. */
151 const struct iovec
*disabled_iov
;
153 struct database_pers_head
*head
;
156 pthread_mutex_t memlock
;
158 bool last_alloc_failed
;
162 /* Paths of the file for the persistent storage. */
163 #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd"
164 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
165 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
166 #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services"
167 #define _PATH_NSCD_NETGROUP_DB "/var/db/nscd/netgroup"
169 /* Path used when not using persistent storage. */
170 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
172 /* Maximum alignment requirement we will encounter. */
173 #define BLOCK_ALIGN_LOG 3
174 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
175 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
177 /* Default value for the maximum size of the database files. */
178 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
180 /* Number of bytes of data we initially reserve for each hash table bucket. */
181 #define DEFAULT_DATASIZE_PER_BUCKET 1024
183 /* Default module of hash table. */
184 #define DEFAULT_SUGGESTED_MODULE 211
187 /* Number of seconds between two cache pruning runs if we do not have
188 better information when it is really needed. */
189 #define CACHE_PRUNE_INTERVAL 15
192 /* Global variables. */
193 extern struct database_dyn dbs
[lastdb
] attribute_hidden
;
194 extern const char *const dbnames
[lastdb
];
195 extern const char *const serv2str
[LASTREQ
];
197 extern const struct iovec pwd_iov_disabled
;
198 extern const struct iovec grp_iov_disabled
;
199 extern const struct iovec hst_iov_disabled
;
200 extern const struct iovec serv_iov_disabled
;
201 extern const struct iovec netgroup_iov_disabled
;
204 /* Initial number of threads to run. */
206 /* Maximum number of threads to use. */
207 extern int max_nthreads
;
209 /* Inotify descriptor. */
210 extern int inotify_fd
;
212 /* User name to run server processes as. */
213 extern const char *server_user
;
215 /* Name and UID of user who is allowed to request statistics. */
216 extern const char *stat_user
;
217 extern uid_t stat_uid
;
219 /* Time the server was started. */
220 extern time_t start_time
;
222 /* Number of times clients had to wait. */
223 extern unsigned long int client_queued
;
225 /* Maximum needed alignment. */
226 extern const size_t block_align
;
228 /* Number of times a value is reloaded without being used. UINT_MAX
230 extern unsigned int reload_count
;
232 /* Pagesize minus one. */
233 extern uintptr_t pagesize_m1
;
235 /* Nonzero if paranoia mode is enabled. */
237 /* Time after which the process restarts. */
238 extern time_t restart_time
;
239 /* How much time between restarts. */
240 extern time_t restart_interval
;
241 /* Old current working directory. */
242 extern const char *oldcwd
;
243 /* Old user and group ID. */
244 extern uid_t old_uid
;
245 extern gid_t old_gid
;
248 /* Prototypes for global functions. */
250 /* Wrapper functions with error checking for standard functions. */
251 #include <programs/xmalloc.h>
254 extern void termination_handler (int signum
) __attribute__ ((__noreturn__
));
255 extern int nscd_open_socket (void);
256 void notify_parent (int child_ret
);
257 void do_exit (int child_ret
, int errnum
, const char *format
, ...);
260 extern void nscd_init (void);
261 extern void register_traced_file (size_t dbidx
, struct traced_file
*finfo
);
263 extern void install_watches (struct traced_file
*finfo
);
265 extern void close_sockets (void);
266 extern void start_threads (void) __attribute__ ((__noreturn__
));
269 extern int nscd_parse_file (const char *fname
,
270 struct database_dyn dbs
[lastdb
]);
273 extern void send_stats (int fd
, struct database_dyn dbs
[lastdb
]);
274 extern int receive_print_stats (void) __attribute__ ((__noreturn__
));
277 extern struct datahead
*cache_search (request_type
, const void *key
,
278 size_t len
, struct database_dyn
*table
,
280 extern int cache_add (int type
, const void *key
, size_t len
,
281 struct datahead
*packet
, bool first
,
282 struct database_dyn
*table
, uid_t owner
,
284 extern time_t prune_cache (struct database_dyn
*table
, time_t now
, int fd
);
287 extern void addpwbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
288 void *key
, uid_t uid
);
289 extern void addpwbyuid (struct database_dyn
*db
, int fd
, request_header
*req
,
290 void *key
, uid_t uid
);
291 extern time_t readdpwbyname (struct database_dyn
*db
, struct hashentry
*he
,
292 struct datahead
*dh
);
293 extern time_t readdpwbyuid (struct database_dyn
*db
, struct hashentry
*he
,
294 struct datahead
*dh
);
297 extern void addgrbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
298 void *key
, uid_t uid
);
299 extern void addgrbygid (struct database_dyn
*db
, int fd
, request_header
*req
,
300 void *key
, uid_t uid
);
301 extern time_t readdgrbyname (struct database_dyn
*db
, struct hashentry
*he
,
302 struct datahead
*dh
);
303 extern time_t readdgrbygid (struct database_dyn
*db
, struct hashentry
*he
,
304 struct datahead
*dh
);
307 extern void addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
308 void *key
, uid_t uid
);
309 extern void addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
310 void *key
, uid_t uid
);
311 extern void addhstbynamev6 (struct database_dyn
*db
, int fd
,
312 request_header
*req
, void *key
, uid_t uid
);
313 extern void addhstbyaddrv6 (struct database_dyn
*db
, int fd
,
314 request_header
*req
, void *key
, uid_t uid
);
315 extern time_t readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
316 struct datahead
*dh
);
317 extern time_t readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
318 struct datahead
*dh
);
319 extern time_t readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
320 struct datahead
*dh
);
321 extern time_t readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
322 struct datahead
*dh
);
325 extern void addhstai (struct database_dyn
*db
, int fd
, request_header
*req
,
326 void *key
, uid_t uid
);
327 extern time_t readdhstai (struct database_dyn
*db
, struct hashentry
*he
,
328 struct datahead
*dh
);
332 extern void addinitgroups (struct database_dyn
*db
, int fd
,
333 request_header
*req
, void *key
, uid_t uid
);
334 extern time_t readdinitgroups (struct database_dyn
*db
, struct hashentry
*he
,
335 struct datahead
*dh
);
338 extern void addservbyname (struct database_dyn
*db
, int fd
,
339 request_header
*req
, void *key
, uid_t uid
);
340 extern time_t readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
341 struct datahead
*dh
);
342 extern void addservbyport (struct database_dyn
*db
, int fd
,
343 request_header
*req
, void *key
, uid_t uid
);
344 extern time_t readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
345 struct datahead
*dh
);
347 /* netgroupcache.c */
348 extern void addinnetgr (struct database_dyn
*db
, int fd
, request_header
*req
,
349 void *key
, uid_t uid
);
350 extern time_t readdinnetgr (struct database_dyn
*db
, struct hashentry
*he
,
351 struct datahead
*dh
);
352 extern void addgetnetgrent (struct database_dyn
*db
, int fd
,
353 request_header
*req
, void *key
, uid_t uid
);
354 extern time_t readdgetnetgrent (struct database_dyn
*db
, struct hashentry
*he
,
355 struct datahead
*dh
);
358 extern void *mempool_alloc (struct database_dyn
*db
, size_t len
,
360 extern void gc (struct database_dyn
*db
);
363 /* nscd_setup_thread.c */
364 extern int setup_thread (struct database_dyn
*db
);
367 extern void nscd_print_cache (const char *name
);
369 /* Special version of TEMP_FAILURE_RETRY for functions returning error
371 #define TEMP_FAILURE_RETRY_VAL(expression) \
373 ({ long int __result; \
374 do __result = (long int) (expression); \
375 while (__result == EINTR); \