[df49c60] | 1 | /* |
---|
| 2 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
| 3 | * unrestricted use provided that this legend is included on all tape |
---|
| 4 | * media and as a part of the software program in whole or part. Users |
---|
| 5 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
| 6 | * to license or distribute it to anyone else except as part of a product or |
---|
| 7 | * program developed by the user or with the express written consent of |
---|
| 8 | * Sun Microsystems, Inc. |
---|
| 9 | * |
---|
| 10 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
| 11 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
| 12 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
| 13 | * |
---|
| 14 | * Sun RPC is provided with no support and without any obligation on the |
---|
| 15 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
| 16 | * modification or enhancement. |
---|
| 17 | * |
---|
| 18 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
| 19 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
| 20 | * OR ANY PART THEREOF. |
---|
| 21 | * |
---|
| 22 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
| 23 | * or profits or other special, indirect and consequential damages, even if |
---|
| 24 | * Sun has been advised of the possibility of such damages. |
---|
| 25 | * |
---|
| 26 | * Sun Microsystems, Inc. |
---|
| 27 | * 2550 Garcia Avenue |
---|
| 28 | * Mountain View, California 94043 |
---|
| 29 | */ |
---|
| 30 | #if !defined(lint) && defined(SCCSIDS) |
---|
| 31 | static char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro"; |
---|
| 32 | #endif |
---|
| 33 | /* |
---|
| 34 | * netname utility routines convert from unix names to network names and |
---|
| 35 | * vice-versa This module is operating system dependent! What we define here |
---|
| 36 | * will work with any unix system that has adopted the sun NIS domain |
---|
| 37 | * architecture. |
---|
| 38 | */ |
---|
| 39 | #include <sys/param.h> |
---|
| 40 | #include <rpc/rpc.h> |
---|
| 41 | #include <rpc/rpc_com.h> |
---|
| 42 | #ifdef YP |
---|
| 43 | #include <rpcsvc/yp_prot.h> |
---|
| 44 | #include <rpcsvc/ypclnt.h> |
---|
| 45 | #endif |
---|
| 46 | #include <ctype.h> |
---|
| 47 | #include <stdio.h> |
---|
| 48 | #include <grp.h> |
---|
| 49 | #include <pwd.h> |
---|
| 50 | #include <string.h> |
---|
| 51 | #include <stdlib.h> |
---|
| 52 | #include <unistd.h> |
---|
| 53 | |
---|
| 54 | static char *OPSYS = "unix"; |
---|
| 55 | #ifdef YP |
---|
| 56 | static char *NETID = "netid.byname"; |
---|
| 57 | #endif |
---|
| 58 | static char *NETIDFILE = "/etc/netid"; |
---|
| 59 | |
---|
| 60 | static int getnetid __P(( char *, char * )); |
---|
| 61 | static int _getgroups __P(( char *, gid_t * )); |
---|
| 62 | |
---|
| 63 | #ifndef NGROUPS |
---|
| 64 | #define NGROUPS 16 |
---|
| 65 | #endif |
---|
| 66 | |
---|
| 67 | /* |
---|
| 68 | * Convert network-name into unix credential |
---|
| 69 | */ |
---|
| 70 | int |
---|
| 71 | netname2user(netname, uidp, gidp, gidlenp, gidlist) |
---|
| 72 | char netname[MAXNETNAMELEN + 1]; |
---|
| 73 | uid_t *uidp; |
---|
| 74 | gid_t *gidp; |
---|
| 75 | int *gidlenp; |
---|
| 76 | gid_t *gidlist; |
---|
| 77 | { |
---|
| 78 | char *p; |
---|
| 79 | int gidlen; |
---|
| 80 | uid_t uid; |
---|
| 81 | long luid; |
---|
| 82 | struct passwd *pwd; |
---|
| 83 | char val[1024]; |
---|
| 84 | char *val1, *val2; |
---|
| 85 | char *domain; |
---|
| 86 | int vallen; |
---|
| 87 | int err; |
---|
| 88 | |
---|
| 89 | if (getnetid(netname, val)) { |
---|
| 90 | p = strtok(val, ":"); |
---|
| 91 | if (p == NULL) |
---|
| 92 | return (0); |
---|
| 93 | *uidp = (uid_t) atol(val); |
---|
| 94 | p = strtok(NULL, "\n,"); |
---|
| 95 | *gidp = (gid_t) atol(p); |
---|
| 96 | if (p == NULL) { |
---|
| 97 | return (0); |
---|
| 98 | } |
---|
| 99 | gidlen = 0; |
---|
| 100 | for (gidlen = 0; gidlen < NGROUPS; gidlen++) { |
---|
| 101 | p = strtok(NULL, "\n,"); |
---|
| 102 | if (p == NULL) |
---|
| 103 | break; |
---|
| 104 | gidlist[gidlen] = (gid_t) atol(p); |
---|
| 105 | } |
---|
| 106 | *gidlenp = gidlen; |
---|
| 107 | |
---|
| 108 | return (1); |
---|
| 109 | } |
---|
| 110 | val1 = strchr(netname, '.'); |
---|
| 111 | if (val1 == NULL) |
---|
| 112 | return (0); |
---|
| 113 | if (strncmp(netname, OPSYS, (val1-netname))) |
---|
| 114 | return (0); |
---|
| 115 | val1++; |
---|
| 116 | val2 = strchr(val1, '@'); |
---|
| 117 | if (val2 == NULL) |
---|
| 118 | return (0); |
---|
| 119 | vallen = val2 - val1; |
---|
| 120 | if (vallen > (1024 - 1)) |
---|
| 121 | vallen = 1024 - 1; |
---|
| 122 | (void) strncpy(val, val1, 1024); |
---|
| 123 | val[vallen] = 0; |
---|
| 124 | |
---|
| 125 | err = _rpc_get_default_domain(&domain); /* change to rpc */ |
---|
| 126 | if (err) |
---|
| 127 | return (0); |
---|
| 128 | |
---|
| 129 | if (strcmp(val2 + 1, domain)) |
---|
| 130 | return (0); /* wrong domain */ |
---|
| 131 | |
---|
| 132 | if (sscanf(val, "%ld", &luid) != 1) |
---|
| 133 | return (0); |
---|
| 134 | uid = luid; |
---|
| 135 | |
---|
| 136 | /* use initgroups method */ |
---|
| 137 | pwd = getpwuid(uid); |
---|
| 138 | if (pwd == NULL) |
---|
| 139 | return (0); |
---|
| 140 | *uidp = pwd->pw_uid; |
---|
| 141 | *gidp = pwd->pw_gid; |
---|
| 142 | *gidlenp = _getgroups(pwd->pw_name, gidlist); |
---|
| 143 | return (1); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /* |
---|
| 147 | * initgroups |
---|
| 148 | */ |
---|
| 149 | |
---|
| 150 | static int |
---|
| 151 | _getgroups(uname, groups) |
---|
| 152 | char *uname; |
---|
| 153 | gid_t groups[NGROUPS]; |
---|
| 154 | { |
---|
| 155 | gid_t ngroups = 0; |
---|
| 156 | register struct group *grp; |
---|
| 157 | register int i; |
---|
| 158 | register int j; |
---|
| 159 | int filter; |
---|
| 160 | |
---|
| 161 | setgrent(); |
---|
| 162 | while ((grp = getgrent())) { |
---|
| 163 | for (i = 0; grp->gr_mem[i]; i++) |
---|
| 164 | if (!strcmp(grp->gr_mem[i], uname)) { |
---|
| 165 | if (ngroups == NGROUPS) { |
---|
| 166 | #ifdef DEBUG |
---|
| 167 | fprintf(stderr, |
---|
| 168 | "initgroups: %s is in too many groups\n", uname); |
---|
| 169 | #endif |
---|
| 170 | goto toomany; |
---|
| 171 | } |
---|
| 172 | /* filter out duplicate group entries */ |
---|
| 173 | filter = 0; |
---|
| 174 | for (j = 0; j < ngroups; j++) |
---|
| 175 | if (groups[j] == grp->gr_gid) { |
---|
| 176 | filter++; |
---|
| 177 | break; |
---|
| 178 | } |
---|
| 179 | if (!filter) |
---|
| 180 | groups[ngroups++] = grp->gr_gid; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | toomany: |
---|
| 184 | endgrent(); |
---|
| 185 | return (ngroups); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /* |
---|
| 189 | * Convert network-name to hostname |
---|
| 190 | */ |
---|
| 191 | int |
---|
| 192 | netname2host(netname, hostname, hostlen) |
---|
| 193 | char netname[MAXNETNAMELEN + 1]; |
---|
| 194 | char *hostname; |
---|
| 195 | int hostlen; |
---|
| 196 | { |
---|
| 197 | int err; |
---|
| 198 | char valbuf[1024]; |
---|
| 199 | char *val; |
---|
| 200 | char *val2; |
---|
| 201 | int vallen; |
---|
| 202 | char *domain; |
---|
| 203 | |
---|
| 204 | if (getnetid(netname, valbuf)) { |
---|
| 205 | val = valbuf; |
---|
| 206 | if ((*val == '0') && (val[1] == ':')) { |
---|
| 207 | (void) strncpy(hostname, val + 2, hostlen); |
---|
| 208 | return (1); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | val = strchr(netname, '.'); |
---|
| 212 | if (val == NULL) |
---|
| 213 | return (0); |
---|
| 214 | if (strncmp(netname, OPSYS, (val - netname))) |
---|
| 215 | return (0); |
---|
| 216 | val++; |
---|
| 217 | val2 = strchr(val, '@'); |
---|
| 218 | if (val2 == NULL) |
---|
| 219 | return (0); |
---|
| 220 | vallen = val2 - val; |
---|
| 221 | if (vallen > (hostlen - 1)) |
---|
| 222 | vallen = hostlen - 1; |
---|
| 223 | (void) strncpy(hostname, val, vallen); |
---|
| 224 | hostname[vallen] = 0; |
---|
| 225 | |
---|
| 226 | err = _rpc_get_default_domain(&domain); /* change to rpc */ |
---|
| 227 | if (err) |
---|
| 228 | return (0); |
---|
| 229 | |
---|
| 230 | if (strcmp(val2 + 1, domain)) |
---|
| 231 | return (0); /* wrong domain */ |
---|
| 232 | else |
---|
| 233 | return (1); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /* |
---|
| 237 | * reads the file /etc/netid looking for a + to optionally go to the |
---|
| 238 | * network information service. |
---|
| 239 | */ |
---|
| 240 | int |
---|
| 241 | getnetid(key, ret) |
---|
| 242 | char *key, *ret; |
---|
| 243 | { |
---|
| 244 | char buf[1024]; /* big enough */ |
---|
| 245 | char *res; |
---|
| 246 | char *mkey; |
---|
| 247 | char *mval; |
---|
| 248 | FILE *fd; |
---|
| 249 | #ifdef YP |
---|
| 250 | char *domain; |
---|
| 251 | int err; |
---|
| 252 | char *lookup; |
---|
| 253 | int len; |
---|
| 254 | #endif |
---|
| 255 | |
---|
| 256 | fd = fopen(NETIDFILE, "r"); |
---|
| 257 | if (fd == (FILE *) 0) { |
---|
| 258 | #ifdef YP |
---|
| 259 | res = "+"; |
---|
| 260 | goto getnetidyp; |
---|
| 261 | #else |
---|
| 262 | return (0); |
---|
| 263 | #endif |
---|
| 264 | } |
---|
| 265 | for (;;) { |
---|
| 266 | if (fd == (FILE *) 0) |
---|
| 267 | return (0); /* getnetidyp brings us here */ |
---|
| 268 | res = fgets(buf, 1024, fd); |
---|
| 269 | if (res == 0) { |
---|
| 270 | fclose(fd); |
---|
| 271 | return (0); |
---|
| 272 | } |
---|
| 273 | if (res[0] == '#') |
---|
| 274 | continue; |
---|
| 275 | else if (res[0] == '+') { |
---|
| 276 | #ifdef YP |
---|
| 277 | getnetidyp: |
---|
| 278 | err = yp_get_default_domain(&domain); |
---|
| 279 | if (err) { |
---|
| 280 | continue; |
---|
| 281 | } |
---|
| 282 | lookup = NULL; |
---|
| 283 | err = yp_match(domain, NETID, key, |
---|
| 284 | strlen(key), &lookup, &len); |
---|
| 285 | if (err) { |
---|
| 286 | #ifdef DEBUG |
---|
| 287 | fprintf(stderr, "match failed error %d\n", err); |
---|
| 288 | #endif |
---|
| 289 | continue; |
---|
| 290 | } |
---|
| 291 | lookup[len] = 0; |
---|
| 292 | strcpy(ret, lookup); |
---|
| 293 | free(lookup); |
---|
| 294 | if (fd != NULL) |
---|
| 295 | fclose(fd); |
---|
| 296 | return (2); |
---|
| 297 | #else /* YP */ |
---|
| 298 | #ifdef DEBUG |
---|
| 299 | fprintf(stderr, |
---|
| 300 | "Bad record in %s '+' -- NIS not supported in this library copy\n", |
---|
| 301 | NETIDFILE); |
---|
| 302 | #endif |
---|
| 303 | continue; |
---|
| 304 | #endif /* YP */ |
---|
| 305 | } else { |
---|
| 306 | mkey = strtok(buf, "\t "); |
---|
| 307 | if (mkey == NULL) { |
---|
| 308 | fprintf(stderr, |
---|
| 309 | "Bad record in %s -- %s", NETIDFILE, buf); |
---|
| 310 | continue; |
---|
| 311 | } |
---|
| 312 | mval = strtok(NULL, " \t#\n"); |
---|
| 313 | if (mval == NULL) { |
---|
| 314 | fprintf(stderr, |
---|
| 315 | "Bad record in %s val problem - %s", NETIDFILE, buf); |
---|
| 316 | continue; |
---|
| 317 | } |
---|
| 318 | if (strcmp(mkey, key) == 0) { |
---|
| 319 | strcpy(ret, mval); |
---|
| 320 | fclose(fd); |
---|
| 321 | return (1); |
---|
| 322 | |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | } |
---|