xref: /freebsd/usr.sbin/ctld/ctld.c (revision 61a2a354f89fa70b29c317d3a4231c598a88e182)
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  * $FreeBSD$
30009ea47eSEdward Tomasz Napierala  */
31009ea47eSEdward Tomasz Napierala 
32009ea47eSEdward Tomasz Napierala #include <sys/types.h>
33009ea47eSEdward Tomasz Napierala #include <sys/time.h>
34009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
35009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
36009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
37009ea47eSEdward Tomasz Napierala #include <assert.h>
38009ea47eSEdward Tomasz Napierala #include <ctype.h>
39009ea47eSEdward Tomasz Napierala #include <errno.h>
40009ea47eSEdward Tomasz Napierala #include <netdb.h>
41009ea47eSEdward Tomasz Napierala #include <signal.h>
42009ea47eSEdward Tomasz Napierala #include <stdbool.h>
43009ea47eSEdward Tomasz Napierala #include <stdio.h>
44009ea47eSEdward Tomasz Napierala #include <stdint.h>
45009ea47eSEdward Tomasz Napierala #include <stdlib.h>
46009ea47eSEdward Tomasz Napierala #include <string.h>
47009ea47eSEdward Tomasz Napierala #include <unistd.h>
48009ea47eSEdward Tomasz Napierala 
49009ea47eSEdward Tomasz Napierala #include "ctld.h"
50009ea47eSEdward Tomasz Napierala 
51ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false;
52ba3a2d31SEdward Tomasz Napierala 
53009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
54009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
55009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
56009ea47eSEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala static int nchildren = 0;
58009ea47eSEdward Tomasz Napierala 
59009ea47eSEdward Tomasz Napierala static void
60009ea47eSEdward Tomasz Napierala usage(void)
61009ea47eSEdward Tomasz Napierala {
62009ea47eSEdward Tomasz Napierala 
63009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
64009ea47eSEdward Tomasz Napierala 	exit(1);
65009ea47eSEdward Tomasz Napierala }
66009ea47eSEdward Tomasz Napierala 
67009ea47eSEdward Tomasz Napierala char *
68009ea47eSEdward Tomasz Napierala checked_strdup(const char *s)
69009ea47eSEdward Tomasz Napierala {
70009ea47eSEdward Tomasz Napierala 	char *c;
71009ea47eSEdward Tomasz Napierala 
72009ea47eSEdward Tomasz Napierala 	c = strdup(s);
73009ea47eSEdward Tomasz Napierala 	if (c == NULL)
74009ea47eSEdward Tomasz Napierala 		log_err(1, "strdup");
75009ea47eSEdward Tomasz Napierala 	return (c);
76009ea47eSEdward Tomasz Napierala }
77009ea47eSEdward Tomasz Napierala 
78009ea47eSEdward Tomasz Napierala struct conf *
79009ea47eSEdward Tomasz Napierala conf_new(void)
80009ea47eSEdward Tomasz Napierala {
81009ea47eSEdward Tomasz Napierala 	struct conf *conf;
82009ea47eSEdward Tomasz Napierala 
83009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
84009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
85009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
86009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
87009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
88009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
89009ea47eSEdward Tomasz Napierala 
90009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
91009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
92009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
93009ea47eSEdward Tomasz Napierala 
94009ea47eSEdward Tomasz Napierala 	return (conf);
95009ea47eSEdward Tomasz Napierala }
96009ea47eSEdward Tomasz Napierala 
97009ea47eSEdward Tomasz Napierala void
98009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
99009ea47eSEdward Tomasz Napierala {
100009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
101009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
102009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
103009ea47eSEdward Tomasz Napierala 
104009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
105009ea47eSEdward Tomasz Napierala 
106009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
107009ea47eSEdward Tomasz Napierala 		target_delete(targ);
108009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
109009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
110009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
111009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
112009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
113009ea47eSEdward Tomasz Napierala 	free(conf);
114009ea47eSEdward Tomasz Napierala }
115009ea47eSEdward Tomasz Napierala 
116009ea47eSEdward Tomasz Napierala static struct auth *
117009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
118009ea47eSEdward Tomasz Napierala {
119009ea47eSEdward Tomasz Napierala 	struct auth *auth;
120009ea47eSEdward Tomasz Napierala 
121009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
122009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
123009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
124009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
125009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
126009ea47eSEdward Tomasz Napierala 	return (auth);
127009ea47eSEdward Tomasz Napierala }
128009ea47eSEdward Tomasz Napierala 
129009ea47eSEdward Tomasz Napierala static void
130009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
131009ea47eSEdward Tomasz Napierala {
132009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
133009ea47eSEdward Tomasz Napierala 
134009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
135009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
136009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
137009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
138009ea47eSEdward Tomasz Napierala 	free(auth);
139009ea47eSEdward Tomasz Napierala }
140009ea47eSEdward Tomasz Napierala 
141009ea47eSEdward Tomasz Napierala const struct auth *
142d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
143009ea47eSEdward Tomasz Napierala {
144009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
145009ea47eSEdward Tomasz Napierala 
146009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
147009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
148009ea47eSEdward Tomasz Napierala 			return (auth);
149009ea47eSEdward Tomasz Napierala 	}
150009ea47eSEdward Tomasz Napierala 
151009ea47eSEdward Tomasz Napierala 	return (NULL);
152009ea47eSEdward Tomasz Napierala }
153009ea47eSEdward Tomasz Napierala 
1541658628dSEdward Tomasz Napierala static void
1551658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1561658628dSEdward Tomasz Napierala {
1571658628dSEdward Tomasz Napierala 	size_t len;
1581658628dSEdward Tomasz Napierala 
1591658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1601658628dSEdward Tomasz Napierala 	if (len > 16) {
1611658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1621658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1631658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1641658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1651658628dSEdward Tomasz Napierala 		else
1661658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1671658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1681658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
169f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1701658628dSEdward Tomasz Napierala 	}
1711658628dSEdward Tomasz Napierala 	if (len < 12) {
1721658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1731658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1741658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1751658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1761658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1771658628dSEdward Tomasz Napierala 		else
1781658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1791658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 16 characters "
1801658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
181f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1821658628dSEdward Tomasz Napierala 	}
1831658628dSEdward Tomasz Napierala 
1841658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
1851658628dSEdward Tomasz Napierala 		len = strlen(auth->a_secret);
1861658628dSEdward Tomasz Napierala 		if (len > 16) {
1871658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
1881658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1891658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
1901658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
1911658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
1921658628dSEdward Tomasz Napierala 			else
1931658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1941658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
1951658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
1961658628dSEdward Tomasz Napierala 				    auth->a_user,
197f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
1981658628dSEdward Tomasz Napierala 		}
1991658628dSEdward Tomasz Napierala 		if (len < 12) {
2001658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2011658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2021658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2031658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2041658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2051658628dSEdward Tomasz Napierala 			else
2061658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2071658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2081658628dSEdward Tomasz Napierala 				    "at least 16 characters long",
2091658628dSEdward Tomasz Napierala 				    auth->a_user,
210f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2111658628dSEdward Tomasz Napierala 		}
2121658628dSEdward Tomasz Napierala 	}
2131658628dSEdward Tomasz Napierala }
2141658628dSEdward Tomasz Napierala 
2151658628dSEdward Tomasz Napierala const struct auth *
2161658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2171658628dSEdward Tomasz Napierala     const char *secret)
2181658628dSEdward Tomasz Napierala {
2191658628dSEdward Tomasz Napierala 	struct auth *auth;
2201658628dSEdward Tomasz Napierala 
2211658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2221658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2231658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2241658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2251658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2261658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2271658628dSEdward Tomasz Napierala 		else
2281658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2291658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
230f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2311658628dSEdward Tomasz Napierala 		return (NULL);
2321658628dSEdward Tomasz Napierala 	}
2331658628dSEdward Tomasz Napierala 
2341658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2351658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2361658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2371658628dSEdward Tomasz Napierala 
2381658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2391658628dSEdward Tomasz Napierala 
2401658628dSEdward Tomasz Napierala 	return (auth);
2411658628dSEdward Tomasz Napierala }
2421658628dSEdward Tomasz Napierala 
2431658628dSEdward Tomasz Napierala const struct auth *
2441658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2451658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2461658628dSEdward Tomasz Napierala {
2471658628dSEdward Tomasz Napierala 	struct auth *auth;
2481658628dSEdward Tomasz Napierala 
2491658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2501658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2511658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2521658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2531658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2541658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2551658628dSEdward Tomasz Napierala 			    ag->ag_name);
2561658628dSEdward Tomasz Napierala 		else
2571658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2581658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
259f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2601658628dSEdward Tomasz Napierala 		return (NULL);
2611658628dSEdward Tomasz Napierala 	}
2621658628dSEdward Tomasz Napierala 
2631658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2641658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2651658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2661658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2671658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2681658628dSEdward Tomasz Napierala 
2691658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2701658628dSEdward Tomasz Napierala 
2711658628dSEdward Tomasz Napierala 	return (auth);
2721658628dSEdward Tomasz Napierala }
2731658628dSEdward Tomasz Napierala 
2748cb2e958SEdward Tomasz Napierala const struct auth_name *
2758cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2768cb2e958SEdward Tomasz Napierala {
2778cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
2788cb2e958SEdward Tomasz Napierala 
2798cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
2808cb2e958SEdward Tomasz Napierala 	if (an == NULL)
2818cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
2828cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
2838cb2e958SEdward Tomasz Napierala 	an->an_initator_name = checked_strdup(name);
2848cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
2858cb2e958SEdward Tomasz Napierala 	return (an);
2868cb2e958SEdward Tomasz Napierala }
2878cb2e958SEdward Tomasz Napierala 
2888cb2e958SEdward Tomasz Napierala static void
2898cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
2908cb2e958SEdward Tomasz Napierala {
2918cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
2928cb2e958SEdward Tomasz Napierala 
2938cb2e958SEdward Tomasz Napierala 	free(an->an_initator_name);
2948cb2e958SEdward Tomasz Napierala 	free(an);
2958cb2e958SEdward Tomasz Napierala }
2968cb2e958SEdward Tomasz Napierala 
2978cb2e958SEdward Tomasz Napierala bool
2988cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
2998cb2e958SEdward Tomasz Napierala {
3008cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3018cb2e958SEdward Tomasz Napierala 		return (false);
3028cb2e958SEdward Tomasz Napierala 	return (true);
3038cb2e958SEdward Tomasz Napierala }
3048cb2e958SEdward Tomasz Napierala 
3058cb2e958SEdward Tomasz Napierala const struct auth_name *
3068cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3078cb2e958SEdward Tomasz Napierala {
3088cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3098cb2e958SEdward Tomasz Napierala 
3108cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
3118cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_name->an_initator_name, name) == 0)
3128cb2e958SEdward Tomasz Napierala 			return (auth_name);
3138cb2e958SEdward Tomasz Napierala 	}
3148cb2e958SEdward Tomasz Napierala 
3158cb2e958SEdward Tomasz Napierala 	return (NULL);
3168cb2e958SEdward Tomasz Napierala }
3178cb2e958SEdward Tomasz Napierala 
3188cb2e958SEdward Tomasz Napierala const struct auth_portal *
3198cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3208cb2e958SEdward Tomasz Napierala {
3218cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
3228cb2e958SEdward Tomasz Napierala 
3238cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3248cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3258cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3268cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
3278cb2e958SEdward Tomasz Napierala 	ap->ap_initator_portal = checked_strdup(portal);
3288cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
3298cb2e958SEdward Tomasz Napierala 	return (ap);
3308cb2e958SEdward Tomasz Napierala }
3318cb2e958SEdward Tomasz Napierala 
3328cb2e958SEdward Tomasz Napierala static void
3338cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
3348cb2e958SEdward Tomasz Napierala {
3358cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
3368cb2e958SEdward Tomasz Napierala 
3378cb2e958SEdward Tomasz Napierala 	free(ap->ap_initator_portal);
3388cb2e958SEdward Tomasz Napierala 	free(ap);
3398cb2e958SEdward Tomasz Napierala }
3408cb2e958SEdward Tomasz Napierala 
3418cb2e958SEdward Tomasz Napierala bool
3428cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
3438cb2e958SEdward Tomasz Napierala {
3448cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
3458cb2e958SEdward Tomasz Napierala 		return (false);
3468cb2e958SEdward Tomasz Napierala 	return (true);
3478cb2e958SEdward Tomasz Napierala }
3488cb2e958SEdward Tomasz Napierala 
3498cb2e958SEdward Tomasz Napierala const struct auth_portal *
3508cb2e958SEdward Tomasz Napierala auth_portal_find(const struct auth_group *ag, const char *portal)
3518cb2e958SEdward Tomasz Napierala {
3528cb2e958SEdward Tomasz Napierala 	const struct auth_portal *auth_portal;
3538cb2e958SEdward Tomasz Napierala 
3548cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next) {
3558cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_portal->ap_initator_portal, portal) == 0)
3568cb2e958SEdward Tomasz Napierala 			return (auth_portal);
3578cb2e958SEdward Tomasz Napierala 	}
3588cb2e958SEdward Tomasz Napierala 
3598cb2e958SEdward Tomasz Napierala 	return (NULL);
3608cb2e958SEdward Tomasz Napierala }
3618cb2e958SEdward Tomasz Napierala 
362009ea47eSEdward Tomasz Napierala struct auth_group *
363009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
364009ea47eSEdward Tomasz Napierala {
365009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
366009ea47eSEdward Tomasz Napierala 
367009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
368009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
369009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
370009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
371009ea47eSEdward Tomasz Napierala 			return (NULL);
372009ea47eSEdward Tomasz Napierala 		}
373009ea47eSEdward Tomasz Napierala 	}
374009ea47eSEdward Tomasz Napierala 
375009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
376009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
377009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
378009ea47eSEdward Tomasz Napierala 	if (name != NULL)
379009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
380009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
3818cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
3828cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
383009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
384009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
385009ea47eSEdward Tomasz Napierala 
386009ea47eSEdward Tomasz Napierala 	return (ag);
387009ea47eSEdward Tomasz Napierala }
388009ea47eSEdward Tomasz Napierala 
389009ea47eSEdward Tomasz Napierala void
390009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
391009ea47eSEdward Tomasz Napierala {
3928cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
3938cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
3948cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
395009ea47eSEdward Tomasz Napierala 
396009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
397009ea47eSEdward Tomasz Napierala 
3988cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
399009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
4008cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
4018cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
4028cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
4038cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
4048cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
405009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
406009ea47eSEdward Tomasz Napierala 	free(ag);
407009ea47eSEdward Tomasz Napierala }
408009ea47eSEdward Tomasz Napierala 
409009ea47eSEdward Tomasz Napierala struct auth_group *
410d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
411009ea47eSEdward Tomasz Napierala {
412009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
413009ea47eSEdward Tomasz Napierala 
414009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
415009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
416009ea47eSEdward Tomasz Napierala 			return (ag);
417009ea47eSEdward Tomasz Napierala 	}
418009ea47eSEdward Tomasz Napierala 
419009ea47eSEdward Tomasz Napierala 	return (NULL);
420009ea47eSEdward Tomasz Napierala }
421009ea47eSEdward Tomasz Napierala 
422df9900fbSEdward Tomasz Napierala static int
423df9900fbSEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, int type)
424df9900fbSEdward Tomasz Napierala {
425df9900fbSEdward Tomasz Napierala 
426df9900fbSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN) {
427df9900fbSEdward Tomasz Napierala 		ag->ag_type = type;
428df9900fbSEdward Tomasz Napierala 		return (0);
429df9900fbSEdward Tomasz Napierala 	}
430df9900fbSEdward Tomasz Napierala 
431df9900fbSEdward Tomasz Napierala 	if (ag->ag_type == type)
432df9900fbSEdward Tomasz Napierala 		return (0);
433df9900fbSEdward Tomasz Napierala 
434df9900fbSEdward Tomasz Napierala 	return (1);
435df9900fbSEdward Tomasz Napierala }
436df9900fbSEdward Tomasz Napierala 
437df9900fbSEdward Tomasz Napierala int
438df9900fbSEdward Tomasz Napierala auth_group_set_type_str(struct auth_group *ag, const char *str)
439df9900fbSEdward Tomasz Napierala {
440df9900fbSEdward Tomasz Napierala 	int error, type;
441df9900fbSEdward Tomasz Napierala 
442df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
443df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
444e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
445e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
446df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
447df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
448df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
449df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
450df9900fbSEdward Tomasz Napierala 	} else {
451df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
452df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
453df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
454df9900fbSEdward Tomasz Napierala 		else
455df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
456df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
457df9900fbSEdward Tomasz Napierala 		return (1);
458df9900fbSEdward Tomasz Napierala 	}
459df9900fbSEdward Tomasz Napierala 
460df9900fbSEdward Tomasz Napierala 	error = auth_group_set_type(ag, type);
461df9900fbSEdward Tomasz Napierala 	if (error != 0) {
462df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
463df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
464df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
465df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
466df9900fbSEdward Tomasz Napierala 		else
467df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
468df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
469df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
470df9900fbSEdward Tomasz Napierala 		return (1);
471df9900fbSEdward Tomasz Napierala 	}
472df9900fbSEdward Tomasz Napierala 
473df9900fbSEdward Tomasz Napierala 	return (error);
474df9900fbSEdward Tomasz Napierala }
475df9900fbSEdward Tomasz Napierala 
476009ea47eSEdward Tomasz Napierala static struct portal *
477009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
478009ea47eSEdward Tomasz Napierala {
479009ea47eSEdward Tomasz Napierala 	struct portal *portal;
480009ea47eSEdward Tomasz Napierala 
481009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
482009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
483009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
484009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
485009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
486009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
487009ea47eSEdward Tomasz Napierala 	return (portal);
488009ea47eSEdward Tomasz Napierala }
489009ea47eSEdward Tomasz Napierala 
490009ea47eSEdward Tomasz Napierala static void
491009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
492009ea47eSEdward Tomasz Napierala {
493009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
494009ea47eSEdward Tomasz Napierala 	freeaddrinfo(portal->p_ai);
495009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
496009ea47eSEdward Tomasz Napierala 	free(portal);
497009ea47eSEdward Tomasz Napierala }
498009ea47eSEdward Tomasz Napierala 
499009ea47eSEdward Tomasz Napierala struct portal_group *
500009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
501009ea47eSEdward Tomasz Napierala {
502009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
503009ea47eSEdward Tomasz Napierala 
504009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
505009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
506009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
507009ea47eSEdward Tomasz Napierala 		return (NULL);
508009ea47eSEdward Tomasz Napierala 	}
509009ea47eSEdward Tomasz Napierala 
510009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
511009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
512009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
513009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
514009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
515009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
516009ea47eSEdward Tomasz Napierala 	conf->conf_last_portal_group_tag++;
517009ea47eSEdward Tomasz Napierala 	pg->pg_tag = conf->conf_last_portal_group_tag;
518009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
519009ea47eSEdward Tomasz Napierala 
520009ea47eSEdward Tomasz Napierala 	return (pg);
521009ea47eSEdward Tomasz Napierala }
522009ea47eSEdward Tomasz Napierala 
523009ea47eSEdward Tomasz Napierala void
524009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
525009ea47eSEdward Tomasz Napierala {
526009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
527009ea47eSEdward Tomasz Napierala 
528009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
529009ea47eSEdward Tomasz Napierala 
530009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
531009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
532009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
533009ea47eSEdward Tomasz Napierala 	free(pg);
534009ea47eSEdward Tomasz Napierala }
535009ea47eSEdward Tomasz Napierala 
536009ea47eSEdward Tomasz Napierala struct portal_group *
537d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
538009ea47eSEdward Tomasz Napierala {
539009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
540009ea47eSEdward Tomasz Napierala 
541009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
542009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
543009ea47eSEdward Tomasz Napierala 			return (pg);
544009ea47eSEdward Tomasz Napierala 	}
545009ea47eSEdward Tomasz Napierala 
546009ea47eSEdward Tomasz Napierala 	return (NULL);
547009ea47eSEdward Tomasz Napierala }
548009ea47eSEdward Tomasz Napierala 
549009ea47eSEdward Tomasz Napierala int
550009ea47eSEdward Tomasz Napierala portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
551009ea47eSEdward Tomasz Napierala {
552009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
553009ea47eSEdward Tomasz Napierala 	struct portal *portal;
554009ea47eSEdward Tomasz Napierala 	char *addr, *ch, *arg;
555009ea47eSEdward Tomasz Napierala 	const char *port;
556009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
557009ea47eSEdward Tomasz Napierala 
558009ea47eSEdward Tomasz Napierala 	portal = portal_new(pg);
559009ea47eSEdward Tomasz Napierala 	portal->p_listen = checked_strdup(value);
560009ea47eSEdward Tomasz Napierala 	portal->p_iser = iser;
561009ea47eSEdward Tomasz Napierala 
562009ea47eSEdward Tomasz Napierala 	arg = portal->p_listen;
563009ea47eSEdward Tomasz Napierala 	if (arg[0] == '\0') {
564009ea47eSEdward Tomasz Napierala 		log_warnx("empty listen address");
565009ea47eSEdward Tomasz Napierala 		free(portal->p_listen);
566009ea47eSEdward Tomasz Napierala 		free(portal);
567009ea47eSEdward Tomasz Napierala 		return (1);
568009ea47eSEdward Tomasz Napierala 	}
569009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
570009ea47eSEdward Tomasz Napierala 		/*
571009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
572009ea47eSEdward Tomasz Napierala 		 */
573009ea47eSEdward Tomasz Napierala 		arg++;
574009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
575009ea47eSEdward Tomasz Napierala 		if (arg == NULL) {
576009ea47eSEdward Tomasz Napierala 			log_warnx("invalid listen address %s",
577009ea47eSEdward Tomasz Napierala 			    portal->p_listen);
578009ea47eSEdward Tomasz Napierala 			free(portal->p_listen);
579009ea47eSEdward Tomasz Napierala 			free(portal);
580009ea47eSEdward Tomasz Napierala 			return (1);
581009ea47eSEdward Tomasz Napierala 		}
582009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
583009ea47eSEdward Tomasz Napierala 			port = "3260";
584009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
585009ea47eSEdward Tomasz Napierala 			port = arg + 1;
586009ea47eSEdward Tomasz Napierala 		} else {
587009ea47eSEdward Tomasz Napierala 			log_warnx("invalid listen address %s",
588009ea47eSEdward Tomasz Napierala 			    portal->p_listen);
589009ea47eSEdward Tomasz Napierala 			free(portal->p_listen);
590009ea47eSEdward Tomasz Napierala 			free(portal);
591009ea47eSEdward Tomasz Napierala 			return (1);
592009ea47eSEdward Tomasz Napierala 		}
593009ea47eSEdward Tomasz Napierala 	} else {
594009ea47eSEdward Tomasz Napierala 		/*
595009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
596009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
597009ea47eSEdward Tomasz Napierala 		 */
598009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
599009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
600009ea47eSEdward Tomasz Napierala 				colons++;
601009ea47eSEdward Tomasz Napierala 		}
602009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
603009ea47eSEdward Tomasz Napierala 			addr = arg;
604009ea47eSEdward Tomasz Napierala 			port = "3260";
605009ea47eSEdward Tomasz Napierala 		} else {
606009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
607009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
608009ea47eSEdward Tomasz Napierala 				port = "3260";
609009ea47eSEdward Tomasz Napierala 			else
610009ea47eSEdward Tomasz Napierala 				port = arg;
611009ea47eSEdward Tomasz Napierala 		}
612009ea47eSEdward Tomasz Napierala 	}
613009ea47eSEdward Tomasz Napierala 
614009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
615009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
616009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
617009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
618009ea47eSEdward Tomasz Napierala 
619009ea47eSEdward Tomasz Napierala 	error = getaddrinfo(addr, port, &hints, &portal->p_ai);
620009ea47eSEdward Tomasz Napierala 	if (error != 0) {
621009ea47eSEdward Tomasz Napierala 		log_warnx("getaddrinfo for %s failed: %s",
622009ea47eSEdward Tomasz Napierala 		    portal->p_listen, gai_strerror(error));
623009ea47eSEdward Tomasz Napierala 		free(portal->p_listen);
624009ea47eSEdward Tomasz Napierala 		free(portal);
625009ea47eSEdward Tomasz Napierala 		return (1);
626009ea47eSEdward Tomasz Napierala 	}
627009ea47eSEdward Tomasz Napierala 
628009ea47eSEdward Tomasz Napierala 	/*
629009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
630009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
631009ea47eSEdward Tomasz Napierala 	 */
632009ea47eSEdward Tomasz Napierala 
633009ea47eSEdward Tomasz Napierala 	return (0);
634009ea47eSEdward Tomasz Napierala }
635009ea47eSEdward Tomasz Napierala 
636009ea47eSEdward Tomasz Napierala static bool
637009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
638009ea47eSEdward Tomasz Napierala {
639009ea47eSEdward Tomasz Napierala 	switch (ch) {
640009ea47eSEdward Tomasz Napierala 	case '0':
641009ea47eSEdward Tomasz Napierala 	case '1':
642009ea47eSEdward Tomasz Napierala 	case '2':
643009ea47eSEdward Tomasz Napierala 	case '3':
644009ea47eSEdward Tomasz Napierala 	case '4':
645009ea47eSEdward Tomasz Napierala 	case '5':
646009ea47eSEdward Tomasz Napierala 	case '6':
647009ea47eSEdward Tomasz Napierala 	case '7':
648009ea47eSEdward Tomasz Napierala 	case '8':
649009ea47eSEdward Tomasz Napierala 	case '9':
650009ea47eSEdward Tomasz Napierala 	case 'a':
651009ea47eSEdward Tomasz Napierala 	case 'A':
652009ea47eSEdward Tomasz Napierala 	case 'b':
653009ea47eSEdward Tomasz Napierala 	case 'B':
654009ea47eSEdward Tomasz Napierala 	case 'c':
655009ea47eSEdward Tomasz Napierala 	case 'C':
656009ea47eSEdward Tomasz Napierala 	case 'd':
657009ea47eSEdward Tomasz Napierala 	case 'D':
658009ea47eSEdward Tomasz Napierala 	case 'e':
659009ea47eSEdward Tomasz Napierala 	case 'E':
660009ea47eSEdward Tomasz Napierala 	case 'f':
661009ea47eSEdward Tomasz Napierala 	case 'F':
662009ea47eSEdward Tomasz Napierala 		return (true);
663009ea47eSEdward Tomasz Napierala 	default:
664009ea47eSEdward Tomasz Napierala 		return (false);
665009ea47eSEdward Tomasz Napierala 	}
666009ea47eSEdward Tomasz Napierala }
667009ea47eSEdward Tomasz Napierala 
668009ea47eSEdward Tomasz Napierala bool
669009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
670009ea47eSEdward Tomasz Napierala {
671009ea47eSEdward Tomasz Napierala 	int i;
672009ea47eSEdward Tomasz Napierala 
673009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
674009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
675009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
676009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
677009ea47eSEdward Tomasz Napierala 		return (false);
678009ea47eSEdward Tomasz Napierala 	}
679009ea47eSEdward Tomasz Napierala 
680009ea47eSEdward Tomasz Napierala 	/*
681009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
682009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
683009ea47eSEdward Tomasz Napierala 	 */
684009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
685009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
686009ea47eSEdward Tomasz Napierala 			/*
687009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
688009ea47eSEdward Tomasz Napierala 			 * 	by 3.2.6.2: iSCSI Name Encoding.
689009ea47eSEdward Tomasz Napierala 			 */
690009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
691009ea47eSEdward Tomasz Napierala 				continue;
692009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
693009ea47eSEdward Tomasz Napierala 				continue;
694009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
695009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
696009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
697009ea47eSEdward Tomasz Napierala 			break;
698009ea47eSEdward Tomasz Napierala 		}
699009ea47eSEdward Tomasz Napierala 		/*
700009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
701009ea47eSEdward Tomasz Napierala 		 */
702009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
703009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
704009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
705009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
706009ea47eSEdward Tomasz Napierala 			    "digits", name);
707009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
708009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
709009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
710009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
711009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
712009ea47eSEdward Tomasz Napierala 				break;
713009ea47eSEdward Tomasz Napierala 			}
714009ea47eSEdward Tomasz Napierala 		}
715009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
716009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
717009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
718009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
719009ea47eSEdward Tomasz Napierala 			    "digits", name);
720009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
721009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
722009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
723009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
724009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
725009ea47eSEdward Tomasz Napierala 				break;
726009ea47eSEdward Tomasz Napierala 			}
727009ea47eSEdward Tomasz Napierala 		}
728009ea47eSEdward Tomasz Napierala 	} else {
729009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
730009ea47eSEdward Tomasz Napierala 		    "either \".iqn\", \"eui.\", or \"naa.\"",
731009ea47eSEdward Tomasz Napierala 		    name);
732009ea47eSEdward Tomasz Napierala 	}
733009ea47eSEdward Tomasz Napierala 	return (true);
734009ea47eSEdward Tomasz Napierala }
735009ea47eSEdward Tomasz Napierala 
736009ea47eSEdward Tomasz Napierala struct target *
737f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
738009ea47eSEdward Tomasz Napierala {
739009ea47eSEdward Tomasz Napierala 	struct target *targ;
740009ea47eSEdward Tomasz Napierala 	int i, len;
741009ea47eSEdward Tomasz Napierala 
742f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
743009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
744f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
745009ea47eSEdward Tomasz Napierala 		return (NULL);
746009ea47eSEdward Tomasz Napierala 	}
747f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
748f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
749009ea47eSEdward Tomasz Napierala 		return (NULL);
750009ea47eSEdward Tomasz Napierala 	}
751009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
752009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
753009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
754f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
755009ea47eSEdward Tomasz Napierala 
756009ea47eSEdward Tomasz Napierala 	/*
757009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
758009ea47eSEdward Tomasz Napierala 	 */
759f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
760009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
761f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
762009ea47eSEdward Tomasz Napierala 
763009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&targ->t_luns);
764009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
765009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
766009ea47eSEdward Tomasz Napierala 
767009ea47eSEdward Tomasz Napierala 	return (targ);
768009ea47eSEdward Tomasz Napierala }
769009ea47eSEdward Tomasz Napierala 
770009ea47eSEdward Tomasz Napierala void
771009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
772009ea47eSEdward Tomasz Napierala {
773009ea47eSEdward Tomasz Napierala 	struct lun *lun, *tmp;
774009ea47eSEdward Tomasz Napierala 
775009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
776009ea47eSEdward Tomasz Napierala 
777009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
778009ea47eSEdward Tomasz Napierala 		lun_delete(lun);
779f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
780009ea47eSEdward Tomasz Napierala 	free(targ);
781009ea47eSEdward Tomasz Napierala }
782009ea47eSEdward Tomasz Napierala 
783009ea47eSEdward Tomasz Napierala struct target *
784f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
785009ea47eSEdward Tomasz Napierala {
786009ea47eSEdward Tomasz Napierala 	struct target *targ;
787009ea47eSEdward Tomasz Napierala 
788009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
789f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
790009ea47eSEdward Tomasz Napierala 			return (targ);
791009ea47eSEdward Tomasz Napierala 	}
792009ea47eSEdward Tomasz Napierala 
793009ea47eSEdward Tomasz Napierala 	return (NULL);
794009ea47eSEdward Tomasz Napierala }
795009ea47eSEdward Tomasz Napierala 
796009ea47eSEdward Tomasz Napierala struct lun *
797009ea47eSEdward Tomasz Napierala lun_new(struct target *targ, int lun_id)
798009ea47eSEdward Tomasz Napierala {
799009ea47eSEdward Tomasz Napierala 	struct lun *lun;
800009ea47eSEdward Tomasz Napierala 
801009ea47eSEdward Tomasz Napierala 	lun = lun_find(targ, lun_id);
802009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
803009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun %d for target \"%s\"",
804f7ae5bf8SEdward Tomasz Napierala 		    lun_id, targ->t_name);
805009ea47eSEdward Tomasz Napierala 		return (NULL);
806009ea47eSEdward Tomasz Napierala 	}
807009ea47eSEdward Tomasz Napierala 
808009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
809009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
810009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
811009ea47eSEdward Tomasz Napierala 	lun->l_lun = lun_id;
812009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
813009ea47eSEdward Tomasz Napierala 	lun->l_target = targ;
814009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
815009ea47eSEdward Tomasz Napierala 
816009ea47eSEdward Tomasz Napierala 	return (lun);
817009ea47eSEdward Tomasz Napierala }
818009ea47eSEdward Tomasz Napierala 
819009ea47eSEdward Tomasz Napierala void
820009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
821009ea47eSEdward Tomasz Napierala {
822009ea47eSEdward Tomasz Napierala 	struct lun_option *lo, *tmp;
823009ea47eSEdward Tomasz Napierala 
824009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
825009ea47eSEdward Tomasz Napierala 
826009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
827009ea47eSEdward Tomasz Napierala 		lun_option_delete(lo);
828009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
829009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
830009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
831009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
832009ea47eSEdward Tomasz Napierala 	free(lun);
833009ea47eSEdward Tomasz Napierala }
834009ea47eSEdward Tomasz Napierala 
835009ea47eSEdward Tomasz Napierala struct lun *
836d6093026SEdward Tomasz Napierala lun_find(const struct target *targ, int lun_id)
837009ea47eSEdward Tomasz Napierala {
838009ea47eSEdward Tomasz Napierala 	struct lun *lun;
839009ea47eSEdward Tomasz Napierala 
840009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
841009ea47eSEdward Tomasz Napierala 		if (lun->l_lun == lun_id)
842009ea47eSEdward Tomasz Napierala 			return (lun);
843009ea47eSEdward Tomasz Napierala 	}
844009ea47eSEdward Tomasz Napierala 
845009ea47eSEdward Tomasz Napierala 	return (NULL);
846009ea47eSEdward Tomasz Napierala }
847009ea47eSEdward Tomasz Napierala 
848009ea47eSEdward Tomasz Napierala void
849009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
850009ea47eSEdward Tomasz Napierala {
851009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
852009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
853009ea47eSEdward Tomasz Napierala }
854009ea47eSEdward Tomasz Napierala 
855009ea47eSEdward Tomasz Napierala void
856009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
857009ea47eSEdward Tomasz Napierala {
858009ea47eSEdward Tomasz Napierala 
859009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
860009ea47eSEdward Tomasz Napierala }
861009ea47eSEdward Tomasz Napierala 
862009ea47eSEdward Tomasz Napierala void
863009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
864009ea47eSEdward Tomasz Napierala {
865009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
866009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
867009ea47eSEdward Tomasz Napierala }
868009ea47eSEdward Tomasz Napierala 
869009ea47eSEdward Tomasz Napierala void
870009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
871009ea47eSEdward Tomasz Napierala {
872009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
873009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
874009ea47eSEdward Tomasz Napierala }
875009ea47eSEdward Tomasz Napierala 
876009ea47eSEdward Tomasz Napierala void
877009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
878009ea47eSEdward Tomasz Napierala {
879009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
880009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
881009ea47eSEdward Tomasz Napierala }
882009ea47eSEdward Tomasz Napierala 
883009ea47eSEdward Tomasz Napierala void
884009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
885009ea47eSEdward Tomasz Napierala {
886009ea47eSEdward Tomasz Napierala 
887009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
888009ea47eSEdward Tomasz Napierala }
889009ea47eSEdward Tomasz Napierala 
890009ea47eSEdward Tomasz Napierala void
891009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
892009ea47eSEdward Tomasz Napierala {
893009ea47eSEdward Tomasz Napierala 
894009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
895009ea47eSEdward Tomasz Napierala }
896009ea47eSEdward Tomasz Napierala 
897009ea47eSEdward Tomasz Napierala struct lun_option *
898009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value)
899009ea47eSEdward Tomasz Napierala {
900009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
901009ea47eSEdward Tomasz Napierala 
902009ea47eSEdward Tomasz Napierala 	lo = lun_option_find(lun, name);
903009ea47eSEdward Tomasz Napierala 	if (lo != NULL) {
904009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
905f7ae5bf8SEdward Tomasz Napierala 		    name, lun->l_lun, lun->l_target->t_name);
906009ea47eSEdward Tomasz Napierala 		return (NULL);
907009ea47eSEdward Tomasz Napierala 	}
908009ea47eSEdward Tomasz Napierala 
909009ea47eSEdward Tomasz Napierala 	lo = calloc(1, sizeof(*lo));
910009ea47eSEdward Tomasz Napierala 	if (lo == NULL)
911009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
912009ea47eSEdward Tomasz Napierala 	lo->lo_name = checked_strdup(name);
913009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
914009ea47eSEdward Tomasz Napierala 	lo->lo_lun = lun;
915009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
916009ea47eSEdward Tomasz Napierala 
917009ea47eSEdward Tomasz Napierala 	return (lo);
918009ea47eSEdward Tomasz Napierala }
919009ea47eSEdward Tomasz Napierala 
920009ea47eSEdward Tomasz Napierala void
921009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo)
922009ea47eSEdward Tomasz Napierala {
923009ea47eSEdward Tomasz Napierala 
924009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
925009ea47eSEdward Tomasz Napierala 
926009ea47eSEdward Tomasz Napierala 	free(lo->lo_name);
927009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
928009ea47eSEdward Tomasz Napierala 	free(lo);
929009ea47eSEdward Tomasz Napierala }
930009ea47eSEdward Tomasz Napierala 
931009ea47eSEdward Tomasz Napierala struct lun_option *
932d6093026SEdward Tomasz Napierala lun_option_find(const struct lun *lun, const char *name)
933009ea47eSEdward Tomasz Napierala {
934009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
935009ea47eSEdward Tomasz Napierala 
936009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
937009ea47eSEdward Tomasz Napierala 		if (strcmp(lo->lo_name, name) == 0)
938009ea47eSEdward Tomasz Napierala 			return (lo);
939009ea47eSEdward Tomasz Napierala 	}
940009ea47eSEdward Tomasz Napierala 
941009ea47eSEdward Tomasz Napierala 	return (NULL);
942009ea47eSEdward Tomasz Napierala }
943009ea47eSEdward Tomasz Napierala 
944009ea47eSEdward Tomasz Napierala void
945009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value)
946009ea47eSEdward Tomasz Napierala {
947009ea47eSEdward Tomasz Napierala 
948009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
949009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
950009ea47eSEdward Tomasz Napierala }
951009ea47eSEdward Tomasz Napierala 
952009ea47eSEdward Tomasz Napierala static struct connection *
953009ea47eSEdward Tomasz Napierala connection_new(struct portal *portal, int fd, const char *host)
954009ea47eSEdward Tomasz Napierala {
955009ea47eSEdward Tomasz Napierala 	struct connection *conn;
956009ea47eSEdward Tomasz Napierala 
957009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
958009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
959009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
960009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
961009ea47eSEdward Tomasz Napierala 	conn->conn_socket = fd;
962009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
963009ea47eSEdward Tomasz Napierala 
964009ea47eSEdward Tomasz Napierala 	/*
965009ea47eSEdward Tomasz Napierala 	 * Default values, from RFC 3720, section 12.
966009ea47eSEdward Tomasz Napierala 	 */
967009ea47eSEdward Tomasz Napierala 	conn->conn_max_data_segment_length = 8192;
968009ea47eSEdward Tomasz Napierala 	conn->conn_max_burst_length = 262144;
969009ea47eSEdward Tomasz Napierala 	conn->conn_immediate_data = true;
970009ea47eSEdward Tomasz Napierala 
971009ea47eSEdward Tomasz Napierala 	return (conn);
972009ea47eSEdward Tomasz Napierala }
973009ea47eSEdward Tomasz Napierala 
974009ea47eSEdward Tomasz Napierala #if 0
975009ea47eSEdward Tomasz Napierala static void
976009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
977009ea47eSEdward Tomasz Napierala {
978009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
979009ea47eSEdward Tomasz Napierala 	struct auth *auth;
9808cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
9818cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
982009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
983009ea47eSEdward Tomasz Napierala 	struct portal *portal;
984009ea47eSEdward Tomasz Napierala 	struct target *targ;
985009ea47eSEdward Tomasz Napierala 	struct lun *lun;
986009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
987009ea47eSEdward Tomasz Napierala 
988009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
989009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
990009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
991009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
992009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
993009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
9948cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
9958cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
9968cb2e958SEdward Tomasz Napierala 			    auth_name->an_initator_name);
9978cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
9988cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
9998cb2e958SEdward Tomasz Napierala 			    auth_portal->an_initator_portal);
1000009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1001009ea47eSEdward Tomasz Napierala 	}
1002009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1003009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1004009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1005009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1006009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1007009ea47eSEdward Tomasz Napierala 	}
1008009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1009f7ae5bf8SEdward Tomasz Napierala 		fprintf(stderr, "target %s {\n", targ->t_name);
1010009ea47eSEdward Tomasz Napierala 		if (targ->t_alias != NULL)
1011009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1012009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1013009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1014009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1015009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1016009ea47eSEdward Tomasz Napierala 				fprintf(stderr, "\t\toption %s %s\n",
1017009ea47eSEdward Tomasz Napierala 				    lo->lo_name, lo->lo_value);
1018009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t}\n");
1019009ea47eSEdward Tomasz Napierala 		}
1020009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1021009ea47eSEdward Tomasz Napierala 	}
1022009ea47eSEdward Tomasz Napierala }
1023009ea47eSEdward Tomasz Napierala #endif
1024009ea47eSEdward Tomasz Napierala 
10252fabfaa5SEdward Tomasz Napierala static int
10262fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
10272fabfaa5SEdward Tomasz Napierala {
10282fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
1029a19eebb1SEdward Tomasz Napierala 	const struct target *targ2;
10302fabfaa5SEdward Tomasz Napierala 
10312fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
10322fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
10332fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
10342fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
10352fabfaa5SEdward Tomasz Napierala 			log_warnx("missing path for lun %d, target \"%s\"",
1036f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
10372fabfaa5SEdward Tomasz Napierala 			return (1);
10382fabfaa5SEdward Tomasz Napierala 		}
10392fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
10402fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
10412fabfaa5SEdward Tomasz Napierala 			log_warnx("missing size for ramdisk-backed lun %d, "
1042f7ae5bf8SEdward Tomasz Napierala 			    "target \"%s\"", lun->l_lun, lun->l_target->t_name);
10432fabfaa5SEdward Tomasz Napierala 			return (1);
10442fabfaa5SEdward Tomasz Napierala 		}
10452fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
10462fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
10472fabfaa5SEdward Tomasz Napierala 			    "for ramdisk-backed lun %d, target \"%s\"",
1048f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
10492fabfaa5SEdward Tomasz Napierala 			return (1);
10502fabfaa5SEdward Tomasz Napierala 		}
10512fabfaa5SEdward Tomasz Napierala 	}
10522fabfaa5SEdward Tomasz Napierala 	if (lun->l_lun < 0 || lun->l_lun > 255) {
10532fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid lun number for lun %d, target \"%s\"; "
10542fabfaa5SEdward Tomasz Napierala 		    "must be between 0 and 255", lun->l_lun,
1055f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
10562fabfaa5SEdward Tomasz Napierala 		return (1);
10572fabfaa5SEdward Tomasz Napierala 	}
10582fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
10592fabfaa5SEdward Tomasz Napierala 		lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
10602fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
10612fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1062f7ae5bf8SEdward Tomasz Napierala 		    "must be larger than 0", lun->l_lun, lun->l_target->t_name);
10632fabfaa5SEdward Tomasz Napierala 		return (1);
10642fabfaa5SEdward Tomasz Napierala 	}
10652fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
10662fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid size for lun %d, target \"%s\"; "
10672fabfaa5SEdward Tomasz Napierala 		    "must be multiple of blocksize", lun->l_lun,
1068f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
10692fabfaa5SEdward Tomasz Napierala 		return (1);
10702fabfaa5SEdward Tomasz Napierala 	}
1071a19eebb1SEdward Tomasz Napierala 	TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1072a19eebb1SEdward Tomasz Napierala 		TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1073a19eebb1SEdward Tomasz Napierala 			if (lun == lun2)
1074a19eebb1SEdward Tomasz Napierala 				continue;
1075a19eebb1SEdward Tomasz Napierala 			if (lun->l_path != NULL && lun2->l_path != NULL &&
1076a19eebb1SEdward Tomasz Napierala 			    strcmp(lun->l_path, lun2->l_path) == 0) {
1077a19eebb1SEdward Tomasz Napierala 				log_debugx("WARNING: path \"%s\" duplicated "
1078a19eebb1SEdward Tomasz Napierala 				    "between lun %d, target \"%s\", and "
1079a19eebb1SEdward Tomasz Napierala 				    "lun %d, target \"%s\"", lun->l_path,
1080f7ae5bf8SEdward Tomasz Napierala 				    lun->l_lun, lun->l_target->t_name,
1081f7ae5bf8SEdward Tomasz Napierala 				    lun2->l_lun, lun2->l_target->t_name);
1082a19eebb1SEdward Tomasz Napierala 			}
1083a19eebb1SEdward Tomasz Napierala 		}
1084a19eebb1SEdward Tomasz Napierala 	}
10852fabfaa5SEdward Tomasz Napierala 
10862fabfaa5SEdward Tomasz Napierala 	return (0);
10872fabfaa5SEdward Tomasz Napierala }
10882fabfaa5SEdward Tomasz Napierala 
1089009ea47eSEdward Tomasz Napierala int
1090009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1091009ea47eSEdward Tomasz Napierala {
1092009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1093009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1094009ea47eSEdward Tomasz Napierala 	struct target *targ;
10952fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
109619b59f13SEdward Tomasz Napierala 	bool found_lun;
10972fabfaa5SEdward Tomasz Napierala 	int error;
1098009ea47eSEdward Tomasz Napierala 
1099009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1100009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1101009ea47eSEdward Tomasz Napierala 
1102009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1103009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1104e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1105e7977549SEdward Tomasz Napierala 			    "default");
1106e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1107009ea47eSEdward Tomasz Napierala 		}
1108009ea47eSEdward Tomasz Napierala 		if (targ->t_portal_group == NULL) {
1109009ea47eSEdward Tomasz Napierala 			targ->t_portal_group = portal_group_find(conf,
1110009ea47eSEdward Tomasz Napierala 			    "default");
1111009ea47eSEdward Tomasz Napierala 			assert(targ->t_portal_group != NULL);
1112009ea47eSEdward Tomasz Napierala 		}
111319b59f13SEdward Tomasz Napierala 		found_lun = false;
1114009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
11152fabfaa5SEdward Tomasz Napierala 			error = conf_verify_lun(lun);
11162fabfaa5SEdward Tomasz Napierala 			if (error != 0)
11172fabfaa5SEdward Tomasz Napierala 				return (error);
111819b59f13SEdward Tomasz Napierala 			found_lun = true;
1119009ea47eSEdward Tomasz Napierala 		}
112019b59f13SEdward Tomasz Napierala 		if (!found_lun) {
112119b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
112219b59f13SEdward Tomasz Napierala 			    targ->t_name);
1123009ea47eSEdward Tomasz Napierala 			return (1);
1124009ea47eSEdward Tomasz Napierala 		}
1125009ea47eSEdward Tomasz Napierala 	}
1126009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1127009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1128009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1129009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1130ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1131009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1132009ea47eSEdward Tomasz Napierala 		}
1133009ea47eSEdward Tomasz Napierala 
1134009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1135009ea47eSEdward Tomasz Napierala 			if (targ->t_portal_group == pg)
1136009ea47eSEdward Tomasz Napierala 				break;
1137009ea47eSEdward Tomasz Napierala 		}
1138009ea47eSEdward Tomasz Napierala 		if (targ == NULL) {
1139009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1140009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1141009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1142009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
1143009ea47eSEdward Tomasz Napierala 		} else
1144009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = false;
1145009ea47eSEdward Tomasz Napierala 	}
1146009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1147009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1148009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1149009ea47eSEdward Tomasz Napierala 		else
1150009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1151009ea47eSEdward Tomasz Napierala 
1152009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1153009ea47eSEdward Tomasz Napierala 			if (targ->t_auth_group == ag)
1154009ea47eSEdward Tomasz Napierala 				break;
1155009ea47eSEdward Tomasz Napierala 		}
1156009ea47eSEdward Tomasz Napierala 		if (targ == NULL && ag->ag_name != NULL &&
1157ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1158009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1159009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1160009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1161009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1162009ea47eSEdward Tomasz Napierala 		}
1163009ea47eSEdward Tomasz Napierala 	}
1164009ea47eSEdward Tomasz Napierala 
1165009ea47eSEdward Tomasz Napierala 	return (0);
1166009ea47eSEdward Tomasz Napierala }
1167009ea47eSEdward Tomasz Napierala 
1168009ea47eSEdward Tomasz Napierala static int
1169009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1170009ea47eSEdward Tomasz Napierala {
1171009ea47eSEdward Tomasz Napierala 	struct target *oldtarg, *newtarg, *tmptarg;
1172009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1173009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1174009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
1175009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
1176009ea47eSEdward Tomasz Napierala 	int changed, cumulated_error = 0, error;
1177009ea47eSEdward Tomasz Napierala 	int one = 1;
1178009ea47eSEdward Tomasz Napierala 
1179009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1180009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1181009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1182009ea47eSEdward Tomasz Napierala 	}
1183009ea47eSEdward Tomasz Napierala 
1184009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1185009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1186009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1187009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1188009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1189009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1190009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1191009ea47eSEdward Tomasz Napierala 		} else {
1192009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1193009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1194009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1195009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1196009ea47eSEdward Tomasz Napierala 		}
1197009ea47eSEdward Tomasz Napierala 	}
1198009ea47eSEdward Tomasz Napierala 
1199009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1200009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1201009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1202009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1203009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1204009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1205009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1206009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1207009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1208009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1209009ea47eSEdward Tomasz Napierala 		}
1210009ea47eSEdward Tomasz Napierala 	}
1211009ea47eSEdward Tomasz Napierala 
1212a113ac05SEdward Tomasz Napierala 	if (oldconf->conf_kernel_port_on != newconf->conf_kernel_port_on) {
1213a113ac05SEdward Tomasz Napierala 		if (newconf->conf_kernel_port_on == true) {
1214a113ac05SEdward Tomasz Napierala 			log_debugx("enabling CTL iSCSI port");
1215a113ac05SEdward Tomasz Napierala 			error = kernel_port_on();
1216a113ac05SEdward Tomasz Napierala 			if (error != 0)
1217fe845cdbSEdward Tomasz Napierala 				log_errx(1, "failed to enable CTL iSCSI port; exiting");
1218a113ac05SEdward Tomasz Napierala 		} else {
1219a113ac05SEdward Tomasz Napierala 			error = kernel_port_off();
1220a113ac05SEdward Tomasz Napierala 			if (error != 0)
1221a113ac05SEdward Tomasz Napierala 				log_warnx("failed to disable CTL iSCSI port");
1222a113ac05SEdward Tomasz Napierala 		}
1223a113ac05SEdward Tomasz Napierala 	}
1224a113ac05SEdward Tomasz Napierala 
1225d5e316e5SEdward Tomasz Napierala 	/*
1226d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1227d5e316e5SEdward Tomasz Napierala 	 * 	the old lun or target into newconf, so that subsequent
1228*61a2a354SEdward Tomasz Napierala 	 * 	conf_apply() would try to remove them again.  That would
1229*61a2a354SEdward Tomasz Napierala 	 * 	be somewhat hairy, though, and lun deletion failures don't
1230*61a2a354SEdward Tomasz Napierala 	 * 	really happen, so leave it as it is for now.
1231d5e316e5SEdward Tomasz Napierala 	 */
1232009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1233009ea47eSEdward Tomasz Napierala 		/*
1234009ea47eSEdward Tomasz Napierala 		 * First, remove any targets present in the old configuration
1235009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1236009ea47eSEdward Tomasz Napierala 		 */
1237f7ae5bf8SEdward Tomasz Napierala 		newtarg = target_find(newconf, oldtarg->t_name);
1238009ea47eSEdward Tomasz Napierala 		if (newtarg == NULL) {
1239009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1240009ea47eSEdward Tomasz Napierala 			    tmplun) {
1241cadf3831SEdward Tomasz Napierala 				log_debugx("target %s not found in new "
1242cadf3831SEdward Tomasz Napierala 				    "configuration; removing its lun %d, "
1243009ea47eSEdward Tomasz Napierala 				    "backed by CTL lun %d",
1244f7ae5bf8SEdward Tomasz Napierala 				    oldtarg->t_name, oldlun->l_lun,
1245009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1246009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1247009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1248009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1249009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1250f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1251009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1252009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1253009ea47eSEdward Tomasz Napierala 				}
1254009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1255009ea47eSEdward Tomasz Napierala 			}
1256009ea47eSEdward Tomasz Napierala 			target_delete(oldtarg);
1257009ea47eSEdward Tomasz Napierala 			continue;
1258009ea47eSEdward Tomasz Napierala 		}
1259009ea47eSEdward Tomasz Napierala 
1260009ea47eSEdward Tomasz Napierala 		/*
1261009ea47eSEdward Tomasz Napierala 		 * Second, remove any LUNs present in the old target
1262009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1263009ea47eSEdward Tomasz Napierala 		 */
1264009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1265009ea47eSEdward Tomasz Napierala 			newlun = lun_find(newtarg, oldlun->l_lun);
1266009ea47eSEdward Tomasz Napierala 			if (newlun == NULL) {
1267009ea47eSEdward Tomasz Napierala 				log_debugx("lun %d, target %s, CTL lun %d "
1268cadf3831SEdward Tomasz Napierala 				    "not found in new configuration; "
1269f7ae5bf8SEdward Tomasz Napierala 				    "removing", oldlun->l_lun, oldtarg->t_name,
1270009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1271009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1272009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1273009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1274009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1275f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1276009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1277009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1278009ea47eSEdward Tomasz Napierala 				}
1279009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1280009ea47eSEdward Tomasz Napierala 				continue;
1281009ea47eSEdward Tomasz Napierala 			}
1282009ea47eSEdward Tomasz Napierala 
1283009ea47eSEdward Tomasz Napierala 			/*
1284009ea47eSEdward Tomasz Napierala 			 * Also remove the LUNs changed by more than size.
1285009ea47eSEdward Tomasz Napierala 			 */
1286009ea47eSEdward Tomasz Napierala 			changed = 0;
1287009ea47eSEdward Tomasz Napierala 			assert(oldlun->l_backend != NULL);
1288009ea47eSEdward Tomasz Napierala 			assert(newlun->l_backend != NULL);
1289009ea47eSEdward Tomasz Napierala 			if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1290009ea47eSEdward Tomasz Napierala 				log_debugx("backend for lun %d, target %s, "
1291009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1292f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1293009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1294009ea47eSEdward Tomasz Napierala 				changed = 1;
1295009ea47eSEdward Tomasz Napierala 			}
1296009ea47eSEdward Tomasz Napierala 			if (oldlun->l_blocksize != newlun->l_blocksize) {
1297009ea47eSEdward Tomasz Napierala 				log_debugx("blocksize for lun %d, target %s, "
1298009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1299f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1300009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1301009ea47eSEdward Tomasz Napierala 				changed = 1;
1302009ea47eSEdward Tomasz Napierala 			}
1303009ea47eSEdward Tomasz Napierala 			if (newlun->l_device_id != NULL &&
1304009ea47eSEdward Tomasz Napierala 			    (oldlun->l_device_id == NULL ||
1305009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1306009ea47eSEdward Tomasz Napierala 			     0)) {
1307009ea47eSEdward Tomasz Napierala 				log_debugx("device-id for lun %d, target %s, "
1308009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1309f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1310009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1311009ea47eSEdward Tomasz Napierala 				changed = 1;
1312009ea47eSEdward Tomasz Napierala 			}
1313009ea47eSEdward Tomasz Napierala 			if (newlun->l_path != NULL &&
1314009ea47eSEdward Tomasz Napierala 			    (oldlun->l_path == NULL ||
1315009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1316009ea47eSEdward Tomasz Napierala 				log_debugx("path for lun %d, target %s, "
1317009ea47eSEdward Tomasz Napierala 				    "CTL lun %d, changed; removing",
1318f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1319009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1320009ea47eSEdward Tomasz Napierala 				changed = 1;
1321009ea47eSEdward Tomasz Napierala 			}
1322009ea47eSEdward Tomasz Napierala 			if (newlun->l_serial != NULL &&
1323009ea47eSEdward Tomasz Napierala 			    (oldlun->l_serial == NULL ||
1324009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1325009ea47eSEdward Tomasz Napierala 				log_debugx("serial for lun %d, target %s, "
1326009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1327f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1328009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1329009ea47eSEdward Tomasz Napierala 				changed = 1;
1330009ea47eSEdward Tomasz Napierala 			}
1331009ea47eSEdward Tomasz Napierala 			if (changed) {
1332009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1333009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1334009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1335009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1336f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1337009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1338009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1339009ea47eSEdward Tomasz Napierala 				}
1340009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1341009ea47eSEdward Tomasz Napierala 				continue;
1342009ea47eSEdward Tomasz Napierala 			}
1343009ea47eSEdward Tomasz Napierala 
1344009ea47eSEdward Tomasz Napierala 			lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1345009ea47eSEdward Tomasz Napierala 		}
1346009ea47eSEdward Tomasz Napierala 	}
1347009ea47eSEdward Tomasz Napierala 
1348009ea47eSEdward Tomasz Napierala 	/*
1349009ea47eSEdward Tomasz Napierala 	 * Now add new targets or modify existing ones.
1350009ea47eSEdward Tomasz Napierala 	 */
1351009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1352f7ae5bf8SEdward Tomasz Napierala 		oldtarg = target_find(oldconf, newtarg->t_name);
1353009ea47eSEdward Tomasz Napierala 
1354d5e316e5SEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(newlun, &newtarg->t_luns, l_next, tmplun) {
1355009ea47eSEdward Tomasz Napierala 			if (oldtarg != NULL) {
1356009ea47eSEdward Tomasz Napierala 				oldlun = lun_find(oldtarg, newlun->l_lun);
1357009ea47eSEdward Tomasz Napierala 				if (oldlun != NULL) {
1358009ea47eSEdward Tomasz Napierala 					if (newlun->l_size != oldlun->l_size) {
1359009ea47eSEdward Tomasz Napierala 						log_debugx("resizing lun %d, "
1360009ea47eSEdward Tomasz Napierala 						    "target %s, CTL lun %d",
1361009ea47eSEdward Tomasz Napierala 						    newlun->l_lun,
1362f7ae5bf8SEdward Tomasz Napierala 						    newtarg->t_name,
1363009ea47eSEdward Tomasz Napierala 						    newlun->l_ctl_lun);
1364009ea47eSEdward Tomasz Napierala 						error =
1365009ea47eSEdward Tomasz Napierala 						    kernel_lun_resize(newlun);
1366009ea47eSEdward Tomasz Napierala 						if (error != 0) {
1367009ea47eSEdward Tomasz Napierala 							log_warnx("failed to "
1368009ea47eSEdward Tomasz Napierala 							    "resize lun %d, "
1369009ea47eSEdward Tomasz Napierala 							    "target %s, "
1370009ea47eSEdward Tomasz Napierala 							    "CTL lun %d",
1371009ea47eSEdward Tomasz Napierala 							    newlun->l_lun,
1372f7ae5bf8SEdward Tomasz Napierala 							    newtarg->t_name,
1373009ea47eSEdward Tomasz Napierala 							    newlun->l_lun);
1374009ea47eSEdward Tomasz Napierala 							cumulated_error++;
1375009ea47eSEdward Tomasz Napierala 						}
1376009ea47eSEdward Tomasz Napierala 					}
1377009ea47eSEdward Tomasz Napierala 					continue;
1378009ea47eSEdward Tomasz Napierala 				}
1379009ea47eSEdward Tomasz Napierala 			}
1380009ea47eSEdward Tomasz Napierala 			log_debugx("adding lun %d, target %s",
1381f7ae5bf8SEdward Tomasz Napierala 			    newlun->l_lun, newtarg->t_name);
1382009ea47eSEdward Tomasz Napierala 			error = kernel_lun_add(newlun);
1383009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1384009ea47eSEdward Tomasz Napierala 				log_warnx("failed to add lun %d, target %s",
1385f7ae5bf8SEdward Tomasz Napierala 				    newlun->l_lun, newtarg->t_name);
1386d5e316e5SEdward Tomasz Napierala 				lun_delete(newlun);
1387009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1388009ea47eSEdward Tomasz Napierala 			}
1389009ea47eSEdward Tomasz Napierala 		}
1390009ea47eSEdward Tomasz Napierala 	}
1391009ea47eSEdward Tomasz Napierala 
1392009ea47eSEdward Tomasz Napierala 	/*
1393009ea47eSEdward Tomasz Napierala 	 * Go through the new portals, opening the sockets as neccessary.
1394009ea47eSEdward Tomasz Napierala 	 */
1395009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1396009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
1397009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
1398009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
1399009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
1400009ea47eSEdward Tomasz Napierala 			continue;
1401009ea47eSEdward Tomasz Napierala 		}
1402009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1403009ea47eSEdward Tomasz Napierala 			/*
1404009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
1405009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
1406009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
1407009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
1408009ea47eSEdward Tomasz Napierala 			 */
1409009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1410009ea47eSEdward Tomasz Napierala 			    pg_next) {
1411009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1412009ea47eSEdward Tomasz Napierala 				    p_next) {
1413009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
1414009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
1415009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
1416009ea47eSEdward Tomasz Napierala 						newp->p_socket =
1417009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
1418009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
1419009ea47eSEdward Tomasz Napierala 						break;
1420009ea47eSEdward Tomasz Napierala 					}
1421009ea47eSEdward Tomasz Napierala 				}
1422009ea47eSEdward Tomasz Napierala 			}
1423009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
1424009ea47eSEdward Tomasz Napierala 				/*
1425009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
1426009ea47eSEdward Tomasz Napierala 				 */
1427009ea47eSEdward Tomasz Napierala 				continue;
1428009ea47eSEdward Tomasz Napierala 			}
1429009ea47eSEdward Tomasz Napierala 
1430009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1431ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
14328cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
14338cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
14348cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
14358cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
14368cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
14378cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
14388cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
1439ba3a2d31SEdward Tomasz Napierala 				continue;
1440ba3a2d31SEdward Tomasz Napierala 			}
1441ba3a2d31SEdward Tomasz Napierala #endif
1442ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
1443009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
1444009ea47eSEdward Tomasz Napierala 
1445009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
1446009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
1447009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
1448009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
1449009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
1450009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
1451009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
1452009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1453009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1454009ea47eSEdward Tomasz Napierala 				continue;
1455009ea47eSEdward Tomasz Napierala 			}
1456009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
1457009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
1458009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1459009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
1460009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
1461009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1462009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1463009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1464009ea47eSEdward Tomasz Napierala 				continue;
1465009ea47eSEdward Tomasz Napierala 			}
1466009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
1467009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
1468009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1469009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
1470009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1471009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1472009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1473009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1474009ea47eSEdward Tomasz Napierala 				continue;
1475009ea47eSEdward Tomasz Napierala 			}
1476009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
1477009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1478009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
1479009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1480009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1481009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1482009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1483009ea47eSEdward Tomasz Napierala 				continue;
1484009ea47eSEdward Tomasz Napierala 			}
1485009ea47eSEdward Tomasz Napierala 		}
1486009ea47eSEdward Tomasz Napierala 	}
1487009ea47eSEdward Tomasz Napierala 
1488009ea47eSEdward Tomasz Napierala 	/*
1489009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
1490009ea47eSEdward Tomasz Napierala 	 */
1491009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1492009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1493009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
1494009ea47eSEdward Tomasz Napierala 				continue;
1495009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
1496009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
1497009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
1498009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
1499009ea47eSEdward Tomasz Napierala 		}
1500009ea47eSEdward Tomasz Napierala 	}
1501009ea47eSEdward Tomasz Napierala 
1502009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
1503009ea47eSEdward Tomasz Napierala }
1504009ea47eSEdward Tomasz Napierala 
1505009ea47eSEdward Tomasz Napierala bool
1506009ea47eSEdward Tomasz Napierala timed_out(void)
1507009ea47eSEdward Tomasz Napierala {
1508009ea47eSEdward Tomasz Napierala 
1509009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
1510009ea47eSEdward Tomasz Napierala }
1511009ea47eSEdward Tomasz Napierala 
1512009ea47eSEdward Tomasz Napierala static void
1513009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused)
1514009ea47eSEdward Tomasz Napierala {
1515009ea47eSEdward Tomasz Napierala 	/*
1516009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
1517009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
1518009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
1519009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
1520009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
1521009ea47eSEdward Tomasz Napierala 	 */
1522009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
1523009ea47eSEdward Tomasz Napierala 		/*
1524009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
1525009ea47eSEdward Tomasz Napierala 		 */
1526009ea47eSEdward Tomasz Napierala 		_exit(2);
1527009ea47eSEdward Tomasz Napierala 	}
1528009ea47eSEdward Tomasz Napierala 
1529009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
1530009ea47eSEdward Tomasz Napierala }
1531009ea47eSEdward Tomasz Napierala 
1532009ea47eSEdward Tomasz Napierala static void
1533009ea47eSEdward Tomasz Napierala set_timeout(const struct conf *conf)
1534009ea47eSEdward Tomasz Napierala {
1535009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
1536009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
1537009ea47eSEdward Tomasz Napierala 	int error;
1538009ea47eSEdward Tomasz Napierala 
1539009ea47eSEdward Tomasz Napierala 	if (conf->conf_timeout <= 0) {
1540009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
1541009ea47eSEdward Tomasz Napierala 		return;
1542009ea47eSEdward Tomasz Napierala 	}
1543009ea47eSEdward Tomasz Napierala 
1544009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
1545009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigalrm_handler;
1546009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
1547009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
1548009ea47eSEdward Tomasz Napierala 	if (error != 0)
1549009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1550009ea47eSEdward Tomasz Napierala 
1551009ea47eSEdward Tomasz Napierala 	/*
1552009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
1553009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
1554009ea47eSEdward Tomasz Napierala 	 */
1555009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
1556009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
1557009ea47eSEdward Tomasz Napierala 	itv.it_value.tv_sec = conf->conf_timeout;
1558009ea47eSEdward Tomasz Napierala 
1559009ea47eSEdward Tomasz Napierala 	log_debugx("setting session timeout to %d seconds",
1560009ea47eSEdward Tomasz Napierala 	    conf->conf_timeout);
1561009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
1562009ea47eSEdward Tomasz Napierala 	if (error != 0)
1563009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
1564009ea47eSEdward Tomasz Napierala }
1565009ea47eSEdward Tomasz Napierala 
1566009ea47eSEdward Tomasz Napierala static int
1567009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
1568009ea47eSEdward Tomasz Napierala {
1569009ea47eSEdward Tomasz Napierala 	pid_t pid;
1570009ea47eSEdward Tomasz Napierala 	int status;
1571009ea47eSEdward Tomasz Napierala 	int num = 0;
1572009ea47eSEdward Tomasz Napierala 
1573009ea47eSEdward Tomasz Napierala 	for (;;) {
1574009ea47eSEdward Tomasz Napierala 		/*
1575009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
1576009ea47eSEdward Tomasz Napierala 		 */
1577009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
1578009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
1579009ea47eSEdward Tomasz Napierala 		else
1580009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
1581009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
1582009ea47eSEdward Tomasz Napierala 			break;
1583009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
1584009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
1585009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
1586009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
1587009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
1588009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
1589009ea47eSEdward Tomasz Napierala 		} else {
1590009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
1591009ea47eSEdward Tomasz Napierala 		}
1592009ea47eSEdward Tomasz Napierala 		num++;
1593009ea47eSEdward Tomasz Napierala 	}
1594009ea47eSEdward Tomasz Napierala 
1595009ea47eSEdward Tomasz Napierala 	return (num);
1596009ea47eSEdward Tomasz Napierala }
1597009ea47eSEdward Tomasz Napierala 
1598009ea47eSEdward Tomasz Napierala static void
15998eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
16008eab95d6SEdward Tomasz Napierala     const struct sockaddr *client_sa, socklen_t client_salen, bool dont_fork)
1601009ea47eSEdward Tomasz Napierala {
1602009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1603009ea47eSEdward Tomasz Napierala 	int error;
1604009ea47eSEdward Tomasz Napierala 	pid_t pid;
1605009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
1606009ea47eSEdward Tomasz Napierala 	struct conf *conf;
1607009ea47eSEdward Tomasz Napierala 
1608009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
1609009ea47eSEdward Tomasz Napierala 
1610009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
1611009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
1612009ea47eSEdward Tomasz Napierala 	} else {
1613009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
1614009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
1615009ea47eSEdward Tomasz Napierala 
1616009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1617009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
1618009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
1619009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
1620009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
1621009ea47eSEdward Tomasz Napierala 		}
1622009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
1623009ea47eSEdward Tomasz Napierala 		    nchildren);
1624009ea47eSEdward Tomasz Napierala 		nchildren++;
1625009ea47eSEdward Tomasz Napierala 		pid = fork();
1626009ea47eSEdward Tomasz Napierala 		if (pid < 0)
1627009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
1628009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
1629009ea47eSEdward Tomasz Napierala 			close(fd);
1630009ea47eSEdward Tomasz Napierala 			return;
1631009ea47eSEdward Tomasz Napierala 		}
1632009ea47eSEdward Tomasz Napierala 	}
1633009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
1634009ea47eSEdward Tomasz Napierala 
16358eab95d6SEdward Tomasz Napierala 	error = getnameinfo(client_sa, client_salen,
1636009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
1637009ea47eSEdward Tomasz Napierala 	if (error != 0)
16389a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
1639009ea47eSEdward Tomasz Napierala 
1640009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
1641009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
1642009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
1643009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
1644009ea47eSEdward Tomasz Napierala 
1645009ea47eSEdward Tomasz Napierala 	conn = connection_new(portal, fd, host);
1646009ea47eSEdward Tomasz Napierala 	set_timeout(conf);
1647009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
1648009ea47eSEdward Tomasz Napierala 	login(conn);
1649009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1650009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
1651009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
1652009ea47eSEdward Tomasz Napierala 	} else {
1653009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
1654009ea47eSEdward Tomasz Napierala 		discovery(conn);
1655009ea47eSEdward Tomasz Napierala 	}
1656009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
1657009ea47eSEdward Tomasz Napierala 	exit(0);
1658009ea47eSEdward Tomasz Napierala }
1659009ea47eSEdward Tomasz Napierala 
1660009ea47eSEdward Tomasz Napierala static int
1661009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
1662009ea47eSEdward Tomasz Napierala {
1663009ea47eSEdward Tomasz Napierala 
1664009ea47eSEdward Tomasz Napierala 	/*
1665009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
1666009ea47eSEdward Tomasz Napierala 	 */
1667009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
1668009ea47eSEdward Tomasz Napierala 		return (nfds);
1669009ea47eSEdward Tomasz Napierala 
1670009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
1671009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
1672009ea47eSEdward Tomasz Napierala 		nfds = fd;
1673009ea47eSEdward Tomasz Napierala 	return (nfds);
1674009ea47eSEdward Tomasz Napierala }
1675009ea47eSEdward Tomasz Napierala 
1676009ea47eSEdward Tomasz Napierala static void
1677009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
1678009ea47eSEdward Tomasz Napierala {
1679009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1680009ea47eSEdward Tomasz Napierala 	struct portal *portal;
16819a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
16829a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
1683009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1684009ea47eSEdward Tomasz Napierala 	int connection_id;
16858cab2ed4SEdward Tomasz Napierala 	int portal_id;
1686ba3a2d31SEdward Tomasz Napierala #endif
1687009ea47eSEdward Tomasz Napierala 	fd_set fdset;
1688009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
1689009ea47eSEdward Tomasz Napierala 
1690009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
1691009ea47eSEdward Tomasz Napierala 
1692009ea47eSEdward Tomasz Napierala 	for (;;) {
1693009ea47eSEdward Tomasz Napierala 		if (sighup_received || sigterm_received)
1694009ea47eSEdward Tomasz Napierala 			return;
1695009ea47eSEdward Tomasz Napierala 
1696009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1697ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
16988eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
16998eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
17008eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
1701009ea47eSEdward Tomasz Napierala 
17028cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
17038cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
17048cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
17058cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
17068cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
17078cab2ed4SEdward Tomasz Napierala 						goto found;
17088cab2ed4SEdward Tomasz Napierala 					}
17098cab2ed4SEdward Tomasz Napierala 				}
17108cab2ed4SEdward Tomasz Napierala 			}
1711009ea47eSEdward Tomasz Napierala 
17128cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
17138cab2ed4SEdward Tomasz Napierala 			    portal_id);
17148cab2ed4SEdward Tomasz Napierala 
17158cab2ed4SEdward Tomasz Napierala found:
17168eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
17178eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, client_salen,
17188eab95d6SEdward Tomasz Napierala 			    dont_fork);
1719ba3a2d31SEdward Tomasz Napierala 		} else {
1720ba3a2d31SEdward Tomasz Napierala #endif
1721ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
1722ba3a2d31SEdward Tomasz Napierala 
1723009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
1724009ea47eSEdward Tomasz Napierala 			nfds = 0;
1725009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1726009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1727009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
1728009ea47eSEdward Tomasz Napierala 			}
1729009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
1730009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
1731009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
1732009ea47eSEdward Tomasz Napierala 					return;
1733009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
1734009ea47eSEdward Tomasz Napierala 			}
1735009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1736009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
1737009ea47eSEdward Tomasz Napierala 					if (!FD_ISSET(portal->p_socket, &fdset))
1738009ea47eSEdward Tomasz Napierala 						continue;
17399a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
17409a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
17419a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
17429a95cfffSEdward Tomasz Napierala 					    &client_salen);
1743009ea47eSEdward Tomasz Napierala 					if (client_fd < 0)
1744009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
17459a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
17468eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
17478eab95d6SEdward Tomasz Napierala 					    client_salen, dont_fork);
1748009ea47eSEdward Tomasz Napierala 					break;
1749009ea47eSEdward Tomasz Napierala 				}
1750009ea47eSEdward Tomasz Napierala 			}
1751ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1752ba3a2d31SEdward Tomasz Napierala 		}
1753ba3a2d31SEdward Tomasz Napierala #endif
1754009ea47eSEdward Tomasz Napierala 	}
1755009ea47eSEdward Tomasz Napierala }
1756009ea47eSEdward Tomasz Napierala 
1757009ea47eSEdward Tomasz Napierala static void
1758009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
1759009ea47eSEdward Tomasz Napierala {
1760009ea47eSEdward Tomasz Napierala 
1761009ea47eSEdward Tomasz Napierala 	sighup_received = true;
1762009ea47eSEdward Tomasz Napierala }
1763009ea47eSEdward Tomasz Napierala 
1764009ea47eSEdward Tomasz Napierala static void
1765009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
1766009ea47eSEdward Tomasz Napierala {
1767009ea47eSEdward Tomasz Napierala 
1768009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
1769009ea47eSEdward Tomasz Napierala }
1770009ea47eSEdward Tomasz Napierala 
1771009ea47eSEdward Tomasz Napierala static void
177270b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
177370b939ecSEdward Tomasz Napierala {
177470b939ecSEdward Tomasz Napierala 
177570b939ecSEdward Tomasz Napierala 	/*
177670b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
177770b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
177870b939ecSEdward Tomasz Napierala 	 * wait_for_children().
177970b939ecSEdward Tomasz Napierala 	 */
178070b939ecSEdward Tomasz Napierala }
178170b939ecSEdward Tomasz Napierala 
178270b939ecSEdward Tomasz Napierala static void
1783009ea47eSEdward Tomasz Napierala register_signals(void)
1784009ea47eSEdward Tomasz Napierala {
1785009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
1786009ea47eSEdward Tomasz Napierala 	int error;
1787009ea47eSEdward Tomasz Napierala 
1788009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
1789009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
1790009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
1791009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
1792009ea47eSEdward Tomasz Napierala 	if (error != 0)
1793009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1794009ea47eSEdward Tomasz Napierala 
1795009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
1796009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
1797009ea47eSEdward Tomasz Napierala 	if (error != 0)
1798009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1799009ea47eSEdward Tomasz Napierala 
1800009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
1801009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
1802009ea47eSEdward Tomasz Napierala 	if (error != 0)
1803009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
180470b939ecSEdward Tomasz Napierala 
180570b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
180670b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
180770b939ecSEdward Tomasz Napierala 	if (error != 0)
180870b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
1809009ea47eSEdward Tomasz Napierala }
1810009ea47eSEdward Tomasz Napierala 
1811009ea47eSEdward Tomasz Napierala int
1812009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
1813009ea47eSEdward Tomasz Napierala {
1814009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
1815009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
1816009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
1817009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
1818009ea47eSEdward Tomasz Napierala 
1819ba3a2d31SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "df:R")) != -1) {
1820009ea47eSEdward Tomasz Napierala 		switch (ch) {
1821009ea47eSEdward Tomasz Napierala 		case 'd':
1822009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
1823009ea47eSEdward Tomasz Napierala 			debug++;
1824009ea47eSEdward Tomasz Napierala 			break;
1825009ea47eSEdward Tomasz Napierala 		case 'f':
1826009ea47eSEdward Tomasz Napierala 			config_path = optarg;
1827009ea47eSEdward Tomasz Napierala 			break;
1828ba3a2d31SEdward Tomasz Napierala 		case 'R':
1829ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
1830ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
1831ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
1832ba3a2d31SEdward Tomasz Napierala #endif
1833ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
1834ba3a2d31SEdward Tomasz Napierala 			break;
1835009ea47eSEdward Tomasz Napierala 		case '?':
1836009ea47eSEdward Tomasz Napierala 		default:
1837009ea47eSEdward Tomasz Napierala 			usage();
1838009ea47eSEdward Tomasz Napierala 		}
1839009ea47eSEdward Tomasz Napierala 	}
1840009ea47eSEdward Tomasz Napierala 	argc -= optind;
1841009ea47eSEdward Tomasz Napierala 	if (argc != 0)
1842009ea47eSEdward Tomasz Napierala 		usage();
1843009ea47eSEdward Tomasz Napierala 
1844009ea47eSEdward Tomasz Napierala 	log_init(debug);
1845009ea47eSEdward Tomasz Napierala 	kernel_init();
1846009ea47eSEdward Tomasz Napierala 
1847009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
1848009ea47eSEdward Tomasz Napierala 	newconf = conf_new_from_file(config_path);
1849009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
1850fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
1851009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
1852009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
1853009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
1854009ea47eSEdward Tomasz Napierala 	}
1855009ea47eSEdward Tomasz Napierala 
1856009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
1857009ea47eSEdward Tomasz Napierala 	if (error != 0)
1858fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
1859fe845cdbSEdward Tomasz Napierala 
1860009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
1861009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
1862009ea47eSEdward Tomasz Napierala 
1863009ea47eSEdward Tomasz Napierala 	register_signals();
1864009ea47eSEdward Tomasz Napierala 
18655d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
18665d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
18675d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
18685d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
18695d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
18705d5a95f1SEdward Tomasz Napierala 			exit(1);
18715d5a95f1SEdward Tomasz Napierala 		}
18725d5a95f1SEdward Tomasz Napierala 	}
18735d5a95f1SEdward Tomasz Napierala 
1874009ea47eSEdward Tomasz Napierala 	for (;;) {
1875009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
1876009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
1877009ea47eSEdward Tomasz Napierala 			sighup_received = false;
1878009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
1879009ea47eSEdward Tomasz Napierala 			tmpconf = conf_new_from_file(config_path);
1880009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
1881009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
1882009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
1883009ea47eSEdward Tomasz Napierala 			} else {
1884009ea47eSEdward Tomasz Napierala 				if (debug > 0)
1885009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
1886009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
1887009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
1888009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
1889009ea47eSEdward Tomasz Napierala 				if (error != 0)
1890009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
1891009ea47eSEdward Tomasz Napierala 					    "configuration");
1892009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
1893009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
1894009ea47eSEdward Tomasz Napierala 			}
1895009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
1896009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
1897009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
1898009ea47eSEdward Tomasz Napierala 
1899009ea47eSEdward Tomasz Napierala 			log_debugx("disabling CTL iSCSI port "
1900009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
1901009ea47eSEdward Tomasz Napierala 
1902009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
1903009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
1904009ea47eSEdward Tomasz Napierala 			if (debug > 0)
1905009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
1906009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
1907009ea47eSEdward Tomasz Napierala 			if (error != 0)
1908009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
1909009ea47eSEdward Tomasz Napierala 
1910009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
1911009ea47eSEdward Tomasz Napierala 			exit(0);
1912009ea47eSEdward Tomasz Napierala 		} else {
1913009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
1914009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
1915009ea47eSEdward Tomasz Napierala 		}
1916009ea47eSEdward Tomasz Napierala 	}
1917009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
1918009ea47eSEdward Tomasz Napierala }
1919