Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
5.4 KiB

  1. /* $Source: /u/mark/src/pax/RCS/names.c,v $
  2. *
  3. * $Revision: 1.2 $
  4. *
  5. * names.c - Look up user and/or group names.
  6. *
  7. * DESCRIPTION
  8. *
  9. * These functions support UID and GID name lookup. The results are
  10. * cached to improve performance.
  11. *
  12. * AUTHOR
  13. *
  14. * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  15. *
  16. * Sponsored by The USENIX Association for public distribution.
  17. *
  18. * Copyright (c) 1989 Mark H. Colburn.
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms are permitted
  22. * provided that the above copyright notice is duplicated in all such
  23. * forms and that any documentation, advertising materials, and other
  24. * materials related to such distribution and use acknowledge that the
  25. * software was developed * by Mark H. Colburn and sponsored by The
  26. * USENIX Association.
  27. *
  28. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  29. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  31. *
  32. * $Log: names.c,v $
  33. * Revision 1.2 89/02/12 10:05:05 mark
  34. * 1.2 release fixes
  35. *
  36. * Revision 1.1 88/12/23 18:02:19 mark
  37. * Initial revision
  38. *
  39. */
  40. #ifndef lint
  41. static char *ident = "$Id: names.c,v 1.2 89/02/12 10:05:05 mark Exp $";
  42. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  43. #endif /* ! lint */
  44. /* Headers */
  45. #include "pax.h"
  46. /* Defines */
  47. #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
  48. #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
  49. /* Internal Identifiers */
  50. static int saveuid = -993;
  51. static char saveuname[TUNMLEN];
  52. static int my_uid = -993;
  53. static int savegid = -993;
  54. static char savegname[TGNMLEN];
  55. static int my_gid = -993;
  56. /* finduname - find a user or group name from a uid or gid
  57. *
  58. * DESCRIPTION
  59. *
  60. * Look up a user name from a uid/gid, maintaining a cache.
  61. *
  62. * PARAMETERS
  63. *
  64. * char uname[] - name (to be returned to user)
  65. * int uuid - id of name to find
  66. *
  67. *
  68. * RETURNS
  69. *
  70. * Returns a name which is associated with the user id given. If there
  71. * is not name which corresponds to the user-id given, then a pointer
  72. * to a string of zero length is returned.
  73. *
  74. * FIXME
  75. *
  76. * 1. for now it's a one-entry cache.
  77. * 2. The "-993" is to reduce the chance of a hit on the first lookup.
  78. */
  79. #ifdef __STDC__
  80. char *finduname(int uuid)
  81. #else
  82. char *finduname(uuid)
  83. int uuid;
  84. #endif
  85. {
  86. struct passwd *pw;
  87. #ifdef DF_TRACE_DEBUG
  88. printf("DF_TRACE_DEBUG: char *finduname() in names.c\n");
  89. #endif
  90. if (uuid != saveuid) {
  91. saveuid = uuid;
  92. saveuname[0] = '\0';
  93. pw = getpwuid(uuid);
  94. if (pw) {
  95. strncpy(saveuname, pw->pw_name, TUNMLEN);
  96. }
  97. }
  98. return(saveuname);
  99. }
  100. /* finduid - get the uid for a given user name
  101. *
  102. * DESCRIPTION
  103. *
  104. * This does just the opposit of finduname. Given a user name it
  105. * finds the corresponding UID for that name.
  106. *
  107. * PARAMETERS
  108. *
  109. * char uname[] - username to find a UID for
  110. *
  111. * RETURNS
  112. *
  113. * The UID which corresponds to the uname given, if any. If no UID
  114. * could be found, then the UID which corrsponds the user running the
  115. * program is returned.
  116. *
  117. */
  118. #ifdef __STDC__
  119. int finduid(char *uname)
  120. #else
  121. int finduid(uname)
  122. char *uname;
  123. #endif
  124. {
  125. struct passwd *pw;
  126. extern struct passwd *getpwnam();
  127. #ifdef DF_TRACE_DEBUG
  128. printf("DF_TRACE_DEBUG: int finduid() in names.c\n");
  129. #endif
  130. //printf ("myuid: %d\n", myuid);
  131. return (myuid);
  132. if (uname[0] != saveuname[0]/* Quick test w/o proc call */
  133. ||0 != strncmp(uname, saveuname, TUNMLEN)) {
  134. strncpy(saveuname, uname, TUNMLEN);
  135. pw = getpwnam(uname);
  136. if (pw) {
  137. saveuid = pw->pw_uid;
  138. } else {
  139. saveuid = myuid;
  140. }
  141. }
  142. return (saveuid);
  143. }
  144. /* findgname - look up a group name from a gid
  145. *
  146. * DESCRIPTION
  147. *
  148. * Look up a group name from a gid, maintaining a cache.
  149. *
  150. *
  151. * PARAMETERS
  152. *
  153. * int ggid - goupid of group to find
  154. *
  155. * RETURNS
  156. *
  157. * A string which is associated with the group ID given. If no name
  158. * can be found, a string of zero length is returned.
  159. */
  160. #ifdef __STDC__
  161. char *findgname(int ggid)
  162. #else
  163. char *findgname(ggid)
  164. int ggid;
  165. #endif
  166. {
  167. struct group *gr;
  168. #ifdef DF_TRACE_DEBUG
  169. printf("DF_TRACE_DEBUG: char *findgname() in names.c\n");
  170. #endif
  171. if (ggid != savegid) {
  172. savegid = ggid;
  173. savegname[0] = '\0';
  174. #ifndef _POSIX_SOURCE /* Xn */
  175. setgrent();
  176. #endif /* Xn */
  177. gr = getgrgid(ggid);
  178. if (gr) {
  179. strncpy(savegname, gr->gr_name, TGNMLEN);
  180. }
  181. }
  182. return(savegname);
  183. }
  184. /* findgid - get the gid for a given group name
  185. *
  186. * DESCRIPTION
  187. *
  188. * This does just the opposit of finduname. Given a group name it
  189. * finds the corresponding GID for that name.
  190. *
  191. * PARAMETERS
  192. *
  193. * char uname[] - groupname to find a GID for
  194. *
  195. * RETURNS
  196. *
  197. * The GID which corresponds to the uname given, if any. If no GID
  198. * could be found, then the GID which corrsponds the group running the
  199. * program is returned.
  200. *
  201. */
  202. #ifdef __STDC__
  203. int findgid(char *gname)
  204. #else
  205. int findgid(gname)
  206. char *gname;
  207. #endif
  208. {
  209. struct group *gr;
  210. /* Quick test w/o proc call */
  211. #ifdef DF_TRACE_DEBUG
  212. printf("DF_TRACE_DEBUG: int findgid() in names.c\n");
  213. #endif
  214. if (gname[0] != savegname[0] || strncmp(gname, savegname, TUNMLEN) != 0) {
  215. strncpy(savegname, gname, TUNMLEN);
  216. gr = getgrnam(gname);
  217. if (gr) {
  218. savegid = gr->gr_gid;
  219. } else {
  220. savegid = mygid;
  221. }
  222. }
  223. return (savegid);
  224. }