db-netgrp.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Netgroup file parser in nss_db modules.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  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. 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. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <ctype.h>
  17. #include <dlfcn.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <netgroup.h>
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <libc-lock.h>
  24. #include <paths.h>
  25. #include <stdlib.h>
  26. #include "nsswitch.h"
  27. #include "nss_db.h"
  28. /* The hashing function we use. */
  29. #include "../intl/hash-string.h"
  30. #define DBFILE _PATH_VARDB "netgroup.db"
  31. /* Maintenance of the shared handle open on the database. */
  32. enum nss_status
  33. _nss_db_setnetgrent (const char *group, struct __netgrent *result)
  34. {
  35. struct nss_db_map state;
  36. enum nss_status status = internal_setent (DBFILE, &state);
  37. if (status == NSS_STATUS_SUCCESS)
  38. {
  39. const struct nss_db_header *header = state.header;
  40. const stridx_t *hashtable
  41. = (const stridx_t *) ((const char *) header
  42. + header->dbs[0].hashoffset);
  43. const char *valstrtab = (const char *) header + header->valstroffset;
  44. uint32_t hashval = __hash_string (group);
  45. size_t grouplen = strlen (group);
  46. size_t hidx = hashval % header->dbs[0].hashsize;
  47. size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2);
  48. status = NSS_STATUS_NOTFOUND;
  49. while (hashtable[hidx] != ~((stridx_t) 0))
  50. {
  51. const char *valstr = valstrtab + hashtable[hidx];
  52. if (strncmp (valstr, group, grouplen) == 0
  53. && isblank (valstr[grouplen]))
  54. {
  55. const char *cp = &valstr[grouplen + 1];
  56. while (isblank (*cp))
  57. ++cp;
  58. if (*cp != '\0')
  59. {
  60. result->data = strdup (cp);
  61. if (result->data == NULL)
  62. status = NSS_STATUS_TRYAGAIN;
  63. else
  64. {
  65. status = NSS_STATUS_SUCCESS;
  66. result->cursor = result->data;
  67. }
  68. break;
  69. }
  70. }
  71. if ((hidx += hval2) >= header->dbs[0].hashsize)
  72. hidx -= header->dbs[0].hashsize;
  73. }
  74. internal_endent (&state);
  75. }
  76. return status;
  77. }
  78. enum nss_status
  79. _nss_db_endnetgrent (struct __netgrent *result)
  80. {
  81. free (result->data);
  82. result->data = NULL;
  83. result->data_size = 0;
  84. result->cursor = NULL;
  85. return NSS_STATUS_SUCCESS;
  86. }
  87. extern enum nss_status _nss_netgroup_parseline (char **cursor,
  88. struct __netgrent *result,
  89. char *buffer, size_t buflen,
  90. int *errnop);
  91. enum nss_status
  92. _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
  93. int *errnop)
  94. {
  95. enum nss_status status;
  96. status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
  97. errnop);
  98. return status;
  99. }