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