]> andersk Git - openssh.git/blob - auth-krb4.c
Hopefully things did not get mixed around too much. It compiles under
[openssh.git] / 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.22 2001/01/21 19:05:41 markus Exp $");
27
28 #ifdef KRB4
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 "auth.h"
36 #include "radix.h"
37
38 char *ticket = NULL;
39
40 extern ServerOptions options;
41
42 /*
43  * try krb4 authentication,
44  * return 1 on success, 0 on failure, -1 if krb4 is not available
45  */
46
47 int
48 auth_krb4_password(struct passwd * pw, const char *password)
49 {
50         AUTH_DAT adata;
51         KTEXT_ST tkt;
52         struct hostent *hp;
53         u_long faddr;
54         char localhost[MAXHOSTNAMELEN];
55         char phost[INST_SZ];
56         char realm[REALM_SZ];
57         int r;
58
59         /*
60          * Try Kerberos password authentication only for non-root
61          * users and only if Kerberos is installed.
62          */
63         if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
64
65                 /* Set up our ticket file. */
66                 if (!krb4_init(pw->pw_uid)) {
67                         log("Couldn't initialize Kerberos ticket file for %s!",
68                             pw->pw_name);
69                         goto kerberos_auth_failure;
70                 }
71                 /* Try to get TGT using our password. */
72                 r = krb_get_pw_in_tkt((char *) pw->pw_name, "",
73                     realm, "krbtgt", realm,
74                     DEFAULT_TKT_LIFE, (char *) password);
75                 if (r != INTK_OK) {
76                         packet_send_debug("Kerberos V4 password "
77                             "authentication for %s failed: %s",
78                             pw->pw_name, krb_err_txt[r]);
79                         goto kerberos_auth_failure;
80                 }
81                 /* Successful authentication. */
82                 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
83
84                 /*
85                  * Now that we have a TGT, try to get a local
86                  * "rcmd" ticket to ensure that we are not talking
87                  * to a bogus Kerberos server.
88                  */
89                 (void) gethostname(localhost, sizeof(localhost));
90                 (void) strlcpy(phost, (char *) krb_get_phost(localhost),
91                     INST_SZ);
92                 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
93
94                 if (r == KSUCCESS) {
95                         if (!(hp = gethostbyname(localhost))) {
96                                 log("Couldn't get local host address!");
97                                 goto kerberos_auth_failure;
98                         }
99                         memmove((void *) &faddr, (void *) hp->h_addr,
100                             sizeof(faddr));
101
102                         /* Verify our "rcmd" ticket. */
103                         r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost,
104                             faddr, &adata, "");
105                         if (r == RD_AP_UNDEC) {
106                                 /*
107                                  * Probably didn't have a srvtab on
108                                  * localhost. Disallow login.
109                                  */
110                                 log("Kerberos V4 TGT for %s unverifiable, "
111                                     "no srvtab installed? krb_rd_req: %s",
112                                     pw->pw_name, krb_err_txt[r]);
113                                 goto kerberos_auth_failure;
114                         } else if (r != KSUCCESS) {
115                                 log("Kerberos V4 %s ticket unverifiable: %s",
116                                     KRB4_SERVICE_NAME, krb_err_txt[r]);
117                                 goto kerberos_auth_failure;
118                         }
119                 } else if (r == KDC_PR_UNKNOWN) {
120                         /*
121                          * Disallow login if no rcmd service exists, and
122                          * log the error.
123                          */
124                         log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s "
125                             "not registered, or srvtab is wrong?", pw->pw_name,
126                         krb_err_txt[r], KRB4_SERVICE_NAME, phost);
127                         goto kerberos_auth_failure;
128                 } else {
129                         /*
130                          * TGT is bad, forget it. Possibly spoofed!
131                          */
132                         packet_send_debug("WARNING: Kerberos V4 TGT "
133                             "possibly spoofed for %s: %s",
134                             pw->pw_name, krb_err_txt[r]);
135                         goto kerberos_auth_failure;
136                 }
137
138                 /* Authentication succeeded. */
139                 return 1;
140
141 kerberos_auth_failure:
142                 krb4_cleanup_proc(NULL);
143
144                 if (!options.kerberos_or_local_passwd)
145                         return 0;
146         } else {
147                 /* Logging in as root or no local Kerberos realm. */
148                 packet_send_debug("Unable to authenticate to Kerberos.");
149         }
150         /* Fall back to ordinary passwd authentication. */
151         return -1;
152 }
153
154 void
155 krb4_cleanup_proc(void *ignore)
156 {
157         debug("krb4_cleanup_proc called");
158         if (ticket) {
159                 (void) dest_tkt();
160                 xfree(ticket);
161                 ticket = NULL;
162         }
163 }
164
165 int
166 krb4_init(uid_t uid)
167 {
168         static int cleanup_registered = 0;
169         const char *tkt_root = TKT_ROOT;
170         struct stat st;
171         int fd;
172
173         if (!ticket) {
174                 /* Set unique ticket string manually since we're still root. */
175                 ticket = xmalloc(MAXPATHLEN);
176 #ifdef AFS
177                 if (lstat("/ticket", &st) != -1)
178                         tkt_root = "/ticket/";
179 #endif /* AFS */
180                 snprintf(ticket, MAXPATHLEN, "%s%u_%d", tkt_root, uid, getpid());
181                 (void) krb_set_tkt_string(ticket);
182         }
183         /* Register ticket cleanup in case of fatal error. */
184         if (!cleanup_registered) {
185                 fatal_add_cleanup(krb4_cleanup_proc, NULL);
186                 cleanup_registered = 1;
187         }
188         /* Try to create our ticket file. */
189         if ((fd = mkstemp(ticket)) != -1) {
190                 close(fd);
191                 return 1;
192         }
193         /* Ticket file exists - make sure user owns it (just passed ticket). */
194         if (lstat(ticket, &st) != -1) {
195                 if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&
196                     st.st_uid == uid)
197                         return 1;
198         }
199         /* Failure - cancel cleanup function, leaving bad ticket for inspection. */
200         log("WARNING: bad ticket file %s", ticket);
201         fatal_remove_cleanup(krb4_cleanup_proc, NULL);
202         cleanup_registered = 0;
203         xfree(ticket);
204         ticket = NULL;
205
206         return 0;
207 }
208
209 int
210 auth_krb4(const char *server_user, KTEXT auth, char **client)
211 {
212         AUTH_DAT adat = {0};
213         KTEXT_ST reply;
214         char instance[INST_SZ];
215         int r, s;
216         socklen_t slen;
217         u_int cksum;
218         Key_schedule schedule;
219         struct sockaddr_in local, foreign;
220
221         s = packet_get_connection_in();
222
223         slen = sizeof(local);
224         memset(&local, 0, sizeof(local));
225         if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
226                 debug("getsockname failed: %.100s", strerror(errno));
227         slen = sizeof(foreign);
228         memset(&foreign, 0, sizeof(foreign));
229         if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
230                 debug("getpeername failed: %.100s", strerror(errno));
231                 fatal_cleanup();
232         }
233         instance[0] = '*';
234         instance[1] = 0;
235
236         /* Get the encrypted request, challenge, and session key. */
237         if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, 0, &adat, ""))) {
238                 packet_send_debug("Kerberos V4 krb_rd_req: %.100s", krb_err_txt[r]);
239                 return 0;
240         }
241         des_key_sched((des_cblock *) adat.session, schedule);
242
243         *client = xmalloc(MAX_K_NAME_SZ);
244         (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
245             *adat.pinst ? "." : "", adat.pinst, adat.prealm);
246
247         /* Check ~/.klogin authorization now. */
248         if (kuserok(&adat, (char *) server_user) != KSUCCESS) {
249                 packet_send_debug("Kerberos V4 .klogin authorization failed!");
250                 log("Kerberos V4 .klogin authorization failed for %s to account %s",
251                     *client, server_user);
252                 xfree(*client);
253                 return 0;
254         }
255         /* Increment the checksum, and return it encrypted with the
256            session key. */
257         cksum = adat.checksum + 1;
258         cksum = htonl(cksum);
259
260         /* If we can't successfully encrypt the checksum, we send back an
261            empty message, admitting our failure. */
262         if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1,
263             schedule, &adat.session, &local, &foreign)) < 0) {
264                 packet_send_debug("Kerberos V4 mk_priv: (%d) %s", r, krb_err_txt[r]);
265                 reply.dat[0] = 0;
266                 reply.length = 0;
267         } else
268                 reply.length = r;
269
270         /* Clear session key. */
271         memset(&adat.session, 0, sizeof(&adat.session));
272
273         packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
274         packet_put_string((char *) reply.dat, reply.length);
275         packet_send();
276         packet_write_wait();
277         return 1;
278 }
279 #endif /* KRB4 */
280
281 #ifdef AFS
282 int
283 auth_kerberos_tgt(struct passwd *pw, const char *string)
284 {
285         CREDENTIALS creds;
286
287         if (pw == NULL)
288                 goto auth_kerberos_tgt_failure;
289         if (!radix_to_creds(string, &creds)) {
290                 log("Protocol error decoding Kerberos V4 tgt");
291                 packet_send_debug("Protocol error decoding Kerberos V4 tgt");
292                 goto auth_kerberos_tgt_failure;
293         }
294         if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
295                 strlcpy(creds.service, "krbtgt", sizeof creds.service);
296
297         if (strcmp(creds.service, "krbtgt")) {
298                 log("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", creds.pname,
299                     creds.pinst[0] ? "." : "", creds.pinst, creds.realm,
300                     pw->pw_name);
301                 packet_send_debug("Kerberos V4 tgt (%s%s%s@%s) rejected for %s",
302                     creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
303                     creds.realm, pw->pw_name);
304                 goto auth_kerberos_tgt_failure;
305         }
306         if (!krb4_init(pw->pw_uid))
307                 goto auth_kerberos_tgt_failure;
308
309         if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
310                 goto auth_kerberos_tgt_failure;
311
312         if (save_credentials(creds.service, creds.instance, creds.realm,
313             creds.session, creds.lifetime, creds.kvno,
314             &creds.ticket_st, creds.issue_date) != KSUCCESS) {
315                 packet_send_debug("Kerberos V4 tgt refused: couldn't save credentials");
316                 goto auth_kerberos_tgt_failure;
317         }
318         /* Successful authentication, passed all checks. */
319         chown(tkt_string(), pw->pw_uid, pw->pw_gid);
320
321         packet_send_debug("Kerberos V4 tgt accepted (%s.%s@%s, %s%s%s@%s)",
322             creds.service, creds.instance, creds.realm, creds.pname,
323             creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
324         memset(&creds, 0, sizeof(creds));
325         packet_start(SSH_SMSG_SUCCESS);
326         packet_send();
327         packet_write_wait();
328         return 1;
329
330 auth_kerberos_tgt_failure:
331         krb4_cleanup_proc(NULL);
332         memset(&creds, 0, sizeof(creds));
333         packet_start(SSH_SMSG_FAILURE);
334         packet_send();
335         packet_write_wait();
336         return 0;
337 }
338
339 int
340 auth_afs_token(struct passwd *pw, const char *token_string)
341 {
342         CREDENTIALS creds;
343         uid_t uid;
344
345         if (pw == NULL) {
346                 /* XXX fake protocol error */
347                 packet_send_debug("Protocol error decoding AFS token");
348                 packet_start(SSH_SMSG_FAILURE);
349                 packet_send();
350                 packet_write_wait();
351                 return 0;
352         }
353         if (!radix_to_creds(token_string, &creds)) {
354                 log("Protocol error decoding AFS token");
355                 packet_send_debug("Protocol error decoding AFS token");
356                 packet_start(SSH_SMSG_FAILURE);
357                 packet_send();
358                 packet_write_wait();
359                 return 0;
360         }
361         if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
362                 strlcpy(creds.service, "afs", sizeof creds.service);
363
364         if (strncmp(creds.pname, "AFS ID ", 7) == 0)
365                 uid = atoi(creds.pname + 7);
366         else
367                 uid = pw->pw_uid;
368
369         if (kafs_settoken(creds.realm, uid, &creds)) {
370                 log("AFS token (%s@%s) rejected for %s", creds.pname, creds.realm,
371                     pw->pw_name);
372                 packet_send_debug("AFS token (%s@%s) rejected for %s", creds.pname,
373                     creds.realm, pw->pw_name);
374                 memset(&creds, 0, sizeof(creds));
375                 packet_start(SSH_SMSG_FAILURE);
376                 packet_send();
377                 packet_write_wait();
378                 return 0;
379         }
380         packet_send_debug("AFS token accepted (%s@%s, %s@%s)", creds.service,
381             creds.realm, creds.pname, creds.realm);
382         memset(&creds, 0, sizeof(creds));
383         packet_start(SSH_SMSG_SUCCESS);
384         packet_send();
385         packet_write_wait();
386         return 1;
387 }
388 #endif /* AFS */
This page took 0.084992 seconds and 5 git commands to generate.