1 /* $NetBSD: auth_unix.c,v 1.16 2000/01/22 22:19:17 mycroft 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 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char *sccsid = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro"; 36 static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC"; 37 #else 38 __RCSID("$NetBSD: auth_unix.c,v 1.16 2000/01/22 22:19:17 mycroft Exp $"); 39 #endif 40 #endif 41 42 /* 43 * auth_unix.c, Implements UNIX style authentication parameters. 44 * 45 * Copyright (C) 1984, Sun Microsystems, Inc. 46 * 47 * The system is very weak. The client uses no encryption for it's 48 * credentials and only sends null verifiers. The server sends backs 49 * null verifiers or optionally a verifier that suggests a new short hand 50 * for the credentials. 51 * 52 */ 53 54 #include "namespace.h" 55 56 #include <sys/param.h> 57 58 #include <assert.h> 59 #include <err.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include <rpc/types.h> 66 #include <rpc/xdr.h> 67 #include <rpc/auth.h> 68 #include <rpc/auth_unix.h> 69 70 #ifdef __weak_alias 71 __weak_alias(authunix_create,_authunix_create) 72 __weak_alias(authunix_create_default,_authunix_create_default) 73 #endif 74 75 76 /* auth_unix.c */ 77 static void authunix_nextverf __P((AUTH *)); 78 static bool_t authunix_marshal __P((AUTH *, XDR *)); 79 static bool_t authunix_validate __P((AUTH *, struct opaque_auth *)); 80 static bool_t authunix_refresh __P((AUTH *)); 81 static void authunix_destroy __P((AUTH *)); 82 static void marshal_new_auth __P((AUTH *)); 83 84 /* 85 * Unix authenticator operations vector 86 */ 87 static const struct auth_ops auth_unix_ops = { 88 authunix_nextverf, 89 authunix_marshal, 90 authunix_validate, 91 authunix_refresh, 92 authunix_destroy 93 }; 94 95 /* 96 * This struct is pointed to by the ah_private field of an auth_handle. 97 */ 98 struct audata { 99 struct opaque_auth au_origcred; /* original credentials */ 100 struct opaque_auth au_shcred; /* short hand cred */ 101 u_long au_shfaults; /* short hand cache faults */ 102 char au_marshed[MAX_AUTH_BYTES]; 103 u_int au_mpos; /* xdr pos at end of marshed */ 104 }; 105 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private) 106 107 /* 108 * Create a unix style authenticator. 109 * Returns an auth handle with the given stuff in it. 110 */ 111 AUTH * 112 authunix_create(machname, uid, gid, len, aup_gids) 113 char *machname; 114 int uid; 115 int gid; 116 int len; 117 int *aup_gids; 118 { 119 struct authunix_parms aup; 120 char mymem[MAX_AUTH_BYTES]; 121 struct timeval now; 122 XDR xdrs; 123 AUTH *auth; 124 struct audata *au; 125 126 /* 127 * Allocate and set up auth handle 128 */ 129 au = NULL; 130 auth = (AUTH *)mem_alloc(sizeof(*auth)); 131 #ifndef KERNEL 132 if (auth == NULL) { 133 warnx("authunix_create: out of memory"); 134 goto cleanup_authunix_create; 135 } 136 #endif 137 au = (struct audata *)mem_alloc(sizeof(*au)); 138 #ifndef KERNEL 139 if (au == NULL) { 140 warnx("authunix_create: out of memory"); 141 goto cleanup_authunix_create; 142 } 143 #endif 144 auth->ah_ops = &auth_unix_ops; 145 auth->ah_private = au; 146 auth->ah_verf = au->au_shcred = _null_auth; 147 au->au_shfaults = 0; 148 au->au_origcred.oa_base = NULL; 149 150 /* 151 * fill in param struct from the given params 152 */ 153 (void)gettimeofday(&now, (struct timezone *)0); 154 aup.aup_time = now.tv_sec; 155 aup.aup_machname = machname; 156 aup.aup_uid = uid; 157 aup.aup_gid = gid; 158 aup.aup_len = (u_int)len; 159 aup.aup_gids = aup_gids; 160 161 /* 162 * Serialize the parameters into origcred 163 */ 164 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE); 165 if (! xdr_authunix_parms(&xdrs, &aup)) 166 abort(); 167 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs); 168 au->au_origcred.oa_flavor = AUTH_UNIX; 169 #ifdef KERNEL 170 au->au_origcred.oa_base = mem_alloc((u_int) len); 171 #else 172 if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) { 173 warnx("authunix_create: out of memory"); 174 goto cleanup_authunix_create; 175 } 176 #endif 177 memmove(au->au_origcred.oa_base, mymem, (size_t)len); 178 179 /* 180 * set auth handle to reflect new cred. 181 */ 182 auth->ah_cred = au->au_origcred; 183 marshal_new_auth(auth); 184 return (auth); 185 #ifndef KERNEL 186 cleanup_authunix_create: 187 if (auth) 188 mem_free(auth, sizeof(*auth)); 189 if (au) { 190 if (au->au_origcred.oa_base) 191 mem_free(au->au_origcred.oa_base, (u_int)len); 192 mem_free(au, sizeof(*au)); 193 } 194 return (NULL); 195 #endif 196 } 197 198 /* 199 * Returns an auth handle with parameters determined by doing lots of 200 * syscalls. 201 */ 202 AUTH * 203 authunix_create_default() 204 { 205 int len; 206 char machname[MAXHOSTNAMELEN + 1]; 207 uid_t uid; 208 gid_t gid; 209 gid_t gids[NGRPS]; 210 211 if (gethostname(machname, sizeof machname) == -1) 212 abort(); 213 machname[sizeof(machname) - 1] = 0; 214 uid = geteuid(); 215 gid = getegid(); 216 if ((len = getgroups(NGRPS, gids)) < 0) 217 abort(); 218 /* XXX: interface problem; those should all have been unsigned */ 219 return (authunix_create(machname, (int)uid, (int)gid, len, 220 (int *)gids)); 221 } 222 223 /* 224 * authunix operations 225 */ 226 227 /* ARGSUSED */ 228 static void 229 authunix_nextverf(auth) 230 AUTH *auth; 231 { 232 /* no action necessary */ 233 } 234 235 static bool_t 236 authunix_marshal(auth, xdrs) 237 AUTH *auth; 238 XDR *xdrs; 239 { 240 struct audata *au; 241 242 _DIAGASSERT(auth != NULL); 243 _DIAGASSERT(xdrs != NULL); 244 245 au = AUTH_PRIVATE(auth); 246 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos)); 247 } 248 249 static bool_t 250 authunix_validate(auth, verf) 251 AUTH *auth; 252 struct opaque_auth *verf; 253 { 254 struct audata *au; 255 XDR xdrs; 256 257 _DIAGASSERT(auth != NULL); 258 _DIAGASSERT(verf != NULL); 259 260 if (verf->oa_flavor == AUTH_SHORT) { 261 au = AUTH_PRIVATE(auth); 262 xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, 263 XDR_DECODE); 264 265 if (au->au_shcred.oa_base != NULL) { 266 mem_free(au->au_shcred.oa_base, 267 au->au_shcred.oa_length); 268 au->au_shcred.oa_base = NULL; 269 } 270 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) { 271 auth->ah_cred = au->au_shcred; 272 } else { 273 xdrs.x_op = XDR_FREE; 274 (void)xdr_opaque_auth(&xdrs, &au->au_shcred); 275 au->au_shcred.oa_base = NULL; 276 auth->ah_cred = au->au_origcred; 277 } 278 marshal_new_auth(auth); 279 } 280 return (TRUE); 281 } 282 283 static bool_t 284 authunix_refresh(auth) 285 AUTH *auth; 286 { 287 struct audata *au = AUTH_PRIVATE(auth); 288 struct authunix_parms aup; 289 struct timeval now; 290 XDR xdrs; 291 int stat; 292 293 _DIAGASSERT(auth != NULL); 294 295 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) { 296 /* there is no hope. Punt */ 297 return (FALSE); 298 } 299 au->au_shfaults ++; 300 301 /* first deserialize the creds back into a struct authunix_parms */ 302 aup.aup_machname = NULL; 303 aup.aup_gids = (int *)NULL; 304 xdrmem_create(&xdrs, au->au_origcred.oa_base, 305 au->au_origcred.oa_length, XDR_DECODE); 306 stat = xdr_authunix_parms(&xdrs, &aup); 307 if (! stat) 308 goto done; 309 310 /* update the time and serialize in place */ 311 (void)gettimeofday(&now, (struct timezone *)0); 312 aup.aup_time = now.tv_sec; 313 xdrs.x_op = XDR_ENCODE; 314 XDR_SETPOS(&xdrs, 0); 315 stat = xdr_authunix_parms(&xdrs, &aup); 316 if (! stat) 317 goto done; 318 auth->ah_cred = au->au_origcred; 319 marshal_new_auth(auth); 320 done: 321 /* free the struct authunix_parms created by deserializing */ 322 xdrs.x_op = XDR_FREE; 323 (void)xdr_authunix_parms(&xdrs, &aup); 324 XDR_DESTROY(&xdrs); 325 return (stat); 326 } 327 328 static void 329 authunix_destroy(auth) 330 AUTH *auth; 331 { 332 struct audata *au; 333 334 _DIAGASSERT(auth != NULL); 335 336 au = AUTH_PRIVATE(auth); 337 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length); 338 339 if (au->au_shcred.oa_base != NULL) 340 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); 341 342 mem_free(auth->ah_private, sizeof(struct audata)); 343 344 if (auth->ah_verf.oa_base != NULL) 345 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length); 346 347 mem_free(auth, sizeof(*auth)); 348 } 349 350 /* 351 * Marshals (pre-serializes) an auth struct. 352 * sets private data, au_marshed and au_mpos 353 */ 354 static void 355 marshal_new_auth(auth) 356 AUTH *auth; 357 { 358 XDR xdr_stream; 359 XDR *xdrs = &xdr_stream; 360 struct audata *au; 361 362 _DIAGASSERT(auth != NULL); 363 364 au = AUTH_PRIVATE(auth); 365 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE); 366 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) || 367 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) 368 warnx("auth_none.c - Fatal marshalling problem"); 369 else 370 au->au_mpos = XDR_GETPOS(xdrs); 371 XDR_DESTROY(xdrs); 372 } 373