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