nscd-client.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright (c) 1998-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /* This file defines everything that client code should need to
  15. know to talk to the nscd daemon. */
  16. #ifndef _NSCD_CLIENT_H
  17. #define _NSCD_CLIENT_H 1
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <sys/types.h>
  23. #include <atomic.h>
  24. #include <nscd-types.h>
  25. #include <sys/uio.h>
  26. /* Version number of the daemon interface */
  27. #define NSCD_VERSION 2
  28. /* Path of the file where the PID of the running system is stored. */
  29. #define _PATH_NSCDPID "/var/run/nscd/nscd.pid"
  30. /* Path for the Unix domain socket. */
  31. #define _PATH_NSCDSOCKET "/var/run/nscd/socket"
  32. /* Path for the configuration file. */
  33. #define _PATH_NSCDCONF "/etc/nscd.conf"
  34. /* Maximum allowed length for the key. */
  35. #define MAXKEYLEN 1024
  36. /* Available services. */
  37. typedef enum
  38. {
  39. GETPWBYNAME,
  40. GETPWBYUID,
  41. GETGRBYNAME,
  42. GETGRBYGID,
  43. GETHOSTBYNAME,
  44. GETHOSTBYNAMEv6,
  45. GETHOSTBYADDR,
  46. GETHOSTBYADDRv6,
  47. SHUTDOWN, /* Shut the server down. */
  48. GETSTAT, /* Get the server statistic. */
  49. INVALIDATE, /* Invalidate one special cache. */
  50. GETFDPW,
  51. GETFDGR,
  52. GETFDHST,
  53. GETAI,
  54. INITGROUPS,
  55. GETSERVBYNAME,
  56. GETSERVBYPORT,
  57. GETFDSERV,
  58. GETNETGRENT,
  59. INNETGR,
  60. GETFDNETGR,
  61. LASTREQ
  62. } request_type;
  63. /* Header common to all requests */
  64. typedef struct
  65. {
  66. int32_t version; /* Version number of the daemon interface. */
  67. request_type type; /* Service requested. */
  68. int32_t key_len; /* Key length. */
  69. } request_header;
  70. /* Structure sent in reply to password query. Note that this struct is
  71. sent also if the service is disabled or there is no record found. */
  72. typedef struct
  73. {
  74. int32_t version;
  75. int32_t found;
  76. nscd_ssize_t pw_name_len;
  77. nscd_ssize_t pw_passwd_len;
  78. uid_t pw_uid;
  79. gid_t pw_gid;
  80. nscd_ssize_t pw_gecos_len;
  81. nscd_ssize_t pw_dir_len;
  82. nscd_ssize_t pw_shell_len;
  83. } pw_response_header;
  84. /* Structure sent in reply to group query. Note that this struct is
  85. sent also if the service is disabled or there is no record found. */
  86. typedef struct
  87. {
  88. int32_t version;
  89. int32_t found;
  90. nscd_ssize_t gr_name_len;
  91. nscd_ssize_t gr_passwd_len;
  92. gid_t gr_gid;
  93. nscd_ssize_t gr_mem_cnt;
  94. } gr_response_header;
  95. /* Structure sent in reply to host query. Note that this struct is
  96. sent also if the service is disabled or there is no record found. */
  97. typedef struct
  98. {
  99. int32_t version;
  100. int32_t found;
  101. nscd_ssize_t h_name_len;
  102. nscd_ssize_t h_aliases_cnt;
  103. int32_t h_addrtype;
  104. int32_t h_length;
  105. nscd_ssize_t h_addr_list_cnt;
  106. int32_t error;
  107. } hst_response_header;
  108. /* Structure sent in reply to addrinfo query. Note that this struct is
  109. sent also if the service is disabled or there is no record found. */
  110. typedef struct
  111. {
  112. int32_t version;
  113. int32_t found;
  114. nscd_ssize_t naddrs;
  115. nscd_ssize_t addrslen;
  116. nscd_ssize_t canonlen;
  117. int32_t error;
  118. } ai_response_header;
  119. /* Structure filled in by __nscd_getai. */
  120. struct nscd_ai_result
  121. {
  122. int naddrs;
  123. char *canon;
  124. uint8_t *family;
  125. char *addrs;
  126. };
  127. /* Structure sent in reply to initgroups query. Note that this struct is
  128. sent also if the service is disabled or there is no record found. */
  129. typedef struct
  130. {
  131. int32_t version;
  132. int32_t found;
  133. nscd_ssize_t ngrps;
  134. } initgr_response_header;
  135. /* Structure sent in reply to services query. Note that this struct is
  136. sent also if the service is disabled or there is no record found. */
  137. typedef struct
  138. {
  139. int32_t version;
  140. int32_t found;
  141. nscd_ssize_t s_name_len;
  142. nscd_ssize_t s_proto_len;
  143. nscd_ssize_t s_aliases_cnt;
  144. int32_t s_port;
  145. } serv_response_header;
  146. /* Structure send in reply to netgroup query. Note that this struct is
  147. sent also if the service is disabled or there is no record found. */
  148. typedef struct
  149. {
  150. int32_t version;
  151. int32_t found;
  152. nscd_ssize_t nresults;
  153. nscd_ssize_t result_len;
  154. } netgroup_response_header;
  155. typedef struct
  156. {
  157. int32_t version;
  158. int32_t found;
  159. int32_t result;
  160. } innetgroup_response_header;
  161. /* Type for offsets in data part of database. */
  162. typedef uint32_t ref_t;
  163. /* Value for invalid/no reference. */
  164. #define ENDREF UINT32_MAX
  165. /* Timestamp type. */
  166. typedef uint64_t nscd_time_t;
  167. /* Maximum timestamp. */
  168. #define MAX_TIMEOUT_VALUE \
  169. (sizeof (time_t) == sizeof (long int) ? LONG_MAX : INT_MAX)
  170. /* Alignment requirement of the beginning of the data region. */
  171. #define ALIGN 16
  172. /* Head of record in data part of database. */
  173. struct datahead
  174. {
  175. nscd_ssize_t allocsize; /* Allocated Bytes. */
  176. nscd_ssize_t recsize; /* Size of the record. */
  177. nscd_time_t timeout; /* Time when this entry becomes invalid. */
  178. uint8_t notfound; /* Nonzero if data has not been found. */
  179. uint8_t nreloads; /* Reloads without use. */
  180. uint8_t usable; /* False if the entry must be ignored. */
  181. uint8_t unused; /* Unused. */
  182. uint32_t ttl; /* TTL value used. */
  183. /* We need to have the following element aligned for the response
  184. header data types and their use in the 'struct dataset' types
  185. defined in the XXXcache.c files. */
  186. union
  187. {
  188. pw_response_header pwdata;
  189. gr_response_header grdata;
  190. hst_response_header hstdata;
  191. ai_response_header aidata;
  192. initgr_response_header initgrdata;
  193. serv_response_header servdata;
  194. netgroup_response_header netgroupdata;
  195. innetgroup_response_header innetgroupdata;
  196. nscd_ssize_t align1;
  197. nscd_time_t align2;
  198. } data[0];
  199. };
  200. static inline time_t
  201. datahead_init_common (struct datahead *head, nscd_ssize_t allocsize,
  202. nscd_ssize_t recsize, uint32_t ttl)
  203. {
  204. /* Initialize so that we don't write out junk in uninitialized data to the
  205. cache. */
  206. memset (head, 0, sizeof (*head));
  207. head->allocsize = allocsize;
  208. head->recsize = recsize;
  209. head->usable = true;
  210. head->ttl = ttl;
  211. /* Compute and return the timeout time. */
  212. return head->timeout = time (NULL) + ttl;
  213. }
  214. static inline time_t
  215. datahead_init_pos (struct datahead *head, nscd_ssize_t allocsize,
  216. nscd_ssize_t recsize, uint8_t nreloads, uint32_t ttl)
  217. {
  218. time_t ret = datahead_init_common (head, allocsize, recsize, ttl);
  219. head->notfound = false;
  220. head->nreloads = nreloads;
  221. return ret;
  222. }
  223. static inline time_t
  224. datahead_init_neg (struct datahead *head, nscd_ssize_t allocsize,
  225. nscd_ssize_t recsize, uint32_t ttl)
  226. {
  227. time_t ret = datahead_init_common (head, allocsize, recsize, ttl);
  228. /* We don't need to touch nreloads here since it is set to our desired value
  229. (0) when we clear the structure. */
  230. head->notfound = true;
  231. return ret;
  232. }
  233. /* Structure for one hash table entry. */
  234. struct hashentry
  235. {
  236. request_type type:8; /* Which type of dataset. */
  237. bool first; /* True if this was the original key. */
  238. nscd_ssize_t len; /* Length of key. */
  239. ref_t key; /* Pointer to key. */
  240. int32_t owner; /* If secure table, this is the owner. */
  241. ref_t next; /* Next entry in this hash bucket list. */
  242. ref_t packet; /* Records for the result. */
  243. union
  244. {
  245. struct hashentry *dellist; /* Next record to be deleted. This can be a
  246. pointer since only nscd uses this field. */
  247. ref_t *prevp; /* Pointer to field containing forward
  248. reference. */
  249. };
  250. };
  251. /* Current persistent database version. */
  252. #define DB_VERSION 2
  253. /* Maximum time allowed between updates of the timestamp. */
  254. #define MAPPING_TIMEOUT (5 * 60)
  255. /* Used indices for the EXTRA_DATA element of 'database_pers_head'.
  256. Each database has its own indices. */
  257. #define NSCD_HST_IDX_CONF_TIMESTAMP 0
  258. /* Header of persistent database file. */
  259. struct database_pers_head
  260. {
  261. int32_t version;
  262. int32_t header_size;
  263. volatile int32_t gc_cycle;
  264. volatile int32_t nscd_certainly_running;
  265. volatile nscd_time_t timestamp;
  266. /* Room for extensions. */
  267. volatile uint32_t extra_data[4];
  268. nscd_ssize_t module;
  269. nscd_ssize_t data_size;
  270. nscd_ssize_t first_free; /* Offset of first free byte in data area. */
  271. nscd_ssize_t nentries;
  272. nscd_ssize_t maxnentries;
  273. nscd_ssize_t maxnsearched;
  274. uint64_t poshit;
  275. uint64_t neghit;
  276. uint64_t posmiss;
  277. uint64_t negmiss;
  278. uint64_t rdlockdelayed;
  279. uint64_t wrlockdelayed;
  280. uint64_t addfailed;
  281. ref_t array[0];
  282. };
  283. /* Mapped database record. */
  284. struct mapped_database
  285. {
  286. const struct database_pers_head *head;
  287. const char *data;
  288. size_t mapsize;
  289. int counter; /* > 0 indicates it is usable. */
  290. size_t datasize;
  291. };
  292. #define NO_MAPPING ((struct mapped_database *) -1l)
  293. struct locked_map_ptr
  294. {
  295. int lock;
  296. struct mapped_database *mapped;
  297. };
  298. #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name
  299. /* Try acquiring lock for mapptr, returns true if it succeeds, false
  300. if not. */
  301. static inline bool
  302. __nscd_acquire_maplock (volatile struct locked_map_ptr *mapptr)
  303. {
  304. int cnt = 0;
  305. while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr->lock,
  306. 1, 0) != 0, 0))
  307. {
  308. // XXX Best number of rounds?
  309. if (__glibc_unlikely (++cnt > 5))
  310. return false;
  311. atomic_spin_nop ();
  312. }
  313. return true;
  314. }
  315. /* Open socket connection to nscd server. */
  316. extern int __nscd_open_socket (const char *key, size_t keylen,
  317. request_type type, void *response,
  318. size_t responselen) attribute_hidden;
  319. /* Try to get a file descriptor for the shared memory segment
  320. containing the database. */
  321. extern struct mapped_database *__nscd_get_mapping (request_type type,
  322. const char *key,
  323. struct mapped_database **mappedp) attribute_hidden;
  324. /* Get reference of mapping. */
  325. extern struct mapped_database *__nscd_get_map_ref (request_type type,
  326. const char *name,
  327. volatile struct locked_map_ptr *mapptr,
  328. int *gc_cyclep)
  329. attribute_hidden;
  330. /* Unmap database. */
  331. extern void __nscd_unmap (struct mapped_database *mapped)
  332. attribute_hidden;
  333. /* Drop reference of mapping. */
  334. static int
  335. __attribute__ ((unused))
  336. __nscd_drop_map_ref (struct mapped_database *map, int *gc_cycle)
  337. {
  338. if (map != NO_MAPPING)
  339. {
  340. int now_cycle = map->head->gc_cycle;
  341. if (__glibc_unlikely (now_cycle != *gc_cycle))
  342. {
  343. /* We might have read inconsistent data. */
  344. *gc_cycle = now_cycle;
  345. return -1;
  346. }
  347. if (atomic_fetch_add_relaxed (&map->counter, -1) == 1)
  348. __nscd_unmap (map);
  349. }
  350. return 0;
  351. }
  352. /* Search the mapped database. */
  353. extern struct datahead *__nscd_cache_search (request_type type,
  354. const char *key,
  355. size_t keylen,
  356. const struct mapped_database *mapped,
  357. size_t datalen)
  358. attribute_hidden;
  359. /* Wrappers around read, readv and write that only read/write less than LEN
  360. bytes on error or EOF. */
  361. extern ssize_t __readall (int fd, void *buf, size_t len)
  362. attribute_hidden;
  363. extern ssize_t __readvall (int fd, const struct iovec *iov, int iovcnt)
  364. attribute_hidden;
  365. extern ssize_t writeall (int fd, const void *buf, size_t len)
  366. attribute_hidden;
  367. /* Get netlink timestamp counter from mapped area or zero. */
  368. extern uint32_t __nscd_get_nl_timestamp (void)
  369. attribute_hidden;
  370. #endif /* nscd.h */