Imported Upstream version 1.6.6
[debian/sudo] / auth / kerb4.c
1 /*
2  * Copyright (c) 1999, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * 4. Products derived from this software may not be called "Sudo" nor
20  *    may "Sudo" appear in their names without specific prior written
21  *    permission from the author.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "config.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <stdio.h>
40 #ifdef STDC_HEADERS
41 # include <stdlib.h>
42 # include <stddef.h>
43 #else
44 # ifdef HAVE_STDLIB_H
45 #  include <stdlib.h>
46 # endif
47 #endif /* STDC_HEADERS */
48 #ifdef HAVE_STRING_H
49 # include <string.h>
50 #else
51 # ifdef HAVE_STRINGS_H
52 #  include <strings.h>
53 # endif
54 #endif /* HAVE_STRING_H */
55 #ifdef HAVE_UNISTD_H
56 # include <unistd.h>
57 #endif /* HAVE_UNISTD_H */
58 #include <pwd.h>
59 #include <krb.h>
60
61 #include "sudo.h"
62 #include "sudo_auth.h"
63
64 #ifndef lint
65 static const char rcsid[] = "$Sudo: kerb4.c,v 1.6 2001/12/14 19:52:53 millert Exp $";
66 #endif /* lint */
67
68 int
69 kerb4_init(pw, promptp, auth)
70     struct passwd *pw;
71     char **promptp;
72     sudo_auth *auth;
73 {
74     static char realm[REALM_SZ];
75
76     /* Don't try to verify root */
77     if (pw->pw_uid == 0)
78         return(AUTH_FAILURE);
79
80     /* Get the local realm, or retrun failure (no krb.conf) */
81     if (krb_get_lrealm(realm, 1) != KSUCCESS)
82         return(AUTH_FAILURE);
83
84     /* Stash a pointer to the realm (used in kerb4_verify) */
85     auth->data = (VOID *) realm;
86
87     return(AUTH_SUCCESS);
88 }
89
90 int
91 kerb4_verify(pw, pass, auth)
92     struct passwd *pw;
93     char *pass;
94     sudo_auth *auth;
95 {
96     char tkfile[sizeof(_PATH_SUDO_TIMEDIR) + 4 + MAX_UID_T_LEN];
97     char *realm = (char *) auth->data;
98     int error;
99
100     /*
101      * Set the ticket file to be in sudo sudo timedir so we don't
102      * wipe out other (real) kerberos tickets.
103      */
104     (void) sprintf(tkfile, "%s/tkt%ld", _PATH_SUDO_TIMEDIR, (long) pw->pw_uid);
105     (void) krb_set_tkt_string(tkfile);
106
107     /* Convert the password to a ticket given. */
108     error = krb_get_pw_in_tkt(pw->pw_name, "", realm, "krbtgt", realm,
109         DEFAULT_TKT_LIFE, pass);
110
111     switch (error) {
112         case INTK_OK:
113             dest_tkt();                 /* we are done with the temp ticket */
114             return(AUTH_SUCCESS);
115             break;
116         case INTK_BADPW:
117         case KDC_PR_UNKNOWN:
118             break;
119         default:
120             (void) fprintf(stderr, "Warning: Kerberos error: %s\n",
121                 krb_err_txt[error]);
122     }
123
124     return(AUTH_FAILURE);
125 }