1/* Copyright (c) 1998-2017 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 <http://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. */
34typedef 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. */
66struct 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. */
93static inline void
94init_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 strncpy (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) \
120static 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. */
127struct 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. */
194extern struct database_dyn dbs[lastdb] attribute_hidden;
195extern const char *const dbnames[lastdb];
196extern const char *const serv2str[LASTREQ];
197
198extern const struct iovec pwd_iov_disabled;
199extern const struct iovec grp_iov_disabled;
200extern const struct iovec hst_iov_disabled;
201extern const struct iovec serv_iov_disabled;
202extern const struct iovec netgroup_iov_disabled;
203
204
205/* Initial number of threads to run. */
206extern int nthreads;
207/* Maximum number of threads to use. */
208extern int max_nthreads;
209
210/* Inotify descriptor. */
211extern int inotify_fd;
212
213/* User name to run server processes as. */
214extern const char *server_user;
215
216/* Name and UID of user who is allowed to request statistics. */
217extern const char *stat_user;
218extern uid_t stat_uid;
219
220/* Time the server was started. */
221extern time_t start_time;
222
223/* Number of times clients had to wait. */
224extern unsigned long int client_queued;
225
226/* Maximum needed alignment. */
227extern const size_t block_align;
228
229/* Number of times a value is reloaded without being used. UINT_MAX
230 means unlimited. */
231extern unsigned int reload_count;
232
233/* Pagesize minus one. */
234extern uintptr_t pagesize_m1;
235
236/* Nonzero if paranoia mode is enabled. */
237extern int paranoia;
238/* Time after which the process restarts. */
239extern time_t restart_time;
240/* How much time between restarts. */
241extern time_t restart_interval;
242/* Old current working directory. */
243extern const char *oldcwd;
244/* Old user and group ID. */
245extern uid_t old_uid;
246extern 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 */
255extern void termination_handler (int signum) __attribute__ ((__noreturn__));
256extern int nscd_open_socket (void);
257void notify_parent (int child_ret);
258void do_exit (int child_ret, int errnum, const char *format, ...);
259
260/* connections.c */
261extern void nscd_init (void);
262extern void register_traced_file (size_t dbidx, struct traced_file *finfo);
263#ifdef HAVE_INOTIFY
264extern void install_watches (struct traced_file *finfo);
265#endif
266extern void close_sockets (void);
267extern void start_threads (void) __attribute__ ((__noreturn__));
268
269/* nscd_conf.c */
270extern int nscd_parse_file (const char *fname,
271 struct database_dyn dbs[lastdb]);
272
273/* nscd_stat.c */
274extern void send_stats (int fd, struct database_dyn dbs[lastdb]);
275extern int receive_print_stats (void) __attribute__ ((__noreturn__));
276
277/* cache.c */
278extern struct datahead *cache_search (request_type, const void *key,
279 size_t len, struct database_dyn *table,
280 uid_t owner);
281extern 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);
285extern time_t prune_cache (struct database_dyn *table, time_t now, int fd);
286
287/* pwdcache.c */
288extern void addpwbyname (struct database_dyn *db, int fd, request_header *req,
289 void *key, uid_t uid);
290extern void addpwbyuid (struct database_dyn *db, int fd, request_header *req,
291 void *key, uid_t uid);
292extern time_t readdpwbyname (struct database_dyn *db, struct hashentry *he,
293 struct datahead *dh);
294extern time_t readdpwbyuid (struct database_dyn *db, struct hashentry *he,
295 struct datahead *dh);
296
297/* grpcache.c */
298extern void addgrbyname (struct database_dyn *db, int fd, request_header *req,
299 void *key, uid_t uid);
300extern void addgrbygid (struct database_dyn *db, int fd, request_header *req,
301 void *key, uid_t uid);
302extern time_t readdgrbyname (struct database_dyn *db, struct hashentry *he,
303 struct datahead *dh);
304extern time_t readdgrbygid (struct database_dyn *db, struct hashentry *he,
305 struct datahead *dh);
306
307/* hstcache.c */
308extern void addhstbyname (struct database_dyn *db, int fd, request_header *req,
309 void *key, uid_t uid);
310extern void addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
311 void *key, uid_t uid);
312extern void addhstbynamev6 (struct database_dyn *db, int fd,
313 request_header *req, void *key, uid_t uid);
314extern void addhstbyaddrv6 (struct database_dyn *db, int fd,
315 request_header *req, void *key, uid_t uid);
316extern time_t readdhstbyname (struct database_dyn *db, struct hashentry *he,
317 struct datahead *dh);
318extern time_t readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
319 struct datahead *dh);
320extern time_t readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
321 struct datahead *dh);
322extern time_t readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
323 struct datahead *dh);
324
325/* aicache.c */
326extern void addhstai (struct database_dyn *db, int fd, request_header *req,
327 void *key, uid_t uid);
328extern time_t readdhstai (struct database_dyn *db, struct hashentry *he,
329 struct datahead *dh);
330
331
332/* initgrcache.c */
333extern void addinitgroups (struct database_dyn *db, int fd,
334 request_header *req, void *key, uid_t uid);
335extern time_t readdinitgroups (struct database_dyn *db, struct hashentry *he,
336 struct datahead *dh);
337
338/* servicecache.c */
339extern void addservbyname (struct database_dyn *db, int fd,
340 request_header *req, void *key, uid_t uid);
341extern time_t readdservbyname (struct database_dyn *db, struct hashentry *he,
342 struct datahead *dh);
343extern void addservbyport (struct database_dyn *db, int fd,
344 request_header *req, void *key, uid_t uid);
345extern time_t readdservbyport (struct database_dyn *db, struct hashentry *he,
346 struct datahead *dh);
347
348/* netgroupcache.c */
349extern void addinnetgr (struct database_dyn *db, int fd, request_header *req,
350 void *key, uid_t uid);
351extern time_t readdinnetgr (struct database_dyn *db, struct hashentry *he,
352 struct datahead *dh);
353extern void addgetnetgrent (struct database_dyn *db, int fd,
354 request_header *req, void *key, uid_t uid);
355extern time_t readdgetnetgrent (struct database_dyn *db, struct hashentry *he,
356 struct datahead *dh);
357
358/* mem.c */
359extern void *mempool_alloc (struct database_dyn *db, size_t len,
360 int data_alloc);
361extern void gc (struct database_dyn *db);
362
363
364/* nscd_setup_thread.c */
365extern int setup_thread (struct database_dyn *db);
366
367
368/* Special version of TEMP_FAILURE_RETRY for functions returning error
369 values. */
370#define TEMP_FAILURE_RETRY_VAL(expression) \
371 (__extension__ \
372 ({ long int __result; \
373 do __result = (long int) (expression); \
374 while (__result == EINTR); \
375 __result; }))
376
377#endif /* nscd.h */
378