1 /* $NetBSD: auth_unix.c,v 1.27 2020/10/03 18:31:29 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #if defined(LIBC_SCCS) && !defined(lint) 36 #if 0 37 static char *sccsid = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro"; 38 static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC"; 39 #else 40 __RCSID("$NetBSD: auth_unix.c,v 1.27 2020/10/03 18:31:29 christos Exp $"); 41 #endif 42 #endif 43 44 /* 45 * auth_unix.c, Implements UNIX style authentication parameters. 46 * 47 * Copyright (C) 1984, Sun Microsystems, Inc. 48 * 49 * The system is very weak. The client uses no encryption for its 50 * credentials and only sends null verifiers. The server sends backs 51 * null verifiers or optionally a verifier that suggests a new short hand 52 * for the credentials. 53 * 54 */ 55 56 #include "namespace.h" 57 #include "reentrant.h" 58 #include <sys/param.h> 59 60 #include <assert.h> 61 #include <err.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 #include <rpc/types.h> 68 #include <rpc/xdr.h> 69 #include <rpc/auth.h> 70 #include <rpc/auth_unix.h> 71 72 #ifdef __weak_alias 73 __weak_alias(authunix_create,_authunix_create) 74 __weak_alias(authunix_create_default,_authunix_create_default) 75 #endif 76 77 78 /* auth_unix.c */ 79 static void authunix_nextverf(AUTH *); 80 static bool_t authunix_marshal(AUTH *, XDR *); 81 static bool_t authunix_validate(AUTH *, struct opaque_auth *); 82 static bool_t authunix_refresh(AUTH *); 83 static void authunix_destroy(AUTH *); 84 static void marshal_new_auth(AUTH *); 85 static const struct auth_ops *authunix_ops(void); 86 87 /* 88 * This struct is pointed to by the ah_private field of an auth_handle. 89 */ 90 struct audata { 91 struct opaque_auth au_origcred; /* original credentials */ 92 struct opaque_auth au_shcred; /* short hand cred */ 93 u_long au_shfaults; /* short hand cache faults */ 94 char au_marshed[MAX_AUTH_BYTES]; 95 u_int au_mpos; /* xdr pos at end of marshed */ 96 }; 97 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private) 98 99 /* 100 * Create a unix style authenticator. 101 * Returns an auth handle with the given stuff in it. 102 */ 103 AUTH * 104 authunix_create(char *machname, int uid, int gid, int len, int *aup_gids) 105 { 106 struct authunix_parms aup; 107 char mymem[MAX_AUTH_BYTES]; 108 struct timeval now; 109 XDR xdrs; 110 AUTH *auth; 111 struct audata *au; 112 113 /* 114 * Allocate and set up auth handle 115 */ 116 au = NULL; 117 auth = mem_alloc(sizeof(*auth)); 118 #ifndef KERNEL 119 if (auth == NULL) { 120 warn("%s: out of memory", __func__); 121 goto cleanup_authunix_create; 122 } 123 #endif 124 au = mem_alloc(sizeof(*au)); 125 #ifndef KERNEL 126 if (au == NULL) { 127 warn("%s: out of memory", __func__); 128 goto cleanup_authunix_create; 129 } 130 #endif 131 auth->ah_ops = authunix_ops(); 132 auth->ah_private = au; 133 auth->ah_verf = au->au_shcred = _null_auth; 134 au->au_shfaults = 0; 135 au->au_origcred.oa_base = NULL; 136 137 /* 138 * fill in param struct from the given params 139 */ 140 (void)gettimeofday(&now, NULL); 141 aup.aup_time = (u_long)now.tv_sec; /* XXX: truncate on 32 bit */ 142 aup.aup_machname = machname; 143 aup.aup_uid = uid; 144 aup.aup_gid = gid; 145 aup.aup_len = (u_int)len; 146 aup.aup_gids = aup_gids; 147 148 /* 149 * Serialize the parameters into origcred 150 */ 151 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE); 152 if (! xdr_authunix_parms(&xdrs, &aup)) 153 abort(); 154 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs); 155 au->au_origcred.oa_flavor = AUTH_UNIX; 156 #ifdef KERNEL 157 au->au_origcred.oa_base = mem_alloc((size_t)len); 158 #else 159 if ((au->au_origcred.oa_base = mem_alloc((size_t)len)) == NULL) { 160 warn("%s: out of memory", __func__); 161 goto cleanup_authunix_create; 162 } 163 #endif 164 memmove(au->au_origcred.oa_base, mymem, (size_t)len); 165 166 /* 167 * set auth handle to reflect new cred. 168 */ 169 auth->ah_cred = au->au_origcred; 170 marshal_new_auth(auth); 171 return (auth); 172 #ifndef KERNEL 173 cleanup_authunix_create: 174 if (auth) 175 mem_free(auth, sizeof(*auth)); 176 if (au) { 177 if (au->au_origcred.oa_base) 178 mem_free(au->au_origcred.oa_base, (u_int)len); 179 mem_free(au, sizeof(*au)); 180 } 181 return (NULL); 182 #endif 183 } 184 185 /* 186 * Some servers will refuse mounts if the group list is larger 187 * than it expects (like 8). This allows the application to set 188 * the maximum size of the group list that will be sent. 189 */ 190 static int maxgrplist = NGROUPS; 191 192 void 193 set_rpc_maxgrouplist(int num) 194 { 195 if (num < NGROUPS) 196 maxgrplist = num; 197 } 198 199 /* 200 * Returns an auth handle with parameters determined by doing lots of 201 * syscalls. 202 */ 203 AUTH * 204 authunix_create_default(void) 205 { 206 int len; 207 char machname[MAXHOSTNAMELEN + 1]; 208 uid_t uid; 209 gid_t gid; 210 gid_t gids[NGRPS]; 211 212 if (gethostname(machname, sizeof machname) == -1) 213 abort(); 214 machname[sizeof(machname) - 1] = 0; 215 uid = geteuid(); 216 gid = getegid(); 217 if ((len = getgroups(NGRPS, gids)) < 0) 218 abort(); 219 if (len > maxgrplist) 220 len = maxgrplist; 221 /* XXX: interface problem; those should all have been unsigned */ 222 return (authunix_create(machname, (int)uid, (int)gid, len, 223 (int *)gids)); 224 } 225 226 /* 227 * authunix operations 228 */ 229 230 /* ARGSUSED */ 231 static void 232 authunix_nextverf(AUTH *auth) 233 { 234 /* no action necessary */ 235 } 236 237 static bool_t 238 authunix_marshal(AUTH *auth, 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 *auth, struct opaque_auth *verf) 251 { 252 struct audata *au; 253 XDR xdrs; 254 255 _DIAGASSERT(auth != NULL); 256 _DIAGASSERT(verf != NULL); 257 258 if (verf->oa_flavor == AUTH_SHORT) { 259 au = AUTH_PRIVATE(auth); 260 xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, 261 XDR_DECODE); 262 263 if (au->au_shcred.oa_base != NULL) { 264 mem_free(au->au_shcred.oa_base, 265 au->au_shcred.oa_length); 266 au->au_shcred.oa_base = NULL; 267 } 268 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) { 269 auth->ah_cred = au->au_shcred; 270 } else { 271 xdrs.x_op = XDR_FREE; 272 (void)xdr_opaque_auth(&xdrs, &au->au_shcred); 273 au->au_shcred.oa_base = NULL; 274 auth->ah_cred = au->au_origcred; 275 } 276 marshal_new_auth(auth); 277 } 278 return (TRUE); 279 } 280 281 static bool_t 282 authunix_refresh(AUTH *auth) 283 { 284 struct audata *au = AUTH_PRIVATE(auth); 285 struct authunix_parms aup; 286 struct timeval now; 287 XDR xdrs; 288 int stat; 289 290 _DIAGASSERT(auth != NULL); 291 292 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) { 293 /* there is no hope. Punt */ 294 return (FALSE); 295 } 296 au->au_shfaults++; 297 298 /* first deserialize the creds back into a struct authunix_parms */ 299 aup.aup_machname = NULL; 300 aup.aup_gids = NULL; 301 xdrmem_create(&xdrs, au->au_origcred.oa_base, 302 au->au_origcred.oa_length, XDR_DECODE); 303 stat = xdr_authunix_parms(&xdrs, &aup); 304 if (! stat) 305 goto done; 306 307 /* update the time and serialize in place */ 308 (void)gettimeofday(&now, NULL); 309 aup.aup_time = (u_long)now.tv_sec; /* XXX: truncate on 32 bit */ 310 xdrs.x_op = XDR_ENCODE; 311 XDR_SETPOS(&xdrs, 0); 312 stat = xdr_authunix_parms(&xdrs, &aup); 313 if (! stat) 314 goto done; 315 auth->ah_cred = au->au_origcred; 316 marshal_new_auth(auth); 317 done: 318 /* free the struct authunix_parms created by deserializing */ 319 xdrs.x_op = XDR_FREE; 320 (void)xdr_authunix_parms(&xdrs, &aup); 321 XDR_DESTROY(&xdrs); 322 return (stat); 323 } 324 325 static void 326 authunix_destroy(AUTH *auth) 327 { 328 struct audata *au; 329 330 _DIAGASSERT(auth != NULL); 331 332 au = AUTH_PRIVATE(auth); 333 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length); 334 335 if (au->au_shcred.oa_base != NULL) 336 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); 337 338 mem_free(auth->ah_private, sizeof(struct audata)); 339 340 if (auth->ah_verf.oa_base != NULL) 341 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length); 342 343 mem_free(auth, sizeof(*auth)); 344 } 345 346 /* 347 * Marshals (pre-serializes) an auth struct. 348 * sets private data, au_marshed and au_mpos 349 */ 350 static void 351 marshal_new_auth(AUTH *auth) 352 { 353 XDR xdr_stream; 354 XDR *xdrs = &xdr_stream; 355 struct audata *au; 356 357 _DIAGASSERT(auth != NULL); 358 359 au = AUTH_PRIVATE(auth); 360 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE); 361 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) || 362 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) 363 warnx("%s: Fatal marshalling problem", __func__); 364 else 365 au->au_mpos = XDR_GETPOS(xdrs); 366 XDR_DESTROY(xdrs); 367 } 368 369 static const struct auth_ops * 370 authunix_ops(void) 371 { 372 static struct auth_ops ops; 373 #ifdef _REENTRANT 374 extern mutex_t ops_lock; 375 #endif 376 377 /* VARIABLES PROTECTED BY ops_lock: ops */ 378 379 mutex_lock(&ops_lock); 380 if (ops.ah_nextverf == NULL) { 381 ops.ah_nextverf = authunix_nextverf; 382 ops.ah_marshal = authunix_marshal; 383 ops.ah_validate = authunix_validate; 384 ops.ah_refresh = authunix_refresh; 385 ops.ah_destroy = authunix_destroy; 386 } 387 mutex_unlock(&ops_lock); 388 return (&ops); 389 } 390