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