xref: /titanic_50/usr/src/cmd/ssh/sshd/auth-krb4.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Dug Song.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
5*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
6*7c478bd9Sstevel@tonic-gate  * are met:
7*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
8*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
9*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
10*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
11*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*7c478bd9Sstevel@tonic-gate  */
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate #include "includes.h"
26*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth-krb4.c,v 1.28 2002/09/26 11:38:43 markus Exp $");
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include "ssh.h"
31*7c478bd9Sstevel@tonic-gate #include "ssh1.h"
32*7c478bd9Sstevel@tonic-gate #include "packet.h"
33*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
34*7c478bd9Sstevel@tonic-gate #include "log.h"
35*7c478bd9Sstevel@tonic-gate #include "servconf.h"
36*7c478bd9Sstevel@tonic-gate #include "uidswap.h"
37*7c478bd9Sstevel@tonic-gate #include "auth.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #ifdef AFS
40*7c478bd9Sstevel@tonic-gate #include "radix.h"
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #ifdef KRB4
44*7c478bd9Sstevel@tonic-gate extern ServerOptions options;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static int
krb4_init(void * context)47*7c478bd9Sstevel@tonic-gate krb4_init(void *context)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	static int cleanup_registered = 0;
50*7c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = (Authctxt *)context;
51*7c478bd9Sstevel@tonic-gate 	const char *tkt_root = TKT_ROOT;
52*7c478bd9Sstevel@tonic-gate 	struct stat st;
53*7c478bd9Sstevel@tonic-gate 	int fd;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (!authctxt->krb4_ticket_file) {
56*7c478bd9Sstevel@tonic-gate 		/* Set unique ticket string manually since we're still root. */
57*7c478bd9Sstevel@tonic-gate 		authctxt->krb4_ticket_file = xmalloc(MAXPATHLEN);
58*7c478bd9Sstevel@tonic-gate #ifdef AFS
59*7c478bd9Sstevel@tonic-gate 		if (lstat("/ticket", &st) != -1)
60*7c478bd9Sstevel@tonic-gate 			tkt_root = "/ticket/";
61*7c478bd9Sstevel@tonic-gate #endif /* AFS */
62*7c478bd9Sstevel@tonic-gate 		snprintf(authctxt->krb4_ticket_file, MAXPATHLEN, "%s%u_%ld",
63*7c478bd9Sstevel@tonic-gate 		    tkt_root, authctxt->pw->pw_uid, (long)getpid());
64*7c478bd9Sstevel@tonic-gate 		krb_set_tkt_string(authctxt->krb4_ticket_file);
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 	/* Register ticket cleanup in case of fatal error. */
67*7c478bd9Sstevel@tonic-gate 	if (!cleanup_registered) {
68*7c478bd9Sstevel@tonic-gate 		fatal_add_cleanup(krb4_cleanup_proc, authctxt);
69*7c478bd9Sstevel@tonic-gate 		cleanup_registered = 1;
70*7c478bd9Sstevel@tonic-gate 	}
71*7c478bd9Sstevel@tonic-gate 	/* Try to create our ticket file. */
72*7c478bd9Sstevel@tonic-gate 	if ((fd = mkstemp(authctxt->krb4_ticket_file)) != -1) {
73*7c478bd9Sstevel@tonic-gate 		close(fd);
74*7c478bd9Sstevel@tonic-gate 		return (1);
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 	/* Ticket file exists - make sure user owns it (just passed ticket). */
77*7c478bd9Sstevel@tonic-gate 	if (lstat(authctxt->krb4_ticket_file, &st) != -1) {
78*7c478bd9Sstevel@tonic-gate 		if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&
79*7c478bd9Sstevel@tonic-gate 		    st.st_uid == authctxt->pw->pw_uid)
80*7c478bd9Sstevel@tonic-gate 			return (1);
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 	/* Failure - cancel cleanup function, leaving ticket for inspection. */
83*7c478bd9Sstevel@tonic-gate 	log("WARNING: bad ticket file %s", authctxt->krb4_ticket_file);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	fatal_remove_cleanup(krb4_cleanup_proc, authctxt);
86*7c478bd9Sstevel@tonic-gate 	cleanup_registered = 0;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	xfree(authctxt->krb4_ticket_file);
89*7c478bd9Sstevel@tonic-gate 	authctxt->krb4_ticket_file = NULL;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	return (0);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * try krb4 authentication,
96*7c478bd9Sstevel@tonic-gate  * return 1 on success, 0 on failure, -1 if krb4 is not available
97*7c478bd9Sstevel@tonic-gate  */
98*7c478bd9Sstevel@tonic-gate int
auth_krb4_password(Authctxt * authctxt,const char * password)99*7c478bd9Sstevel@tonic-gate auth_krb4_password(Authctxt *authctxt, const char *password)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	AUTH_DAT adata;
102*7c478bd9Sstevel@tonic-gate 	KTEXT_ST tkt;
103*7c478bd9Sstevel@tonic-gate 	struct hostent *hp;
104*7c478bd9Sstevel@tonic-gate 	struct passwd *pw;
105*7c478bd9Sstevel@tonic-gate 	char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ];
106*7c478bd9Sstevel@tonic-gate 	u_int32_t faddr;
107*7c478bd9Sstevel@tonic-gate 	int r;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if ((pw = authctxt->pw) == NULL)
110*7c478bd9Sstevel@tonic-gate 		return (0);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	/*
113*7c478bd9Sstevel@tonic-gate 	 * Try Kerberos password authentication only for non-root
114*7c478bd9Sstevel@tonic-gate 	 * users and only if Kerberos is installed.
115*7c478bd9Sstevel@tonic-gate 	 */
116*7c478bd9Sstevel@tonic-gate 	if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
117*7c478bd9Sstevel@tonic-gate 		/* Set up our ticket file. */
118*7c478bd9Sstevel@tonic-gate 		if (!krb4_init(authctxt)) {
119*7c478bd9Sstevel@tonic-gate 			log("Couldn't initialize Kerberos ticket file for %s!",
120*7c478bd9Sstevel@tonic-gate 			    pw->pw_name);
121*7c478bd9Sstevel@tonic-gate 			goto failure;
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 		/* Try to get TGT using our password. */
124*7c478bd9Sstevel@tonic-gate 		r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm,
125*7c478bd9Sstevel@tonic-gate 		    "krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password);
126*7c478bd9Sstevel@tonic-gate 		if (r != INTK_OK) {
127*7c478bd9Sstevel@tonic-gate 			debug("Kerberos v4 password authentication for %s "
128*7c478bd9Sstevel@tonic-gate 			    "failed: %s", pw->pw_name, krb_err_txt[r]);
129*7c478bd9Sstevel@tonic-gate 			goto failure;
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		/* Successful authentication. */
132*7c478bd9Sstevel@tonic-gate 		chown(tkt_string(), pw->pw_uid, pw->pw_gid);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 		/*
135*7c478bd9Sstevel@tonic-gate 		 * Now that we have a TGT, try to get a local
136*7c478bd9Sstevel@tonic-gate 		 * "rcmd" ticket to ensure that we are not talking
137*7c478bd9Sstevel@tonic-gate 		 * to a bogus Kerberos server.
138*7c478bd9Sstevel@tonic-gate 		 */
139*7c478bd9Sstevel@tonic-gate 		gethostname(localhost, sizeof(localhost));
140*7c478bd9Sstevel@tonic-gate 		strlcpy(phost, (char *)krb_get_phost(localhost),
141*7c478bd9Sstevel@tonic-gate 		    sizeof(phost));
142*7c478bd9Sstevel@tonic-gate 		r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 		if (r == KSUCCESS) {
145*7c478bd9Sstevel@tonic-gate 			if ((hp = gethostbyname(localhost)) == NULL) {
146*7c478bd9Sstevel@tonic-gate 				log("Couldn't get local host address!");
147*7c478bd9Sstevel@tonic-gate 				goto failure;
148*7c478bd9Sstevel@tonic-gate 			}
149*7c478bd9Sstevel@tonic-gate 			memmove((void *)&faddr, (void *)hp->h_addr,
150*7c478bd9Sstevel@tonic-gate 			    sizeof(faddr));
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 			/* Verify our "rcmd" ticket. */
153*7c478bd9Sstevel@tonic-gate 			r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost,
154*7c478bd9Sstevel@tonic-gate 			    faddr, &adata, "");
155*7c478bd9Sstevel@tonic-gate 			if (r == RD_AP_UNDEC) {
156*7c478bd9Sstevel@tonic-gate 				/*
157*7c478bd9Sstevel@tonic-gate 				 * Probably didn't have a srvtab on
158*7c478bd9Sstevel@tonic-gate 				 * localhost. Disallow login.
159*7c478bd9Sstevel@tonic-gate 				 */
160*7c478bd9Sstevel@tonic-gate 				log("Kerberos v4 TGT for %s unverifiable, "
161*7c478bd9Sstevel@tonic-gate 				    "no srvtab installed? krb_rd_req: %s",
162*7c478bd9Sstevel@tonic-gate 				    pw->pw_name, krb_err_txt[r]);
163*7c478bd9Sstevel@tonic-gate 				goto failure;
164*7c478bd9Sstevel@tonic-gate 			} else if (r != KSUCCESS) {
165*7c478bd9Sstevel@tonic-gate 				log("Kerberos v4 %s ticket unverifiable: %s",
166*7c478bd9Sstevel@tonic-gate 				    KRB4_SERVICE_NAME, krb_err_txt[r]);
167*7c478bd9Sstevel@tonic-gate 				goto failure;
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 		} else if (r == KDC_PR_UNKNOWN) {
170*7c478bd9Sstevel@tonic-gate 			/*
171*7c478bd9Sstevel@tonic-gate 			 * Disallow login if no rcmd service exists, and
172*7c478bd9Sstevel@tonic-gate 			 * log the error.
173*7c478bd9Sstevel@tonic-gate 			 */
174*7c478bd9Sstevel@tonic-gate 			log("Kerberos v4 TGT for %s unverifiable: %s; %s.%s "
175*7c478bd9Sstevel@tonic-gate 			    "not registered, or srvtab is wrong?", pw->pw_name,
176*7c478bd9Sstevel@tonic-gate 			    krb_err_txt[r], KRB4_SERVICE_NAME, phost);
177*7c478bd9Sstevel@tonic-gate 			goto failure;
178*7c478bd9Sstevel@tonic-gate 		} else {
179*7c478bd9Sstevel@tonic-gate 			/*
180*7c478bd9Sstevel@tonic-gate 			 * TGT is bad, forget it. Possibly spoofed!
181*7c478bd9Sstevel@tonic-gate 			 */
182*7c478bd9Sstevel@tonic-gate 			debug("WARNING: Kerberos v4 TGT possibly spoofed "
183*7c478bd9Sstevel@tonic-gate 			    "for %s: %s", pw->pw_name, krb_err_txt[r]);
184*7c478bd9Sstevel@tonic-gate 			goto failure;
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 		/* Authentication succeeded. */
187*7c478bd9Sstevel@tonic-gate 		return (1);
188*7c478bd9Sstevel@tonic-gate 	} else
189*7c478bd9Sstevel@tonic-gate 		/* Logging in as root or no local Kerberos realm. */
190*7c478bd9Sstevel@tonic-gate 		debug("Unable to authenticate to Kerberos.");
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate  failure:
193*7c478bd9Sstevel@tonic-gate 	krb4_cleanup_proc(authctxt);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	if (!options.kerberos_or_local_passwd)
196*7c478bd9Sstevel@tonic-gate 		return (0);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	/* Fall back to ordinary passwd authentication. */
199*7c478bd9Sstevel@tonic-gate 	return (-1);
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate void
krb4_cleanup_proc(void * context)203*7c478bd9Sstevel@tonic-gate krb4_cleanup_proc(void *context)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = (Authctxt *)context;
206*7c478bd9Sstevel@tonic-gate 	debug("krb4_cleanup_proc called");
207*7c478bd9Sstevel@tonic-gate 	if (authctxt->krb4_ticket_file) {
208*7c478bd9Sstevel@tonic-gate 		(void) dest_tkt();
209*7c478bd9Sstevel@tonic-gate 		xfree(authctxt->krb4_ticket_file);
210*7c478bd9Sstevel@tonic-gate 		authctxt->krb4_ticket_file = NULL;
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate int
auth_krb4(Authctxt * authctxt,KTEXT auth,char ** client,KTEXT reply)215*7c478bd9Sstevel@tonic-gate auth_krb4(Authctxt *authctxt, KTEXT auth, char **client, KTEXT reply)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	AUTH_DAT adat = {0};
218*7c478bd9Sstevel@tonic-gate 	Key_schedule schedule;
219*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in local, foreign;
220*7c478bd9Sstevel@tonic-gate 	char instance[INST_SZ];
221*7c478bd9Sstevel@tonic-gate 	socklen_t slen;
222*7c478bd9Sstevel@tonic-gate 	u_int cksum;
223*7c478bd9Sstevel@tonic-gate 	int r, s;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	s = packet_get_connection_in();
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	slen = sizeof(local);
228*7c478bd9Sstevel@tonic-gate 	memset(&local, 0, sizeof(local));
229*7c478bd9Sstevel@tonic-gate 	if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
230*7c478bd9Sstevel@tonic-gate 		debug("getsockname failed: %.100s", strerror(errno));
231*7c478bd9Sstevel@tonic-gate 	slen = sizeof(foreign);
232*7c478bd9Sstevel@tonic-gate 	memset(&foreign, 0, sizeof(foreign));
233*7c478bd9Sstevel@tonic-gate 	if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
234*7c478bd9Sstevel@tonic-gate 		debug("getpeername failed: %.100s", strerror(errno));
235*7c478bd9Sstevel@tonic-gate 		fatal_cleanup();
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 	instance[0] = '*';
238*7c478bd9Sstevel@tonic-gate 	instance[1] = 0;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	/* Get the encrypted request, challenge, and session key. */
241*7c478bd9Sstevel@tonic-gate 	if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance,
242*7c478bd9Sstevel@tonic-gate 	    0, &adat, ""))) {
243*7c478bd9Sstevel@tonic-gate 		debug("Kerberos v4 krb_rd_req: %.100s", krb_err_txt[r]);
244*7c478bd9Sstevel@tonic-gate 		return (0);
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	des_key_sched((des_cblock *) adat.session, schedule);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	*client = xmalloc(MAX_K_NAME_SZ);
249*7c478bd9Sstevel@tonic-gate 	(void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
250*7c478bd9Sstevel@tonic-gate 	    *adat.pinst ? "." : "", adat.pinst, adat.prealm);
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	/* Check ~/.klogin authorization now. */
253*7c478bd9Sstevel@tonic-gate 	if (kuserok(&adat, authctxt->user) != KSUCCESS) {
254*7c478bd9Sstevel@tonic-gate 		log("Kerberos v4 .klogin authorization failed for %s to "
255*7c478bd9Sstevel@tonic-gate 		    "account %s", *client, authctxt->user);
256*7c478bd9Sstevel@tonic-gate 		xfree(*client);
257*7c478bd9Sstevel@tonic-gate 		*client = NULL;
258*7c478bd9Sstevel@tonic-gate 		return (0);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	/* Increment the checksum, and return it encrypted with the
261*7c478bd9Sstevel@tonic-gate 	   session key. */
262*7c478bd9Sstevel@tonic-gate 	cksum = adat.checksum + 1;
263*7c478bd9Sstevel@tonic-gate 	cksum = htonl(cksum);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	/* If we can't successfully encrypt the checksum, we send back an
266*7c478bd9Sstevel@tonic-gate 	   empty message, admitting our failure. */
267*7c478bd9Sstevel@tonic-gate 	if ((r = krb_mk_priv((u_char *) & cksum, reply->dat, sizeof(cksum) + 1,
268*7c478bd9Sstevel@tonic-gate 	    schedule, &adat.session, &local, &foreign)) < 0) {
269*7c478bd9Sstevel@tonic-gate 		debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]);
270*7c478bd9Sstevel@tonic-gate 		reply->dat[0] = 0;
271*7c478bd9Sstevel@tonic-gate 		reply->length = 0;
272*7c478bd9Sstevel@tonic-gate 	} else
273*7c478bd9Sstevel@tonic-gate 		reply->length = r;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	/* Clear session key. */
276*7c478bd9Sstevel@tonic-gate 	memset(&adat.session, 0, sizeof(&adat.session));
277*7c478bd9Sstevel@tonic-gate 	return (1);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate #endif /* KRB4 */
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate #ifdef AFS
282*7c478bd9Sstevel@tonic-gate int
auth_krb4_tgt(Authctxt * authctxt,const char * string)283*7c478bd9Sstevel@tonic-gate auth_krb4_tgt(Authctxt *authctxt, const char *string)
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	CREDENTIALS creds;
286*7c478bd9Sstevel@tonic-gate 	struct passwd *pw;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	if ((pw = authctxt->pw) == NULL)
289*7c478bd9Sstevel@tonic-gate 		goto failure;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	temporarily_use_uid(pw);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	if (!radix_to_creds(string, &creds)) {
294*7c478bd9Sstevel@tonic-gate 		log("Protocol error decoding Kerberos v4 TGT");
295*7c478bd9Sstevel@tonic-gate 		goto failure;
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 	if (strncmp(creds.service, "", 1) == 0)	/* backward compatibility */
298*7c478bd9Sstevel@tonic-gate 		strlcpy(creds.service, "krbtgt", sizeof creds.service);
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	if (strcmp(creds.service, "krbtgt")) {
301*7c478bd9Sstevel@tonic-gate 		log("Kerberos v4 TGT (%s%s%s@%s) rejected for %s",
302*7c478bd9Sstevel@tonic-gate 		    creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
303*7c478bd9Sstevel@tonic-gate 		    creds.realm, pw->pw_name);
304*7c478bd9Sstevel@tonic-gate 		goto failure;
305*7c478bd9Sstevel@tonic-gate 	}
306*7c478bd9Sstevel@tonic-gate 	if (!krb4_init(authctxt))
307*7c478bd9Sstevel@tonic-gate 		goto failure;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
310*7c478bd9Sstevel@tonic-gate 		goto failure;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	if (save_credentials(creds.service, creds.instance, creds.realm,
313*7c478bd9Sstevel@tonic-gate 	    creds.session, creds.lifetime, creds.kvno, &creds.ticket_st,
314*7c478bd9Sstevel@tonic-gate 	    creds.issue_date) != KSUCCESS) {
315*7c478bd9Sstevel@tonic-gate 		debug("Kerberos v4 TGT refused: couldn't save credentials");
316*7c478bd9Sstevel@tonic-gate 		goto failure;
317*7c478bd9Sstevel@tonic-gate 	}
318*7c478bd9Sstevel@tonic-gate 	/* Successful authentication, passed all checks. */
319*7c478bd9Sstevel@tonic-gate 	chown(tkt_string(), pw->pw_uid, pw->pw_gid);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	debug("Kerberos v4 TGT accepted (%s%s%s@%s)",
322*7c478bd9Sstevel@tonic-gate 	    creds.pname, creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
323*7c478bd9Sstevel@tonic-gate 	memset(&creds, 0, sizeof(creds));
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	restore_uid();
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	return (1);
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate  failure:
330*7c478bd9Sstevel@tonic-gate 	krb4_cleanup_proc(authctxt);
331*7c478bd9Sstevel@tonic-gate 	memset(&creds, 0, sizeof(creds));
332*7c478bd9Sstevel@tonic-gate 	restore_uid();
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	return (0);
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate int
auth_afs_token(Authctxt * authctxt,const char * token_string)338*7c478bd9Sstevel@tonic-gate auth_afs_token(Authctxt *authctxt, const char *token_string)
339*7c478bd9Sstevel@tonic-gate {
340*7c478bd9Sstevel@tonic-gate 	CREDENTIALS creds;
341*7c478bd9Sstevel@tonic-gate 	struct passwd *pw;
342*7c478bd9Sstevel@tonic-gate 	uid_t uid;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	if ((pw = authctxt->pw) == NULL)
345*7c478bd9Sstevel@tonic-gate 		return (0);
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if (!radix_to_creds(token_string, &creds)) {
348*7c478bd9Sstevel@tonic-gate 		log("Protocol error decoding AFS token");
349*7c478bd9Sstevel@tonic-gate 		return (0);
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 	if (strncmp(creds.service, "", 1) == 0)	/* backward compatibility */
352*7c478bd9Sstevel@tonic-gate 		strlcpy(creds.service, "afs", sizeof creds.service);
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	if (strncmp(creds.pname, "AFS ID ", 7) == 0)
355*7c478bd9Sstevel@tonic-gate 		uid = atoi(creds.pname + 7);
356*7c478bd9Sstevel@tonic-gate 	else
357*7c478bd9Sstevel@tonic-gate 		uid = pw->pw_uid;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	if (kafs_settoken(creds.realm, uid, &creds)) {
360*7c478bd9Sstevel@tonic-gate 		log("AFS token (%s@%s) rejected for %s",
361*7c478bd9Sstevel@tonic-gate 		    creds.pname, creds.realm, pw->pw_name);
362*7c478bd9Sstevel@tonic-gate 		memset(&creds, 0, sizeof(creds));
363*7c478bd9Sstevel@tonic-gate 		return (0);
364*7c478bd9Sstevel@tonic-gate 	}
365*7c478bd9Sstevel@tonic-gate 	debug("AFS token accepted (%s@%s)", creds.pname, creds.realm);
366*7c478bd9Sstevel@tonic-gate 	memset(&creds, 0, sizeof(creds));
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	return (1);
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate #endif /* AFS */
371