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