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