1 /* $OpenBSD: auth.c,v 1.161 2024/05/17 00:30:23 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/socket.h>
31 #include <sys/wait.h>
32
33 #include <netinet/in.h>
34
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #ifdef HAVE_PATHS_H
39 # include <paths.h>
40 #endif
41 #include <pwd.h>
42 #ifdef HAVE_LOGIN_H
43 #include <login.h>
44 #endif
45 #ifdef USE_SHADOW
46 #include <shadow.h>
47 #endif
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <limits.h>
53 #include <netdb.h>
54 #include <time.h>
55
56 #include "xmalloc.h"
57 #include "match.h"
58 #include "groupaccess.h"
59 #include "log.h"
60 #include "sshbuf.h"
61 #include "misc.h"
62 #include "servconf.h"
63 #include "sshkey.h"
64 #include "hostfile.h"
65 #include "auth.h"
66 #include "auth-options.h"
67 #include "canohost.h"
68 #include "uidswap.h"
69 #include "packet.h"
70 #include "loginrec.h"
71 #ifdef GSSAPI
72 #include "ssh-gss.h"
73 #endif
74 #include "authfile.h"
75 #include "monitor_wrap.h"
76 #include "ssherr.h"
77 #include "channels.h"
78
79 /* import */
80 extern ServerOptions options;
81 extern struct include_list includes;
82 extern struct sshbuf *loginmsg;
83 extern struct passwd *privsep_pw;
84 extern struct sshauthopt *auth_opts;
85
86 /* Debugging messages */
87 static struct sshbuf *auth_debug;
88
89 /*
90 * Check if the user is allowed to log in via ssh. If user is listed
91 * in DenyUsers or one of user's groups is listed in DenyGroups, false
92 * will be returned. If AllowUsers isn't empty and user isn't listed
93 * there, or if AllowGroups isn't empty and one of user's groups isn't
94 * listed there, false will be returned.
95 * If the user's shell is not executable, false will be returned.
96 * Otherwise true is returned.
97 */
98 int
allowed_user(struct ssh * ssh,struct passwd * pw)99 allowed_user(struct ssh *ssh, struct passwd * pw)
100 {
101 struct stat st;
102 const char *hostname = NULL, *ipaddr = NULL;
103 u_int i;
104 int r;
105
106 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
107 if (!pw || !pw->pw_name)
108 return 0;
109
110 if (!options.use_pam && platform_locked_account(pw)) {
111 logit("User %.100s not allowed because account is locked",
112 pw->pw_name);
113 return 0;
114 }
115
116 /*
117 * Deny if shell does not exist or is not executable unless we
118 * are chrooting.
119 */
120 if (options.chroot_directory == NULL ||
121 strcasecmp(options.chroot_directory, "none") == 0) {
122 char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
123 _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
124
125 if (stat(shell, &st) == -1) {
126 logit("User %.100s not allowed because shell %.100s "
127 "does not exist", pw->pw_name, shell);
128 free(shell);
129 return 0;
130 }
131 if (S_ISREG(st.st_mode) == 0 ||
132 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
133 logit("User %.100s not allowed because shell %.100s "
134 "is not executable", pw->pw_name, shell);
135 free(shell);
136 return 0;
137 }
138 free(shell);
139 }
140
141 if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
142 options.num_deny_groups > 0 || options.num_allow_groups > 0) {
143 hostname = auth_get_canonical_hostname(ssh, options.use_dns);
144 ipaddr = ssh_remote_ipaddr(ssh);
145 }
146
147 /* Return false if user is listed in DenyUsers */
148 if (options.num_deny_users > 0) {
149 for (i = 0; i < options.num_deny_users; i++) {
150 r = match_user(pw->pw_name, hostname, ipaddr,
151 options.deny_users[i]);
152 if (r < 0) {
153 fatal("Invalid DenyUsers pattern \"%.100s\"",
154 options.deny_users[i]);
155 } else if (r != 0) {
156 logit("User %.100s from %.100s not allowed "
157 "because listed in DenyUsers",
158 pw->pw_name, hostname);
159 return 0;
160 }
161 }
162 }
163 /* Return false if AllowUsers isn't empty and user isn't listed there */
164 if (options.num_allow_users > 0) {
165 for (i = 0; i < options.num_allow_users; i++) {
166 r = match_user(pw->pw_name, hostname, ipaddr,
167 options.allow_users[i]);
168 if (r < 0) {
169 fatal("Invalid AllowUsers pattern \"%.100s\"",
170 options.allow_users[i]);
171 } else if (r == 1)
172 break;
173 }
174 /* i < options.num_allow_users iff we break for loop */
175 if (i >= options.num_allow_users) {
176 logit("User %.100s from %.100s not allowed because "
177 "not listed in AllowUsers", pw->pw_name, hostname);
178 return 0;
179 }
180 }
181 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
182 /* Get the user's group access list (primary and supplementary) */
183 if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
184 logit("User %.100s from %.100s not allowed because "
185 "not in any group", pw->pw_name, hostname);
186 return 0;
187 }
188
189 /* Return false if one of user's groups is listed in DenyGroups */
190 if (options.num_deny_groups > 0)
191 if (ga_match(options.deny_groups,
192 options.num_deny_groups)) {
193 ga_free();
194 logit("User %.100s from %.100s not allowed "
195 "because a group is listed in DenyGroups",
196 pw->pw_name, hostname);
197 return 0;
198 }
199 /*
200 * Return false if AllowGroups isn't empty and one of user's groups
201 * isn't listed there
202 */
203 if (options.num_allow_groups > 0)
204 if (!ga_match(options.allow_groups,
205 options.num_allow_groups)) {
206 ga_free();
207 logit("User %.100s from %.100s not allowed "
208 "because none of user's groups are listed "
209 "in AllowGroups", pw->pw_name, hostname);
210 return 0;
211 }
212 ga_free();
213 }
214
215 #ifdef CUSTOM_SYS_AUTH_ALLOWED_USER
216 if (!sys_auth_allowed_user(pw, loginmsg))
217 return 0;
218 #endif
219
220 /* We found no reason not to let this user try to log on... */
221 return 1;
222 }
223
224 /*
225 * Formats any key left in authctxt->auth_method_key for inclusion in
226 * auth_log()'s message. Also includes authxtct->auth_method_info if present.
227 */
228 static char *
format_method_key(Authctxt * authctxt)229 format_method_key(Authctxt *authctxt)
230 {
231 const struct sshkey *key = authctxt->auth_method_key;
232 const char *methinfo = authctxt->auth_method_info;
233 char *fp, *cafp, *ret = NULL;
234
235 if (key == NULL)
236 return NULL;
237
238 if (sshkey_is_cert(key)) {
239 fp = sshkey_fingerprint(key,
240 options.fingerprint_hash, SSH_FP_DEFAULT);
241 cafp = sshkey_fingerprint(key->cert->signature_key,
242 options.fingerprint_hash, SSH_FP_DEFAULT);
243 xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s",
244 sshkey_type(key), fp == NULL ? "(null)" : fp,
245 key->cert->key_id,
246 (unsigned long long)key->cert->serial,
247 sshkey_type(key->cert->signature_key),
248 cafp == NULL ? "(null)" : cafp,
249 methinfo == NULL ? "" : ", ",
250 methinfo == NULL ? "" : methinfo);
251 free(fp);
252 free(cafp);
253 } else {
254 fp = sshkey_fingerprint(key, options.fingerprint_hash,
255 SSH_FP_DEFAULT);
256 xasprintf(&ret, "%s %s%s%s", sshkey_type(key),
257 fp == NULL ? "(null)" : fp,
258 methinfo == NULL ? "" : ", ",
259 methinfo == NULL ? "" : methinfo);
260 free(fp);
261 }
262 return ret;
263 }
264
265 void
auth_log(struct ssh * ssh,int authenticated,int partial,const char * method,const char * submethod)266 auth_log(struct ssh *ssh, int authenticated, int partial,
267 const char *method, const char *submethod)
268 {
269 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
270 int level = SYSLOG_LEVEL_VERBOSE;
271 const char *authmsg;
272 char *extra = NULL;
273
274 if (!mm_is_monitor() && !authctxt->postponed)
275 return;
276
277 /* Raise logging level */
278 if (authenticated == 1 ||
279 !authctxt->valid ||
280 authctxt->failures >= options.max_authtries / 2 ||
281 strcmp(method, "password") == 0)
282 level = SYSLOG_LEVEL_INFO;
283
284 if (authctxt->postponed)
285 authmsg = "Postponed";
286 else if (partial)
287 authmsg = "Partial";
288 else
289 authmsg = authenticated ? "Accepted" : "Failed";
290
291 if ((extra = format_method_key(authctxt)) == NULL) {
292 if (authctxt->auth_method_info != NULL)
293 extra = xstrdup(authctxt->auth_method_info);
294 }
295
296 do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
297 authmsg,
298 method,
299 submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
300 authctxt->valid ? "" : "invalid user ",
301 authctxt->user,
302 ssh_remote_ipaddr(ssh),
303 ssh_remote_port(ssh),
304 extra != NULL ? ": " : "",
305 extra != NULL ? extra : "");
306
307 free(extra);
308
309 #if defined(CUSTOM_FAILED_LOGIN) || defined(SSH_AUDIT_EVENTS)
310 if (authenticated == 0 && !(authctxt->postponed || partial)) {
311 /* Log failed login attempt */
312 # ifdef CUSTOM_FAILED_LOGIN
313 if (strcmp(method, "password") == 0 ||
314 strncmp(method, "keyboard-interactive", 20) == 0 ||
315 strcmp(method, "challenge-response") == 0)
316 record_failed_login(ssh, authctxt->user,
317 auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
318 # endif
319 # ifdef SSH_AUDIT_EVENTS
320 audit_event(ssh, audit_classify_auth(method));
321 # endif
322 }
323 #endif
324 #if defined(CUSTOM_FAILED_LOGIN) && defined(WITH_AIXAUTHENTICATE)
325 if (authenticated)
326 sys_auth_record_login(authctxt->user,
327 auth_get_canonical_hostname(ssh, options.use_dns), "ssh",
328 loginmsg);
329 #endif
330 }
331
332 void
auth_maxtries_exceeded(struct ssh * ssh)333 auth_maxtries_exceeded(struct ssh *ssh)
334 {
335 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
336
337 error("maximum authentication attempts exceeded for "
338 "%s%.100s from %.200s port %d ssh2",
339 authctxt->valid ? "" : "invalid user ",
340 authctxt->user,
341 ssh_remote_ipaddr(ssh),
342 ssh_remote_port(ssh));
343 ssh_packet_disconnect(ssh, "Too many authentication failures");
344 /* NOTREACHED */
345 }
346
347 /*
348 * Check whether root logins are disallowed.
349 */
350 int
auth_root_allowed(struct ssh * ssh,const char * method)351 auth_root_allowed(struct ssh *ssh, const char *method)
352 {
353 switch (options.permit_root_login) {
354 case PERMIT_YES:
355 return 1;
356 case PERMIT_NO_PASSWD:
357 if (strcmp(method, "publickey") == 0 ||
358 strcmp(method, "hostbased") == 0 ||
359 strcmp(method, "gssapi-with-mic") == 0)
360 return 1;
361 break;
362 case PERMIT_FORCED_ONLY:
363 if (auth_opts->force_command != NULL) {
364 logit("Root login accepted for forced command.");
365 return 1;
366 }
367 break;
368 }
369 logit("ROOT LOGIN REFUSED FROM %.200s port %d",
370 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
371 return 0;
372 }
373
374
375 /*
376 * Given a template and a passwd structure, build a filename
377 * by substituting % tokenised options. Currently, %% becomes '%',
378 * %h becomes the home directory and %u the username.
379 *
380 * This returns a buffer allocated by xmalloc.
381 */
382 char *
expand_authorized_keys(const char * filename,struct passwd * pw)383 expand_authorized_keys(const char *filename, struct passwd *pw)
384 {
385 char *file, uidstr[32], ret[PATH_MAX];
386 int i;
387
388 snprintf(uidstr, sizeof(uidstr), "%llu",
389 (unsigned long long)pw->pw_uid);
390 file = percent_expand(filename, "h", pw->pw_dir,
391 "u", pw->pw_name, "U", uidstr, (char *)NULL);
392
393 /*
394 * Ensure that filename starts anchored. If not, be backward
395 * compatible and prepend the '%h/'
396 */
397 if (path_absolute(file))
398 return (file);
399
400 i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
401 if (i < 0 || (size_t)i >= sizeof(ret))
402 fatal("expand_authorized_keys: path too long");
403 free(file);
404 return (xstrdup(ret));
405 }
406
407 char *
authorized_principals_file(struct passwd * pw)408 authorized_principals_file(struct passwd *pw)
409 {
410 if (options.authorized_principals_file == NULL)
411 return NULL;
412 return expand_authorized_keys(options.authorized_principals_file, pw);
413 }
414
415 /* return ok if key exists in sysfile or userfile */
416 HostStatus
check_key_in_hostfiles(struct passwd * pw,struct sshkey * key,const char * host,const char * sysfile,const char * userfile)417 check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
418 const char *sysfile, const char *userfile)
419 {
420 char *user_hostfile;
421 struct stat st;
422 HostStatus host_status;
423 struct hostkeys *hostkeys;
424 const struct hostkey_entry *found;
425
426 hostkeys = init_hostkeys();
427 load_hostkeys(hostkeys, host, sysfile, 0);
428 if (userfile != NULL) {
429 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
430 if (options.strict_modes &&
431 (stat(user_hostfile, &st) == 0) &&
432 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
433 (st.st_mode & 022) != 0)) {
434 logit("Authentication refused for %.100s: "
435 "bad owner or modes for %.200s",
436 pw->pw_name, user_hostfile);
437 auth_debug_add("Ignored %.200s: bad ownership or modes",
438 user_hostfile);
439 } else {
440 temporarily_use_uid(pw);
441 load_hostkeys(hostkeys, host, user_hostfile, 0);
442 restore_uid();
443 }
444 free(user_hostfile);
445 }
446 host_status = check_key_in_hostkeys(hostkeys, key, &found);
447 if (host_status == HOST_REVOKED)
448 error("WARNING: revoked key for %s attempted authentication",
449 host);
450 else if (host_status == HOST_OK)
451 debug_f("key for %s found at %s:%ld",
452 found->host, found->file, found->line);
453 else
454 debug_f("key for host %s not found", host);
455
456 free_hostkeys(hostkeys);
457
458 return host_status;
459 }
460
461 struct passwd *
getpwnamallow(struct ssh * ssh,const char * user)462 getpwnamallow(struct ssh *ssh, const char *user)
463 {
464 #ifdef HAVE_LOGIN_CAP
465 extern login_cap_t *lc;
466 #ifdef BSD_AUTH
467 auth_session_t *as;
468 #endif
469 #endif
470 struct passwd *pw;
471 struct connection_info *ci;
472 u_int i;
473
474 ci = server_get_connection_info(ssh, 1, options.use_dns);
475 ci->user = user;
476 parse_server_match_config(&options, &includes, ci);
477 log_change_level(options.log_level);
478 log_verbose_reset();
479 for (i = 0; i < options.num_log_verbose; i++)
480 log_verbose_add(options.log_verbose[i]);
481 server_process_permitopen(ssh);
482
483 #if defined(_AIX) && defined(HAVE_SETAUTHDB)
484 aix_setauthdb(user);
485 #endif
486
487 pw = getpwnam(user);
488
489 #if defined(_AIX) && defined(HAVE_SETAUTHDB)
490 aix_restoreauthdb();
491 #endif
492 if (pw == NULL) {
493 logit("Invalid user %.100s from %.100s port %d",
494 user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
495 #ifdef CUSTOM_FAILED_LOGIN
496 record_failed_login(ssh, user,
497 auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
498 #endif
499 #ifdef SSH_AUDIT_EVENTS
500 audit_event(ssh, SSH_INVALID_USER);
501 #endif /* SSH_AUDIT_EVENTS */
502 return (NULL);
503 }
504 if (!allowed_user(ssh, pw))
505 return (NULL);
506 #ifdef HAVE_LOGIN_CAP
507 if ((lc = login_getpwclass(pw)) == NULL) {
508 debug("unable to get login class: %s", user);
509 return (NULL);
510 }
511 #ifdef BSD_AUTH
512 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
513 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
514 debug("Approval failure for %s", user);
515 pw = NULL;
516 }
517 if (as != NULL)
518 auth_close(as);
519 #endif
520 #endif
521 if (pw != NULL)
522 return (pwcopy(pw));
523 return (NULL);
524 }
525
526 /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
527 int
auth_key_is_revoked(struct sshkey * key)528 auth_key_is_revoked(struct sshkey *key)
529 {
530 char *fp = NULL;
531 int r;
532
533 if (options.revoked_keys_file == NULL)
534 return 0;
535 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
536 SSH_FP_DEFAULT)) == NULL) {
537 r = SSH_ERR_ALLOC_FAIL;
538 error_fr(r, "fingerprint key");
539 goto out;
540 }
541
542 r = sshkey_check_revoked(key, options.revoked_keys_file);
543 switch (r) {
544 case 0:
545 break; /* not revoked */
546 case SSH_ERR_KEY_REVOKED:
547 error("Authentication key %s %s revoked by file %s",
548 sshkey_type(key), fp, options.revoked_keys_file);
549 goto out;
550 default:
551 error_r(r, "Error checking authentication key %s %s in "
552 "revoked keys file %s", sshkey_type(key), fp,
553 options.revoked_keys_file);
554 goto out;
555 }
556
557 /* Success */
558 r = 0;
559
560 out:
561 free(fp);
562 return r == 0 ? 0 : 1;
563 }
564
565 void
auth_debug_add(const char * fmt,...)566 auth_debug_add(const char *fmt,...)
567 {
568 char buf[1024];
569 va_list args;
570 int r;
571
572 va_start(args, fmt);
573 vsnprintf(buf, sizeof(buf), fmt, args);
574 va_end(args);
575 debug3("%s", buf);
576 if (auth_debug != NULL)
577 if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
578 fatal_fr(r, "sshbuf_put_cstring");
579 }
580
581 void
auth_debug_send(struct ssh * ssh)582 auth_debug_send(struct ssh *ssh)
583 {
584 char *msg;
585 int r;
586
587 if (auth_debug == NULL)
588 return;
589 while (sshbuf_len(auth_debug) != 0) {
590 if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
591 fatal_fr(r, "sshbuf_get_cstring");
592 ssh_packet_send_debug(ssh, "%s", msg);
593 free(msg);
594 }
595 }
596
597 void
auth_debug_reset(void)598 auth_debug_reset(void)
599 {
600 if (auth_debug != NULL)
601 sshbuf_reset(auth_debug);
602 else if ((auth_debug = sshbuf_new()) == NULL)
603 fatal_f("sshbuf_new failed");
604 }
605
606 struct passwd *
fakepw(void)607 fakepw(void)
608 {
609 static int done = 0;
610 static struct passwd fake;
611 const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
612 "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */
613 char *cp;
614
615 if (done)
616 return (&fake);
617
618 memset(&fake, 0, sizeof(fake));
619 fake.pw_name = "NOUSER";
620 fake.pw_passwd = xstrdup("$2a$10$"
621 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
622 for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++)
623 *cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)];
624 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
625 fake.pw_gecos = "NOUSER";
626 #endif
627 fake.pw_uid = privsep_pw == NULL ? (uid_t)-1 : privsep_pw->pw_uid;
628 fake.pw_gid = privsep_pw == NULL ? (gid_t)-1 : privsep_pw->pw_gid;
629 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
630 fake.pw_class = "";
631 #endif
632 fake.pw_dir = "/nonexist";
633 fake.pw_shell = "/nonexist";
634 done = 1;
635
636 return (&fake);
637 }
638
639 /*
640 * Return the canonical name of the host in the other side of the current
641 * connection. The host name is cached, so it is efficient to call this
642 * several times.
643 */
644
645 const char *
auth_get_canonical_hostname(struct ssh * ssh,int use_dns)646 auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
647 {
648 static char *dnsname;
649
650 if (!use_dns)
651 return ssh_remote_ipaddr(ssh);
652 if (dnsname != NULL)
653 return dnsname;
654 dnsname = ssh_remote_hostname(ssh);
655 return dnsname;
656 }
657
658 /* These functions link key/cert options to the auth framework */
659
660 /* Log sshauthopt options locally and (optionally) for remote transmission */
661 void
auth_log_authopts(const char * loc,const struct sshauthopt * opts,int do_remote)662 auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
663 {
664 int do_env = options.permit_user_env && opts->nenv > 0;
665 int do_permitopen = opts->npermitopen > 0 &&
666 (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
667 int do_permitlisten = opts->npermitlisten > 0 &&
668 (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
669 size_t i;
670 char msg[1024], buf[64];
671
672 snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
673 /* Try to keep this alphabetically sorted */
674 snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
675 opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
676 opts->force_command == NULL ? "" : " command",
677 do_env ? " environment" : "",
678 opts->valid_before == 0 ? "" : "expires",
679 opts->no_require_user_presence ? " no-touch-required" : "",
680 do_permitopen ? " permitopen" : "",
681 do_permitlisten ? " permitlisten" : "",
682 opts->permit_port_forwarding_flag ? " port-forwarding" : "",
683 opts->cert_principals == NULL ? "" : " principals",
684 opts->permit_pty_flag ? " pty" : "",
685 opts->require_verify ? " uv" : "",
686 opts->force_tun_device == -1 ? "" : " tun=",
687 opts->force_tun_device == -1 ? "" : buf,
688 opts->permit_user_rc ? " user-rc" : "",
689 opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
690
691 debug("%s: %s", loc, msg);
692 if (do_remote)
693 auth_debug_add("%s: %s", loc, msg);
694
695 if (options.permit_user_env) {
696 for (i = 0; i < opts->nenv; i++) {
697 debug("%s: environment: %s", loc, opts->env[i]);
698 if (do_remote) {
699 auth_debug_add("%s: environment: %s",
700 loc, opts->env[i]);
701 }
702 }
703 }
704
705 /* Go into a little more details for the local logs. */
706 if (opts->valid_before != 0) {
707 format_absolute_time(opts->valid_before, buf, sizeof(buf));
708 debug("%s: expires at %s", loc, buf);
709 }
710 if (opts->cert_principals != NULL) {
711 debug("%s: authorized principals: \"%s\"",
712 loc, opts->cert_principals);
713 }
714 if (opts->force_command != NULL)
715 debug("%s: forced command: \"%s\"", loc, opts->force_command);
716 if (do_permitopen) {
717 for (i = 0; i < opts->npermitopen; i++) {
718 debug("%s: permitted open: %s",
719 loc, opts->permitopen[i]);
720 }
721 }
722 if (do_permitlisten) {
723 for (i = 0; i < opts->npermitlisten; i++) {
724 debug("%s: permitted listen: %s",
725 loc, opts->permitlisten[i]);
726 }
727 }
728 }
729
730 /* Activate a new set of key/cert options; merging with what is there. */
731 int
auth_activate_options(struct ssh * ssh,struct sshauthopt * opts)732 auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
733 {
734 struct sshauthopt *old = auth_opts;
735 const char *emsg = NULL;
736
737 debug_f("setting new authentication options");
738 if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
739 error("Inconsistent authentication options: %s", emsg);
740 return -1;
741 }
742 return 0;
743 }
744
745 /* Disable forwarding, etc for the session */
746 void
auth_restrict_session(struct ssh * ssh)747 auth_restrict_session(struct ssh *ssh)
748 {
749 struct sshauthopt *restricted;
750
751 debug_f("restricting session");
752
753 /* A blank sshauthopt defaults to permitting nothing */
754 if ((restricted = sshauthopt_new()) == NULL)
755 fatal_f("sshauthopt_new failed");
756 restricted->permit_pty_flag = 1;
757 restricted->restricted = 1;
758
759 if (auth_activate_options(ssh, restricted) != 0)
760 fatal_f("failed to restrict session");
761 sshauthopt_free(restricted);
762 }
763