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