]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
http://www.psc.edu/networking/projects/hpn-ssh/openssh-5.1p1-hpn13v5.diff.gz committe...
[gssapi-openssh.git] / openssh / auth2.c
1 /* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "includes.h"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
31
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "xmalloc.h"
39 #include "atomicio.h"
40 #include "ssh2.h"
41 #include "packet.h"
42 #include "log.h"
43 #include "buffer.h"
44 #include "servconf.h"
45 #include "compat.h"
46 #include "key.h"
47 #include "hostfile.h"
48 #include "auth.h"
49 #include "dispatch.h"
50 #include "pathnames.h"
51 #include "buffer.h"
52 #include "canohost.h"
53
54 #ifdef GSSAPI
55 #include "ssh-gss.h"
56 #endif
57 #include "monitor_wrap.h"
58
59 /* import */
60 extern ServerOptions options;
61 extern u_char *session_id2;
62 extern u_int session_id2_len;
63 extern Buffer loginmsg;
64
65 /* methods */
66
67 extern Authmethod method_none;
68 extern Authmethod method_pubkey;
69 extern Authmethod method_passwd;
70 extern Authmethod method_kbdint;
71 extern Authmethod method_hostbased;
72 #ifdef GSSAPI
73 extern Authmethod method_gssapi;
74 #endif
75
76 static int log_flag = 0;
77
78
79 Authmethod *authmethods[] = {
80         &method_none,
81         &method_pubkey,
82 #ifdef GSSAPI
83         &method_gssapi,
84 #endif
85         &method_passwd,
86         &method_kbdint,
87         &method_hostbased,
88         NULL
89 };
90
91 /* protocol */
92
93 static void input_service_request(int, u_int32_t, void *);
94 static void input_userauth_request(int, u_int32_t, void *);
95
96 /* helper */
97 static Authmethod *authmethod_lookup(const char *);
98 static char *authmethods_get(void);
99
100 char *
101 auth2_read_banner(void)
102 {
103         struct stat st;
104         char *banner = NULL;
105         size_t len, n;
106         int fd;
107
108         if ((fd = open(options.banner, O_RDONLY)) == -1)
109                 return (NULL);
110         if (fstat(fd, &st) == -1) {
111                 close(fd);
112                 return (NULL);
113         }
114         if (st.st_size > 1*1024*1024) {
115                 close(fd);
116                 return (NULL);
117         }
118
119         len = (size_t)st.st_size;               /* truncate */
120         banner = xmalloc(len + 1);
121         n = atomicio(read, fd, banner, len);
122         close(fd);
123
124         if (n != len) {
125                 xfree(banner);
126                 return (NULL);
127         }
128         banner[n] = '\0';
129
130         return (banner);
131 }
132
133 void
134 userauth_send_banner(const char *msg)
135 {
136         if (datafellows & SSH_BUG_BANNER)
137                 return;
138
139         packet_start(SSH2_MSG_USERAUTH_BANNER);
140         packet_put_cstring(msg);
141         packet_put_cstring("");         /* language, unused */
142         packet_send();
143         debug("%s: sent", __func__);
144 }
145
146 static void
147 userauth_banner(void)
148 {
149         char *banner = NULL;
150
151         if (options.banner == NULL ||
152             strcasecmp(options.banner, "none") == 0 ||
153             (datafellows & SSH_BUG_BANNER) != 0)
154                 return;
155
156         if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
157                 goto done;
158         userauth_send_banner(banner);
159
160 done:
161         if (banner)
162                 xfree(banner);
163 }
164
165 /*
166  * loop until authctxt->success == TRUE
167  */
168 void
169 do_authentication2(Authctxt *authctxt)
170 {
171         dispatch_init(&dispatch_protocol_error);
172         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
173         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
174 }
175
176 /*ARGSUSED*/
177 static void
178 input_service_request(int type, u_int32_t seq, void *ctxt)
179 {
180         Authctxt *authctxt = ctxt;
181         u_int len;
182         int acceptit = 0;
183         char *service = packet_get_string(&len);
184         packet_check_eom();
185
186         if (authctxt == NULL)
187                 fatal("input_service_request: no authctxt");
188
189         if (strcmp(service, "ssh-userauth") == 0) {
190                 if (!authctxt->success) {
191                         acceptit = 1;
192                         /* now we can handle user-auth requests */
193                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
194                 }
195         }
196         /* XXX all other service requests are denied */
197
198         if (acceptit) {
199                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
200                 packet_put_cstring(service);
201                 packet_send();
202                 packet_write_wait();
203         } else {
204                 debug("bad service request %s", service);
205                 packet_disconnect("bad service request %s", service);
206         }
207         xfree(service);
208 }
209
210 /*ARGSUSED*/
211 static void
212 input_userauth_request(int type, u_int32_t seq, void *ctxt)
213 {
214         Authctxt *authctxt = ctxt;
215         Authmethod *m = NULL;
216         char *user, *service, *method, *style = NULL;
217         int authenticated = 0;
218
219         if (authctxt == NULL)
220                 fatal("input_userauth_request: no authctxt");
221
222         user = packet_get_string(NULL);
223         service = packet_get_string(NULL);
224         method = packet_get_string(NULL);
225         debug("userauth-request for user %s service %s method %s", user, service, method);
226         if (!log_flag) {
227                 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s", 
228                       get_remote_ipaddr(), get_remote_port(), user);
229                 log_flag = 1;
230         }
231         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
232
233         if ((style = strchr(user, ':')) != NULL)
234                 *style++ = 0;
235
236         if (authctxt->attempt++ == 0) {
237                 /* setup auth context */
238                 authctxt->pw = PRIVSEP(getpwnamallow(user));
239                 authctxt->user = xstrdup(user);
240                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
241                         authctxt->valid = 1;
242                         debug2("input_userauth_request: setting up authctxt for %s", user);
243                 } else {
244                         logit("input_userauth_request: invalid user %s", user);
245                         authctxt->pw = fakepw();
246 #ifdef SSH_AUDIT_EVENTS
247                         PRIVSEP(audit_event(SSH_INVALID_USER));
248 #endif
249                 }
250 #ifdef USE_PAM
251                 if (options.use_pam)
252                         PRIVSEP(start_pam(authctxt));
253 #endif
254                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
255                     use_privsep ? " [net]" : "");
256                 authctxt->service = xstrdup(service);
257                 authctxt->style = style ? xstrdup(style) : NULL;
258                 if (use_privsep)
259                         mm_inform_authserv(service, style);
260                 userauth_banner();
261         } else if (strcmp(user, authctxt->user) != 0 ||
262             strcmp(service, authctxt->service) != 0) {
263                 packet_disconnect("Change of username or service not allowed: "
264                     "(%s,%s) -> (%s,%s)",
265                     authctxt->user, authctxt->service, user, service);
266         }
267         /* reset state */
268         auth2_challenge_stop(authctxt);
269
270 #ifdef GSSAPI
271         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
272         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
273 #endif
274
275         authctxt->postponed = 0;
276
277         /* try to authenticate user */
278         m = authmethod_lookup(method);
279         if (m != NULL && authctxt->failures < options.max_authtries) {
280                 debug2("input_userauth_request: try method %s", method);
281                 authenticated = m->userauth(authctxt);
282         }
283         userauth_finish(authctxt, authenticated, method);
284
285         xfree(service);
286         xfree(user);
287         xfree(method);
288 }
289
290 void
291 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
292 {
293         char *methods;
294
295         if (!authctxt->valid && authenticated)
296                 fatal("INTERNAL ERROR: authenticated invalid user %s",
297                     authctxt->user);
298
299         /* Special handling for root */
300         if (authenticated && authctxt->pw->pw_uid == 0 &&
301             !auth_root_allowed(method)) {
302                 authenticated = 0;
303 #ifdef SSH_AUDIT_EVENTS
304                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
305 #endif
306         }
307
308 #ifdef USE_PAM
309         if (options.use_pam && authenticated) {
310                 if (!PRIVSEP(do_pam_account())) {
311                         /* if PAM returned a message, send it to the user */
312                         if (buffer_len(&loginmsg) > 0) {
313                                 buffer_append(&loginmsg, "\0", 1);
314                                 userauth_send_banner(buffer_ptr(&loginmsg));
315                                 packet_write_wait();
316                         }
317                         fatal("Access denied for user %s by PAM account "
318                             "configuration", authctxt->user);
319                 }
320         }
321 #endif
322
323 #ifdef _UNICOS
324         if (authenticated && cray_access_denied(authctxt->user)) {
325                 authenticated = 0;
326                 fatal("Access denied for user %s.",authctxt->user);
327         }
328 #endif /* _UNICOS */
329
330         /* Log before sending the reply */
331         auth_log(authctxt, authenticated, method, " ssh2");
332
333         if (authctxt->postponed)
334                 return;
335
336         /* XXX todo: check if multiple auth methods are needed */
337         if (authenticated == 1) {
338                 /* turn off userauth */
339                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
340                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
341                 packet_send();
342                 packet_write_wait();
343                 /* now we can break out */
344                 authctxt->success = 1;
345         } else {
346
347                 /* Allow initial try of "none" auth without failure penalty */
348                 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
349                         authctxt->failures++;
350                 if (authctxt->failures >= options.max_authtries) {
351 #ifdef SSH_AUDIT_EVENTS
352                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
353 #endif
354                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
355                 }
356                 methods = authmethods_get();
357                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
358                 packet_put_cstring(methods);
359                 packet_put_char(0);     /* XXX partial success, unused */
360                 packet_send();
361                 packet_write_wait();
362                 xfree(methods);
363         }
364 }
365
366 static char *
367 authmethods_get(void)
368 {
369         Buffer b;
370         char *list;
371         int i;
372
373         buffer_init(&b);
374         for (i = 0; authmethods[i] != NULL; i++) {
375                 if (strcmp(authmethods[i]->name, "none") == 0)
376                         continue;
377                 if (authmethods[i]->enabled != NULL &&
378                     *(authmethods[i]->enabled) != 0) {
379                         if (buffer_len(&b) > 0)
380                                 buffer_append(&b, ",", 1);
381                         buffer_append(&b, authmethods[i]->name,
382                             strlen(authmethods[i]->name));
383                 }
384         }
385         buffer_append(&b, "\0", 1);
386         list = xstrdup(buffer_ptr(&b));
387         buffer_free(&b);
388         return list;
389 }
390
391 static Authmethod *
392 authmethod_lookup(const char *name)
393 {
394         int i;
395
396         if (name != NULL)
397                 for (i = 0; authmethods[i] != NULL; i++)
398                         if (authmethods[i]->enabled != NULL &&
399                             *(authmethods[i]->enabled) != 0 &&
400                             strcmp(name, authmethods[i]->name) == 0)
401                                 return authmethods[i];
402         debug2("Unrecognized authentication method name: %s",
403             name ? name : "NULL");
404         return NULL;
405 }
406
This page took 0.166402 seconds and 5 git commands to generate.