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. 8: * 9: * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10: * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11: * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12: * 13: * Sun RPC is provided with no support and without any obligation on the 14: * part of Sun Microsystems, Inc. to assist in its use, correction, 15: * modification or enhancement. 16: * 17: * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18: * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19: * OR ANY PART THEREOF. 20: * 21: * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22: * or profits or other special, indirect and consequential damages, even if 23: * Sun has been advised of the possibility of such damages. 24: * 25: * Sun Microsystems, Inc. 26: * 2550 Garcia Avenue 27: * Mountain View, California 94043 28: */ 29: #ifndef lint 30: static char sccsid[] = "@(#)auth_unix.c 1.5 85/03/17 Copyr 1984 Sun Micro"; 31: #endif 32: 33: /* 34: * auth_unix.c, Implements UNIX style authentication parameters. 35: * 36: * Copyright (C) 1984, Sun Microsystems, Inc. 37: * 38: * The system is very weak. The client uses no encryption for it's 39: * credentials and only sends null verifiers. The server sends backs 40: * null verifiers or optionally a verifier that suggests a new short hand 41: * for the credentials. 42: * 43: */ 44: 45: #include <stdio.h> 46: #include "types.h" 47: #include <sys/time.h> 48: #include "xdr.h" 49: #include "auth.h" 50: #include "auth_unix.h" 51: char *malloc(); 52: 53: /* 54: * Unix authenticator operations vector 55: */ 56: static void authunix_nextverf(); 57: static bool_t authunix_marshal(); 58: static bool_t authunix_validate(); 59: static bool_t authunix_refresh(); 60: static void authunix_destroy(); 61: 62: static struct auth_ops auth_unix_ops = { 63: authunix_nextverf, 64: authunix_marshal, 65: authunix_validate, 66: authunix_refresh, 67: authunix_destroy 68: }; 69: 70: /* 71: * This struct is pointed to by the ah_private field of an auth_handle. 72: */ 73: struct audata { 74: struct opaque_auth au_origcred; /* original credentials */ 75: struct opaque_auth au_shcred; /* short hand cred */ 76: u_long au_shfaults; /* short hand cache faults */ 77: char au_marshed[MAX_AUTH_BYTES]; 78: u_int au_mpos; /* xdr pos at end of marshed */ 79: }; 80: #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private) 81: 82: static bool_t marshal_new_auth(); 83: 84: 85: /* 86: * Create a unix style authenticator. 87: * Returns an auth handle with the given stuff in it. 88: */ 89: AUTH * 90: authunix_create(machname, uid, gid, len, aup_gids) 91: char *machname; 92: int uid; 93: int gid; 94: register int len; 95: int *aup_gids; 96: { 97: struct authunix_parms aup; 98: char mymem[MAX_AUTH_BYTES]; 99: struct timeval now; 100: XDR xdrs; 101: register AUTH *auth; 102: register struct audata *au; 103: 104: /* 105: * Allocate and set up auth handle 106: */ 107: auth = (AUTH *)mem_alloc(sizeof(*auth)); 108: if (auth == NULL) { 109: fprintf(stderr, "authunix_create: out of memory\n"); 110: return (NULL); 111: } 112: au = (struct audata *)mem_alloc(sizeof(*au)); 113: if (au == NULL) { 114: fprintf(stderr, "authunix_create: out of memory\n"); 115: return (NULL); 116: } 117: auth->ah_ops = &auth_unix_ops; 118: auth->ah_private = (caddr_t)au; 119: auth->ah_verf = au->au_shcred = _null_auth; 120: au->au_shfaults = 0; 121: 122: /* 123: * fill in param struct from the given params 124: */ 125: (void)gettimeofday(&now, (struct timezone *)0); 126: aup.aup_time = now.tv_sec; 127: aup.aup_machname = machname; 128: aup.aup_uid = uid; 129: aup.aup_gid = gid; 130: aup.aup_len = (u_int)len; 131: aup.aup_gids = aup_gids; 132: 133: /* 134: * Serialize the parameters into origcred 135: */ 136: xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE); 137: if (! xdr_authunix_parms(&xdrs, &aup)) 138: abort(); 139: au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs); 140: au->au_origcred.oa_flavor = AUTH_UNIX; 141: if ((au->au_origcred.oa_base = mem_alloc(len)) == NULL) { 142: fprintf(stderr, "authunix_create: out of memory\n"); 143: return (NULL); 144: } 145: bcopy(mymem, au->au_origcred.oa_base, (u_int)len); 146: 147: /* 148: * set auth handle to reflect new cred. 149: */ 150: auth->ah_cred = au->au_origcred; 151: marshal_new_auth(auth); 152: return (auth); 153: } 154: 155: /* 156: * Returns an auth handle with parameters determined by doing lots of 157: * syscalls. 158: */ 159: AUTH * 160: authunix_create_default() 161: { 162: register int len; 163: char machname[MAX_MACHINE_NAME + 1]; 164: register int uid; 165: register int gid; 166: int gids[NGRPS]; 167: 168: if (gethostname(machname, MAX_MACHINE_NAME) == -1) 169: abort(); 170: machname[MAX_MACHINE_NAME] = 0; 171: uid = geteuid(); 172: gid = getegid(); 173: if ((len = getgroups(NGRPS, gids)) < 0) 174: abort(); 175: return (authunix_create(machname, uid, gid, len, gids)); 176: } 177: 178: /* 179: * authunix operations 180: */ 181: 182: static void 183: authunix_nextverf(auth) 184: AUTH *auth; 185: { 186: /* no action necessary */ 187: } 188: 189: static bool_t 190: authunix_marshal(auth, xdrs) 191: AUTH *auth; 192: XDR *xdrs; 193: { 194: register struct audata *au = AUTH_PRIVATE(auth); 195: 196: return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos)); 197: } 198: 199: static bool_t 200: authunix_validate(auth, verf) 201: register AUTH *auth; 202: struct opaque_auth verf; 203: { 204: register struct audata *au; 205: XDR xdrs; 206: 207: if (verf.oa_flavor == AUTH_SHORT) { 208: au = AUTH_PRIVATE(auth); 209: xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE); 210: 211: if (au->au_shcred.oa_base != NULL) { 212: mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); 213: au->au_shcred.oa_base = NULL; 214: } 215: if (xdr_opaque_auth(&xdrs, &au->au_shcred)) { 216: auth->ah_cred = au->au_shcred; 217: } else { 218: xdrs.x_op = XDR_FREE; 219: (void)xdr_opaque_auth(&xdrs, &au->au_shcred); 220: au->au_shcred.oa_base = NULL; 221: auth->ah_cred = au->au_origcred; 222: } 223: marshal_new_auth(auth); 224: } 225: return (TRUE); 226: } 227: 228: static bool_t 229: authunix_refresh(auth) 230: register AUTH *auth; 231: { 232: register struct audata *au = AUTH_PRIVATE(auth); 233: struct authunix_parms aup; 234: struct timeval now; 235: XDR xdrs; 236: register int stat; 237: 238: if (auth->ah_cred.oa_base == au->au_origcred.oa_base) { 239: /* there is no hope. Punt */ 240: return (FALSE); 241: } 242: au->au_shfaults ++; 243: 244: /* first deserialize the creds back into a struct authunix_parms */ 245: aup.aup_machname = NULL; 246: aup.aup_gids = (int *)NULL; 247: xdrmem_create(&xdrs, au->au_origcred.oa_base, 248: au->au_origcred.oa_length, XDR_DECODE); 249: stat = xdr_authunix_parms(&xdrs, &aup); 250: if (! stat) 251: goto done; 252: 253: /* update the time and serialize in place */ 254: (void)gettimeofday(&now, (struct timezone *)0); 255: aup.aup_time = now.tv_sec; 256: xdrs.x_op = XDR_ENCODE; 257: XDR_SETPOS(&xdrs, 0); 258: stat = xdr_authunix_parms(&xdrs, &aup); 259: if (! stat) 260: goto done; 261: auth->ah_cred = au->au_origcred; 262: marshal_new_auth(auth); 263: done: 264: /* free the struct authunix_parms created by deserializing */ 265: xdrs.x_op = XDR_FREE; 266: (void)xdr_authunix_parms(&xdrs, &aup); 267: XDR_DESTROY(&xdrs); 268: return (stat); 269: } 270: 271: static void 272: authunix_destroy(auth) 273: register AUTH *auth; 274: { 275: register struct audata *au = AUTH_PRIVATE(auth); 276: 277: mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length); 278: 279: if (au->au_shcred.oa_base != NULL) 280: mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); 281: 282: mem_free(auth->ah_private, sizeof(struct audata)); 283: 284: if (auth->ah_verf.oa_base != NULL) 285: mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length); 286: 287: mem_free((caddr_t)auth, sizeof(*auth)); 288: 289: } 290: 291: /* 292: * Marshals (pre-serializes) an auth struct. 293: * sets private data, au_marshed and au_mpos 294: */ 295: static bool_t 296: marshal_new_auth(auth) 297: register AUTH *auth; 298: { 299: XDR xdr_stream; 300: register XDR *xdrs = &xdr_stream; 301: register struct audata *au = AUTH_PRIVATE(auth); 302: 303: xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE); 304: if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) || 305: (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) { 306: } else { 307: au->au_mpos = XDR_GETPOS(xdrs); 308: } 309: XDR_DESTROY(xdrs); 310: }