]> andersk Git - openssh.git/blob - auth2.c
- (djm) Split out and improve OSF SIA auth code. Patch from Chris Adams
[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.40 2001/02/10 12:52:02 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                 setproctitle("%s", user);
193                 pw = getpwnam(user);
194                 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
195                         authctxt->pw = pwcopy(pw);
196                         authctxt->valid = 1;
197                         debug2("input_userauth_request: setting up authctxt for %s", user);
198 #ifdef USE_PAM
199                         start_pam(pw->pw_name);
200 #endif
201                 } else {
202                         log("input_userauth_request: illegal user %s", user);
203 #ifdef USE_PAM
204                         start_pam("NOUSER");
205 #endif
206                 }
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
222         /* try to authenticate user */
223         m = authmethod_lookup(method);
224         if (m != NULL) {
225                 debug2("input_userauth_request: try method %s", method);
226                 authenticated = m->userauth(authctxt);
227         }
228         if (!authctxt->valid && authenticated)
229                 fatal("INTERNAL ERROR: authenticated invalid user %s",
230                     authctxt->user);
231
232         /* Special handling for root */
233         if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
234                 authenticated = 0;
235
236 #ifdef USE_PAM
237         if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
238                 authenticated = 0;
239 #endif /* USE_PAM */
240
241         /* Log before sending the reply */
242         auth_log(authctxt, authenticated, method, " ssh2");
243
244         if (!authctxt->postponed)
245                 userauth_reply(authctxt, authenticated);
246
247         xfree(service);
248         xfree(user);
249         xfree(method);
250 }
251
252 void
253 userauth_banner(void)
254 {
255         struct stat st;
256         char *banner = NULL;
257         off_t len, n;
258         int fd;
259
260         if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
261                 return;
262         if ((fd = open(options.banner, O_RDONLY)) < 0) {
263                 error("userauth_banner: open %s failed: %s",
264                     options.banner, strerror(errno));
265                 return;
266         }
267         if (fstat(fd, &st) < 0)
268                 goto done;
269         len = st.st_size;
270         banner = xmalloc(len + 1);
271         if ((n = read(fd, banner, len)) < 0)
272                 goto done;
273         banner[n] = '\0';
274         packet_start(SSH2_MSG_USERAUTH_BANNER);
275         packet_put_cstring(banner);
276         packet_put_cstring("");         /* language, unused */
277         packet_send();
278         debug("userauth_banner: sent");
279 done:
280         if (banner)
281                 xfree(banner);
282         close(fd);
283         return;
284 }
285
286 void
287 userauth_reply(Authctxt *authctxt, int authenticated)
288 {
289         char *methods;
290
291         /* XXX todo: check if multiple auth methods are needed */
292         if (authenticated == 1) {
293 #ifdef WITH_AIXAUTHENTICATE
294                 /* We don't have a pty yet, so just label the line as "ssh" */
295                 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
296                     get_canonical_hostname(options.reverse_mapping_check),
297                     "ssh", &aixloginmsg) < 0)
298                         aixloginmsg = NULL;
299 #endif /* WITH_AIXAUTHENTICATE */
300                 /* turn off userauth */
301                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
302                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
303                 packet_send();
304                 packet_write_wait();
305                 /* now we can break out */
306                 authctxt->success = 1;
307         } else {
308                 if (authctxt->failures++ > AUTH_FAIL_MAX)
309                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
310                 methods = authmethods_get();
311                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
312                 packet_put_cstring(methods);
313                 packet_put_char(0);     /* XXX partial success, unused */
314                 packet_send();
315                 packet_write_wait();
316                 xfree(methods);
317         }
318 }
319
320 int
321 userauth_none(Authctxt *authctxt)
322 {
323         /* disable method "none", only allowed one time */
324         Authmethod *m = authmethod_lookup("none");
325         if (m != NULL)
326                 m->enabled = NULL;
327         packet_done();
328         userauth_banner();
329
330         if (authctxt->valid == 0)
331                 return(0);
332
333 #ifdef HAVE_CYGWIN
334         if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
335                 return(0);
336 #endif
337 #ifdef USE_PAM
338         return auth_pam_password(authctxt->pw, "");
339 #elif defined(HAVE_OSF_SIA)
340         return 0;
341 #else /* !HAVE_OSF_SIA && !USE_PAM */
342         return auth_password(authctxt->pw, "");
343 #endif /* USE_PAM */
344 }
345
346 int
347 userauth_passwd(Authctxt *authctxt)
348 {
349         char *password;
350         int authenticated = 0;
351         int change;
352         u_int len;
353         change = packet_get_char();
354         if (change)
355                 log("password change not supported");
356         password = packet_get_string(&len);
357         packet_done();
358         if (authctxt->valid &&
359 #ifdef HAVE_CYGWIN
360                 check_nt_auth(1, authctxt->pw->pw_uid) &&
361 #endif
362 #ifdef USE_PAM
363             auth_pam_password(authctxt->pw, password) == 1)
364 #elif defined(HAVE_OSF_SIA)
365             auth_sia_password(authctxt->user, password) == 1)
366 #else /* !USE_PAM && !HAVE_OSF_SIA */
367             auth_password(authctxt->pw, password) == 1)
368 #endif /* USE_PAM */
369                 authenticated = 1;
370         memset(password, 0, len);
371         xfree(password);
372         return authenticated;
373 }
374
375 int
376 userauth_kbdint(Authctxt *authctxt)
377 {
378         int authenticated = 0;
379         char *lang = NULL;
380         char *devs = NULL;
381
382         lang = packet_get_string(NULL);
383         devs = packet_get_string(NULL);
384         packet_done();
385
386         debug("keyboard-interactive language %s devs %s", lang, devs);
387
388         if (options.challenge_reponse_authentication)
389                 authenticated = auth2_challenge(authctxt, devs);
390
391 #ifdef USE_PAM
392         if (authenticated == 0)
393                 authenticated = auth2_pam(authctxt);
394 #endif
395         xfree(lang);
396         xfree(devs);
397 #ifdef HAVE_CYGWIN
398         if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
399                 return(0);
400 #endif
401         return authenticated;
402 }
403
404 int
405 userauth_pubkey(Authctxt *authctxt)
406 {
407         Buffer b;
408         Key *key;
409         char *pkalg, *pkblob, *sig;
410         u_int alen, blen, slen;
411         int have_sig, pktype;
412         int authenticated = 0;
413
414         if (!authctxt->valid) {
415                 debug2("userauth_pubkey: disabled because of invalid user");
416                 return 0;
417         }
418         have_sig = packet_get_char();
419         if (datafellows & SSH_BUG_PKAUTH) {
420                 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
421                 /* no explicit pkalg given */
422                 pkblob = packet_get_string(&blen);
423                 buffer_init(&b);
424                 buffer_append(&b, pkblob, blen);
425                 /* so we have to extract the pkalg from the pkblob */
426                 pkalg = buffer_get_string(&b, &alen);
427                 buffer_free(&b);
428         } else {
429                 pkalg = packet_get_string(&alen);
430                 pkblob = packet_get_string(&blen);
431         }
432         pktype = key_type_from_name(pkalg);
433         if (pktype == KEY_UNSPEC) {
434                 /* this is perfectly legal */
435                 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
436                 xfree(pkalg);
437                 xfree(pkblob);
438                 return 0;
439         }
440         key = key_from_blob(pkblob, blen);
441         if (key != NULL) {
442                 if (have_sig) {
443                         sig = packet_get_string(&slen);
444                         packet_done();
445                         buffer_init(&b);
446                         if (datafellows & SSH_OLD_SESSIONID) {
447                                 buffer_append(&b, session_id2, session_id2_len);
448                         } else {
449                                 buffer_put_string(&b, session_id2, session_id2_len);
450                         }
451                         /* reconstruct packet */
452                         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
453                         buffer_put_cstring(&b, authctxt->user);
454                         buffer_put_cstring(&b,
455                             datafellows & SSH_BUG_PKSERVICE ?
456                             "ssh-userauth" :
457                             authctxt->service);
458                         if (datafellows & SSH_BUG_PKAUTH) {
459                                 buffer_put_char(&b, have_sig);
460                         } else {
461                                 buffer_put_cstring(&b, "publickey");
462                                 buffer_put_char(&b, have_sig);
463                                 buffer_put_cstring(&b, key_ssh_name(key));
464                         }
465                         buffer_put_string(&b, pkblob, blen);
466 #ifdef DEBUG_PK
467                         buffer_dump(&b);
468 #endif
469                         /* test for correct signature */
470                         if (user_key_allowed(authctxt->pw, key) &&
471                             key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
472                                 authenticated = 1;
473                         buffer_clear(&b);
474                         xfree(sig);
475                 } else {
476                         debug("test whether pkalg/pkblob are acceptable");
477                         packet_done();
478
479                         /* XXX fake reply and always send PK_OK ? */
480                         /*
481                          * XXX this allows testing whether a user is allowed
482                          * to login: if you happen to have a valid pubkey this
483                          * message is sent. the message is NEVER sent at all
484                          * if a user is not allowed to login. is this an
485                          * issue? -markus
486                          */
487                         if (user_key_allowed(authctxt->pw, key)) {
488                                 packet_start(SSH2_MSG_USERAUTH_PK_OK);
489                                 packet_put_string(pkalg, alen);
490                                 packet_put_string(pkblob, blen);
491                                 packet_send();
492                                 packet_write_wait();
493                                 authctxt->postponed = 1;
494                         }
495                 }
496                 if (authenticated != 1)
497                         auth_clear_options();
498                 key_free(key);
499         }
500         debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
501         xfree(pkalg);
502         xfree(pkblob);
503 #ifdef HAVE_CYGWIN
504         if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
505                 return(0);
506 #endif
507         return authenticated;
508 }
509
510 /* get current user */
511
512 struct passwd*
513 auth_get_user(void)
514 {
515         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
516 }
517
518 #define DELIM   ","
519
520 char *
521 authmethods_get(void)
522 {
523         Authmethod *method = NULL;
524         u_int size = 0;
525         char *list;
526
527         for (method = authmethods; method->name != NULL; method++) {
528                 if (strcmp(method->name, "none") == 0)
529                         continue;
530                 if (method->enabled != NULL && *(method->enabled) != 0) {
531                         if (size != 0)
532                                 size += strlen(DELIM);
533                         size += strlen(method->name);
534                 }
535         }
536         size++;                 /* trailing '\0' */
537         list = xmalloc(size);
538         list[0] = '\0';
539
540         for (method = authmethods; method->name != NULL; method++) {
541                 if (strcmp(method->name, "none") == 0)
542                         continue;
543                 if (method->enabled != NULL && *(method->enabled) != 0) {
544                         if (list[0] != '\0')
545                                 strlcat(list, DELIM, size);
546                         strlcat(list, method->name, size);
547                 }
548         }
549         return list;
550 }
551
552 Authmethod *
553 authmethod_lookup(const char *name)
554 {
555         Authmethod *method = NULL;
556         if (name != NULL)
557                 for (method = authmethods; method->name != NULL; method++)
558                         if (method->enabled != NULL &&
559                             *(method->enabled) != 0 &&
560                             strcmp(name, method->name) == 0)
561                                 return method;
562         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
563         return NULL;
564 }
565
566 /* return 1 if user allows given key */
567 int
568 user_key_allowed(struct passwd *pw, Key *key)
569 {
570         char line[8192], file[MAXPATHLEN];
571         int found_key = 0;
572         FILE *f;
573         u_long linenum = 0;
574         struct stat st;
575         Key *found;
576
577         if (pw == NULL)
578                 return 0;
579
580         /* Temporarily use the user's uid. */
581         temporarily_use_uid(pw->pw_uid);
582
583         /* The authorized keys. */
584         snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
585             _PATH_SSH_USER_PERMITTED_KEYS2);
586
587         /* Fail quietly if file does not exist */
588         if (stat(file, &st) < 0) {
589                 /* Restore the privileged uid. */
590                 restore_uid();
591                 return 0;
592         }
593         /* Open the file containing the authorized keys. */
594         f = fopen(file, "r");
595         if (!f) {
596                 /* Restore the privileged uid. */
597                 restore_uid();
598                 return 0;
599         }
600         if (options.strict_modes) {
601                 int fail = 0;
602                 char buf[1024];
603                 /* Check open file in order to avoid open/stat races */
604                 if (fstat(fileno(f), &st) < 0 ||
605                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
606                     (st.st_mode & 022) != 0) {
607                         snprintf(buf, sizeof buf,
608                             "%s authentication refused for %.100s: "
609                             "bad ownership or modes for '%s'.",
610                             key_type(key), pw->pw_name, file);
611                         fail = 1;
612                 } else {
613                         /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
614                         int i;
615                         static const char *check[] = {
616                                 "", _PATH_SSH_USER_DIR, NULL
617                         };
618                         for (i = 0; check[i]; i++) {
619                                 snprintf(line, sizeof line, "%.500s/%.100s",
620                                     pw->pw_dir, check[i]);
621                                 if (stat(line, &st) < 0 ||
622                                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
623                                     (st.st_mode & 022) != 0) {
624                                         snprintf(buf, sizeof buf,
625                                             "%s authentication refused for %.100s: "
626                                             "bad ownership or modes for '%s'.",
627                                             key_type(key), pw->pw_name, line);
628                                         fail = 1;
629                                         break;
630                                 }
631                         }
632                 }
633                 if (fail) {
634                         fclose(f);
635                         log("%s",buf);
636                         restore_uid();
637                         return 0;
638                 }
639         }
640         found_key = 0;
641         found = key_new(key->type);
642
643         while (fgets(line, sizeof(line), f)) {
644                 char *cp, *options = NULL;
645                 linenum++;
646                 /* Skip leading whitespace, empty and comment lines. */
647                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
648                         ;
649                 if (!*cp || *cp == '\n' || *cp == '#')
650                         continue;
651
652                 if (key_read(found, &cp) == -1) {
653                         /* no key?  check if there are options for this key */
654                         int quoted = 0;
655                         debug2("user_key_allowed: check options: '%s'", cp);
656                         options = cp;
657                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
658                                 if (*cp == '\\' && cp[1] == '"')
659                                         cp++;   /* Skip both */
660                                 else if (*cp == '"')
661                                         quoted = !quoted;
662                         }
663                         /* Skip remaining whitespace. */
664                         for (; *cp == ' ' || *cp == '\t'; cp++)
665                                 ;
666                         if (key_read(found, &cp) == -1) {
667                                 debug2("user_key_allowed: advance: '%s'", cp);
668                                 /* still no key?  advance to next line*/
669                                 continue;
670                         }
671                 }
672                 if (key_equal(found, key) &&
673                     auth_parse_options(pw, options, file, linenum) == 1) {
674                         found_key = 1;
675                         debug("matching key found: file %s, line %ld",
676                             file, linenum);
677                         break;
678                 }
679         }
680         restore_uid();
681         fclose(f);
682         key_free(found);
683         return found_key;
684 }
This page took 0.12971 seconds and 5 git commands to generate.