1 /*
2 * Copyright (c) 1999, 2001, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19 */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <stdio.h>
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 # include <stddef.h>
29 #else
30 # ifdef HAVE_STDLIB_H
31 # include <stdlib.h>
32 # endif
33 #endif /* STDC_HEADERS */
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #else
37 # ifdef HAVE_STRINGS_H
38 # include <strings.h>
39 # endif
40 #endif /* HAVE_STRING_H */
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif /* HAVE_UNISTD_H */
44 #include <pwd.h>
45 #include <krb.h>
46
47 #include "sudo.h"
48 #include "sudo_auth.h"
49
50 #ifndef lint
51 static const char rcsid[] = "$Sudo: kerb4.c,v 1.11 2004/02/13 21:36:47 millert Exp $";
52 #endif /* lint */
53
54 int
kerb4_init(pw,promptp,auth)55 kerb4_init(pw, promptp, auth)
56 struct passwd *pw;
57 char **promptp;
58 sudo_auth *auth;
59 {
60 static char realm[REALM_SZ];
61
62 /* Don't try to verify root */
63 if (pw->pw_uid == 0)
64 return(AUTH_FAILURE);
65
66 /* Get the local realm, or retrun failure (no krb.conf) */
67 if (krb_get_lrealm(realm, 1) != KSUCCESS)
68 return(AUTH_FAILURE);
69
70 /* Stash a pointer to the realm (used in kerb4_verify) */
71 auth->data = (VOID *) realm;
72
73 return(AUTH_SUCCESS);
74 }
75
76 int
kerb4_verify(pw,pass,auth)77 kerb4_verify(pw, pass, auth)
78 struct passwd *pw;
79 char *pass;
80 sudo_auth *auth;
81 {
82 char tkfile[sizeof(_PATH_SUDO_TIMEDIR) + 4 + MAX_UID_T_LEN];
83 char *realm = (char *) auth->data;
84 int error;
85
86 /*
87 * Set the ticket file to be in sudo sudo timedir so we don't
88 * wipe out other (real) kerberos tickets.
89 */
90 (void) snprintf(tkfile, sizeof(tkfile), "%s/tkt%lu",
91 _PATH_SUDO_TIMEDIR, (unsigned long) pw->pw_uid);
92 (void) krb_set_tkt_string(tkfile);
93
94 /* Convert the password to a ticket given. */
95 error = krb_get_pw_in_tkt(pw->pw_name, "", realm, "krbtgt", realm,
96 DEFAULT_TKT_LIFE, pass);
97
98 switch (error) {
99 case INTK_OK:
100 dest_tkt(); /* we are done with the temp ticket */
101 return(AUTH_SUCCESS);
102 break;
103 case INTK_BADPW:
104 case KDC_PR_UNKNOWN:
105 break;
106 default:
107 (void) fprintf(stderr, "Warning: Kerberos error: %s\n",
108 krb_err_txt[error]);
109 }
110
111 return(AUTH_FAILURE);
112 }
113