a27303b0a9d4b9076728412314abbe92e3f30137
[debian/sudo] / auth / dce.c
1 /*
2  * Copyright (c) 1996, 1998-2005 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  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
16  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18  *
19  * Sponsored in part by the Defense Advanced Research Projects
20  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22  */
23 /*
24  *  The code below basically comes from the examples supplied on
25  *  the OSF DCE 1.0.3 manpages for the sec_login routines, with
26  *  enough additional polishing to make the routine work with the
27  *  rest of sudo.
28  *
29  *  This code is known to work on HP 700 and 800 series systems
30  *  running HP-UX 9.X and 10.X, with either HP's version 1.2.1 of DCE.
31  *  (aka, OSF DCE 1.0.3) or with HP's version 1.4 of DCE (aka, OSF
32  *  DCE 1.1).
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
60 #include <dce/rpc.h>
61 #include <dce/sec_login.h>
62 #include <dce/dce_error.h> /* required to call dce_error_inq_text routine */
63
64 #include "sudo.h"
65 #include "sudo_auth.h"
66
67 #ifndef lint
68 __unused static const char rcsid[] = "$Sudo: dce.c,v 1.11.2.2 2007/06/12 01:28:42 millert Exp $";
69 #endif /* lint */
70
71 static int check_dce_status __P((error_status_t, char *));
72
73 int
74 dce_verify(pw, plain_pw, auth)
75     struct passwd *pw;
76     char *plain_pw;
77     sudo_auth *auth;
78 {
79     struct passwd               temp_pw;
80     sec_passwd_rec_t            password_rec;
81     sec_login_handle_t          login_context;
82     boolean32                   reset_passwd;
83     sec_login_auth_src_t        auth_src;
84     error_status_t              status;
85
86     /*
87      * Create the local context of the DCE principal necessary
88      * to perform authenticated network operations.  The network
89      * identity set up by this operation cannot be used until it
90      * is validated via sec_login_validate_identity().
91      */
92     if (sec_login_setup_identity((unsigned_char_p_t) pw->pw_name,
93         sec_login_no_flags, &login_context, &status)) {
94
95         if (check_dce_status(status, "sec_login_setup_identity(1):"))
96             return(AUTH_FAILURE);
97
98         password_rec.key.key_type = sec_passwd_plain;
99         password_rec.key.tagged_union.plain = (idl_char *) plain_pw;
100         password_rec.pepper = NULL;
101         password_rec.version_number = sec_passwd_c_version_none;
102
103         /* Validate the login context with the password */
104         if (sec_login_validate_identity(login_context, &password_rec,
105             &reset_passwd, &auth_src, &status)) {
106
107             if (check_dce_status(status, "sec_login_validate_identity(1):"))
108                 return(AUTH_FAILURE);
109
110             /*
111              * Certify that the DCE Security Server used to set
112              * up and validate a login context is legitimate.  Makes
113              * sure that we didn't get spoofed by another DCE server.
114              */
115             if (!sec_login_certify_identity(login_context, &status)) {
116                 (void) fprintf(stderr, "Whoa! Bogus authentication server!\n");
117                 (void) check_dce_status(status,"sec_login_certify_identity(1):");
118                 return(AUTH_FAILURE);
119             }
120             if (check_dce_status(status, "sec_login_certify_identity(2):"))
121                 return(AUTH_FAILURE);
122
123             /*
124              * Sets the network credentials to those specified
125              * by the now validated login context.
126              */
127             sec_login_set_context(login_context, &status);
128             if (check_dce_status(status, "sec_login_set_context:"))
129                 return(AUTH_FAILURE);
130
131             /*
132              * Oops, your credentials were no good. Possibly
133              * caused by clock times out of adjustment between
134              * DCE client and DCE security server...
135              */
136             if (auth_src != sec_login_auth_src_network) {
137                     (void) fprintf(stderr,
138                         "You have no network credentials.\n");
139                     return(AUTH_FAILURE);
140             }
141             /* Check if the password has aged and is thus no good */
142             if (reset_passwd) {
143                     (void) fprintf(stderr,
144                         "Your DCE password needs resetting.\n");
145                     return(AUTH_FAILURE);
146             }
147
148             /*
149              * We should be a valid user by this point.  Pull the
150              * user's password structure from the DCE security
151              * server just to make sure.  If we get it with no
152              * problems, then we really are legitimate...
153              */
154             sec_login_get_pwent(login_context, (sec_login_passwd_t) &temp_pw,
155                 &status);
156             if (check_dce_status(status, "sec_login_get_pwent:"))
157                 return(AUTH_FAILURE);
158
159             /*
160              * If we get to here, then the pwent above properly fetched
161              * the password structure from the DCE registry, so the user
162              * must be valid.  We don't really care what the user's
163              * registry password is, just that the user could be
164              * validated.  In fact, if we tried to compare the local
165              * password to the DCE entry at this point, the operation
166              * would fail if the hidden password feature is turned on,
167              * because the password field would contain an asterisk.
168              * Also go ahead and destroy the user's DCE login context
169              * before we leave here (and don't bother checking the
170              * status), in order to clean up credentials files in
171              * /opt/dcelocal/var/security/creds.  By doing this, we are
172              * assuming that the user will not need DCE authentication
173              * later in the program, only local authentication.  If this
174              * is not true, then the login_context will have to be
175              * returned to the calling program, and the context purged
176              * somewhere later in the program.
177              */
178             sec_login_purge_context(&login_context, &status);
179             return(AUTH_SUCCESS);
180         } else {
181             if(check_dce_status(status, "sec_login_validate_identity(2):"))
182                 return(AUTH_FAILURE);
183             sec_login_purge_context(&login_context, &status);
184             if(check_dce_status(status, "sec_login_purge_context:"))
185                 return(AUTH_FAILURE);
186         }
187     }
188     (void) check_dce_status(status, "sec_login_setup_identity(2):");
189     return(AUTH_FAILURE);
190 }
191
192 /* Returns 0 for DCE "ok" status, 1 otherwise */
193 static int
194 check_dce_status(input_status, comment)
195     error_status_t input_status;
196     char *comment;
197 {
198     int error_stat;
199     unsigned char error_string[dce_c_error_string_len];
200
201     if (input_status == rpc_s_ok)
202         return(0);
203     dce_error_inq_text(input_status, error_string, &error_stat);
204     (void) fprintf(stderr, "%s %s\n", comment, error_string);
205     return(1);
206 }