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