1 /* Copyright (c) 1998-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18 #ifndef _NSCD_H
19 #define _NSCD_H 1
20
21 #include <pthread.h>
22 #include <stdbool.h>
23 #include <time.h>
24 #include <sys/uio.h>
25
26 /* The declarations for the request and response types are in the file
27 "nscd-client.h", which should contain everything needed by client
28 functions. */
29 #include "nscd-client.h"
30
31
32 /* Handle databases. */
33 typedef enum
34 {
35 pwddb,
36 grpdb,
37 hstdb,
38 servdb,
39 netgrdb,
40 lastdb
41 } dbtype;
42
43
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
48 is removed. */
49 #define DEFAULT_RELOAD_LIMIT 5
50
51
52 /* Time before restarting the process in paranoia mode. */
53 #define RESTART_INTERVAL (60 * 60)
54
55
56 /* Stack size for worker threads. */
57 #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4)
58
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)
62
63 /* Records the file registered per database that when changed
64 or modified requires invalidating the database. */
65 struct traced_file
66 {
67 /* Tracks the last modified time of the traced file. */
68 time_t mtime;
69 /* Support multiple registered files per database. */
70 struct traced_file *next;
71 int call_res_init;
72 /* Requires Inotify support to do anything useful. */
73 #define TRACED_FILE 0
74 #define TRACED_DIR 1
75 int inotify_descr[2];
76 # ifndef PATH_MAX
77 # define PATH_MAX 1024
78 # endif
79 /* The parent directory is used to scan for creation/deletion. */
80 char dname[PATH_MAX];
81 /* Just the name of the file with no directory component. */
82 char *sfname;
83 /* The full-path name of the registered file. */
84 char fname[];
85 };
86
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
91 not. */
92 static inline void
init_traced_file(struct traced_file * file,const char * fname,int crinit)93 init_traced_file(struct traced_file *file, const char *fname, int crinit)
94 {
95 char *dname;
96 file->mtime = 0;
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, '/');
105 if (dname != NULL)
106 {
107 size_t len = (size_t)(dname - fname);
108 if (len > sizeof (file->dname))
109 abort ();
110 memcpy (file->dname, file->fname, len);
111 file->dname[len] = '\0';
112 }
113 /* The basename is the name just after the last forward slash. */
114 file->sfname = &dname[1];
115 file->call_res_init = crinit;
116 }
117
118 #define define_traced_file(id, filename) \
119 static union \
120 { \
121 struct traced_file file; \
122 char buf[sizeof (struct traced_file) + sizeof (filename)]; \
123 } id##_traced_file;
124
125 /* Structure describing dynamic part of one database. */
126 struct database_dyn
127 {
128 pthread_rwlock_t lock;
129 pthread_cond_t prune_cond;
130 pthread_mutex_t prune_lock;
131 pthread_mutex_t prune_run_lock;
132 time_t wakeup_time;
133
134 int enabled;
135 int check_file;
136 int clear_cache;
137 int persistent;
138 int shared;
139 int propagate;
140 struct traced_file *traced_files;
141 const char *db_filename;
142 size_t suggested_module;
143 size_t max_db_size;
144
145 unsigned long int postimeout; /* In seconds. */
146 unsigned long int negtimeout; /* In seconds. */
147
148 int wr_fd; /* Writable file descriptor. */
149 int ro_fd; /* Unwritable file descriptor. */
150
151 const struct iovec *disabled_iov;
152
153 struct database_pers_head *head;
154 char *data;
155 size_t memsize;
156 pthread_mutex_t memlock;
157 bool mmap_used;
158 bool last_alloc_failed;
159 };
160
161
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"
168
169 /* Path used when not using persistent storage. */
170 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
171
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)
176
177 /* Default value for the maximum size of the database files. */
178 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
179
180 /* Number of bytes of data we initially reserve for each hash table bucket. */
181 #define DEFAULT_DATASIZE_PER_BUCKET 1024
182
183 /* Default module of hash table. */
184 #define DEFAULT_SUGGESTED_MODULE 211
185
186
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
190
191
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];
196
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;
202
203
204 /* Initial number of threads to run. */
205 extern int nthreads;
206 /* Maximum number of threads to use. */
207 extern int max_nthreads;
208
209 /* Inotify descriptor. */
210 extern int inotify_fd;
211
212 /* User name to run server processes as. */
213 extern const char *server_user;
214
215 /* Name and UID of user who is allowed to request statistics. */
216 extern const char *stat_user;
217 extern uid_t stat_uid;
218
219 /* Time the server was started. */
220 extern time_t start_time;
221
222 /* Number of times clients had to wait. */
223 extern unsigned long int client_queued;
224
225 /* Maximum needed alignment. */
226 extern const size_t block_align;
227
228 /* Number of times a value is reloaded without being used. UINT_MAX
229 means unlimited. */
230 extern unsigned int reload_count;
231
232 /* Pagesize minus one. */
233 extern uintptr_t pagesize_m1;
234
235 /* Nonzero if paranoia mode is enabled. */
236 extern int paranoia;
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;
246
247
248 /* Prototypes for global functions. */
249
250 /* Wrapper functions with error checking for standard functions. */
251 #include <programs/xmalloc.h>
252
253 /* nscd.c */
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, ...);
258
259 /* connections.c */
260 extern void nscd_init (void);
261 extern void register_traced_file (size_t dbidx, struct traced_file *finfo);
262 #ifdef HAVE_INOTIFY
263 extern void install_watches (struct traced_file *finfo);
264 #endif
265 extern void close_sockets (void);
266 extern void start_threads (void) __attribute__ ((__noreturn__));
267
268 /* nscd_conf.c */
269 extern int nscd_parse_file (const char *fname,
270 struct database_dyn dbs[lastdb]);
271
272 /* nscd_stat.c */
273 extern void send_stats (int fd, struct database_dyn dbs[lastdb]);
274 extern int receive_print_stats (void) __attribute__ ((__noreturn__));
275
276 /* cache.c */
277 extern struct datahead *cache_search (request_type, const void *key,
278 size_t len, struct database_dyn *table,
279 uid_t owner);
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,
283 bool prune_wakeup);
284 extern time_t prune_cache (struct database_dyn *table, time_t now, int fd);
285
286 /* pwdcache.c */
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);
295
296 /* grpcache.c */
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);
305
306 /* hstcache.c */
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);
323
324 /* aicache.c */
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);
329
330
331 /* initgrcache.c */
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);
336
337 /* servicecache.c */
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);
346
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);
356
357 /* mem.c */
358 extern void *mempool_alloc (struct database_dyn *db, size_t len,
359 int data_alloc);
360 extern void gc (struct database_dyn *db);
361
362
363 /* nscd_setup_thread.c */
364 extern int setup_thread (struct database_dyn *db);
365
366 /* cachedumper.c */
367 extern void nscd_print_cache (const char *name);
368
369 /* Special version of TEMP_FAILURE_RETRY for functions returning error
370 values. */
371 #define TEMP_FAILURE_RETRY_VAL(expression) \
372 (__extension__ \
373 ({ long int __result; \
374 do __result = (long int) (expression); \
375 while (__result == EINTR); \
376 __result; }))
377
378 #endif /* nscd.h */
379