|
|
1.1 ! root 1: /* $Source: /src386/usr/bin/pax/names.c,v $ ! 2: * ! 3: * $Revision: 1.1 $ ! 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 ([email protected]) ! 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.1 92/08/28 08:02:21 bin ! 34: * Initial revision ! 35: * ! 36: * Revision 1.1 89/02/14 16:48:03 jep ! 37: * Initial revision ! 38: * ! 39: * Revision 1.1 88/12/23 18:02:19 mark ! 40: * Initial revision ! 41: * ! 42: */ ! 43: ! 44: #ifndef lint ! 45: static char *ident = "$Id: names.c,v 1.1 92/08/28 08:02:21 bin Exp Locker: bin $"; ! 46: static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n"; ! 47: #endif /* ! lint */ ! 48: ! 49: ! 50: /* Headers */ ! 51: ! 52: #include "pax.h" ! 53: ! 54: ! 55: /* Defines */ ! 56: ! 57: #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid ) ! 58: #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid ) ! 59: ! 60: ! 61: /* Internal Identifiers */ ! 62: ! 63: static int saveuid = -993; ! 64: static char saveuname[TUNMLEN]; ! 65: static int my_uid = -993; ! 66: ! 67: static int savegid = -993; ! 68: static char savegname[TGNMLEN]; ! 69: static int my_gid = -993; ! 70: ! 71: ! 72: /* finduname - find a user or group name from a uid or gid ! 73: * ! 74: * DESCRIPTION ! 75: * ! 76: * Look up a user name from a uid/gid, maintaining a cache. ! 77: * ! 78: * PARAMETERS ! 79: * ! 80: * char uname[] - name (to be returned to user) ! 81: * int uuid - id of name to find ! 82: * ! 83: * ! 84: * RETURNS ! 85: * ! 86: * Returns a name which is associated with the user id given. If there ! 87: * is not name which corresponds to the user-id given, then a pointer ! 88: * to a string of zero length is returned. ! 89: * ! 90: * FIXME ! 91: * ! 92: * 1. for now it's a one-entry cache. ! 93: * 2. The "-993" is to reduce the chance of a hit on the first lookup. ! 94: */ ! 95: ! 96: #if __STDC__ ! 97: ! 98: char *finduname(int uuid) ! 99: ! 100: #else ! 101: ! 102: char *finduname(uuid) ! 103: int uuid; ! 104: ! 105: #endif ! 106: { ! 107: struct passwd *pw; ! 108: ! 109: if (uuid != saveuid) { ! 110: saveuid = uuid; ! 111: saveuname[0] = '\0'; ! 112: pw = getpwuid(uuid); ! 113: if (pw) { ! 114: strncpy(saveuname, pw->pw_name, TUNMLEN); ! 115: } ! 116: } ! 117: return(saveuname); ! 118: } ! 119: ! 120: ! 121: /* finduid - get the uid for a given user name ! 122: * ! 123: * DESCRIPTION ! 124: * ! 125: * This does just the opposit of finduname. Given a user name it ! 126: * finds the corresponding UID for that name. ! 127: * ! 128: * PARAMETERS ! 129: * ! 130: * char uname[] - username to find a UID for ! 131: * ! 132: * RETURNS ! 133: * ! 134: * The UID which corresponds to the uname given, if any. If no UID ! 135: * could be found, then the UID which corrsponds the user running the ! 136: * program is returned. ! 137: * ! 138: */ ! 139: ! 140: #if __STDC__ ! 141: ! 142: int finduid(char *uname) ! 143: ! 144: #else ! 145: ! 146: int finduid(uname) ! 147: char *uname; ! 148: ! 149: #endif ! 150: { ! 151: struct passwd *pw; ! 152: extern struct passwd *getpwnam(); ! 153: ! 154: if (uname[0] != saveuname[0]/* Quick test w/o proc call */ ! 155: ||0 != strncmp(uname, saveuname, TUNMLEN)) { ! 156: strncpy(saveuname, uname, TUNMLEN); ! 157: pw = getpwnam(uname); ! 158: if (pw) { ! 159: saveuid = pw->pw_uid; ! 160: } else { ! 161: saveuid = myuid; ! 162: } ! 163: } ! 164: return (saveuid); ! 165: } ! 166: ! 167: ! 168: /* findgname - look up a group name from a gid ! 169: * ! 170: * DESCRIPTION ! 171: * ! 172: * Look up a group name from a gid, maintaining a cache. ! 173: * ! 174: * ! 175: * PARAMETERS ! 176: * ! 177: * int ggid - goupid of group to find ! 178: * ! 179: * RETURNS ! 180: * ! 181: * A string which is associated with the group ID given. If no name ! 182: * can be found, a string of zero length is returned. ! 183: */ ! 184: ! 185: #if __STDC__ ! 186: ! 187: char *findgname(int ggid) ! 188: ! 189: #else ! 190: ! 191: char *findgname(ggid) ! 192: int ggid; ! 193: ! 194: #endif ! 195: { ! 196: struct group *gr; ! 197: ! 198: if (ggid != savegid) { ! 199: savegid = ggid; ! 200: savegname[0] = '\0'; ! 201: setgrent(); ! 202: gr = getgrgid(ggid); ! 203: if (gr) { ! 204: strncpy(savegname, gr->gr_name, TGNMLEN); ! 205: } ! 206: } ! 207: return(savegname); ! 208: } ! 209: ! 210: ! 211: ! 212: /* findgid - get the gid for a given group name ! 213: * ! 214: * DESCRIPTION ! 215: * ! 216: * This does just the opposit of finduname. Given a group name it ! 217: * finds the corresponding GID for that name. ! 218: * ! 219: * PARAMETERS ! 220: * ! 221: * char uname[] - groupname to find a GID for ! 222: * ! 223: * RETURNS ! 224: * ! 225: * The GID which corresponds to the uname given, if any. If no GID ! 226: * could be found, then the GID which corrsponds the group running the ! 227: * program is returned. ! 228: * ! 229: */ ! 230: ! 231: #if __STDC__ ! 232: ! 233: int findgid(char *gname) ! 234: ! 235: #else ! 236: ! 237: int findgid(gname) ! 238: char *gname; ! 239: ! 240: #endif ! 241: { ! 242: struct group *gr; ! 243: ! 244: /* Quick test w/o proc call */ ! 245: if (gname[0] != savegname[0] || strncmp(gname, savegname, TUNMLEN) != 0) { ! 246: strncpy(savegname, gname, TUNMLEN); ! 247: gr = getgrnam(gname); ! 248: if (gr) { ! 249: savegid = gr->gr_gid; ! 250: } else { ! 251: savegid = mygid; ! 252: } ! 253: } ! 254: return (savegid); ! 255: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.