pwcache.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* $OpenBSD: pwcache.c,v 1.9 2005/08/08 08:05:34 espie Exp $ */
  2. /*
  3. * Copyright (c) 1989, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* OPENBSD ORIGINAL: lib/libc/gen/pwcache.c */
  31. #include "includes.h"
  32. #include <sys/types.h>
  33. #include <grp.h>
  34. #include <pwd.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #define NCACHE 64 /* power of 2 */
  39. #define MASK (NCACHE - 1) /* bits to store with */
  40. #ifndef HAVE_USER_FROM_UID
  41. char *
  42. user_from_uid(uid_t uid, int nouser)
  43. {
  44. static struct ncache {
  45. uid_t uid;
  46. char *name;
  47. } c_uid[NCACHE];
  48. static int pwopen;
  49. static char nbuf[15]; /* 32 bits == 10 digits */
  50. struct passwd *pw;
  51. struct ncache *cp;
  52. cp = c_uid + (uid & MASK);
  53. if (cp->uid != uid || cp->name == NULL) {
  54. if (pwopen == 0) {
  55. #ifdef HAVE_SETPASSENT
  56. setpassent(1);
  57. #endif
  58. pwopen = 1;
  59. }
  60. if ((pw = getpwuid(uid)) == NULL) {
  61. if (nouser)
  62. return (NULL);
  63. (void)snprintf(nbuf, sizeof(nbuf), "%lu", (u_long)uid);
  64. }
  65. cp->uid = uid;
  66. if (cp->name != NULL)
  67. free(cp->name);
  68. cp->name = strdup(pw ? pw->pw_name : nbuf);
  69. }
  70. return (cp->name);
  71. }
  72. #endif
  73. #ifndef HAVE_GROUP_FROM_GID
  74. char *
  75. group_from_gid(gid_t gid, int nogroup)
  76. {
  77. static struct ncache {
  78. gid_t gid;
  79. char *name;
  80. } c_gid[NCACHE];
  81. static int gropen;
  82. static char nbuf[15]; /* 32 bits == 10 digits */
  83. struct group *gr;
  84. struct ncache *cp;
  85. cp = c_gid + (gid & MASK);
  86. if (cp->gid != gid || cp->name == NULL) {
  87. if (gropen == 0) {
  88. #ifdef HAVE_SETGROUPENT
  89. setgroupent(1);
  90. #endif
  91. gropen = 1;
  92. }
  93. if ((gr = getgrgid(gid)) == NULL) {
  94. if (nogroup)
  95. return (NULL);
  96. (void)snprintf(nbuf, sizeof(nbuf), "%lu", (u_long)gid);
  97. }
  98. cp->gid = gid;
  99. if (cp->name != NULL)
  100. free(cp->name);
  101. cp->name = strdup(gr ? gr->gr_name : nbuf);
  102. }
  103. return (cp->name);
  104. }
  105. #endif