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