1 # 2 3 4 /* 5 * Getname / getuserid for those with no 6 * hashed passwd data base). 7 * Do not compile this module in if you DO have hashed 8 * passwd's -- this is slower. 9 * 10 * Also provided here is a getpw routine which can share 11 * the open file. This is used for the Version 6 getenv 12 * implementation. 13 */ 14 15 static char *SccsId = "@(#)getname.c 1.1 10/08/80"; 16 17 #include "rcv.h" 18 19 static FILE *pwfile = NULL; /* Pw file held open */ 20 static char *pwname = "/etc/passwd"; /* Name of passwd file */ 21 22 /* 23 * Search the passwd file for a uid. Return name through ref parameter 24 * if found, indicating success with 0 return. Return -1 on error. 25 * If -1 is passed as the user id, close the passwd file. 26 */ 27 28 getname(uid, namebuf) 29 char namebuf[]; 30 { 31 register char *cp, *cp2; 32 char linebuf[BUFSIZ]; 33 34 if (uid == -1) { 35 if (pwfile != NULL) 36 fclose(pwfile); 37 pwfile = NULL; 38 return(0); 39 } 40 if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL) 41 return(-1); 42 rewind(pwfile); 43 while (fgets(linebuf, BUFSIZ, pwfile) != NULL) 44 if (pweval(linebuf) == uid) { 45 for (cp = linebuf, cp2 = namebuf; *cp != ':'; 46 *cp2++ = *cp++) 47 ; 48 *cp2 = '\0'; 49 return(0); 50 } 51 return(-1); 52 } 53 54 /* 55 * Read the users password file line into the passed line 56 * buffer. 57 */ 58 59 getpw(uid, linebuf) 60 char linebuf[]; 61 { 62 register char *cp, *cp2; 63 64 if (uid == -1) { 65 if (pwfile != NULL) 66 fclose(pwfile); 67 pwfile = NULL; 68 return(0); 69 } 70 if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL) 71 return(-1); 72 rewind(pwfile); 73 while (fgets(linebuf, BUFSIZ, pwfile) != NULL) 74 if (pweval(linebuf) == uid) { 75 if (linebuf[0] != '\0') 76 linebuf[strlen(linebuf)-1] = '\0'; 77 return(0); 78 } 79 return(-1); 80 } 81 82 /* 83 * Look for passwd line belonging to 'name' 84 */ 85 86 getpwnam(name, linebuf) 87 char name[], linebuf[]; 88 { 89 register char *cp, *cp2; 90 91 if (name == NOSTR) { 92 if (pwfile != NULL) 93 fclose(pwfile); 94 pwfile = NULL; 95 return(0); 96 } 97 if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL) { 98 perror(pwname); 99 return(-1); 100 } 101 rewind(pwfile); 102 while (fgets(linebuf, BUFSIZ, pwfile) != NULL) { 103 cp = linebuf; 104 cp2 = name; 105 while (*cp2++ == *cp++) 106 ; 107 if (*--cp == ':' && *--cp2 == 0) 108 return(0); 109 } 110 return(-1); 111 } 112 113 /* 114 * Convert the passed name to a user id and return it. Return -1 115 * on error. Iff the name passed is -1 (yech) close the pwfile. 116 */ 117 118 getuserid(name) 119 char name[]; 120 { 121 register char *cp, *cp2; 122 char linebuf[BUFSIZ]; 123 124 if (name == (char *) -1) { 125 if (pwfile != NULL) 126 fclose(pwfile); 127 pwfile = NULL; 128 return(0); 129 } 130 if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL) 131 return(-1); 132 rewind(pwfile); 133 while (fgets(linebuf, BUFSIZ, pwfile) != NULL) { 134 for (cp = name, cp2 = linebuf; *cp++ == *cp2++;) 135 ; 136 if (*--cp == '\0' && *--cp2 == ':') 137 return(pweval(linebuf)); 138 } 139 return(-1); 140 } 141 142 /* 143 * Evaluate the user id of the passed passwd line and return it. 144 */ 145 146 static 147 pweval(line) 148 char line[]; 149 { 150 register char *cp; 151 register int i; 152 register int uid; 153 154 for (cp = line, i = 0; i < 2; i += (*cp++ == ':')) 155 ; 156 uid = atoi(cp); 157 158 #ifdef UIDGID 159 while (*cp && *cp != ':') 160 cp++; 161 cp++; 162 uid |= atoi(cp) << 8; 163 #endif 164 165 return(uid); 166 } 167