xref: /freebsd/usr.sbin/ctld/ctld.c (revision 6de78f9f24992462e9081f7ebd377648f64272f1)
1009ea47eSEdward Tomasz Napierala /*-
2009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
3009ea47eSEdward Tomasz Napierala  * All rights reserved.
4009ea47eSEdward Tomasz Napierala  *
5009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
7009ea47eSEdward Tomasz Napierala  *
8009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10009ea47eSEdward Tomasz Napierala  * are met:
11009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16009ea47eSEdward Tomasz Napierala  *
17009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
28009ea47eSEdward Tomasz Napierala  *
29009ea47eSEdward Tomasz Napierala  */
30009ea47eSEdward Tomasz Napierala 
31b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h>
32b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$");
33b7a65e39SEdward Tomasz Napierala 
34009ea47eSEdward Tomasz Napierala #include <sys/types.h>
35009ea47eSEdward Tomasz Napierala #include <sys/time.h>
36009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
37009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
38009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
39073edb1cSAlexander Motin #include <arpa/inet.h>
40009ea47eSEdward Tomasz Napierala #include <assert.h>
41009ea47eSEdward Tomasz Napierala #include <ctype.h>
42009ea47eSEdward Tomasz Napierala #include <errno.h>
43009ea47eSEdward Tomasz Napierala #include <netdb.h>
44009ea47eSEdward Tomasz Napierala #include <signal.h>
45009ea47eSEdward Tomasz Napierala #include <stdbool.h>
46009ea47eSEdward Tomasz Napierala #include <stdio.h>
47009ea47eSEdward Tomasz Napierala #include <stdint.h>
48009ea47eSEdward Tomasz Napierala #include <stdlib.h>
49009ea47eSEdward Tomasz Napierala #include <string.h>
50009ea47eSEdward Tomasz Napierala #include <unistd.h>
51009ea47eSEdward Tomasz Napierala 
52009ea47eSEdward Tomasz Napierala #include "ctld.h"
53829603e2SAlexander Motin #include "isns.h"
54009ea47eSEdward Tomasz Napierala 
55ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false;
56ba3a2d31SEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
58009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
59009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
60009ea47eSEdward Tomasz Napierala 
61009ea47eSEdward Tomasz Napierala static int nchildren = 0;
62009ea47eSEdward Tomasz Napierala 
63009ea47eSEdward Tomasz Napierala static void
64009ea47eSEdward Tomasz Napierala usage(void)
65009ea47eSEdward Tomasz Napierala {
66009ea47eSEdward Tomasz Napierala 
67009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
68009ea47eSEdward Tomasz Napierala 	exit(1);
69009ea47eSEdward Tomasz Napierala }
70009ea47eSEdward Tomasz Napierala 
71009ea47eSEdward Tomasz Napierala char *
72009ea47eSEdward Tomasz Napierala checked_strdup(const char *s)
73009ea47eSEdward Tomasz Napierala {
74009ea47eSEdward Tomasz Napierala 	char *c;
75009ea47eSEdward Tomasz Napierala 
76009ea47eSEdward Tomasz Napierala 	c = strdup(s);
77009ea47eSEdward Tomasz Napierala 	if (c == NULL)
78009ea47eSEdward Tomasz Napierala 		log_err(1, "strdup");
79009ea47eSEdward Tomasz Napierala 	return (c);
80009ea47eSEdward Tomasz Napierala }
81009ea47eSEdward Tomasz Napierala 
82009ea47eSEdward Tomasz Napierala struct conf *
83009ea47eSEdward Tomasz Napierala conf_new(void)
84009ea47eSEdward Tomasz Napierala {
85009ea47eSEdward Tomasz Napierala 	struct conf *conf;
86009ea47eSEdward Tomasz Napierala 
87009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
88009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
89009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
90009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
91009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
92009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
93829603e2SAlexander Motin 	TAILQ_INIT(&conf->conf_isns);
94009ea47eSEdward Tomasz Napierala 
95829603e2SAlexander Motin 	conf->conf_isns_period = 900;
96829603e2SAlexander Motin 	conf->conf_isns_timeout = 5;
97009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
98009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
99009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
100009ea47eSEdward Tomasz Napierala 
101009ea47eSEdward Tomasz Napierala 	return (conf);
102009ea47eSEdward Tomasz Napierala }
103009ea47eSEdward Tomasz Napierala 
104009ea47eSEdward Tomasz Napierala void
105009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
106009ea47eSEdward Tomasz Napierala {
107009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
108009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
109009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
110829603e2SAlexander Motin 	struct isns *is, *istmp;
111009ea47eSEdward Tomasz Napierala 
112009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
113009ea47eSEdward Tomasz Napierala 
114009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
115009ea47eSEdward Tomasz Napierala 		target_delete(targ);
116009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
117009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
118009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
119009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
120829603e2SAlexander Motin 	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
121829603e2SAlexander Motin 		isns_delete(is);
122009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
123009ea47eSEdward Tomasz Napierala 	free(conf);
124009ea47eSEdward Tomasz Napierala }
125009ea47eSEdward Tomasz Napierala 
126009ea47eSEdward Tomasz Napierala static struct auth *
127009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
128009ea47eSEdward Tomasz Napierala {
129009ea47eSEdward Tomasz Napierala 	struct auth *auth;
130009ea47eSEdward Tomasz Napierala 
131009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
132009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
133009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
134009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
135009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
136009ea47eSEdward Tomasz Napierala 	return (auth);
137009ea47eSEdward Tomasz Napierala }
138009ea47eSEdward Tomasz Napierala 
139009ea47eSEdward Tomasz Napierala static void
140009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
141009ea47eSEdward Tomasz Napierala {
142009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
143009ea47eSEdward Tomasz Napierala 
144009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
145009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
146009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
147009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
148009ea47eSEdward Tomasz Napierala 	free(auth);
149009ea47eSEdward Tomasz Napierala }
150009ea47eSEdward Tomasz Napierala 
151009ea47eSEdward Tomasz Napierala const struct auth *
152d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
153009ea47eSEdward Tomasz Napierala {
154009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
155009ea47eSEdward Tomasz Napierala 
156009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
157009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
158009ea47eSEdward Tomasz Napierala 			return (auth);
159009ea47eSEdward Tomasz Napierala 	}
160009ea47eSEdward Tomasz Napierala 
161009ea47eSEdward Tomasz Napierala 	return (NULL);
162009ea47eSEdward Tomasz Napierala }
163009ea47eSEdward Tomasz Napierala 
1641658628dSEdward Tomasz Napierala static void
1651658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1661658628dSEdward Tomasz Napierala {
1671658628dSEdward Tomasz Napierala 	size_t len;
1681658628dSEdward Tomasz Napierala 
1691658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1701658628dSEdward Tomasz Napierala 	if (len > 16) {
1711658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1721658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1731658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1741658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1751658628dSEdward Tomasz Napierala 		else
1761658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1771658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1781658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
179f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1801658628dSEdward Tomasz Napierala 	}
1811658628dSEdward Tomasz Napierala 	if (len < 12) {
1821658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1831658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1841658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1851658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1861658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1871658628dSEdward Tomasz Napierala 		else
1881658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1891658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 16 characters "
1901658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
191f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1921658628dSEdward Tomasz Napierala 	}
1931658628dSEdward Tomasz Napierala 
1941658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
1951658628dSEdward Tomasz Napierala 		len = strlen(auth->a_secret);
1961658628dSEdward Tomasz Napierala 		if (len > 16) {
1971658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
1981658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1991658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
2001658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2011658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2021658628dSEdward Tomasz Napierala 			else
2031658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2041658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
2051658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2061658628dSEdward Tomasz Napierala 				    auth->a_user,
207f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2081658628dSEdward Tomasz Napierala 		}
2091658628dSEdward Tomasz Napierala 		if (len < 12) {
2101658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2111658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2121658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2131658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2141658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2151658628dSEdward Tomasz Napierala 			else
2161658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2171658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2181658628dSEdward Tomasz Napierala 				    "at least 16 characters long",
2191658628dSEdward Tomasz Napierala 				    auth->a_user,
220f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2211658628dSEdward Tomasz Napierala 		}
2221658628dSEdward Tomasz Napierala 	}
2231658628dSEdward Tomasz Napierala }
2241658628dSEdward Tomasz Napierala 
2251658628dSEdward Tomasz Napierala const struct auth *
2261658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2271658628dSEdward Tomasz Napierala     const char *secret)
2281658628dSEdward Tomasz Napierala {
2291658628dSEdward Tomasz Napierala 	struct auth *auth;
2301658628dSEdward Tomasz Napierala 
2311658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2321658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2331658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2341658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2351658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2361658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2371658628dSEdward Tomasz Napierala 		else
2381658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2391658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
240f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2411658628dSEdward Tomasz Napierala 		return (NULL);
2421658628dSEdward Tomasz Napierala 	}
2431658628dSEdward Tomasz Napierala 
2441658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2451658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2461658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2471658628dSEdward Tomasz Napierala 
2481658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2491658628dSEdward Tomasz Napierala 
2501658628dSEdward Tomasz Napierala 	return (auth);
2511658628dSEdward Tomasz Napierala }
2521658628dSEdward Tomasz Napierala 
2531658628dSEdward Tomasz Napierala const struct auth *
2541658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2551658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2561658628dSEdward Tomasz Napierala {
2571658628dSEdward Tomasz Napierala 	struct auth *auth;
2581658628dSEdward Tomasz Napierala 
2591658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2601658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2611658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2621658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2631658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2641658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2651658628dSEdward Tomasz Napierala 			    ag->ag_name);
2661658628dSEdward Tomasz Napierala 		else
2671658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2681658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
269f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2701658628dSEdward Tomasz Napierala 		return (NULL);
2711658628dSEdward Tomasz Napierala 	}
2721658628dSEdward Tomasz Napierala 
2731658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2741658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2751658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2761658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2771658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2781658628dSEdward Tomasz Napierala 
2791658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2801658628dSEdward Tomasz Napierala 
2811658628dSEdward Tomasz Napierala 	return (auth);
2821658628dSEdward Tomasz Napierala }
2831658628dSEdward Tomasz Napierala 
2848cb2e958SEdward Tomasz Napierala const struct auth_name *
2858cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2868cb2e958SEdward Tomasz Napierala {
2878cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
2888cb2e958SEdward Tomasz Napierala 
2898cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
2908cb2e958SEdward Tomasz Napierala 	if (an == NULL)
2918cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
2928cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
2938cb2e958SEdward Tomasz Napierala 	an->an_initator_name = checked_strdup(name);
2948cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
2958cb2e958SEdward Tomasz Napierala 	return (an);
2968cb2e958SEdward Tomasz Napierala }
2978cb2e958SEdward Tomasz Napierala 
2988cb2e958SEdward Tomasz Napierala static void
2998cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
3008cb2e958SEdward Tomasz Napierala {
3018cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
3028cb2e958SEdward Tomasz Napierala 
3038cb2e958SEdward Tomasz Napierala 	free(an->an_initator_name);
3048cb2e958SEdward Tomasz Napierala 	free(an);
3058cb2e958SEdward Tomasz Napierala }
3068cb2e958SEdward Tomasz Napierala 
3078cb2e958SEdward Tomasz Napierala bool
3088cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
3098cb2e958SEdward Tomasz Napierala {
3108cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3118cb2e958SEdward Tomasz Napierala 		return (false);
3128cb2e958SEdward Tomasz Napierala 	return (true);
3138cb2e958SEdward Tomasz Napierala }
3148cb2e958SEdward Tomasz Napierala 
3158cb2e958SEdward Tomasz Napierala const struct auth_name *
3168cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3178cb2e958SEdward Tomasz Napierala {
3188cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3198cb2e958SEdward Tomasz Napierala 
3208cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
3218cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_name->an_initator_name, name) == 0)
3228cb2e958SEdward Tomasz Napierala 			return (auth_name);
3238cb2e958SEdward Tomasz Napierala 	}
3248cb2e958SEdward Tomasz Napierala 
3258cb2e958SEdward Tomasz Napierala 	return (NULL);
3268cb2e958SEdward Tomasz Napierala }
3278cb2e958SEdward Tomasz Napierala 
328e867e162SEdward Tomasz Napierala int
329e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name)
330e867e162SEdward Tomasz Napierala {
331e867e162SEdward Tomasz Napierala 	if (!auth_name_defined(ag))
332e867e162SEdward Tomasz Napierala 		return (0);
333e867e162SEdward Tomasz Napierala 
334e867e162SEdward Tomasz Napierala 	if (auth_name_find(ag, initiator_name) == NULL)
335e867e162SEdward Tomasz Napierala 		return (1);
336e867e162SEdward Tomasz Napierala 
337e867e162SEdward Tomasz Napierala 	return (0);
338e867e162SEdward Tomasz Napierala }
339e867e162SEdward Tomasz Napierala 
3408cb2e958SEdward Tomasz Napierala const struct auth_portal *
3418cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3428cb2e958SEdward Tomasz Napierala {
3438cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
344073edb1cSAlexander Motin 	char *net, *mask, *str, *tmp;
345073edb1cSAlexander Motin 	int len, dm, m;
3468cb2e958SEdward Tomasz Napierala 
3478cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3488cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3498cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3508cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
3518cb2e958SEdward Tomasz Napierala 	ap->ap_initator_portal = checked_strdup(portal);
352073edb1cSAlexander Motin 	mask = str = checked_strdup(portal);
353073edb1cSAlexander Motin 	net = strsep(&mask, "/");
354073edb1cSAlexander Motin 	if (net[0] == '[')
355073edb1cSAlexander Motin 		net++;
356073edb1cSAlexander Motin 	len = strlen(net);
357073edb1cSAlexander Motin 	if (len == 0)
358073edb1cSAlexander Motin 		goto error;
359073edb1cSAlexander Motin 	if (net[len - 1] == ']')
360073edb1cSAlexander Motin 		net[len - 1] = 0;
361073edb1cSAlexander Motin 	if (strchr(net, ':') != NULL) {
362073edb1cSAlexander Motin 		struct sockaddr_in6 *sin6 =
363073edb1cSAlexander Motin 		    (struct sockaddr_in6 *)&ap->ap_sa;
364073edb1cSAlexander Motin 
365073edb1cSAlexander Motin 		sin6->sin6_len = sizeof(*sin6);
366073edb1cSAlexander Motin 		sin6->sin6_family = AF_INET6;
367073edb1cSAlexander Motin 		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
368073edb1cSAlexander Motin 			goto error;
369073edb1cSAlexander Motin 		dm = 128;
370073edb1cSAlexander Motin 	} else {
371073edb1cSAlexander Motin 		struct sockaddr_in *sin =
372073edb1cSAlexander Motin 		    (struct sockaddr_in *)&ap->ap_sa;
373073edb1cSAlexander Motin 
374073edb1cSAlexander Motin 		sin->sin_len = sizeof(*sin);
375073edb1cSAlexander Motin 		sin->sin_family = AF_INET;
376073edb1cSAlexander Motin 		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
377073edb1cSAlexander Motin 			goto error;
378073edb1cSAlexander Motin 		dm = 32;
379073edb1cSAlexander Motin 	}
380073edb1cSAlexander Motin 	if (mask != NULL) {
381073edb1cSAlexander Motin 		m = strtol(mask, &tmp, 0);
382073edb1cSAlexander Motin 		if (m < 0 || m > dm || tmp[0] != 0)
383073edb1cSAlexander Motin 			goto error;
384073edb1cSAlexander Motin 	} else
385073edb1cSAlexander Motin 		m = dm;
386073edb1cSAlexander Motin 	ap->ap_mask = m;
387073edb1cSAlexander Motin 	free(str);
3888cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
3898cb2e958SEdward Tomasz Napierala 	return (ap);
390073edb1cSAlexander Motin 
391073edb1cSAlexander Motin error:
392073edb1cSAlexander Motin 	log_errx(1, "Incorrect initiator portal '%s'", portal);
393073edb1cSAlexander Motin 	return (NULL);
3948cb2e958SEdward Tomasz Napierala }
3958cb2e958SEdward Tomasz Napierala 
3968cb2e958SEdward Tomasz Napierala static void
3978cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
3988cb2e958SEdward Tomasz Napierala {
3998cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
4008cb2e958SEdward Tomasz Napierala 
4018cb2e958SEdward Tomasz Napierala 	free(ap->ap_initator_portal);
4028cb2e958SEdward Tomasz Napierala 	free(ap);
4038cb2e958SEdward Tomasz Napierala }
4048cb2e958SEdward Tomasz Napierala 
4058cb2e958SEdward Tomasz Napierala bool
4068cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
4078cb2e958SEdward Tomasz Napierala {
4088cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
4098cb2e958SEdward Tomasz Napierala 		return (false);
4108cb2e958SEdward Tomasz Napierala 	return (true);
4118cb2e958SEdward Tomasz Napierala }
4128cb2e958SEdward Tomasz Napierala 
4138cb2e958SEdward Tomasz Napierala const struct auth_portal *
414073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
4158cb2e958SEdward Tomasz Napierala {
416073edb1cSAlexander Motin 	const struct auth_portal *ap;
4175e46a066SAlexander Motin 	const uint8_t *a, *b;
418073edb1cSAlexander Motin 	int i;
4195e46a066SAlexander Motin 	uint8_t bmask;
4208cb2e958SEdward Tomasz Napierala 
421073edb1cSAlexander Motin 	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
422073edb1cSAlexander Motin 		if (ap->ap_sa.ss_family != ss->ss_family)
423073edb1cSAlexander Motin 			continue;
424073edb1cSAlexander Motin 		if (ss->ss_family == AF_INET) {
4255e46a066SAlexander Motin 			a = (const uint8_t *)
4265e46a066SAlexander Motin 			    &((const struct sockaddr_in *)ss)->sin_addr;
4275e46a066SAlexander Motin 			b = (const uint8_t *)
4285e46a066SAlexander Motin 			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
429073edb1cSAlexander Motin 		} else {
4305e46a066SAlexander Motin 			a = (const uint8_t *)
4315e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
4325e46a066SAlexander Motin 			b = (const uint8_t *)
4335e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
434073edb1cSAlexander Motin 		}
435073edb1cSAlexander Motin 		for (i = 0; i < ap->ap_mask / 8; i++) {
436073edb1cSAlexander Motin 			if (a[i] != b[i])
437073edb1cSAlexander Motin 				goto next;
438073edb1cSAlexander Motin 		}
439073edb1cSAlexander Motin 		if (ap->ap_mask % 8) {
440073edb1cSAlexander Motin 			bmask = 0xff << (8 - (ap->ap_mask % 8));
441073edb1cSAlexander Motin 			if ((a[i] & bmask) != (b[i] & bmask))
442073edb1cSAlexander Motin 				goto next;
443073edb1cSAlexander Motin 		}
444073edb1cSAlexander Motin 		return (ap);
445073edb1cSAlexander Motin next:
446073edb1cSAlexander Motin 		;
4478cb2e958SEdward Tomasz Napierala 	}
4488cb2e958SEdward Tomasz Napierala 
4498cb2e958SEdward Tomasz Napierala 	return (NULL);
4508cb2e958SEdward Tomasz Napierala }
4518cb2e958SEdward Tomasz Napierala 
452e867e162SEdward Tomasz Napierala int
453e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
454e867e162SEdward Tomasz Napierala {
455e867e162SEdward Tomasz Napierala 
456e867e162SEdward Tomasz Napierala 	if (!auth_portal_defined(ag))
457e867e162SEdward Tomasz Napierala 		return (0);
458e867e162SEdward Tomasz Napierala 
459e867e162SEdward Tomasz Napierala 	if (auth_portal_find(ag, sa) == NULL)
460e867e162SEdward Tomasz Napierala 		return (1);
461e867e162SEdward Tomasz Napierala 
462e867e162SEdward Tomasz Napierala 	return (0);
463e867e162SEdward Tomasz Napierala }
464e867e162SEdward Tomasz Napierala 
465009ea47eSEdward Tomasz Napierala struct auth_group *
466009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
467009ea47eSEdward Tomasz Napierala {
468009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
469009ea47eSEdward Tomasz Napierala 
470009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
471009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
472009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
473009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
474009ea47eSEdward Tomasz Napierala 			return (NULL);
475009ea47eSEdward Tomasz Napierala 		}
476009ea47eSEdward Tomasz Napierala 	}
477009ea47eSEdward Tomasz Napierala 
478009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
479009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
480009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
481009ea47eSEdward Tomasz Napierala 	if (name != NULL)
482009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
483009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
4848cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
4858cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
486009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
487009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
488009ea47eSEdward Tomasz Napierala 
489009ea47eSEdward Tomasz Napierala 	return (ag);
490009ea47eSEdward Tomasz Napierala }
491009ea47eSEdward Tomasz Napierala 
492009ea47eSEdward Tomasz Napierala void
493009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
494009ea47eSEdward Tomasz Napierala {
4958cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
4968cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
4978cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
498009ea47eSEdward Tomasz Napierala 
499009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
500009ea47eSEdward Tomasz Napierala 
5018cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
502009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
5038cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
5048cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
5058cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
5068cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
5078cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
508009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
509009ea47eSEdward Tomasz Napierala 	free(ag);
510009ea47eSEdward Tomasz Napierala }
511009ea47eSEdward Tomasz Napierala 
512009ea47eSEdward Tomasz Napierala struct auth_group *
513d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
514009ea47eSEdward Tomasz Napierala {
515009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
516009ea47eSEdward Tomasz Napierala 
517009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
518009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
519009ea47eSEdward Tomasz Napierala 			return (ag);
520009ea47eSEdward Tomasz Napierala 	}
521009ea47eSEdward Tomasz Napierala 
522009ea47eSEdward Tomasz Napierala 	return (NULL);
523009ea47eSEdward Tomasz Napierala }
524009ea47eSEdward Tomasz Napierala 
525df9900fbSEdward Tomasz Napierala int
526bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str)
527df9900fbSEdward Tomasz Napierala {
528bd833b11SEdward Tomasz Napierala 	int type;
529df9900fbSEdward Tomasz Napierala 
530df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
531df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
532e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
533e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
534df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
535df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
536df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
537df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
538df9900fbSEdward Tomasz Napierala 	} else {
539df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
540df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
541df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
542df9900fbSEdward Tomasz Napierala 		else
543df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
544df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
545df9900fbSEdward Tomasz Napierala 		return (1);
546df9900fbSEdward Tomasz Napierala 	}
547df9900fbSEdward Tomasz Napierala 
548bd833b11SEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
549bd833b11SEdward Tomasz Napierala 		if (ag->ag_name != NULL) {
550df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
551df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
552df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
553bd833b11SEdward Tomasz Napierala 		} else {
554df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
555df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
556df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
557bd833b11SEdward Tomasz Napierala 		}
558df9900fbSEdward Tomasz Napierala 		return (1);
559df9900fbSEdward Tomasz Napierala 	}
560df9900fbSEdward Tomasz Napierala 
561bd833b11SEdward Tomasz Napierala 	ag->ag_type = type;
562bd833b11SEdward Tomasz Napierala 
563bd833b11SEdward Tomasz Napierala 	return (0);
564df9900fbSEdward Tomasz Napierala }
565df9900fbSEdward Tomasz Napierala 
566009ea47eSEdward Tomasz Napierala static struct portal *
567009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
568009ea47eSEdward Tomasz Napierala {
569009ea47eSEdward Tomasz Napierala 	struct portal *portal;
570009ea47eSEdward Tomasz Napierala 
571009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
572009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
573009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
574009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
575009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
576009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
577009ea47eSEdward Tomasz Napierala 	return (portal);
578009ea47eSEdward Tomasz Napierala }
579009ea47eSEdward Tomasz Napierala 
580009ea47eSEdward Tomasz Napierala static void
581009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
582009ea47eSEdward Tomasz Napierala {
58344a5953aSEdward Tomasz Napierala 
584009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
58544a5953aSEdward Tomasz Napierala 	if (portal->p_ai != NULL)
586009ea47eSEdward Tomasz Napierala 		freeaddrinfo(portal->p_ai);
587009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
588009ea47eSEdward Tomasz Napierala 	free(portal);
589009ea47eSEdward Tomasz Napierala }
590009ea47eSEdward Tomasz Napierala 
591009ea47eSEdward Tomasz Napierala struct portal_group *
592009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
593009ea47eSEdward Tomasz Napierala {
594009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
595009ea47eSEdward Tomasz Napierala 
596009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
597009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
598009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
599009ea47eSEdward Tomasz Napierala 		return (NULL);
600009ea47eSEdward Tomasz Napierala 	}
601009ea47eSEdward Tomasz Napierala 
602009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
603009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
604009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
605009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
606009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
607009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
608009ea47eSEdward Tomasz Napierala 	conf->conf_last_portal_group_tag++;
609009ea47eSEdward Tomasz Napierala 	pg->pg_tag = conf->conf_last_portal_group_tag;
610009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
611009ea47eSEdward Tomasz Napierala 
612009ea47eSEdward Tomasz Napierala 	return (pg);
613009ea47eSEdward Tomasz Napierala }
614009ea47eSEdward Tomasz Napierala 
615009ea47eSEdward Tomasz Napierala void
616009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
617009ea47eSEdward Tomasz Napierala {
618009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
619009ea47eSEdward Tomasz Napierala 
620009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
621009ea47eSEdward Tomasz Napierala 
622009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
623009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
624009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
6256dbdbf82SEdward Tomasz Napierala 	free(pg->pg_redirection);
626009ea47eSEdward Tomasz Napierala 	free(pg);
627009ea47eSEdward Tomasz Napierala }
628009ea47eSEdward Tomasz Napierala 
629009ea47eSEdward Tomasz Napierala struct portal_group *
630d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
631009ea47eSEdward Tomasz Napierala {
632009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
633009ea47eSEdward Tomasz Napierala 
634009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
635009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
636009ea47eSEdward Tomasz Napierala 			return (pg);
637009ea47eSEdward Tomasz Napierala 	}
638009ea47eSEdward Tomasz Napierala 
639009ea47eSEdward Tomasz Napierala 	return (NULL);
640009ea47eSEdward Tomasz Napierala }
641009ea47eSEdward Tomasz Napierala 
642829603e2SAlexander Motin static int
643829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
644009ea47eSEdward Tomasz Napierala {
645009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
646829603e2SAlexander Motin 	char *addr, *ch;
647009ea47eSEdward Tomasz Napierala 	const char *port;
648009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
649009ea47eSEdward Tomasz Napierala 
650009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
651009ea47eSEdward Tomasz Napierala 		/*
652009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
653009ea47eSEdward Tomasz Napierala 		 */
654009ea47eSEdward Tomasz Napierala 		arg++;
655009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
656829603e2SAlexander Motin 		if (arg == NULL)
657009ea47eSEdward Tomasz Napierala 			return (1);
658009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
659829603e2SAlexander Motin 			port = def_port;
660009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
661009ea47eSEdward Tomasz Napierala 			port = arg + 1;
662829603e2SAlexander Motin 		} else
663009ea47eSEdward Tomasz Napierala 			return (1);
664009ea47eSEdward Tomasz Napierala 	} else {
665009ea47eSEdward Tomasz Napierala 		/*
666009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
667009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
668009ea47eSEdward Tomasz Napierala 		 */
669009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
670009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
671009ea47eSEdward Tomasz Napierala 				colons++;
672009ea47eSEdward Tomasz Napierala 		}
673009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
674009ea47eSEdward Tomasz Napierala 			addr = arg;
675829603e2SAlexander Motin 			port = def_port;
676009ea47eSEdward Tomasz Napierala 		} else {
677009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
678009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
679829603e2SAlexander Motin 				port = def_port;
680009ea47eSEdward Tomasz Napierala 			else
681009ea47eSEdward Tomasz Napierala 				port = arg;
682009ea47eSEdward Tomasz Napierala 		}
683009ea47eSEdward Tomasz Napierala 	}
684009ea47eSEdward Tomasz Napierala 
685009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
686009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
687009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
688009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
689829603e2SAlexander Motin 	error = getaddrinfo(addr, port, &hints, ai);
690829603e2SAlexander Motin 	if (error != 0)
691829603e2SAlexander Motin 		return (1);
692829603e2SAlexander Motin 	return (0);
693829603e2SAlexander Motin }
694009ea47eSEdward Tomasz Napierala 
695829603e2SAlexander Motin int
696829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
697829603e2SAlexander Motin {
698829603e2SAlexander Motin 	struct portal *portal;
699829603e2SAlexander Motin 
700829603e2SAlexander Motin 	portal = portal_new(pg);
701829603e2SAlexander Motin 	portal->p_listen = checked_strdup(value);
702829603e2SAlexander Motin 	portal->p_iser = iser;
703829603e2SAlexander Motin 
704829603e2SAlexander Motin 	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
705829603e2SAlexander Motin 		log_warnx("invalid listen address %s", portal->p_listen);
70644a5953aSEdward Tomasz Napierala 		portal_delete(portal);
707009ea47eSEdward Tomasz Napierala 		return (1);
708009ea47eSEdward Tomasz Napierala 	}
709009ea47eSEdward Tomasz Napierala 
710009ea47eSEdward Tomasz Napierala 	/*
711009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
712009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
713009ea47eSEdward Tomasz Napierala 	 */
714009ea47eSEdward Tomasz Napierala 
715009ea47eSEdward Tomasz Napierala 	return (0);
716009ea47eSEdward Tomasz Napierala }
717009ea47eSEdward Tomasz Napierala 
718829603e2SAlexander Motin int
719829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr)
720829603e2SAlexander Motin {
721829603e2SAlexander Motin 	struct isns *isns;
722829603e2SAlexander Motin 
723829603e2SAlexander Motin 	isns = calloc(1, sizeof(*isns));
724829603e2SAlexander Motin 	if (isns == NULL)
725829603e2SAlexander Motin 		log_err(1, "calloc");
726829603e2SAlexander Motin 	isns->i_conf = conf;
727829603e2SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
728829603e2SAlexander Motin 	isns->i_addr = checked_strdup(addr);
729829603e2SAlexander Motin 
730829603e2SAlexander Motin 	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
731829603e2SAlexander Motin 		log_warnx("invalid iSNS address %s", isns->i_addr);
732829603e2SAlexander Motin 		isns_delete(isns);
733829603e2SAlexander Motin 		return (1);
734829603e2SAlexander Motin 	}
735829603e2SAlexander Motin 
736829603e2SAlexander Motin 	/*
737829603e2SAlexander Motin 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
738829603e2SAlexander Motin 	 *	those into multiple servers.
739829603e2SAlexander Motin 	 */
740829603e2SAlexander Motin 
741829603e2SAlexander Motin 	return (0);
742829603e2SAlexander Motin }
743829603e2SAlexander Motin 
744829603e2SAlexander Motin void
745829603e2SAlexander Motin isns_delete(struct isns *isns)
746829603e2SAlexander Motin {
747829603e2SAlexander Motin 
748829603e2SAlexander Motin 	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
749829603e2SAlexander Motin 	free(isns->i_addr);
750829603e2SAlexander Motin 	if (isns->i_ai != NULL)
751829603e2SAlexander Motin 		freeaddrinfo(isns->i_ai);
752829603e2SAlexander Motin 	free(isns);
753829603e2SAlexander Motin }
754829603e2SAlexander Motin 
755829603e2SAlexander Motin static int
756829603e2SAlexander Motin isns_do_connect(struct isns *isns)
757829603e2SAlexander Motin {
758829603e2SAlexander Motin 	int s;
759829603e2SAlexander Motin 
760829603e2SAlexander Motin 	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
761829603e2SAlexander Motin 	    isns->i_ai->ai_protocol);
762829603e2SAlexander Motin 	if (s < 0) {
763829603e2SAlexander Motin 		log_warn("socket(2) failed for %s", isns->i_addr);
764829603e2SAlexander Motin 		return (-1);
765829603e2SAlexander Motin 	}
766829603e2SAlexander Motin 	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
767829603e2SAlexander Motin 		log_warn("connect(2) failed for %s", isns->i_addr);
768829603e2SAlexander Motin 		close(s);
769829603e2SAlexander Motin 		return (-1);
770829603e2SAlexander Motin 	}
771829603e2SAlexander Motin 	return(s);
772829603e2SAlexander Motin }
773829603e2SAlexander Motin 
774829603e2SAlexander Motin static int
775829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname)
776829603e2SAlexander Motin {
777829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
778829603e2SAlexander Motin 	struct target *target;
779829603e2SAlexander Motin 	struct portal *portal;
780829603e2SAlexander Motin 	struct portal_group *pg;
781829603e2SAlexander Motin 	struct isns_req *req;
782829603e2SAlexander Motin 	int res = 0;
783829603e2SAlexander Motin 	uint32_t error;
784829603e2SAlexander Motin 
785829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
786829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
787829603e2SAlexander Motin 	isns_req_add_delim(req);
788829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
789829603e2SAlexander Motin 	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
790829603e2SAlexander Motin 	isns_req_add_32(req, 6, conf->conf_isns_period);
791829603e2SAlexander Motin 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
792829603e2SAlexander Motin 		if (pg->pg_unassigned)
793829603e2SAlexander Motin 			continue;
794829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
795829603e2SAlexander Motin 			isns_req_add_addr(req, 16, portal->p_ai);
796829603e2SAlexander Motin 			isns_req_add_port(req, 17, portal->p_ai);
797829603e2SAlexander Motin 		}
798829603e2SAlexander Motin 	}
799829603e2SAlexander Motin 	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
800829603e2SAlexander Motin 		isns_req_add_str(req, 32, target->t_name);
801829603e2SAlexander Motin 		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
802829603e2SAlexander Motin 		if (target->t_alias != NULL)
803829603e2SAlexander Motin 			isns_req_add_str(req, 34, target->t_alias);
804829603e2SAlexander Motin 		pg = target->t_portal_group;
805829603e2SAlexander Motin 		isns_req_add_32(req, 51, pg->pg_tag);
806829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
807829603e2SAlexander Motin 			isns_req_add_addr(req, 49, portal->p_ai);
808829603e2SAlexander Motin 			isns_req_add_port(req, 50, portal->p_ai);
809829603e2SAlexander Motin 		}
810829603e2SAlexander Motin 	}
811829603e2SAlexander Motin 	res = isns_req_send(s, req);
812829603e2SAlexander Motin 	if (res < 0) {
813829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
814829603e2SAlexander Motin 		goto quit;
815829603e2SAlexander Motin 	}
816829603e2SAlexander Motin 	res = isns_req_receive(s, req);
817829603e2SAlexander Motin 	if (res < 0) {
818829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
819829603e2SAlexander Motin 		goto quit;
820829603e2SAlexander Motin 	}
821829603e2SAlexander Motin 	error = isns_req_get_status(req);
822829603e2SAlexander Motin 	if (error != 0) {
823829603e2SAlexander Motin 		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
824829603e2SAlexander Motin 		res = -1;
825829603e2SAlexander Motin 	}
826829603e2SAlexander Motin quit:
827829603e2SAlexander Motin 	isns_req_free(req);
828829603e2SAlexander Motin 	return (res);
829829603e2SAlexander Motin }
830829603e2SAlexander Motin 
831829603e2SAlexander Motin static int
832829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname)
833829603e2SAlexander Motin {
834829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
835829603e2SAlexander Motin 	struct isns_req *req;
836829603e2SAlexander Motin 	int res = 0;
837829603e2SAlexander Motin 	uint32_t error;
838829603e2SAlexander Motin 
839829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
840829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
841829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
842829603e2SAlexander Motin 	isns_req_add_delim(req);
843829603e2SAlexander Motin 	isns_req_add(req, 2, 0, NULL);
844829603e2SAlexander Motin 	res = isns_req_send(s, req);
845829603e2SAlexander Motin 	if (res < 0) {
846829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
847829603e2SAlexander Motin 		goto quit;
848829603e2SAlexander Motin 	}
849829603e2SAlexander Motin 	res = isns_req_receive(s, req);
850829603e2SAlexander Motin 	if (res < 0) {
851829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
852829603e2SAlexander Motin 		goto quit;
853829603e2SAlexander Motin 	}
854829603e2SAlexander Motin 	error = isns_req_get_status(req);
855829603e2SAlexander Motin 	if (error != 0) {
856829603e2SAlexander Motin 		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
857829603e2SAlexander Motin 		res = -1;
858829603e2SAlexander Motin 	}
859829603e2SAlexander Motin quit:
860829603e2SAlexander Motin 	isns_req_free(req);
861829603e2SAlexander Motin 	return (res);
862829603e2SAlexander Motin }
863829603e2SAlexander Motin 
864829603e2SAlexander Motin static int
865829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname)
866829603e2SAlexander Motin {
867829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
868829603e2SAlexander Motin 	struct isns_req *req;
869829603e2SAlexander Motin 	int res = 0;
870829603e2SAlexander Motin 	uint32_t error;
871829603e2SAlexander Motin 
872829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
873829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
874829603e2SAlexander Motin 	isns_req_add_delim(req);
875829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
876829603e2SAlexander Motin 	res = isns_req_send(s, req);
877829603e2SAlexander Motin 	if (res < 0) {
878829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
879829603e2SAlexander Motin 		goto quit;
880829603e2SAlexander Motin 	}
881829603e2SAlexander Motin 	res = isns_req_receive(s, req);
882829603e2SAlexander Motin 	if (res < 0) {
883829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
884829603e2SAlexander Motin 		goto quit;
885829603e2SAlexander Motin 	}
886829603e2SAlexander Motin 	error = isns_req_get_status(req);
887829603e2SAlexander Motin 	if (error != 0) {
888829603e2SAlexander Motin 		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
889829603e2SAlexander Motin 		res = -1;
890829603e2SAlexander Motin 	}
891829603e2SAlexander Motin quit:
892829603e2SAlexander Motin 	isns_req_free(req);
893829603e2SAlexander Motin 	return (res);
894829603e2SAlexander Motin }
895829603e2SAlexander Motin 
896829603e2SAlexander Motin void
897829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns)
898829603e2SAlexander Motin {
899829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
900d5cc4a4aSAlexander Motin 	int s;
901829603e2SAlexander Motin 	char hostname[256];
902829603e2SAlexander Motin 
903829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
904829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
905829603e2SAlexander Motin 		return;
906829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
907829603e2SAlexander Motin 	s = isns_do_connect(isns);
908829603e2SAlexander Motin 	if (s < 0) {
909829603e2SAlexander Motin 		set_timeout(0, false);
910829603e2SAlexander Motin 		return;
911829603e2SAlexander Motin 	}
912829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
913829603e2SAlexander Motin 
914829603e2SAlexander Motin 	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
915829603e2SAlexander Motin 		oldisns = isns;
916d5cc4a4aSAlexander Motin 	isns_do_deregister(oldisns, s, hostname);
917d5cc4a4aSAlexander Motin 	isns_do_register(isns, s, hostname);
918829603e2SAlexander Motin 	close(s);
919829603e2SAlexander Motin 	set_timeout(0, false);
920829603e2SAlexander Motin }
921829603e2SAlexander Motin 
922829603e2SAlexander Motin void
923829603e2SAlexander Motin isns_check(struct isns *isns)
924829603e2SAlexander Motin {
925829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
926829603e2SAlexander Motin 	int s, res;
927829603e2SAlexander Motin 	char hostname[256];
928829603e2SAlexander Motin 
929829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
930829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
931829603e2SAlexander Motin 		return;
932829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
933829603e2SAlexander Motin 	s = isns_do_connect(isns);
934829603e2SAlexander Motin 	if (s < 0) {
935829603e2SAlexander Motin 		set_timeout(0, false);
936829603e2SAlexander Motin 		return;
937829603e2SAlexander Motin 	}
938829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
939829603e2SAlexander Motin 
940829603e2SAlexander Motin 	res = isns_do_check(isns, s, hostname);
941829603e2SAlexander Motin 	if (res < 0) {
942d5cc4a4aSAlexander Motin 		isns_do_deregister(isns, s, hostname);
943d5cc4a4aSAlexander Motin 		isns_do_register(isns, s, hostname);
944829603e2SAlexander Motin 	}
945829603e2SAlexander Motin 	close(s);
946829603e2SAlexander Motin 	set_timeout(0, false);
947829603e2SAlexander Motin }
948829603e2SAlexander Motin 
949829603e2SAlexander Motin void
950829603e2SAlexander Motin isns_deregister(struct isns *isns)
951829603e2SAlexander Motin {
952829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
953d5cc4a4aSAlexander Motin 	int s;
954829603e2SAlexander Motin 	char hostname[256];
955829603e2SAlexander Motin 
956829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
957829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
958829603e2SAlexander Motin 		return;
959829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
960829603e2SAlexander Motin 	s = isns_do_connect(isns);
961829603e2SAlexander Motin 	if (s < 0)
962829603e2SAlexander Motin 		return;
963829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
964829603e2SAlexander Motin 
965d5cc4a4aSAlexander Motin 	isns_do_deregister(isns, s, hostname);
966829603e2SAlexander Motin 	close(s);
967829603e2SAlexander Motin 	set_timeout(0, false);
968829603e2SAlexander Motin }
969829603e2SAlexander Motin 
97005374883SEdward Tomasz Napierala int
971bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str)
97205374883SEdward Tomasz Napierala {
973bd833b11SEdward Tomasz Napierala 	int filter;
97405374883SEdward Tomasz Napierala 
97505374883SEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
97605374883SEdward Tomasz Napierala 		filter = PG_FILTER_NONE;
97705374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal") == 0) {
97805374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL;
97905374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name") == 0) {
98005374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME;
98105374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name-auth") == 0) {
98205374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME_AUTH;
98305374883SEdward Tomasz Napierala 	} else {
98405374883SEdward Tomasz Napierala 		log_warnx("invalid discovery-filter \"%s\" for portal-group "
98505374883SEdward Tomasz Napierala 		    "\"%s\"; valid values are \"none\", \"portal\", "
98605374883SEdward Tomasz Napierala 		    "\"portal-name\", and \"portal-name-auth\"",
98705374883SEdward Tomasz Napierala 		    str, pg->pg_name);
98805374883SEdward Tomasz Napierala 		return (1);
98905374883SEdward Tomasz Napierala 	}
99005374883SEdward Tomasz Napierala 
991bd833b11SEdward Tomasz Napierala 	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
992bd833b11SEdward Tomasz Napierala 	    pg->pg_discovery_filter != filter) {
99305374883SEdward Tomasz Napierala 		log_warnx("cannot set discovery-filter to \"%s\" for "
99405374883SEdward Tomasz Napierala 		    "portal-group \"%s\"; already has a different "
99505374883SEdward Tomasz Napierala 		    "value", str, pg->pg_name);
99605374883SEdward Tomasz Napierala 		return (1);
99705374883SEdward Tomasz Napierala 	}
99805374883SEdward Tomasz Napierala 
999bd833b11SEdward Tomasz Napierala 	pg->pg_discovery_filter = filter;
1000bd833b11SEdward Tomasz Napierala 
1001bd833b11SEdward Tomasz Napierala 	return (0);
100205374883SEdward Tomasz Napierala }
100305374883SEdward Tomasz Napierala 
10046dbdbf82SEdward Tomasz Napierala int
10056dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr)
10066dbdbf82SEdward Tomasz Napierala {
10076dbdbf82SEdward Tomasz Napierala 
10086dbdbf82SEdward Tomasz Napierala 	if (pg->pg_redirection != NULL) {
10096dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
10106dbdbf82SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
10116dbdbf82SEdward Tomasz Napierala 		    addr, pg->pg_name);
10126dbdbf82SEdward Tomasz Napierala 		return (1);
10136dbdbf82SEdward Tomasz Napierala 	}
10146dbdbf82SEdward Tomasz Napierala 
10156dbdbf82SEdward Tomasz Napierala 	pg->pg_redirection = checked_strdup(addr);
10166dbdbf82SEdward Tomasz Napierala 
10176dbdbf82SEdward Tomasz Napierala 	return (0);
10186dbdbf82SEdward Tomasz Napierala }
10196dbdbf82SEdward Tomasz Napierala 
1020009ea47eSEdward Tomasz Napierala static bool
1021009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
1022009ea47eSEdward Tomasz Napierala {
1023009ea47eSEdward Tomasz Napierala 	switch (ch) {
1024009ea47eSEdward Tomasz Napierala 	case '0':
1025009ea47eSEdward Tomasz Napierala 	case '1':
1026009ea47eSEdward Tomasz Napierala 	case '2':
1027009ea47eSEdward Tomasz Napierala 	case '3':
1028009ea47eSEdward Tomasz Napierala 	case '4':
1029009ea47eSEdward Tomasz Napierala 	case '5':
1030009ea47eSEdward Tomasz Napierala 	case '6':
1031009ea47eSEdward Tomasz Napierala 	case '7':
1032009ea47eSEdward Tomasz Napierala 	case '8':
1033009ea47eSEdward Tomasz Napierala 	case '9':
1034009ea47eSEdward Tomasz Napierala 	case 'a':
1035009ea47eSEdward Tomasz Napierala 	case 'A':
1036009ea47eSEdward Tomasz Napierala 	case 'b':
1037009ea47eSEdward Tomasz Napierala 	case 'B':
1038009ea47eSEdward Tomasz Napierala 	case 'c':
1039009ea47eSEdward Tomasz Napierala 	case 'C':
1040009ea47eSEdward Tomasz Napierala 	case 'd':
1041009ea47eSEdward Tomasz Napierala 	case 'D':
1042009ea47eSEdward Tomasz Napierala 	case 'e':
1043009ea47eSEdward Tomasz Napierala 	case 'E':
1044009ea47eSEdward Tomasz Napierala 	case 'f':
1045009ea47eSEdward Tomasz Napierala 	case 'F':
1046009ea47eSEdward Tomasz Napierala 		return (true);
1047009ea47eSEdward Tomasz Napierala 	default:
1048009ea47eSEdward Tomasz Napierala 		return (false);
1049009ea47eSEdward Tomasz Napierala 	}
1050009ea47eSEdward Tomasz Napierala }
1051009ea47eSEdward Tomasz Napierala 
1052009ea47eSEdward Tomasz Napierala bool
1053009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
1054009ea47eSEdward Tomasz Napierala {
1055009ea47eSEdward Tomasz Napierala 	int i;
1056009ea47eSEdward Tomasz Napierala 
1057009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
1058009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
1059009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
1060009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
1061009ea47eSEdward Tomasz Napierala 		return (false);
1062009ea47eSEdward Tomasz Napierala 	}
1063009ea47eSEdward Tomasz Napierala 
1064009ea47eSEdward Tomasz Napierala 	/*
1065009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
1066009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
1067009ea47eSEdward Tomasz Napierala 	 */
1068009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1069009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1070009ea47eSEdward Tomasz Napierala 			/*
1071009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
1072009ea47eSEdward Tomasz Napierala 			 *      by 3.2.6.2: iSCSI Name Encoding.
1073009ea47eSEdward Tomasz Napierala 			 */
1074009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
1075009ea47eSEdward Tomasz Napierala 				continue;
1076009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1077009ea47eSEdward Tomasz Napierala 				continue;
1078009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
1079009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
1080009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
1081009ea47eSEdward Tomasz Napierala 			break;
1082009ea47eSEdward Tomasz Napierala 		}
1083009ea47eSEdward Tomasz Napierala 		/*
1084009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
1085009ea47eSEdward Tomasz Napierala 		 */
1086009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1087009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
1088009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1089009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
1090009ea47eSEdward Tomasz Napierala 			    "digits", name);
1091009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
1092009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1093009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1094009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1095009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1096009ea47eSEdward Tomasz Napierala 				break;
1097009ea47eSEdward Tomasz Napierala 			}
1098009ea47eSEdward Tomasz Napierala 		}
1099009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1100009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
1101009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1102009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
1103009ea47eSEdward Tomasz Napierala 			    "digits", name);
1104009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
1105009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1106009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1107009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1108009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1109009ea47eSEdward Tomasz Napierala 				break;
1110009ea47eSEdward Tomasz Napierala 			}
1111009ea47eSEdward Tomasz Napierala 		}
1112009ea47eSEdward Tomasz Napierala 	} else {
1113009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
1114009ea47eSEdward Tomasz Napierala 		    "either \".iqn\", \"eui.\", or \"naa.\"",
1115009ea47eSEdward Tomasz Napierala 		    name);
1116009ea47eSEdward Tomasz Napierala 	}
1117009ea47eSEdward Tomasz Napierala 	return (true);
1118009ea47eSEdward Tomasz Napierala }
1119009ea47eSEdward Tomasz Napierala 
1120009ea47eSEdward Tomasz Napierala struct target *
1121f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
1122009ea47eSEdward Tomasz Napierala {
1123009ea47eSEdward Tomasz Napierala 	struct target *targ;
1124009ea47eSEdward Tomasz Napierala 	int i, len;
1125009ea47eSEdward Tomasz Napierala 
1126f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
1127009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
1128f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
1129009ea47eSEdward Tomasz Napierala 		return (NULL);
1130009ea47eSEdward Tomasz Napierala 	}
1131f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
1132f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
1133009ea47eSEdward Tomasz Napierala 		return (NULL);
1134009ea47eSEdward Tomasz Napierala 	}
1135009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
1136009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
1137009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1138f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
1139009ea47eSEdward Tomasz Napierala 
1140009ea47eSEdward Tomasz Napierala 	/*
1141009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
1142009ea47eSEdward Tomasz Napierala 	 */
1143f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
1144009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
1145f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
1146009ea47eSEdward Tomasz Napierala 
1147009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&targ->t_luns);
1148009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
1149009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1150009ea47eSEdward Tomasz Napierala 
1151009ea47eSEdward Tomasz Napierala 	return (targ);
1152009ea47eSEdward Tomasz Napierala }
1153009ea47eSEdward Tomasz Napierala 
1154009ea47eSEdward Tomasz Napierala void
1155009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
1156009ea47eSEdward Tomasz Napierala {
1157009ea47eSEdward Tomasz Napierala 	struct lun *lun, *tmp;
1158009ea47eSEdward Tomasz Napierala 
1159009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1160009ea47eSEdward Tomasz Napierala 
1161009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
1162009ea47eSEdward Tomasz Napierala 		lun_delete(lun);
1163f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
11646dbdbf82SEdward Tomasz Napierala 	free(targ->t_redirection);
1165009ea47eSEdward Tomasz Napierala 	free(targ);
1166009ea47eSEdward Tomasz Napierala }
1167009ea47eSEdward Tomasz Napierala 
1168009ea47eSEdward Tomasz Napierala struct target *
1169f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
1170009ea47eSEdward Tomasz Napierala {
1171009ea47eSEdward Tomasz Napierala 	struct target *targ;
1172009ea47eSEdward Tomasz Napierala 
1173009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1174f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
1175009ea47eSEdward Tomasz Napierala 			return (targ);
1176009ea47eSEdward Tomasz Napierala 	}
1177009ea47eSEdward Tomasz Napierala 
1178009ea47eSEdward Tomasz Napierala 	return (NULL);
1179009ea47eSEdward Tomasz Napierala }
1180009ea47eSEdward Tomasz Napierala 
11816dbdbf82SEdward Tomasz Napierala int
11826dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr)
11836dbdbf82SEdward Tomasz Napierala {
11846dbdbf82SEdward Tomasz Napierala 
11856dbdbf82SEdward Tomasz Napierala 	if (target->t_redirection != NULL) {
11866dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
11876dbdbf82SEdward Tomasz Napierala 		    "target \"%s\"; already defined",
11886dbdbf82SEdward Tomasz Napierala 		    addr, target->t_name);
11896dbdbf82SEdward Tomasz Napierala 		return (1);
11906dbdbf82SEdward Tomasz Napierala 	}
11916dbdbf82SEdward Tomasz Napierala 
11926dbdbf82SEdward Tomasz Napierala 	target->t_redirection = checked_strdup(addr);
11936dbdbf82SEdward Tomasz Napierala 
11946dbdbf82SEdward Tomasz Napierala 	return (0);
11956dbdbf82SEdward Tomasz Napierala }
11966dbdbf82SEdward Tomasz Napierala 
1197009ea47eSEdward Tomasz Napierala struct lun *
1198009ea47eSEdward Tomasz Napierala lun_new(struct target *targ, int lun_id)
1199009ea47eSEdward Tomasz Napierala {
1200009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1201009ea47eSEdward Tomasz Napierala 
1202009ea47eSEdward Tomasz Napierala 	lun = lun_find(targ, lun_id);
1203009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
1204009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun %d for target \"%s\"",
1205f7ae5bf8SEdward Tomasz Napierala 		    lun_id, targ->t_name);
1206009ea47eSEdward Tomasz Napierala 		return (NULL);
1207009ea47eSEdward Tomasz Napierala 	}
1208009ea47eSEdward Tomasz Napierala 
1209009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
1210009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
1211009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1212009ea47eSEdward Tomasz Napierala 	lun->l_lun = lun_id;
1213009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
1214009ea47eSEdward Tomasz Napierala 	lun->l_target = targ;
1215009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
1216009ea47eSEdward Tomasz Napierala 
1217009ea47eSEdward Tomasz Napierala 	return (lun);
1218009ea47eSEdward Tomasz Napierala }
1219009ea47eSEdward Tomasz Napierala 
1220009ea47eSEdward Tomasz Napierala void
1221009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
1222009ea47eSEdward Tomasz Napierala {
1223009ea47eSEdward Tomasz Napierala 	struct lun_option *lo, *tmp;
1224009ea47eSEdward Tomasz Napierala 
1225009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
1226009ea47eSEdward Tomasz Napierala 
1227009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
1228009ea47eSEdward Tomasz Napierala 		lun_option_delete(lo);
1229009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1230009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1231009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1232009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1233009ea47eSEdward Tomasz Napierala 	free(lun);
1234009ea47eSEdward Tomasz Napierala }
1235009ea47eSEdward Tomasz Napierala 
1236009ea47eSEdward Tomasz Napierala struct lun *
1237d6093026SEdward Tomasz Napierala lun_find(const struct target *targ, int lun_id)
1238009ea47eSEdward Tomasz Napierala {
1239009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1240009ea47eSEdward Tomasz Napierala 
1241009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1242009ea47eSEdward Tomasz Napierala 		if (lun->l_lun == lun_id)
1243009ea47eSEdward Tomasz Napierala 			return (lun);
1244009ea47eSEdward Tomasz Napierala 	}
1245009ea47eSEdward Tomasz Napierala 
1246009ea47eSEdward Tomasz Napierala 	return (NULL);
1247009ea47eSEdward Tomasz Napierala }
1248009ea47eSEdward Tomasz Napierala 
1249009ea47eSEdward Tomasz Napierala void
1250009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
1251009ea47eSEdward Tomasz Napierala {
1252009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1253009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
1254009ea47eSEdward Tomasz Napierala }
1255009ea47eSEdward Tomasz Napierala 
1256009ea47eSEdward Tomasz Napierala void
1257009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
1258009ea47eSEdward Tomasz Napierala {
1259009ea47eSEdward Tomasz Napierala 
1260009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
1261009ea47eSEdward Tomasz Napierala }
1262009ea47eSEdward Tomasz Napierala 
1263009ea47eSEdward Tomasz Napierala void
1264009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
1265009ea47eSEdward Tomasz Napierala {
1266009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1267009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
1268009ea47eSEdward Tomasz Napierala }
1269009ea47eSEdward Tomasz Napierala 
1270009ea47eSEdward Tomasz Napierala void
1271009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
1272009ea47eSEdward Tomasz Napierala {
1273009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1274009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
1275009ea47eSEdward Tomasz Napierala }
1276009ea47eSEdward Tomasz Napierala 
1277009ea47eSEdward Tomasz Napierala void
1278009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
1279009ea47eSEdward Tomasz Napierala {
1280009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1281009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
1282009ea47eSEdward Tomasz Napierala }
1283009ea47eSEdward Tomasz Napierala 
1284009ea47eSEdward Tomasz Napierala void
1285009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
1286009ea47eSEdward Tomasz Napierala {
1287009ea47eSEdward Tomasz Napierala 
1288009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
1289009ea47eSEdward Tomasz Napierala }
1290009ea47eSEdward Tomasz Napierala 
1291009ea47eSEdward Tomasz Napierala void
1292009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
1293009ea47eSEdward Tomasz Napierala {
1294009ea47eSEdward Tomasz Napierala 
1295009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
1296009ea47eSEdward Tomasz Napierala }
1297009ea47eSEdward Tomasz Napierala 
1298009ea47eSEdward Tomasz Napierala struct lun_option *
1299009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value)
1300009ea47eSEdward Tomasz Napierala {
1301009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1302009ea47eSEdward Tomasz Napierala 
1303009ea47eSEdward Tomasz Napierala 	lo = lun_option_find(lun, name);
1304009ea47eSEdward Tomasz Napierala 	if (lo != NULL) {
1305009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
1306f7ae5bf8SEdward Tomasz Napierala 		    name, lun->l_lun, lun->l_target->t_name);
1307009ea47eSEdward Tomasz Napierala 		return (NULL);
1308009ea47eSEdward Tomasz Napierala 	}
1309009ea47eSEdward Tomasz Napierala 
1310009ea47eSEdward Tomasz Napierala 	lo = calloc(1, sizeof(*lo));
1311009ea47eSEdward Tomasz Napierala 	if (lo == NULL)
1312009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1313009ea47eSEdward Tomasz Napierala 	lo->lo_name = checked_strdup(name);
1314009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
1315009ea47eSEdward Tomasz Napierala 	lo->lo_lun = lun;
1316009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
1317009ea47eSEdward Tomasz Napierala 
1318009ea47eSEdward Tomasz Napierala 	return (lo);
1319009ea47eSEdward Tomasz Napierala }
1320009ea47eSEdward Tomasz Napierala 
1321009ea47eSEdward Tomasz Napierala void
1322009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo)
1323009ea47eSEdward Tomasz Napierala {
1324009ea47eSEdward Tomasz Napierala 
1325009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
1326009ea47eSEdward Tomasz Napierala 
1327009ea47eSEdward Tomasz Napierala 	free(lo->lo_name);
1328009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
1329009ea47eSEdward Tomasz Napierala 	free(lo);
1330009ea47eSEdward Tomasz Napierala }
1331009ea47eSEdward Tomasz Napierala 
1332009ea47eSEdward Tomasz Napierala struct lun_option *
1333d6093026SEdward Tomasz Napierala lun_option_find(const struct lun *lun, const char *name)
1334009ea47eSEdward Tomasz Napierala {
1335009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1336009ea47eSEdward Tomasz Napierala 
1337009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
1338009ea47eSEdward Tomasz Napierala 		if (strcmp(lo->lo_name, name) == 0)
1339009ea47eSEdward Tomasz Napierala 			return (lo);
1340009ea47eSEdward Tomasz Napierala 	}
1341009ea47eSEdward Tomasz Napierala 
1342009ea47eSEdward Tomasz Napierala 	return (NULL);
1343009ea47eSEdward Tomasz Napierala }
1344009ea47eSEdward Tomasz Napierala 
1345009ea47eSEdward Tomasz Napierala void
1346009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value)
1347009ea47eSEdward Tomasz Napierala {
1348009ea47eSEdward Tomasz Napierala 
1349009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
1350009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
1351009ea47eSEdward Tomasz Napierala }
1352009ea47eSEdward Tomasz Napierala 
1353009ea47eSEdward Tomasz Napierala static struct connection *
1354073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host,
1355073edb1cSAlexander Motin     const struct sockaddr *client_sa)
1356009ea47eSEdward Tomasz Napierala {
1357009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1358009ea47eSEdward Tomasz Napierala 
1359009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
1360009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
1361009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1362009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
1363009ea47eSEdward Tomasz Napierala 	conn->conn_socket = fd;
1364009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
1365073edb1cSAlexander Motin 	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1366009ea47eSEdward Tomasz Napierala 
1367009ea47eSEdward Tomasz Napierala 	/*
1368009ea47eSEdward Tomasz Napierala 	 * Default values, from RFC 3720, section 12.
1369009ea47eSEdward Tomasz Napierala 	 */
1370009ea47eSEdward Tomasz Napierala 	conn->conn_max_data_segment_length = 8192;
1371009ea47eSEdward Tomasz Napierala 	conn->conn_max_burst_length = 262144;
1372009ea47eSEdward Tomasz Napierala 	conn->conn_immediate_data = true;
1373009ea47eSEdward Tomasz Napierala 
1374009ea47eSEdward Tomasz Napierala 	return (conn);
1375009ea47eSEdward Tomasz Napierala }
1376009ea47eSEdward Tomasz Napierala 
1377009ea47eSEdward Tomasz Napierala #if 0
1378009ea47eSEdward Tomasz Napierala static void
1379009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
1380009ea47eSEdward Tomasz Napierala {
1381009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1382009ea47eSEdward Tomasz Napierala 	struct auth *auth;
13838cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
13848cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
1385009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1386009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1387009ea47eSEdward Tomasz Napierala 	struct target *targ;
1388009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1389009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1390009ea47eSEdward Tomasz Napierala 
1391009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1392009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1393009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1394009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1395009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
1396009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
13978cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
13988cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
13998cb2e958SEdward Tomasz Napierala 			    auth_name->an_initator_name);
14008cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
14018cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
14028cb2e958SEdward Tomasz Napierala 			    auth_portal->an_initator_portal);
1403009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1404009ea47eSEdward Tomasz Napierala 	}
1405009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1406009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1407009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1408009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1409009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1410009ea47eSEdward Tomasz Napierala 	}
1411009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1412f7ae5bf8SEdward Tomasz Napierala 		fprintf(stderr, "target %s {\n", targ->t_name);
1413009ea47eSEdward Tomasz Napierala 		if (targ->t_alias != NULL)
1414009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1415009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1416009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1417009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1418009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1419009ea47eSEdward Tomasz Napierala 				fprintf(stderr, "\t\toption %s %s\n",
1420009ea47eSEdward Tomasz Napierala 				    lo->lo_name, lo->lo_value);
1421009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t}\n");
1422009ea47eSEdward Tomasz Napierala 		}
1423009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1424009ea47eSEdward Tomasz Napierala 	}
1425009ea47eSEdward Tomasz Napierala }
1426009ea47eSEdward Tomasz Napierala #endif
1427009ea47eSEdward Tomasz Napierala 
14282fabfaa5SEdward Tomasz Napierala static int
14292fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
14302fabfaa5SEdward Tomasz Napierala {
14312fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
1432a19eebb1SEdward Tomasz Napierala 	const struct target *targ2;
14332fabfaa5SEdward Tomasz Napierala 
14342fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
14352fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
14362fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
14372fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
14382fabfaa5SEdward Tomasz Napierala 			log_warnx("missing path for lun %d, target \"%s\"",
1439f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
14402fabfaa5SEdward Tomasz Napierala 			return (1);
14412fabfaa5SEdward Tomasz Napierala 		}
14422fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
14432fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
14442fabfaa5SEdward Tomasz Napierala 			log_warnx("missing size for ramdisk-backed lun %d, "
1445f7ae5bf8SEdward Tomasz Napierala 			    "target \"%s\"", lun->l_lun, lun->l_target->t_name);
14462fabfaa5SEdward Tomasz Napierala 			return (1);
14472fabfaa5SEdward Tomasz Napierala 		}
14482fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
14492fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
14502fabfaa5SEdward Tomasz Napierala 			    "for ramdisk-backed lun %d, target \"%s\"",
1451f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
14522fabfaa5SEdward Tomasz Napierala 			return (1);
14532fabfaa5SEdward Tomasz Napierala 		}
14542fabfaa5SEdward Tomasz Napierala 	}
14552fabfaa5SEdward Tomasz Napierala 	if (lun->l_lun < 0 || lun->l_lun > 255) {
14562fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid lun number for lun %d, target \"%s\"; "
14572fabfaa5SEdward Tomasz Napierala 		    "must be between 0 and 255", lun->l_lun,
1458f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
14592fabfaa5SEdward Tomasz Napierala 		return (1);
14602fabfaa5SEdward Tomasz Napierala 	}
14612fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
14622fabfaa5SEdward Tomasz Napierala 		lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
14632fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
14642fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1465f7ae5bf8SEdward Tomasz Napierala 		    "must be larger than 0", lun->l_lun, lun->l_target->t_name);
14662fabfaa5SEdward Tomasz Napierala 		return (1);
14672fabfaa5SEdward Tomasz Napierala 	}
14682fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
14692fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid size for lun %d, target \"%s\"; "
14702fabfaa5SEdward Tomasz Napierala 		    "must be multiple of blocksize", lun->l_lun,
1471f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
14722fabfaa5SEdward Tomasz Napierala 		return (1);
14732fabfaa5SEdward Tomasz Napierala 	}
1474a19eebb1SEdward Tomasz Napierala 	TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1475a19eebb1SEdward Tomasz Napierala 		TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1476a19eebb1SEdward Tomasz Napierala 			if (lun == lun2)
1477a19eebb1SEdward Tomasz Napierala 				continue;
1478a19eebb1SEdward Tomasz Napierala 			if (lun->l_path != NULL && lun2->l_path != NULL &&
1479a19eebb1SEdward Tomasz Napierala 			    strcmp(lun->l_path, lun2->l_path) == 0) {
1480a19eebb1SEdward Tomasz Napierala 				log_debugx("WARNING: path \"%s\" duplicated "
1481a19eebb1SEdward Tomasz Napierala 				    "between lun %d, target \"%s\", and "
1482a19eebb1SEdward Tomasz Napierala 				    "lun %d, target \"%s\"", lun->l_path,
1483f7ae5bf8SEdward Tomasz Napierala 				    lun->l_lun, lun->l_target->t_name,
1484f7ae5bf8SEdward Tomasz Napierala 				    lun2->l_lun, lun2->l_target->t_name);
1485a19eebb1SEdward Tomasz Napierala 			}
1486a19eebb1SEdward Tomasz Napierala 		}
1487a19eebb1SEdward Tomasz Napierala 	}
14882fabfaa5SEdward Tomasz Napierala 
14892fabfaa5SEdward Tomasz Napierala 	return (0);
14902fabfaa5SEdward Tomasz Napierala }
14912fabfaa5SEdward Tomasz Napierala 
1492009ea47eSEdward Tomasz Napierala int
1493009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1494009ea47eSEdward Tomasz Napierala {
1495009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1496009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1497009ea47eSEdward Tomasz Napierala 	struct target *targ;
14982fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
14994e5c38f8SEdward Tomasz Napierala 	bool found;
15002fabfaa5SEdward Tomasz Napierala 	int error;
1501009ea47eSEdward Tomasz Napierala 
1502009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1503009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1504009ea47eSEdward Tomasz Napierala 
1505009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1506009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1507e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1508e7977549SEdward Tomasz Napierala 			    "default");
1509e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1510009ea47eSEdward Tomasz Napierala 		}
1511009ea47eSEdward Tomasz Napierala 		if (targ->t_portal_group == NULL) {
1512009ea47eSEdward Tomasz Napierala 			targ->t_portal_group = portal_group_find(conf,
1513009ea47eSEdward Tomasz Napierala 			    "default");
1514009ea47eSEdward Tomasz Napierala 			assert(targ->t_portal_group != NULL);
1515009ea47eSEdward Tomasz Napierala 		}
15164e5c38f8SEdward Tomasz Napierala 		found = false;
1517009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
15182fabfaa5SEdward Tomasz Napierala 			error = conf_verify_lun(lun);
15192fabfaa5SEdward Tomasz Napierala 			if (error != 0)
15202fabfaa5SEdward Tomasz Napierala 				return (error);
15214e5c38f8SEdward Tomasz Napierala 			found = true;
1522009ea47eSEdward Tomasz Napierala 		}
15236dbdbf82SEdward Tomasz Napierala 		if (!found && targ->t_redirection == NULL) {
152419b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
152519b59f13SEdward Tomasz Napierala 			    targ->t_name);
1526009ea47eSEdward Tomasz Napierala 		}
15276dbdbf82SEdward Tomasz Napierala 		if (found && targ->t_redirection != NULL) {
15286dbdbf82SEdward Tomasz Napierala 			log_debugx("target \"%s\" contains luns, "
15296dbdbf82SEdward Tomasz Napierala 			    " but configured for redirection",
15306dbdbf82SEdward Tomasz Napierala 			    targ->t_name);
15316dbdbf82SEdward Tomasz Napierala 		}
1532009ea47eSEdward Tomasz Napierala 	}
1533009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1534009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1535009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1536009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1537ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1538009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1539009ea47eSEdward Tomasz Napierala 		}
1540009ea47eSEdward Tomasz Napierala 
154105374883SEdward Tomasz Napierala 		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
154205374883SEdward Tomasz Napierala 			pg->pg_discovery_filter = PG_FILTER_NONE;
154305374883SEdward Tomasz Napierala 
1544009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1545009ea47eSEdward Tomasz Napierala 			if (targ->t_portal_group == pg)
1546009ea47eSEdward Tomasz Napierala 				break;
1547009ea47eSEdward Tomasz Napierala 		}
15486dbdbf82SEdward Tomasz Napierala 		if (pg->pg_redirection != NULL) {
15496dbdbf82SEdward Tomasz Napierala 			if (targ != NULL) {
15506dbdbf82SEdward Tomasz Napierala 				log_debugx("portal-group \"%s\" assigned "
15516dbdbf82SEdward Tomasz Napierala 				    "to target \"%s\", but configured "
15526dbdbf82SEdward Tomasz Napierala 				    "for redirection",
15536dbdbf82SEdward Tomasz Napierala 				    pg->pg_name, targ->t_name);
15546dbdbf82SEdward Tomasz Napierala 			}
15556dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
15566dbdbf82SEdward Tomasz Napierala 		} else if (targ != NULL) {
15576dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
15586dbdbf82SEdward Tomasz Napierala 		} else {
1559009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1560009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1561009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1562009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
15636dbdbf82SEdward Tomasz Napierala 		}
1564009ea47eSEdward Tomasz Napierala 	}
1565009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1566009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1567009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1568009ea47eSEdward Tomasz Napierala 		else
1569009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1570009ea47eSEdward Tomasz Napierala 
15714e5c38f8SEdward Tomasz Napierala 		found = false;
1572009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
15734e5c38f8SEdward Tomasz Napierala 			if (targ->t_auth_group == ag) {
15744e5c38f8SEdward Tomasz Napierala 				found = true;
1575009ea47eSEdward Tomasz Napierala 				break;
1576009ea47eSEdward Tomasz Napierala 			}
15774e5c38f8SEdward Tomasz Napierala 		}
15784e5c38f8SEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
15794e5c38f8SEdward Tomasz Napierala 			if (pg->pg_discovery_auth_group == ag) {
15804e5c38f8SEdward Tomasz Napierala 				found = true;
15814e5c38f8SEdward Tomasz Napierala 				break;
15824e5c38f8SEdward Tomasz Napierala 			}
15834e5c38f8SEdward Tomasz Napierala 		}
15844e5c38f8SEdward Tomasz Napierala 		if (!found && ag->ag_name != NULL &&
1585ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1586009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1587009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1588009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1589009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1590009ea47eSEdward Tomasz Napierala 		}
1591009ea47eSEdward Tomasz Napierala 	}
1592009ea47eSEdward Tomasz Napierala 
1593009ea47eSEdward Tomasz Napierala 	return (0);
1594009ea47eSEdward Tomasz Napierala }
1595009ea47eSEdward Tomasz Napierala 
1596009ea47eSEdward Tomasz Napierala static int
1597009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1598009ea47eSEdward Tomasz Napierala {
1599009ea47eSEdward Tomasz Napierala 	struct target *oldtarg, *newtarg, *tmptarg;
1600009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1601009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1602009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
1603829603e2SAlexander Motin 	struct isns *oldns, *newns;
1604009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
1605009ea47eSEdward Tomasz Napierala 	int changed, cumulated_error = 0, error;
1606009ea47eSEdward Tomasz Napierala 	int one = 1;
1607009ea47eSEdward Tomasz Napierala 
1608009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1609009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1610009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1611009ea47eSEdward Tomasz Napierala 	}
1612009ea47eSEdward Tomasz Napierala 
1613009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1614009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1615009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1616009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1617009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1618009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1619009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1620009ea47eSEdward Tomasz Napierala 		} else {
1621009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1622009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1623009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1624009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1625009ea47eSEdward Tomasz Napierala 		}
1626009ea47eSEdward Tomasz Napierala 	}
1627009ea47eSEdward Tomasz Napierala 
1628009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1629009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1630009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1631009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1632009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1633009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1634009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1635009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1636009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1637009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1638009ea47eSEdward Tomasz Napierala 		}
1639009ea47eSEdward Tomasz Napierala 	}
1640009ea47eSEdward Tomasz Napierala 
1641829603e2SAlexander Motin 	/* Deregister on removed iSNS servers. */
1642829603e2SAlexander Motin 	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1643829603e2SAlexander Motin 		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1644829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1645829603e2SAlexander Motin 				break;
1646829603e2SAlexander Motin 		}
1647829603e2SAlexander Motin 		if (newns == NULL)
1648829603e2SAlexander Motin 			isns_deregister(oldns);
1649829603e2SAlexander Motin 	}
1650829603e2SAlexander Motin 
1651d5e316e5SEdward Tomasz Napierala 	/*
1652d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1653d5e316e5SEdward Tomasz Napierala 	 *      the old lun or target into newconf, so that subsequent
165461a2a354SEdward Tomasz Napierala 	 *      conf_apply() would try to remove them again.  That would
165561a2a354SEdward Tomasz Napierala 	 *      be somewhat hairy, though, and lun deletion failures don't
165661a2a354SEdward Tomasz Napierala 	 *      really happen, so leave it as it is for now.
1657d5e316e5SEdward Tomasz Napierala 	 */
1658009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1659009ea47eSEdward Tomasz Napierala 		/*
1660009ea47eSEdward Tomasz Napierala 		 * First, remove any targets present in the old configuration
1661009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1662009ea47eSEdward Tomasz Napierala 		 */
1663f7ae5bf8SEdward Tomasz Napierala 		newtarg = target_find(newconf, oldtarg->t_name);
1664009ea47eSEdward Tomasz Napierala 		if (newtarg == NULL) {
1665*6de78f9fSAlexander Motin 			error = kernel_port_remove(oldtarg);
1666*6de78f9fSAlexander Motin 			if (error != 0) {
1667*6de78f9fSAlexander Motin 				log_warnx("failed to remove target %s",
1668*6de78f9fSAlexander Motin 				    oldtarg->t_name);
1669*6de78f9fSAlexander Motin 				/*
1670*6de78f9fSAlexander Motin 				 * XXX: Uncomment after fixing the root cause.
1671*6de78f9fSAlexander Motin 				 *
1672*6de78f9fSAlexander Motin 				 * cumulated_error++;
1673*6de78f9fSAlexander Motin 				 */
1674*6de78f9fSAlexander Motin 			}
1675009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1676009ea47eSEdward Tomasz Napierala 			    tmplun) {
1677cadf3831SEdward Tomasz Napierala 				log_debugx("target %s not found in new "
1678cadf3831SEdward Tomasz Napierala 				    "configuration; removing its lun %d, "
1679009ea47eSEdward Tomasz Napierala 				    "backed by CTL lun %d",
1680f7ae5bf8SEdward Tomasz Napierala 				    oldtarg->t_name, oldlun->l_lun,
1681009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1682009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1683009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1684009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1685009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1686f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1687009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1688009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1689009ea47eSEdward Tomasz Napierala 				}
1690009ea47eSEdward Tomasz Napierala 			}
1691009ea47eSEdward Tomasz Napierala 			continue;
1692009ea47eSEdward Tomasz Napierala 		}
1693009ea47eSEdward Tomasz Napierala 
1694009ea47eSEdward Tomasz Napierala 		/*
1695009ea47eSEdward Tomasz Napierala 		 * Second, remove any LUNs present in the old target
1696009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1697009ea47eSEdward Tomasz Napierala 		 */
1698009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1699009ea47eSEdward Tomasz Napierala 			newlun = lun_find(newtarg, oldlun->l_lun);
1700009ea47eSEdward Tomasz Napierala 			if (newlun == NULL) {
1701009ea47eSEdward Tomasz Napierala 				log_debugx("lun %d, target %s, CTL lun %d "
1702cadf3831SEdward Tomasz Napierala 				    "not found in new configuration; "
1703f7ae5bf8SEdward Tomasz Napierala 				    "removing", oldlun->l_lun, oldtarg->t_name,
1704009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1705009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1706009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1707009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1708009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1709f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1710009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1711009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1712009ea47eSEdward Tomasz Napierala 				}
1713009ea47eSEdward Tomasz Napierala 				continue;
1714009ea47eSEdward Tomasz Napierala 			}
1715009ea47eSEdward Tomasz Napierala 
1716009ea47eSEdward Tomasz Napierala 			/*
1717009ea47eSEdward Tomasz Napierala 			 * Also remove the LUNs changed by more than size.
1718009ea47eSEdward Tomasz Napierala 			 */
1719009ea47eSEdward Tomasz Napierala 			changed = 0;
1720009ea47eSEdward Tomasz Napierala 			assert(oldlun->l_backend != NULL);
1721009ea47eSEdward Tomasz Napierala 			assert(newlun->l_backend != NULL);
1722009ea47eSEdward Tomasz Napierala 			if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1723009ea47eSEdward Tomasz Napierala 				log_debugx("backend for lun %d, target %s, "
1724009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1725f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1726009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1727009ea47eSEdward Tomasz Napierala 				changed = 1;
1728009ea47eSEdward Tomasz Napierala 			}
1729009ea47eSEdward Tomasz Napierala 			if (oldlun->l_blocksize != newlun->l_blocksize) {
1730009ea47eSEdward Tomasz Napierala 				log_debugx("blocksize for lun %d, target %s, "
1731009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1732f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1733009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1734009ea47eSEdward Tomasz Napierala 				changed = 1;
1735009ea47eSEdward Tomasz Napierala 			}
1736009ea47eSEdward Tomasz Napierala 			if (newlun->l_device_id != NULL &&
1737009ea47eSEdward Tomasz Napierala 			    (oldlun->l_device_id == NULL ||
1738009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1739009ea47eSEdward Tomasz Napierala 			     0)) {
1740009ea47eSEdward Tomasz Napierala 				log_debugx("device-id for lun %d, target %s, "
1741009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1742f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1743009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1744009ea47eSEdward Tomasz Napierala 				changed = 1;
1745009ea47eSEdward Tomasz Napierala 			}
1746009ea47eSEdward Tomasz Napierala 			if (newlun->l_path != NULL &&
1747009ea47eSEdward Tomasz Napierala 			    (oldlun->l_path == NULL ||
1748009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1749009ea47eSEdward Tomasz Napierala 				log_debugx("path for lun %d, target %s, "
1750009ea47eSEdward Tomasz Napierala 				    "CTL lun %d, changed; removing",
1751f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1752009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1753009ea47eSEdward Tomasz Napierala 				changed = 1;
1754009ea47eSEdward Tomasz Napierala 			}
1755009ea47eSEdward Tomasz Napierala 			if (newlun->l_serial != NULL &&
1756009ea47eSEdward Tomasz Napierala 			    (oldlun->l_serial == NULL ||
1757009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1758009ea47eSEdward Tomasz Napierala 				log_debugx("serial for lun %d, target %s, "
1759009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1760f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1761009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1762009ea47eSEdward Tomasz Napierala 				changed = 1;
1763009ea47eSEdward Tomasz Napierala 			}
1764009ea47eSEdward Tomasz Napierala 			if (changed) {
1765009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1766009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1767009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1768009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1769f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1770009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1771009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1772009ea47eSEdward Tomasz Napierala 				}
1773009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1774009ea47eSEdward Tomasz Napierala 				continue;
1775009ea47eSEdward Tomasz Napierala 			}
1776009ea47eSEdward Tomasz Napierala 
1777009ea47eSEdward Tomasz Napierala 			lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1778009ea47eSEdward Tomasz Napierala 		}
1779009ea47eSEdward Tomasz Napierala 	}
1780009ea47eSEdward Tomasz Napierala 
1781009ea47eSEdward Tomasz Napierala 	/*
1782009ea47eSEdward Tomasz Napierala 	 * Now add new targets or modify existing ones.
1783009ea47eSEdward Tomasz Napierala 	 */
1784009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1785f7ae5bf8SEdward Tomasz Napierala 		oldtarg = target_find(oldconf, newtarg->t_name);
1786009ea47eSEdward Tomasz Napierala 
1787d5e316e5SEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(newlun, &newtarg->t_luns, l_next, tmplun) {
1788009ea47eSEdward Tomasz Napierala 			if (oldtarg != NULL) {
1789009ea47eSEdward Tomasz Napierala 				oldlun = lun_find(oldtarg, newlun->l_lun);
1790009ea47eSEdward Tomasz Napierala 				if (oldlun != NULL) {
17918cf98331SAlexander Motin 					if (newlun->l_size != oldlun->l_size ||
17928cf98331SAlexander Motin 					    newlun->l_size == 0) {
1793009ea47eSEdward Tomasz Napierala 						log_debugx("resizing lun %d, "
1794009ea47eSEdward Tomasz Napierala 						    "target %s, CTL lun %d",
1795009ea47eSEdward Tomasz Napierala 						    newlun->l_lun,
1796f7ae5bf8SEdward Tomasz Napierala 						    newtarg->t_name,
1797009ea47eSEdward Tomasz Napierala 						    newlun->l_ctl_lun);
1798009ea47eSEdward Tomasz Napierala 						error =
1799009ea47eSEdward Tomasz Napierala 						    kernel_lun_resize(newlun);
1800009ea47eSEdward Tomasz Napierala 						if (error != 0) {
1801009ea47eSEdward Tomasz Napierala 							log_warnx("failed to "
1802009ea47eSEdward Tomasz Napierala 							    "resize lun %d, "
1803009ea47eSEdward Tomasz Napierala 							    "target %s, "
1804009ea47eSEdward Tomasz Napierala 							    "CTL lun %d",
1805009ea47eSEdward Tomasz Napierala 							    newlun->l_lun,
1806f7ae5bf8SEdward Tomasz Napierala 							    newtarg->t_name,
1807009ea47eSEdward Tomasz Napierala 							    newlun->l_lun);
1808009ea47eSEdward Tomasz Napierala 							cumulated_error++;
1809009ea47eSEdward Tomasz Napierala 						}
1810009ea47eSEdward Tomasz Napierala 					}
1811009ea47eSEdward Tomasz Napierala 					continue;
1812009ea47eSEdward Tomasz Napierala 				}
1813009ea47eSEdward Tomasz Napierala 			}
1814009ea47eSEdward Tomasz Napierala 			log_debugx("adding lun %d, target %s",
1815f7ae5bf8SEdward Tomasz Napierala 			    newlun->l_lun, newtarg->t_name);
1816009ea47eSEdward Tomasz Napierala 			error = kernel_lun_add(newlun);
1817009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1818009ea47eSEdward Tomasz Napierala 				log_warnx("failed to add lun %d, target %s",
1819f7ae5bf8SEdward Tomasz Napierala 				    newlun->l_lun, newtarg->t_name);
1820d5e316e5SEdward Tomasz Napierala 				lun_delete(newlun);
1821009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1822009ea47eSEdward Tomasz Napierala 			}
1823009ea47eSEdward Tomasz Napierala 		}
1824e48bb73aSEdward Tomasz Napierala 		if (oldtarg == NULL) {
1825e48bb73aSEdward Tomasz Napierala 			error = kernel_port_add(newtarg);
1826e48bb73aSEdward Tomasz Napierala 			if (error != 0) {
1827e48bb73aSEdward Tomasz Napierala 				log_warnx("failed to add target %s",
1828e48bb73aSEdward Tomasz Napierala 				    oldtarg->t_name);
1829e48bb73aSEdward Tomasz Napierala 				/*
1830e48bb73aSEdward Tomasz Napierala 				 * XXX: Uncomment after fixing the root cause.
1831e48bb73aSEdward Tomasz Napierala 				 *
1832e48bb73aSEdward Tomasz Napierala 				 * cumulated_error++;
1833e48bb73aSEdward Tomasz Napierala 				 */
1834e48bb73aSEdward Tomasz Napierala 			}
1835e48bb73aSEdward Tomasz Napierala 		}
1836009ea47eSEdward Tomasz Napierala 	}
1837009ea47eSEdward Tomasz Napierala 
1838009ea47eSEdward Tomasz Napierala 	/*
1839009ea47eSEdward Tomasz Napierala 	 * Go through the new portals, opening the sockets as neccessary.
1840009ea47eSEdward Tomasz Napierala 	 */
1841009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1842009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
1843009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
1844009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
1845009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
1846009ea47eSEdward Tomasz Napierala 			continue;
1847009ea47eSEdward Tomasz Napierala 		}
1848009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1849009ea47eSEdward Tomasz Napierala 			/*
1850009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
1851009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
1852009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
1853009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
1854009ea47eSEdward Tomasz Napierala 			 */
1855009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1856009ea47eSEdward Tomasz Napierala 			    pg_next) {
1857009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1858009ea47eSEdward Tomasz Napierala 				    p_next) {
1859009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
1860009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
1861009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
1862009ea47eSEdward Tomasz Napierala 						newp->p_socket =
1863009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
1864009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
1865009ea47eSEdward Tomasz Napierala 						break;
1866009ea47eSEdward Tomasz Napierala 					}
1867009ea47eSEdward Tomasz Napierala 				}
1868009ea47eSEdward Tomasz Napierala 			}
1869009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
1870009ea47eSEdward Tomasz Napierala 				/*
1871009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
1872009ea47eSEdward Tomasz Napierala 				 */
1873009ea47eSEdward Tomasz Napierala 				continue;
1874009ea47eSEdward Tomasz Napierala 			}
1875009ea47eSEdward Tomasz Napierala 
1876009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1877ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
18788cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
18798cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
18808cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
18818cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
18828cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
18838cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
18848cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
1885ba3a2d31SEdward Tomasz Napierala 				continue;
1886ba3a2d31SEdward Tomasz Napierala 			}
1887ba3a2d31SEdward Tomasz Napierala #endif
1888ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
1889009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
1890009ea47eSEdward Tomasz Napierala 
1891009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
1892009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
1893009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
1894009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
1895009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
1896009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
1897009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
1898009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1899009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1900009ea47eSEdward Tomasz Napierala 				continue;
1901009ea47eSEdward Tomasz Napierala 			}
1902009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
1903009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
1904009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1905009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
1906009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
1907009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1908009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1909009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1910009ea47eSEdward Tomasz Napierala 				continue;
1911009ea47eSEdward Tomasz Napierala 			}
1912009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
1913009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
1914009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1915009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
1916009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1917009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1918009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1919009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1920009ea47eSEdward Tomasz Napierala 				continue;
1921009ea47eSEdward Tomasz Napierala 			}
1922009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
1923009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1924009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
1925009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1926009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1927009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1928009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1929009ea47eSEdward Tomasz Napierala 				continue;
1930009ea47eSEdward Tomasz Napierala 			}
1931009ea47eSEdward Tomasz Napierala 		}
1932009ea47eSEdward Tomasz Napierala 	}
1933009ea47eSEdward Tomasz Napierala 
1934009ea47eSEdward Tomasz Napierala 	/*
1935009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
1936009ea47eSEdward Tomasz Napierala 	 */
1937009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1938009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1939009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
1940009ea47eSEdward Tomasz Napierala 				continue;
1941009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
1942009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
1943009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
1944009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
1945009ea47eSEdward Tomasz Napierala 		}
1946009ea47eSEdward Tomasz Napierala 	}
1947009ea47eSEdward Tomasz Napierala 
1948829603e2SAlexander Motin 	/* (Re-)Register on remaining/new iSNS servers. */
1949829603e2SAlexander Motin 	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1950829603e2SAlexander Motin 		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1951829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1952829603e2SAlexander Motin 				break;
1953829603e2SAlexander Motin 		}
1954829603e2SAlexander Motin 		isns_register(newns, oldns);
1955829603e2SAlexander Motin 	}
1956829603e2SAlexander Motin 
1957829603e2SAlexander Motin 	/* Schedule iSNS update */
1958829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
1959829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
1960829603e2SAlexander Motin 
1961009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
1962009ea47eSEdward Tomasz Napierala }
1963009ea47eSEdward Tomasz Napierala 
1964009ea47eSEdward Tomasz Napierala bool
1965009ea47eSEdward Tomasz Napierala timed_out(void)
1966009ea47eSEdward Tomasz Napierala {
1967009ea47eSEdward Tomasz Napierala 
1968009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
1969009ea47eSEdward Tomasz Napierala }
1970009ea47eSEdward Tomasz Napierala 
1971009ea47eSEdward Tomasz Napierala static void
1972829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused)
1973009ea47eSEdward Tomasz Napierala {
1974009ea47eSEdward Tomasz Napierala 	/*
1975009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
1976009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
1977009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
1978009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
1979009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
1980009ea47eSEdward Tomasz Napierala 	 */
1981009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
1982009ea47eSEdward Tomasz Napierala 		/*
1983009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
1984009ea47eSEdward Tomasz Napierala 		 */
1985009ea47eSEdward Tomasz Napierala 		_exit(2);
1986009ea47eSEdward Tomasz Napierala 	}
1987009ea47eSEdward Tomasz Napierala 
1988009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
1989009ea47eSEdward Tomasz Napierala }
1990009ea47eSEdward Tomasz Napierala 
1991009ea47eSEdward Tomasz Napierala static void
1992829603e2SAlexander Motin sigalrm_handler(int dummy __unused)
1993829603e2SAlexander Motin {
1994829603e2SAlexander Motin 
1995829603e2SAlexander Motin 	sigalrm_received = true;
1996829603e2SAlexander Motin }
1997829603e2SAlexander Motin 
1998829603e2SAlexander Motin void
1999829603e2SAlexander Motin set_timeout(int timeout, int fatal)
2000009ea47eSEdward Tomasz Napierala {
2001009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2002009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
2003009ea47eSEdward Tomasz Napierala 	int error;
2004009ea47eSEdward Tomasz Napierala 
2005829603e2SAlexander Motin 	if (timeout <= 0) {
2006009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
2007829603e2SAlexander Motin 		bzero(&itv, sizeof(itv));
2008829603e2SAlexander Motin 		error = setitimer(ITIMER_REAL, &itv, NULL);
2009829603e2SAlexander Motin 		if (error != 0)
2010829603e2SAlexander Motin 			log_err(1, "setitimer");
2011829603e2SAlexander Motin 		sigalrm_received = false;
2012009ea47eSEdward Tomasz Napierala 		return;
2013009ea47eSEdward Tomasz Napierala 	}
2014009ea47eSEdward Tomasz Napierala 
2015829603e2SAlexander Motin 	sigalrm_received = false;
2016009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2017829603e2SAlexander Motin 	if (fatal)
2018829603e2SAlexander Motin 		sa.sa_handler = sigalrm_handler_fatal;
2019829603e2SAlexander Motin 	else
2020009ea47eSEdward Tomasz Napierala 		sa.sa_handler = sigalrm_handler;
2021009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2022009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
2023009ea47eSEdward Tomasz Napierala 	if (error != 0)
2024009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2025009ea47eSEdward Tomasz Napierala 
2026009ea47eSEdward Tomasz Napierala 	/*
2027009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
2028009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
2029009ea47eSEdward Tomasz Napierala 	 */
2030829603e2SAlexander Motin 	log_debugx("setting session timeout to %d seconds", timeout);
2031009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
2032009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
2033829603e2SAlexander Motin 	itv.it_value.tv_sec = timeout;
2034009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
2035009ea47eSEdward Tomasz Napierala 	if (error != 0)
2036009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
2037009ea47eSEdward Tomasz Napierala }
2038009ea47eSEdward Tomasz Napierala 
2039009ea47eSEdward Tomasz Napierala static int
2040009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
2041009ea47eSEdward Tomasz Napierala {
2042009ea47eSEdward Tomasz Napierala 	pid_t pid;
2043009ea47eSEdward Tomasz Napierala 	int status;
2044009ea47eSEdward Tomasz Napierala 	int num = 0;
2045009ea47eSEdward Tomasz Napierala 
2046009ea47eSEdward Tomasz Napierala 	for (;;) {
2047009ea47eSEdward Tomasz Napierala 		/*
2048009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
2049009ea47eSEdward Tomasz Napierala 		 */
2050009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
2051009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
2052009ea47eSEdward Tomasz Napierala 		else
2053009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
2054009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
2055009ea47eSEdward Tomasz Napierala 			break;
2056009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
2057009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
2058009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
2059009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
2060009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
2061009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
2062009ea47eSEdward Tomasz Napierala 		} else {
2063009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
2064009ea47eSEdward Tomasz Napierala 		}
2065009ea47eSEdward Tomasz Napierala 		num++;
2066009ea47eSEdward Tomasz Napierala 	}
2067009ea47eSEdward Tomasz Napierala 
2068009ea47eSEdward Tomasz Napierala 	return (num);
2069009ea47eSEdward Tomasz Napierala }
2070009ea47eSEdward Tomasz Napierala 
2071009ea47eSEdward Tomasz Napierala static void
20728eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
2073073edb1cSAlexander Motin     const struct sockaddr *client_sa, bool dont_fork)
2074009ea47eSEdward Tomasz Napierala {
2075009ea47eSEdward Tomasz Napierala 	struct connection *conn;
2076009ea47eSEdward Tomasz Napierala 	int error;
2077009ea47eSEdward Tomasz Napierala 	pid_t pid;
2078009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
2079009ea47eSEdward Tomasz Napierala 	struct conf *conf;
2080009ea47eSEdward Tomasz Napierala 
2081009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
2082009ea47eSEdward Tomasz Napierala 
2083009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
2084009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
2085009ea47eSEdward Tomasz Napierala 	} else {
2086009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
2087009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
2088009ea47eSEdward Tomasz Napierala 
2089009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2090009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
2091009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
2092009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
2093009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2094009ea47eSEdward Tomasz Napierala 		}
2095009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
2096009ea47eSEdward Tomasz Napierala 		    nchildren);
2097009ea47eSEdward Tomasz Napierala 		nchildren++;
2098009ea47eSEdward Tomasz Napierala 		pid = fork();
2099009ea47eSEdward Tomasz Napierala 		if (pid < 0)
2100009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
2101009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
2102009ea47eSEdward Tomasz Napierala 			close(fd);
2103009ea47eSEdward Tomasz Napierala 			return;
2104009ea47eSEdward Tomasz Napierala 		}
2105009ea47eSEdward Tomasz Napierala 	}
2106009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
2107009ea47eSEdward Tomasz Napierala 
2108073edb1cSAlexander Motin 	error = getnameinfo(client_sa, client_sa->sa_len,
2109009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2110009ea47eSEdward Tomasz Napierala 	if (error != 0)
21119a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2112009ea47eSEdward Tomasz Napierala 
2113009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
2114009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
2115009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
2116009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
2117009ea47eSEdward Tomasz Napierala 
2118073edb1cSAlexander Motin 	conn = connection_new(portal, fd, host, client_sa);
2119829603e2SAlexander Motin 	set_timeout(conf->conf_timeout, true);
2120009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
2121009ea47eSEdward Tomasz Napierala 	login(conn);
2122009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2123009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
2124009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
2125009ea47eSEdward Tomasz Napierala 	} else {
2126009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2127009ea47eSEdward Tomasz Napierala 		discovery(conn);
2128009ea47eSEdward Tomasz Napierala 	}
2129009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
2130009ea47eSEdward Tomasz Napierala 	exit(0);
2131009ea47eSEdward Tomasz Napierala }
2132009ea47eSEdward Tomasz Napierala 
2133009ea47eSEdward Tomasz Napierala static int
2134009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
2135009ea47eSEdward Tomasz Napierala {
2136009ea47eSEdward Tomasz Napierala 
2137009ea47eSEdward Tomasz Napierala 	/*
2138009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
2139009ea47eSEdward Tomasz Napierala 	 */
2140009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
2141009ea47eSEdward Tomasz Napierala 		return (nfds);
2142009ea47eSEdward Tomasz Napierala 
2143009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
2144009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
2145009ea47eSEdward Tomasz Napierala 		nfds = fd;
2146009ea47eSEdward Tomasz Napierala 	return (nfds);
2147009ea47eSEdward Tomasz Napierala }
2148009ea47eSEdward Tomasz Napierala 
2149009ea47eSEdward Tomasz Napierala static void
2150009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
2151009ea47eSEdward Tomasz Napierala {
2152009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
2153009ea47eSEdward Tomasz Napierala 	struct portal *portal;
21549a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
21559a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
2156009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2157009ea47eSEdward Tomasz Napierala 	int connection_id;
21588cab2ed4SEdward Tomasz Napierala 	int portal_id;
2159ba3a2d31SEdward Tomasz Napierala #endif
2160009ea47eSEdward Tomasz Napierala 	fd_set fdset;
2161009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
2162009ea47eSEdward Tomasz Napierala 
2163009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
2164009ea47eSEdward Tomasz Napierala 
2165009ea47eSEdward Tomasz Napierala 	for (;;) {
2166829603e2SAlexander Motin 		if (sighup_received || sigterm_received || timed_out())
2167009ea47eSEdward Tomasz Napierala 			return;
2168009ea47eSEdward Tomasz Napierala 
2169009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2170ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
21718eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
21728eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
21738eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
21740488e848SEdward Tomasz Napierala 			assert(client_salen >= client_sa.ss_len);
2175009ea47eSEdward Tomasz Napierala 
21768cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
21778cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
21788cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
21798cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
21808cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
21818cab2ed4SEdward Tomasz Napierala 						goto found;
21828cab2ed4SEdward Tomasz Napierala 					}
21838cab2ed4SEdward Tomasz Napierala 				}
21848cab2ed4SEdward Tomasz Napierala 			}
2185009ea47eSEdward Tomasz Napierala 
21868cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
21878cab2ed4SEdward Tomasz Napierala 			    portal_id);
21888cab2ed4SEdward Tomasz Napierala 
21898cab2ed4SEdward Tomasz Napierala found:
21908eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
2191073edb1cSAlexander Motin 			    (struct sockaddr *)&client_sa, dont_fork);
2192ba3a2d31SEdward Tomasz Napierala 		} else {
2193ba3a2d31SEdward Tomasz Napierala #endif
2194ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2195ba3a2d31SEdward Tomasz Napierala 
2196009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
2197009ea47eSEdward Tomasz Napierala 			nfds = 0;
2198009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2199009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2200009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
2201009ea47eSEdward Tomasz Napierala 			}
2202009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2203009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
2204009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
2205009ea47eSEdward Tomasz Napierala 					return;
2206009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
2207009ea47eSEdward Tomasz Napierala 			}
2208009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2209009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2210009ea47eSEdward Tomasz Napierala 					if (!FD_ISSET(portal->p_socket, &fdset))
2211009ea47eSEdward Tomasz Napierala 						continue;
22129a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
22139a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
22149a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
22159a95cfffSEdward Tomasz Napierala 					    &client_salen);
2216009ea47eSEdward Tomasz Napierala 					if (client_fd < 0)
2217009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
22180488e848SEdward Tomasz Napierala 					assert(client_salen >= client_sa.ss_len);
22190488e848SEdward Tomasz Napierala 
22209a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
22218eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
2222073edb1cSAlexander Motin 					    dont_fork);
2223009ea47eSEdward Tomasz Napierala 					break;
2224009ea47eSEdward Tomasz Napierala 				}
2225009ea47eSEdward Tomasz Napierala 			}
2226ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2227ba3a2d31SEdward Tomasz Napierala 		}
2228ba3a2d31SEdward Tomasz Napierala #endif
2229009ea47eSEdward Tomasz Napierala 	}
2230009ea47eSEdward Tomasz Napierala }
2231009ea47eSEdward Tomasz Napierala 
2232009ea47eSEdward Tomasz Napierala static void
2233009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
2234009ea47eSEdward Tomasz Napierala {
2235009ea47eSEdward Tomasz Napierala 
2236009ea47eSEdward Tomasz Napierala 	sighup_received = true;
2237009ea47eSEdward Tomasz Napierala }
2238009ea47eSEdward Tomasz Napierala 
2239009ea47eSEdward Tomasz Napierala static void
2240009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
2241009ea47eSEdward Tomasz Napierala {
2242009ea47eSEdward Tomasz Napierala 
2243009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
2244009ea47eSEdward Tomasz Napierala }
2245009ea47eSEdward Tomasz Napierala 
2246009ea47eSEdward Tomasz Napierala static void
224770b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
224870b939ecSEdward Tomasz Napierala {
224970b939ecSEdward Tomasz Napierala 
225070b939ecSEdward Tomasz Napierala 	/*
225170b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
225270b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
225370b939ecSEdward Tomasz Napierala 	 * wait_for_children().
225470b939ecSEdward Tomasz Napierala 	 */
225570b939ecSEdward Tomasz Napierala }
225670b939ecSEdward Tomasz Napierala 
225770b939ecSEdward Tomasz Napierala static void
2258009ea47eSEdward Tomasz Napierala register_signals(void)
2259009ea47eSEdward Tomasz Napierala {
2260009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2261009ea47eSEdward Tomasz Napierala 	int error;
2262009ea47eSEdward Tomasz Napierala 
2263009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2264009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
2265009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2266009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
2267009ea47eSEdward Tomasz Napierala 	if (error != 0)
2268009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2269009ea47eSEdward Tomasz Napierala 
2270009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2271009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
2272009ea47eSEdward Tomasz Napierala 	if (error != 0)
2273009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2274009ea47eSEdward Tomasz Napierala 
2275009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2276009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
2277009ea47eSEdward Tomasz Napierala 	if (error != 0)
2278009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
227970b939ecSEdward Tomasz Napierala 
228070b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
228170b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
228270b939ecSEdward Tomasz Napierala 	if (error != 0)
228370b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
2284009ea47eSEdward Tomasz Napierala }
2285009ea47eSEdward Tomasz Napierala 
2286009ea47eSEdward Tomasz Napierala int
2287009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
2288009ea47eSEdward Tomasz Napierala {
2289009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
2290829603e2SAlexander Motin 	struct isns *newns;
2291009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
2292009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
2293009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
2294009ea47eSEdward Tomasz Napierala 
2295ba3a2d31SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "df:R")) != -1) {
2296009ea47eSEdward Tomasz Napierala 		switch (ch) {
2297009ea47eSEdward Tomasz Napierala 		case 'd':
2298009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
2299009ea47eSEdward Tomasz Napierala 			debug++;
2300009ea47eSEdward Tomasz Napierala 			break;
2301009ea47eSEdward Tomasz Napierala 		case 'f':
2302009ea47eSEdward Tomasz Napierala 			config_path = optarg;
2303009ea47eSEdward Tomasz Napierala 			break;
2304ba3a2d31SEdward Tomasz Napierala 		case 'R':
2305ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
2306ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2307ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
2308ba3a2d31SEdward Tomasz Napierala #endif
2309ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
2310ba3a2d31SEdward Tomasz Napierala 			break;
2311009ea47eSEdward Tomasz Napierala 		case '?':
2312009ea47eSEdward Tomasz Napierala 		default:
2313009ea47eSEdward Tomasz Napierala 			usage();
2314009ea47eSEdward Tomasz Napierala 		}
2315009ea47eSEdward Tomasz Napierala 	}
2316009ea47eSEdward Tomasz Napierala 	argc -= optind;
2317009ea47eSEdward Tomasz Napierala 	if (argc != 0)
2318009ea47eSEdward Tomasz Napierala 		usage();
2319009ea47eSEdward Tomasz Napierala 
2320009ea47eSEdward Tomasz Napierala 	log_init(debug);
2321009ea47eSEdward Tomasz Napierala 	kernel_init();
2322009ea47eSEdward Tomasz Napierala 
2323009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
2324009ea47eSEdward Tomasz Napierala 	newconf = conf_new_from_file(config_path);
2325009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
2326fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
2327009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
2328009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
2329009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
2330009ea47eSEdward Tomasz Napierala 	}
2331009ea47eSEdward Tomasz Napierala 
2332009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
2333009ea47eSEdward Tomasz Napierala 	if (error != 0)
2334fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
2335fe845cdbSEdward Tomasz Napierala 
2336009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
2337009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
2338009ea47eSEdward Tomasz Napierala 
2339009ea47eSEdward Tomasz Napierala 	register_signals();
2340009ea47eSEdward Tomasz Napierala 
23415d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
23425d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
23435d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
23445d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
23455d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
23465d5a95f1SEdward Tomasz Napierala 			exit(1);
23475d5a95f1SEdward Tomasz Napierala 		}
23485d5a95f1SEdward Tomasz Napierala 	}
23495d5a95f1SEdward Tomasz Napierala 
2350829603e2SAlexander Motin 	/* Schedule iSNS update */
2351829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2352829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2353829603e2SAlexander Motin 
2354009ea47eSEdward Tomasz Napierala 	for (;;) {
2355009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
2356009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
2357009ea47eSEdward Tomasz Napierala 			sighup_received = false;
2358009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
2359009ea47eSEdward Tomasz Napierala 			tmpconf = conf_new_from_file(config_path);
2360009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
2361009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
2362009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
2363009ea47eSEdward Tomasz Napierala 			} else {
2364009ea47eSEdward Tomasz Napierala 				if (debug > 0)
2365009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
2366009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
2367009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
2368009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
2369009ea47eSEdward Tomasz Napierala 				if (error != 0)
2370009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
2371009ea47eSEdward Tomasz Napierala 					    "configuration");
2372009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
2373009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
2374009ea47eSEdward Tomasz Napierala 			}
2375009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
2376009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
2377009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
2378009ea47eSEdward Tomasz Napierala 
2379009ea47eSEdward Tomasz Napierala 			log_debugx("disabling CTL iSCSI port "
2380009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
2381009ea47eSEdward Tomasz Napierala 
2382009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
2383009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
2384009ea47eSEdward Tomasz Napierala 			if (debug > 0)
2385009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
2386009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
2387009ea47eSEdward Tomasz Napierala 			if (error != 0)
2388009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
2389829603e2SAlexander Motin 			conf_delete(oldconf);
2390829603e2SAlexander Motin 			oldconf = NULL;
2391009ea47eSEdward Tomasz Napierala 
2392009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
2393009ea47eSEdward Tomasz Napierala 			exit(0);
2394009ea47eSEdward Tomasz Napierala 		} else {
2395009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
2396009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2397829603e2SAlexander Motin 			if (timed_out()) {
2398829603e2SAlexander Motin 				set_timeout(0, false);
2399829603e2SAlexander Motin 				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2400829603e2SAlexander Motin 					isns_check(newns);
2401829603e2SAlexander Motin 				/* Schedule iSNS update */
2402829603e2SAlexander Motin 				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2403829603e2SAlexander Motin 					set_timeout((newconf->conf_isns_period
2404829603e2SAlexander Motin 					    + 2) / 3,
2405829603e2SAlexander Motin 					    false);
2406829603e2SAlexander Motin 				}
2407829603e2SAlexander Motin 			}
2408009ea47eSEdward Tomasz Napierala 		}
2409009ea47eSEdward Tomasz Napierala 	}
2410009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
2411009ea47eSEdward Tomasz Napierala }
2412