Imported Upstream version 1.6.6
[debian/sudo] / auth / dce.c
1 /*
2  * Copyright (c) 1996, 1998, 1999, 2001
3  *      Todd C. Miller <Todd.Miller@courtesan.com>.  All rights reserved.
4  *
5  * This code is derived from software contributed by Jeff Earickson
6  * of Colby College, Waterville, ME <jaearick@colby.edu>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * 4. Products derived from this software may not be called "Sudo" nor
23  *    may "Sudo" appear in their names without specific prior written
24  *    permission from the author.
25  *
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
29  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*
38  *  The code below basically comes from the examples supplied on
39  *  the OSF DCE 1.0.3 manpages for the sec_login routines, with
40  *  enough additional polishing to make the routine work with the
41  *  rest of sudo.
42  *
43  *  This code is known to work on HP 700 and 800 series systems
44  *  running HP-UX 9.X and 10.X, with either HP's version 1.2.1 of DCE.
45  *  (aka, OSF DCE 1.0.3) or with HP's version 1.4 of DCE (aka, OSF
46  *  DCE 1.1).
47  */
48
49 #include "config.h"
50
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <stdio.h>
54 #ifdef STDC_HEADERS
55 # include <stdlib.h>
56 # include <stddef.h>
57 #else
58 # ifdef HAVE_STDLIB_H
59 #  include <stdlib.h>
60 # endif
61 #endif /* STDC_HEADERS */
62 #ifdef HAVE_STRING_H
63 # include <string.h>
64 #else
65 # ifdef HAVE_STRINGS_H
66 #  include <strings.h>
67 # endif
68 #endif /* HAVE_STRING_H */
69 #ifdef HAVE_UNISTD_H
70 # include <unistd.h>
71 #endif /* HAVE_UNISTD_H */
72 #include <pwd.h>
73
74 #include <dce/rpc.h>
75 #include <dce/sec_login.h>
76 #include <dce/dce_error.h> /* required to call dce_error_inq_text routine */
77
78 #include "sudo.h"
79 #include "sudo_auth.h"
80
81 #ifndef lint
82 static const char rcsid[] = "$Sudo: dce.c,v 1.8 2001/12/14 19:52:53 millert Exp $";
83 #endif /* lint */
84
85 static int check_dce_status __P((error_status_t, char *));
86
87 int
88 dce_verify(pw, plain_pw, auth)
89     struct passwd *pw;
90     char *plain_pw;
91     sudo_auth *auth;
92 {
93     struct passwd               temp_pw;
94     sec_passwd_rec_t            password_rec;
95     sec_login_handle_t          login_context;
96     boolean32                   reset_passwd;
97     sec_login_auth_src_t        auth_src;
98     error_status_t              status;
99
100     /*
101      * Create the local context of the DCE principal necessary
102      * to perform authenticated network operations.  The network
103      * identity set up by this operation cannot be used until it
104      * is validated via sec_login_validate_identity().
105      */
106     if (sec_login_setup_identity((unsigned_char_p_t) pw->pw_name,
107         sec_login_no_flags, &login_context, &status)) {
108
109         if (check_dce_status(status, "sec_login_setup_identity(1):"))
110             return(AUTH_FAILURE);
111
112         password_rec.key.key_type = sec_passwd_plain;
113         password_rec.key.tagged_union.plain = (idl_char *) plain_pw;
114         password_rec.pepper = NULL;
115         password_rec.version_number = sec_passwd_c_version_none;
116
117         /* Validate the login context with the password */
118         if (sec_login_validate_identity(login_context, &password_rec,
119             &reset_passwd, &auth_src, &status)) {
120
121             if (check_dce_status(status, "sec_login_validate_identity(1):"))
122                 return(AUTH_FAILURE);
123
124             /*
125              * Certify that the DCE Security Server used to set
126              * up and validate a login context is legitimate.  Makes
127              * sure that we didn't get spoofed by another DCE server.
128              */
129             if (!sec_login_certify_identity(login_context, &status)) {
130                 (void) fprintf(stderr, "Whoa! Bogus authentication server!\n");
131                 (void) check_dce_status(status,"sec_login_certify_identity(1):");
132                 return(AUTH_FAILURE);
133             }
134             if (check_dce_status(status, "sec_login_certify_identity(2):"))
135                 return(AUTH_FAILURE);
136
137             /*
138              * Sets the network credentials to those specified
139              * by the now validated login context.
140              */
141             sec_login_set_context(login_context, &status);
142             if (check_dce_status(status, "sec_login_set_context:"))
143                 return(AUTH_FAILURE);
144
145             /*
146              * Oops, your credentials were no good. Possibly
147              * caused by clock times out of adjustment between
148              * DCE client and DCE security server...
149              */
150             if (auth_src != sec_login_auth_src_network) {
151                     (void) fprintf(stderr,
152                         "You have no network credentials.\n");
153                     return(AUTH_FAILURE);
154             }
155             /* Check if the password has aged and is thus no good */
156             if (reset_passwd) {
157                     (void) fprintf(stderr,
158                         "Your DCE password needs resetting.\n");
159                     return(AUTH_FAILURE);
160             }
161
162             /*
163              * We should be a valid user by this point.  Pull the
164              * user's password structure from the DCE security
165              * server just to make sure.  If we get it with no
166              * problems, then we really are legitimate...
167              */
168             sec_login_get_pwent(login_context, (sec_login_passwd_t) &temp_pw,
169                 &status);
170             if (check_dce_status(status, "sec_login_get_pwent:"))
171                 return(AUTH_FAILURE);
172
173             /*
174              * If we get to here, then the pwent above properly fetched
175              * the password structure from the DCE registry, so the user
176              * must be valid.  We don't really care what the user's
177              * registry password is, just that the user could be
178              * validated.  In fact, if we tried to compare the local
179              * password to the DCE entry at this point, the operation
180              * would fail if the hidden password feature is turned on,
181              * because the password field would contain an asterisk.
182              * Also go ahead and destroy the user's DCE login context
183              * before we leave here (and don't bother checking the
184              * status), in order to clean up credentials files in
185              * /opt/dcelocal/var/security/creds.  By doing this, we are
186              * assuming that the user will not need DCE authentication
187              * later in the program, only local authentication.  If this
188              * is not true, then the login_context will have to be
189              * returned to the calling program, and the context purged
190              * somewhere later in the program.
191              */
192             sec_login_purge_context(&login_context, &status);
193             return(AUTH_SUCCESS);
194         } else {
195             if(check_dce_status(status, "sec_login_validate_identity(2):"))
196                 return(AUTH_FAILURE);
197             sec_login_purge_context(&login_context, &status);
198             if(check_dce_status(status, "sec_login_purge_context:"))
199                 return(AUTH_FAILURE);
200         }
201     }
202     (void) check_dce_status(status, "sec_login_setup_identity(2):");
203     return(AUTH_FAILURE);
204 }
205
206 /* Returns 0 for DCE "ok" status, 1 otherwise */
207 static int
208 check_dce_status(input_status, comment)
209     error_status_t input_status;
210     char *comment;
211 {
212     int error_stat;
213     unsigned char error_string[dce_c_error_string_len];
214
215     if (input_status == rpc_s_ok)
216         return(0);
217     dce_error_inq_text(input_status, error_string, &error_stat);
218     (void) fprintf(stderr, "%s %s\n", comment, error_string);
219     return(1);
220 }