10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11134SCasper.Dik@Sun.COM * Common Development and Distribution License (the "License").
6*11134SCasper.Dik@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
20132Srobinson */
21132Srobinson
22132Srobinson /*
23*11134SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
300Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
310Sstevel@tonic-gate * California.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * auth_sys.c, Implements UNIX (system) style authentication parameters.
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * The system is very weak. The client uses no encryption for its
380Sstevel@tonic-gate * credentials and only sends null verifiers. The server sends backs
390Sstevel@tonic-gate * null verifiers or optionally a verifier that suggests a new short hand
400Sstevel@tonic-gate * for the credentials.
410Sstevel@tonic-gate *
420Sstevel@tonic-gate */
430Sstevel@tonic-gate #include "mt.h"
440Sstevel@tonic-gate #include "rpc_mt.h"
45*11134SCasper.Dik@Sun.COM #include <alloca.h>
460Sstevel@tonic-gate #include <stdio.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate #include <stdlib.h>
490Sstevel@tonic-gate #include <unistd.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <rpc/types.h>
520Sstevel@tonic-gate #include <rpc/xdr.h>
530Sstevel@tonic-gate #include <rpc/auth.h>
540Sstevel@tonic-gate #include <rpc/auth_sys.h>
550Sstevel@tonic-gate #include <synch.h>
560Sstevel@tonic-gate
57132Srobinson extern int gethostname(char *, int);
58132Srobinson extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
590Sstevel@tonic-gate
60132Srobinson static struct auth_ops *authsys_ops(void);
610Sstevel@tonic-gate
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate * This struct is pointed to by the ah_private field of an auth_handle.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate struct audata {
660Sstevel@tonic-gate struct opaque_auth au_origcred; /* original credentials */
670Sstevel@tonic-gate struct opaque_auth au_shcred; /* short hand cred */
680Sstevel@tonic-gate uint_t au_shfaults; /* short hand cache faults */
690Sstevel@tonic-gate char au_marshed[MAX_AUTH_BYTES];
700Sstevel@tonic-gate uint_t au_mpos; /* xdr pos at end of marshed */
710Sstevel@tonic-gate };
720Sstevel@tonic-gate #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
730Sstevel@tonic-gate
740Sstevel@tonic-gate static void marshal_new_auth();
750Sstevel@tonic-gate
760Sstevel@tonic-gate static const char auth_sys_str[] = "%s : %s";
770Sstevel@tonic-gate static const char authsys_create_str[] = "authsys_create";
780Sstevel@tonic-gate static const char __no_mem_auth[] = "out of memory";
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Create a (sys) unix style authenticator.
820Sstevel@tonic-gate * Returns an auth handle with the given stuff in it.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate AUTH *
authsys_create(const char * machname,const uid_t uid,const gid_t gid,const int len,const gid_t * aup_gids)85132Srobinson authsys_create(const char *machname, const uid_t uid, const gid_t gid,
86132Srobinson const int len, const gid_t *aup_gids)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate struct authsys_parms aup;
890Sstevel@tonic-gate char mymem[MAX_AUTH_BYTES];
900Sstevel@tonic-gate struct timeval now;
910Sstevel@tonic-gate XDR xdrs;
920Sstevel@tonic-gate AUTH *auth;
930Sstevel@tonic-gate struct audata *au;
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * Allocate and set up auth handle
970Sstevel@tonic-gate */
98132Srobinson auth = malloc(sizeof (*auth));
990Sstevel@tonic-gate if (auth == NULL) {
1000Sstevel@tonic-gate (void) syslog(LOG_ERR, auth_sys_str, authsys_create_str,
101*11134SCasper.Dik@Sun.COM __no_mem_auth);
1020Sstevel@tonic-gate return (NULL);
1030Sstevel@tonic-gate }
104132Srobinson au = malloc(sizeof (*au));
1050Sstevel@tonic-gate if (au == NULL) {
1060Sstevel@tonic-gate (void) syslog(LOG_ERR, auth_sys_str, authsys_create_str,
107*11134SCasper.Dik@Sun.COM __no_mem_auth);
108132Srobinson free(auth);
1090Sstevel@tonic-gate return (NULL);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate auth->ah_ops = authsys_ops();
1120Sstevel@tonic-gate auth->ah_private = (caddr_t)au;
1130Sstevel@tonic-gate auth->ah_verf = au->au_shcred = _null_auth;
1140Sstevel@tonic-gate au->au_shfaults = 0;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * fill in param struct from the given params
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate (void) gettimeofday(&now, (struct timezone *)0);
1200Sstevel@tonic-gate aup.aup_time = now.tv_sec;
1210Sstevel@tonic-gate aup.aup_machname = (char *)machname;
1220Sstevel@tonic-gate aup.aup_uid = uid;
1230Sstevel@tonic-gate aup.aup_gid = gid;
1240Sstevel@tonic-gate aup.aup_len = (uint_t)len;
1250Sstevel@tonic-gate aup.aup_gids = (gid_t *)aup_gids;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * Serialize the parameters into origcred
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
131132Srobinson if (!xdr_authsys_parms(&xdrs, &aup)) {
1320Sstevel@tonic-gate (void) syslog(LOG_ERR, auth_sys_str, authsys_create_str,
133*11134SCasper.Dik@Sun.COM ": xdr_authsys_parms failed");
1340Sstevel@tonic-gate return (NULL);
1350Sstevel@tonic-gate }
136132Srobinson au->au_origcred.oa_length = XDR_GETPOS(&xdrs);
1370Sstevel@tonic-gate au->au_origcred.oa_flavor = AUTH_SYS;
138132Srobinson if ((au->au_origcred.oa_base = malloc(au->au_origcred.oa_length)) ==
139*11134SCasper.Dik@Sun.COM NULL) {
1400Sstevel@tonic-gate (void) syslog(LOG_ERR, auth_sys_str, authsys_create_str,
141*11134SCasper.Dik@Sun.COM __no_mem_auth);
142132Srobinson free(au);
143132Srobinson free(auth);
1440Sstevel@tonic-gate return (NULL);
1450Sstevel@tonic-gate }
146132Srobinson (void) memcpy(au->au_origcred.oa_base, mymem,
147*11134SCasper.Dik@Sun.COM (size_t)au->au_origcred.oa_length);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * set auth handle to reflect new cred.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate auth->ah_cred = au->au_origcred;
1530Sstevel@tonic-gate (void) marshal_new_auth(auth);
1540Sstevel@tonic-gate return (auth);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * authsys_create_default is a public interface.
1590Sstevel@tonic-gate *
1600Sstevel@tonic-gate * Returns an auth handle with parameters determined by doing lots of
1610Sstevel@tonic-gate * syscalls.
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate static const char authsys_def_str[] =
1650Sstevel@tonic-gate "authsys_create_default: get%s failed: %m";
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate AUTH *
authsys_create_default(void)1680Sstevel@tonic-gate authsys_create_default(void)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate int len;
1710Sstevel@tonic-gate char machname[MAX_MACHINE_NAME + 1];
1720Sstevel@tonic-gate uid_t uid;
1730Sstevel@tonic-gate gid_t gid;
174*11134SCasper.Dik@Sun.COM int maxgrp = getgroups(0, NULL);
175*11134SCasper.Dik@Sun.COM gid_t *gids = alloca(maxgrp * sizeof (gid_t));
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (gethostname(machname, MAX_MACHINE_NAME) == -1) {
1780Sstevel@tonic-gate (void) syslog(LOG_ERR, authsys_def_str, "hostname");
1790Sstevel@tonic-gate return (NULL);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate machname[MAX_MACHINE_NAME] = 0;
1820Sstevel@tonic-gate uid = geteuid();
1830Sstevel@tonic-gate gid = getegid();
184*11134SCasper.Dik@Sun.COM if ((len = getgroups(maxgrp, gids)) < 0) {
1850Sstevel@tonic-gate (void) syslog(LOG_ERR, authsys_def_str, "groups");
1860Sstevel@tonic-gate return (NULL);
1870Sstevel@tonic-gate }
188*11134SCasper.Dik@Sun.COM if (len > NGRPS)
189*11134SCasper.Dik@Sun.COM len = NGRPS;
190132Srobinson return (authsys_create(machname, uid, gid, len, gids));
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * authsys_create_ruid() is a private routine and is a
1950Sstevel@tonic-gate * variant of authsys_create_default().
1960Sstevel@tonic-gate *
1970Sstevel@tonic-gate * authsys_create_default() is using the effective uid.
1980Sstevel@tonic-gate * authsys_create_ruid() is using the real uid.
1990Sstevel@tonic-gate *
2000Sstevel@tonic-gate * This routine is used by key_call_ext() in key_call.c
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate AUTH *
authsys_create_ruid(void)2030Sstevel@tonic-gate authsys_create_ruid(void)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate int len;
2060Sstevel@tonic-gate char machname[MAX_MACHINE_NAME + 1];
2070Sstevel@tonic-gate uid_t uid;
2080Sstevel@tonic-gate gid_t gid;
209*11134SCasper.Dik@Sun.COM int maxgrp = getgroups(0, NULL);
210*11134SCasper.Dik@Sun.COM gid_t *gids = alloca(maxgrp * sizeof (gid_t));
2110Sstevel@tonic-gate AUTH *res;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (gethostname(machname, MAX_MACHINE_NAME) == -1) {
2140Sstevel@tonic-gate (void) syslog(LOG_ERR,
215*11134SCasper.Dik@Sun.COM "authsys_create_ruid:gethostname failed");
2160Sstevel@tonic-gate return (NULL);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate machname[MAX_MACHINE_NAME] = 0;
2190Sstevel@tonic-gate uid = getuid();
2200Sstevel@tonic-gate gid = getgid();
221*11134SCasper.Dik@Sun.COM if ((len = getgroups(maxgrp, gids)) < 0) {
2220Sstevel@tonic-gate (void) syslog(LOG_ERR,
223*11134SCasper.Dik@Sun.COM "authsys_create_ruid:getgroups failed");
2240Sstevel@tonic-gate return (NULL);
2250Sstevel@tonic-gate }
226*11134SCasper.Dik@Sun.COM if (len > NGRPS)
227*11134SCasper.Dik@Sun.COM len = NGRPS;
2280Sstevel@tonic-gate res = authsys_create(machname, uid, gid, len, gids);
2290Sstevel@tonic-gate return (res);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * authsys operations
2340Sstevel@tonic-gate */
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /*ARGSUSED*/
2370Sstevel@tonic-gate static void
authsys_nextverf(AUTH * auth)2380Sstevel@tonic-gate authsys_nextverf(AUTH *auth)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate /* no action necessary */
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate static bool_t
authsys_marshal(AUTH * auth,XDR * xdrs)2440Sstevel@tonic-gate authsys_marshal(AUTH *auth, XDR *xdrs)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate /* LINTED pointer alignment */
2470Sstevel@tonic-gate struct audata *au = AUTH_PRIVATE(auth);
2480Sstevel@tonic-gate
249132Srobinson return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate static bool_t
authsys_validate(AUTH * auth,struct opaque_auth * verf)2530Sstevel@tonic-gate authsys_validate(AUTH *auth, struct opaque_auth *verf)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate struct audata *au;
2560Sstevel@tonic-gate XDR xdrs;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate if (verf->oa_flavor == AUTH_SHORT) {
2590Sstevel@tonic-gate /* LINTED pointer alignment */
2600Sstevel@tonic-gate au = AUTH_PRIVATE(auth);
2610Sstevel@tonic-gate xdrmem_create(&xdrs, verf->oa_base,
262*11134SCasper.Dik@Sun.COM verf->oa_length, XDR_DECODE);
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate if (au->au_shcred.oa_base != NULL) {
265132Srobinson free(au->au_shcred.oa_base);
2660Sstevel@tonic-gate au->au_shcred.oa_base = NULL;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
2690Sstevel@tonic-gate auth->ah_cred = au->au_shcred;
2700Sstevel@tonic-gate } else {
2710Sstevel@tonic-gate xdrs.x_op = XDR_FREE;
2720Sstevel@tonic-gate (void) xdr_opaque_auth(&xdrs, &au->au_shcred);
2730Sstevel@tonic-gate au->au_shcred.oa_base = NULL;
2740Sstevel@tonic-gate auth->ah_cred = au->au_origcred;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate (void) marshal_new_auth(auth);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate return (TRUE);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*ARGSUSED*/
2820Sstevel@tonic-gate static bool_t
authsys_refresh(AUTH * auth,void * dummy)2830Sstevel@tonic-gate authsys_refresh(AUTH *auth, void *dummy)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate /* LINTED pointer alignment */
2860Sstevel@tonic-gate struct audata *au = AUTH_PRIVATE(auth);
2870Sstevel@tonic-gate struct authsys_parms aup;
2880Sstevel@tonic-gate struct timeval now;
2890Sstevel@tonic-gate XDR xdrs;
2900Sstevel@tonic-gate int stat;
2910Sstevel@tonic-gate
292132Srobinson if (auth->ah_cred.oa_base == au->au_origcred.oa_base)
293132Srobinson return (FALSE); /* there is no hope. Punt */
2940Sstevel@tonic-gate au->au_shfaults ++;
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /* first deserialize the creds back into a struct authsys_parms */
2970Sstevel@tonic-gate aup.aup_machname = NULL;
298132Srobinson aup.aup_gids = NULL;
2990Sstevel@tonic-gate xdrmem_create(&xdrs, au->au_origcred.oa_base,
3000Sstevel@tonic-gate au->au_origcred.oa_length, XDR_DECODE);
3010Sstevel@tonic-gate stat = xdr_authsys_parms(&xdrs, &aup);
302132Srobinson if (!stat)
3030Sstevel@tonic-gate goto done;
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate /* update the time and serialize in place */
3060Sstevel@tonic-gate (void) gettimeofday(&now, (struct timezone *)0);
3070Sstevel@tonic-gate aup.aup_time = now.tv_sec;
3080Sstevel@tonic-gate xdrs.x_op = XDR_ENCODE;
3090Sstevel@tonic-gate XDR_SETPOS(&xdrs, 0);
3100Sstevel@tonic-gate stat = xdr_authsys_parms(&xdrs, &aup);
311132Srobinson if (!stat)
3120Sstevel@tonic-gate goto done;
3130Sstevel@tonic-gate auth->ah_cred = au->au_origcred;
3140Sstevel@tonic-gate (void) marshal_new_auth(auth);
3150Sstevel@tonic-gate done:
3160Sstevel@tonic-gate /* free the struct authsys_parms created by deserializing */
3170Sstevel@tonic-gate xdrs.x_op = XDR_FREE;
3180Sstevel@tonic-gate (void) xdr_authsys_parms(&xdrs, &aup);
3190Sstevel@tonic-gate XDR_DESTROY(&xdrs);
3200Sstevel@tonic-gate return (stat);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate static void
authsys_destroy(AUTH * auth)3240Sstevel@tonic-gate authsys_destroy(AUTH *auth)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate /* LINTED pointer alignment */
3270Sstevel@tonic-gate struct audata *au = AUTH_PRIVATE(auth);
3280Sstevel@tonic-gate
329132Srobinson free(au->au_origcred.oa_base);
3300Sstevel@tonic-gate if (au->au_shcred.oa_base != NULL)
331132Srobinson free(au->au_shcred.oa_base);
332132Srobinson free(auth->ah_private);
3330Sstevel@tonic-gate if (auth->ah_verf.oa_base != NULL)
334132Srobinson free(auth->ah_verf.oa_base);
335132Srobinson free(auth);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate * Marshals (pre-serializes) an auth struct.
3400Sstevel@tonic-gate * sets private data, au_marshed and au_mpos
3410Sstevel@tonic-gate */
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate static const char marshal_new_auth_str[] =
3440Sstevel@tonic-gate "marshal_new_auth - Fatal marshalling problem";
3450Sstevel@tonic-gate static void
marshal_new_auth(AUTH * auth)3460Sstevel@tonic-gate marshal_new_auth(AUTH *auth)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate XDR xdr_stream;
3490Sstevel@tonic-gate XDR *xdrs = &xdr_stream;
3500Sstevel@tonic-gate /* LINTED pointer alignment */
3510Sstevel@tonic-gate struct audata *au = AUTH_PRIVATE(auth);
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
354132Srobinson if ((!xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
355132Srobinson (!xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
3560Sstevel@tonic-gate (void) syslog(LOG_ERR, marshal_new_auth_str);
3570Sstevel@tonic-gate } else {
3580Sstevel@tonic-gate au->au_mpos = XDR_GETPOS(xdrs);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate XDR_DESTROY(xdrs);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate static struct auth_ops *
authsys_ops(void)3640Sstevel@tonic-gate authsys_ops(void)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate static struct auth_ops ops;
3670Sstevel@tonic-gate extern mutex_t ops_lock;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
3700Sstevel@tonic-gate
371132Srobinson (void) mutex_lock(&ops_lock);
3720Sstevel@tonic-gate if (ops.ah_nextverf == NULL) {
3730Sstevel@tonic-gate ops.ah_nextverf = authsys_nextverf;
3740Sstevel@tonic-gate ops.ah_marshal = authsys_marshal;
3750Sstevel@tonic-gate ops.ah_validate = authsys_validate;
3760Sstevel@tonic-gate ops.ah_refresh = authsys_refresh;
3770Sstevel@tonic-gate ops.ah_destroy = authsys_destroy;
3780Sstevel@tonic-gate }
379132Srobinson (void) mutex_unlock(&ops_lock);
3800Sstevel@tonic-gate return (&ops);
3810Sstevel@tonic-gate }
382