Merge branch 'vendor/OPENSSH'
[dragonfly.git] / crypto / openssh / auth2.c
1 /* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker 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 "atomicio.h"
39 #include "xmalloc.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
53 #ifdef GSSAPI
54 #include "ssh-gss.h"
55 #endif
56 #include "monitor_wrap.h"
57
58 /* import */
59 extern ServerOptions options;
60 extern u_char *session_id2;
61 extern u_int session_id2_len;
62 extern Buffer loginmsg;
63
64 /* methods */
65
66 extern Authmethod method_none;
67 extern Authmethod method_pubkey;
68 extern Authmethod method_passwd;
69 extern Authmethod method_kbdint;
70 extern Authmethod method_hostbased;
71 #ifdef GSSAPI
72 extern Authmethod method_gssapi;
73 #endif
74 #ifdef JPAKE
75 extern Authmethod method_jpake;
76 #endif
77
78 Authmethod *authmethods[] = {
79         &method_none,
80         &method_pubkey,
81 #ifdef GSSAPI
82         &method_gssapi,
83 #endif
84 #ifdef JPAKE
85         &method_jpake,
86 #endif
87         &method_passwd,
88         &method_kbdint,
89         &method_hostbased,
90         NULL
91 };
92
93 /* protocol */
94
95 static void input_service_request(int, u_int32_t, void *);
96 static void input_userauth_request(int, u_int32_t, void *);
97
98 /* helper */
99 static Authmethod *authmethod_lookup(const char *);
100 static char *authmethods_get(void);
101
102 char *
103 auth2_read_banner(void)
104 {
105         struct stat st;
106         char *banner = NULL;
107         size_t len, n;
108         int fd;
109
110         if ((fd = open(options.banner, O_RDONLY)) == -1)
111                 return (NULL);
112         if (fstat(fd, &st) == -1) {
113                 close(fd);
114                 return (NULL);
115         }
116         if (st.st_size > 1*1024*1024) {
117                 close(fd);
118                 return (NULL);
119         }
120
121         len = (size_t)st.st_size;               /* truncate */
122         banner = xmalloc(len + 1);
123         n = atomicio(read, fd, banner, len);
124         close(fd);
125
126         if (n != len) {
127                 xfree(banner);
128                 return (NULL);
129         }
130         banner[n] = '\0';
131
132         return (banner);
133 }
134
135 void
136 userauth_send_banner(const char *msg)
137 {
138         if (datafellows & SSH_BUG_BANNER)
139                 return;
140
141         packet_start(SSH2_MSG_USERAUTH_BANNER);
142         packet_put_cstring(msg);
143         packet_put_cstring("");         /* language, unused */
144         packet_send();
145         debug("%s: sent", __func__);
146 }
147
148 static void
149 userauth_banner(void)
150 {
151         char *banner = NULL;
152
153         if (options.banner == NULL ||
154             strcasecmp(options.banner, "none") == 0 ||
155             (datafellows & SSH_BUG_BANNER) != 0)
156                 return;
157
158         if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
159                 goto done;
160         userauth_send_banner(banner);
161
162 done:
163         if (banner)
164                 xfree(banner);
165 }
166
167 /*
168  * loop until authctxt->success == TRUE
169  */
170 void
171 do_authentication2(Authctxt *authctxt)
172 {
173         dispatch_init(&dispatch_protocol_error);
174         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
175         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
176 }
177
178 /*ARGSUSED*/
179 static void
180 input_service_request(int type, u_int32_t seq, void *ctxt)
181 {
182         Authctxt *authctxt = ctxt;
183         u_int len;
184         int acceptit = 0;
185         char *service = packet_get_string(&len);
186         packet_check_eom();
187
188         if (authctxt == NULL)
189                 fatal("input_service_request: no authctxt");
190
191         if (strcmp(service, "ssh-userauth") == 0) {
192                 if (!authctxt->success) {
193                         acceptit = 1;
194                         /* now we can handle user-auth requests */
195                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
196                 }
197         }
198         /* XXX all other service requests are denied */
199
200         if (acceptit) {
201                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
202                 packet_put_cstring(service);
203                 packet_send();
204                 packet_write_wait();
205         } else {
206                 debug("bad service request %s", service);
207                 packet_disconnect("bad service request %s", service);
208         }
209         xfree(service);
210 }
211
212 /*ARGSUSED*/
213 static void
214 input_userauth_request(int type, u_int32_t seq, void *ctxt)
215 {
216         Authctxt *authctxt = ctxt;
217         Authmethod *m = NULL;
218         char *user, *service, *method, *style = NULL;
219         int authenticated = 0;
220 #ifdef HAVE_LOGIN_CAP
221         login_cap_t *lc;
222         const char *from_host, *from_ip;
223
224         from_host = get_canonical_hostname(options.use_dns);
225         from_ip = get_remote_ipaddr();
226 #endif
227
228         if (authctxt == NULL)
229                 fatal("input_userauth_request: no authctxt");
230
231         user = packet_get_string(NULL);
232         service = packet_get_string(NULL);
233         method = packet_get_string(NULL);
234         debug("userauth-request for user %s service %s method %s", user, service, method);
235         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
236
237         if ((style = strchr(user, ':')) != NULL)
238                 *style++ = 0;
239
240         if (authctxt->attempt++ == 0) {
241                 /* setup auth context */
242                 authctxt->pw = PRIVSEP(getpwnamallow(user));
243                 authctxt->user = xstrdup(user);
244                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
245                         authctxt->valid = 1;
246                         debug2("input_userauth_request: setting up authctxt for %s", user);
247                 } else {
248                         logit("input_userauth_request: invalid user %s", user);
249                         authctxt->pw = fakepw();
250 #ifdef SSH_AUDIT_EVENTS
251                         PRIVSEP(audit_event(SSH_INVALID_USER));
252 #endif
253                 }
254 #ifdef USE_PAM
255                 if (options.use_pam)
256                         PRIVSEP(start_pam(authctxt));
257 #endif
258                 setproctitle("%s%s", authctxt->valid ? user : "unknown",
259                     use_privsep ? " [net]" : "");
260                 authctxt->service = xstrdup(service);
261                 authctxt->style = style ? xstrdup(style) : NULL;
262                 if (use_privsep)
263                         mm_inform_authserv(service, style);
264                 userauth_banner();
265         } else if (strcmp(user, authctxt->user) != 0 ||
266             strcmp(service, authctxt->service) != 0) {
267                 packet_disconnect("Change of username or service not allowed: "
268                     "(%s,%s) -> (%s,%s)",
269                     authctxt->user, authctxt->service, user, service);
270         }
271
272 #ifdef HAVE_LOGIN_CAP
273         if (authctxt->pw != NULL) {
274                 lc = login_getpwclass(authctxt->pw);
275                 if (lc == NULL)
276                         lc = login_getclassbyname(NULL, authctxt->pw);
277                 if (!auth_hostok(lc, from_host, from_ip)) {
278                         logit("Denied connection for %.200s from %.200s [%.200s].",
279                             authctxt->pw->pw_name, from_host, from_ip);
280                         packet_disconnect("Sorry, you are not allowed to connect.");
281                 }
282                 if (!auth_timeok(lc, time(NULL))) {
283                         logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
284                             authctxt->pw->pw_name, from_host);
285                         packet_disconnect("Logins not available right now.");
286                 }
287                 login_close(lc);
288                 lc = NULL;
289         }
290 #endif  /* HAVE_LOGIN_CAP */
291
292         /* reset state */
293         auth2_challenge_stop(authctxt);
294 #ifdef JPAKE
295         auth2_jpake_stop(authctxt);
296 #endif
297
298 #ifdef GSSAPI
299         /* XXX move to auth2_gssapi_stop() */
300         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
301         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
302 #endif
303
304         authctxt->postponed = 0;
305
306         /* try to authenticate user */
307         m = authmethod_lookup(method);
308         if (m != NULL && authctxt->failures < options.max_authtries) {
309                 debug2("input_userauth_request: try method %s", method);
310                 authenticated = m->userauth(authctxt);
311         }
312         userauth_finish(authctxt, authenticated, method);
313
314         xfree(service);
315         xfree(user);
316         xfree(method);
317 }
318
319 void
320 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
321 {
322         char *methods;
323
324         if (!authctxt->valid && authenticated)
325                 fatal("INTERNAL ERROR: authenticated invalid user %s",
326                     authctxt->user);
327
328         /* Special handling for root */
329         if (authenticated && authctxt->pw->pw_uid == 0 &&
330             !auth_root_allowed(method)) {
331                 authenticated = 0;
332 #ifdef SSH_AUDIT_EVENTS
333                 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
334 #endif
335         }
336
337 #ifdef USE_PAM
338         if (options.use_pam && authenticated) {
339                 if (!PRIVSEP(do_pam_account())) {
340                         /* if PAM returned a message, send it to the user */
341                         if (buffer_len(&loginmsg) > 0) {
342                                 buffer_append(&loginmsg, "\0", 1);
343                                 userauth_send_banner(buffer_ptr(&loginmsg));
344                                 packet_write_wait();
345                         }
346                         fatal("Access denied for user %s by PAM account "
347                             "configuration", authctxt->user);
348                 }
349         }
350 #endif
351
352 #ifdef _UNICOS
353         if (authenticated && cray_access_denied(authctxt->user)) {
354                 authenticated = 0;
355                 fatal("Access denied for user %s.",authctxt->user);
356         }
357 #endif /* _UNICOS */
358
359         /* Log before sending the reply */
360         auth_log(authctxt, authenticated, method, " ssh2");
361
362         if (authctxt->postponed)
363                 return;
364
365         /* XXX todo: check if multiple auth methods are needed */
366         if (authenticated == 1) {
367                 /* turn off userauth */
368                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
369                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
370                 packet_send();
371                 packet_write_wait();
372                 /* now we can break out */
373                 authctxt->success = 1;
374         } else {
375
376                 /* Allow initial try of "none" auth without failure penalty */
377                 if (authctxt->attempt > 1 || strcmp(method, "none") != 0)
378                         authctxt->failures++;
379                 if (authctxt->failures >= options.max_authtries) {
380 #ifdef SSH_AUDIT_EVENTS
381                         PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
382 #endif
383                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
384                 }
385                 methods = authmethods_get();
386                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
387                 packet_put_cstring(methods);
388                 packet_put_char(0);     /* XXX partial success, unused */
389                 packet_send();
390                 packet_write_wait();
391                 xfree(methods);
392         }
393 }
394
395 static char *
396 authmethods_get(void)
397 {
398         Buffer b;
399         char *list;
400         int i;
401
402         buffer_init(&b);
403         for (i = 0; authmethods[i] != NULL; i++) {
404                 if (strcmp(authmethods[i]->name, "none") == 0)
405                         continue;
406                 if (authmethods[i]->enabled != NULL &&
407                     *(authmethods[i]->enabled) != 0) {
408                         if (buffer_len(&b) > 0)
409                                 buffer_append(&b, ",", 1);
410                         buffer_append(&b, authmethods[i]->name,
411                             strlen(authmethods[i]->name));
412                 }
413         }
414         buffer_append(&b, "\0", 1);
415         list = xstrdup(buffer_ptr(&b));
416         buffer_free(&b);
417         return list;
418 }
419
420 static Authmethod *
421 authmethod_lookup(const char *name)
422 {
423         int i;
424
425         if (name != NULL)
426                 for (i = 0; authmethods[i] != NULL; i++)
427                         if (authmethods[i]->enabled != NULL &&
428                             *(authmethods[i]->enabled) != 0 &&
429                             strcmp(name, authmethods[i]->name) == 0)
430                                 return authmethods[i];
431         debug2("Unrecognized authentication method name: %s",
432             name ? name : "NULL");
433         return NULL;
434 }
435