xref: /openbsd-src/usr.bin/ssh/auth2-methods.c (revision a14ca28dd9d9b51e58d96ffb5d8b6575c5cd2f86)
13235f7a9Sdjm /*
23235f7a9Sdjm  * Copyright (c) 2012,2023 Damien Miller <djm@mindrot.org>
33235f7a9Sdjm  *
43235f7a9Sdjm  * Permission to use, copy, modify, and distribute this software for any
53235f7a9Sdjm  * purpose with or without fee is hereby granted, provided that the above
63235f7a9Sdjm  * copyright notice and this permission notice appear in all copies.
73235f7a9Sdjm  *
83235f7a9Sdjm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
93235f7a9Sdjm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
103235f7a9Sdjm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
113235f7a9Sdjm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
123235f7a9Sdjm  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
133235f7a9Sdjm  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
143235f7a9Sdjm  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
153235f7a9Sdjm  */
163235f7a9Sdjm 
173235f7a9Sdjm #include <sys/types.h>
183235f7a9Sdjm 
193235f7a9Sdjm #include <stdlib.h>
203235f7a9Sdjm #include <string.h>
213235f7a9Sdjm 
223235f7a9Sdjm #include "log.h"
233235f7a9Sdjm #include "misc.h"
243235f7a9Sdjm #include "servconf.h"
253235f7a9Sdjm #include "xmalloc.h"
263235f7a9Sdjm #include "hostfile.h"
273235f7a9Sdjm #include "auth.h"
283235f7a9Sdjm 
293235f7a9Sdjm extern ServerOptions options;
303235f7a9Sdjm 
313235f7a9Sdjm /*
32*a14ca28dSdjm  * Configuration of enabled authentication methods. Separate from the rest of
333235f7a9Sdjm  * auth2-*.c because we want to query it during server configuration validity
343235f7a9Sdjm  * checking in the sshd listener process without pulling all the auth code in
353235f7a9Sdjm  * too.
363235f7a9Sdjm  */
373235f7a9Sdjm 
38*a14ca28dSdjm /* "none" is allowed only one time and it is cleared by userauth_none() later */
393235f7a9Sdjm int none_enabled = 1;
403235f7a9Sdjm struct authmethod_cfg methodcfg_none = {
413235f7a9Sdjm 	"none",
423235f7a9Sdjm 	NULL,
433235f7a9Sdjm 	&none_enabled
443235f7a9Sdjm };
453235f7a9Sdjm struct authmethod_cfg methodcfg_pubkey = {
463235f7a9Sdjm 	"publickey",
473235f7a9Sdjm 	"publickey-hostbound-v00@openssh.com",
483235f7a9Sdjm 	&options.pubkey_authentication
493235f7a9Sdjm };
503235f7a9Sdjm #ifdef GSSAPI
513235f7a9Sdjm struct authmethod_cfg methodcfg_gssapi = {
523235f7a9Sdjm 	"gssapi-with-mic",
533235f7a9Sdjm 	NULL,
543235f7a9Sdjm 	&options.gss_authentication
553235f7a9Sdjm };
563235f7a9Sdjm #endif
573235f7a9Sdjm struct authmethod_cfg methodcfg_passwd = {
583235f7a9Sdjm 	"password",
593235f7a9Sdjm 	NULL,
603235f7a9Sdjm 	&options.password_authentication
613235f7a9Sdjm };
623235f7a9Sdjm struct authmethod_cfg methodcfg_kbdint = {
633235f7a9Sdjm 	"keyboard-interactive",
643235f7a9Sdjm 	NULL,
653235f7a9Sdjm 	&options.kbd_interactive_authentication
663235f7a9Sdjm };
673235f7a9Sdjm struct authmethod_cfg methodcfg_hostbased = {
683235f7a9Sdjm 	"hostbased",
693235f7a9Sdjm 	NULL,
703235f7a9Sdjm 	&options.hostbased_authentication
713235f7a9Sdjm };
723235f7a9Sdjm 
733235f7a9Sdjm static struct authmethod_cfg *authmethod_cfgs[] = {
743235f7a9Sdjm 	&methodcfg_none,
753235f7a9Sdjm 	&methodcfg_pubkey,
763235f7a9Sdjm #ifdef GSSAPI
773235f7a9Sdjm 	&methodcfg_gssapi,
783235f7a9Sdjm #endif
793235f7a9Sdjm 	&methodcfg_passwd,
803235f7a9Sdjm 	&methodcfg_kbdint,
813235f7a9Sdjm 	&methodcfg_hostbased,
823235f7a9Sdjm 	NULL
833235f7a9Sdjm };
843235f7a9Sdjm 
853235f7a9Sdjm /*
86*a14ca28dSdjm  * Check a comma-separated list of methods for validity. If need_enable is
873235f7a9Sdjm  * non-zero, then also require that the methods are enabled.
883235f7a9Sdjm  * Returns 0 on success or -1 if the methods list is invalid.
893235f7a9Sdjm  */
903235f7a9Sdjm int
auth2_methods_valid(const char * _methods,int need_enable)913235f7a9Sdjm auth2_methods_valid(const char *_methods, int need_enable)
923235f7a9Sdjm {
933235f7a9Sdjm 	char *methods, *omethods, *method, *p;
943235f7a9Sdjm 	u_int i, found;
953235f7a9Sdjm 	int ret = -1;
963235f7a9Sdjm 	const struct authmethod_cfg *cfg;
973235f7a9Sdjm 
983235f7a9Sdjm 	if (*_methods == '\0') {
993235f7a9Sdjm 		error("empty authentication method list");
1003235f7a9Sdjm 		return -1;
1013235f7a9Sdjm 	}
1023235f7a9Sdjm 	omethods = methods = xstrdup(_methods);
1033235f7a9Sdjm 	while ((method = strsep(&methods, ",")) != NULL) {
1043235f7a9Sdjm 		for (found = i = 0; !found && authmethod_cfgs[i] != NULL; i++) {
1053235f7a9Sdjm 			cfg = authmethod_cfgs[i];
1063235f7a9Sdjm 			if ((p = strchr(method, ':')) != NULL)
1073235f7a9Sdjm 				*p = '\0';
1083235f7a9Sdjm 			if (strcmp(method, cfg->name) != 0)
1093235f7a9Sdjm 				continue;
1103235f7a9Sdjm 			if (need_enable) {
1113235f7a9Sdjm 				if (cfg->enabled == NULL ||
1123235f7a9Sdjm 				    *(cfg->enabled) == 0) {
1133235f7a9Sdjm 					error("Disabled method \"%s\" in "
1143235f7a9Sdjm 					    "AuthenticationMethods list \"%s\"",
1153235f7a9Sdjm 					    method, _methods);
1163235f7a9Sdjm 					goto out;
1173235f7a9Sdjm 				}
1183235f7a9Sdjm 			}
1193235f7a9Sdjm 			found = 1;
1203235f7a9Sdjm 			break;
1213235f7a9Sdjm 		}
1223235f7a9Sdjm 		if (!found) {
1233235f7a9Sdjm 			error("Unknown authentication method \"%s\" in list",
1243235f7a9Sdjm 			    method);
1253235f7a9Sdjm 			goto out;
1263235f7a9Sdjm 		}
1273235f7a9Sdjm 	}
1283235f7a9Sdjm 	ret = 0;
1293235f7a9Sdjm  out:
1303235f7a9Sdjm 	free(omethods);
1313235f7a9Sdjm 	return ret;
1323235f7a9Sdjm }
133