]> andersk Git - openssh.git/blob - auth2.c
cd3886dcc16dfb223b1057efc16925474699d0de
[openssh.git] / auth2.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2.c,v 1.42 2001/02/13 22:49:40 markus Exp $");
27
28 #include <openssl/evp.h>
29
30 #include "ssh2.h"
31 #include "xmalloc.h"
32 #include "rsa.h"
33 #include "pty.h"
34 #include "packet.h"
35 #include "buffer.h"
36 #include "log.h"
37 #include "servconf.h"
38 #include "compat.h"
39 #include "channels.h"
40 #include "bufaux.h"
41 #include "auth.h"
42 #include "session.h"
43 #include "dispatch.h"
44 #include "key.h"
45 #include "cipher.h"
46 #include "kex.h"
47 #include "pathnames.h"
48 #include "uidswap.h"
49 #include "auth-options.h"
50
51 /* import */
52 extern ServerOptions options;
53 extern u_char *session_id2;
54 extern int session_id2_len;
55
56 #ifdef WITH_AIXAUTHENTICATE
57 extern char *aixloginmsg;
58 #endif
59
60 static Authctxt *x_authctxt = NULL;
61 static int one = 1;
62
63 typedef struct Authmethod Authmethod;
64 struct Authmethod {
65         char    *name;
66         int     (*userauth)(Authctxt *authctxt);
67         int     *enabled;
68 };
69
70 /* protocol */
71
72 void    input_service_request(int type, int plen, void *ctxt);
73 void    input_userauth_request(int type, int plen, void *ctxt);
74 void    protocol_error(int type, int plen, void *ctxt);
75
76 /* helper */
77 Authmethod      *authmethod_lookup(const char *name);
78 struct passwd   *pwcopy(struct passwd *pw);
79 int     user_key_allowed(struct passwd *pw, Key *key);
80 char    *authmethods_get(void);
81
82 /* auth */
83 void    userauth_banner(void);
84 int     userauth_none(Authctxt *authctxt);
85 int     userauth_passwd(Authctxt *authctxt);
86 int     userauth_pubkey(Authctxt *authctxt);
87 int     userauth_kbdint(Authctxt *authctxt);
88
89 Authmethod authmethods[] = {
90         {"none",
91                 userauth_none,
92                 &one},
93         {"publickey",
94                 userauth_pubkey,
95                 &options.pubkey_authentication},
96         {"password",
97                 userauth_passwd,
98                 &options.password_authentication},
99         {"keyboard-interactive",
100                 userauth_kbdint,
101                 &options.kbd_interactive_authentication},
102         {NULL, NULL, NULL}
103 };
104
105 /*
106  * loop until authctxt->success == TRUE
107  */
108
109 void
110 do_authentication2()
111 {
112         Authctxt *authctxt = authctxt_new();
113
114         x_authctxt = authctxt;          /*XXX*/
115
116         /* challenge-reponse is implemented via keyboard interactive */
117         if (options.challenge_reponse_authentication)
118                 options.kbd_interactive_authentication = 1;
119
120         dispatch_init(&protocol_error);
121         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
122         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
123         do_authenticated2(authctxt);
124 }
125
126 void
127 protocol_error(int type, int plen, void *ctxt)
128 {
129         log("auth: protocol error: type %d plen %d", type, plen);
130         packet_start(SSH2_MSG_UNIMPLEMENTED);
131         packet_put_int(0);
132         packet_send();
133         packet_write_wait();
134 }
135
136 void
137 input_service_request(int type, int plen, void *ctxt)
138 {
139         Authctxt *authctxt = ctxt;
140         u_int len;
141         int accept = 0;
142         char *service = packet_get_string(&len);
143         packet_done();
144
145         if (authctxt == NULL)
146                 fatal("input_service_request: no authctxt");
147
148         if (strcmp(service, "ssh-userauth") == 0) {
149                 if (!authctxt->success) {
150                         accept = 1;
151                         /* now we can handle user-auth requests */
152                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
153                 }
154         }
155         /* XXX all other service requests are denied */
156
157         if (accept) {
158                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
159                 packet_put_cstring(service);
160                 packet_send();
161                 packet_write_wait();
162         } else {
163                 debug("bad service request %s", service);
164                 packet_disconnect("bad service request %s", service);
165         }
166         xfree(service);
167 }
168
169 void
170 input_userauth_request(int type, int plen, void *ctxt)
171 {
172         Authctxt *authctxt = ctxt;
173         Authmethod *m = NULL;
174         char *user, *service, *method, *style = NULL;
175         int authenticated = 0;
176
177         if (authctxt == NULL)
178                 fatal("input_userauth_request: no authctxt");
179
180         user = packet_get_string(NULL);
181         service = packet_get_string(NULL);
182         method = packet_get_string(NULL);
183         debug("userauth-request for user %s service %s method %s", user, service, method);
184         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
185
186         if ((style = strchr(user, ':')) != NULL)
187                 *style++ = 0;
188
189         if (authctxt->attempt++ == 0) {
190                 /* setup auth context */
191                 struct passwd *pw = NULL;
192                 pw = getpwnam(user);
193                 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
194                         authctxt->pw = pwcopy(pw);
195                         authctxt->valid = 1;
196                         debug2("input_userauth_request: setting up authctxt for %s", user);
197 #ifdef USE_PAM
198                         start_pam(pw->pw_name);
199 #endif
200                 } else {
201                         log("input_userauth_request: illegal user %s", user);
202 #ifdef USE_PAM
203                         start_pam("NOUSER");
204 #endif
205                 }
206                 setproctitle("%s", pw ? user : "unknown");
207                 authctxt->user = xstrdup(user);
208                 authctxt->service = xstrdup(service);
209                 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
210         } else if (authctxt->valid) {
211                 if (strcmp(user, authctxt->user) != 0 ||
212                     strcmp(service, authctxt->service) != 0) {
213                         log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
214                             user, service, authctxt->user, authctxt->service);
215                         authctxt->valid = 0;
216                 }
217         }
218         /* reset state */
219         dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
220         authctxt->postponed = 0;
221 #ifdef BSD_AUTH
222         if (authctxt->as) {
223                 auth_close(authctxt->as);
224                 authctxt->as = NULL;
225         }
226 #endif
227
228         /* try to authenticate user */
229         m = authmethod_lookup(method);
230         if (m != NULL) {
231                 debug2("input_userauth_request: try method %s", method);
232                 authenticated = m->userauth(authctxt);
233         }
234         if (!authctxt->valid && authenticated)
235                 fatal("INTERNAL ERROR: authenticated invalid user %s",
236                     authctxt->user);
237
238         /* Special handling for root */
239         if (authenticated && authctxt->pw->pw_uid == 0 &&
240             !auth_root_allowed(method))
241                 authenticated = 0;
242
243 #ifdef USE_PAM
244         if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
245             NULL))
246                 authenticated = 0;
247 #endif /* USE_PAM */
248
249         /* Log before sending the reply */
250         auth_log(authctxt, authenticated, method, " ssh2");
251
252         if (!authctxt->postponed)
253                 userauth_reply(authctxt, authenticated);
254
255         xfree(service);
256         xfree(user);
257         xfree(method);
258 }
259
260 void
261 userauth_banner(void)
262 {
263         struct stat st;
264         char *banner = NULL;
265         off_t len, n;
266         int fd;
267
268         if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
269                 return;
270         if ((fd = open(options.banner, O_RDONLY)) < 0) {
271                 error("userauth_banner: open %s failed: %s",
272                     options.banner, strerror(errno));
273                 return;
274         }
275         if (fstat(fd, &st) < 0)
276                 goto done;
277         len = st.st_size;
278         banner = xmalloc(len + 1);
279         if ((n = read(fd, banner, len)) < 0)
280                 goto done;
281         banner[n] = '\0';
282         packet_start(SSH2_MSG_USERAUTH_BANNER);
283         packet_put_cstring(banner);
284         packet_put_cstring("");         /* language, unused */
285         packet_send();
286         debug("userauth_banner: sent");
287 done:
288         if (banner)
289                 xfree(banner);
290         close(fd);
291         return;
292 }
293
294 void
295 userauth_reply(Authctxt *authctxt, int authenticated)
296 {
297         char *methods;
298
299         /* XXX todo: check if multiple auth methods are needed */
300         if (authenticated == 1) {
301 #ifdef WITH_AIXAUTHENTICATE
302                 /* We don't have a pty yet, so just label the line as "ssh" */
303                 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
304                     get_canonical_hostname(options.reverse_mapping_check),
305                     "ssh", &aixloginmsg) < 0)
306                         aixloginmsg = NULL;
307 #endif /* WITH_AIXAUTHENTICATE */
308                 /* turn off userauth */
309                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
310                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
311                 packet_send();
312                 packet_write_wait();
313                 /* now we can break out */
314                 authctxt->success = 1;
315         } else {
316                 if (authctxt->failures++ > AUTH_FAIL_MAX)
317                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
318                 methods = authmethods_get();
319                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
320                 packet_put_cstring(methods);
321                 packet_put_char(0);     /* XXX partial success, unused */
322                 packet_send();
323                 packet_write_wait();
324                 xfree(methods);
325         }
326 }
327
328 int
329 userauth_none(Authctxt *authctxt)
330 {
331         /* disable method "none", only allowed one time */
332         Authmethod *m = authmethod_lookup("none");
333         if (m != NULL)
334                 m->enabled = NULL;
335         packet_done();
336         userauth_banner();
337
338         if (authctxt->valid == 0)
339                 return(0);
340
341 #ifdef HAVE_CYGWIN
342         if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
343                 return(0);
344 #endif
345 #ifdef USE_PAM
346         return auth_pam_password(authctxt->pw, "");
347 #elif defined(HAVE_OSF_SIA)
348         return 0;
349 #else /* !HAVE_OSF_SIA && !USE_PAM */
350         return auth_password(authctxt, "");
351 #endif /* USE_PAM */
352 }
353
354 int
355 userauth_passwd(Authctxt *authctxt)
356 {
357         char *password;
358         int authenticated = 0;
359         int change;
360         u_int len;
361         change = packet_get_char();
362         if (change)
363                 log("password change not supported");
364         password = packet_get_string(&len);
365         packet_done();
366         if (authctxt->valid &&
367 #ifdef HAVE_CYGWIN
368                 check_nt_auth(1, authctxt->pw->pw_uid) &&
369 #endif
370 #ifdef USE_PAM
371             auth_pam_password(authctxt->pw, password) == 1)
372 #elif defined(HAVE_OSF_SIA)
373             auth_sia_password(authctxt->user, password) == 1)
374 #else /* !USE_PAM && !HAVE_OSF_SIA */
375             auth_password(authctxt, password) == 1)
376 #endif /* USE_PAM */
377                 authenticated = 1;
378         memset(password, 0, len);
379         xfree(password);
380         return authenticated;
381 }
382
383 int
384 userauth_kbdint(Authctxt *authctxt)
385 {
386         int authenticated = 0;
387         char *lang = NULL;
388         char *devs = NULL;
389
390         lang = packet_get_string(NULL);
391         devs = packet_get_string(NULL);
392         packet_done();
393
394         debug("keyboard-interactive language %s devs %s", lang, devs);
395
396         if (options.challenge_reponse_authentication)
397                 authenticated = auth2_challenge(authctxt, devs);
398
399 #ifdef USE_PAM
400         if (authenticated == 0)
401                 authenticated = auth2_pam(authctxt);
402 #endif
403         xfree(lang);
404         xfree(devs);
405 #ifdef HAVE_CYGWIN
406         if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
407                 return(0);
408 #endif
409         return authenticated;
410 }
411
412 int
413 userauth_pubkey(Authctxt *authctxt)
414 {
415         Buffer b;
416         Key *key;
417         char *pkalg, *pkblob, *sig;
418         u_int alen, blen, slen;
419         int have_sig, pktype;
420         int authenticated = 0;
421
422         if (!authctxt->valid) {
423                 debug2("userauth_pubkey: disabled because of invalid user");
424                 return 0;
425         }
426         have_sig = packet_get_char();
427         if (datafellows & SSH_BUG_PKAUTH) {
428                 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
429                 /* no explicit pkalg given */
430                 pkblob = packet_get_string(&blen);
431                 buffer_init(&b);
432                 buffer_append(&b, pkblob, blen);
433                 /* so we have to extract the pkalg from the pkblob */
434                 pkalg = buffer_get_string(&b, &alen);
435                 buffer_free(&b);
436         } else {
437                 pkalg = packet_get_string(&alen);
438                 pkblob = packet_get_string(&blen);
439         }
440         pktype = key_type_from_name(pkalg);
441         if (pktype == KEY_UNSPEC) {
442                 /* this is perfectly legal */
443                 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
444                 xfree(pkalg);
445                 xfree(pkblob);
446                 return 0;
447         }
448         key = key_from_blob(pkblob, blen);
449         if (key != NULL) {
450                 if (have_sig) {
451                         sig = packet_get_string(&slen);
452                         packet_done();
453                         buffer_init(&b);
454                         if (datafellows & SSH_OLD_SESSIONID) {
455                                 buffer_append(&b, session_id2, session_id2_len);
456                         } else {
457                                 buffer_put_string(&b, session_id2, session_id2_len);
458                         }
459                         /* reconstruct packet */
460                         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
461                         buffer_put_cstring(&b, authctxt->user);
462                         buffer_put_cstring(&b,
463                             datafellows & SSH_BUG_PKSERVICE ?
464                             "ssh-userauth" :
465                             authctxt->service);
466                         if (datafellows & SSH_BUG_PKAUTH) {
467                                 buffer_put_char(&b, have_sig);
468                         } else {
469                                 buffer_put_cstring(&b, "publickey");
470                                 buffer_put_char(&b, have_sig);
471                                 buffer_put_cstring(&b, key_ssh_name(key));
472                         }
473                         buffer_put_string(&b, pkblob, blen);
474 #ifdef DEBUG_PK
475                         buffer_dump(&b);
476 #endif
477                         /* test for correct signature */
478                         if (user_key_allowed(authctxt->pw, key) &&
479                             key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
480                                 authenticated = 1;
481                         buffer_clear(&b);
482                         xfree(sig);
483                 } else {
484                         debug("test whether pkalg/pkblob are acceptable");
485                         packet_done();
486
487                         /* XXX fake reply and always send PK_OK ? */
488                         /*
489                          * XXX this allows testing whether a user is allowed
490                          * to login: if you happen to have a valid pubkey this
491                          * message is sent. the message is NEVER sent at all
492                          * if a user is not allowed to login. is this an
493                          * issue? -markus
494                          */
495                         if (user_key_allowed(authctxt->pw, key)) {
496                                 packet_start(SSH2_MSG_USERAUTH_PK_OK);
497                                 packet_put_string(pkalg, alen);
498                                 packet_put_string(pkblob, blen);
499                                 packet_send();
500                                 packet_write_wait();
501                                 authctxt->postponed = 1;
502                         }
503                 }
504                 if (authenticated != 1)
505                         auth_clear_options();
506                 key_free(key);
507         }
508         debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
509         xfree(pkalg);
510         xfree(pkblob);
511 #ifdef HAVE_CYGWIN
512         if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
513                 return(0);
514 #endif
515         return authenticated;
516 }
517
518 /* get current user */
519
520 struct passwd*
521 auth_get_user(void)
522 {
523         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
524 }
525
526 #define DELIM   ","
527
528 char *
529 authmethods_get(void)
530 {
531         Authmethod *method = NULL;
532         u_int size = 0;
533         char *list;
534
535         for (method = authmethods; method->name != NULL; method++) {
536                 if (strcmp(method->name, "none") == 0)
537                         continue;
538                 if (method->enabled != NULL && *(method->enabled) != 0) {
539                         if (size != 0)
540                                 size += strlen(DELIM);
541                         size += strlen(method->name);
542                 }
543         }
544         size++;                 /* trailing '\0' */
545         list = xmalloc(size);
546         list[0] = '\0';
547
548         for (method = authmethods; method->name != NULL; method++) {
549                 if (strcmp(method->name, "none") == 0)
550                         continue;
551                 if (method->enabled != NULL && *(method->enabled) != 0) {
552                         if (list[0] != '\0')
553                                 strlcat(list, DELIM, size);
554                         strlcat(list, method->name, size);
555                 }
556         }
557         return list;
558 }
559
560 Authmethod *
561 authmethod_lookup(const char *name)
562 {
563         Authmethod *method = NULL;
564         if (name != NULL)
565                 for (method = authmethods; method->name != NULL; method++)
566                         if (method->enabled != NULL &&
567                             *(method->enabled) != 0 &&
568                             strcmp(name, method->name) == 0)
569                                 return method;
570         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
571         return NULL;
572 }
573
574 /* return 1 if user allows given key */
575 int
576 user_key_allowed(struct passwd *pw, Key *key)
577 {
578         char line[8192], file[MAXPATHLEN];
579         int found_key = 0;
580         FILE *f;
581         u_long linenum = 0;
582         struct stat st;
583         Key *found;
584
585         if (pw == NULL)
586                 return 0;
587
588         /* Temporarily use the user's uid. */
589         temporarily_use_uid(pw->pw_uid);
590
591         /* The authorized keys. */
592         snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
593             _PATH_SSH_USER_PERMITTED_KEYS2);
594
595         /* Fail quietly if file does not exist */
596         if (stat(file, &st) < 0) {
597                 /* Restore the privileged uid. */
598                 restore_uid();
599                 return 0;
600         }
601         /* Open the file containing the authorized keys. */
602         f = fopen(file, "r");
603         if (!f) {
604                 /* Restore the privileged uid. */
605                 restore_uid();
606                 return 0;
607         }
608         if (options.strict_modes) {
609                 int fail = 0;
610                 char buf[1024];
611                 /* Check open file in order to avoid open/stat races */
612                 if (fstat(fileno(f), &st) < 0 ||
613                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
614                     (st.st_mode & 022) != 0) {
615                         snprintf(buf, sizeof buf,
616                             "%s authentication refused for %.100s: "
617                             "bad ownership or modes for '%s'.",
618                             key_type(key), pw->pw_name, file);
619                         fail = 1;
620                 } else {
621                         /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
622                         int i;
623                         static const char *check[] = {
624                                 "", _PATH_SSH_USER_DIR, NULL
625                         };
626                         for (i = 0; check[i]; i++) {
627                                 snprintf(line, sizeof line, "%.500s/%.100s",
628                                     pw->pw_dir, check[i]);
629                                 if (stat(line, &st) < 0 ||
630                                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
631                                     (st.st_mode & 022) != 0) {
632                                         snprintf(buf, sizeof buf,
633                                             "%s authentication refused for %.100s: "
634                                             "bad ownership or modes for '%s'.",
635                                             key_type(key), pw->pw_name, line);
636                                         fail = 1;
637                                         break;
638                                 }
639                         }
640                 }
641                 if (fail) {
642                         fclose(f);
643                         log("%s",buf);
644                         restore_uid();
645                         return 0;
646                 }
647         }
648         found_key = 0;
649         found = key_new(key->type);
650
651         while (fgets(line, sizeof(line), f)) {
652                 char *cp, *options = NULL;
653                 linenum++;
654                 /* Skip leading whitespace, empty and comment lines. */
655                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
656                         ;
657                 if (!*cp || *cp == '\n' || *cp == '#')
658                         continue;
659
660                 if (key_read(found, &cp) == -1) {
661                         /* no key?  check if there are options for this key */
662                         int quoted = 0;
663                         debug2("user_key_allowed: check options: '%s'", cp);
664                         options = cp;
665                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
666                                 if (*cp == '\\' && cp[1] == '"')
667                                         cp++;   /* Skip both */
668                                 else if (*cp == '"')
669                                         quoted = !quoted;
670                         }
671                         /* Skip remaining whitespace. */
672                         for (; *cp == ' ' || *cp == '\t'; cp++)
673                                 ;
674                         if (key_read(found, &cp) == -1) {
675                                 debug2("user_key_allowed: advance: '%s'", cp);
676                                 /* still no key?  advance to next line*/
677                                 continue;
678                         }
679                 }
680                 if (key_equal(found, key) &&
681                     auth_parse_options(pw, options, file, linenum) == 1) {
682                         found_key = 1;
683                         debug("matching key found: file %s, line %ld",
684                             file, linenum);
685                         break;
686                 }
687         }
688         restore_uid();
689         fclose(f);
690         key_free(found);
691         return found_key;
692 }
This page took 0.703817 seconds and 3 git commands to generate.