xref: /freebsd/usr.sbin/ctld/ctld.c (revision 8cf98331b0affb6dcf36a725656c8a1ab4a2e3f1)
1009ea47eSEdward Tomasz Napierala /*-
2009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
3009ea47eSEdward Tomasz Napierala  * All rights reserved.
4009ea47eSEdward Tomasz Napierala  *
5009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
7009ea47eSEdward Tomasz Napierala  *
8009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10009ea47eSEdward Tomasz Napierala  * are met:
11009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16009ea47eSEdward Tomasz Napierala  *
17009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
28009ea47eSEdward Tomasz Napierala  *
29009ea47eSEdward Tomasz Napierala  */
30009ea47eSEdward Tomasz Napierala 
31b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h>
32b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$");
33b7a65e39SEdward Tomasz Napierala 
34009ea47eSEdward Tomasz Napierala #include <sys/types.h>
35009ea47eSEdward Tomasz Napierala #include <sys/time.h>
36009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
37009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
38009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
39073edb1cSAlexander Motin #include <arpa/inet.h>
40009ea47eSEdward Tomasz Napierala #include <assert.h>
41009ea47eSEdward Tomasz Napierala #include <ctype.h>
42009ea47eSEdward Tomasz Napierala #include <errno.h>
43009ea47eSEdward Tomasz Napierala #include <netdb.h>
44009ea47eSEdward Tomasz Napierala #include <signal.h>
45009ea47eSEdward Tomasz Napierala #include <stdbool.h>
46009ea47eSEdward Tomasz Napierala #include <stdio.h>
47009ea47eSEdward Tomasz Napierala #include <stdint.h>
48009ea47eSEdward Tomasz Napierala #include <stdlib.h>
49009ea47eSEdward Tomasz Napierala #include <string.h>
50009ea47eSEdward Tomasz Napierala #include <unistd.h>
51009ea47eSEdward Tomasz Napierala 
52009ea47eSEdward Tomasz Napierala #include "ctld.h"
53009ea47eSEdward Tomasz Napierala 
54ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false;
55ba3a2d31SEdward Tomasz Napierala 
56009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
57009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
58009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
59009ea47eSEdward Tomasz Napierala 
60009ea47eSEdward Tomasz Napierala static int nchildren = 0;
61009ea47eSEdward Tomasz Napierala 
62009ea47eSEdward Tomasz Napierala static void
63009ea47eSEdward Tomasz Napierala usage(void)
64009ea47eSEdward Tomasz Napierala {
65009ea47eSEdward Tomasz Napierala 
66009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
67009ea47eSEdward Tomasz Napierala 	exit(1);
68009ea47eSEdward Tomasz Napierala }
69009ea47eSEdward Tomasz Napierala 
70009ea47eSEdward Tomasz Napierala char *
71009ea47eSEdward Tomasz Napierala checked_strdup(const char *s)
72009ea47eSEdward Tomasz Napierala {
73009ea47eSEdward Tomasz Napierala 	char *c;
74009ea47eSEdward Tomasz Napierala 
75009ea47eSEdward Tomasz Napierala 	c = strdup(s);
76009ea47eSEdward Tomasz Napierala 	if (c == NULL)
77009ea47eSEdward Tomasz Napierala 		log_err(1, "strdup");
78009ea47eSEdward Tomasz Napierala 	return (c);
79009ea47eSEdward Tomasz Napierala }
80009ea47eSEdward Tomasz Napierala 
81009ea47eSEdward Tomasz Napierala struct conf *
82009ea47eSEdward Tomasz Napierala conf_new(void)
83009ea47eSEdward Tomasz Napierala {
84009ea47eSEdward Tomasz Napierala 	struct conf *conf;
85009ea47eSEdward Tomasz Napierala 
86009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
87009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
88009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
89009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
90009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
91009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
92009ea47eSEdward Tomasz Napierala 
93009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
94009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
95009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
96009ea47eSEdward Tomasz Napierala 
97009ea47eSEdward Tomasz Napierala 	return (conf);
98009ea47eSEdward Tomasz Napierala }
99009ea47eSEdward Tomasz Napierala 
100009ea47eSEdward Tomasz Napierala void
101009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
102009ea47eSEdward Tomasz Napierala {
103009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
104009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
105009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
106009ea47eSEdward Tomasz Napierala 
107009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
108009ea47eSEdward Tomasz Napierala 
109009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
110009ea47eSEdward Tomasz Napierala 		target_delete(targ);
111009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
112009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
113009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
114009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
115009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
116009ea47eSEdward Tomasz Napierala 	free(conf);
117009ea47eSEdward Tomasz Napierala }
118009ea47eSEdward Tomasz Napierala 
119009ea47eSEdward Tomasz Napierala static struct auth *
120009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
121009ea47eSEdward Tomasz Napierala {
122009ea47eSEdward Tomasz Napierala 	struct auth *auth;
123009ea47eSEdward Tomasz Napierala 
124009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
125009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
126009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
127009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
128009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
129009ea47eSEdward Tomasz Napierala 	return (auth);
130009ea47eSEdward Tomasz Napierala }
131009ea47eSEdward Tomasz Napierala 
132009ea47eSEdward Tomasz Napierala static void
133009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
134009ea47eSEdward Tomasz Napierala {
135009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
136009ea47eSEdward Tomasz Napierala 
137009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
138009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
139009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
140009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
141009ea47eSEdward Tomasz Napierala 	free(auth);
142009ea47eSEdward Tomasz Napierala }
143009ea47eSEdward Tomasz Napierala 
144009ea47eSEdward Tomasz Napierala const struct auth *
145d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
146009ea47eSEdward Tomasz Napierala {
147009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
148009ea47eSEdward Tomasz Napierala 
149009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
150009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
151009ea47eSEdward Tomasz Napierala 			return (auth);
152009ea47eSEdward Tomasz Napierala 	}
153009ea47eSEdward Tomasz Napierala 
154009ea47eSEdward Tomasz Napierala 	return (NULL);
155009ea47eSEdward Tomasz Napierala }
156009ea47eSEdward Tomasz Napierala 
1571658628dSEdward Tomasz Napierala static void
1581658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1591658628dSEdward Tomasz Napierala {
1601658628dSEdward Tomasz Napierala 	size_t len;
1611658628dSEdward Tomasz Napierala 
1621658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1631658628dSEdward Tomasz Napierala 	if (len > 16) {
1641658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1651658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1661658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1671658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1681658628dSEdward Tomasz Napierala 		else
1691658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1701658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1711658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
172f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1731658628dSEdward Tomasz Napierala 	}
1741658628dSEdward Tomasz Napierala 	if (len < 12) {
1751658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1761658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1771658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1781658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1791658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1801658628dSEdward Tomasz Napierala 		else
1811658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1821658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 16 characters "
1831658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
184f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1851658628dSEdward Tomasz Napierala 	}
1861658628dSEdward Tomasz Napierala 
1871658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
1881658628dSEdward Tomasz Napierala 		len = strlen(auth->a_secret);
1891658628dSEdward Tomasz Napierala 		if (len > 16) {
1901658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
1911658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1921658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
1931658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
1941658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
1951658628dSEdward Tomasz Napierala 			else
1961658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1971658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
1981658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
1991658628dSEdward Tomasz Napierala 				    auth->a_user,
200f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2011658628dSEdward Tomasz Napierala 		}
2021658628dSEdward Tomasz Napierala 		if (len < 12) {
2031658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2041658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2051658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2061658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2071658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2081658628dSEdward Tomasz Napierala 			else
2091658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2101658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2111658628dSEdward Tomasz Napierala 				    "at least 16 characters long",
2121658628dSEdward Tomasz Napierala 				    auth->a_user,
213f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2141658628dSEdward Tomasz Napierala 		}
2151658628dSEdward Tomasz Napierala 	}
2161658628dSEdward Tomasz Napierala }
2171658628dSEdward Tomasz Napierala 
2181658628dSEdward Tomasz Napierala const struct auth *
2191658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2201658628dSEdward Tomasz Napierala     const char *secret)
2211658628dSEdward Tomasz Napierala {
2221658628dSEdward Tomasz Napierala 	struct auth *auth;
2231658628dSEdward Tomasz Napierala 
2241658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2251658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2261658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2271658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2281658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2291658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2301658628dSEdward Tomasz Napierala 		else
2311658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2321658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
233f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2341658628dSEdward Tomasz Napierala 		return (NULL);
2351658628dSEdward Tomasz Napierala 	}
2361658628dSEdward Tomasz Napierala 
2371658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2381658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2391658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2401658628dSEdward Tomasz Napierala 
2411658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2421658628dSEdward Tomasz Napierala 
2431658628dSEdward Tomasz Napierala 	return (auth);
2441658628dSEdward Tomasz Napierala }
2451658628dSEdward Tomasz Napierala 
2461658628dSEdward Tomasz Napierala const struct auth *
2471658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2481658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2491658628dSEdward Tomasz Napierala {
2501658628dSEdward Tomasz Napierala 	struct auth *auth;
2511658628dSEdward Tomasz Napierala 
2521658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2531658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2541658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2551658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2561658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2571658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2581658628dSEdward Tomasz Napierala 			    ag->ag_name);
2591658628dSEdward Tomasz Napierala 		else
2601658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2611658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
262f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2631658628dSEdward Tomasz Napierala 		return (NULL);
2641658628dSEdward Tomasz Napierala 	}
2651658628dSEdward Tomasz Napierala 
2661658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2671658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2681658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2691658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2701658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2711658628dSEdward Tomasz Napierala 
2721658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2731658628dSEdward Tomasz Napierala 
2741658628dSEdward Tomasz Napierala 	return (auth);
2751658628dSEdward Tomasz Napierala }
2761658628dSEdward Tomasz Napierala 
2778cb2e958SEdward Tomasz Napierala const struct auth_name *
2788cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2798cb2e958SEdward Tomasz Napierala {
2808cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
2818cb2e958SEdward Tomasz Napierala 
2828cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
2838cb2e958SEdward Tomasz Napierala 	if (an == NULL)
2848cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
2858cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
2868cb2e958SEdward Tomasz Napierala 	an->an_initator_name = checked_strdup(name);
2878cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
2888cb2e958SEdward Tomasz Napierala 	return (an);
2898cb2e958SEdward Tomasz Napierala }
2908cb2e958SEdward Tomasz Napierala 
2918cb2e958SEdward Tomasz Napierala static void
2928cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
2938cb2e958SEdward Tomasz Napierala {
2948cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
2958cb2e958SEdward Tomasz Napierala 
2968cb2e958SEdward Tomasz Napierala 	free(an->an_initator_name);
2978cb2e958SEdward Tomasz Napierala 	free(an);
2988cb2e958SEdward Tomasz Napierala }
2998cb2e958SEdward Tomasz Napierala 
3008cb2e958SEdward Tomasz Napierala bool
3018cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
3028cb2e958SEdward Tomasz Napierala {
3038cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3048cb2e958SEdward Tomasz Napierala 		return (false);
3058cb2e958SEdward Tomasz Napierala 	return (true);
3068cb2e958SEdward Tomasz Napierala }
3078cb2e958SEdward Tomasz Napierala 
3088cb2e958SEdward Tomasz Napierala const struct auth_name *
3098cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3108cb2e958SEdward Tomasz Napierala {
3118cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3128cb2e958SEdward Tomasz Napierala 
3138cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
3148cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_name->an_initator_name, name) == 0)
3158cb2e958SEdward Tomasz Napierala 			return (auth_name);
3168cb2e958SEdward Tomasz Napierala 	}
3178cb2e958SEdward Tomasz Napierala 
3188cb2e958SEdward Tomasz Napierala 	return (NULL);
3198cb2e958SEdward Tomasz Napierala }
3208cb2e958SEdward Tomasz Napierala 
3218cb2e958SEdward Tomasz Napierala const struct auth_portal *
3228cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3238cb2e958SEdward Tomasz Napierala {
3248cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
325073edb1cSAlexander Motin 	char *net, *mask, *str, *tmp;
326073edb1cSAlexander Motin 	int len, dm, m;
3278cb2e958SEdward Tomasz Napierala 
3288cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3298cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3308cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3318cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
3328cb2e958SEdward Tomasz Napierala 	ap->ap_initator_portal = checked_strdup(portal);
333073edb1cSAlexander Motin 	mask = str = checked_strdup(portal);
334073edb1cSAlexander Motin 	net = strsep(&mask, "/");
335073edb1cSAlexander Motin 	if (net[0] == '[')
336073edb1cSAlexander Motin 		net++;
337073edb1cSAlexander Motin 	len = strlen(net);
338073edb1cSAlexander Motin 	if (len == 0)
339073edb1cSAlexander Motin 		goto error;
340073edb1cSAlexander Motin 	if (net[len - 1] == ']')
341073edb1cSAlexander Motin 		net[len - 1] = 0;
342073edb1cSAlexander Motin 	if (strchr(net, ':') != NULL) {
343073edb1cSAlexander Motin 		struct sockaddr_in6 *sin6 =
344073edb1cSAlexander Motin 		    (struct sockaddr_in6 *)&ap->ap_sa;
345073edb1cSAlexander Motin 
346073edb1cSAlexander Motin 		sin6->sin6_len = sizeof(*sin6);
347073edb1cSAlexander Motin 		sin6->sin6_family = AF_INET6;
348073edb1cSAlexander Motin 		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
349073edb1cSAlexander Motin 			goto error;
350073edb1cSAlexander Motin 		dm = 128;
351073edb1cSAlexander Motin 	} else {
352073edb1cSAlexander Motin 		struct sockaddr_in *sin =
353073edb1cSAlexander Motin 		    (struct sockaddr_in *)&ap->ap_sa;
354073edb1cSAlexander Motin 
355073edb1cSAlexander Motin 		sin->sin_len = sizeof(*sin);
356073edb1cSAlexander Motin 		sin->sin_family = AF_INET;
357073edb1cSAlexander Motin 		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
358073edb1cSAlexander Motin 			goto error;
359073edb1cSAlexander Motin 		dm = 32;
360073edb1cSAlexander Motin 	}
361073edb1cSAlexander Motin 	if (mask != NULL) {
362073edb1cSAlexander Motin 		m = strtol(mask, &tmp, 0);
363073edb1cSAlexander Motin 		if (m < 0 || m > dm || tmp[0] != 0)
364073edb1cSAlexander Motin 			goto error;
365073edb1cSAlexander Motin 	} else
366073edb1cSAlexander Motin 		m = dm;
367073edb1cSAlexander Motin 	ap->ap_mask = m;
368073edb1cSAlexander Motin 	free(str);
3698cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
3708cb2e958SEdward Tomasz Napierala 	return (ap);
371073edb1cSAlexander Motin 
372073edb1cSAlexander Motin error:
373073edb1cSAlexander Motin 	log_errx(1, "Incorrect initiator portal '%s'", portal);
374073edb1cSAlexander Motin 	return (NULL);
3758cb2e958SEdward Tomasz Napierala }
3768cb2e958SEdward Tomasz Napierala 
3778cb2e958SEdward Tomasz Napierala static void
3788cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
3798cb2e958SEdward Tomasz Napierala {
3808cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
3818cb2e958SEdward Tomasz Napierala 
3828cb2e958SEdward Tomasz Napierala 	free(ap->ap_initator_portal);
3838cb2e958SEdward Tomasz Napierala 	free(ap);
3848cb2e958SEdward Tomasz Napierala }
3858cb2e958SEdward Tomasz Napierala 
3868cb2e958SEdward Tomasz Napierala bool
3878cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
3888cb2e958SEdward Tomasz Napierala {
3898cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
3908cb2e958SEdward Tomasz Napierala 		return (false);
3918cb2e958SEdward Tomasz Napierala 	return (true);
3928cb2e958SEdward Tomasz Napierala }
3938cb2e958SEdward Tomasz Napierala 
3948cb2e958SEdward Tomasz Napierala const struct auth_portal *
395073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
3968cb2e958SEdward Tomasz Napierala {
397073edb1cSAlexander Motin 	const struct auth_portal *ap;
3985e46a066SAlexander Motin 	const uint8_t *a, *b;
399073edb1cSAlexander Motin 	int i;
4005e46a066SAlexander Motin 	uint8_t bmask;
4018cb2e958SEdward Tomasz Napierala 
402073edb1cSAlexander Motin 	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
403073edb1cSAlexander Motin 		if (ap->ap_sa.ss_family != ss->ss_family)
404073edb1cSAlexander Motin 			continue;
405073edb1cSAlexander Motin 		if (ss->ss_family == AF_INET) {
4065e46a066SAlexander Motin 			a = (const uint8_t *)
4075e46a066SAlexander Motin 			    &((const struct sockaddr_in *)ss)->sin_addr;
4085e46a066SAlexander Motin 			b = (const uint8_t *)
4095e46a066SAlexander Motin 			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
410073edb1cSAlexander Motin 		} else {
4115e46a066SAlexander Motin 			a = (const uint8_t *)
4125e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
4135e46a066SAlexander Motin 			b = (const uint8_t *)
4145e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
415073edb1cSAlexander Motin 		}
416073edb1cSAlexander Motin 		for (i = 0; i < ap->ap_mask / 8; i++) {
417073edb1cSAlexander Motin 			if (a[i] != b[i])
418073edb1cSAlexander Motin 				goto next;
419073edb1cSAlexander Motin 		}
420073edb1cSAlexander Motin 		if (ap->ap_mask % 8) {
421073edb1cSAlexander Motin 			bmask = 0xff << (8 - (ap->ap_mask % 8));
422073edb1cSAlexander Motin 			if ((a[i] & bmask) != (b[i] & bmask))
423073edb1cSAlexander Motin 				goto next;
424073edb1cSAlexander Motin 		}
425073edb1cSAlexander Motin 		return (ap);
426073edb1cSAlexander Motin next:
427073edb1cSAlexander Motin 		;
4288cb2e958SEdward Tomasz Napierala 	}
4298cb2e958SEdward Tomasz Napierala 
4308cb2e958SEdward Tomasz Napierala 	return (NULL);
4318cb2e958SEdward Tomasz Napierala }
4328cb2e958SEdward Tomasz Napierala 
433009ea47eSEdward Tomasz Napierala struct auth_group *
434009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
435009ea47eSEdward Tomasz Napierala {
436009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
437009ea47eSEdward Tomasz Napierala 
438009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
439009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
440009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
441009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
442009ea47eSEdward Tomasz Napierala 			return (NULL);
443009ea47eSEdward Tomasz Napierala 		}
444009ea47eSEdward Tomasz Napierala 	}
445009ea47eSEdward Tomasz Napierala 
446009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
447009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
448009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
449009ea47eSEdward Tomasz Napierala 	if (name != NULL)
450009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
451009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
4528cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
4538cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
454009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
455009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
456009ea47eSEdward Tomasz Napierala 
457009ea47eSEdward Tomasz Napierala 	return (ag);
458009ea47eSEdward Tomasz Napierala }
459009ea47eSEdward Tomasz Napierala 
460009ea47eSEdward Tomasz Napierala void
461009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
462009ea47eSEdward Tomasz Napierala {
4638cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
4648cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
4658cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
466009ea47eSEdward Tomasz Napierala 
467009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
468009ea47eSEdward Tomasz Napierala 
4698cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
470009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
4718cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
4728cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
4738cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
4748cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
4758cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
476009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
477009ea47eSEdward Tomasz Napierala 	free(ag);
478009ea47eSEdward Tomasz Napierala }
479009ea47eSEdward Tomasz Napierala 
480009ea47eSEdward Tomasz Napierala struct auth_group *
481d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
482009ea47eSEdward Tomasz Napierala {
483009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
484009ea47eSEdward Tomasz Napierala 
485009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
486009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
487009ea47eSEdward Tomasz Napierala 			return (ag);
488009ea47eSEdward Tomasz Napierala 	}
489009ea47eSEdward Tomasz Napierala 
490009ea47eSEdward Tomasz Napierala 	return (NULL);
491009ea47eSEdward Tomasz Napierala }
492009ea47eSEdward Tomasz Napierala 
493df9900fbSEdward Tomasz Napierala static int
494df9900fbSEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, int type)
495df9900fbSEdward Tomasz Napierala {
496df9900fbSEdward Tomasz Napierala 
497df9900fbSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN) {
498df9900fbSEdward Tomasz Napierala 		ag->ag_type = type;
499df9900fbSEdward Tomasz Napierala 		return (0);
500df9900fbSEdward Tomasz Napierala 	}
501df9900fbSEdward Tomasz Napierala 
502df9900fbSEdward Tomasz Napierala 	if (ag->ag_type == type)
503df9900fbSEdward Tomasz Napierala 		return (0);
504df9900fbSEdward Tomasz Napierala 
505df9900fbSEdward Tomasz Napierala 	return (1);
506df9900fbSEdward Tomasz Napierala }
507df9900fbSEdward Tomasz Napierala 
508df9900fbSEdward Tomasz Napierala int
509df9900fbSEdward Tomasz Napierala auth_group_set_type_str(struct auth_group *ag, const char *str)
510df9900fbSEdward Tomasz Napierala {
511df9900fbSEdward Tomasz Napierala 	int error, type;
512df9900fbSEdward Tomasz Napierala 
513df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
514df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
515e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
516e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
517df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
518df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
519df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
520df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
521df9900fbSEdward Tomasz Napierala 	} else {
522df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
523df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
524df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
525df9900fbSEdward Tomasz Napierala 		else
526df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
527df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
528df9900fbSEdward Tomasz Napierala 		return (1);
529df9900fbSEdward Tomasz Napierala 	}
530df9900fbSEdward Tomasz Napierala 
531df9900fbSEdward Tomasz Napierala 	error = auth_group_set_type(ag, type);
532df9900fbSEdward Tomasz Napierala 	if (error != 0) {
533df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
534df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
535df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
536df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
537df9900fbSEdward Tomasz Napierala 		else
538df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
539df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
540df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
541df9900fbSEdward Tomasz Napierala 		return (1);
542df9900fbSEdward Tomasz Napierala 	}
543df9900fbSEdward Tomasz Napierala 
544df9900fbSEdward Tomasz Napierala 	return (error);
545df9900fbSEdward Tomasz Napierala }
546df9900fbSEdward Tomasz Napierala 
547009ea47eSEdward Tomasz Napierala static struct portal *
548009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
549009ea47eSEdward Tomasz Napierala {
550009ea47eSEdward Tomasz Napierala 	struct portal *portal;
551009ea47eSEdward Tomasz Napierala 
552009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
553009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
554009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
555009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
556009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
557009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
558009ea47eSEdward Tomasz Napierala 	return (portal);
559009ea47eSEdward Tomasz Napierala }
560009ea47eSEdward Tomasz Napierala 
561009ea47eSEdward Tomasz Napierala static void
562009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
563009ea47eSEdward Tomasz Napierala {
56444a5953aSEdward Tomasz Napierala 
565009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
56644a5953aSEdward Tomasz Napierala 	if (portal->p_ai != NULL)
567009ea47eSEdward Tomasz Napierala 		freeaddrinfo(portal->p_ai);
568009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
569009ea47eSEdward Tomasz Napierala 	free(portal);
570009ea47eSEdward Tomasz Napierala }
571009ea47eSEdward Tomasz Napierala 
572009ea47eSEdward Tomasz Napierala struct portal_group *
573009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
574009ea47eSEdward Tomasz Napierala {
575009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
576009ea47eSEdward Tomasz Napierala 
577009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
578009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
579009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
580009ea47eSEdward Tomasz Napierala 		return (NULL);
581009ea47eSEdward Tomasz Napierala 	}
582009ea47eSEdward Tomasz Napierala 
583009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
584009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
585009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
586009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
587009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
588009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
589009ea47eSEdward Tomasz Napierala 	conf->conf_last_portal_group_tag++;
590009ea47eSEdward Tomasz Napierala 	pg->pg_tag = conf->conf_last_portal_group_tag;
591009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
592009ea47eSEdward Tomasz Napierala 
593009ea47eSEdward Tomasz Napierala 	return (pg);
594009ea47eSEdward Tomasz Napierala }
595009ea47eSEdward Tomasz Napierala 
596009ea47eSEdward Tomasz Napierala void
597009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
598009ea47eSEdward Tomasz Napierala {
599009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
600009ea47eSEdward Tomasz Napierala 
601009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
602009ea47eSEdward Tomasz Napierala 
603009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
604009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
605009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
606009ea47eSEdward Tomasz Napierala 	free(pg);
607009ea47eSEdward Tomasz Napierala }
608009ea47eSEdward Tomasz Napierala 
609009ea47eSEdward Tomasz Napierala struct portal_group *
610d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
611009ea47eSEdward Tomasz Napierala {
612009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
613009ea47eSEdward Tomasz Napierala 
614009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
615009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
616009ea47eSEdward Tomasz Napierala 			return (pg);
617009ea47eSEdward Tomasz Napierala 	}
618009ea47eSEdward Tomasz Napierala 
619009ea47eSEdward Tomasz Napierala 	return (NULL);
620009ea47eSEdward Tomasz Napierala }
621009ea47eSEdward Tomasz Napierala 
622009ea47eSEdward Tomasz Napierala int
623009ea47eSEdward Tomasz Napierala portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
624009ea47eSEdward Tomasz Napierala {
625009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
626009ea47eSEdward Tomasz Napierala 	struct portal *portal;
627009ea47eSEdward Tomasz Napierala 	char *addr, *ch, *arg;
628009ea47eSEdward Tomasz Napierala 	const char *port;
629009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
630009ea47eSEdward Tomasz Napierala 
631009ea47eSEdward Tomasz Napierala 	portal = portal_new(pg);
632009ea47eSEdward Tomasz Napierala 	portal->p_listen = checked_strdup(value);
633009ea47eSEdward Tomasz Napierala 	portal->p_iser = iser;
634009ea47eSEdward Tomasz Napierala 
635009ea47eSEdward Tomasz Napierala 	arg = portal->p_listen;
636009ea47eSEdward Tomasz Napierala 	if (arg[0] == '\0') {
637009ea47eSEdward Tomasz Napierala 		log_warnx("empty listen address");
63844a5953aSEdward Tomasz Napierala 		portal_delete(portal);
639009ea47eSEdward Tomasz Napierala 		return (1);
640009ea47eSEdward Tomasz Napierala 	}
641009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
642009ea47eSEdward Tomasz Napierala 		/*
643009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
644009ea47eSEdward Tomasz Napierala 		 */
645009ea47eSEdward Tomasz Napierala 		arg++;
646009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
647009ea47eSEdward Tomasz Napierala 		if (arg == NULL) {
648009ea47eSEdward Tomasz Napierala 			log_warnx("invalid listen address %s",
649009ea47eSEdward Tomasz Napierala 			    portal->p_listen);
65044a5953aSEdward Tomasz Napierala 			portal_delete(portal);
651009ea47eSEdward Tomasz Napierala 			return (1);
652009ea47eSEdward Tomasz Napierala 		}
653009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
654009ea47eSEdward Tomasz Napierala 			port = "3260";
655009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
656009ea47eSEdward Tomasz Napierala 			port = arg + 1;
657009ea47eSEdward Tomasz Napierala 		} else {
658009ea47eSEdward Tomasz Napierala 			log_warnx("invalid listen address %s",
659009ea47eSEdward Tomasz Napierala 			    portal->p_listen);
66044a5953aSEdward Tomasz Napierala 			portal_delete(portal);
661009ea47eSEdward Tomasz Napierala 			return (1);
662009ea47eSEdward Tomasz Napierala 		}
663009ea47eSEdward Tomasz Napierala 	} else {
664009ea47eSEdward Tomasz Napierala 		/*
665009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
666009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
667009ea47eSEdward Tomasz Napierala 		 */
668009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
669009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
670009ea47eSEdward Tomasz Napierala 				colons++;
671009ea47eSEdward Tomasz Napierala 		}
672009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
673009ea47eSEdward Tomasz Napierala 			addr = arg;
674009ea47eSEdward Tomasz Napierala 			port = "3260";
675009ea47eSEdward Tomasz Napierala 		} else {
676009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
677009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
678009ea47eSEdward Tomasz Napierala 				port = "3260";
679009ea47eSEdward Tomasz Napierala 			else
680009ea47eSEdward Tomasz Napierala 				port = arg;
681009ea47eSEdward Tomasz Napierala 		}
682009ea47eSEdward Tomasz Napierala 	}
683009ea47eSEdward Tomasz Napierala 
684009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
685009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
686009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
687009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
688009ea47eSEdward Tomasz Napierala 
689009ea47eSEdward Tomasz Napierala 	error = getaddrinfo(addr, port, &hints, &portal->p_ai);
690009ea47eSEdward Tomasz Napierala 	if (error != 0) {
691009ea47eSEdward Tomasz Napierala 		log_warnx("getaddrinfo for %s failed: %s",
692009ea47eSEdward Tomasz Napierala 		    portal->p_listen, gai_strerror(error));
69344a5953aSEdward Tomasz Napierala 		portal_delete(portal);
694009ea47eSEdward Tomasz Napierala 		return (1);
695009ea47eSEdward Tomasz Napierala 	}
696009ea47eSEdward Tomasz Napierala 
697009ea47eSEdward Tomasz Napierala 	/*
698009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
699009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
700009ea47eSEdward Tomasz Napierala 	 */
701009ea47eSEdward Tomasz Napierala 
702009ea47eSEdward Tomasz Napierala 	return (0);
703009ea47eSEdward Tomasz Napierala }
704009ea47eSEdward Tomasz Napierala 
705009ea47eSEdward Tomasz Napierala static bool
706009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
707009ea47eSEdward Tomasz Napierala {
708009ea47eSEdward Tomasz Napierala 	switch (ch) {
709009ea47eSEdward Tomasz Napierala 	case '0':
710009ea47eSEdward Tomasz Napierala 	case '1':
711009ea47eSEdward Tomasz Napierala 	case '2':
712009ea47eSEdward Tomasz Napierala 	case '3':
713009ea47eSEdward Tomasz Napierala 	case '4':
714009ea47eSEdward Tomasz Napierala 	case '5':
715009ea47eSEdward Tomasz Napierala 	case '6':
716009ea47eSEdward Tomasz Napierala 	case '7':
717009ea47eSEdward Tomasz Napierala 	case '8':
718009ea47eSEdward Tomasz Napierala 	case '9':
719009ea47eSEdward Tomasz Napierala 	case 'a':
720009ea47eSEdward Tomasz Napierala 	case 'A':
721009ea47eSEdward Tomasz Napierala 	case 'b':
722009ea47eSEdward Tomasz Napierala 	case 'B':
723009ea47eSEdward Tomasz Napierala 	case 'c':
724009ea47eSEdward Tomasz Napierala 	case 'C':
725009ea47eSEdward Tomasz Napierala 	case 'd':
726009ea47eSEdward Tomasz Napierala 	case 'D':
727009ea47eSEdward Tomasz Napierala 	case 'e':
728009ea47eSEdward Tomasz Napierala 	case 'E':
729009ea47eSEdward Tomasz Napierala 	case 'f':
730009ea47eSEdward Tomasz Napierala 	case 'F':
731009ea47eSEdward Tomasz Napierala 		return (true);
732009ea47eSEdward Tomasz Napierala 	default:
733009ea47eSEdward Tomasz Napierala 		return (false);
734009ea47eSEdward Tomasz Napierala 	}
735009ea47eSEdward Tomasz Napierala }
736009ea47eSEdward Tomasz Napierala 
737009ea47eSEdward Tomasz Napierala bool
738009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
739009ea47eSEdward Tomasz Napierala {
740009ea47eSEdward Tomasz Napierala 	int i;
741009ea47eSEdward Tomasz Napierala 
742009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
743009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
744009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
745009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
746009ea47eSEdward Tomasz Napierala 		return (false);
747009ea47eSEdward Tomasz Napierala 	}
748009ea47eSEdward Tomasz Napierala 
749009ea47eSEdward Tomasz Napierala 	/*
750009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
751009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
752009ea47eSEdward Tomasz Napierala 	 */
753009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
754009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
755009ea47eSEdward Tomasz Napierala 			/*
756009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
757009ea47eSEdward Tomasz Napierala 			 * 	by 3.2.6.2: iSCSI Name Encoding.
758009ea47eSEdward Tomasz Napierala 			 */
759009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
760009ea47eSEdward Tomasz Napierala 				continue;
761009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
762009ea47eSEdward Tomasz Napierala 				continue;
763009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
764009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
765009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
766009ea47eSEdward Tomasz Napierala 			break;
767009ea47eSEdward Tomasz Napierala 		}
768009ea47eSEdward Tomasz Napierala 		/*
769009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
770009ea47eSEdward Tomasz Napierala 		 */
771009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
772009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
773009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
774009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
775009ea47eSEdward Tomasz Napierala 			    "digits", name);
776009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
777009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
778009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
779009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
780009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
781009ea47eSEdward Tomasz Napierala 				break;
782009ea47eSEdward Tomasz Napierala 			}
783009ea47eSEdward Tomasz Napierala 		}
784009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
785009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
786009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
787009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
788009ea47eSEdward Tomasz Napierala 			    "digits", name);
789009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
790009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
791009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
792009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
793009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
794009ea47eSEdward Tomasz Napierala 				break;
795009ea47eSEdward Tomasz Napierala 			}
796009ea47eSEdward Tomasz Napierala 		}
797009ea47eSEdward Tomasz Napierala 	} else {
798009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
799009ea47eSEdward Tomasz Napierala 		    "either \".iqn\", \"eui.\", or \"naa.\"",
800009ea47eSEdward Tomasz Napierala 		    name);
801009ea47eSEdward Tomasz Napierala 	}
802009ea47eSEdward Tomasz Napierala 	return (true);
803009ea47eSEdward Tomasz Napierala }
804009ea47eSEdward Tomasz Napierala 
805009ea47eSEdward Tomasz Napierala struct target *
806f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
807009ea47eSEdward Tomasz Napierala {
808009ea47eSEdward Tomasz Napierala 	struct target *targ;
809009ea47eSEdward Tomasz Napierala 	int i, len;
810009ea47eSEdward Tomasz Napierala 
811f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
812009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
813f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
814009ea47eSEdward Tomasz Napierala 		return (NULL);
815009ea47eSEdward Tomasz Napierala 	}
816f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
817f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
818009ea47eSEdward Tomasz Napierala 		return (NULL);
819009ea47eSEdward Tomasz Napierala 	}
820009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
821009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
822009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
823f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
824009ea47eSEdward Tomasz Napierala 
825009ea47eSEdward Tomasz Napierala 	/*
826009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
827009ea47eSEdward Tomasz Napierala 	 */
828f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
829009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
830f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
831009ea47eSEdward Tomasz Napierala 
832009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&targ->t_luns);
833009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
834009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
835009ea47eSEdward Tomasz Napierala 
836009ea47eSEdward Tomasz Napierala 	return (targ);
837009ea47eSEdward Tomasz Napierala }
838009ea47eSEdward Tomasz Napierala 
839009ea47eSEdward Tomasz Napierala void
840009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
841009ea47eSEdward Tomasz Napierala {
842009ea47eSEdward Tomasz Napierala 	struct lun *lun, *tmp;
843009ea47eSEdward Tomasz Napierala 
844009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
845009ea47eSEdward Tomasz Napierala 
846009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
847009ea47eSEdward Tomasz Napierala 		lun_delete(lun);
848f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
849009ea47eSEdward Tomasz Napierala 	free(targ);
850009ea47eSEdward Tomasz Napierala }
851009ea47eSEdward Tomasz Napierala 
852009ea47eSEdward Tomasz Napierala struct target *
853f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
854009ea47eSEdward Tomasz Napierala {
855009ea47eSEdward Tomasz Napierala 	struct target *targ;
856009ea47eSEdward Tomasz Napierala 
857009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
858f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
859009ea47eSEdward Tomasz Napierala 			return (targ);
860009ea47eSEdward Tomasz Napierala 	}
861009ea47eSEdward Tomasz Napierala 
862009ea47eSEdward Tomasz Napierala 	return (NULL);
863009ea47eSEdward Tomasz Napierala }
864009ea47eSEdward Tomasz Napierala 
865009ea47eSEdward Tomasz Napierala struct lun *
866009ea47eSEdward Tomasz Napierala lun_new(struct target *targ, int lun_id)
867009ea47eSEdward Tomasz Napierala {
868009ea47eSEdward Tomasz Napierala 	struct lun *lun;
869009ea47eSEdward Tomasz Napierala 
870009ea47eSEdward Tomasz Napierala 	lun = lun_find(targ, lun_id);
871009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
872009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun %d for target \"%s\"",
873f7ae5bf8SEdward Tomasz Napierala 		    lun_id, targ->t_name);
874009ea47eSEdward Tomasz Napierala 		return (NULL);
875009ea47eSEdward Tomasz Napierala 	}
876009ea47eSEdward Tomasz Napierala 
877009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
878009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
879009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
880009ea47eSEdward Tomasz Napierala 	lun->l_lun = lun_id;
881009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
882009ea47eSEdward Tomasz Napierala 	lun->l_target = targ;
883009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
884009ea47eSEdward Tomasz Napierala 
885009ea47eSEdward Tomasz Napierala 	return (lun);
886009ea47eSEdward Tomasz Napierala }
887009ea47eSEdward Tomasz Napierala 
888009ea47eSEdward Tomasz Napierala void
889009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
890009ea47eSEdward Tomasz Napierala {
891009ea47eSEdward Tomasz Napierala 	struct lun_option *lo, *tmp;
892009ea47eSEdward Tomasz Napierala 
893009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
894009ea47eSEdward Tomasz Napierala 
895009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
896009ea47eSEdward Tomasz Napierala 		lun_option_delete(lo);
897009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
898009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
899009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
900009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
901009ea47eSEdward Tomasz Napierala 	free(lun);
902009ea47eSEdward Tomasz Napierala }
903009ea47eSEdward Tomasz Napierala 
904009ea47eSEdward Tomasz Napierala struct lun *
905d6093026SEdward Tomasz Napierala lun_find(const struct target *targ, int lun_id)
906009ea47eSEdward Tomasz Napierala {
907009ea47eSEdward Tomasz Napierala 	struct lun *lun;
908009ea47eSEdward Tomasz Napierala 
909009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
910009ea47eSEdward Tomasz Napierala 		if (lun->l_lun == lun_id)
911009ea47eSEdward Tomasz Napierala 			return (lun);
912009ea47eSEdward Tomasz Napierala 	}
913009ea47eSEdward Tomasz Napierala 
914009ea47eSEdward Tomasz Napierala 	return (NULL);
915009ea47eSEdward Tomasz Napierala }
916009ea47eSEdward Tomasz Napierala 
917009ea47eSEdward Tomasz Napierala void
918009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
919009ea47eSEdward Tomasz Napierala {
920009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
921009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
922009ea47eSEdward Tomasz Napierala }
923009ea47eSEdward Tomasz Napierala 
924009ea47eSEdward Tomasz Napierala void
925009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
926009ea47eSEdward Tomasz Napierala {
927009ea47eSEdward Tomasz Napierala 
928009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
929009ea47eSEdward Tomasz Napierala }
930009ea47eSEdward Tomasz Napierala 
931009ea47eSEdward Tomasz Napierala void
932009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
933009ea47eSEdward Tomasz Napierala {
934009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
935009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
936009ea47eSEdward Tomasz Napierala }
937009ea47eSEdward Tomasz Napierala 
938009ea47eSEdward Tomasz Napierala void
939009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
940009ea47eSEdward Tomasz Napierala {
941009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
942009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
943009ea47eSEdward Tomasz Napierala }
944009ea47eSEdward Tomasz Napierala 
945009ea47eSEdward Tomasz Napierala void
946009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
947009ea47eSEdward Tomasz Napierala {
948009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
949009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
950009ea47eSEdward Tomasz Napierala }
951009ea47eSEdward Tomasz Napierala 
952009ea47eSEdward Tomasz Napierala void
953009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
954009ea47eSEdward Tomasz Napierala {
955009ea47eSEdward Tomasz Napierala 
956009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
957009ea47eSEdward Tomasz Napierala }
958009ea47eSEdward Tomasz Napierala 
959009ea47eSEdward Tomasz Napierala void
960009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
961009ea47eSEdward Tomasz Napierala {
962009ea47eSEdward Tomasz Napierala 
963009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
964009ea47eSEdward Tomasz Napierala }
965009ea47eSEdward Tomasz Napierala 
966009ea47eSEdward Tomasz Napierala struct lun_option *
967009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value)
968009ea47eSEdward Tomasz Napierala {
969009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
970009ea47eSEdward Tomasz Napierala 
971009ea47eSEdward Tomasz Napierala 	lo = lun_option_find(lun, name);
972009ea47eSEdward Tomasz Napierala 	if (lo != NULL) {
973009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
974f7ae5bf8SEdward Tomasz Napierala 		    name, lun->l_lun, lun->l_target->t_name);
975009ea47eSEdward Tomasz Napierala 		return (NULL);
976009ea47eSEdward Tomasz Napierala 	}
977009ea47eSEdward Tomasz Napierala 
978009ea47eSEdward Tomasz Napierala 	lo = calloc(1, sizeof(*lo));
979009ea47eSEdward Tomasz Napierala 	if (lo == NULL)
980009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
981009ea47eSEdward Tomasz Napierala 	lo->lo_name = checked_strdup(name);
982009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
983009ea47eSEdward Tomasz Napierala 	lo->lo_lun = lun;
984009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
985009ea47eSEdward Tomasz Napierala 
986009ea47eSEdward Tomasz Napierala 	return (lo);
987009ea47eSEdward Tomasz Napierala }
988009ea47eSEdward Tomasz Napierala 
989009ea47eSEdward Tomasz Napierala void
990009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo)
991009ea47eSEdward Tomasz Napierala {
992009ea47eSEdward Tomasz Napierala 
993009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
994009ea47eSEdward Tomasz Napierala 
995009ea47eSEdward Tomasz Napierala 	free(lo->lo_name);
996009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
997009ea47eSEdward Tomasz Napierala 	free(lo);
998009ea47eSEdward Tomasz Napierala }
999009ea47eSEdward Tomasz Napierala 
1000009ea47eSEdward Tomasz Napierala struct lun_option *
1001d6093026SEdward Tomasz Napierala lun_option_find(const struct lun *lun, const char *name)
1002009ea47eSEdward Tomasz Napierala {
1003009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1004009ea47eSEdward Tomasz Napierala 
1005009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
1006009ea47eSEdward Tomasz Napierala 		if (strcmp(lo->lo_name, name) == 0)
1007009ea47eSEdward Tomasz Napierala 			return (lo);
1008009ea47eSEdward Tomasz Napierala 	}
1009009ea47eSEdward Tomasz Napierala 
1010009ea47eSEdward Tomasz Napierala 	return (NULL);
1011009ea47eSEdward Tomasz Napierala }
1012009ea47eSEdward Tomasz Napierala 
1013009ea47eSEdward Tomasz Napierala void
1014009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value)
1015009ea47eSEdward Tomasz Napierala {
1016009ea47eSEdward Tomasz Napierala 
1017009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
1018009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
1019009ea47eSEdward Tomasz Napierala }
1020009ea47eSEdward Tomasz Napierala 
1021009ea47eSEdward Tomasz Napierala static struct connection *
1022073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host,
1023073edb1cSAlexander Motin     const struct sockaddr *client_sa)
1024009ea47eSEdward Tomasz Napierala {
1025009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1026009ea47eSEdward Tomasz Napierala 
1027009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
1028009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
1029009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1030009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
1031009ea47eSEdward Tomasz Napierala 	conn->conn_socket = fd;
1032009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
1033073edb1cSAlexander Motin 	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1034009ea47eSEdward Tomasz Napierala 
1035009ea47eSEdward Tomasz Napierala 	/*
1036009ea47eSEdward Tomasz Napierala 	 * Default values, from RFC 3720, section 12.
1037009ea47eSEdward Tomasz Napierala 	 */
1038009ea47eSEdward Tomasz Napierala 	conn->conn_max_data_segment_length = 8192;
1039009ea47eSEdward Tomasz Napierala 	conn->conn_max_burst_length = 262144;
1040009ea47eSEdward Tomasz Napierala 	conn->conn_immediate_data = true;
1041009ea47eSEdward Tomasz Napierala 
1042009ea47eSEdward Tomasz Napierala 	return (conn);
1043009ea47eSEdward Tomasz Napierala }
1044009ea47eSEdward Tomasz Napierala 
1045009ea47eSEdward Tomasz Napierala #if 0
1046009ea47eSEdward Tomasz Napierala static void
1047009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
1048009ea47eSEdward Tomasz Napierala {
1049009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1050009ea47eSEdward Tomasz Napierala 	struct auth *auth;
10518cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
10528cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
1053009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1054009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1055009ea47eSEdward Tomasz Napierala 	struct target *targ;
1056009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1057009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1058009ea47eSEdward Tomasz Napierala 
1059009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1060009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1061009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1062009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1063009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
1064009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
10658cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
10668cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
10678cb2e958SEdward Tomasz Napierala 			    auth_name->an_initator_name);
10688cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
10698cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
10708cb2e958SEdward Tomasz Napierala 			    auth_portal->an_initator_portal);
1071009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1072009ea47eSEdward Tomasz Napierala 	}
1073009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1074009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1075009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1076009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1077009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1078009ea47eSEdward Tomasz Napierala 	}
1079009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1080f7ae5bf8SEdward Tomasz Napierala 		fprintf(stderr, "target %s {\n", targ->t_name);
1081009ea47eSEdward Tomasz Napierala 		if (targ->t_alias != NULL)
1082009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1083009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1084009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1085009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1086009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1087009ea47eSEdward Tomasz Napierala 				fprintf(stderr, "\t\toption %s %s\n",
1088009ea47eSEdward Tomasz Napierala 				    lo->lo_name, lo->lo_value);
1089009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t}\n");
1090009ea47eSEdward Tomasz Napierala 		}
1091009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1092009ea47eSEdward Tomasz Napierala 	}
1093009ea47eSEdward Tomasz Napierala }
1094009ea47eSEdward Tomasz Napierala #endif
1095009ea47eSEdward Tomasz Napierala 
10962fabfaa5SEdward Tomasz Napierala static int
10972fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
10982fabfaa5SEdward Tomasz Napierala {
10992fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
1100a19eebb1SEdward Tomasz Napierala 	const struct target *targ2;
11012fabfaa5SEdward Tomasz Napierala 
11022fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
11032fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
11042fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
11052fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
11062fabfaa5SEdward Tomasz Napierala 			log_warnx("missing path for lun %d, target \"%s\"",
1107f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
11082fabfaa5SEdward Tomasz Napierala 			return (1);
11092fabfaa5SEdward Tomasz Napierala 		}
11102fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
11112fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
11122fabfaa5SEdward Tomasz Napierala 			log_warnx("missing size for ramdisk-backed lun %d, "
1113f7ae5bf8SEdward Tomasz Napierala 			    "target \"%s\"", lun->l_lun, lun->l_target->t_name);
11142fabfaa5SEdward Tomasz Napierala 			return (1);
11152fabfaa5SEdward Tomasz Napierala 		}
11162fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
11172fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
11182fabfaa5SEdward Tomasz Napierala 			    "for ramdisk-backed lun %d, target \"%s\"",
1119f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
11202fabfaa5SEdward Tomasz Napierala 			return (1);
11212fabfaa5SEdward Tomasz Napierala 		}
11222fabfaa5SEdward Tomasz Napierala 	}
11232fabfaa5SEdward Tomasz Napierala 	if (lun->l_lun < 0 || lun->l_lun > 255) {
11242fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid lun number for lun %d, target \"%s\"; "
11252fabfaa5SEdward Tomasz Napierala 		    "must be between 0 and 255", lun->l_lun,
1126f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
11272fabfaa5SEdward Tomasz Napierala 		return (1);
11282fabfaa5SEdward Tomasz Napierala 	}
11292fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
11302fabfaa5SEdward Tomasz Napierala 		lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
11312fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
11322fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1133f7ae5bf8SEdward Tomasz Napierala 		    "must be larger than 0", lun->l_lun, lun->l_target->t_name);
11342fabfaa5SEdward Tomasz Napierala 		return (1);
11352fabfaa5SEdward Tomasz Napierala 	}
11362fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
11372fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid size for lun %d, target \"%s\"; "
11382fabfaa5SEdward Tomasz Napierala 		    "must be multiple of blocksize", lun->l_lun,
1139f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
11402fabfaa5SEdward Tomasz Napierala 		return (1);
11412fabfaa5SEdward Tomasz Napierala 	}
1142a19eebb1SEdward Tomasz Napierala 	TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1143a19eebb1SEdward Tomasz Napierala 		TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1144a19eebb1SEdward Tomasz Napierala 			if (lun == lun2)
1145a19eebb1SEdward Tomasz Napierala 				continue;
1146a19eebb1SEdward Tomasz Napierala 			if (lun->l_path != NULL && lun2->l_path != NULL &&
1147a19eebb1SEdward Tomasz Napierala 			    strcmp(lun->l_path, lun2->l_path) == 0) {
1148a19eebb1SEdward Tomasz Napierala 				log_debugx("WARNING: path \"%s\" duplicated "
1149a19eebb1SEdward Tomasz Napierala 				    "between lun %d, target \"%s\", and "
1150a19eebb1SEdward Tomasz Napierala 				    "lun %d, target \"%s\"", lun->l_path,
1151f7ae5bf8SEdward Tomasz Napierala 				    lun->l_lun, lun->l_target->t_name,
1152f7ae5bf8SEdward Tomasz Napierala 				    lun2->l_lun, lun2->l_target->t_name);
1153a19eebb1SEdward Tomasz Napierala 			}
1154a19eebb1SEdward Tomasz Napierala 		}
1155a19eebb1SEdward Tomasz Napierala 	}
11562fabfaa5SEdward Tomasz Napierala 
11572fabfaa5SEdward Tomasz Napierala 	return (0);
11582fabfaa5SEdward Tomasz Napierala }
11592fabfaa5SEdward Tomasz Napierala 
1160009ea47eSEdward Tomasz Napierala int
1161009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1162009ea47eSEdward Tomasz Napierala {
1163009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1164009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1165009ea47eSEdward Tomasz Napierala 	struct target *targ;
11662fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
116719b59f13SEdward Tomasz Napierala 	bool found_lun;
11682fabfaa5SEdward Tomasz Napierala 	int error;
1169009ea47eSEdward Tomasz Napierala 
1170009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1171009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1172009ea47eSEdward Tomasz Napierala 
1173009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1174009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1175e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1176e7977549SEdward Tomasz Napierala 			    "default");
1177e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1178009ea47eSEdward Tomasz Napierala 		}
1179009ea47eSEdward Tomasz Napierala 		if (targ->t_portal_group == NULL) {
1180009ea47eSEdward Tomasz Napierala 			targ->t_portal_group = portal_group_find(conf,
1181009ea47eSEdward Tomasz Napierala 			    "default");
1182009ea47eSEdward Tomasz Napierala 			assert(targ->t_portal_group != NULL);
1183009ea47eSEdward Tomasz Napierala 		}
118419b59f13SEdward Tomasz Napierala 		found_lun = false;
1185009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
11862fabfaa5SEdward Tomasz Napierala 			error = conf_verify_lun(lun);
11872fabfaa5SEdward Tomasz Napierala 			if (error != 0)
11882fabfaa5SEdward Tomasz Napierala 				return (error);
118919b59f13SEdward Tomasz Napierala 			found_lun = true;
1190009ea47eSEdward Tomasz Napierala 		}
119119b59f13SEdward Tomasz Napierala 		if (!found_lun) {
119219b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
119319b59f13SEdward Tomasz Napierala 			    targ->t_name);
1194009ea47eSEdward Tomasz Napierala 		}
1195009ea47eSEdward Tomasz Napierala 	}
1196009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1197009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1198009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1199009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1200ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1201009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1202009ea47eSEdward Tomasz Napierala 		}
1203009ea47eSEdward Tomasz Napierala 
1204009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1205009ea47eSEdward Tomasz Napierala 			if (targ->t_portal_group == pg)
1206009ea47eSEdward Tomasz Napierala 				break;
1207009ea47eSEdward Tomasz Napierala 		}
1208009ea47eSEdward Tomasz Napierala 		if (targ == NULL) {
1209009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1210009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1211009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1212009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
1213009ea47eSEdward Tomasz Napierala 		} else
1214009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = false;
1215009ea47eSEdward Tomasz Napierala 	}
1216009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1217009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1218009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1219009ea47eSEdward Tomasz Napierala 		else
1220009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1221009ea47eSEdward Tomasz Napierala 
1222009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1223009ea47eSEdward Tomasz Napierala 			if (targ->t_auth_group == ag)
1224009ea47eSEdward Tomasz Napierala 				break;
1225009ea47eSEdward Tomasz Napierala 		}
1226009ea47eSEdward Tomasz Napierala 		if (targ == NULL && ag->ag_name != NULL &&
1227ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1228009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1229009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1230009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1231009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1232009ea47eSEdward Tomasz Napierala 		}
1233009ea47eSEdward Tomasz Napierala 	}
1234009ea47eSEdward Tomasz Napierala 
1235009ea47eSEdward Tomasz Napierala 	return (0);
1236009ea47eSEdward Tomasz Napierala }
1237009ea47eSEdward Tomasz Napierala 
1238009ea47eSEdward Tomasz Napierala static int
1239009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1240009ea47eSEdward Tomasz Napierala {
1241009ea47eSEdward Tomasz Napierala 	struct target *oldtarg, *newtarg, *tmptarg;
1242009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1243009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1244009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
1245009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
1246009ea47eSEdward Tomasz Napierala 	int changed, cumulated_error = 0, error;
1247009ea47eSEdward Tomasz Napierala 	int one = 1;
1248009ea47eSEdward Tomasz Napierala 
1249009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1250009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1251009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1252009ea47eSEdward Tomasz Napierala 	}
1253009ea47eSEdward Tomasz Napierala 
1254009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1255009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1256009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1257009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1258009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1259009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1260009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1261009ea47eSEdward Tomasz Napierala 		} else {
1262009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1263009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1264009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1265009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1266009ea47eSEdward Tomasz Napierala 		}
1267009ea47eSEdward Tomasz Napierala 	}
1268009ea47eSEdward Tomasz Napierala 
1269009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1270009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1271009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1272009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1273009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1274009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1275009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1276009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1277009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1278009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1279009ea47eSEdward Tomasz Napierala 		}
1280009ea47eSEdward Tomasz Napierala 	}
1281009ea47eSEdward Tomasz Napierala 
1282d5e316e5SEdward Tomasz Napierala 	/*
1283d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1284d5e316e5SEdward Tomasz Napierala 	 * 	the old lun or target into newconf, so that subsequent
128561a2a354SEdward Tomasz Napierala 	 * 	conf_apply() would try to remove them again.  That would
128661a2a354SEdward Tomasz Napierala 	 * 	be somewhat hairy, though, and lun deletion failures don't
128761a2a354SEdward Tomasz Napierala 	 * 	really happen, so leave it as it is for now.
1288d5e316e5SEdward Tomasz Napierala 	 */
1289009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1290009ea47eSEdward Tomasz Napierala 		/*
1291009ea47eSEdward Tomasz Napierala 		 * First, remove any targets present in the old configuration
1292009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1293009ea47eSEdward Tomasz Napierala 		 */
1294f7ae5bf8SEdward Tomasz Napierala 		newtarg = target_find(newconf, oldtarg->t_name);
1295009ea47eSEdward Tomasz Napierala 		if (newtarg == NULL) {
1296009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1297009ea47eSEdward Tomasz Napierala 			    tmplun) {
1298cadf3831SEdward Tomasz Napierala 				log_debugx("target %s not found in new "
1299cadf3831SEdward Tomasz Napierala 				    "configuration; removing its lun %d, "
1300009ea47eSEdward Tomasz Napierala 				    "backed by CTL lun %d",
1301f7ae5bf8SEdward Tomasz Napierala 				    oldtarg->t_name, oldlun->l_lun,
1302009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1303009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1304009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1305009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1306009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1307f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1308009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1309009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1310009ea47eSEdward Tomasz Napierala 				}
1311009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1312009ea47eSEdward Tomasz Napierala 			}
1313917d38fbSAlexander Motin 			kernel_port_remove(oldtarg);
1314009ea47eSEdward Tomasz Napierala 			target_delete(oldtarg);
1315009ea47eSEdward Tomasz Napierala 			continue;
1316009ea47eSEdward Tomasz Napierala 		}
1317009ea47eSEdward Tomasz Napierala 
1318009ea47eSEdward Tomasz Napierala 		/*
1319009ea47eSEdward Tomasz Napierala 		 * Second, remove any LUNs present in the old target
1320009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1321009ea47eSEdward Tomasz Napierala 		 */
1322009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1323009ea47eSEdward Tomasz Napierala 			newlun = lun_find(newtarg, oldlun->l_lun);
1324009ea47eSEdward Tomasz Napierala 			if (newlun == NULL) {
1325009ea47eSEdward Tomasz Napierala 				log_debugx("lun %d, target %s, CTL lun %d "
1326cadf3831SEdward Tomasz Napierala 				    "not found in new configuration; "
1327f7ae5bf8SEdward Tomasz Napierala 				    "removing", oldlun->l_lun, oldtarg->t_name,
1328009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1329009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1330009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1331009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1332009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1333f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1334009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1335009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1336009ea47eSEdward Tomasz Napierala 				}
1337009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1338009ea47eSEdward Tomasz Napierala 				continue;
1339009ea47eSEdward Tomasz Napierala 			}
1340009ea47eSEdward Tomasz Napierala 
1341009ea47eSEdward Tomasz Napierala 			/*
1342009ea47eSEdward Tomasz Napierala 			 * Also remove the LUNs changed by more than size.
1343009ea47eSEdward Tomasz Napierala 			 */
1344009ea47eSEdward Tomasz Napierala 			changed = 0;
1345009ea47eSEdward Tomasz Napierala 			assert(oldlun->l_backend != NULL);
1346009ea47eSEdward Tomasz Napierala 			assert(newlun->l_backend != NULL);
1347009ea47eSEdward Tomasz Napierala 			if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1348009ea47eSEdward Tomasz Napierala 				log_debugx("backend for lun %d, target %s, "
1349009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1350f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1351009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1352009ea47eSEdward Tomasz Napierala 				changed = 1;
1353009ea47eSEdward Tomasz Napierala 			}
1354009ea47eSEdward Tomasz Napierala 			if (oldlun->l_blocksize != newlun->l_blocksize) {
1355009ea47eSEdward Tomasz Napierala 				log_debugx("blocksize for lun %d, target %s, "
1356009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1357f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1358009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1359009ea47eSEdward Tomasz Napierala 				changed = 1;
1360009ea47eSEdward Tomasz Napierala 			}
1361009ea47eSEdward Tomasz Napierala 			if (newlun->l_device_id != NULL &&
1362009ea47eSEdward Tomasz Napierala 			    (oldlun->l_device_id == NULL ||
1363009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1364009ea47eSEdward Tomasz Napierala 			     0)) {
1365009ea47eSEdward Tomasz Napierala 				log_debugx("device-id for lun %d, target %s, "
1366009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1367f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1368009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1369009ea47eSEdward Tomasz Napierala 				changed = 1;
1370009ea47eSEdward Tomasz Napierala 			}
1371009ea47eSEdward Tomasz Napierala 			if (newlun->l_path != NULL &&
1372009ea47eSEdward Tomasz Napierala 			    (oldlun->l_path == NULL ||
1373009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1374009ea47eSEdward Tomasz Napierala 				log_debugx("path for lun %d, target %s, "
1375009ea47eSEdward Tomasz Napierala 				    "CTL lun %d, changed; removing",
1376f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1377009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1378009ea47eSEdward Tomasz Napierala 				changed = 1;
1379009ea47eSEdward Tomasz Napierala 			}
1380009ea47eSEdward Tomasz Napierala 			if (newlun->l_serial != NULL &&
1381009ea47eSEdward Tomasz Napierala 			    (oldlun->l_serial == NULL ||
1382009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1383009ea47eSEdward Tomasz Napierala 				log_debugx("serial for lun %d, target %s, "
1384009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1385f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1386009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1387009ea47eSEdward Tomasz Napierala 				changed = 1;
1388009ea47eSEdward Tomasz Napierala 			}
1389009ea47eSEdward Tomasz Napierala 			if (changed) {
1390009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1391009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1392009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1393009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1394f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1395009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1396009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1397009ea47eSEdward Tomasz Napierala 				}
1398009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1399009ea47eSEdward Tomasz Napierala 				continue;
1400009ea47eSEdward Tomasz Napierala 			}
1401009ea47eSEdward Tomasz Napierala 
1402009ea47eSEdward Tomasz Napierala 			lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1403009ea47eSEdward Tomasz Napierala 		}
1404009ea47eSEdward Tomasz Napierala 	}
1405009ea47eSEdward Tomasz Napierala 
1406009ea47eSEdward Tomasz Napierala 	/*
1407009ea47eSEdward Tomasz Napierala 	 * Now add new targets or modify existing ones.
1408009ea47eSEdward Tomasz Napierala 	 */
1409009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1410f7ae5bf8SEdward Tomasz Napierala 		oldtarg = target_find(oldconf, newtarg->t_name);
1411009ea47eSEdward Tomasz Napierala 
1412d5e316e5SEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(newlun, &newtarg->t_luns, l_next, tmplun) {
1413009ea47eSEdward Tomasz Napierala 			if (oldtarg != NULL) {
1414009ea47eSEdward Tomasz Napierala 				oldlun = lun_find(oldtarg, newlun->l_lun);
1415009ea47eSEdward Tomasz Napierala 				if (oldlun != NULL) {
1416*8cf98331SAlexander Motin 					if (newlun->l_size != oldlun->l_size ||
1417*8cf98331SAlexander Motin 					    newlun->l_size == 0) {
1418009ea47eSEdward Tomasz Napierala 						log_debugx("resizing lun %d, "
1419009ea47eSEdward Tomasz Napierala 						    "target %s, CTL lun %d",
1420009ea47eSEdward Tomasz Napierala 						    newlun->l_lun,
1421f7ae5bf8SEdward Tomasz Napierala 						    newtarg->t_name,
1422009ea47eSEdward Tomasz Napierala 						    newlun->l_ctl_lun);
1423009ea47eSEdward Tomasz Napierala 						error =
1424009ea47eSEdward Tomasz Napierala 						    kernel_lun_resize(newlun);
1425009ea47eSEdward Tomasz Napierala 						if (error != 0) {
1426009ea47eSEdward Tomasz Napierala 							log_warnx("failed to "
1427009ea47eSEdward Tomasz Napierala 							    "resize lun %d, "
1428009ea47eSEdward Tomasz Napierala 							    "target %s, "
1429009ea47eSEdward Tomasz Napierala 							    "CTL lun %d",
1430009ea47eSEdward Tomasz Napierala 							    newlun->l_lun,
1431f7ae5bf8SEdward Tomasz Napierala 							    newtarg->t_name,
1432009ea47eSEdward Tomasz Napierala 							    newlun->l_lun);
1433009ea47eSEdward Tomasz Napierala 							cumulated_error++;
1434009ea47eSEdward Tomasz Napierala 						}
1435009ea47eSEdward Tomasz Napierala 					}
1436009ea47eSEdward Tomasz Napierala 					continue;
1437009ea47eSEdward Tomasz Napierala 				}
1438009ea47eSEdward Tomasz Napierala 			}
1439009ea47eSEdward Tomasz Napierala 			log_debugx("adding lun %d, target %s",
1440f7ae5bf8SEdward Tomasz Napierala 			    newlun->l_lun, newtarg->t_name);
1441009ea47eSEdward Tomasz Napierala 			error = kernel_lun_add(newlun);
1442009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1443009ea47eSEdward Tomasz Napierala 				log_warnx("failed to add lun %d, target %s",
1444f7ae5bf8SEdward Tomasz Napierala 				    newlun->l_lun, newtarg->t_name);
1445d5e316e5SEdward Tomasz Napierala 				lun_delete(newlun);
1446009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1447009ea47eSEdward Tomasz Napierala 			}
1448009ea47eSEdward Tomasz Napierala 		}
1449917d38fbSAlexander Motin 		if (oldtarg == NULL)
1450917d38fbSAlexander Motin 			kernel_port_add(newtarg);
1451009ea47eSEdward Tomasz Napierala 	}
1452009ea47eSEdward Tomasz Napierala 
1453009ea47eSEdward Tomasz Napierala 	/*
1454009ea47eSEdward Tomasz Napierala 	 * Go through the new portals, opening the sockets as neccessary.
1455009ea47eSEdward Tomasz Napierala 	 */
1456009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1457009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
1458009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
1459009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
1460009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
1461009ea47eSEdward Tomasz Napierala 			continue;
1462009ea47eSEdward Tomasz Napierala 		}
1463009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1464009ea47eSEdward Tomasz Napierala 			/*
1465009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
1466009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
1467009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
1468009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
1469009ea47eSEdward Tomasz Napierala 			 */
1470009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1471009ea47eSEdward Tomasz Napierala 			    pg_next) {
1472009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1473009ea47eSEdward Tomasz Napierala 				    p_next) {
1474009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
1475009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
1476009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
1477009ea47eSEdward Tomasz Napierala 						newp->p_socket =
1478009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
1479009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
1480009ea47eSEdward Tomasz Napierala 						break;
1481009ea47eSEdward Tomasz Napierala 					}
1482009ea47eSEdward Tomasz Napierala 				}
1483009ea47eSEdward Tomasz Napierala 			}
1484009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
1485009ea47eSEdward Tomasz Napierala 				/*
1486009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
1487009ea47eSEdward Tomasz Napierala 				 */
1488009ea47eSEdward Tomasz Napierala 				continue;
1489009ea47eSEdward Tomasz Napierala 			}
1490009ea47eSEdward Tomasz Napierala 
1491009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1492ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
14938cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
14948cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
14958cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
14968cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
14978cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
14988cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
14998cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
1500ba3a2d31SEdward Tomasz Napierala 				continue;
1501ba3a2d31SEdward Tomasz Napierala 			}
1502ba3a2d31SEdward Tomasz Napierala #endif
1503ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
1504009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
1505009ea47eSEdward Tomasz Napierala 
1506009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
1507009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
1508009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
1509009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
1510009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
1511009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
1512009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
1513009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1514009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1515009ea47eSEdward Tomasz Napierala 				continue;
1516009ea47eSEdward Tomasz Napierala 			}
1517009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
1518009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
1519009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1520009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
1521009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
1522009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1523009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1524009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1525009ea47eSEdward Tomasz Napierala 				continue;
1526009ea47eSEdward Tomasz Napierala 			}
1527009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
1528009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
1529009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1530009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
1531009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1532009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1533009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1534009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1535009ea47eSEdward Tomasz Napierala 				continue;
1536009ea47eSEdward Tomasz Napierala 			}
1537009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
1538009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1539009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
1540009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1541009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1542009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1543009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1544009ea47eSEdward Tomasz Napierala 				continue;
1545009ea47eSEdward Tomasz Napierala 			}
1546009ea47eSEdward Tomasz Napierala 		}
1547009ea47eSEdward Tomasz Napierala 	}
1548009ea47eSEdward Tomasz Napierala 
1549009ea47eSEdward Tomasz Napierala 	/*
1550009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
1551009ea47eSEdward Tomasz Napierala 	 */
1552009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1553009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1554009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
1555009ea47eSEdward Tomasz Napierala 				continue;
1556009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
1557009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
1558009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
1559009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
1560009ea47eSEdward Tomasz Napierala 		}
1561009ea47eSEdward Tomasz Napierala 	}
1562009ea47eSEdward Tomasz Napierala 
1563009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
1564009ea47eSEdward Tomasz Napierala }
1565009ea47eSEdward Tomasz Napierala 
1566009ea47eSEdward Tomasz Napierala bool
1567009ea47eSEdward Tomasz Napierala timed_out(void)
1568009ea47eSEdward Tomasz Napierala {
1569009ea47eSEdward Tomasz Napierala 
1570009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
1571009ea47eSEdward Tomasz Napierala }
1572009ea47eSEdward Tomasz Napierala 
1573009ea47eSEdward Tomasz Napierala static void
1574009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused)
1575009ea47eSEdward Tomasz Napierala {
1576009ea47eSEdward Tomasz Napierala 	/*
1577009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
1578009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
1579009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
1580009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
1581009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
1582009ea47eSEdward Tomasz Napierala 	 */
1583009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
1584009ea47eSEdward Tomasz Napierala 		/*
1585009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
1586009ea47eSEdward Tomasz Napierala 		 */
1587009ea47eSEdward Tomasz Napierala 		_exit(2);
1588009ea47eSEdward Tomasz Napierala 	}
1589009ea47eSEdward Tomasz Napierala 
1590009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
1591009ea47eSEdward Tomasz Napierala }
1592009ea47eSEdward Tomasz Napierala 
1593009ea47eSEdward Tomasz Napierala static void
1594009ea47eSEdward Tomasz Napierala set_timeout(const struct conf *conf)
1595009ea47eSEdward Tomasz Napierala {
1596009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
1597009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
1598009ea47eSEdward Tomasz Napierala 	int error;
1599009ea47eSEdward Tomasz Napierala 
1600009ea47eSEdward Tomasz Napierala 	if (conf->conf_timeout <= 0) {
1601009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
1602009ea47eSEdward Tomasz Napierala 		return;
1603009ea47eSEdward Tomasz Napierala 	}
1604009ea47eSEdward Tomasz Napierala 
1605009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
1606009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigalrm_handler;
1607009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
1608009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
1609009ea47eSEdward Tomasz Napierala 	if (error != 0)
1610009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1611009ea47eSEdward Tomasz Napierala 
1612009ea47eSEdward Tomasz Napierala 	/*
1613009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
1614009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
1615009ea47eSEdward Tomasz Napierala 	 */
1616009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
1617009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
1618009ea47eSEdward Tomasz Napierala 	itv.it_value.tv_sec = conf->conf_timeout;
1619009ea47eSEdward Tomasz Napierala 
1620009ea47eSEdward Tomasz Napierala 	log_debugx("setting session timeout to %d seconds",
1621009ea47eSEdward Tomasz Napierala 	    conf->conf_timeout);
1622009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
1623009ea47eSEdward Tomasz Napierala 	if (error != 0)
1624009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
1625009ea47eSEdward Tomasz Napierala }
1626009ea47eSEdward Tomasz Napierala 
1627009ea47eSEdward Tomasz Napierala static int
1628009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
1629009ea47eSEdward Tomasz Napierala {
1630009ea47eSEdward Tomasz Napierala 	pid_t pid;
1631009ea47eSEdward Tomasz Napierala 	int status;
1632009ea47eSEdward Tomasz Napierala 	int num = 0;
1633009ea47eSEdward Tomasz Napierala 
1634009ea47eSEdward Tomasz Napierala 	for (;;) {
1635009ea47eSEdward Tomasz Napierala 		/*
1636009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
1637009ea47eSEdward Tomasz Napierala 		 */
1638009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
1639009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
1640009ea47eSEdward Tomasz Napierala 		else
1641009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
1642009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
1643009ea47eSEdward Tomasz Napierala 			break;
1644009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
1645009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
1646009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
1647009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
1648009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
1649009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
1650009ea47eSEdward Tomasz Napierala 		} else {
1651009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
1652009ea47eSEdward Tomasz Napierala 		}
1653009ea47eSEdward Tomasz Napierala 		num++;
1654009ea47eSEdward Tomasz Napierala 	}
1655009ea47eSEdward Tomasz Napierala 
1656009ea47eSEdward Tomasz Napierala 	return (num);
1657009ea47eSEdward Tomasz Napierala }
1658009ea47eSEdward Tomasz Napierala 
1659009ea47eSEdward Tomasz Napierala static void
16608eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
1661073edb1cSAlexander Motin     const struct sockaddr *client_sa, bool dont_fork)
1662009ea47eSEdward Tomasz Napierala {
1663009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1664009ea47eSEdward Tomasz Napierala 	int error;
1665009ea47eSEdward Tomasz Napierala 	pid_t pid;
1666009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
1667009ea47eSEdward Tomasz Napierala 	struct conf *conf;
1668009ea47eSEdward Tomasz Napierala 
1669009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
1670009ea47eSEdward Tomasz Napierala 
1671009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
1672009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
1673009ea47eSEdward Tomasz Napierala 	} else {
1674009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
1675009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
1676009ea47eSEdward Tomasz Napierala 
1677009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1678009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
1679009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
1680009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
1681009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
1682009ea47eSEdward Tomasz Napierala 		}
1683009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
1684009ea47eSEdward Tomasz Napierala 		    nchildren);
1685009ea47eSEdward Tomasz Napierala 		nchildren++;
1686009ea47eSEdward Tomasz Napierala 		pid = fork();
1687009ea47eSEdward Tomasz Napierala 		if (pid < 0)
1688009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
1689009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
1690009ea47eSEdward Tomasz Napierala 			close(fd);
1691009ea47eSEdward Tomasz Napierala 			return;
1692009ea47eSEdward Tomasz Napierala 		}
1693009ea47eSEdward Tomasz Napierala 	}
1694009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
1695009ea47eSEdward Tomasz Napierala 
1696073edb1cSAlexander Motin 	error = getnameinfo(client_sa, client_sa->sa_len,
1697009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
1698009ea47eSEdward Tomasz Napierala 	if (error != 0)
16999a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
1700009ea47eSEdward Tomasz Napierala 
1701009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
1702009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
1703009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
1704009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
1705009ea47eSEdward Tomasz Napierala 
1706073edb1cSAlexander Motin 	conn = connection_new(portal, fd, host, client_sa);
1707009ea47eSEdward Tomasz Napierala 	set_timeout(conf);
1708009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
1709009ea47eSEdward Tomasz Napierala 	login(conn);
1710009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1711009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
1712009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
1713009ea47eSEdward Tomasz Napierala 	} else {
1714009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
1715009ea47eSEdward Tomasz Napierala 		discovery(conn);
1716009ea47eSEdward Tomasz Napierala 	}
1717009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
1718009ea47eSEdward Tomasz Napierala 	exit(0);
1719009ea47eSEdward Tomasz Napierala }
1720009ea47eSEdward Tomasz Napierala 
1721009ea47eSEdward Tomasz Napierala static int
1722009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
1723009ea47eSEdward Tomasz Napierala {
1724009ea47eSEdward Tomasz Napierala 
1725009ea47eSEdward Tomasz Napierala 	/*
1726009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
1727009ea47eSEdward Tomasz Napierala 	 */
1728009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
1729009ea47eSEdward Tomasz Napierala 		return (nfds);
1730009ea47eSEdward Tomasz Napierala 
1731009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
1732009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
1733009ea47eSEdward Tomasz Napierala 		nfds = fd;
1734009ea47eSEdward Tomasz Napierala 	return (nfds);
1735009ea47eSEdward Tomasz Napierala }
1736009ea47eSEdward Tomasz Napierala 
1737009ea47eSEdward Tomasz Napierala static void
1738009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
1739009ea47eSEdward Tomasz Napierala {
1740009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1741009ea47eSEdward Tomasz Napierala 	struct portal *portal;
17429a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
17439a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
1744009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1745009ea47eSEdward Tomasz Napierala 	int connection_id;
17468cab2ed4SEdward Tomasz Napierala 	int portal_id;
1747ba3a2d31SEdward Tomasz Napierala #endif
1748009ea47eSEdward Tomasz Napierala 	fd_set fdset;
1749009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
1750009ea47eSEdward Tomasz Napierala 
1751009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
1752009ea47eSEdward Tomasz Napierala 
1753009ea47eSEdward Tomasz Napierala 	for (;;) {
1754009ea47eSEdward Tomasz Napierala 		if (sighup_received || sigterm_received)
1755009ea47eSEdward Tomasz Napierala 			return;
1756009ea47eSEdward Tomasz Napierala 
1757009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1758ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
17598eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
17608eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
17618eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
17620488e848SEdward Tomasz Napierala 			assert(client_salen >= client_sa.ss_len);
1763009ea47eSEdward Tomasz Napierala 
17648cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
17658cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
17668cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
17678cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
17688cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
17698cab2ed4SEdward Tomasz Napierala 						goto found;
17708cab2ed4SEdward Tomasz Napierala 					}
17718cab2ed4SEdward Tomasz Napierala 				}
17728cab2ed4SEdward Tomasz Napierala 			}
1773009ea47eSEdward Tomasz Napierala 
17748cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
17758cab2ed4SEdward Tomasz Napierala 			    portal_id);
17768cab2ed4SEdward Tomasz Napierala 
17778cab2ed4SEdward Tomasz Napierala found:
17788eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
1779073edb1cSAlexander Motin 			    (struct sockaddr *)&client_sa, dont_fork);
1780ba3a2d31SEdward Tomasz Napierala 		} else {
1781ba3a2d31SEdward Tomasz Napierala #endif
1782ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
1783ba3a2d31SEdward Tomasz Napierala 
1784009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
1785009ea47eSEdward Tomasz Napierala 			nfds = 0;
1786009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1787009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1788009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
1789009ea47eSEdward Tomasz Napierala 			}
1790009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
1791009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
1792009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
1793009ea47eSEdward Tomasz Napierala 					return;
1794009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
1795009ea47eSEdward Tomasz Napierala 			}
1796009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1797009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
1798009ea47eSEdward Tomasz Napierala 					if (!FD_ISSET(portal->p_socket, &fdset))
1799009ea47eSEdward Tomasz Napierala 						continue;
18009a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
18019a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
18029a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
18039a95cfffSEdward Tomasz Napierala 					    &client_salen);
1804009ea47eSEdward Tomasz Napierala 					if (client_fd < 0)
1805009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
18060488e848SEdward Tomasz Napierala 					assert(client_salen >= client_sa.ss_len);
18070488e848SEdward Tomasz Napierala 
18089a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
18098eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
1810073edb1cSAlexander Motin 					    dont_fork);
1811009ea47eSEdward Tomasz Napierala 					break;
1812009ea47eSEdward Tomasz Napierala 				}
1813009ea47eSEdward Tomasz Napierala 			}
1814ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1815ba3a2d31SEdward Tomasz Napierala 		}
1816ba3a2d31SEdward Tomasz Napierala #endif
1817009ea47eSEdward Tomasz Napierala 	}
1818009ea47eSEdward Tomasz Napierala }
1819009ea47eSEdward Tomasz Napierala 
1820009ea47eSEdward Tomasz Napierala static void
1821009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
1822009ea47eSEdward Tomasz Napierala {
1823009ea47eSEdward Tomasz Napierala 
1824009ea47eSEdward Tomasz Napierala 	sighup_received = true;
1825009ea47eSEdward Tomasz Napierala }
1826009ea47eSEdward Tomasz Napierala 
1827009ea47eSEdward Tomasz Napierala static void
1828009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
1829009ea47eSEdward Tomasz Napierala {
1830009ea47eSEdward Tomasz Napierala 
1831009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
1832009ea47eSEdward Tomasz Napierala }
1833009ea47eSEdward Tomasz Napierala 
1834009ea47eSEdward Tomasz Napierala static void
183570b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
183670b939ecSEdward Tomasz Napierala {
183770b939ecSEdward Tomasz Napierala 
183870b939ecSEdward Tomasz Napierala 	/*
183970b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
184070b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
184170b939ecSEdward Tomasz Napierala 	 * wait_for_children().
184270b939ecSEdward Tomasz Napierala 	 */
184370b939ecSEdward Tomasz Napierala }
184470b939ecSEdward Tomasz Napierala 
184570b939ecSEdward Tomasz Napierala static void
1846009ea47eSEdward Tomasz Napierala register_signals(void)
1847009ea47eSEdward Tomasz Napierala {
1848009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
1849009ea47eSEdward Tomasz Napierala 	int error;
1850009ea47eSEdward Tomasz Napierala 
1851009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
1852009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
1853009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
1854009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
1855009ea47eSEdward Tomasz Napierala 	if (error != 0)
1856009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1857009ea47eSEdward Tomasz Napierala 
1858009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
1859009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
1860009ea47eSEdward Tomasz Napierala 	if (error != 0)
1861009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1862009ea47eSEdward Tomasz Napierala 
1863009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
1864009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
1865009ea47eSEdward Tomasz Napierala 	if (error != 0)
1866009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
186770b939ecSEdward Tomasz Napierala 
186870b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
186970b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
187070b939ecSEdward Tomasz Napierala 	if (error != 0)
187170b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
1872009ea47eSEdward Tomasz Napierala }
1873009ea47eSEdward Tomasz Napierala 
1874009ea47eSEdward Tomasz Napierala int
1875009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
1876009ea47eSEdward Tomasz Napierala {
1877009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
1878009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
1879009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
1880009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
1881009ea47eSEdward Tomasz Napierala 
1882ba3a2d31SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "df:R")) != -1) {
1883009ea47eSEdward Tomasz Napierala 		switch (ch) {
1884009ea47eSEdward Tomasz Napierala 		case 'd':
1885009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
1886009ea47eSEdward Tomasz Napierala 			debug++;
1887009ea47eSEdward Tomasz Napierala 			break;
1888009ea47eSEdward Tomasz Napierala 		case 'f':
1889009ea47eSEdward Tomasz Napierala 			config_path = optarg;
1890009ea47eSEdward Tomasz Napierala 			break;
1891ba3a2d31SEdward Tomasz Napierala 		case 'R':
1892ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
1893ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
1894ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
1895ba3a2d31SEdward Tomasz Napierala #endif
1896ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
1897ba3a2d31SEdward Tomasz Napierala 			break;
1898009ea47eSEdward Tomasz Napierala 		case '?':
1899009ea47eSEdward Tomasz Napierala 		default:
1900009ea47eSEdward Tomasz Napierala 			usage();
1901009ea47eSEdward Tomasz Napierala 		}
1902009ea47eSEdward Tomasz Napierala 	}
1903009ea47eSEdward Tomasz Napierala 	argc -= optind;
1904009ea47eSEdward Tomasz Napierala 	if (argc != 0)
1905009ea47eSEdward Tomasz Napierala 		usage();
1906009ea47eSEdward Tomasz Napierala 
1907009ea47eSEdward Tomasz Napierala 	log_init(debug);
1908009ea47eSEdward Tomasz Napierala 	kernel_init();
1909009ea47eSEdward Tomasz Napierala 
1910009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
1911009ea47eSEdward Tomasz Napierala 	newconf = conf_new_from_file(config_path);
1912009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
1913fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
1914009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
1915009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
1916009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
1917009ea47eSEdward Tomasz Napierala 	}
1918009ea47eSEdward Tomasz Napierala 
1919009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
1920009ea47eSEdward Tomasz Napierala 	if (error != 0)
1921fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
1922fe845cdbSEdward Tomasz Napierala 
1923009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
1924009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
1925009ea47eSEdward Tomasz Napierala 
1926009ea47eSEdward Tomasz Napierala 	register_signals();
1927009ea47eSEdward Tomasz Napierala 
19285d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
19295d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
19305d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
19315d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
19325d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
19335d5a95f1SEdward Tomasz Napierala 			exit(1);
19345d5a95f1SEdward Tomasz Napierala 		}
19355d5a95f1SEdward Tomasz Napierala 	}
19365d5a95f1SEdward Tomasz Napierala 
1937009ea47eSEdward Tomasz Napierala 	for (;;) {
1938009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
1939009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
1940009ea47eSEdward Tomasz Napierala 			sighup_received = false;
1941009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
1942009ea47eSEdward Tomasz Napierala 			tmpconf = conf_new_from_file(config_path);
1943009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
1944009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
1945009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
1946009ea47eSEdward Tomasz Napierala 			} else {
1947009ea47eSEdward Tomasz Napierala 				if (debug > 0)
1948009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
1949009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
1950009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
1951009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
1952009ea47eSEdward Tomasz Napierala 				if (error != 0)
1953009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
1954009ea47eSEdward Tomasz Napierala 					    "configuration");
1955009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
1956009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
1957009ea47eSEdward Tomasz Napierala 			}
1958009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
1959009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
1960009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
1961009ea47eSEdward Tomasz Napierala 
1962009ea47eSEdward Tomasz Napierala 			log_debugx("disabling CTL iSCSI port "
1963009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
1964009ea47eSEdward Tomasz Napierala 
1965009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
1966009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
1967009ea47eSEdward Tomasz Napierala 			if (debug > 0)
1968009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
1969009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
1970009ea47eSEdward Tomasz Napierala 			if (error != 0)
1971009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
1972009ea47eSEdward Tomasz Napierala 
1973009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
1974009ea47eSEdward Tomasz Napierala 			exit(0);
1975009ea47eSEdward Tomasz Napierala 		} else {
1976009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
1977009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
1978009ea47eSEdward Tomasz Napierala 		}
1979009ea47eSEdward Tomasz Napierala 	}
1980009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
1981009ea47eSEdward Tomasz Napierala }
1982