xref: /freebsd/usr.sbin/ctld/ctld.c (revision 6b31e1302fa6af42c3d0f51928bf67038c108089)
1009ea47eSEdward Tomasz Napierala /*-
2009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
3009ea47eSEdward Tomasz Napierala  * All rights reserved.
4009ea47eSEdward Tomasz Napierala  *
5009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
7009ea47eSEdward Tomasz Napierala  *
8009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10009ea47eSEdward Tomasz Napierala  * are met:
11009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16009ea47eSEdward Tomasz Napierala  *
17009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
28009ea47eSEdward Tomasz Napierala  *
29009ea47eSEdward Tomasz Napierala  */
30009ea47eSEdward Tomasz Napierala 
31b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h>
32b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$");
33b7a65e39SEdward Tomasz Napierala 
34009ea47eSEdward Tomasz Napierala #include <sys/types.h>
35009ea47eSEdward Tomasz Napierala #include <sys/time.h>
36009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
37009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
38009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
39073edb1cSAlexander Motin #include <arpa/inet.h>
40009ea47eSEdward Tomasz Napierala #include <assert.h>
41009ea47eSEdward Tomasz Napierala #include <ctype.h>
42009ea47eSEdward Tomasz Napierala #include <errno.h>
43009ea47eSEdward Tomasz Napierala #include <netdb.h>
44009ea47eSEdward Tomasz Napierala #include <signal.h>
45009ea47eSEdward Tomasz Napierala #include <stdbool.h>
46009ea47eSEdward Tomasz Napierala #include <stdio.h>
47009ea47eSEdward Tomasz Napierala #include <stdint.h>
48009ea47eSEdward Tomasz Napierala #include <stdlib.h>
49009ea47eSEdward Tomasz Napierala #include <string.h>
50009ea47eSEdward Tomasz Napierala #include <unistd.h>
51009ea47eSEdward Tomasz Napierala 
52009ea47eSEdward Tomasz Napierala #include "ctld.h"
53829603e2SAlexander Motin #include "isns.h"
54009ea47eSEdward Tomasz Napierala 
55ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false;
56ba3a2d31SEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
58009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
59009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
60009ea47eSEdward Tomasz Napierala 
61009ea47eSEdward Tomasz Napierala static int nchildren = 0;
62*6b31e130SAlexander Motin static uint16_t last_portal_group_tag = 0;
63009ea47eSEdward Tomasz Napierala 
64009ea47eSEdward Tomasz Napierala static void
65009ea47eSEdward Tomasz Napierala usage(void)
66009ea47eSEdward Tomasz Napierala {
67009ea47eSEdward Tomasz Napierala 
68009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
69009ea47eSEdward Tomasz Napierala 	exit(1);
70009ea47eSEdward Tomasz Napierala }
71009ea47eSEdward Tomasz Napierala 
72009ea47eSEdward Tomasz Napierala char *
73009ea47eSEdward Tomasz Napierala checked_strdup(const char *s)
74009ea47eSEdward Tomasz Napierala {
75009ea47eSEdward Tomasz Napierala 	char *c;
76009ea47eSEdward Tomasz Napierala 
77009ea47eSEdward Tomasz Napierala 	c = strdup(s);
78009ea47eSEdward Tomasz Napierala 	if (c == NULL)
79009ea47eSEdward Tomasz Napierala 		log_err(1, "strdup");
80009ea47eSEdward Tomasz Napierala 	return (c);
81009ea47eSEdward Tomasz Napierala }
82009ea47eSEdward Tomasz Napierala 
83009ea47eSEdward Tomasz Napierala struct conf *
84009ea47eSEdward Tomasz Napierala conf_new(void)
85009ea47eSEdward Tomasz Napierala {
86009ea47eSEdward Tomasz Napierala 	struct conf *conf;
87009ea47eSEdward Tomasz Napierala 
88009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
89009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
90009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
91920c6cbaSAlexander Motin 	TAILQ_INIT(&conf->conf_luns);
92009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
93009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
94009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
95829603e2SAlexander Motin 	TAILQ_INIT(&conf->conf_isns);
96009ea47eSEdward Tomasz Napierala 
97829603e2SAlexander Motin 	conf->conf_isns_period = 900;
98829603e2SAlexander Motin 	conf->conf_isns_timeout = 5;
99009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
100009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
101009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
102009ea47eSEdward Tomasz Napierala 
103009ea47eSEdward Tomasz Napierala 	return (conf);
104009ea47eSEdward Tomasz Napierala }
105009ea47eSEdward Tomasz Napierala 
106009ea47eSEdward Tomasz Napierala void
107009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
108009ea47eSEdward Tomasz Napierala {
109920c6cbaSAlexander Motin 	struct lun *lun, *ltmp;
110009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
111009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
112009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
113829603e2SAlexander Motin 	struct isns *is, *istmp;
114009ea47eSEdward Tomasz Napierala 
115009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
116009ea47eSEdward Tomasz Napierala 
117920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
118920c6cbaSAlexander Motin 		lun_delete(lun);
119009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
120009ea47eSEdward Tomasz Napierala 		target_delete(targ);
121009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
122009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
123009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
124009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
125829603e2SAlexander Motin 	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
126829603e2SAlexander Motin 		isns_delete(is);
127009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
128009ea47eSEdward Tomasz Napierala 	free(conf);
129009ea47eSEdward Tomasz Napierala }
130009ea47eSEdward Tomasz Napierala 
131009ea47eSEdward Tomasz Napierala static struct auth *
132009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
133009ea47eSEdward Tomasz Napierala {
134009ea47eSEdward Tomasz Napierala 	struct auth *auth;
135009ea47eSEdward Tomasz Napierala 
136009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
137009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
138009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
139009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
140009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
141009ea47eSEdward Tomasz Napierala 	return (auth);
142009ea47eSEdward Tomasz Napierala }
143009ea47eSEdward Tomasz Napierala 
144009ea47eSEdward Tomasz Napierala static void
145009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
146009ea47eSEdward Tomasz Napierala {
147009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
148009ea47eSEdward Tomasz Napierala 
149009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
150009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
151009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
152009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
153009ea47eSEdward Tomasz Napierala 	free(auth);
154009ea47eSEdward Tomasz Napierala }
155009ea47eSEdward Tomasz Napierala 
156009ea47eSEdward Tomasz Napierala const struct auth *
157d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user)
158009ea47eSEdward Tomasz Napierala {
159009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
160009ea47eSEdward Tomasz Napierala 
161009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
162009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
163009ea47eSEdward Tomasz Napierala 			return (auth);
164009ea47eSEdward Tomasz Napierala 	}
165009ea47eSEdward Tomasz Napierala 
166009ea47eSEdward Tomasz Napierala 	return (NULL);
167009ea47eSEdward Tomasz Napierala }
168009ea47eSEdward Tomasz Napierala 
1691658628dSEdward Tomasz Napierala static void
1701658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1711658628dSEdward Tomasz Napierala {
1721658628dSEdward Tomasz Napierala 	size_t len;
1731658628dSEdward Tomasz Napierala 
1741658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1751658628dSEdward Tomasz Napierala 	if (len > 16) {
1761658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1771658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1781658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1791658628dSEdward Tomasz Napierala 			    "long", auth->a_user, 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 long; it should be at most 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 	if (len < 12) {
1871658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1881658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1891658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1901658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1911658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1921658628dSEdward Tomasz Napierala 		else
1931658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1941658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 16 characters "
1951658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
196f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1971658628dSEdward Tomasz Napierala 	}
1981658628dSEdward Tomasz Napierala 
1991658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
2001658628dSEdward Tomasz Napierala 		len = strlen(auth->a_secret);
2011658628dSEdward Tomasz Napierala 		if (len > 16) {
2021658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2031658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2041658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
2051658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2061658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2071658628dSEdward Tomasz Napierala 			else
2081658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2091658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
2101658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
2111658628dSEdward Tomasz Napierala 				    auth->a_user,
212f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2131658628dSEdward Tomasz Napierala 		}
2141658628dSEdward Tomasz Napierala 		if (len < 12) {
2151658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
2161658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2171658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2181658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2191658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2201658628dSEdward Tomasz Napierala 			else
2211658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2221658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2231658628dSEdward Tomasz Napierala 				    "at least 16 characters long",
2241658628dSEdward Tomasz Napierala 				    auth->a_user,
225f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2261658628dSEdward Tomasz Napierala 		}
2271658628dSEdward Tomasz Napierala 	}
2281658628dSEdward Tomasz Napierala }
2291658628dSEdward Tomasz Napierala 
2301658628dSEdward Tomasz Napierala const struct auth *
2311658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2321658628dSEdward Tomasz Napierala     const char *secret)
2331658628dSEdward Tomasz Napierala {
2341658628dSEdward Tomasz Napierala 	struct auth *auth;
2351658628dSEdward Tomasz Napierala 
2361658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2371658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2381658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2391658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2401658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2411658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2421658628dSEdward Tomasz Napierala 		else
2431658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2441658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
245f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2461658628dSEdward Tomasz Napierala 		return (NULL);
2471658628dSEdward Tomasz Napierala 	}
2481658628dSEdward Tomasz Napierala 
2491658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2501658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2511658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2521658628dSEdward Tomasz Napierala 
2531658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2541658628dSEdward Tomasz Napierala 
2551658628dSEdward Tomasz Napierala 	return (auth);
2561658628dSEdward Tomasz Napierala }
2571658628dSEdward Tomasz Napierala 
2581658628dSEdward Tomasz Napierala const struct auth *
2591658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2601658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2611658628dSEdward Tomasz Napierala {
2621658628dSEdward Tomasz Napierala 	struct auth *auth;
2631658628dSEdward Tomasz Napierala 
2641658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2651658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2661658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2671658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2681658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2691658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2701658628dSEdward Tomasz Napierala 			    ag->ag_name);
2711658628dSEdward Tomasz Napierala 		else
2721658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2731658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
274f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2751658628dSEdward Tomasz Napierala 		return (NULL);
2761658628dSEdward Tomasz Napierala 	}
2771658628dSEdward Tomasz Napierala 
2781658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2791658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2801658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2811658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2821658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2831658628dSEdward Tomasz Napierala 
2841658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2851658628dSEdward Tomasz Napierala 
2861658628dSEdward Tomasz Napierala 	return (auth);
2871658628dSEdward Tomasz Napierala }
2881658628dSEdward Tomasz Napierala 
2898cb2e958SEdward Tomasz Napierala const struct auth_name *
2908cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2918cb2e958SEdward Tomasz Napierala {
2928cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
2938cb2e958SEdward Tomasz Napierala 
2948cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
2958cb2e958SEdward Tomasz Napierala 	if (an == NULL)
2968cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
2978cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
2988cb2e958SEdward Tomasz Napierala 	an->an_initator_name = checked_strdup(name);
2998cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
3008cb2e958SEdward Tomasz Napierala 	return (an);
3018cb2e958SEdward Tomasz Napierala }
3028cb2e958SEdward Tomasz Napierala 
3038cb2e958SEdward Tomasz Napierala static void
3048cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
3058cb2e958SEdward Tomasz Napierala {
3068cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
3078cb2e958SEdward Tomasz Napierala 
3088cb2e958SEdward Tomasz Napierala 	free(an->an_initator_name);
3098cb2e958SEdward Tomasz Napierala 	free(an);
3108cb2e958SEdward Tomasz Napierala }
3118cb2e958SEdward Tomasz Napierala 
3128cb2e958SEdward Tomasz Napierala bool
3138cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
3148cb2e958SEdward Tomasz Napierala {
3158cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
3168cb2e958SEdward Tomasz Napierala 		return (false);
3178cb2e958SEdward Tomasz Napierala 	return (true);
3188cb2e958SEdward Tomasz Napierala }
3198cb2e958SEdward Tomasz Napierala 
3208cb2e958SEdward Tomasz Napierala const struct auth_name *
3218cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3228cb2e958SEdward Tomasz Napierala {
3238cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3248cb2e958SEdward Tomasz Napierala 
3258cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
3268cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_name->an_initator_name, name) == 0)
3278cb2e958SEdward Tomasz Napierala 			return (auth_name);
3288cb2e958SEdward Tomasz Napierala 	}
3298cb2e958SEdward Tomasz Napierala 
3308cb2e958SEdward Tomasz Napierala 	return (NULL);
3318cb2e958SEdward Tomasz Napierala }
3328cb2e958SEdward Tomasz Napierala 
333e867e162SEdward Tomasz Napierala int
334e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name)
335e867e162SEdward Tomasz Napierala {
336e867e162SEdward Tomasz Napierala 	if (!auth_name_defined(ag))
337e867e162SEdward Tomasz Napierala 		return (0);
338e867e162SEdward Tomasz Napierala 
339e867e162SEdward Tomasz Napierala 	if (auth_name_find(ag, initiator_name) == NULL)
340e867e162SEdward Tomasz Napierala 		return (1);
341e867e162SEdward Tomasz Napierala 
342e867e162SEdward Tomasz Napierala 	return (0);
343e867e162SEdward Tomasz Napierala }
344e867e162SEdward Tomasz Napierala 
3458cb2e958SEdward Tomasz Napierala const struct auth_portal *
3468cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3478cb2e958SEdward Tomasz Napierala {
3488cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
349073edb1cSAlexander Motin 	char *net, *mask, *str, *tmp;
350073edb1cSAlexander Motin 	int len, dm, m;
3518cb2e958SEdward Tomasz Napierala 
3528cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3538cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3548cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3558cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
3568cb2e958SEdward Tomasz Napierala 	ap->ap_initator_portal = checked_strdup(portal);
357073edb1cSAlexander Motin 	mask = str = checked_strdup(portal);
358073edb1cSAlexander Motin 	net = strsep(&mask, "/");
359073edb1cSAlexander Motin 	if (net[0] == '[')
360073edb1cSAlexander Motin 		net++;
361073edb1cSAlexander Motin 	len = strlen(net);
362073edb1cSAlexander Motin 	if (len == 0)
363073edb1cSAlexander Motin 		goto error;
364073edb1cSAlexander Motin 	if (net[len - 1] == ']')
365073edb1cSAlexander Motin 		net[len - 1] = 0;
366073edb1cSAlexander Motin 	if (strchr(net, ':') != NULL) {
367073edb1cSAlexander Motin 		struct sockaddr_in6 *sin6 =
368073edb1cSAlexander Motin 		    (struct sockaddr_in6 *)&ap->ap_sa;
369073edb1cSAlexander Motin 
370073edb1cSAlexander Motin 		sin6->sin6_len = sizeof(*sin6);
371073edb1cSAlexander Motin 		sin6->sin6_family = AF_INET6;
372073edb1cSAlexander Motin 		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
373073edb1cSAlexander Motin 			goto error;
374073edb1cSAlexander Motin 		dm = 128;
375073edb1cSAlexander Motin 	} else {
376073edb1cSAlexander Motin 		struct sockaddr_in *sin =
377073edb1cSAlexander Motin 		    (struct sockaddr_in *)&ap->ap_sa;
378073edb1cSAlexander Motin 
379073edb1cSAlexander Motin 		sin->sin_len = sizeof(*sin);
380073edb1cSAlexander Motin 		sin->sin_family = AF_INET;
381073edb1cSAlexander Motin 		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
382073edb1cSAlexander Motin 			goto error;
383073edb1cSAlexander Motin 		dm = 32;
384073edb1cSAlexander Motin 	}
385073edb1cSAlexander Motin 	if (mask != NULL) {
386073edb1cSAlexander Motin 		m = strtol(mask, &tmp, 0);
387073edb1cSAlexander Motin 		if (m < 0 || m > dm || tmp[0] != 0)
388073edb1cSAlexander Motin 			goto error;
389073edb1cSAlexander Motin 	} else
390073edb1cSAlexander Motin 		m = dm;
391073edb1cSAlexander Motin 	ap->ap_mask = m;
392073edb1cSAlexander Motin 	free(str);
3938cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
3948cb2e958SEdward Tomasz Napierala 	return (ap);
395073edb1cSAlexander Motin 
396073edb1cSAlexander Motin error:
397073edb1cSAlexander Motin 	log_errx(1, "Incorrect initiator portal '%s'", portal);
398073edb1cSAlexander Motin 	return (NULL);
3998cb2e958SEdward Tomasz Napierala }
4008cb2e958SEdward Tomasz Napierala 
4018cb2e958SEdward Tomasz Napierala static void
4028cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
4038cb2e958SEdward Tomasz Napierala {
4048cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
4058cb2e958SEdward Tomasz Napierala 
4068cb2e958SEdward Tomasz Napierala 	free(ap->ap_initator_portal);
4078cb2e958SEdward Tomasz Napierala 	free(ap);
4088cb2e958SEdward Tomasz Napierala }
4098cb2e958SEdward Tomasz Napierala 
4108cb2e958SEdward Tomasz Napierala bool
4118cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
4128cb2e958SEdward Tomasz Napierala {
4138cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
4148cb2e958SEdward Tomasz Napierala 		return (false);
4158cb2e958SEdward Tomasz Napierala 	return (true);
4168cb2e958SEdward Tomasz Napierala }
4178cb2e958SEdward Tomasz Napierala 
4188cb2e958SEdward Tomasz Napierala const struct auth_portal *
419073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
4208cb2e958SEdward Tomasz Napierala {
421073edb1cSAlexander Motin 	const struct auth_portal *ap;
4225e46a066SAlexander Motin 	const uint8_t *a, *b;
423073edb1cSAlexander Motin 	int i;
4245e46a066SAlexander Motin 	uint8_t bmask;
4258cb2e958SEdward Tomasz Napierala 
426073edb1cSAlexander Motin 	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
427073edb1cSAlexander Motin 		if (ap->ap_sa.ss_family != ss->ss_family)
428073edb1cSAlexander Motin 			continue;
429073edb1cSAlexander Motin 		if (ss->ss_family == AF_INET) {
4305e46a066SAlexander Motin 			a = (const uint8_t *)
4315e46a066SAlexander Motin 			    &((const struct sockaddr_in *)ss)->sin_addr;
4325e46a066SAlexander Motin 			b = (const uint8_t *)
4335e46a066SAlexander Motin 			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
434073edb1cSAlexander Motin 		} else {
4355e46a066SAlexander Motin 			a = (const uint8_t *)
4365e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
4375e46a066SAlexander Motin 			b = (const uint8_t *)
4385e46a066SAlexander Motin 			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
439073edb1cSAlexander Motin 		}
440073edb1cSAlexander Motin 		for (i = 0; i < ap->ap_mask / 8; i++) {
441073edb1cSAlexander Motin 			if (a[i] != b[i])
442073edb1cSAlexander Motin 				goto next;
443073edb1cSAlexander Motin 		}
444073edb1cSAlexander Motin 		if (ap->ap_mask % 8) {
445073edb1cSAlexander Motin 			bmask = 0xff << (8 - (ap->ap_mask % 8));
446073edb1cSAlexander Motin 			if ((a[i] & bmask) != (b[i] & bmask))
447073edb1cSAlexander Motin 				goto next;
448073edb1cSAlexander Motin 		}
449073edb1cSAlexander Motin 		return (ap);
450073edb1cSAlexander Motin next:
451073edb1cSAlexander Motin 		;
4528cb2e958SEdward Tomasz Napierala 	}
4538cb2e958SEdward Tomasz Napierala 
4548cb2e958SEdward Tomasz Napierala 	return (NULL);
4558cb2e958SEdward Tomasz Napierala }
4568cb2e958SEdward Tomasz Napierala 
457e867e162SEdward Tomasz Napierala int
458e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
459e867e162SEdward Tomasz Napierala {
460e867e162SEdward Tomasz Napierala 
461e867e162SEdward Tomasz Napierala 	if (!auth_portal_defined(ag))
462e867e162SEdward Tomasz Napierala 		return (0);
463e867e162SEdward Tomasz Napierala 
464e867e162SEdward Tomasz Napierala 	if (auth_portal_find(ag, sa) == NULL)
465e867e162SEdward Tomasz Napierala 		return (1);
466e867e162SEdward Tomasz Napierala 
467e867e162SEdward Tomasz Napierala 	return (0);
468e867e162SEdward Tomasz Napierala }
469e867e162SEdward Tomasz Napierala 
470009ea47eSEdward Tomasz Napierala struct auth_group *
471009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
472009ea47eSEdward Tomasz Napierala {
473009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
474009ea47eSEdward Tomasz Napierala 
475009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
476009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
477009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
478009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
479009ea47eSEdward Tomasz Napierala 			return (NULL);
480009ea47eSEdward Tomasz Napierala 		}
481009ea47eSEdward Tomasz Napierala 	}
482009ea47eSEdward Tomasz Napierala 
483009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
484009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
485009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
486009ea47eSEdward Tomasz Napierala 	if (name != NULL)
487009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
488009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
4898cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
4908cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
491009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
492009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
493009ea47eSEdward Tomasz Napierala 
494009ea47eSEdward Tomasz Napierala 	return (ag);
495009ea47eSEdward Tomasz Napierala }
496009ea47eSEdward Tomasz Napierala 
497009ea47eSEdward Tomasz Napierala void
498009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
499009ea47eSEdward Tomasz Napierala {
5008cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
5018cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
5028cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
503009ea47eSEdward Tomasz Napierala 
504009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
505009ea47eSEdward Tomasz Napierala 
5068cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
507009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
5088cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
5098cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
5108cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
5118cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
5128cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
513009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
514009ea47eSEdward Tomasz Napierala 	free(ag);
515009ea47eSEdward Tomasz Napierala }
516009ea47eSEdward Tomasz Napierala 
517009ea47eSEdward Tomasz Napierala struct auth_group *
518d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name)
519009ea47eSEdward Tomasz Napierala {
520009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
521009ea47eSEdward Tomasz Napierala 
522009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
523009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
524009ea47eSEdward Tomasz Napierala 			return (ag);
525009ea47eSEdward Tomasz Napierala 	}
526009ea47eSEdward Tomasz Napierala 
527009ea47eSEdward Tomasz Napierala 	return (NULL);
528009ea47eSEdward Tomasz Napierala }
529009ea47eSEdward Tomasz Napierala 
530df9900fbSEdward Tomasz Napierala int
531bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str)
532df9900fbSEdward Tomasz Napierala {
533bd833b11SEdward Tomasz Napierala 	int type;
534df9900fbSEdward Tomasz Napierala 
535df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
536df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
537e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
538e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
539df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
540df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
541df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
542df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
543df9900fbSEdward Tomasz Napierala 	} else {
544df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
545df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
546df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
547df9900fbSEdward Tomasz Napierala 		else
548df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
549df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
550df9900fbSEdward Tomasz Napierala 		return (1);
551df9900fbSEdward Tomasz Napierala 	}
552df9900fbSEdward Tomasz Napierala 
553bd833b11SEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
554bd833b11SEdward Tomasz Napierala 		if (ag->ag_name != NULL) {
555df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
556df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
557df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
558bd833b11SEdward Tomasz Napierala 		} else {
559df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
560df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
561df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
562bd833b11SEdward Tomasz Napierala 		}
563df9900fbSEdward Tomasz Napierala 		return (1);
564df9900fbSEdward Tomasz Napierala 	}
565df9900fbSEdward Tomasz Napierala 
566bd833b11SEdward Tomasz Napierala 	ag->ag_type = type;
567bd833b11SEdward Tomasz Napierala 
568bd833b11SEdward Tomasz Napierala 	return (0);
569df9900fbSEdward Tomasz Napierala }
570df9900fbSEdward Tomasz Napierala 
571009ea47eSEdward Tomasz Napierala static struct portal *
572009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
573009ea47eSEdward Tomasz Napierala {
574009ea47eSEdward Tomasz Napierala 	struct portal *portal;
575009ea47eSEdward Tomasz Napierala 
576009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
577009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
578009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
579009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
580009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
581009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
582009ea47eSEdward Tomasz Napierala 	return (portal);
583009ea47eSEdward Tomasz Napierala }
584009ea47eSEdward Tomasz Napierala 
585009ea47eSEdward Tomasz Napierala static void
586009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
587009ea47eSEdward Tomasz Napierala {
58844a5953aSEdward Tomasz Napierala 
589009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
59044a5953aSEdward Tomasz Napierala 	if (portal->p_ai != NULL)
591009ea47eSEdward Tomasz Napierala 		freeaddrinfo(portal->p_ai);
592009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
593009ea47eSEdward Tomasz Napierala 	free(portal);
594009ea47eSEdward Tomasz Napierala }
595009ea47eSEdward Tomasz Napierala 
596009ea47eSEdward Tomasz Napierala struct portal_group *
597009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
598009ea47eSEdward Tomasz Napierala {
599009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
600009ea47eSEdward Tomasz Napierala 
601009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
602009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
603009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
604009ea47eSEdward Tomasz Napierala 		return (NULL);
605009ea47eSEdward Tomasz Napierala 	}
606009ea47eSEdward Tomasz Napierala 
607009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
608009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
609009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
610009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
611009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
612009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
613*6b31e130SAlexander Motin 	pg->pg_tag = 0;		/* Assigned later in conf_apply(). */
614009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
615009ea47eSEdward Tomasz Napierala 
616009ea47eSEdward Tomasz Napierala 	return (pg);
617009ea47eSEdward Tomasz Napierala }
618009ea47eSEdward Tomasz Napierala 
619009ea47eSEdward Tomasz Napierala void
620009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
621009ea47eSEdward Tomasz Napierala {
622009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
623009ea47eSEdward Tomasz Napierala 
624009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
625009ea47eSEdward Tomasz Napierala 
626009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
627009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
628009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
6296dbdbf82SEdward Tomasz Napierala 	free(pg->pg_redirection);
630009ea47eSEdward Tomasz Napierala 	free(pg);
631009ea47eSEdward Tomasz Napierala }
632009ea47eSEdward Tomasz Napierala 
633009ea47eSEdward Tomasz Napierala struct portal_group *
634d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name)
635009ea47eSEdward Tomasz Napierala {
636009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
637009ea47eSEdward Tomasz Napierala 
638009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
639009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
640009ea47eSEdward Tomasz Napierala 			return (pg);
641009ea47eSEdward Tomasz Napierala 	}
642009ea47eSEdward Tomasz Napierala 
643009ea47eSEdward Tomasz Napierala 	return (NULL);
644009ea47eSEdward Tomasz Napierala }
645009ea47eSEdward Tomasz Napierala 
646829603e2SAlexander Motin static int
647829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
648009ea47eSEdward Tomasz Napierala {
649009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
6501606ad8cSAlexander Motin 	char *str, *addr, *ch;
651009ea47eSEdward Tomasz Napierala 	const char *port;
652009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
653009ea47eSEdward Tomasz Napierala 
6541606ad8cSAlexander Motin 	str = arg = strdup(arg);
655009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
656009ea47eSEdward Tomasz Napierala 		/*
657009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
658009ea47eSEdward Tomasz Napierala 		 */
659009ea47eSEdward Tomasz Napierala 		arg++;
660009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
661829603e2SAlexander Motin 		if (arg == NULL)
662009ea47eSEdward Tomasz Napierala 			return (1);
663009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
664829603e2SAlexander Motin 			port = def_port;
665009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
666009ea47eSEdward Tomasz Napierala 			port = arg + 1;
6671606ad8cSAlexander Motin 		} else {
6681606ad8cSAlexander Motin 			free(str);
669009ea47eSEdward Tomasz Napierala 			return (1);
6701606ad8cSAlexander Motin 		}
671009ea47eSEdward Tomasz Napierala 	} else {
672009ea47eSEdward Tomasz Napierala 		/*
673009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
674009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
675009ea47eSEdward Tomasz Napierala 		 */
676009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
677009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
678009ea47eSEdward Tomasz Napierala 				colons++;
679009ea47eSEdward Tomasz Napierala 		}
680009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
681009ea47eSEdward Tomasz Napierala 			addr = arg;
682829603e2SAlexander Motin 			port = def_port;
683009ea47eSEdward Tomasz Napierala 		} else {
684009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
685009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
686829603e2SAlexander Motin 				port = def_port;
687009ea47eSEdward Tomasz Napierala 			else
688009ea47eSEdward Tomasz Napierala 				port = arg;
689009ea47eSEdward Tomasz Napierala 		}
690009ea47eSEdward Tomasz Napierala 	}
691009ea47eSEdward Tomasz Napierala 
692009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
693009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
694009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
695009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
696829603e2SAlexander Motin 	error = getaddrinfo(addr, port, &hints, ai);
6971606ad8cSAlexander Motin 	free(str);
6981606ad8cSAlexander Motin 	return ((error != 0) ? 1 : 0);
699829603e2SAlexander Motin }
700009ea47eSEdward Tomasz Napierala 
701829603e2SAlexander Motin int
702829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
703829603e2SAlexander Motin {
704829603e2SAlexander Motin 	struct portal *portal;
705829603e2SAlexander Motin 
706829603e2SAlexander Motin 	portal = portal_new(pg);
707829603e2SAlexander Motin 	portal->p_listen = checked_strdup(value);
708829603e2SAlexander Motin 	portal->p_iser = iser;
709829603e2SAlexander Motin 
710829603e2SAlexander Motin 	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
711829603e2SAlexander Motin 		log_warnx("invalid listen address %s", portal->p_listen);
71244a5953aSEdward Tomasz Napierala 		portal_delete(portal);
713009ea47eSEdward Tomasz Napierala 		return (1);
714009ea47eSEdward Tomasz Napierala 	}
715009ea47eSEdward Tomasz Napierala 
716009ea47eSEdward Tomasz Napierala 	/*
717009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
718009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
719009ea47eSEdward Tomasz Napierala 	 */
720009ea47eSEdward Tomasz Napierala 
721009ea47eSEdward Tomasz Napierala 	return (0);
722009ea47eSEdward Tomasz Napierala }
723009ea47eSEdward Tomasz Napierala 
724829603e2SAlexander Motin int
725829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr)
726829603e2SAlexander Motin {
727829603e2SAlexander Motin 	struct isns *isns;
728829603e2SAlexander Motin 
729829603e2SAlexander Motin 	isns = calloc(1, sizeof(*isns));
730829603e2SAlexander Motin 	if (isns == NULL)
731829603e2SAlexander Motin 		log_err(1, "calloc");
732829603e2SAlexander Motin 	isns->i_conf = conf;
733829603e2SAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
734829603e2SAlexander Motin 	isns->i_addr = checked_strdup(addr);
735829603e2SAlexander Motin 
736829603e2SAlexander Motin 	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
737829603e2SAlexander Motin 		log_warnx("invalid iSNS address %s", isns->i_addr);
738829603e2SAlexander Motin 		isns_delete(isns);
739829603e2SAlexander Motin 		return (1);
740829603e2SAlexander Motin 	}
741829603e2SAlexander Motin 
742829603e2SAlexander Motin 	/*
743829603e2SAlexander Motin 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
744829603e2SAlexander Motin 	 *	those into multiple servers.
745829603e2SAlexander Motin 	 */
746829603e2SAlexander Motin 
747829603e2SAlexander Motin 	return (0);
748829603e2SAlexander Motin }
749829603e2SAlexander Motin 
750829603e2SAlexander Motin void
751829603e2SAlexander Motin isns_delete(struct isns *isns)
752829603e2SAlexander Motin {
753829603e2SAlexander Motin 
754829603e2SAlexander Motin 	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
755829603e2SAlexander Motin 	free(isns->i_addr);
756829603e2SAlexander Motin 	if (isns->i_ai != NULL)
757829603e2SAlexander Motin 		freeaddrinfo(isns->i_ai);
758829603e2SAlexander Motin 	free(isns);
759829603e2SAlexander Motin }
760829603e2SAlexander Motin 
761829603e2SAlexander Motin static int
762829603e2SAlexander Motin isns_do_connect(struct isns *isns)
763829603e2SAlexander Motin {
764829603e2SAlexander Motin 	int s;
765829603e2SAlexander Motin 
766829603e2SAlexander Motin 	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
767829603e2SAlexander Motin 	    isns->i_ai->ai_protocol);
768829603e2SAlexander Motin 	if (s < 0) {
769829603e2SAlexander Motin 		log_warn("socket(2) failed for %s", isns->i_addr);
770829603e2SAlexander Motin 		return (-1);
771829603e2SAlexander Motin 	}
772829603e2SAlexander Motin 	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
773829603e2SAlexander Motin 		log_warn("connect(2) failed for %s", isns->i_addr);
774829603e2SAlexander Motin 		close(s);
775829603e2SAlexander Motin 		return (-1);
776829603e2SAlexander Motin 	}
777829603e2SAlexander Motin 	return(s);
778829603e2SAlexander Motin }
779829603e2SAlexander Motin 
780829603e2SAlexander Motin static int
781829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname)
782829603e2SAlexander Motin {
783829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
784829603e2SAlexander Motin 	struct target *target;
785829603e2SAlexander Motin 	struct portal *portal;
786829603e2SAlexander Motin 	struct portal_group *pg;
787829603e2SAlexander Motin 	struct isns_req *req;
788829603e2SAlexander Motin 	int res = 0;
789829603e2SAlexander Motin 	uint32_t error;
790829603e2SAlexander Motin 
791829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
792829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
793829603e2SAlexander Motin 	isns_req_add_delim(req);
794829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
795829603e2SAlexander Motin 	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
796829603e2SAlexander Motin 	isns_req_add_32(req, 6, conf->conf_isns_period);
797829603e2SAlexander Motin 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
798829603e2SAlexander Motin 		if (pg->pg_unassigned)
799829603e2SAlexander Motin 			continue;
800829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
801829603e2SAlexander Motin 			isns_req_add_addr(req, 16, portal->p_ai);
802829603e2SAlexander Motin 			isns_req_add_port(req, 17, portal->p_ai);
803829603e2SAlexander Motin 		}
804829603e2SAlexander Motin 	}
805829603e2SAlexander Motin 	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
806829603e2SAlexander Motin 		isns_req_add_str(req, 32, target->t_name);
807829603e2SAlexander Motin 		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
808829603e2SAlexander Motin 		if (target->t_alias != NULL)
809829603e2SAlexander Motin 			isns_req_add_str(req, 34, target->t_alias);
810829603e2SAlexander Motin 		pg = target->t_portal_group;
811829603e2SAlexander Motin 		isns_req_add_32(req, 51, pg->pg_tag);
812829603e2SAlexander Motin 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
813829603e2SAlexander Motin 			isns_req_add_addr(req, 49, portal->p_ai);
814829603e2SAlexander Motin 			isns_req_add_port(req, 50, portal->p_ai);
815829603e2SAlexander Motin 		}
816829603e2SAlexander Motin 	}
817829603e2SAlexander Motin 	res = isns_req_send(s, req);
818829603e2SAlexander Motin 	if (res < 0) {
819829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
820829603e2SAlexander Motin 		goto quit;
821829603e2SAlexander Motin 	}
822829603e2SAlexander Motin 	res = isns_req_receive(s, req);
823829603e2SAlexander Motin 	if (res < 0) {
824829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
825829603e2SAlexander Motin 		goto quit;
826829603e2SAlexander Motin 	}
827829603e2SAlexander Motin 	error = isns_req_get_status(req);
828829603e2SAlexander Motin 	if (error != 0) {
829829603e2SAlexander Motin 		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
830829603e2SAlexander Motin 		res = -1;
831829603e2SAlexander Motin 	}
832829603e2SAlexander Motin quit:
833829603e2SAlexander Motin 	isns_req_free(req);
834829603e2SAlexander Motin 	return (res);
835829603e2SAlexander Motin }
836829603e2SAlexander Motin 
837829603e2SAlexander Motin static int
838829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname)
839829603e2SAlexander Motin {
840829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
841829603e2SAlexander Motin 	struct isns_req *req;
842829603e2SAlexander Motin 	int res = 0;
843829603e2SAlexander Motin 	uint32_t error;
844829603e2SAlexander Motin 
845829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
846829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
847829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
848829603e2SAlexander Motin 	isns_req_add_delim(req);
849829603e2SAlexander Motin 	isns_req_add(req, 2, 0, NULL);
850829603e2SAlexander Motin 	res = isns_req_send(s, req);
851829603e2SAlexander Motin 	if (res < 0) {
852829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
853829603e2SAlexander Motin 		goto quit;
854829603e2SAlexander Motin 	}
855829603e2SAlexander Motin 	res = isns_req_receive(s, req);
856829603e2SAlexander Motin 	if (res < 0) {
857829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
858829603e2SAlexander Motin 		goto quit;
859829603e2SAlexander Motin 	}
860829603e2SAlexander Motin 	error = isns_req_get_status(req);
861829603e2SAlexander Motin 	if (error != 0) {
862829603e2SAlexander Motin 		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
863829603e2SAlexander Motin 		res = -1;
864829603e2SAlexander Motin 	}
865829603e2SAlexander Motin quit:
866829603e2SAlexander Motin 	isns_req_free(req);
867829603e2SAlexander Motin 	return (res);
868829603e2SAlexander Motin }
869829603e2SAlexander Motin 
870829603e2SAlexander Motin static int
871829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname)
872829603e2SAlexander Motin {
873829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
874829603e2SAlexander Motin 	struct isns_req *req;
875829603e2SAlexander Motin 	int res = 0;
876829603e2SAlexander Motin 	uint32_t error;
877829603e2SAlexander Motin 
878829603e2SAlexander Motin 	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
879829603e2SAlexander Motin 	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
880829603e2SAlexander Motin 	isns_req_add_delim(req);
881829603e2SAlexander Motin 	isns_req_add_str(req, 1, hostname);
882829603e2SAlexander Motin 	res = isns_req_send(s, req);
883829603e2SAlexander Motin 	if (res < 0) {
884829603e2SAlexander Motin 		log_warn("send(2) failed for %s", isns->i_addr);
885829603e2SAlexander Motin 		goto quit;
886829603e2SAlexander Motin 	}
887829603e2SAlexander Motin 	res = isns_req_receive(s, req);
888829603e2SAlexander Motin 	if (res < 0) {
889829603e2SAlexander Motin 		log_warn("receive(2) failed for %s", isns->i_addr);
890829603e2SAlexander Motin 		goto quit;
891829603e2SAlexander Motin 	}
892829603e2SAlexander Motin 	error = isns_req_get_status(req);
893829603e2SAlexander Motin 	if (error != 0) {
894829603e2SAlexander Motin 		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
895829603e2SAlexander Motin 		res = -1;
896829603e2SAlexander Motin 	}
897829603e2SAlexander Motin quit:
898829603e2SAlexander Motin 	isns_req_free(req);
899829603e2SAlexander Motin 	return (res);
900829603e2SAlexander Motin }
901829603e2SAlexander Motin 
902829603e2SAlexander Motin void
903829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns)
904829603e2SAlexander Motin {
905829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
906d5cc4a4aSAlexander Motin 	int s;
907829603e2SAlexander Motin 	char hostname[256];
908829603e2SAlexander Motin 
909829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
910829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
911829603e2SAlexander Motin 		return;
912829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
913829603e2SAlexander Motin 	s = isns_do_connect(isns);
914829603e2SAlexander Motin 	if (s < 0) {
915829603e2SAlexander Motin 		set_timeout(0, false);
916829603e2SAlexander Motin 		return;
917829603e2SAlexander Motin 	}
918829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
919829603e2SAlexander Motin 
920829603e2SAlexander Motin 	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
921829603e2SAlexander Motin 		oldisns = isns;
922d5cc4a4aSAlexander Motin 	isns_do_deregister(oldisns, s, hostname);
923d5cc4a4aSAlexander Motin 	isns_do_register(isns, s, hostname);
924829603e2SAlexander Motin 	close(s);
925829603e2SAlexander Motin 	set_timeout(0, false);
926829603e2SAlexander Motin }
927829603e2SAlexander Motin 
928829603e2SAlexander Motin void
929829603e2SAlexander Motin isns_check(struct isns *isns)
930829603e2SAlexander Motin {
931829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
932829603e2SAlexander Motin 	int s, res;
933829603e2SAlexander Motin 	char hostname[256];
934829603e2SAlexander Motin 
935829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
936829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
937829603e2SAlexander Motin 		return;
938829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
939829603e2SAlexander Motin 	s = isns_do_connect(isns);
940829603e2SAlexander Motin 	if (s < 0) {
941829603e2SAlexander Motin 		set_timeout(0, false);
942829603e2SAlexander Motin 		return;
943829603e2SAlexander Motin 	}
944829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
945829603e2SAlexander Motin 
946829603e2SAlexander Motin 	res = isns_do_check(isns, s, hostname);
947829603e2SAlexander Motin 	if (res < 0) {
948d5cc4a4aSAlexander Motin 		isns_do_deregister(isns, s, hostname);
949d5cc4a4aSAlexander Motin 		isns_do_register(isns, s, hostname);
950829603e2SAlexander Motin 	}
951829603e2SAlexander Motin 	close(s);
952829603e2SAlexander Motin 	set_timeout(0, false);
953829603e2SAlexander Motin }
954829603e2SAlexander Motin 
955829603e2SAlexander Motin void
956829603e2SAlexander Motin isns_deregister(struct isns *isns)
957829603e2SAlexander Motin {
958829603e2SAlexander Motin 	struct conf *conf = isns->i_conf;
959d5cc4a4aSAlexander Motin 	int s;
960829603e2SAlexander Motin 	char hostname[256];
961829603e2SAlexander Motin 
962829603e2SAlexander Motin 	if (TAILQ_EMPTY(&conf->conf_targets) ||
963829603e2SAlexander Motin 	    TAILQ_EMPTY(&conf->conf_portal_groups))
964829603e2SAlexander Motin 		return;
965829603e2SAlexander Motin 	set_timeout(conf->conf_isns_timeout, false);
966829603e2SAlexander Motin 	s = isns_do_connect(isns);
967829603e2SAlexander Motin 	if (s < 0)
968829603e2SAlexander Motin 		return;
969829603e2SAlexander Motin 	gethostname(hostname, sizeof(hostname));
970829603e2SAlexander Motin 
971d5cc4a4aSAlexander Motin 	isns_do_deregister(isns, s, hostname);
972829603e2SAlexander Motin 	close(s);
973829603e2SAlexander Motin 	set_timeout(0, false);
974829603e2SAlexander Motin }
975829603e2SAlexander Motin 
97605374883SEdward Tomasz Napierala int
977bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str)
97805374883SEdward Tomasz Napierala {
979bd833b11SEdward Tomasz Napierala 	int filter;
98005374883SEdward Tomasz Napierala 
98105374883SEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
98205374883SEdward Tomasz Napierala 		filter = PG_FILTER_NONE;
98305374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal") == 0) {
98405374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL;
98505374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name") == 0) {
98605374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME;
98705374883SEdward Tomasz Napierala 	} else if (strcmp(str, "portal-name-auth") == 0) {
98805374883SEdward Tomasz Napierala 		filter = PG_FILTER_PORTAL_NAME_AUTH;
98905374883SEdward Tomasz Napierala 	} else {
99005374883SEdward Tomasz Napierala 		log_warnx("invalid discovery-filter \"%s\" for portal-group "
99105374883SEdward Tomasz Napierala 		    "\"%s\"; valid values are \"none\", \"portal\", "
99205374883SEdward Tomasz Napierala 		    "\"portal-name\", and \"portal-name-auth\"",
99305374883SEdward Tomasz Napierala 		    str, pg->pg_name);
99405374883SEdward Tomasz Napierala 		return (1);
99505374883SEdward Tomasz Napierala 	}
99605374883SEdward Tomasz Napierala 
997bd833b11SEdward Tomasz Napierala 	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
998bd833b11SEdward Tomasz Napierala 	    pg->pg_discovery_filter != filter) {
99905374883SEdward Tomasz Napierala 		log_warnx("cannot set discovery-filter to \"%s\" for "
100005374883SEdward Tomasz Napierala 		    "portal-group \"%s\"; already has a different "
100105374883SEdward Tomasz Napierala 		    "value", str, pg->pg_name);
100205374883SEdward Tomasz Napierala 		return (1);
100305374883SEdward Tomasz Napierala 	}
100405374883SEdward Tomasz Napierala 
1005bd833b11SEdward Tomasz Napierala 	pg->pg_discovery_filter = filter;
1006bd833b11SEdward Tomasz Napierala 
1007bd833b11SEdward Tomasz Napierala 	return (0);
100805374883SEdward Tomasz Napierala }
100905374883SEdward Tomasz Napierala 
10106dbdbf82SEdward Tomasz Napierala int
10116dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr)
10126dbdbf82SEdward Tomasz Napierala {
10136dbdbf82SEdward Tomasz Napierala 
10146dbdbf82SEdward Tomasz Napierala 	if (pg->pg_redirection != NULL) {
10156dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
10166dbdbf82SEdward Tomasz Napierala 		    "portal-group \"%s\"; already defined",
10176dbdbf82SEdward Tomasz Napierala 		    addr, pg->pg_name);
10186dbdbf82SEdward Tomasz Napierala 		return (1);
10196dbdbf82SEdward Tomasz Napierala 	}
10206dbdbf82SEdward Tomasz Napierala 
10216dbdbf82SEdward Tomasz Napierala 	pg->pg_redirection = checked_strdup(addr);
10226dbdbf82SEdward Tomasz Napierala 
10236dbdbf82SEdward Tomasz Napierala 	return (0);
10246dbdbf82SEdward Tomasz Napierala }
10256dbdbf82SEdward Tomasz Napierala 
1026009ea47eSEdward Tomasz Napierala static bool
1027009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
1028009ea47eSEdward Tomasz Napierala {
1029009ea47eSEdward Tomasz Napierala 	switch (ch) {
1030009ea47eSEdward Tomasz Napierala 	case '0':
1031009ea47eSEdward Tomasz Napierala 	case '1':
1032009ea47eSEdward Tomasz Napierala 	case '2':
1033009ea47eSEdward Tomasz Napierala 	case '3':
1034009ea47eSEdward Tomasz Napierala 	case '4':
1035009ea47eSEdward Tomasz Napierala 	case '5':
1036009ea47eSEdward Tomasz Napierala 	case '6':
1037009ea47eSEdward Tomasz Napierala 	case '7':
1038009ea47eSEdward Tomasz Napierala 	case '8':
1039009ea47eSEdward Tomasz Napierala 	case '9':
1040009ea47eSEdward Tomasz Napierala 	case 'a':
1041009ea47eSEdward Tomasz Napierala 	case 'A':
1042009ea47eSEdward Tomasz Napierala 	case 'b':
1043009ea47eSEdward Tomasz Napierala 	case 'B':
1044009ea47eSEdward Tomasz Napierala 	case 'c':
1045009ea47eSEdward Tomasz Napierala 	case 'C':
1046009ea47eSEdward Tomasz Napierala 	case 'd':
1047009ea47eSEdward Tomasz Napierala 	case 'D':
1048009ea47eSEdward Tomasz Napierala 	case 'e':
1049009ea47eSEdward Tomasz Napierala 	case 'E':
1050009ea47eSEdward Tomasz Napierala 	case 'f':
1051009ea47eSEdward Tomasz Napierala 	case 'F':
1052009ea47eSEdward Tomasz Napierala 		return (true);
1053009ea47eSEdward Tomasz Napierala 	default:
1054009ea47eSEdward Tomasz Napierala 		return (false);
1055009ea47eSEdward Tomasz Napierala 	}
1056009ea47eSEdward Tomasz Napierala }
1057009ea47eSEdward Tomasz Napierala 
1058009ea47eSEdward Tomasz Napierala bool
1059009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
1060009ea47eSEdward Tomasz Napierala {
1061009ea47eSEdward Tomasz Napierala 	int i;
1062009ea47eSEdward Tomasz Napierala 
1063009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
1064009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
1065009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
1066009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
1067009ea47eSEdward Tomasz Napierala 		return (false);
1068009ea47eSEdward Tomasz Napierala 	}
1069009ea47eSEdward Tomasz Napierala 
1070009ea47eSEdward Tomasz Napierala 	/*
1071009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
1072009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
1073009ea47eSEdward Tomasz Napierala 	 */
1074009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1075009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1076009ea47eSEdward Tomasz Napierala 			/*
1077009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
1078009ea47eSEdward Tomasz Napierala 			 *      by 3.2.6.2: iSCSI Name Encoding.
1079009ea47eSEdward Tomasz Napierala 			 */
1080009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
1081009ea47eSEdward Tomasz Napierala 				continue;
1082009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1083009ea47eSEdward Tomasz Napierala 				continue;
1084009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
1085009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
1086009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
1087009ea47eSEdward Tomasz Napierala 			break;
1088009ea47eSEdward Tomasz Napierala 		}
1089009ea47eSEdward Tomasz Napierala 		/*
1090009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
1091009ea47eSEdward Tomasz Napierala 		 */
1092009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1093009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
1094009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1095009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
1096009ea47eSEdward Tomasz Napierala 			    "digits", name);
1097009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
1098009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1099009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1100009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1101009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1102009ea47eSEdward Tomasz Napierala 				break;
1103009ea47eSEdward Tomasz Napierala 			}
1104009ea47eSEdward Tomasz Napierala 		}
1105009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1106009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
1107009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1108009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
1109009ea47eSEdward Tomasz Napierala 			    "digits", name);
1110009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
1111009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
1112009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
1113009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
1114009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
1115009ea47eSEdward Tomasz Napierala 				break;
1116009ea47eSEdward Tomasz Napierala 			}
1117009ea47eSEdward Tomasz Napierala 		}
1118009ea47eSEdward Tomasz Napierala 	} else {
1119009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
1120009ea47eSEdward Tomasz Napierala 		    "either \".iqn\", \"eui.\", or \"naa.\"",
1121009ea47eSEdward Tomasz Napierala 		    name);
1122009ea47eSEdward Tomasz Napierala 	}
1123009ea47eSEdward Tomasz Napierala 	return (true);
1124009ea47eSEdward Tomasz Napierala }
1125009ea47eSEdward Tomasz Napierala 
1126009ea47eSEdward Tomasz Napierala struct target *
1127f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
1128009ea47eSEdward Tomasz Napierala {
1129009ea47eSEdward Tomasz Napierala 	struct target *targ;
1130009ea47eSEdward Tomasz Napierala 	int i, len;
1131009ea47eSEdward Tomasz Napierala 
1132f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
1133009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
1134f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
1135009ea47eSEdward Tomasz Napierala 		return (NULL);
1136009ea47eSEdward Tomasz Napierala 	}
1137f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
1138f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
1139009ea47eSEdward Tomasz Napierala 		return (NULL);
1140009ea47eSEdward Tomasz Napierala 	}
1141009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
1142009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
1143009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1144f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
1145009ea47eSEdward Tomasz Napierala 
1146009ea47eSEdward Tomasz Napierala 	/*
1147009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
1148009ea47eSEdward Tomasz Napierala 	 */
1149f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
1150009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
1151f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
1152009ea47eSEdward Tomasz Napierala 
1153009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
1154009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1155009ea47eSEdward Tomasz Napierala 
1156009ea47eSEdward Tomasz Napierala 	return (targ);
1157009ea47eSEdward Tomasz Napierala }
1158009ea47eSEdward Tomasz Napierala 
1159009ea47eSEdward Tomasz Napierala void
1160009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
1161009ea47eSEdward Tomasz Napierala {
1162009ea47eSEdward Tomasz Napierala 
1163009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1164009ea47eSEdward Tomasz Napierala 
1165f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
11666dbdbf82SEdward Tomasz Napierala 	free(targ->t_redirection);
1167009ea47eSEdward Tomasz Napierala 	free(targ);
1168009ea47eSEdward Tomasz Napierala }
1169009ea47eSEdward Tomasz Napierala 
1170009ea47eSEdward Tomasz Napierala struct target *
1171f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
1172009ea47eSEdward Tomasz Napierala {
1173009ea47eSEdward Tomasz Napierala 	struct target *targ;
1174009ea47eSEdward Tomasz Napierala 
1175009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1176f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
1177009ea47eSEdward Tomasz Napierala 			return (targ);
1178009ea47eSEdward Tomasz Napierala 	}
1179009ea47eSEdward Tomasz Napierala 
1180009ea47eSEdward Tomasz Napierala 	return (NULL);
1181009ea47eSEdward Tomasz Napierala }
1182009ea47eSEdward Tomasz Napierala 
11836dbdbf82SEdward Tomasz Napierala int
11846dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr)
11856dbdbf82SEdward Tomasz Napierala {
11866dbdbf82SEdward Tomasz Napierala 
11876dbdbf82SEdward Tomasz Napierala 	if (target->t_redirection != NULL) {
11886dbdbf82SEdward Tomasz Napierala 		log_warnx("cannot set redirection to \"%s\" for "
11896dbdbf82SEdward Tomasz Napierala 		    "target \"%s\"; already defined",
11906dbdbf82SEdward Tomasz Napierala 		    addr, target->t_name);
11916dbdbf82SEdward Tomasz Napierala 		return (1);
11926dbdbf82SEdward Tomasz Napierala 	}
11936dbdbf82SEdward Tomasz Napierala 
11946dbdbf82SEdward Tomasz Napierala 	target->t_redirection = checked_strdup(addr);
11956dbdbf82SEdward Tomasz Napierala 
11966dbdbf82SEdward Tomasz Napierala 	return (0);
11976dbdbf82SEdward Tomasz Napierala }
11986dbdbf82SEdward Tomasz Napierala 
1199920c6cbaSAlexander Motin void
1200920c6cbaSAlexander Motin target_set_ctl_port(struct target *target, uint32_t value)
1201920c6cbaSAlexander Motin {
1202920c6cbaSAlexander Motin 
1203920c6cbaSAlexander Motin 	target->t_ctl_port = value;
1204920c6cbaSAlexander Motin }
1205920c6cbaSAlexander Motin 
1206009ea47eSEdward Tomasz Napierala struct lun *
1207920c6cbaSAlexander Motin lun_new(struct conf *conf, const char *name)
1208009ea47eSEdward Tomasz Napierala {
1209009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1210009ea47eSEdward Tomasz Napierala 
1211920c6cbaSAlexander Motin 	lun = lun_find(conf, name);
1212009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
1213920c6cbaSAlexander Motin 		log_warnx("duplicated lun \"%s\"", name);
1214009ea47eSEdward Tomasz Napierala 		return (NULL);
1215009ea47eSEdward Tomasz Napierala 	}
1216009ea47eSEdward Tomasz Napierala 
1217009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
1218009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
1219009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1220920c6cbaSAlexander Motin 	lun->l_conf = conf;
1221920c6cbaSAlexander Motin 	lun->l_name = checked_strdup(name);
1222009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
1223920c6cbaSAlexander Motin 	TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1224009ea47eSEdward Tomasz Napierala 
1225009ea47eSEdward Tomasz Napierala 	return (lun);
1226009ea47eSEdward Tomasz Napierala }
1227009ea47eSEdward Tomasz Napierala 
1228009ea47eSEdward Tomasz Napierala void
1229009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
1230009ea47eSEdward Tomasz Napierala {
1231920c6cbaSAlexander Motin 	struct target *targ;
1232009ea47eSEdward Tomasz Napierala 	struct lun_option *lo, *tmp;
1233920c6cbaSAlexander Motin 	int i;
1234009ea47eSEdward Tomasz Napierala 
1235920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1236920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1237920c6cbaSAlexander Motin 			if (targ->t_luns[i] == lun)
1238920c6cbaSAlexander Motin 				targ->t_luns[i] = NULL;
1239920c6cbaSAlexander Motin 		}
1240920c6cbaSAlexander Motin 	}
1241920c6cbaSAlexander Motin 	TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1242009ea47eSEdward Tomasz Napierala 
1243009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
1244009ea47eSEdward Tomasz Napierala 		lun_option_delete(lo);
1245920c6cbaSAlexander Motin 	free(lun->l_name);
1246009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1247009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1248009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1249920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1250009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1251009ea47eSEdward Tomasz Napierala 	free(lun);
1252009ea47eSEdward Tomasz Napierala }
1253009ea47eSEdward Tomasz Napierala 
1254009ea47eSEdward Tomasz Napierala struct lun *
1255920c6cbaSAlexander Motin lun_find(const struct conf *conf, const char *name)
1256009ea47eSEdward Tomasz Napierala {
1257009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1258009ea47eSEdward Tomasz Napierala 
1259920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1260920c6cbaSAlexander Motin 		if (strcmp(lun->l_name, name) == 0)
1261009ea47eSEdward Tomasz Napierala 			return (lun);
1262009ea47eSEdward Tomasz Napierala 	}
1263009ea47eSEdward Tomasz Napierala 
1264009ea47eSEdward Tomasz Napierala 	return (NULL);
1265009ea47eSEdward Tomasz Napierala }
1266009ea47eSEdward Tomasz Napierala 
1267009ea47eSEdward Tomasz Napierala void
1268009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
1269009ea47eSEdward Tomasz Napierala {
1270009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
1271009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
1272009ea47eSEdward Tomasz Napierala }
1273009ea47eSEdward Tomasz Napierala 
1274009ea47eSEdward Tomasz Napierala void
1275009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
1276009ea47eSEdward Tomasz Napierala {
1277009ea47eSEdward Tomasz Napierala 
1278009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
1279009ea47eSEdward Tomasz Napierala }
1280009ea47eSEdward Tomasz Napierala 
1281009ea47eSEdward Tomasz Napierala void
1282009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
1283009ea47eSEdward Tomasz Napierala {
1284009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
1285009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
1286009ea47eSEdward Tomasz Napierala }
1287009ea47eSEdward Tomasz Napierala 
1288009ea47eSEdward Tomasz Napierala void
1289009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
1290009ea47eSEdward Tomasz Napierala {
1291009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
1292009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
1293009ea47eSEdward Tomasz Napierala }
1294009ea47eSEdward Tomasz Napierala 
1295009ea47eSEdward Tomasz Napierala void
1296920c6cbaSAlexander Motin lun_set_scsiname(struct lun *lun, const char *value)
1297920c6cbaSAlexander Motin {
1298920c6cbaSAlexander Motin 	free(lun->l_scsiname);
1299920c6cbaSAlexander Motin 	lun->l_scsiname = checked_strdup(value);
1300920c6cbaSAlexander Motin }
1301920c6cbaSAlexander Motin 
1302920c6cbaSAlexander Motin void
1303009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
1304009ea47eSEdward Tomasz Napierala {
1305009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
1306009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
1307009ea47eSEdward Tomasz Napierala }
1308009ea47eSEdward Tomasz Napierala 
1309009ea47eSEdward Tomasz Napierala void
1310009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
1311009ea47eSEdward Tomasz Napierala {
1312009ea47eSEdward Tomasz Napierala 
1313009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
1314009ea47eSEdward Tomasz Napierala }
1315009ea47eSEdward Tomasz Napierala 
1316009ea47eSEdward Tomasz Napierala void
1317009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
1318009ea47eSEdward Tomasz Napierala {
1319009ea47eSEdward Tomasz Napierala 
1320009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
1321009ea47eSEdward Tomasz Napierala }
1322009ea47eSEdward Tomasz Napierala 
1323009ea47eSEdward Tomasz Napierala struct lun_option *
1324009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value)
1325009ea47eSEdward Tomasz Napierala {
1326009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1327009ea47eSEdward Tomasz Napierala 
1328009ea47eSEdward Tomasz Napierala 	lo = lun_option_find(lun, name);
1329009ea47eSEdward Tomasz Napierala 	if (lo != NULL) {
1330920c6cbaSAlexander Motin 		log_warnx("duplicated lun option \"%s\" for lun \"%s\"",
1331920c6cbaSAlexander Motin 		    name, lun->l_name);
1332009ea47eSEdward Tomasz Napierala 		return (NULL);
1333009ea47eSEdward Tomasz Napierala 	}
1334009ea47eSEdward Tomasz Napierala 
1335009ea47eSEdward Tomasz Napierala 	lo = calloc(1, sizeof(*lo));
1336009ea47eSEdward Tomasz Napierala 	if (lo == NULL)
1337009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1338009ea47eSEdward Tomasz Napierala 	lo->lo_name = checked_strdup(name);
1339009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
1340009ea47eSEdward Tomasz Napierala 	lo->lo_lun = lun;
1341009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
1342009ea47eSEdward Tomasz Napierala 
1343009ea47eSEdward Tomasz Napierala 	return (lo);
1344009ea47eSEdward Tomasz Napierala }
1345009ea47eSEdward Tomasz Napierala 
1346009ea47eSEdward Tomasz Napierala void
1347009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo)
1348009ea47eSEdward Tomasz Napierala {
1349009ea47eSEdward Tomasz Napierala 
1350009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
1351009ea47eSEdward Tomasz Napierala 
1352009ea47eSEdward Tomasz Napierala 	free(lo->lo_name);
1353009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
1354009ea47eSEdward Tomasz Napierala 	free(lo);
1355009ea47eSEdward Tomasz Napierala }
1356009ea47eSEdward Tomasz Napierala 
1357009ea47eSEdward Tomasz Napierala struct lun_option *
1358d6093026SEdward Tomasz Napierala lun_option_find(const struct lun *lun, const char *name)
1359009ea47eSEdward Tomasz Napierala {
1360009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1361009ea47eSEdward Tomasz Napierala 
1362009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
1363009ea47eSEdward Tomasz Napierala 		if (strcmp(lo->lo_name, name) == 0)
1364009ea47eSEdward Tomasz Napierala 			return (lo);
1365009ea47eSEdward Tomasz Napierala 	}
1366009ea47eSEdward Tomasz Napierala 
1367009ea47eSEdward Tomasz Napierala 	return (NULL);
1368009ea47eSEdward Tomasz Napierala }
1369009ea47eSEdward Tomasz Napierala 
1370009ea47eSEdward Tomasz Napierala void
1371009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value)
1372009ea47eSEdward Tomasz Napierala {
1373009ea47eSEdward Tomasz Napierala 
1374009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
1375009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
1376009ea47eSEdward Tomasz Napierala }
1377009ea47eSEdward Tomasz Napierala 
1378009ea47eSEdward Tomasz Napierala static struct connection *
1379073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host,
1380073edb1cSAlexander Motin     const struct sockaddr *client_sa)
1381009ea47eSEdward Tomasz Napierala {
1382009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1383009ea47eSEdward Tomasz Napierala 
1384009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
1385009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
1386009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
1387009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
1388009ea47eSEdward Tomasz Napierala 	conn->conn_socket = fd;
1389009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
1390073edb1cSAlexander Motin 	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1391009ea47eSEdward Tomasz Napierala 
1392009ea47eSEdward Tomasz Napierala 	/*
1393009ea47eSEdward Tomasz Napierala 	 * Default values, from RFC 3720, section 12.
1394009ea47eSEdward Tomasz Napierala 	 */
1395009ea47eSEdward Tomasz Napierala 	conn->conn_max_data_segment_length = 8192;
1396009ea47eSEdward Tomasz Napierala 	conn->conn_max_burst_length = 262144;
1397009ea47eSEdward Tomasz Napierala 	conn->conn_immediate_data = true;
1398009ea47eSEdward Tomasz Napierala 
1399009ea47eSEdward Tomasz Napierala 	return (conn);
1400009ea47eSEdward Tomasz Napierala }
1401009ea47eSEdward Tomasz Napierala 
1402009ea47eSEdward Tomasz Napierala #if 0
1403009ea47eSEdward Tomasz Napierala static void
1404009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
1405009ea47eSEdward Tomasz Napierala {
1406009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1407009ea47eSEdward Tomasz Napierala 	struct auth *auth;
14088cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
14098cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
1410009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1411009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1412009ea47eSEdward Tomasz Napierala 	struct target *targ;
1413009ea47eSEdward Tomasz Napierala 	struct lun *lun;
1414009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
1415009ea47eSEdward Tomasz Napierala 
1416009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1417009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1418009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1419009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1420009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
1421009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
14228cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
14238cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
14248cb2e958SEdward Tomasz Napierala 			    auth_name->an_initator_name);
14258cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
14268cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
14278cb2e958SEdward Tomasz Napierala 			    auth_portal->an_initator_portal);
1428009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1429009ea47eSEdward Tomasz Napierala 	}
1430009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1431009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1432009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1433009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1434009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1435009ea47eSEdward Tomasz Napierala 	}
1436920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1437920c6cbaSAlexander Motin 		fprintf(stderr, "\tlun %s {\n", lun->l_name);
1438009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1439009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1440009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\toption %s %s\n",
1441009ea47eSEdward Tomasz Napierala 			    lo->lo_name, lo->lo_value);
1442009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "\t}\n");
1443009ea47eSEdward Tomasz Napierala 	}
1444920c6cbaSAlexander Motin 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1445920c6cbaSAlexander Motin 		fprintf(stderr, "target %s {\n", targ->t_name);
1446920c6cbaSAlexander Motin 		if (targ->t_alias != NULL)
1447920c6cbaSAlexander Motin 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1448009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1449009ea47eSEdward Tomasz Napierala 	}
1450009ea47eSEdward Tomasz Napierala }
1451009ea47eSEdward Tomasz Napierala #endif
1452009ea47eSEdward Tomasz Napierala 
14532fabfaa5SEdward Tomasz Napierala static int
14542fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
14552fabfaa5SEdward Tomasz Napierala {
14562fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
14572fabfaa5SEdward Tomasz Napierala 
14582fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
14592fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
14602fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
14612fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
1462920c6cbaSAlexander Motin 			log_warnx("missing path for lun \"%s\"",
1463920c6cbaSAlexander Motin 			    lun->l_name);
14642fabfaa5SEdward Tomasz Napierala 			return (1);
14652fabfaa5SEdward Tomasz Napierala 		}
14662fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
14672fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
1468920c6cbaSAlexander Motin 			log_warnx("missing size for ramdisk-backed lun \"%s\"",
1469920c6cbaSAlexander Motin 			    lun->l_name);
14702fabfaa5SEdward Tomasz Napierala 			return (1);
14712fabfaa5SEdward Tomasz Napierala 		}
14722fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
14732fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
1474920c6cbaSAlexander Motin 			    "for ramdisk-backed lun \"%s\"",
1475920c6cbaSAlexander Motin 			    lun->l_name);
14762fabfaa5SEdward Tomasz Napierala 			return (1);
14772fabfaa5SEdward Tomasz Napierala 		}
14782fabfaa5SEdward Tomasz Napierala 	}
14792fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
14802fabfaa5SEdward Tomasz Napierala 		lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
14812fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
1482920c6cbaSAlexander Motin 		log_warnx("invalid blocksize for lun \"%s\"; "
1483920c6cbaSAlexander Motin 		    "must be larger than 0", lun->l_name);
14842fabfaa5SEdward Tomasz Napierala 		return (1);
14852fabfaa5SEdward Tomasz Napierala 	}
14862fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1487920c6cbaSAlexander Motin 		log_warnx("invalid size for lun \"%s\"; "
1488920c6cbaSAlexander Motin 		    "must be multiple of blocksize", lun->l_name);
14892fabfaa5SEdward Tomasz Napierala 		return (1);
14902fabfaa5SEdward Tomasz Napierala 	}
1491920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1492a19eebb1SEdward Tomasz Napierala 		if (lun == lun2)
1493a19eebb1SEdward Tomasz Napierala 			continue;
1494a19eebb1SEdward Tomasz Napierala 		if (lun->l_path != NULL && lun2->l_path != NULL &&
1495a19eebb1SEdward Tomasz Napierala 		    strcmp(lun->l_path, lun2->l_path) == 0) {
1496a19eebb1SEdward Tomasz Napierala 			log_debugx("WARNING: path \"%s\" duplicated "
1497920c6cbaSAlexander Motin 			    "between lun \"%s\", and "
1498920c6cbaSAlexander Motin 			    "lun \"%s\"", lun->l_path,
1499920c6cbaSAlexander Motin 			    lun->l_name, lun2->l_name);
1500a19eebb1SEdward Tomasz Napierala 		}
1501a19eebb1SEdward Tomasz Napierala 	}
15022fabfaa5SEdward Tomasz Napierala 
15032fabfaa5SEdward Tomasz Napierala 	return (0);
15042fabfaa5SEdward Tomasz Napierala }
15052fabfaa5SEdward Tomasz Napierala 
1506009ea47eSEdward Tomasz Napierala int
1507009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1508009ea47eSEdward Tomasz Napierala {
1509009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1510009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1511009ea47eSEdward Tomasz Napierala 	struct target *targ;
15122fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
15134e5c38f8SEdward Tomasz Napierala 	bool found;
1514920c6cbaSAlexander Motin 	int error, i;
1515009ea47eSEdward Tomasz Napierala 
1516009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1517009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1518009ea47eSEdward Tomasz Napierala 
1519920c6cbaSAlexander Motin 	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1520920c6cbaSAlexander Motin 		error = conf_verify_lun(lun);
1521920c6cbaSAlexander Motin 		if (error != 0)
1522920c6cbaSAlexander Motin 			return (error);
1523920c6cbaSAlexander Motin 	}
1524009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1525009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1526e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1527e7977549SEdward Tomasz Napierala 			    "default");
1528e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1529009ea47eSEdward Tomasz Napierala 		}
1530009ea47eSEdward Tomasz Napierala 		if (targ->t_portal_group == NULL) {
1531009ea47eSEdward Tomasz Napierala 			targ->t_portal_group = portal_group_find(conf,
1532009ea47eSEdward Tomasz Napierala 			    "default");
1533009ea47eSEdward Tomasz Napierala 			assert(targ->t_portal_group != NULL);
1534009ea47eSEdward Tomasz Napierala 		}
15354e5c38f8SEdward Tomasz Napierala 		found = false;
1536920c6cbaSAlexander Motin 		for (i = 0; i < MAX_LUNS; i++) {
1537920c6cbaSAlexander Motin 			if (targ->t_luns[i] != NULL)
15384e5c38f8SEdward Tomasz Napierala 				found = true;
1539009ea47eSEdward Tomasz Napierala 		}
15406dbdbf82SEdward Tomasz Napierala 		if (!found && targ->t_redirection == NULL) {
154119b59f13SEdward Tomasz Napierala 			log_warnx("no LUNs defined for target \"%s\"",
154219b59f13SEdward Tomasz Napierala 			    targ->t_name);
1543009ea47eSEdward Tomasz Napierala 		}
15446dbdbf82SEdward Tomasz Napierala 		if (found && targ->t_redirection != NULL) {
15456dbdbf82SEdward Tomasz Napierala 			log_debugx("target \"%s\" contains luns, "
15466dbdbf82SEdward Tomasz Napierala 			    " but configured for redirection",
15476dbdbf82SEdward Tomasz Napierala 			    targ->t_name);
15486dbdbf82SEdward Tomasz Napierala 		}
1549009ea47eSEdward Tomasz Napierala 	}
1550009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1551009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1552009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1553009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1554ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1555009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1556009ea47eSEdward Tomasz Napierala 		}
1557009ea47eSEdward Tomasz Napierala 
155805374883SEdward Tomasz Napierala 		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
155905374883SEdward Tomasz Napierala 			pg->pg_discovery_filter = PG_FILTER_NONE;
156005374883SEdward Tomasz Napierala 
1561009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1562009ea47eSEdward Tomasz Napierala 			if (targ->t_portal_group == pg)
1563009ea47eSEdward Tomasz Napierala 				break;
1564009ea47eSEdward Tomasz Napierala 		}
15656dbdbf82SEdward Tomasz Napierala 		if (pg->pg_redirection != NULL) {
15666dbdbf82SEdward Tomasz Napierala 			if (targ != NULL) {
15676dbdbf82SEdward Tomasz Napierala 				log_debugx("portal-group \"%s\" assigned "
15686dbdbf82SEdward Tomasz Napierala 				    "to target \"%s\", but configured "
15696dbdbf82SEdward Tomasz Napierala 				    "for redirection",
15706dbdbf82SEdward Tomasz Napierala 				    pg->pg_name, targ->t_name);
15716dbdbf82SEdward Tomasz Napierala 			}
15726dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
15736dbdbf82SEdward Tomasz Napierala 		} else if (targ != NULL) {
15746dbdbf82SEdward Tomasz Napierala 			pg->pg_unassigned = false;
15756dbdbf82SEdward Tomasz Napierala 		} else {
1576009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1577009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1578009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1579009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
15806dbdbf82SEdward Tomasz Napierala 		}
1581009ea47eSEdward Tomasz Napierala 	}
1582009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1583009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1584009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1585009ea47eSEdward Tomasz Napierala 		else
1586009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1587009ea47eSEdward Tomasz Napierala 
15884e5c38f8SEdward Tomasz Napierala 		found = false;
1589009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
15904e5c38f8SEdward Tomasz Napierala 			if (targ->t_auth_group == ag) {
15914e5c38f8SEdward Tomasz Napierala 				found = true;
1592009ea47eSEdward Tomasz Napierala 				break;
1593009ea47eSEdward Tomasz Napierala 			}
15944e5c38f8SEdward Tomasz Napierala 		}
15954e5c38f8SEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
15964e5c38f8SEdward Tomasz Napierala 			if (pg->pg_discovery_auth_group == ag) {
15974e5c38f8SEdward Tomasz Napierala 				found = true;
15984e5c38f8SEdward Tomasz Napierala 				break;
15994e5c38f8SEdward Tomasz Napierala 			}
16004e5c38f8SEdward Tomasz Napierala 		}
16014e5c38f8SEdward Tomasz Napierala 		if (!found && ag->ag_name != NULL &&
1602ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1603009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1604009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1605009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1606009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1607009ea47eSEdward Tomasz Napierala 		}
1608009ea47eSEdward Tomasz Napierala 	}
1609009ea47eSEdward Tomasz Napierala 
1610009ea47eSEdward Tomasz Napierala 	return (0);
1611009ea47eSEdward Tomasz Napierala }
1612009ea47eSEdward Tomasz Napierala 
1613009ea47eSEdward Tomasz Napierala static int
1614009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1615009ea47eSEdward Tomasz Napierala {
1616009ea47eSEdward Tomasz Napierala 	struct target *oldtarg, *newtarg, *tmptarg;
1617009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1618009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1619009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
1620829603e2SAlexander Motin 	struct isns *oldns, *newns;
1621009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
16228b94b583SAlexander Motin 	int changed, cumulated_error = 0, error, sockbuf;
1623009ea47eSEdward Tomasz Napierala 	int one = 1;
1624009ea47eSEdward Tomasz Napierala 
1625009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1626009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1627009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1628009ea47eSEdward Tomasz Napierala 	}
1629009ea47eSEdward Tomasz Napierala 
1630009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1631009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1632009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1633009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1634009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1635009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1636009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1637009ea47eSEdward Tomasz Napierala 		} else {
1638009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1639009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1640009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1641009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1642009ea47eSEdward Tomasz Napierala 		}
1643009ea47eSEdward Tomasz Napierala 	}
1644009ea47eSEdward Tomasz Napierala 
1645009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1646009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1647009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1648009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1649009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1650009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1651009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1652009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1653009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1654009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1655009ea47eSEdward Tomasz Napierala 		}
1656009ea47eSEdward Tomasz Napierala 	}
1657009ea47eSEdward Tomasz Napierala 
1658*6b31e130SAlexander Motin 	/*
1659*6b31e130SAlexander Motin 	 * Go through the new portal groups, assigning tags or preserving old.
1660*6b31e130SAlexander Motin 	 */
1661*6b31e130SAlexander Motin 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1662*6b31e130SAlexander Motin 		oldpg = portal_group_find(oldconf, newpg->pg_name);
1663*6b31e130SAlexander Motin 		if (oldpg != NULL)
1664*6b31e130SAlexander Motin 			newpg->pg_tag = oldpg->pg_tag;
1665*6b31e130SAlexander Motin 		else
1666*6b31e130SAlexander Motin 			newpg->pg_tag = ++last_portal_group_tag;
1667*6b31e130SAlexander Motin 	}
1668*6b31e130SAlexander Motin 
1669829603e2SAlexander Motin 	/* Deregister on removed iSNS servers. */
1670829603e2SAlexander Motin 	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1671829603e2SAlexander Motin 		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1672829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1673829603e2SAlexander Motin 				break;
1674829603e2SAlexander Motin 		}
1675829603e2SAlexander Motin 		if (newns == NULL)
1676829603e2SAlexander Motin 			isns_deregister(oldns);
1677829603e2SAlexander Motin 	}
1678829603e2SAlexander Motin 
1679d5e316e5SEdward Tomasz Napierala 	/*
1680d5e316e5SEdward Tomasz Napierala 	 * XXX: If target or lun removal fails, we should somehow "move"
1681d5e316e5SEdward Tomasz Napierala 	 *      the old lun or target into newconf, so that subsequent
168261a2a354SEdward Tomasz Napierala 	 *      conf_apply() would try to remove them again.  That would
168361a2a354SEdward Tomasz Napierala 	 *      be somewhat hairy, though, and lun deletion failures don't
168461a2a354SEdward Tomasz Napierala 	 *      really happen, so leave it as it is for now.
1685d5e316e5SEdward Tomasz Napierala 	 */
1686009ea47eSEdward Tomasz Napierala 	/*
1687009ea47eSEdward Tomasz Napierala 	 * First, remove any targets present in the old configuration
1688009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
1689009ea47eSEdward Tomasz Napierala 	 */
1690920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1691f7ae5bf8SEdward Tomasz Napierala 		newtarg = target_find(newconf, oldtarg->t_name);
1692920c6cbaSAlexander Motin 		if (newtarg != NULL)
1693920c6cbaSAlexander Motin 			continue;
16946de78f9fSAlexander Motin 		error = kernel_port_remove(oldtarg);
16956de78f9fSAlexander Motin 		if (error != 0) {
16966de78f9fSAlexander Motin 			log_warnx("failed to remove target %s",
16976de78f9fSAlexander Motin 			    oldtarg->t_name);
16986de78f9fSAlexander Motin 			/*
16996de78f9fSAlexander Motin 			 * XXX: Uncomment after fixing the root cause.
17006de78f9fSAlexander Motin 			 *
17016de78f9fSAlexander Motin 			 * cumulated_error++;
17026de78f9fSAlexander Motin 			 */
17036de78f9fSAlexander Motin 		}
1704009ea47eSEdward Tomasz Napierala 	}
1705009ea47eSEdward Tomasz Napierala 
1706009ea47eSEdward Tomasz Napierala 	/*
1707920c6cbaSAlexander Motin 	 * Second, remove any LUNs present in the old configuration
1708009ea47eSEdward Tomasz Napierala 	 * and missing in the new one.
1709009ea47eSEdward Tomasz Napierala 	 */
1710920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1711920c6cbaSAlexander Motin 		newlun = lun_find(newconf, oldlun->l_name);
1712009ea47eSEdward Tomasz Napierala 		if (newlun == NULL) {
1713920c6cbaSAlexander Motin 			log_debugx("lun \"%s\", CTL lun %d "
1714cadf3831SEdward Tomasz Napierala 			    "not found in new configuration; "
1715920c6cbaSAlexander Motin 			    "removing", oldlun->l_name, oldlun->l_ctl_lun);
1716009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
1717009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1718920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
1719920c6cbaSAlexander Motin 				    "CTL lun %d",
1720920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
1721009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1722009ea47eSEdward Tomasz Napierala 			}
1723009ea47eSEdward Tomasz Napierala 			continue;
1724009ea47eSEdward Tomasz Napierala 		}
1725009ea47eSEdward Tomasz Napierala 
1726009ea47eSEdward Tomasz Napierala 		/*
1727009ea47eSEdward Tomasz Napierala 		 * Also remove the LUNs changed by more than size.
1728009ea47eSEdward Tomasz Napierala 		 */
1729009ea47eSEdward Tomasz Napierala 		changed = 0;
1730009ea47eSEdward Tomasz Napierala 		assert(oldlun->l_backend != NULL);
1731009ea47eSEdward Tomasz Napierala 		assert(newlun->l_backend != NULL);
1732009ea47eSEdward Tomasz Napierala 		if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1733920c6cbaSAlexander Motin 			log_debugx("backend for lun \"%s\", "
1734009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1735920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1736009ea47eSEdward Tomasz Napierala 			changed = 1;
1737009ea47eSEdward Tomasz Napierala 		}
1738009ea47eSEdward Tomasz Napierala 		if (oldlun->l_blocksize != newlun->l_blocksize) {
1739920c6cbaSAlexander Motin 			log_debugx("blocksize for lun \"%s\", "
1740009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1741920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1742009ea47eSEdward Tomasz Napierala 			changed = 1;
1743009ea47eSEdward Tomasz Napierala 		}
1744009ea47eSEdward Tomasz Napierala 		if (newlun->l_device_id != NULL &&
1745009ea47eSEdward Tomasz Napierala 		    (oldlun->l_device_id == NULL ||
1746009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1747009ea47eSEdward Tomasz Napierala 		     0)) {
1748920c6cbaSAlexander Motin 			log_debugx("device-id for lun \"%s\", "
1749009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1750920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1751009ea47eSEdward Tomasz Napierala 			changed = 1;
1752009ea47eSEdward Tomasz Napierala 		}
1753009ea47eSEdward Tomasz Napierala 		if (newlun->l_path != NULL &&
1754009ea47eSEdward Tomasz Napierala 		    (oldlun->l_path == NULL ||
1755009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1756920c6cbaSAlexander Motin 			log_debugx("path for lun \"%s\", "
1757009ea47eSEdward Tomasz Napierala 			    "CTL lun %d, changed; removing",
1758920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1759009ea47eSEdward Tomasz Napierala 			changed = 1;
1760009ea47eSEdward Tomasz Napierala 		}
1761009ea47eSEdward Tomasz Napierala 		if (newlun->l_serial != NULL &&
1762009ea47eSEdward Tomasz Napierala 		    (oldlun->l_serial == NULL ||
1763009ea47eSEdward Tomasz Napierala 		     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1764920c6cbaSAlexander Motin 			log_debugx("serial for lun \"%s\", "
1765009ea47eSEdward Tomasz Napierala 			    "CTL lun %d changed; removing",
1766920c6cbaSAlexander Motin 			    oldlun->l_name, oldlun->l_ctl_lun);
1767009ea47eSEdward Tomasz Napierala 			changed = 1;
1768009ea47eSEdward Tomasz Napierala 		}
1769009ea47eSEdward Tomasz Napierala 		if (changed) {
1770009ea47eSEdward Tomasz Napierala 			error = kernel_lun_remove(oldlun);
1771009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1772920c6cbaSAlexander Motin 				log_warnx("failed to remove lun \"%s\", "
1773920c6cbaSAlexander Motin 				    "CTL lun %d",
1774920c6cbaSAlexander Motin 				    oldlun->l_name, oldlun->l_ctl_lun);
1775009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1776009ea47eSEdward Tomasz Napierala 			}
1777009ea47eSEdward Tomasz Napierala 			lun_delete(oldlun);
1778009ea47eSEdward Tomasz Napierala 			continue;
1779009ea47eSEdward Tomasz Napierala 		}
1780009ea47eSEdward Tomasz Napierala 
1781009ea47eSEdward Tomasz Napierala 		lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1782009ea47eSEdward Tomasz Napierala 	}
1783920c6cbaSAlexander Motin 
1784920c6cbaSAlexander Motin 	TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1785920c6cbaSAlexander Motin 		oldlun = lun_find(oldconf, newlun->l_name);
1786920c6cbaSAlexander Motin 		if (oldlun != NULL) {
1787920c6cbaSAlexander Motin 			if (newlun->l_size != oldlun->l_size ||
1788920c6cbaSAlexander Motin 			    newlun->l_size == 0) {
1789920c6cbaSAlexander Motin 				log_debugx("resizing lun \"%s\", CTL lun %d",
1790920c6cbaSAlexander Motin 				    newlun->l_name, newlun->l_ctl_lun);
1791920c6cbaSAlexander Motin 				error = kernel_lun_resize(newlun);
1792920c6cbaSAlexander Motin 				if (error != 0) {
1793920c6cbaSAlexander Motin 					log_warnx("failed to "
1794920c6cbaSAlexander Motin 					    "resize lun \"%s\", CTL lun %d",
1795920c6cbaSAlexander Motin 					    newlun->l_name,
1796920c6cbaSAlexander Motin 					    newlun->l_ctl_lun);
1797920c6cbaSAlexander Motin 					cumulated_error++;
1798920c6cbaSAlexander Motin 				}
1799920c6cbaSAlexander Motin 			}
1800920c6cbaSAlexander Motin 			continue;
1801920c6cbaSAlexander Motin 		}
1802920c6cbaSAlexander Motin 		log_debugx("adding lun \"%s\"", newlun->l_name);
1803920c6cbaSAlexander Motin 		error = kernel_lun_add(newlun);
1804920c6cbaSAlexander Motin 		if (error != 0) {
1805920c6cbaSAlexander Motin 			log_warnx("failed to add lun \"%s\"", newlun->l_name);
1806920c6cbaSAlexander Motin 			lun_delete(newlun);
1807920c6cbaSAlexander Motin 			cumulated_error++;
1808920c6cbaSAlexander Motin 		}
1809009ea47eSEdward Tomasz Napierala 	}
1810009ea47eSEdward Tomasz Napierala 
1811009ea47eSEdward Tomasz Napierala 	/*
1812009ea47eSEdward Tomasz Napierala 	 * Now add new targets or modify existing ones.
1813009ea47eSEdward Tomasz Napierala 	 */
1814009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1815f7ae5bf8SEdward Tomasz Napierala 		oldtarg = target_find(oldconf, newtarg->t_name);
1816009ea47eSEdward Tomasz Napierala 
1817920c6cbaSAlexander Motin 		if (oldtarg == NULL)
1818e48bb73aSEdward Tomasz Napierala 			error = kernel_port_add(newtarg);
1819920c6cbaSAlexander Motin 		else {
1820920c6cbaSAlexander Motin 			target_set_ctl_port(newtarg, oldtarg->t_ctl_port);
1821920c6cbaSAlexander Motin 			error = kernel_port_update(newtarg);
1822920c6cbaSAlexander Motin 		}
1823e48bb73aSEdward Tomasz Napierala 		if (error != 0) {
1824920c6cbaSAlexander Motin 			log_warnx("failed to %s target %s",
1825920c6cbaSAlexander Motin 			    (oldtarg == NULL) ? "add" : "update",
18260dd2bee4SEdward Tomasz Napierala 			    newtarg->t_name);
1827e48bb73aSEdward Tomasz Napierala 			/*
1828e48bb73aSEdward Tomasz Napierala 			 * XXX: Uncomment after fixing the root cause.
1829e48bb73aSEdward Tomasz Napierala 			 *
1830e48bb73aSEdward Tomasz Napierala 			 * cumulated_error++;
1831e48bb73aSEdward Tomasz Napierala 			 */
1832e48bb73aSEdward Tomasz Napierala 		}
1833e48bb73aSEdward Tomasz Napierala 	}
1834009ea47eSEdward Tomasz Napierala 
1835009ea47eSEdward Tomasz Napierala 	/*
1836009ea47eSEdward Tomasz Napierala 	 * Go through the new portals, opening the sockets as neccessary.
1837009ea47eSEdward Tomasz Napierala 	 */
1838009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1839009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
1840009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
1841009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
1842009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
1843009ea47eSEdward Tomasz Napierala 			continue;
1844009ea47eSEdward Tomasz Napierala 		}
1845009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1846009ea47eSEdward Tomasz Napierala 			/*
1847009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
1848009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
1849009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
1850009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
1851009ea47eSEdward Tomasz Napierala 			 */
1852009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1853009ea47eSEdward Tomasz Napierala 			    pg_next) {
1854009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1855009ea47eSEdward Tomasz Napierala 				    p_next) {
1856009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
1857009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
1858009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
1859009ea47eSEdward Tomasz Napierala 						newp->p_socket =
1860009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
1861009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
1862009ea47eSEdward Tomasz Napierala 						break;
1863009ea47eSEdward Tomasz Napierala 					}
1864009ea47eSEdward Tomasz Napierala 				}
1865009ea47eSEdward Tomasz Napierala 			}
1866009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
1867009ea47eSEdward Tomasz Napierala 				/*
1868009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
1869009ea47eSEdward Tomasz Napierala 				 */
1870009ea47eSEdward Tomasz Napierala 				continue;
1871009ea47eSEdward Tomasz Napierala 			}
1872009ea47eSEdward Tomasz Napierala 
1873009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1874ba3a2d31SEdward Tomasz Napierala 			if (proxy_mode) {
18758cab2ed4SEdward Tomasz Napierala 				newpg->pg_conf->conf_portal_id++;
18768cab2ed4SEdward Tomasz Napierala 				newp->p_id = newpg->pg_conf->conf_portal_id;
18778cab2ed4SEdward Tomasz Napierala 				log_debugx("listening on %s, portal-group "
18788cab2ed4SEdward Tomasz Napierala 				    "\"%s\", portal id %d, using ICL proxy",
18798cab2ed4SEdward Tomasz Napierala 				    newp->p_listen, newpg->pg_name, newp->p_id);
18808cab2ed4SEdward Tomasz Napierala 				kernel_listen(newp->p_ai, newp->p_iser,
18818cab2ed4SEdward Tomasz Napierala 				    newp->p_id);
1882ba3a2d31SEdward Tomasz Napierala 				continue;
1883ba3a2d31SEdward Tomasz Napierala 			}
1884ba3a2d31SEdward Tomasz Napierala #endif
1885ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
1886009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
1887009ea47eSEdward Tomasz Napierala 
1888009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
1889009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
1890009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
1891009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
1892009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
1893009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
1894009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
1895009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1896009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1897009ea47eSEdward Tomasz Napierala 				continue;
1898009ea47eSEdward Tomasz Napierala 			}
18998b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
19008b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
19018b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
19028b94b583SAlexander Motin 				log_warn("setsockopt(SO_RCVBUF) failed "
19038b94b583SAlexander Motin 				    "for %s", newp->p_listen);
19048b94b583SAlexander Motin 			sockbuf = SOCKBUF_SIZE;
19058b94b583SAlexander Motin 			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
19068b94b583SAlexander Motin 			    &sockbuf, sizeof(sockbuf)) == -1)
19078b94b583SAlexander Motin 				log_warn("setsockopt(SO_SNDBUF) failed "
19088b94b583SAlexander Motin 				    "for %s", newp->p_listen);
1909009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
1910009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
1911009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1912009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
1913009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
1914009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1915009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1916009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1917009ea47eSEdward Tomasz Napierala 				continue;
1918009ea47eSEdward Tomasz Napierala 			}
1919009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
1920009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
1921009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1922009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
1923009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1924009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1925009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1926009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1927009ea47eSEdward Tomasz Napierala 				continue;
1928009ea47eSEdward Tomasz Napierala 			}
1929009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
1930009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1931009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
1932009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1933009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1934009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1935009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1936009ea47eSEdward Tomasz Napierala 				continue;
1937009ea47eSEdward Tomasz Napierala 			}
1938009ea47eSEdward Tomasz Napierala 		}
1939009ea47eSEdward Tomasz Napierala 	}
1940009ea47eSEdward Tomasz Napierala 
1941009ea47eSEdward Tomasz Napierala 	/*
1942009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
1943009ea47eSEdward Tomasz Napierala 	 */
1944009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1945009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1946009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
1947009ea47eSEdward Tomasz Napierala 				continue;
1948009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
1949009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
1950009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
1951009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
1952009ea47eSEdward Tomasz Napierala 		}
1953009ea47eSEdward Tomasz Napierala 	}
1954009ea47eSEdward Tomasz Napierala 
1955829603e2SAlexander Motin 	/* (Re-)Register on remaining/new iSNS servers. */
1956829603e2SAlexander Motin 	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1957829603e2SAlexander Motin 		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1958829603e2SAlexander Motin 			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1959829603e2SAlexander Motin 				break;
1960829603e2SAlexander Motin 		}
1961829603e2SAlexander Motin 		isns_register(newns, oldns);
1962829603e2SAlexander Motin 	}
1963829603e2SAlexander Motin 
1964829603e2SAlexander Motin 	/* Schedule iSNS update */
1965829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
1966829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
1967829603e2SAlexander Motin 
1968009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
1969009ea47eSEdward Tomasz Napierala }
1970009ea47eSEdward Tomasz Napierala 
1971009ea47eSEdward Tomasz Napierala bool
1972009ea47eSEdward Tomasz Napierala timed_out(void)
1973009ea47eSEdward Tomasz Napierala {
1974009ea47eSEdward Tomasz Napierala 
1975009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
1976009ea47eSEdward Tomasz Napierala }
1977009ea47eSEdward Tomasz Napierala 
1978009ea47eSEdward Tomasz Napierala static void
1979829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused)
1980009ea47eSEdward Tomasz Napierala {
1981009ea47eSEdward Tomasz Napierala 	/*
1982009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
1983009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
1984009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
1985009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
1986009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
1987009ea47eSEdward Tomasz Napierala 	 */
1988009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
1989009ea47eSEdward Tomasz Napierala 		/*
1990009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
1991009ea47eSEdward Tomasz Napierala 		 */
1992009ea47eSEdward Tomasz Napierala 		_exit(2);
1993009ea47eSEdward Tomasz Napierala 	}
1994009ea47eSEdward Tomasz Napierala 
1995009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
1996009ea47eSEdward Tomasz Napierala }
1997009ea47eSEdward Tomasz Napierala 
1998009ea47eSEdward Tomasz Napierala static void
1999829603e2SAlexander Motin sigalrm_handler(int dummy __unused)
2000829603e2SAlexander Motin {
2001829603e2SAlexander Motin 
2002829603e2SAlexander Motin 	sigalrm_received = true;
2003829603e2SAlexander Motin }
2004829603e2SAlexander Motin 
2005829603e2SAlexander Motin void
2006829603e2SAlexander Motin set_timeout(int timeout, int fatal)
2007009ea47eSEdward Tomasz Napierala {
2008009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2009009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
2010009ea47eSEdward Tomasz Napierala 	int error;
2011009ea47eSEdward Tomasz Napierala 
2012829603e2SAlexander Motin 	if (timeout <= 0) {
2013009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
2014829603e2SAlexander Motin 		bzero(&itv, sizeof(itv));
2015829603e2SAlexander Motin 		error = setitimer(ITIMER_REAL, &itv, NULL);
2016829603e2SAlexander Motin 		if (error != 0)
2017829603e2SAlexander Motin 			log_err(1, "setitimer");
2018829603e2SAlexander Motin 		sigalrm_received = false;
2019009ea47eSEdward Tomasz Napierala 		return;
2020009ea47eSEdward Tomasz Napierala 	}
2021009ea47eSEdward Tomasz Napierala 
2022829603e2SAlexander Motin 	sigalrm_received = false;
2023009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2024829603e2SAlexander Motin 	if (fatal)
2025829603e2SAlexander Motin 		sa.sa_handler = sigalrm_handler_fatal;
2026829603e2SAlexander Motin 	else
2027009ea47eSEdward Tomasz Napierala 		sa.sa_handler = sigalrm_handler;
2028009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2029009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
2030009ea47eSEdward Tomasz Napierala 	if (error != 0)
2031009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2032009ea47eSEdward Tomasz Napierala 
2033009ea47eSEdward Tomasz Napierala 	/*
2034009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
2035009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
2036009ea47eSEdward Tomasz Napierala 	 */
2037829603e2SAlexander Motin 	log_debugx("setting session timeout to %d seconds", timeout);
2038009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
2039009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
2040829603e2SAlexander Motin 	itv.it_value.tv_sec = timeout;
2041009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
2042009ea47eSEdward Tomasz Napierala 	if (error != 0)
2043009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
2044009ea47eSEdward Tomasz Napierala }
2045009ea47eSEdward Tomasz Napierala 
2046009ea47eSEdward Tomasz Napierala static int
2047009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
2048009ea47eSEdward Tomasz Napierala {
2049009ea47eSEdward Tomasz Napierala 	pid_t pid;
2050009ea47eSEdward Tomasz Napierala 	int status;
2051009ea47eSEdward Tomasz Napierala 	int num = 0;
2052009ea47eSEdward Tomasz Napierala 
2053009ea47eSEdward Tomasz Napierala 	for (;;) {
2054009ea47eSEdward Tomasz Napierala 		/*
2055009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
2056009ea47eSEdward Tomasz Napierala 		 */
2057009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
2058009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
2059009ea47eSEdward Tomasz Napierala 		else
2060009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
2061009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
2062009ea47eSEdward Tomasz Napierala 			break;
2063009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
2064009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
2065009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
2066009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
2067009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
2068009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
2069009ea47eSEdward Tomasz Napierala 		} else {
2070009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
2071009ea47eSEdward Tomasz Napierala 		}
2072009ea47eSEdward Tomasz Napierala 		num++;
2073009ea47eSEdward Tomasz Napierala 	}
2074009ea47eSEdward Tomasz Napierala 
2075009ea47eSEdward Tomasz Napierala 	return (num);
2076009ea47eSEdward Tomasz Napierala }
2077009ea47eSEdward Tomasz Napierala 
2078009ea47eSEdward Tomasz Napierala static void
20798eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd,
2080073edb1cSAlexander Motin     const struct sockaddr *client_sa, bool dont_fork)
2081009ea47eSEdward Tomasz Napierala {
2082009ea47eSEdward Tomasz Napierala 	struct connection *conn;
2083009ea47eSEdward Tomasz Napierala 	int error;
2084009ea47eSEdward Tomasz Napierala 	pid_t pid;
2085009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
2086009ea47eSEdward Tomasz Napierala 	struct conf *conf;
2087009ea47eSEdward Tomasz Napierala 
2088009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
2089009ea47eSEdward Tomasz Napierala 
2090009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
2091009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
2092009ea47eSEdward Tomasz Napierala 	} else {
2093009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
2094009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
2095009ea47eSEdward Tomasz Napierala 
2096009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2097009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
2098009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
2099009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
2100009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2101009ea47eSEdward Tomasz Napierala 		}
2102009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
2103009ea47eSEdward Tomasz Napierala 		    nchildren);
2104009ea47eSEdward Tomasz Napierala 		nchildren++;
2105009ea47eSEdward Tomasz Napierala 		pid = fork();
2106009ea47eSEdward Tomasz Napierala 		if (pid < 0)
2107009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
2108009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
2109009ea47eSEdward Tomasz Napierala 			close(fd);
2110009ea47eSEdward Tomasz Napierala 			return;
2111009ea47eSEdward Tomasz Napierala 		}
2112009ea47eSEdward Tomasz Napierala 	}
2113009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
2114009ea47eSEdward Tomasz Napierala 
2115073edb1cSAlexander Motin 	error = getnameinfo(client_sa, client_sa->sa_len,
2116009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2117009ea47eSEdward Tomasz Napierala 	if (error != 0)
21189a95cfffSEdward Tomasz Napierala 		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2119009ea47eSEdward Tomasz Napierala 
2120009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
2121009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
2122009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
2123009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
2124009ea47eSEdward Tomasz Napierala 
2125073edb1cSAlexander Motin 	conn = connection_new(portal, fd, host, client_sa);
2126829603e2SAlexander Motin 	set_timeout(conf->conf_timeout, true);
2127009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
2128009ea47eSEdward Tomasz Napierala 	login(conn);
2129009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2130009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
2131009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
2132009ea47eSEdward Tomasz Napierala 	} else {
2133009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2134009ea47eSEdward Tomasz Napierala 		discovery(conn);
2135009ea47eSEdward Tomasz Napierala 	}
2136009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
2137009ea47eSEdward Tomasz Napierala 	exit(0);
2138009ea47eSEdward Tomasz Napierala }
2139009ea47eSEdward Tomasz Napierala 
2140009ea47eSEdward Tomasz Napierala static int
2141009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
2142009ea47eSEdward Tomasz Napierala {
2143009ea47eSEdward Tomasz Napierala 
2144009ea47eSEdward Tomasz Napierala 	/*
2145009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
2146009ea47eSEdward Tomasz Napierala 	 */
2147009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
2148009ea47eSEdward Tomasz Napierala 		return (nfds);
2149009ea47eSEdward Tomasz Napierala 
2150009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
2151009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
2152009ea47eSEdward Tomasz Napierala 		nfds = fd;
2153009ea47eSEdward Tomasz Napierala 	return (nfds);
2154009ea47eSEdward Tomasz Napierala }
2155009ea47eSEdward Tomasz Napierala 
2156009ea47eSEdward Tomasz Napierala static void
2157009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
2158009ea47eSEdward Tomasz Napierala {
2159009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
2160009ea47eSEdward Tomasz Napierala 	struct portal *portal;
21619a95cfffSEdward Tomasz Napierala 	struct sockaddr_storage client_sa;
21629a95cfffSEdward Tomasz Napierala 	socklen_t client_salen;
2163009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2164009ea47eSEdward Tomasz Napierala 	int connection_id;
21658cab2ed4SEdward Tomasz Napierala 	int portal_id;
2166ba3a2d31SEdward Tomasz Napierala #endif
2167009ea47eSEdward Tomasz Napierala 	fd_set fdset;
2168009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
2169009ea47eSEdward Tomasz Napierala 
2170009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
2171009ea47eSEdward Tomasz Napierala 
2172009ea47eSEdward Tomasz Napierala 	for (;;) {
2173829603e2SAlexander Motin 		if (sighup_received || sigterm_received || timed_out())
2174009ea47eSEdward Tomasz Napierala 			return;
2175009ea47eSEdward Tomasz Napierala 
2176009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2177ba3a2d31SEdward Tomasz Napierala 		if (proxy_mode) {
21788eab95d6SEdward Tomasz Napierala 			client_salen = sizeof(client_sa);
21798eab95d6SEdward Tomasz Napierala 			kernel_accept(&connection_id, &portal_id,
21808eab95d6SEdward Tomasz Napierala 			    (struct sockaddr *)&client_sa, &client_salen);
21810488e848SEdward Tomasz Napierala 			assert(client_salen >= client_sa.ss_len);
2182009ea47eSEdward Tomasz Napierala 
21838cab2ed4SEdward Tomasz Napierala 			log_debugx("incoming connection, id %d, portal id %d",
21848cab2ed4SEdward Tomasz Napierala 			    connection_id, portal_id);
21858cab2ed4SEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
21868cab2ed4SEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
21878cab2ed4SEdward Tomasz Napierala 					if (portal->p_id == portal_id) {
21888cab2ed4SEdward Tomasz Napierala 						goto found;
21898cab2ed4SEdward Tomasz Napierala 					}
21908cab2ed4SEdward Tomasz Napierala 				}
21918cab2ed4SEdward Tomasz Napierala 			}
2192009ea47eSEdward Tomasz Napierala 
21938cab2ed4SEdward Tomasz Napierala 			log_errx(1, "kernel returned invalid portal_id %d",
21948cab2ed4SEdward Tomasz Napierala 			    portal_id);
21958cab2ed4SEdward Tomasz Napierala 
21968cab2ed4SEdward Tomasz Napierala found:
21978eab95d6SEdward Tomasz Napierala 			handle_connection(portal, connection_id,
2198073edb1cSAlexander Motin 			    (struct sockaddr *)&client_sa, dont_fork);
2199ba3a2d31SEdward Tomasz Napierala 		} else {
2200ba3a2d31SEdward Tomasz Napierala #endif
2201ba3a2d31SEdward Tomasz Napierala 			assert(proxy_mode == false);
2202ba3a2d31SEdward Tomasz Napierala 
2203009ea47eSEdward Tomasz Napierala 			FD_ZERO(&fdset);
2204009ea47eSEdward Tomasz Napierala 			nfds = 0;
2205009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2206009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2207009ea47eSEdward Tomasz Napierala 					nfds = fd_add(portal->p_socket, &fdset, nfds);
2208009ea47eSEdward Tomasz Napierala 			}
2209009ea47eSEdward Tomasz Napierala 			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2210009ea47eSEdward Tomasz Napierala 			if (error <= 0) {
2211009ea47eSEdward Tomasz Napierala 				if (errno == EINTR)
2212009ea47eSEdward Tomasz Napierala 					return;
2213009ea47eSEdward Tomasz Napierala 				log_err(1, "select");
2214009ea47eSEdward Tomasz Napierala 			}
2215009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2216009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2217009ea47eSEdward Tomasz Napierala 					if (!FD_ISSET(portal->p_socket, &fdset))
2218009ea47eSEdward Tomasz Napierala 						continue;
22199a95cfffSEdward Tomasz Napierala 					client_salen = sizeof(client_sa);
22209a95cfffSEdward Tomasz Napierala 					client_fd = accept(portal->p_socket,
22219a95cfffSEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
22229a95cfffSEdward Tomasz Napierala 					    &client_salen);
2223009ea47eSEdward Tomasz Napierala 					if (client_fd < 0)
2224009ea47eSEdward Tomasz Napierala 						log_err(1, "accept");
22250488e848SEdward Tomasz Napierala 					assert(client_salen >= client_sa.ss_len);
22260488e848SEdward Tomasz Napierala 
22279a95cfffSEdward Tomasz Napierala 					handle_connection(portal, client_fd,
22288eab95d6SEdward Tomasz Napierala 					    (struct sockaddr *)&client_sa,
2229073edb1cSAlexander Motin 					    dont_fork);
2230009ea47eSEdward Tomasz Napierala 					break;
2231009ea47eSEdward Tomasz Napierala 				}
2232009ea47eSEdward Tomasz Napierala 			}
2233ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2234ba3a2d31SEdward Tomasz Napierala 		}
2235ba3a2d31SEdward Tomasz Napierala #endif
2236009ea47eSEdward Tomasz Napierala 	}
2237009ea47eSEdward Tomasz Napierala }
2238009ea47eSEdward Tomasz Napierala 
2239009ea47eSEdward Tomasz Napierala static void
2240009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
2241009ea47eSEdward Tomasz Napierala {
2242009ea47eSEdward Tomasz Napierala 
2243009ea47eSEdward Tomasz Napierala 	sighup_received = true;
2244009ea47eSEdward Tomasz Napierala }
2245009ea47eSEdward Tomasz Napierala 
2246009ea47eSEdward Tomasz Napierala static void
2247009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
2248009ea47eSEdward Tomasz Napierala {
2249009ea47eSEdward Tomasz Napierala 
2250009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
2251009ea47eSEdward Tomasz Napierala }
2252009ea47eSEdward Tomasz Napierala 
2253009ea47eSEdward Tomasz Napierala static void
225470b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused)
225570b939ecSEdward Tomasz Napierala {
225670b939ecSEdward Tomasz Napierala 
225770b939ecSEdward Tomasz Napierala 	/*
225870b939ecSEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
225970b939ecSEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
226070b939ecSEdward Tomasz Napierala 	 * wait_for_children().
226170b939ecSEdward Tomasz Napierala 	 */
226270b939ecSEdward Tomasz Napierala }
226370b939ecSEdward Tomasz Napierala 
226470b939ecSEdward Tomasz Napierala static void
2265009ea47eSEdward Tomasz Napierala register_signals(void)
2266009ea47eSEdward Tomasz Napierala {
2267009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
2268009ea47eSEdward Tomasz Napierala 	int error;
2269009ea47eSEdward Tomasz Napierala 
2270009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
2271009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
2272009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
2273009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
2274009ea47eSEdward Tomasz Napierala 	if (error != 0)
2275009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2276009ea47eSEdward Tomasz Napierala 
2277009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2278009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
2279009ea47eSEdward Tomasz Napierala 	if (error != 0)
2280009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
2281009ea47eSEdward Tomasz Napierala 
2282009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
2283009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
2284009ea47eSEdward Tomasz Napierala 	if (error != 0)
2285009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
228670b939ecSEdward Tomasz Napierala 
228770b939ecSEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
228870b939ecSEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
228970b939ecSEdward Tomasz Napierala 	if (error != 0)
229070b939ecSEdward Tomasz Napierala 		log_err(1, "sigaction");
2291009ea47eSEdward Tomasz Napierala }
2292009ea47eSEdward Tomasz Napierala 
2293009ea47eSEdward Tomasz Napierala int
2294009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
2295009ea47eSEdward Tomasz Napierala {
2296009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
2297829603e2SAlexander Motin 	struct isns *newns;
2298009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
2299009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
2300009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
2301009ea47eSEdward Tomasz Napierala 
2302ba3a2d31SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "df:R")) != -1) {
2303009ea47eSEdward Tomasz Napierala 		switch (ch) {
2304009ea47eSEdward Tomasz Napierala 		case 'd':
2305009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
2306009ea47eSEdward Tomasz Napierala 			debug++;
2307009ea47eSEdward Tomasz Napierala 			break;
2308009ea47eSEdward Tomasz Napierala 		case 'f':
2309009ea47eSEdward Tomasz Napierala 			config_path = optarg;
2310009ea47eSEdward Tomasz Napierala 			break;
2311ba3a2d31SEdward Tomasz Napierala 		case 'R':
2312ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
2313ba3a2d31SEdward Tomasz Napierala 			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2314ba3a2d31SEdward Tomasz Napierala 			    "does not support iSER protocol");
2315ba3a2d31SEdward Tomasz Napierala #endif
2316ba3a2d31SEdward Tomasz Napierala 			proxy_mode = true;
2317ba3a2d31SEdward Tomasz Napierala 			break;
2318009ea47eSEdward Tomasz Napierala 		case '?':
2319009ea47eSEdward Tomasz Napierala 		default:
2320009ea47eSEdward Tomasz Napierala 			usage();
2321009ea47eSEdward Tomasz Napierala 		}
2322009ea47eSEdward Tomasz Napierala 	}
2323009ea47eSEdward Tomasz Napierala 	argc -= optind;
2324009ea47eSEdward Tomasz Napierala 	if (argc != 0)
2325009ea47eSEdward Tomasz Napierala 		usage();
2326009ea47eSEdward Tomasz Napierala 
2327009ea47eSEdward Tomasz Napierala 	log_init(debug);
2328009ea47eSEdward Tomasz Napierala 	kernel_init();
2329009ea47eSEdward Tomasz Napierala 
2330009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
2331009ea47eSEdward Tomasz Napierala 	newconf = conf_new_from_file(config_path);
2332009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
2333fe845cdbSEdward Tomasz Napierala 		log_errx(1, "configuration error; exiting");
2334009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
2335009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
2336009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
2337009ea47eSEdward Tomasz Napierala 	}
2338009ea47eSEdward Tomasz Napierala 
2339009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
2340009ea47eSEdward Tomasz Napierala 	if (error != 0)
2341fe845cdbSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration; exiting");
2342fe845cdbSEdward Tomasz Napierala 
2343009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
2344009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
2345009ea47eSEdward Tomasz Napierala 
2346009ea47eSEdward Tomasz Napierala 	register_signals();
2347009ea47eSEdward Tomasz Napierala 
23485d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
23495d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
23505d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
23515d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
23525d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
23535d5a95f1SEdward Tomasz Napierala 			exit(1);
23545d5a95f1SEdward Tomasz Napierala 		}
23555d5a95f1SEdward Tomasz Napierala 	}
23565d5a95f1SEdward Tomasz Napierala 
2357829603e2SAlexander Motin 	/* Schedule iSNS update */
2358829603e2SAlexander Motin 	if (!TAILQ_EMPTY(&newconf->conf_isns))
2359829603e2SAlexander Motin 		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2360829603e2SAlexander Motin 
2361009ea47eSEdward Tomasz Napierala 	for (;;) {
2362009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
2363009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
2364009ea47eSEdward Tomasz Napierala 			sighup_received = false;
2365009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
2366009ea47eSEdward Tomasz Napierala 			tmpconf = conf_new_from_file(config_path);
2367009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
2368009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
2369009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
2370009ea47eSEdward Tomasz Napierala 			} else {
2371009ea47eSEdward Tomasz Napierala 				if (debug > 0)
2372009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
2373009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
2374009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
2375009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
2376009ea47eSEdward Tomasz Napierala 				if (error != 0)
2377009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
2378009ea47eSEdward Tomasz Napierala 					    "configuration");
2379009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
2380009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
2381009ea47eSEdward Tomasz Napierala 			}
2382009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
2383009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
2384009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
2385009ea47eSEdward Tomasz Napierala 
2386*6b31e130SAlexander Motin 			log_debugx("removing CTL iSCSI ports "
2387009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
2388009ea47eSEdward Tomasz Napierala 
2389009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
2390009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
2391009ea47eSEdward Tomasz Napierala 			if (debug > 0)
2392009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
2393009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
2394009ea47eSEdward Tomasz Napierala 			if (error != 0)
2395009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
2396829603e2SAlexander Motin 			conf_delete(oldconf);
2397829603e2SAlexander Motin 			oldconf = NULL;
2398009ea47eSEdward Tomasz Napierala 
2399009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
2400009ea47eSEdward Tomasz Napierala 			exit(0);
2401009ea47eSEdward Tomasz Napierala 		} else {
2402009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
2403009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
2404829603e2SAlexander Motin 			if (timed_out()) {
2405829603e2SAlexander Motin 				set_timeout(0, false);
2406829603e2SAlexander Motin 				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2407829603e2SAlexander Motin 					isns_check(newns);
2408829603e2SAlexander Motin 				/* Schedule iSNS update */
2409829603e2SAlexander Motin 				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2410829603e2SAlexander Motin 					set_timeout((newconf->conf_isns_period
2411829603e2SAlexander Motin 					    + 2) / 3,
2412829603e2SAlexander Motin 					    false);
2413829603e2SAlexander Motin 				}
2414829603e2SAlexander Motin 			}
2415009ea47eSEdward Tomasz Napierala 		}
2416009ea47eSEdward Tomasz Napierala 	}
2417009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
2418009ea47eSEdward Tomasz Napierala }
2419