1255570Strasz/*-
2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation
3255570Strasz * All rights reserved.
4255570Strasz *
5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6255570Strasz * from the FreeBSD Foundation.
7255570Strasz *
8255570Strasz * Redistribution and use in source and binary forms, with or without
9255570Strasz * modification, are permitted provided that the following conditions
10255570Strasz * are met:
11255570Strasz * 1. Redistributions of source code must retain the above copyright
12255570Strasz *    notice, this list of conditions and the following disclaimer.
13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright
14255570Strasz *    notice, this list of conditions and the following disclaimer in the
15255570Strasz *    documentation and/or other materials provided with the distribution.
16255570Strasz *
17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20255570Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27255570Strasz * SUCH DAMAGE.
28255570Strasz *
29255570Strasz */
30255570Strasz
31270888Strasz#include <sys/cdefs.h>
32270888Strasz__FBSDID("$FreeBSD: stable/10/usr.sbin/ctld/ctld.c 317352 2017-04-24 06:33:08Z mav $");
33270888Strasz
34255570Strasz#include <sys/types.h>
35255570Strasz#include <sys/time.h>
36255570Strasz#include <sys/socket.h>
37255570Strasz#include <sys/wait.h>
38255570Strasz#include <netinet/in.h>
39270137Smav#include <arpa/inet.h>
40255570Strasz#include <assert.h>
41255570Strasz#include <ctype.h>
42255570Strasz#include <errno.h>
43255570Strasz#include <netdb.h>
44255570Strasz#include <signal.h>
45255570Strasz#include <stdbool.h>
46255570Strasz#include <stdio.h>
47255570Strasz#include <stdint.h>
48255570Strasz#include <stdlib.h>
49255570Strasz#include <string.h>
50255570Strasz#include <unistd.h>
51255570Strasz
52255570Strasz#include "ctld.h"
53274939Smav#include "isns.h"
54255570Strasz
55265507Straszbool proxy_mode = false;
56265507Strasz
57255570Straszstatic volatile bool sighup_received = false;
58255570Straszstatic volatile bool sigterm_received = false;
59255570Straszstatic volatile bool sigalrm_received = false;
60255570Strasz
61255570Straszstatic int nchildren = 0;
62288729Smavstatic uint16_t last_portal_group_tag = 0xff;
63255570Strasz
64255570Straszstatic void
65255570Straszusage(void)
66255570Strasz{
67255570Strasz
68255570Strasz	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
69255570Strasz	exit(1);
70255570Strasz}
71255570Strasz
72255570Straszchar *
73255570Straszchecked_strdup(const char *s)
74255570Strasz{
75255570Strasz	char *c;
76255570Strasz
77255570Strasz	c = strdup(s);
78255570Strasz	if (c == NULL)
79255570Strasz		log_err(1, "strdup");
80255570Strasz	return (c);
81255570Strasz}
82255570Strasz
83255570Straszstruct conf *
84255570Straszconf_new(void)
85255570Strasz{
86255570Strasz	struct conf *conf;
87255570Strasz
88255570Strasz	conf = calloc(1, sizeof(*conf));
89255570Strasz	if (conf == NULL)
90255570Strasz		log_err(1, "calloc");
91279002Smav	TAILQ_INIT(&conf->conf_luns);
92255570Strasz	TAILQ_INIT(&conf->conf_targets);
93255570Strasz	TAILQ_INIT(&conf->conf_auth_groups);
94279006Smav	TAILQ_INIT(&conf->conf_ports);
95255570Strasz	TAILQ_INIT(&conf->conf_portal_groups);
96279055Smav	TAILQ_INIT(&conf->conf_pports);
97274939Smav	TAILQ_INIT(&conf->conf_isns);
98255570Strasz
99274939Smav	conf->conf_isns_period = 900;
100274939Smav	conf->conf_isns_timeout = 5;
101255570Strasz	conf->conf_debug = 0;
102255570Strasz	conf->conf_timeout = 60;
103255570Strasz	conf->conf_maxproc = 30;
104255570Strasz
105255570Strasz	return (conf);
106255570Strasz}
107255570Strasz
108255570Straszvoid
109255570Straszconf_delete(struct conf *conf)
110255570Strasz{
111279002Smav	struct lun *lun, *ltmp;
112255570Strasz	struct target *targ, *tmp;
113255570Strasz	struct auth_group *ag, *cagtmp;
114255570Strasz	struct portal_group *pg, *cpgtmp;
115279055Smav	struct pport *pp, *pptmp;
116274939Smav	struct isns *is, *istmp;
117255570Strasz
118255570Strasz	assert(conf->conf_pidfh == NULL);
119255570Strasz
120279002Smav	TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
121279002Smav		lun_delete(lun);
122255570Strasz	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
123255570Strasz		target_delete(targ);
124255570Strasz	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
125255570Strasz		auth_group_delete(ag);
126255570Strasz	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
127255570Strasz		portal_group_delete(pg);
128279055Smav	TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
129279055Smav		pport_delete(pp);
130274939Smav	TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
131274939Smav		isns_delete(is);
132279006Smav	assert(TAILQ_EMPTY(&conf->conf_ports));
133255570Strasz	free(conf->conf_pidfile_path);
134255570Strasz	free(conf);
135255570Strasz}
136255570Strasz
137255570Straszstatic struct auth *
138255570Straszauth_new(struct auth_group *ag)
139255570Strasz{
140255570Strasz	struct auth *auth;
141255570Strasz
142255570Strasz	auth = calloc(1, sizeof(*auth));
143255570Strasz	if (auth == NULL)
144255570Strasz		log_err(1, "calloc");
145255570Strasz	auth->a_auth_group = ag;
146255570Strasz	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
147255570Strasz	return (auth);
148255570Strasz}
149255570Strasz
150255570Straszstatic void
151255570Straszauth_delete(struct auth *auth)
152255570Strasz{
153255570Strasz	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
154255570Strasz
155255570Strasz	free(auth->a_user);
156255570Strasz	free(auth->a_secret);
157255570Strasz	free(auth->a_mutual_user);
158255570Strasz	free(auth->a_mutual_secret);
159255570Strasz	free(auth);
160255570Strasz}
161255570Strasz
162255570Straszconst struct auth *
163265514Straszauth_find(const struct auth_group *ag, const char *user)
164255570Strasz{
165255570Strasz	const struct auth *auth;
166255570Strasz
167255570Strasz	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
168255570Strasz		if (strcmp(auth->a_user, user) == 0)
169255570Strasz			return (auth);
170255570Strasz	}
171255570Strasz
172255570Strasz	return (NULL);
173255570Strasz}
174255570Strasz
175263721Straszstatic void
176263721Straszauth_check_secret_length(struct auth *auth)
177263721Strasz{
178263721Strasz	size_t len;
179263721Strasz
180263721Strasz	len = strlen(auth->a_secret);
181263721Strasz	if (len > 16) {
182263721Strasz		if (auth->a_auth_group->ag_name != NULL)
183263721Strasz			log_warnx("secret for user \"%s\", auth-group \"%s\", "
184263721Strasz			    "is too long; it should be at most 16 characters "
185263721Strasz			    "long", auth->a_user, auth->a_auth_group->ag_name);
186263721Strasz		else
187263721Strasz			log_warnx("secret for user \"%s\", target \"%s\", "
188263721Strasz			    "is too long; it should be at most 16 characters "
189263721Strasz			    "long", auth->a_user,
190263723Strasz			    auth->a_auth_group->ag_target->t_name);
191263721Strasz	}
192263721Strasz	if (len < 12) {
193263721Strasz		if (auth->a_auth_group->ag_name != NULL)
194263721Strasz			log_warnx("secret for user \"%s\", auth-group \"%s\", "
195263721Strasz			    "is too short; it should be at least 12 characters "
196263721Strasz			    "long", auth->a_user,
197263721Strasz			    auth->a_auth_group->ag_name);
198263721Strasz		else
199263721Strasz			log_warnx("secret for user \"%s\", target \"%s\", "
200308646Smav			    "is too short; it should be at least 12 characters "
201263721Strasz			    "long", auth->a_user,
202263723Strasz			    auth->a_auth_group->ag_target->t_name);
203263721Strasz	}
204263721Strasz
205263721Strasz	if (auth->a_mutual_secret != NULL) {
206281187Sjpaetzel		len = strlen(auth->a_mutual_secret);
207263721Strasz		if (len > 16) {
208263721Strasz			if (auth->a_auth_group->ag_name != NULL)
209263721Strasz				log_warnx("mutual secret for user \"%s\", "
210263721Strasz				    "auth-group \"%s\", is too long; it should "
211263721Strasz				    "be at most 16 characters long",
212263721Strasz				    auth->a_user, auth->a_auth_group->ag_name);
213263721Strasz			else
214263721Strasz				log_warnx("mutual secret for user \"%s\", "
215263721Strasz				    "target \"%s\", is too long; it should "
216263721Strasz				    "be at most 16 characters long",
217263721Strasz				    auth->a_user,
218263723Strasz				    auth->a_auth_group->ag_target->t_name);
219263721Strasz		}
220263721Strasz		if (len < 12) {
221263721Strasz			if (auth->a_auth_group->ag_name != NULL)
222263721Strasz				log_warnx("mutual secret for user \"%s\", "
223263721Strasz				    "auth-group \"%s\", is too short; it "
224263721Strasz				    "should be at least 12 characters long",
225263721Strasz				    auth->a_user, auth->a_auth_group->ag_name);
226263721Strasz			else
227263721Strasz				log_warnx("mutual secret for user \"%s\", "
228263721Strasz				    "target \"%s\", is too short; it should be "
229308646Smav				    "at least 12 characters long",
230263721Strasz				    auth->a_user,
231263723Strasz				    auth->a_auth_group->ag_target->t_name);
232263721Strasz		}
233263721Strasz	}
234263721Strasz}
235263721Strasz
236263721Straszconst struct auth *
237263721Straszauth_new_chap(struct auth_group *ag, const char *user,
238263721Strasz    const char *secret)
239263721Strasz{
240263721Strasz	struct auth *auth;
241263721Strasz
242263721Strasz	if (ag->ag_type == AG_TYPE_UNKNOWN)
243263721Strasz		ag->ag_type = AG_TYPE_CHAP;
244263721Strasz	if (ag->ag_type != AG_TYPE_CHAP) {
245263721Strasz		if (ag->ag_name != NULL)
246263721Strasz			log_warnx("cannot mix \"chap\" authentication with "
247263721Strasz			    "other types for auth-group \"%s\"", ag->ag_name);
248263721Strasz		else
249263721Strasz			log_warnx("cannot mix \"chap\" authentication with "
250263721Strasz			    "other types for target \"%s\"",
251263723Strasz			    ag->ag_target->t_name);
252263721Strasz		return (NULL);
253263721Strasz	}
254263721Strasz
255263721Strasz	auth = auth_new(ag);
256263721Strasz	auth->a_user = checked_strdup(user);
257263721Strasz	auth->a_secret = checked_strdup(secret);
258263721Strasz
259263721Strasz	auth_check_secret_length(auth);
260263721Strasz
261263721Strasz	return (auth);
262263721Strasz}
263263721Strasz
264263721Straszconst struct auth *
265263721Straszauth_new_chap_mutual(struct auth_group *ag, const char *user,
266263721Strasz    const char *secret, const char *user2, const char *secret2)
267263721Strasz{
268263721Strasz	struct auth *auth;
269263721Strasz
270263721Strasz	if (ag->ag_type == AG_TYPE_UNKNOWN)
271263721Strasz		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
272263721Strasz	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
273263721Strasz		if (ag->ag_name != NULL)
274263721Strasz			log_warnx("cannot mix \"chap-mutual\" authentication "
275263721Strasz			    "with other types for auth-group \"%s\"",
276274870Strasz			    ag->ag_name);
277263721Strasz		else
278263721Strasz			log_warnx("cannot mix \"chap-mutual\" authentication "
279263721Strasz			    "with other types for target \"%s\"",
280263723Strasz			    ag->ag_target->t_name);
281263721Strasz		return (NULL);
282263721Strasz	}
283263721Strasz
284263721Strasz	auth = auth_new(ag);
285263721Strasz	auth->a_user = checked_strdup(user);
286263721Strasz	auth->a_secret = checked_strdup(secret);
287263721Strasz	auth->a_mutual_user = checked_strdup(user2);
288263721Strasz	auth->a_mutual_secret = checked_strdup(secret2);
289263721Strasz
290263721Strasz	auth_check_secret_length(auth);
291263721Strasz
292263721Strasz	return (auth);
293263721Strasz}
294263721Strasz
295263720Straszconst struct auth_name *
296263720Straszauth_name_new(struct auth_group *ag, const char *name)
297263720Strasz{
298263720Strasz	struct auth_name *an;
299263720Strasz
300263720Strasz	an = calloc(1, sizeof(*an));
301263720Strasz	if (an == NULL)
302263720Strasz		log_err(1, "calloc");
303263720Strasz	an->an_auth_group = ag;
304263720Strasz	an->an_initator_name = checked_strdup(name);
305263720Strasz	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
306263720Strasz	return (an);
307263720Strasz}
308263720Strasz
309263720Straszstatic void
310263720Straszauth_name_delete(struct auth_name *an)
311263720Strasz{
312263720Strasz	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
313263720Strasz
314263720Strasz	free(an->an_initator_name);
315263720Strasz	free(an);
316263720Strasz}
317263720Strasz
318263720Straszbool
319263720Straszauth_name_defined(const struct auth_group *ag)
320263720Strasz{
321263720Strasz	if (TAILQ_EMPTY(&ag->ag_names))
322263720Strasz		return (false);
323263720Strasz	return (true);
324263720Strasz}
325263720Strasz
326263720Straszconst struct auth_name *
327263720Straszauth_name_find(const struct auth_group *ag, const char *name)
328263720Strasz{
329263720Strasz	const struct auth_name *auth_name;
330263720Strasz
331263720Strasz	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
332263720Strasz		if (strcmp(auth_name->an_initator_name, name) == 0)
333263720Strasz			return (auth_name);
334263720Strasz	}
335263720Strasz
336263720Strasz	return (NULL);
337263720Strasz}
338263720Strasz
339274949Straszint
340274949Straszauth_name_check(const struct auth_group *ag, const char *initiator_name)
341274949Strasz{
342274949Strasz	if (!auth_name_defined(ag))
343274949Strasz		return (0);
344274949Strasz
345274949Strasz	if (auth_name_find(ag, initiator_name) == NULL)
346274949Strasz		return (1);
347274949Strasz
348274949Strasz	return (0);
349274949Strasz}
350274949Strasz
351263720Straszconst struct auth_portal *
352263720Straszauth_portal_new(struct auth_group *ag, const char *portal)
353263720Strasz{
354263720Strasz	struct auth_portal *ap;
355270137Smav	char *net, *mask, *str, *tmp;
356270137Smav	int len, dm, m;
357263720Strasz
358263720Strasz	ap = calloc(1, sizeof(*ap));
359263720Strasz	if (ap == NULL)
360263720Strasz		log_err(1, "calloc");
361263720Strasz	ap->ap_auth_group = ag;
362263720Strasz	ap->ap_initator_portal = checked_strdup(portal);
363270137Smav	mask = str = checked_strdup(portal);
364270137Smav	net = strsep(&mask, "/");
365270137Smav	if (net[0] == '[')
366270137Smav		net++;
367270137Smav	len = strlen(net);
368270137Smav	if (len == 0)
369270137Smav		goto error;
370270137Smav	if (net[len - 1] == ']')
371270137Smav		net[len - 1] = 0;
372270137Smav	if (strchr(net, ':') != NULL) {
373270137Smav		struct sockaddr_in6 *sin6 =
374270137Smav		    (struct sockaddr_in6 *)&ap->ap_sa;
375270137Smav
376270137Smav		sin6->sin6_len = sizeof(*sin6);
377270137Smav		sin6->sin6_family = AF_INET6;
378270137Smav		if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
379270137Smav			goto error;
380270137Smav		dm = 128;
381270137Smav	} else {
382270137Smav		struct sockaddr_in *sin =
383270137Smav		    (struct sockaddr_in *)&ap->ap_sa;
384270137Smav
385270137Smav		sin->sin_len = sizeof(*sin);
386270137Smav		sin->sin_family = AF_INET;
387270137Smav		if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
388270137Smav			goto error;
389270137Smav		dm = 32;
390270137Smav	}
391270137Smav	if (mask != NULL) {
392270137Smav		m = strtol(mask, &tmp, 0);
393270137Smav		if (m < 0 || m > dm || tmp[0] != 0)
394270137Smav			goto error;
395270137Smav	} else
396270137Smav		m = dm;
397270137Smav	ap->ap_mask = m;
398270137Smav	free(str);
399263720Strasz	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
400263720Strasz	return (ap);
401270137Smav
402270137Smaverror:
403309870Sdelphij	free(str);
404279481Smav	free(ap);
405270137Smav	log_errx(1, "Incorrect initiator portal '%s'", portal);
406270137Smav	return (NULL);
407263720Strasz}
408263720Strasz
409263720Straszstatic void
410263720Straszauth_portal_delete(struct auth_portal *ap)
411263720Strasz{
412263720Strasz	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
413263720Strasz
414263720Strasz	free(ap->ap_initator_portal);
415263720Strasz	free(ap);
416263720Strasz}
417263720Strasz
418263720Straszbool
419263720Straszauth_portal_defined(const struct auth_group *ag)
420263720Strasz{
421263720Strasz	if (TAILQ_EMPTY(&ag->ag_portals))
422263720Strasz		return (false);
423263720Strasz	return (true);
424263720Strasz}
425263720Strasz
426263720Straszconst struct auth_portal *
427270137Smavauth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
428263720Strasz{
429270137Smav	const struct auth_portal *ap;
430270137Smav	const uint8_t *a, *b;
431270137Smav	int i;
432270137Smav	uint8_t bmask;
433263720Strasz
434270137Smav	TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
435270137Smav		if (ap->ap_sa.ss_family != ss->ss_family)
436270137Smav			continue;
437270137Smav		if (ss->ss_family == AF_INET) {
438270137Smav			a = (const uint8_t *)
439270137Smav			    &((const struct sockaddr_in *)ss)->sin_addr;
440270137Smav			b = (const uint8_t *)
441270137Smav			    &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
442270137Smav		} else {
443270137Smav			a = (const uint8_t *)
444270137Smav			    &((const struct sockaddr_in6 *)ss)->sin6_addr;
445270137Smav			b = (const uint8_t *)
446270137Smav			    &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
447270137Smav		}
448270137Smav		for (i = 0; i < ap->ap_mask / 8; i++) {
449270137Smav			if (a[i] != b[i])
450270137Smav				goto next;
451270137Smav		}
452270137Smav		if (ap->ap_mask % 8) {
453270137Smav			bmask = 0xff << (8 - (ap->ap_mask % 8));
454270137Smav			if ((a[i] & bmask) != (b[i] & bmask))
455270137Smav				goto next;
456270137Smav		}
457270137Smav		return (ap);
458270137Smavnext:
459270137Smav		;
460263720Strasz	}
461263720Strasz
462263720Strasz	return (NULL);
463263720Strasz}
464263720Strasz
465274949Straszint
466274949Straszauth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
467274949Strasz{
468274949Strasz
469274949Strasz	if (!auth_portal_defined(ag))
470274949Strasz		return (0);
471274949Strasz
472274949Strasz	if (auth_portal_find(ag, sa) == NULL)
473274949Strasz		return (1);
474274949Strasz
475274949Strasz	return (0);
476274949Strasz}
477274949Strasz
478255570Straszstruct auth_group *
479255570Straszauth_group_new(struct conf *conf, const char *name)
480255570Strasz{
481255570Strasz	struct auth_group *ag;
482255570Strasz
483255570Strasz	if (name != NULL) {
484255570Strasz		ag = auth_group_find(conf, name);
485255570Strasz		if (ag != NULL) {
486255570Strasz			log_warnx("duplicated auth-group \"%s\"", name);
487255570Strasz			return (NULL);
488255570Strasz		}
489255570Strasz	}
490255570Strasz
491255570Strasz	ag = calloc(1, sizeof(*ag));
492255570Strasz	if (ag == NULL)
493255570Strasz		log_err(1, "calloc");
494255570Strasz	if (name != NULL)
495255570Strasz		ag->ag_name = checked_strdup(name);
496255570Strasz	TAILQ_INIT(&ag->ag_auths);
497263720Strasz	TAILQ_INIT(&ag->ag_names);
498263720Strasz	TAILQ_INIT(&ag->ag_portals);
499255570Strasz	ag->ag_conf = conf;
500255570Strasz	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
501255570Strasz
502255570Strasz	return (ag);
503255570Strasz}
504255570Strasz
505255570Straszvoid
506255570Straszauth_group_delete(struct auth_group *ag)
507255570Strasz{
508263720Strasz	struct auth *auth, *auth_tmp;
509263720Strasz	struct auth_name *auth_name, *auth_name_tmp;
510263720Strasz	struct auth_portal *auth_portal, *auth_portal_tmp;
511255570Strasz
512255570Strasz	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
513255570Strasz
514263720Strasz	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
515255570Strasz		auth_delete(auth);
516263720Strasz	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
517263720Strasz		auth_name_delete(auth_name);
518263720Strasz	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
519263720Strasz	    auth_portal_tmp)
520263720Strasz		auth_portal_delete(auth_portal);
521255570Strasz	free(ag->ag_name);
522255570Strasz	free(ag);
523255570Strasz}
524255570Strasz
525255570Straszstruct auth_group *
526265514Straszauth_group_find(const struct conf *conf, const char *name)
527255570Strasz{
528255570Strasz	struct auth_group *ag;
529255570Strasz
530255570Strasz	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
531255570Strasz		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
532255570Strasz			return (ag);
533255570Strasz	}
534255570Strasz
535255570Strasz	return (NULL);
536255570Strasz}
537255570Strasz
538263724Straszint
539275245Straszauth_group_set_type(struct auth_group *ag, const char *str)
540263724Strasz{
541275245Strasz	int type;
542263724Strasz
543263724Strasz	if (strcmp(str, "none") == 0) {
544263724Strasz		type = AG_TYPE_NO_AUTHENTICATION;
545263729Strasz	} else if (strcmp(str, "deny") == 0) {
546263729Strasz		type = AG_TYPE_DENY;
547263724Strasz	} else if (strcmp(str, "chap") == 0) {
548263724Strasz		type = AG_TYPE_CHAP;
549263724Strasz	} else if (strcmp(str, "chap-mutual") == 0) {
550263724Strasz		type = AG_TYPE_CHAP_MUTUAL;
551263724Strasz	} else {
552263724Strasz		if (ag->ag_name != NULL)
553263724Strasz			log_warnx("invalid auth-type \"%s\" for auth-group "
554263724Strasz			    "\"%s\"", str, ag->ag_name);
555263724Strasz		else
556263724Strasz			log_warnx("invalid auth-type \"%s\" for target "
557263724Strasz			    "\"%s\"", str, ag->ag_target->t_name);
558263724Strasz		return (1);
559263724Strasz	}
560263724Strasz
561275245Strasz	if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
562275245Strasz		if (ag->ag_name != NULL) {
563263724Strasz			log_warnx("cannot set auth-type to \"%s\" for "
564263724Strasz			    "auth-group \"%s\"; already has a different "
565263724Strasz			    "type", str, ag->ag_name);
566275245Strasz		} else {
567263724Strasz			log_warnx("cannot set auth-type to \"%s\" for target "
568263724Strasz			    "\"%s\"; already has a different type",
569263724Strasz			    str, ag->ag_target->t_name);
570275245Strasz		}
571263724Strasz		return (1);
572263724Strasz	}
573263724Strasz
574275245Strasz	ag->ag_type = type;
575275245Strasz
576275245Strasz	return (0);
577263724Strasz}
578263724Strasz
579255570Straszstatic struct portal *
580255570Straszportal_new(struct portal_group *pg)
581255570Strasz{
582255570Strasz	struct portal *portal;
583255570Strasz
584255570Strasz	portal = calloc(1, sizeof(*portal));
585255570Strasz	if (portal == NULL)
586255570Strasz		log_err(1, "calloc");
587255570Strasz	TAILQ_INIT(&portal->p_targets);
588255570Strasz	portal->p_portal_group = pg;
589255570Strasz	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
590255570Strasz	return (portal);
591255570Strasz}
592255570Strasz
593255570Straszstatic void
594255570Straszportal_delete(struct portal *portal)
595255570Strasz{
596271632Strasz
597255570Strasz	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
598271632Strasz	if (portal->p_ai != NULL)
599271632Strasz		freeaddrinfo(portal->p_ai);
600255570Strasz	free(portal->p_listen);
601255570Strasz	free(portal);
602255570Strasz}
603255570Strasz
604255570Straszstruct portal_group *
605255570Straszportal_group_new(struct conf *conf, const char *name)
606255570Strasz{
607255570Strasz	struct portal_group *pg;
608255570Strasz
609255678Strasz	pg = portal_group_find(conf, name);
610255678Strasz	if (pg != NULL) {
611255678Strasz		log_warnx("duplicated portal-group \"%s\"", name);
612255678Strasz		return (NULL);
613255570Strasz	}
614255570Strasz
615255570Strasz	pg = calloc(1, sizeof(*pg));
616255570Strasz	if (pg == NULL)
617255570Strasz		log_err(1, "calloc");
618255570Strasz	pg->pg_name = checked_strdup(name);
619291387Smav	TAILQ_INIT(&pg->pg_options);
620255570Strasz	TAILQ_INIT(&pg->pg_portals);
621279006Smav	TAILQ_INIT(&pg->pg_ports);
622255570Strasz	pg->pg_conf = conf;
623279003Smav	pg->pg_tag = 0;		/* Assigned later in conf_apply(). */
624255570Strasz	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
625255570Strasz
626255570Strasz	return (pg);
627255570Strasz}
628255570Strasz
629255570Straszvoid
630255570Straszportal_group_delete(struct portal_group *pg)
631255570Strasz{
632255570Strasz	struct portal *portal, *tmp;
633279006Smav	struct port *port, *tport;
634291387Smav	struct option *o, *otmp;
635255570Strasz
636279006Smav	TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
637279006Smav		port_delete(port);
638255570Strasz	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
639255570Strasz
640255570Strasz	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
641255570Strasz		portal_delete(portal);
642291387Smav	TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
643291387Smav		option_delete(&pg->pg_options, o);
644255570Strasz	free(pg->pg_name);
645275642Strasz	free(pg->pg_redirection);
646255570Strasz	free(pg);
647255570Strasz}
648255570Strasz
649255570Straszstruct portal_group *
650265514Straszportal_group_find(const struct conf *conf, const char *name)
651255570Strasz{
652255570Strasz	struct portal_group *pg;
653255570Strasz
654255570Strasz	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
655255570Strasz		if (strcmp(pg->pg_name, name) == 0)
656255570Strasz			return (pg);
657255570Strasz	}
658255570Strasz
659255570Strasz	return (NULL);
660255570Strasz}
661255570Strasz
662274939Smavstatic int
663274939Smavparse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
664255570Strasz{
665255570Strasz	struct addrinfo hints;
666275674Smav	char *str, *addr, *ch;
667255570Strasz	const char *port;
668255570Strasz	int error, colons = 0;
669255570Strasz
670275674Smav	str = arg = strdup(arg);
671255570Strasz	if (arg[0] == '[') {
672255570Strasz		/*
673255570Strasz		 * IPv6 address in square brackets, perhaps with port.
674255570Strasz		 */
675255570Strasz		arg++;
676255570Strasz		addr = strsep(&arg, "]");
677309870Sdelphij		if (arg == NULL) {
678309870Sdelphij			free(str);
679255570Strasz			return (1);
680309870Sdelphij		}
681255570Strasz		if (arg[0] == '\0') {
682274939Smav			port = def_port;
683255570Strasz		} else if (arg[0] == ':') {
684255570Strasz			port = arg + 1;
685275674Smav		} else {
686275674Smav			free(str);
687255570Strasz			return (1);
688275674Smav		}
689255570Strasz	} else {
690255570Strasz		/*
691255570Strasz		 * Either IPv6 address without brackets - and without
692255570Strasz		 * a port - or IPv4 address.  Just count the colons.
693255570Strasz		 */
694255570Strasz		for (ch = arg; *ch != '\0'; ch++) {
695255570Strasz			if (*ch == ':')
696255570Strasz				colons++;
697255570Strasz		}
698255570Strasz		if (colons > 1) {
699255570Strasz			addr = arg;
700274939Smav			port = def_port;
701255570Strasz		} else {
702255570Strasz			addr = strsep(&arg, ":");
703255570Strasz			if (arg == NULL)
704274939Smav				port = def_port;
705255570Strasz			else
706255570Strasz				port = arg;
707255570Strasz		}
708255570Strasz	}
709255570Strasz
710255570Strasz	memset(&hints, 0, sizeof(hints));
711255570Strasz	hints.ai_family = PF_UNSPEC;
712255570Strasz	hints.ai_socktype = SOCK_STREAM;
713255570Strasz	hints.ai_flags = AI_PASSIVE;
714274939Smav	error = getaddrinfo(addr, port, &hints, ai);
715275674Smav	free(str);
716275674Smav	return ((error != 0) ? 1 : 0);
717274939Smav}
718255570Strasz
719274939Smavint
720274939Smavportal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
721274939Smav{
722274939Smav	struct portal *portal;
723274939Smav
724274939Smav	portal = portal_new(pg);
725274939Smav	portal->p_listen = checked_strdup(value);
726274939Smav	portal->p_iser = iser;
727274939Smav
728274939Smav	if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
729274939Smav		log_warnx("invalid listen address %s", portal->p_listen);
730271632Strasz		portal_delete(portal);
731255570Strasz		return (1);
732255570Strasz	}
733255570Strasz
734255570Strasz	/*
735255570Strasz	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
736255570Strasz	 *	those into multiple portals.
737255570Strasz	 */
738255570Strasz
739255570Strasz	return (0);
740255570Strasz}
741255570Strasz
742274939Smavint
743274939Smavisns_new(struct conf *conf, const char *addr)
744274939Smav{
745274939Smav	struct isns *isns;
746274939Smav
747274939Smav	isns = calloc(1, sizeof(*isns));
748274939Smav	if (isns == NULL)
749274939Smav		log_err(1, "calloc");
750274939Smav	isns->i_conf = conf;
751274939Smav	TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
752274939Smav	isns->i_addr = checked_strdup(addr);
753274939Smav
754274939Smav	if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
755274939Smav		log_warnx("invalid iSNS address %s", isns->i_addr);
756274939Smav		isns_delete(isns);
757274939Smav		return (1);
758274939Smav	}
759274939Smav
760274939Smav	/*
761274939Smav	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
762274939Smav	 *	those into multiple servers.
763274939Smav	 */
764274939Smav
765274939Smav	return (0);
766274939Smav}
767274939Smav
768274939Smavvoid
769274939Smavisns_delete(struct isns *isns)
770274939Smav{
771274939Smav
772274939Smav	TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
773274939Smav	free(isns->i_addr);
774274939Smav	if (isns->i_ai != NULL)
775274939Smav		freeaddrinfo(isns->i_ai);
776274939Smav	free(isns);
777274939Smav}
778274939Smav
779274939Smavstatic int
780274939Smavisns_do_connect(struct isns *isns)
781274939Smav{
782274939Smav	int s;
783274939Smav
784274939Smav	s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
785274939Smav	    isns->i_ai->ai_protocol);
786274939Smav	if (s < 0) {
787274939Smav		log_warn("socket(2) failed for %s", isns->i_addr);
788274939Smav		return (-1);
789274939Smav	}
790274939Smav	if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
791274939Smav		log_warn("connect(2) failed for %s", isns->i_addr);
792274939Smav		close(s);
793274939Smav		return (-1);
794274939Smav	}
795274939Smav	return(s);
796274939Smav}
797274939Smav
798274939Smavstatic int
799274939Smavisns_do_register(struct isns *isns, int s, const char *hostname)
800274939Smav{
801274939Smav	struct conf *conf = isns->i_conf;
802274939Smav	struct target *target;
803274939Smav	struct portal *portal;
804274939Smav	struct portal_group *pg;
805279006Smav	struct port *port;
806274939Smav	struct isns_req *req;
807274939Smav	int res = 0;
808274939Smav	uint32_t error;
809274939Smav
810274939Smav	req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
811274939Smav	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
812274939Smav	isns_req_add_delim(req);
813274939Smav	isns_req_add_str(req, 1, hostname);
814274939Smav	isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
815274939Smav	isns_req_add_32(req, 6, conf->conf_isns_period);
816274939Smav	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
817274939Smav		if (pg->pg_unassigned)
818274939Smav			continue;
819274939Smav		TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
820274939Smav			isns_req_add_addr(req, 16, portal->p_ai);
821274939Smav			isns_req_add_port(req, 17, portal->p_ai);
822274939Smav		}
823274939Smav	}
824274939Smav	TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
825274939Smav		isns_req_add_str(req, 32, target->t_name);
826274939Smav		isns_req_add_32(req, 33, 1); /* 1 -- Target*/
827274939Smav		if (target->t_alias != NULL)
828274939Smav			isns_req_add_str(req, 34, target->t_alias);
829279006Smav		TAILQ_FOREACH(port, &target->t_ports, p_ts) {
830279006Smav			if ((pg = port->p_portal_group) == NULL)
831279006Smav				continue;
832279006Smav			isns_req_add_32(req, 51, pg->pg_tag);
833279006Smav			TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
834279006Smav				isns_req_add_addr(req, 49, portal->p_ai);
835279006Smav				isns_req_add_port(req, 50, portal->p_ai);
836279006Smav			}
837274939Smav		}
838274939Smav	}
839274939Smav	res = isns_req_send(s, req);
840274939Smav	if (res < 0) {
841274939Smav		log_warn("send(2) failed for %s", isns->i_addr);
842274939Smav		goto quit;
843274939Smav	}
844274939Smav	res = isns_req_receive(s, req);
845274939Smav	if (res < 0) {
846274939Smav		log_warn("receive(2) failed for %s", isns->i_addr);
847274939Smav		goto quit;
848274939Smav	}
849274939Smav	error = isns_req_get_status(req);
850274939Smav	if (error != 0) {
851274939Smav		log_warnx("iSNS register error %d for %s", error, isns->i_addr);
852274939Smav		res = -1;
853274939Smav	}
854274939Smavquit:
855274939Smav	isns_req_free(req);
856274939Smav	return (res);
857274939Smav}
858274939Smav
859274939Smavstatic int
860274939Smavisns_do_check(struct isns *isns, int s, const char *hostname)
861274939Smav{
862274939Smav	struct conf *conf = isns->i_conf;
863274939Smav	struct isns_req *req;
864274939Smav	int res = 0;
865274939Smav	uint32_t error;
866274939Smav
867274939Smav	req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
868274939Smav	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
869274939Smav	isns_req_add_str(req, 1, hostname);
870274939Smav	isns_req_add_delim(req);
871274939Smav	isns_req_add(req, 2, 0, NULL);
872274939Smav	res = isns_req_send(s, req);
873274939Smav	if (res < 0) {
874274939Smav		log_warn("send(2) failed for %s", isns->i_addr);
875274939Smav		goto quit;
876274939Smav	}
877274939Smav	res = isns_req_receive(s, req);
878274939Smav	if (res < 0) {
879274939Smav		log_warn("receive(2) failed for %s", isns->i_addr);
880274939Smav		goto quit;
881274939Smav	}
882274939Smav	error = isns_req_get_status(req);
883274939Smav	if (error != 0) {
884274939Smav		log_warnx("iSNS check error %d for %s", error, isns->i_addr);
885274939Smav		res = -1;
886274939Smav	}
887274939Smavquit:
888274939Smav	isns_req_free(req);
889274939Smav	return (res);
890274939Smav}
891274939Smav
892274939Smavstatic int
893274939Smavisns_do_deregister(struct isns *isns, int s, const char *hostname)
894274939Smav{
895274939Smav	struct conf *conf = isns->i_conf;
896274939Smav	struct isns_req *req;
897274939Smav	int res = 0;
898274939Smav	uint32_t error;
899274939Smav
900274939Smav	req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
901274939Smav	isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
902274939Smav	isns_req_add_delim(req);
903274939Smav	isns_req_add_str(req, 1, hostname);
904274939Smav	res = isns_req_send(s, req);
905274939Smav	if (res < 0) {
906274939Smav		log_warn("send(2) failed for %s", isns->i_addr);
907274939Smav		goto quit;
908274939Smav	}
909274939Smav	res = isns_req_receive(s, req);
910274939Smav	if (res < 0) {
911274939Smav		log_warn("receive(2) failed for %s", isns->i_addr);
912274939Smav		goto quit;
913274939Smav	}
914274939Smav	error = isns_req_get_status(req);
915274939Smav	if (error != 0) {
916274939Smav		log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
917274939Smav		res = -1;
918274939Smav	}
919274939Smavquit:
920274939Smav	isns_req_free(req);
921274939Smav	return (res);
922274939Smav}
923274939Smav
924274939Smavvoid
925274939Smavisns_register(struct isns *isns, struct isns *oldisns)
926274939Smav{
927274939Smav	struct conf *conf = isns->i_conf;
928275496Smav	int s;
929274939Smav	char hostname[256];
930274939Smav
931274939Smav	if (TAILQ_EMPTY(&conf->conf_targets) ||
932274939Smav	    TAILQ_EMPTY(&conf->conf_portal_groups))
933274939Smav		return;
934274939Smav	set_timeout(conf->conf_isns_timeout, false);
935274939Smav	s = isns_do_connect(isns);
936274939Smav	if (s < 0) {
937274939Smav		set_timeout(0, false);
938274939Smav		return;
939274939Smav	}
940274939Smav	gethostname(hostname, sizeof(hostname));
941274939Smav
942274939Smav	if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
943274939Smav		oldisns = isns;
944275496Smav	isns_do_deregister(oldisns, s, hostname);
945275496Smav	isns_do_register(isns, s, hostname);
946274939Smav	close(s);
947274939Smav	set_timeout(0, false);
948274939Smav}
949274939Smav
950274939Smavvoid
951274939Smavisns_check(struct isns *isns)
952274939Smav{
953274939Smav	struct conf *conf = isns->i_conf;
954274939Smav	int s, res;
955274939Smav	char hostname[256];
956274939Smav
957274939Smav	if (TAILQ_EMPTY(&conf->conf_targets) ||
958274939Smav	    TAILQ_EMPTY(&conf->conf_portal_groups))
959274939Smav		return;
960274939Smav	set_timeout(conf->conf_isns_timeout, false);
961274939Smav	s = isns_do_connect(isns);
962274939Smav	if (s < 0) {
963274939Smav		set_timeout(0, false);
964274939Smav		return;
965274939Smav	}
966274939Smav	gethostname(hostname, sizeof(hostname));
967274939Smav
968274939Smav	res = isns_do_check(isns, s, hostname);
969274939Smav	if (res < 0) {
970275496Smav		isns_do_deregister(isns, s, hostname);
971275496Smav		isns_do_register(isns, s, hostname);
972274939Smav	}
973274939Smav	close(s);
974274939Smav	set_timeout(0, false);
975274939Smav}
976274939Smav
977274939Smavvoid
978274939Smavisns_deregister(struct isns *isns)
979274939Smav{
980274939Smav	struct conf *conf = isns->i_conf;
981275496Smav	int s;
982274939Smav	char hostname[256];
983274939Smav
984274939Smav	if (TAILQ_EMPTY(&conf->conf_targets) ||
985274939Smav	    TAILQ_EMPTY(&conf->conf_portal_groups))
986274939Smav		return;
987274939Smav	set_timeout(conf->conf_isns_timeout, false);
988274939Smav	s = isns_do_connect(isns);
989274939Smav	if (s < 0)
990274939Smav		return;
991274939Smav	gethostname(hostname, sizeof(hostname));
992274939Smav
993275496Smav	isns_do_deregister(isns, s, hostname);
994274939Smav	close(s);
995274939Smav	set_timeout(0, false);
996274939Smav}
997274939Smav
998275244Straszint
999275245Straszportal_group_set_filter(struct portal_group *pg, const char *str)
1000275244Strasz{
1001275245Strasz	int filter;
1002275244Strasz
1003275244Strasz	if (strcmp(str, "none") == 0) {
1004275244Strasz		filter = PG_FILTER_NONE;
1005275244Strasz	} else if (strcmp(str, "portal") == 0) {
1006275244Strasz		filter = PG_FILTER_PORTAL;
1007275244Strasz	} else if (strcmp(str, "portal-name") == 0) {
1008275244Strasz		filter = PG_FILTER_PORTAL_NAME;
1009275244Strasz	} else if (strcmp(str, "portal-name-auth") == 0) {
1010275244Strasz		filter = PG_FILTER_PORTAL_NAME_AUTH;
1011275244Strasz	} else {
1012275244Strasz		log_warnx("invalid discovery-filter \"%s\" for portal-group "
1013275244Strasz		    "\"%s\"; valid values are \"none\", \"portal\", "
1014275244Strasz		    "\"portal-name\", and \"portal-name-auth\"",
1015275244Strasz		    str, pg->pg_name);
1016275244Strasz		return (1);
1017275244Strasz	}
1018275244Strasz
1019275245Strasz	if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
1020275245Strasz	    pg->pg_discovery_filter != filter) {
1021275244Strasz		log_warnx("cannot set discovery-filter to \"%s\" for "
1022275244Strasz		    "portal-group \"%s\"; already has a different "
1023275244Strasz		    "value", str, pg->pg_name);
1024275244Strasz		return (1);
1025275244Strasz	}
1026275244Strasz
1027275245Strasz	pg->pg_discovery_filter = filter;
1028275245Strasz
1029275245Strasz	return (0);
1030275244Strasz}
1031275244Strasz
1032275642Straszint
1033275642Straszportal_group_set_redirection(struct portal_group *pg, const char *addr)
1034275642Strasz{
1035275642Strasz
1036275642Strasz	if (pg->pg_redirection != NULL) {
1037275642Strasz		log_warnx("cannot set redirection to \"%s\" for "
1038275642Strasz		    "portal-group \"%s\"; already defined",
1039275642Strasz		    addr, pg->pg_name);
1040275642Strasz		return (1);
1041275642Strasz	}
1042275642Strasz
1043275642Strasz	pg->pg_redirection = checked_strdup(addr);
1044275642Strasz
1045275642Strasz	return (0);
1046275642Strasz}
1047275642Strasz
1048255570Straszstatic bool
1049255570Straszvalid_hex(const char ch)
1050255570Strasz{
1051255570Strasz	switch (ch) {
1052255570Strasz	case '0':
1053255570Strasz	case '1':
1054255570Strasz	case '2':
1055255570Strasz	case '3':
1056255570Strasz	case '4':
1057255570Strasz	case '5':
1058255570Strasz	case '6':
1059255570Strasz	case '7':
1060255570Strasz	case '8':
1061255570Strasz	case '9':
1062255570Strasz	case 'a':
1063255570Strasz	case 'A':
1064255570Strasz	case 'b':
1065255570Strasz	case 'B':
1066255570Strasz	case 'c':
1067255570Strasz	case 'C':
1068255570Strasz	case 'd':
1069255570Strasz	case 'D':
1070255570Strasz	case 'e':
1071255570Strasz	case 'E':
1072255570Strasz	case 'f':
1073255570Strasz	case 'F':
1074255570Strasz		return (true);
1075255570Strasz	default:
1076255570Strasz		return (false);
1077255570Strasz	}
1078255570Strasz}
1079255570Strasz
1080255570Straszbool
1081255570Straszvalid_iscsi_name(const char *name)
1082255570Strasz{
1083255570Strasz	int i;
1084255570Strasz
1085255570Strasz	if (strlen(name) >= MAX_NAME_LEN) {
1086255570Strasz		log_warnx("overlong name for target \"%s\"; max length allowed "
1087255570Strasz		    "by iSCSI specification is %d characters",
1088255570Strasz		    name, MAX_NAME_LEN);
1089255570Strasz		return (false);
1090255570Strasz	}
1091255570Strasz
1092255570Strasz	/*
1093255570Strasz	 * In the cases below, we don't return an error, just in case the admin
1094255570Strasz	 * was right, and we're wrong.
1095255570Strasz	 */
1096255570Strasz	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1097255570Strasz		for (i = strlen("iqn."); name[i] != '\0'; i++) {
1098255570Strasz			/*
1099255570Strasz			 * XXX: We should verify UTF-8 normalisation, as defined
1100274870Strasz			 *      by 3.2.6.2: iSCSI Name Encoding.
1101255570Strasz			 */
1102255570Strasz			if (isalnum(name[i]))
1103255570Strasz				continue;
1104255570Strasz			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1105255570Strasz				continue;
1106255570Strasz			log_warnx("invalid character \"%c\" in target name "
1107255570Strasz			    "\"%s\"; allowed characters are letters, digits, "
1108255570Strasz			    "'-', '.', and ':'", name[i], name);
1109255570Strasz			break;
1110255570Strasz		}
1111255570Strasz		/*
1112255570Strasz		 * XXX: Check more stuff: valid date and a valid reversed domain.
1113255570Strasz		 */
1114255570Strasz	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1115255570Strasz		if (strlen(name) != strlen("eui.") + 16)
1116255570Strasz			log_warnx("invalid target name \"%s\"; the \"eui.\" "
1117255570Strasz			    "should be followed by exactly 16 hexadecimal "
1118255570Strasz			    "digits", name);
1119255570Strasz		for (i = strlen("eui."); name[i] != '\0'; i++) {
1120255570Strasz			if (!valid_hex(name[i])) {
1121255570Strasz				log_warnx("invalid character \"%c\" in target "
1122255570Strasz				    "name \"%s\"; allowed characters are 1-9 "
1123255570Strasz				    "and A-F", name[i], name);
1124255570Strasz				break;
1125255570Strasz			}
1126255570Strasz		}
1127255570Strasz	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1128255570Strasz		if (strlen(name) > strlen("naa.") + 32)
1129255570Strasz			log_warnx("invalid target name \"%s\"; the \"naa.\" "
1130255570Strasz			    "should be followed by at most 32 hexadecimal "
1131255570Strasz			    "digits", name);
1132255570Strasz		for (i = strlen("naa."); name[i] != '\0'; i++) {
1133255570Strasz			if (!valid_hex(name[i])) {
1134255570Strasz				log_warnx("invalid character \"%c\" in target "
1135255570Strasz				    "name \"%s\"; allowed characters are 1-9 "
1136255570Strasz				    "and A-F", name[i], name);
1137255570Strasz				break;
1138255570Strasz			}
1139255570Strasz		}
1140255570Strasz	} else {
1141255570Strasz		log_warnx("invalid target name \"%s\"; should start with "
1142288209Sjpaetzel		    "either \"iqn.\", \"eui.\", or \"naa.\"",
1143255570Strasz		    name);
1144255570Strasz	}
1145255570Strasz	return (true);
1146255570Strasz}
1147255570Strasz
1148279055Smavstruct pport *
1149279055Smavpport_new(struct conf *conf, const char *name, uint32_t ctl_port)
1150279055Smav{
1151279055Smav	struct pport *pp;
1152279055Smav
1153279055Smav	pp = calloc(1, sizeof(*pp));
1154279055Smav	if (pp == NULL)
1155279055Smav		log_err(1, "calloc");
1156279055Smav	pp->pp_conf = conf;
1157279055Smav	pp->pp_name = checked_strdup(name);
1158279055Smav	pp->pp_ctl_port = ctl_port;
1159279055Smav	TAILQ_INIT(&pp->pp_ports);
1160279055Smav	TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next);
1161279055Smav	return (pp);
1162279055Smav}
1163279055Smav
1164279055Smavstruct pport *
1165279055Smavpport_find(const struct conf *conf, const char *name)
1166279055Smav{
1167279055Smav	struct pport *pp;
1168279055Smav
1169279055Smav	TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) {
1170279055Smav		if (strcasecmp(pp->pp_name, name) == 0)
1171279055Smav			return (pp);
1172279055Smav	}
1173279055Smav	return (NULL);
1174279055Smav}
1175279055Smav
1176279055Smavstruct pport *
1177279055Smavpport_copy(struct pport *pp, struct conf *conf)
1178279055Smav{
1179279055Smav	struct pport *ppnew;
1180279055Smav
1181279055Smav	ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port);
1182279055Smav	return (ppnew);
1183279055Smav}
1184279055Smav
1185279055Smavvoid
1186279055Smavpport_delete(struct pport *pp)
1187279055Smav{
1188279055Smav	struct port *port, *tport;
1189279055Smav
1190279055Smav	TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport)
1191279055Smav		port_delete(port);
1192279055Smav	TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next);
1193279055Smav	free(pp->pp_name);
1194279055Smav	free(pp);
1195279055Smav}
1196279055Smav
1197279006Smavstruct port *
1198279006Smavport_new(struct conf *conf, struct target *target, struct portal_group *pg)
1199279006Smav{
1200279006Smav	struct port *port;
1201279055Smav	char *name;
1202279056Smav	int ret;
1203279006Smav
1204279056Smav	ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name);
1205279056Smav	if (ret <= 0)
1206279056Smav		log_err(1, "asprintf");
1207279055Smav	if (port_find(conf, name) != NULL) {
1208279055Smav		log_warnx("duplicate port \"%s\"", name);
1209279055Smav		free(name);
1210279055Smav		return (NULL);
1211279055Smav	}
1212279006Smav	port = calloc(1, sizeof(*port));
1213279006Smav	if (port == NULL)
1214279006Smav		log_err(1, "calloc");
1215279006Smav	port->p_conf = conf;
1216279055Smav	port->p_name = name;
1217279006Smav	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1218279006Smav	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1219279006Smav	port->p_target = target;
1220279006Smav	TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs);
1221279006Smav	port->p_portal_group = pg;
1222279006Smav	return (port);
1223279006Smav}
1224279006Smav
1225279006Smavstruct port *
1226279055Smavport_new_pp(struct conf *conf, struct target *target, struct pport *pp)
1227279055Smav{
1228279055Smav	struct port *port;
1229279055Smav	char *name;
1230279056Smav	int ret;
1231279055Smav
1232279056Smav	ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name);
1233279056Smav	if (ret <= 0)
1234279056Smav		log_err(1, "asprintf");
1235279055Smav	if (port_find(conf, name) != NULL) {
1236279055Smav		log_warnx("duplicate port \"%s\"", name);
1237279055Smav		free(name);
1238279055Smav		return (NULL);
1239279055Smav	}
1240279055Smav	port = calloc(1, sizeof(*port));
1241279055Smav	if (port == NULL)
1242279055Smav		log_err(1, "calloc");
1243279055Smav	port->p_conf = conf;
1244279055Smav	port->p_name = name;
1245279055Smav	TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next);
1246279055Smav	TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts);
1247279055Smav	port->p_target = target;
1248279055Smav	TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps);
1249279055Smav	port->p_pport = pp;
1250279055Smav	return (port);
1251279055Smav}
1252279055Smav
1253279055Smavstruct port *
1254279006Smavport_find(const struct conf *conf, const char *name)
1255279006Smav{
1256279006Smav	struct port *port;
1257279006Smav
1258279006Smav	TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1259279006Smav		if (strcasecmp(port->p_name, name) == 0)
1260279006Smav			return (port);
1261279006Smav	}
1262279006Smav
1263279006Smav	return (NULL);
1264279006Smav}
1265279006Smav
1266279006Smavstruct port *
1267279006Smavport_find_in_pg(const struct portal_group *pg, const char *target)
1268279006Smav{
1269279006Smav	struct port *port;
1270279006Smav
1271279006Smav	TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
1272279006Smav		if (strcasecmp(port->p_target->t_name, target) == 0)
1273279006Smav			return (port);
1274279006Smav	}
1275279006Smav
1276279006Smav	return (NULL);
1277279006Smav}
1278279006Smav
1279279006Smavvoid
1280279006Smavport_delete(struct port *port)
1281279006Smav{
1282279006Smav
1283279006Smav	if (port->p_portal_group)
1284279006Smav		TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs);
1285279055Smav	if (port->p_pport)
1286279055Smav		TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps);
1287279006Smav	if (port->p_target)
1288279006Smav		TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts);
1289279006Smav	TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next);
1290279006Smav	free(port->p_name);
1291279006Smav	free(port);
1292279006Smav}
1293279006Smav
1294317352Smavint
1295317352Smavport_is_dummy(struct port *port)
1296317352Smav{
1297317352Smav
1298317352Smav	if (port->p_portal_group) {
1299317352Smav		if (port->p_portal_group->pg_foreign)
1300317352Smav			return (1);
1301317352Smav		if (TAILQ_EMPTY(&port->p_portal_group->pg_portals))
1302317352Smav			return (1);
1303317352Smav	}
1304317352Smav	return (0);
1305317352Smav}
1306317352Smav
1307255570Straszstruct target *
1308263723Strasztarget_new(struct conf *conf, const char *name)
1309255570Strasz{
1310255570Strasz	struct target *targ;
1311255570Strasz	int i, len;
1312255570Strasz
1313263723Strasz	targ = target_find(conf, name);
1314255570Strasz	if (targ != NULL) {
1315263723Strasz		log_warnx("duplicated target \"%s\"", name);
1316255570Strasz		return (NULL);
1317255570Strasz	}
1318263723Strasz	if (valid_iscsi_name(name) == false) {
1319263723Strasz		log_warnx("target name \"%s\" is invalid", name);
1320255570Strasz		return (NULL);
1321255570Strasz	}
1322255570Strasz	targ = calloc(1, sizeof(*targ));
1323255570Strasz	if (targ == NULL)
1324255570Strasz		log_err(1, "calloc");
1325263723Strasz	targ->t_name = checked_strdup(name);
1326255570Strasz
1327255570Strasz	/*
1328255570Strasz	 * RFC 3722 requires us to normalize the name to lowercase.
1329255570Strasz	 */
1330263723Strasz	len = strlen(name);
1331255570Strasz	for (i = 0; i < len; i++)
1332263723Strasz		targ->t_name[i] = tolower(targ->t_name[i]);
1333255570Strasz
1334255570Strasz	targ->t_conf = conf;
1335279006Smav	TAILQ_INIT(&targ->t_ports);
1336255570Strasz	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1337255570Strasz
1338255570Strasz	return (targ);
1339255570Strasz}
1340255570Strasz
1341255570Straszvoid
1342255570Strasztarget_delete(struct target *targ)
1343255570Strasz{
1344279006Smav	struct port *port, *tport;
1345255570Strasz
1346279006Smav	TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport)
1347279006Smav		port_delete(port);
1348255570Strasz	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1349255570Strasz
1350263723Strasz	free(targ->t_name);
1351275642Strasz	free(targ->t_redirection);
1352255570Strasz	free(targ);
1353255570Strasz}
1354255570Strasz
1355255570Straszstruct target *
1356263723Strasztarget_find(struct conf *conf, const char *name)
1357255570Strasz{
1358255570Strasz	struct target *targ;
1359255570Strasz
1360255570Strasz	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1361263723Strasz		if (strcasecmp(targ->t_name, name) == 0)
1362255570Strasz			return (targ);
1363255570Strasz	}
1364255570Strasz
1365255570Strasz	return (NULL);
1366255570Strasz}
1367255570Strasz
1368275642Straszint
1369275642Strasztarget_set_redirection(struct target *target, const char *addr)
1370275642Strasz{
1371275642Strasz
1372275642Strasz	if (target->t_redirection != NULL) {
1373275642Strasz		log_warnx("cannot set redirection to \"%s\" for "
1374275642Strasz		    "target \"%s\"; already defined",
1375275642Strasz		    addr, target->t_name);
1376275642Strasz		return (1);
1377275642Strasz	}
1378275642Strasz
1379275642Strasz	target->t_redirection = checked_strdup(addr);
1380275642Strasz
1381275642Strasz	return (0);
1382275642Strasz}
1383275642Strasz
1384255570Straszstruct lun *
1385279002Smavlun_new(struct conf *conf, const char *name)
1386255570Strasz{
1387255570Strasz	struct lun *lun;
1388255570Strasz
1389279002Smav	lun = lun_find(conf, name);
1390255570Strasz	if (lun != NULL) {
1391279002Smav		log_warnx("duplicated lun \"%s\"", name);
1392255570Strasz		return (NULL);
1393255570Strasz	}
1394255570Strasz
1395255570Strasz	lun = calloc(1, sizeof(*lun));
1396255570Strasz	if (lun == NULL)
1397255570Strasz		log_err(1, "calloc");
1398279002Smav	lun->l_conf = conf;
1399279002Smav	lun->l_name = checked_strdup(name);
1400255570Strasz	TAILQ_INIT(&lun->l_options);
1401279002Smav	TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1402288760Smav	lun->l_ctl_lun = -1;
1403255570Strasz
1404255570Strasz	return (lun);
1405255570Strasz}
1406255570Strasz
1407255570Straszvoid
1408255570Straszlun_delete(struct lun *lun)
1409255570Strasz{
1410279002Smav	struct target *targ;
1411291387Smav	struct option *o, *tmp;
1412279002Smav	int i;
1413255570Strasz
1414279002Smav	TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1415279002Smav		for (i = 0; i < MAX_LUNS; i++) {
1416279002Smav			if (targ->t_luns[i] == lun)
1417279002Smav				targ->t_luns[i] = NULL;
1418279002Smav		}
1419279002Smav	}
1420279002Smav	TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1421255570Strasz
1422291387Smav	TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp)
1423291387Smav		option_delete(&lun->l_options, o);
1424279002Smav	free(lun->l_name);
1425255570Strasz	free(lun->l_backend);
1426255570Strasz	free(lun->l_device_id);
1427255570Strasz	free(lun->l_path);
1428279002Smav	free(lun->l_scsiname);
1429255570Strasz	free(lun->l_serial);
1430255570Strasz	free(lun);
1431255570Strasz}
1432255570Strasz
1433255570Straszstruct lun *
1434279002Smavlun_find(const struct conf *conf, const char *name)
1435255570Strasz{
1436255570Strasz	struct lun *lun;
1437255570Strasz
1438279002Smav	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1439279002Smav		if (strcmp(lun->l_name, name) == 0)
1440255570Strasz			return (lun);
1441255570Strasz	}
1442255570Strasz
1443255570Strasz	return (NULL);
1444255570Strasz}
1445255570Strasz
1446255570Straszvoid
1447255570Straszlun_set_backend(struct lun *lun, const char *value)
1448255570Strasz{
1449255570Strasz	free(lun->l_backend);
1450255570Strasz	lun->l_backend = checked_strdup(value);
1451255570Strasz}
1452255570Strasz
1453255570Straszvoid
1454255570Straszlun_set_blocksize(struct lun *lun, size_t value)
1455255570Strasz{
1456255570Strasz
1457255570Strasz	lun->l_blocksize = value;
1458255570Strasz}
1459255570Strasz
1460255570Straszvoid
1461288810Smavlun_set_device_type(struct lun *lun, uint8_t value)
1462288810Smav{
1463288810Smav
1464288810Smav	lun->l_device_type = value;
1465288810Smav}
1466288810Smav
1467288810Smavvoid
1468255570Straszlun_set_device_id(struct lun *lun, const char *value)
1469255570Strasz{
1470255570Strasz	free(lun->l_device_id);
1471255570Strasz	lun->l_device_id = checked_strdup(value);
1472255570Strasz}
1473255570Strasz
1474255570Straszvoid
1475255570Straszlun_set_path(struct lun *lun, const char *value)
1476255570Strasz{
1477255570Strasz	free(lun->l_path);
1478255570Strasz	lun->l_path = checked_strdup(value);
1479255570Strasz}
1480255570Strasz
1481255570Straszvoid
1482279002Smavlun_set_scsiname(struct lun *lun, const char *value)
1483279002Smav{
1484279002Smav	free(lun->l_scsiname);
1485279002Smav	lun->l_scsiname = checked_strdup(value);
1486279002Smav}
1487279002Smav
1488279002Smavvoid
1489255570Straszlun_set_serial(struct lun *lun, const char *value)
1490255570Strasz{
1491255570Strasz	free(lun->l_serial);
1492255570Strasz	lun->l_serial = checked_strdup(value);
1493255570Strasz}
1494255570Strasz
1495255570Straszvoid
1496255570Straszlun_set_size(struct lun *lun, size_t value)
1497255570Strasz{
1498255570Strasz
1499255570Strasz	lun->l_size = value;
1500255570Strasz}
1501255570Strasz
1502255570Straszvoid
1503255570Straszlun_set_ctl_lun(struct lun *lun, uint32_t value)
1504255570Strasz{
1505255570Strasz
1506255570Strasz	lun->l_ctl_lun = value;
1507255570Strasz}
1508255570Strasz
1509291387Smavstruct option *
1510291387Smavoption_new(struct options *options, const char *name, const char *value)
1511255570Strasz{
1512291387Smav	struct option *o;
1513255570Strasz
1514291387Smav	o = option_find(options, name);
1515291387Smav	if (o != NULL) {
1516291387Smav		log_warnx("duplicated option \"%s\"", name);
1517255570Strasz		return (NULL);
1518255570Strasz	}
1519255570Strasz
1520291387Smav	o = calloc(1, sizeof(*o));
1521291387Smav	if (o == NULL)
1522255570Strasz		log_err(1, "calloc");
1523291387Smav	o->o_name = checked_strdup(name);
1524291387Smav	o->o_value = checked_strdup(value);
1525291387Smav	TAILQ_INSERT_TAIL(options, o, o_next);
1526255570Strasz
1527291387Smav	return (o);
1528255570Strasz}
1529255570Strasz
1530255570Straszvoid
1531291387Smavoption_delete(struct options *options, struct option *o)
1532255570Strasz{
1533255570Strasz
1534291387Smav	TAILQ_REMOVE(options, o, o_next);
1535291387Smav	free(o->o_name);
1536291387Smav	free(o->o_value);
1537291387Smav	free(o);
1538255570Strasz}
1539255570Strasz
1540291387Smavstruct option *
1541291387Smavoption_find(const struct options *options, const char *name)
1542255570Strasz{
1543291387Smav	struct option *o;
1544255570Strasz
1545291387Smav	TAILQ_FOREACH(o, options, o_next) {
1546291387Smav		if (strcmp(o->o_name, name) == 0)
1547291387Smav			return (o);
1548255570Strasz	}
1549255570Strasz
1550255570Strasz	return (NULL);
1551255570Strasz}
1552255570Strasz
1553255570Straszvoid
1554291387Smavoption_set(struct option *o, const char *value)
1555255570Strasz{
1556255570Strasz
1557291387Smav	free(o->o_value);
1558291387Smav	o->o_value = checked_strdup(value);
1559255570Strasz}
1560255570Strasz
1561255570Straszstatic struct connection *
1562270137Smavconnection_new(struct portal *portal, int fd, const char *host,
1563270137Smav    const struct sockaddr *client_sa)
1564255570Strasz{
1565255570Strasz	struct connection *conn;
1566255570Strasz
1567255570Strasz	conn = calloc(1, sizeof(*conn));
1568255570Strasz	if (conn == NULL)
1569255570Strasz		log_err(1, "calloc");
1570255570Strasz	conn->conn_portal = portal;
1571255570Strasz	conn->conn_socket = fd;
1572255570Strasz	conn->conn_initiator_addr = checked_strdup(host);
1573270137Smav	memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1574255570Strasz
1575255570Strasz	/*
1576255570Strasz	 * Default values, from RFC 3720, section 12.
1577255570Strasz	 */
1578255570Strasz	conn->conn_max_data_segment_length = 8192;
1579255570Strasz	conn->conn_max_burst_length = 262144;
1580255570Strasz	conn->conn_immediate_data = true;
1581255570Strasz
1582255570Strasz	return (conn);
1583255570Strasz}
1584255570Strasz
1585255570Strasz#if 0
1586255570Straszstatic void
1587255570Straszconf_print(struct conf *conf)
1588255570Strasz{
1589255570Strasz	struct auth_group *ag;
1590255570Strasz	struct auth *auth;
1591263720Strasz	struct auth_name *auth_name;
1592263720Strasz	struct auth_portal *auth_portal;
1593255570Strasz	struct portal_group *pg;
1594255570Strasz	struct portal *portal;
1595255570Strasz	struct target *targ;
1596255570Strasz	struct lun *lun;
1597291387Smav	struct option *o;
1598255570Strasz
1599255570Strasz	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1600255570Strasz		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1601255570Strasz		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1602255570Strasz			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1603255570Strasz			    auth->a_user, auth->a_secret,
1604255570Strasz			    auth->a_mutual_user, auth->a_mutual_secret);
1605263720Strasz		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1606263720Strasz			fprintf(stderr, "\t initiator-name %s\n",
1607263720Strasz			    auth_name->an_initator_name);
1608263720Strasz		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1609263720Strasz			fprintf(stderr, "\t initiator-portal %s\n",
1610263720Strasz			    auth_portal->an_initator_portal);
1611255570Strasz		fprintf(stderr, "}\n");
1612255570Strasz	}
1613255570Strasz	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1614255570Strasz		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1615255570Strasz		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1616255570Strasz			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1617255570Strasz		fprintf(stderr, "}\n");
1618255570Strasz	}
1619279002Smav	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1620279002Smav		fprintf(stderr, "\tlun %s {\n", lun->l_name);
1621279002Smav		fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1622291387Smav		TAILQ_FOREACH(o, &lun->l_options, o_next)
1623279002Smav			fprintf(stderr, "\t\toption %s %s\n",
1624291387Smav			    lo->o_name, lo->o_value);
1625279002Smav		fprintf(stderr, "\t}\n");
1626279002Smav	}
1627255570Strasz	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1628263723Strasz		fprintf(stderr, "target %s {\n", targ->t_name);
1629255570Strasz		if (targ->t_alias != NULL)
1630255570Strasz			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1631255570Strasz		fprintf(stderr, "}\n");
1632255570Strasz	}
1633255570Strasz}
1634255570Strasz#endif
1635255570Strasz
1636263717Straszstatic int
1637263717Straszconf_verify_lun(struct lun *lun)
1638263717Strasz{
1639263717Strasz	const struct lun *lun2;
1640263717Strasz
1641263717Strasz	if (lun->l_backend == NULL)
1642263717Strasz		lun_set_backend(lun, "block");
1643263717Strasz	if (strcmp(lun->l_backend, "block") == 0) {
1644263717Strasz		if (lun->l_path == NULL) {
1645279002Smav			log_warnx("missing path for lun \"%s\"",
1646279002Smav			    lun->l_name);
1647263717Strasz			return (1);
1648263717Strasz		}
1649263717Strasz	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1650263717Strasz		if (lun->l_size == 0) {
1651279002Smav			log_warnx("missing size for ramdisk-backed lun \"%s\"",
1652279002Smav			    lun->l_name);
1653263717Strasz			return (1);
1654263717Strasz		}
1655263717Strasz		if (lun->l_path != NULL) {
1656263717Strasz			log_warnx("path must not be specified "
1657279002Smav			    "for ramdisk-backed lun \"%s\"",
1658279002Smav			    lun->l_name);
1659263717Strasz			return (1);
1660263717Strasz		}
1661263717Strasz	}
1662263717Strasz	if (lun->l_blocksize == 0) {
1663288823Smav		if (lun->l_device_type == 5)
1664288823Smav			lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE);
1665288823Smav		else
1666288823Smav			lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1667263717Strasz	} else if (lun->l_blocksize < 0) {
1668279002Smav		log_warnx("invalid blocksize for lun \"%s\"; "
1669279002Smav		    "must be larger than 0", lun->l_name);
1670263717Strasz		return (1);
1671263717Strasz	}
1672263717Strasz	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1673279002Smav		log_warnx("invalid size for lun \"%s\"; "
1674279002Smav		    "must be multiple of blocksize", lun->l_name);
1675263717Strasz		return (1);
1676263717Strasz	}
1677279002Smav	TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1678279002Smav		if (lun == lun2)
1679279002Smav			continue;
1680279002Smav		if (lun->l_path != NULL && lun2->l_path != NULL &&
1681279002Smav		    strcmp(lun->l_path, lun2->l_path) == 0) {
1682279002Smav			log_debugx("WARNING: path \"%s\" duplicated "
1683279002Smav			    "between lun \"%s\", and "
1684279002Smav			    "lun \"%s\"", lun->l_path,
1685279002Smav			    lun->l_name, lun2->l_name);
1686263718Strasz		}
1687263718Strasz	}
1688263717Strasz
1689263717Strasz	return (0);
1690263717Strasz}
1691263717Strasz
1692255570Straszint
1693255570Straszconf_verify(struct conf *conf)
1694255570Strasz{
1695255570Strasz	struct auth_group *ag;
1696255570Strasz	struct portal_group *pg;
1697279006Smav	struct port *port;
1698255570Strasz	struct target *targ;
1699263717Strasz	struct lun *lun;
1700274871Strasz	bool found;
1701279002Smav	int error, i;
1702255570Strasz
1703255570Strasz	if (conf->conf_pidfile_path == NULL)
1704255570Strasz		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1705255570Strasz
1706279002Smav	TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1707279002Smav		error = conf_verify_lun(lun);
1708279002Smav		if (error != 0)
1709279002Smav			return (error);
1710279002Smav	}
1711255570Strasz	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1712255570Strasz		if (targ->t_auth_group == NULL) {
1713263726Strasz			targ->t_auth_group = auth_group_find(conf,
1714263726Strasz			    "default");
1715263726Strasz			assert(targ->t_auth_group != NULL);
1716255570Strasz		}
1717279006Smav		if (TAILQ_EMPTY(&targ->t_ports)) {
1718279006Smav			pg = portal_group_find(conf, "default");
1719279006Smav			assert(pg != NULL);
1720279006Smav			port_new(conf, targ, pg);
1721255570Strasz		}
1722274871Strasz		found = false;
1723279002Smav		for (i = 0; i < MAX_LUNS; i++) {
1724279002Smav			if (targ->t_luns[i] != NULL)
1725279002Smav				found = true;
1726255570Strasz		}
1727275642Strasz		if (!found && targ->t_redirection == NULL) {
1728265506Strasz			log_warnx("no LUNs defined for target \"%s\"",
1729265506Strasz			    targ->t_name);
1730255570Strasz		}
1731275642Strasz		if (found && targ->t_redirection != NULL) {
1732275642Strasz			log_debugx("target \"%s\" contains luns, "
1733275642Strasz			    " but configured for redirection",
1734275642Strasz			    targ->t_name);
1735275642Strasz		}
1736255570Strasz	}
1737255570Strasz	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1738255570Strasz		assert(pg->pg_name != NULL);
1739255570Strasz		if (pg->pg_discovery_auth_group == NULL) {
1740255570Strasz			pg->pg_discovery_auth_group =
1741263728Strasz			    auth_group_find(conf, "default");
1742255570Strasz			assert(pg->pg_discovery_auth_group != NULL);
1743255570Strasz		}
1744255570Strasz
1745275244Strasz		if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1746275244Strasz			pg->pg_discovery_filter = PG_FILTER_NONE;
1747275244Strasz
1748288729Smav		if (pg->pg_redirection != NULL) {
1749288729Smav			if (!TAILQ_EMPTY(&pg->pg_ports)) {
1750275642Strasz				log_debugx("portal-group \"%s\" assigned "
1751279006Smav				    "to target, but configured "
1752275642Strasz				    "for redirection",
1753279006Smav				    pg->pg_name);
1754275642Strasz			}
1755275642Strasz			pg->pg_unassigned = false;
1756288729Smav		} else if (!TAILQ_EMPTY(&pg->pg_ports)) {
1757288729Smav			pg->pg_unassigned = false;
1758275642Strasz		} else {
1759255570Strasz			if (strcmp(pg->pg_name, "default") != 0)
1760255570Strasz				log_warnx("portal-group \"%s\" not assigned "
1761255570Strasz				    "to any target", pg->pg_name);
1762255570Strasz			pg->pg_unassigned = true;
1763275642Strasz		}
1764255570Strasz	}
1765255570Strasz	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1766255570Strasz		if (ag->ag_name == NULL)
1767255570Strasz			assert(ag->ag_target != NULL);
1768255570Strasz		else
1769255570Strasz			assert(ag->ag_target == NULL);
1770255570Strasz
1771274871Strasz		found = false;
1772255570Strasz		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1773274871Strasz			if (targ->t_auth_group == ag) {
1774274871Strasz				found = true;
1775255570Strasz				break;
1776274871Strasz			}
1777255570Strasz		}
1778279006Smav		TAILQ_FOREACH(port, &conf->conf_ports, p_next) {
1779279006Smav			if (port->p_auth_group == ag) {
1780279006Smav				found = true;
1781279006Smav				break;
1782279006Smav			}
1783279006Smav		}
1784274871Strasz		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1785274871Strasz			if (pg->pg_discovery_auth_group == ag) {
1786274871Strasz				found = true;
1787274871Strasz				break;
1788274871Strasz			}
1789274871Strasz		}
1790274871Strasz		if (!found && ag->ag_name != NULL &&
1791263728Strasz		    strcmp(ag->ag_name, "default") != 0 &&
1792255570Strasz		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1793255570Strasz		    strcmp(ag->ag_name, "no-access") != 0) {
1794255570Strasz			log_warnx("auth-group \"%s\" not assigned "
1795255570Strasz			    "to any target", ag->ag_name);
1796255570Strasz		}
1797255570Strasz	}
1798255570Strasz
1799255570Strasz	return (0);
1800255570Strasz}
1801255570Strasz
1802255570Straszstatic int
1803255570Straszconf_apply(struct conf *oldconf, struct conf *newconf)
1804255570Strasz{
1805255570Strasz	struct lun *oldlun, *newlun, *tmplun;
1806255570Strasz	struct portal_group *oldpg, *newpg;
1807255570Strasz	struct portal *oldp, *newp;
1808279006Smav	struct port *oldport, *newport, *tmpport;
1809274939Smav	struct isns *oldns, *newns;
1810255570Strasz	pid_t otherpid;
1811279001Smav	int changed, cumulated_error = 0, error, sockbuf;
1812255570Strasz	int one = 1;
1813255570Strasz
1814255570Strasz	if (oldconf->conf_debug != newconf->conf_debug) {
1815255570Strasz		log_debugx("changing debug level to %d", newconf->conf_debug);
1816255570Strasz		log_init(newconf->conf_debug);
1817255570Strasz	}
1818255570Strasz
1819255570Strasz	if (oldconf->conf_pidfh != NULL) {
1820255570Strasz		assert(oldconf->conf_pidfile_path != NULL);
1821255570Strasz		if (newconf->conf_pidfile_path != NULL &&
1822255570Strasz		    strcmp(oldconf->conf_pidfile_path,
1823255570Strasz		    newconf->conf_pidfile_path) == 0) {
1824255570Strasz			newconf->conf_pidfh = oldconf->conf_pidfh;
1825255570Strasz			oldconf->conf_pidfh = NULL;
1826255570Strasz		} else {
1827255570Strasz			log_debugx("removing pidfile %s",
1828255570Strasz			    oldconf->conf_pidfile_path);
1829255570Strasz			pidfile_remove(oldconf->conf_pidfh);
1830255570Strasz			oldconf->conf_pidfh = NULL;
1831255570Strasz		}
1832255570Strasz	}
1833255570Strasz
1834255570Strasz	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1835255570Strasz		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1836255570Strasz		newconf->conf_pidfh =
1837255570Strasz		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1838255570Strasz		if (newconf->conf_pidfh == NULL) {
1839255570Strasz			if (errno == EEXIST)
1840255570Strasz				log_errx(1, "daemon already running, pid: %jd.",
1841255570Strasz				    (intmax_t)otherpid);
1842255570Strasz			log_err(1, "cannot open or create pidfile \"%s\"",
1843255570Strasz			    newconf->conf_pidfile_path);
1844255570Strasz		}
1845255570Strasz	}
1846255570Strasz
1847279003Smav	/*
1848279003Smav	 * Go through the new portal groups, assigning tags or preserving old.
1849279003Smav	 */
1850279003Smav	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1851288729Smav		if (newpg->pg_tag != 0)
1852288729Smav			continue;
1853279003Smav		oldpg = portal_group_find(oldconf, newpg->pg_name);
1854279003Smav		if (oldpg != NULL)
1855279003Smav			newpg->pg_tag = oldpg->pg_tag;
1856279003Smav		else
1857279003Smav			newpg->pg_tag = ++last_portal_group_tag;
1858279003Smav	}
1859279003Smav
1860274939Smav	/* Deregister on removed iSNS servers. */
1861274939Smav	TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1862274939Smav		TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1863274939Smav			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1864274939Smav				break;
1865274939Smav		}
1866274939Smav		if (newns == NULL)
1867274939Smav			isns_deregister(oldns);
1868274939Smav	}
1869274939Smav
1870265518Strasz	/*
1871265518Strasz	 * XXX: If target or lun removal fails, we should somehow "move"
1872274870Strasz	 *      the old lun or target into newconf, so that subsequent
1873274870Strasz	 *      conf_apply() would try to remove them again.  That would
1874274870Strasz	 *      be somewhat hairy, though, and lun deletion failures don't
1875274870Strasz	 *      really happen, so leave it as it is for now.
1876265518Strasz	 */
1877279002Smav	/*
1878279006Smav	 * First, remove any ports present in the old configuration
1879279002Smav	 * and missing in the new one.
1880279002Smav	 */
1881279006Smav	TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) {
1882317352Smav		if (port_is_dummy(oldport))
1883288729Smav			continue;
1884279006Smav		newport = port_find(newconf, oldport->p_name);
1885317352Smav		if (newport != NULL && !port_is_dummy(newport))
1886279002Smav			continue;
1887279055Smav		log_debugx("removing port \"%s\"", oldport->p_name);
1888279006Smav		error = kernel_port_remove(oldport);
1889279002Smav		if (error != 0) {
1890279006Smav			log_warnx("failed to remove port %s",
1891279006Smav			    oldport->p_name);
1892279002Smav			/*
1893279002Smav			 * XXX: Uncomment after fixing the root cause.
1894279002Smav			 *
1895279002Smav			 * cumulated_error++;
1896279002Smav			 */
1897279002Smav		}
1898279002Smav	}
1899279002Smav
1900279002Smav	/*
1901279002Smav	 * Second, remove any LUNs present in the old configuration
1902279002Smav	 * and missing in the new one.
1903279002Smav	 */
1904279002Smav	TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1905279002Smav		newlun = lun_find(newconf, oldlun->l_name);
1906279002Smav		if (newlun == NULL) {
1907279002Smav			log_debugx("lun \"%s\", CTL lun %d "
1908279002Smav			    "not found in new configuration; "
1909279002Smav			    "removing", oldlun->l_name, oldlun->l_ctl_lun);
1910279002Smav			error = kernel_lun_remove(oldlun);
1911279000Smav			if (error != 0) {
1912279002Smav				log_warnx("failed to remove lun \"%s\", "
1913279002Smav				    "CTL lun %d",
1914279002Smav				    oldlun->l_name, oldlun->l_ctl_lun);
1915279002Smav				cumulated_error++;
1916279000Smav			}
1917255570Strasz			continue;
1918255570Strasz		}
1919255570Strasz
1920255570Strasz		/*
1921279002Smav		 * Also remove the LUNs changed by more than size.
1922255570Strasz		 */
1923279002Smav		changed = 0;
1924279002Smav		assert(oldlun->l_backend != NULL);
1925279002Smav		assert(newlun->l_backend != NULL);
1926279002Smav		if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1927279002Smav			log_debugx("backend for lun \"%s\", "
1928279002Smav			    "CTL lun %d changed; removing",
1929279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1930279002Smav			changed = 1;
1931279002Smav		}
1932279002Smav		if (oldlun->l_blocksize != newlun->l_blocksize) {
1933279002Smav			log_debugx("blocksize for lun \"%s\", "
1934279002Smav			    "CTL lun %d changed; removing",
1935279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1936279002Smav			changed = 1;
1937279002Smav		}
1938279002Smav		if (newlun->l_device_id != NULL &&
1939279002Smav		    (oldlun->l_device_id == NULL ||
1940279002Smav		     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1941279002Smav		     0)) {
1942279002Smav			log_debugx("device-id for lun \"%s\", "
1943279002Smav			    "CTL lun %d changed; removing",
1944279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1945279002Smav			changed = 1;
1946279002Smav		}
1947279002Smav		if (newlun->l_path != NULL &&
1948279002Smav		    (oldlun->l_path == NULL ||
1949279002Smav		     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1950279002Smav			log_debugx("path for lun \"%s\", "
1951279002Smav			    "CTL lun %d, changed; removing",
1952279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1953279002Smav			changed = 1;
1954279002Smav		}
1955279002Smav		if (newlun->l_serial != NULL &&
1956279002Smav		    (oldlun->l_serial == NULL ||
1957279002Smav		     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1958279002Smav			log_debugx("serial for lun \"%s\", "
1959279002Smav			    "CTL lun %d changed; removing",
1960279002Smav			    oldlun->l_name, oldlun->l_ctl_lun);
1961279002Smav			changed = 1;
1962279002Smav		}
1963279002Smav		if (changed) {
1964279002Smav			error = kernel_lun_remove(oldlun);
1965279002Smav			if (error != 0) {
1966279002Smav				log_warnx("failed to remove lun \"%s\", "
1967279002Smav				    "CTL lun %d",
1968279002Smav				    oldlun->l_name, oldlun->l_ctl_lun);
1969279002Smav				cumulated_error++;
1970255570Strasz			}
1971279002Smav			lun_delete(oldlun);
1972279002Smav			continue;
1973279002Smav		}
1974255570Strasz
1975279002Smav		lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1976279002Smav	}
1977279002Smav
1978279002Smav	TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1979279002Smav		oldlun = lun_find(oldconf, newlun->l_name);
1980279002Smav		if (oldlun != NULL) {
1981288728Smav			log_debugx("modifying lun \"%s\", CTL lun %d",
1982288728Smav			    newlun->l_name, newlun->l_ctl_lun);
1983288728Smav			error = kernel_lun_modify(newlun);
1984288728Smav			if (error != 0) {
1985288728Smav				log_warnx("failed to "
1986288728Smav				    "modify lun \"%s\", CTL lun %d",
1987279002Smav				    newlun->l_name, newlun->l_ctl_lun);
1988288728Smav				cumulated_error++;
1989255570Strasz			}
1990279002Smav			continue;
1991255570Strasz		}
1992279002Smav		log_debugx("adding lun \"%s\"", newlun->l_name);
1993279002Smav		error = kernel_lun_add(newlun);
1994279002Smav		if (error != 0) {
1995279002Smav			log_warnx("failed to add lun \"%s\"", newlun->l_name);
1996279002Smav			lun_delete(newlun);
1997279002Smav			cumulated_error++;
1998279002Smav		}
1999255570Strasz	}
2000255570Strasz
2001255570Strasz	/*
2002279006Smav	 * Now add new ports or modify existing ones.
2003255570Strasz	 */
2004279006Smav	TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) {
2005317352Smav		if (port_is_dummy(newport))
2006288729Smav			continue;
2007279006Smav		oldport = port_find(oldconf, newport->p_name);
2008255570Strasz
2009317352Smav		if (oldport == NULL || port_is_dummy(oldport)) {
2010279055Smav			log_debugx("adding port \"%s\"", newport->p_name);
2011279006Smav			error = kernel_port_add(newport);
2012279006Smav		} else {
2013279055Smav			log_debugx("updating port \"%s\"", newport->p_name);
2014279006Smav			newport->p_ctl_port = oldport->p_ctl_port;
2015288748Smav			error = kernel_port_update(newport, oldport);
2016277749Strasz		}
2017279002Smav		if (error != 0) {
2018279006Smav			log_warnx("failed to %s port %s",
2019279006Smav			    (oldport == NULL) ? "add" : "update",
2020279006Smav			    newport->p_name);
2021279002Smav			/*
2022279002Smav			 * XXX: Uncomment after fixing the root cause.
2023279002Smav			 *
2024279002Smav			 * cumulated_error++;
2025279002Smav			 */
2026279002Smav		}
2027255570Strasz	}
2028255570Strasz
2029255570Strasz	/*
2030293290Sbdrewery	 * Go through the new portals, opening the sockets as necessary.
2031255570Strasz	 */
2032255570Strasz	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
2033288729Smav		if (newpg->pg_foreign)
2034288729Smav			continue;
2035255570Strasz		if (newpg->pg_unassigned) {
2036255570Strasz			log_debugx("not listening on portal-group \"%s\", "
2037255570Strasz			    "not assigned to any target",
2038255570Strasz			    newpg->pg_name);
2039255570Strasz			continue;
2040255570Strasz		}
2041255570Strasz		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
2042255570Strasz			/*
2043255570Strasz			 * Try to find already open portal and reuse
2044255570Strasz			 * the listening socket.  We don't care about
2045255570Strasz			 * what portal or portal group that was, what
2046255570Strasz			 * matters is the listening address.
2047255570Strasz			 */
2048255570Strasz			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
2049255570Strasz			    pg_next) {
2050255570Strasz				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
2051255570Strasz				    p_next) {
2052255570Strasz					if (strcmp(newp->p_listen,
2053255570Strasz					    oldp->p_listen) == 0 &&
2054255570Strasz					    oldp->p_socket > 0) {
2055255570Strasz						newp->p_socket =
2056255570Strasz						    oldp->p_socket;
2057255570Strasz						oldp->p_socket = 0;
2058255570Strasz						break;
2059255570Strasz					}
2060255570Strasz				}
2061255570Strasz			}
2062255570Strasz			if (newp->p_socket > 0) {
2063255570Strasz				/*
2064255570Strasz				 * We're done with this portal.
2065255570Strasz				 */
2066255570Strasz				continue;
2067255570Strasz			}
2068255570Strasz
2069255570Strasz#ifdef ICL_KERNEL_PROXY
2070265507Strasz			if (proxy_mode) {
2071265509Strasz				newpg->pg_conf->conf_portal_id++;
2072265509Strasz				newp->p_id = newpg->pg_conf->conf_portal_id;
2073265509Strasz				log_debugx("listening on %s, portal-group "
2074265509Strasz				    "\"%s\", portal id %d, using ICL proxy",
2075265509Strasz				    newp->p_listen, newpg->pg_name, newp->p_id);
2076265509Strasz				kernel_listen(newp->p_ai, newp->p_iser,
2077265509Strasz				    newp->p_id);
2078265507Strasz				continue;
2079265507Strasz			}
2080265507Strasz#endif
2081265507Strasz			assert(proxy_mode == false);
2082255570Strasz			assert(newp->p_iser == false);
2083255570Strasz
2084255570Strasz			log_debugx("listening on %s, portal-group \"%s\"",
2085255570Strasz			    newp->p_listen, newpg->pg_name);
2086255570Strasz			newp->p_socket = socket(newp->p_ai->ai_family,
2087255570Strasz			    newp->p_ai->ai_socktype,
2088255570Strasz			    newp->p_ai->ai_protocol);
2089255570Strasz			if (newp->p_socket < 0) {
2090255570Strasz				log_warn("socket(2) failed for %s",
2091255570Strasz				    newp->p_listen);
2092255570Strasz				cumulated_error++;
2093255570Strasz				continue;
2094255570Strasz			}
2095279001Smav			sockbuf = SOCKBUF_SIZE;
2096279001Smav			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
2097279001Smav			    &sockbuf, sizeof(sockbuf)) == -1)
2098279001Smav				log_warn("setsockopt(SO_RCVBUF) failed "
2099279001Smav				    "for %s", newp->p_listen);
2100279001Smav			sockbuf = SOCKBUF_SIZE;
2101279001Smav			if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
2102279001Smav			    &sockbuf, sizeof(sockbuf)) == -1)
2103279001Smav				log_warn("setsockopt(SO_SNDBUF) failed "
2104279001Smav				    "for %s", newp->p_listen);
2105255570Strasz			error = setsockopt(newp->p_socket, SOL_SOCKET,
2106255570Strasz			    SO_REUSEADDR, &one, sizeof(one));
2107255570Strasz			if (error != 0) {
2108255570Strasz				log_warn("setsockopt(SO_REUSEADDR) failed "
2109255570Strasz				    "for %s", newp->p_listen);
2110255570Strasz				close(newp->p_socket);
2111255570Strasz				newp->p_socket = 0;
2112255570Strasz				cumulated_error++;
2113255570Strasz				continue;
2114255570Strasz			}
2115255570Strasz			error = bind(newp->p_socket, newp->p_ai->ai_addr,
2116255570Strasz			    newp->p_ai->ai_addrlen);
2117255570Strasz			if (error != 0) {
2118255570Strasz				log_warn("bind(2) failed for %s",
2119255570Strasz				    newp->p_listen);
2120255570Strasz				close(newp->p_socket);
2121255570Strasz				newp->p_socket = 0;
2122255570Strasz				cumulated_error++;
2123255570Strasz				continue;
2124255570Strasz			}
2125255570Strasz			error = listen(newp->p_socket, -1);
2126255570Strasz			if (error != 0) {
2127255570Strasz				log_warn("listen(2) failed for %s",
2128255570Strasz				    newp->p_listen);
2129255570Strasz				close(newp->p_socket);
2130255570Strasz				newp->p_socket = 0;
2131255570Strasz				cumulated_error++;
2132255570Strasz				continue;
2133255570Strasz			}
2134255570Strasz		}
2135255570Strasz	}
2136255570Strasz
2137255570Strasz	/*
2138255570Strasz	 * Go through the no longer used sockets, closing them.
2139255570Strasz	 */
2140255570Strasz	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
2141255570Strasz		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
2142255570Strasz			if (oldp->p_socket <= 0)
2143255570Strasz				continue;
2144255570Strasz			log_debugx("closing socket for %s, portal-group \"%s\"",
2145255570Strasz			    oldp->p_listen, oldpg->pg_name);
2146255570Strasz			close(oldp->p_socket);
2147255570Strasz			oldp->p_socket = 0;
2148255570Strasz		}
2149255570Strasz	}
2150255570Strasz
2151274939Smav	/* (Re-)Register on remaining/new iSNS servers. */
2152274939Smav	TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
2153274939Smav		TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
2154274939Smav			if (strcmp(oldns->i_addr, newns->i_addr) == 0)
2155274939Smav				break;
2156274939Smav		}
2157274939Smav		isns_register(newns, oldns);
2158274939Smav	}
2159274939Smav
2160274939Smav	/* Schedule iSNS update */
2161274939Smav	if (!TAILQ_EMPTY(&newconf->conf_isns))
2162274939Smav		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2163274939Smav
2164255570Strasz	return (cumulated_error);
2165255570Strasz}
2166255570Strasz
2167255570Straszbool
2168255570Strasztimed_out(void)
2169255570Strasz{
2170255570Strasz
2171255570Strasz	return (sigalrm_received);
2172255570Strasz}
2173255570Strasz
2174255570Straszstatic void
2175274939Smavsigalrm_handler_fatal(int dummy __unused)
2176255570Strasz{
2177255570Strasz	/*
2178255570Strasz	 * It would be easiest to just log an error and exit.  We can't
2179255570Strasz	 * do this, though, because log_errx() is not signal safe, since
2180255570Strasz	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
2181255570Strasz	 * and pdu_receive(), to call log_errx() there.  Should they fail
2182255570Strasz	 * to notice, we'll exit here one second later.
2183255570Strasz	 */
2184255570Strasz	if (sigalrm_received) {
2185255570Strasz		/*
2186255570Strasz		 * Oh well.  Just give up and quit.
2187255570Strasz		 */
2188255570Strasz		_exit(2);
2189255570Strasz	}
2190255570Strasz
2191255570Strasz	sigalrm_received = true;
2192255570Strasz}
2193255570Strasz
2194255570Straszstatic void
2195274939Smavsigalrm_handler(int dummy __unused)
2196255570Strasz{
2197274939Smav
2198274939Smav	sigalrm_received = true;
2199274939Smav}
2200274939Smav
2201274939Smavvoid
2202274939Smavset_timeout(int timeout, int fatal)
2203274939Smav{
2204255570Strasz	struct sigaction sa;
2205255570Strasz	struct itimerval itv;
2206255570Strasz	int error;
2207255570Strasz
2208274939Smav	if (timeout <= 0) {
2209255570Strasz		log_debugx("session timeout disabled");
2210274939Smav		bzero(&itv, sizeof(itv));
2211274939Smav		error = setitimer(ITIMER_REAL, &itv, NULL);
2212274939Smav		if (error != 0)
2213274939Smav			log_err(1, "setitimer");
2214274939Smav		sigalrm_received = false;
2215255570Strasz		return;
2216255570Strasz	}
2217255570Strasz
2218274939Smav	sigalrm_received = false;
2219255570Strasz	bzero(&sa, sizeof(sa));
2220274939Smav	if (fatal)
2221274939Smav		sa.sa_handler = sigalrm_handler_fatal;
2222274939Smav	else
2223274939Smav		sa.sa_handler = sigalrm_handler;
2224255570Strasz	sigfillset(&sa.sa_mask);
2225255570Strasz	error = sigaction(SIGALRM, &sa, NULL);
2226255570Strasz	if (error != 0)
2227255570Strasz		log_err(1, "sigaction");
2228255570Strasz
2229255570Strasz	/*
2230255570Strasz	 * First SIGALRM will arive after conf_timeout seconds.
2231255570Strasz	 * If we do nothing, another one will arrive a second later.
2232255570Strasz	 */
2233274939Smav	log_debugx("setting session timeout to %d seconds", timeout);
2234255570Strasz	bzero(&itv, sizeof(itv));
2235255570Strasz	itv.it_interval.tv_sec = 1;
2236274939Smav	itv.it_value.tv_sec = timeout;
2237255570Strasz	error = setitimer(ITIMER_REAL, &itv, NULL);
2238255570Strasz	if (error != 0)
2239255570Strasz		log_err(1, "setitimer");
2240255570Strasz}
2241255570Strasz
2242255570Straszstatic int
2243255570Straszwait_for_children(bool block)
2244255570Strasz{
2245255570Strasz	pid_t pid;
2246255570Strasz	int status;
2247255570Strasz	int num = 0;
2248255570Strasz
2249255570Strasz	for (;;) {
2250255570Strasz		/*
2251255570Strasz		 * If "block" is true, wait for at least one process.
2252255570Strasz		 */
2253255570Strasz		if (block && num == 0)
2254255570Strasz			pid = wait4(-1, &status, 0, NULL);
2255255570Strasz		else
2256255570Strasz			pid = wait4(-1, &status, WNOHANG, NULL);
2257255570Strasz		if (pid <= 0)
2258255570Strasz			break;
2259255570Strasz		if (WIFSIGNALED(status)) {
2260255570Strasz			log_warnx("child process %d terminated with signal %d",
2261255570Strasz			    pid, WTERMSIG(status));
2262255570Strasz		} else if (WEXITSTATUS(status) != 0) {
2263255570Strasz			log_warnx("child process %d terminated with exit status %d",
2264255570Strasz			    pid, WEXITSTATUS(status));
2265255570Strasz		} else {
2266255570Strasz			log_debugx("child process %d terminated gracefully", pid);
2267255570Strasz		}
2268255570Strasz		num++;
2269255570Strasz	}
2270255570Strasz
2271255570Strasz	return (num);
2272255570Strasz}
2273255570Strasz
2274255570Straszstatic void
2275265513Straszhandle_connection(struct portal *portal, int fd,
2276270137Smav    const struct sockaddr *client_sa, bool dont_fork)
2277255570Strasz{
2278255570Strasz	struct connection *conn;
2279255570Strasz	int error;
2280255570Strasz	pid_t pid;
2281255570Strasz	char host[NI_MAXHOST + 1];
2282255570Strasz	struct conf *conf;
2283255570Strasz
2284255570Strasz	conf = portal->p_portal_group->pg_conf;
2285255570Strasz
2286255570Strasz	if (dont_fork) {
2287255570Strasz		log_debugx("incoming connection; not forking due to -d flag");
2288255570Strasz	} else {
2289255570Strasz		nchildren -= wait_for_children(false);
2290255570Strasz		assert(nchildren >= 0);
2291255570Strasz
2292255570Strasz		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2293255570Strasz			log_debugx("maxproc limit of %d child processes hit; "
2294255570Strasz			    "waiting for child process to exit", conf->conf_maxproc);
2295255570Strasz			nchildren -= wait_for_children(true);
2296255570Strasz			assert(nchildren >= 0);
2297255570Strasz		}
2298255570Strasz		log_debugx("incoming connection; forking child process #%d",
2299255570Strasz		    nchildren);
2300255570Strasz		nchildren++;
2301255570Strasz		pid = fork();
2302255570Strasz		if (pid < 0)
2303255570Strasz			log_err(1, "fork");
2304255570Strasz		if (pid > 0) {
2305255570Strasz			close(fd);
2306255570Strasz			return;
2307255570Strasz		}
2308255570Strasz	}
2309255570Strasz	pidfile_close(conf->conf_pidfh);
2310255570Strasz
2311270137Smav	error = getnameinfo(client_sa, client_sa->sa_len,
2312265513Strasz	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2313265513Strasz	if (error != 0)
2314265513Strasz		log_errx(1, "getnameinfo: %s", gai_strerror(error));
2315255570Strasz
2316265513Strasz	log_debugx("accepted connection from %s; portal group \"%s\"",
2317265513Strasz	    host, portal->p_portal_group->pg_name);
2318265513Strasz	log_set_peer_addr(host);
2319265513Strasz	setproctitle("%s", host);
2320255570Strasz
2321270137Smav	conn = connection_new(portal, fd, host, client_sa);
2322274939Smav	set_timeout(conf->conf_timeout, true);
2323255570Strasz	kernel_capsicate();
2324255570Strasz	login(conn);
2325255570Strasz	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2326255570Strasz		kernel_handoff(conn);
2327255570Strasz		log_debugx("connection handed off to the kernel");
2328255570Strasz	} else {
2329255570Strasz		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2330255570Strasz		discovery(conn);
2331255570Strasz	}
2332255570Strasz	log_debugx("nothing more to do; exiting");
2333255570Strasz	exit(0);
2334255570Strasz}
2335255570Strasz
2336255570Straszstatic int
2337255570Straszfd_add(int fd, fd_set *fdset, int nfds)
2338255570Strasz{
2339255570Strasz
2340255570Strasz	/*
2341255570Strasz	 * Skip sockets which we failed to bind.
2342255570Strasz	 */
2343255570Strasz	if (fd <= 0)
2344255570Strasz		return (nfds);
2345255570Strasz
2346255570Strasz	FD_SET(fd, fdset);
2347255570Strasz	if (fd > nfds)
2348255570Strasz		nfds = fd;
2349255570Strasz	return (nfds);
2350255570Strasz}
2351255570Strasz
2352255570Straszstatic void
2353255570Straszmain_loop(struct conf *conf, bool dont_fork)
2354255570Strasz{
2355255570Strasz	struct portal_group *pg;
2356255570Strasz	struct portal *portal;
2357265512Strasz	struct sockaddr_storage client_sa;
2358265512Strasz	socklen_t client_salen;
2359255570Strasz#ifdef ICL_KERNEL_PROXY
2360255570Strasz	int connection_id;
2361265509Strasz	int portal_id;
2362265507Strasz#endif
2363255570Strasz	fd_set fdset;
2364255570Strasz	int error, nfds, client_fd;
2365255570Strasz
2366255570Strasz	pidfile_write(conf->conf_pidfh);
2367255570Strasz
2368255570Strasz	for (;;) {
2369274939Smav		if (sighup_received || sigterm_received || timed_out())
2370255570Strasz			return;
2371255570Strasz
2372255570Strasz#ifdef ICL_KERNEL_PROXY
2373265507Strasz		if (proxy_mode) {
2374265513Strasz			client_salen = sizeof(client_sa);
2375265513Strasz			kernel_accept(&connection_id, &portal_id,
2376265513Strasz			    (struct sockaddr *)&client_sa, &client_salen);
2377271627Strasz			assert(client_salen >= client_sa.ss_len);
2378255570Strasz
2379265509Strasz			log_debugx("incoming connection, id %d, portal id %d",
2380265509Strasz			    connection_id, portal_id);
2381265509Strasz			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2382265509Strasz				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2383265509Strasz					if (portal->p_id == portal_id) {
2384265509Strasz						goto found;
2385265509Strasz					}
2386265509Strasz				}
2387265509Strasz			}
2388255570Strasz
2389265509Strasz			log_errx(1, "kernel returned invalid portal_id %d",
2390265509Strasz			    portal_id);
2391265509Strasz
2392265509Straszfound:
2393265513Strasz			handle_connection(portal, connection_id,
2394270137Smav			    (struct sockaddr *)&client_sa, dont_fork);
2395265507Strasz		} else {
2396265507Strasz#endif
2397265507Strasz			assert(proxy_mode == false);
2398265507Strasz
2399265507Strasz			FD_ZERO(&fdset);
2400265507Strasz			nfds = 0;
2401265507Strasz			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2402265507Strasz				TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2403265507Strasz					nfds = fd_add(portal->p_socket, &fdset, nfds);
2404255570Strasz			}
2405265507Strasz			error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2406265507Strasz			if (error <= 0) {
2407265507Strasz				if (errno == EINTR)
2408265507Strasz					return;
2409265507Strasz				log_err(1, "select");
2410265507Strasz			}
2411265507Strasz			TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2412265507Strasz				TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2413265507Strasz					if (!FD_ISSET(portal->p_socket, &fdset))
2414265507Strasz						continue;
2415265512Strasz					client_salen = sizeof(client_sa);
2416265512Strasz					client_fd = accept(portal->p_socket,
2417265512Strasz					    (struct sockaddr *)&client_sa,
2418265512Strasz					    &client_salen);
2419281488Smav					if (client_fd < 0) {
2420281488Smav						if (errno == ECONNABORTED)
2421281488Smav							continue;
2422265507Strasz						log_err(1, "accept");
2423281488Smav					}
2424271627Strasz					assert(client_salen >= client_sa.ss_len);
2425271627Strasz
2426265512Strasz					handle_connection(portal, client_fd,
2427265513Strasz					    (struct sockaddr *)&client_sa,
2428270137Smav					    dont_fork);
2429265507Strasz					break;
2430265507Strasz				}
2431265507Strasz			}
2432265507Strasz#ifdef ICL_KERNEL_PROXY
2433255570Strasz		}
2434265507Strasz#endif
2435255570Strasz	}
2436255570Strasz}
2437255570Strasz
2438255570Straszstatic void
2439255570Straszsighup_handler(int dummy __unused)
2440255570Strasz{
2441255570Strasz
2442255570Strasz	sighup_received = true;
2443255570Strasz}
2444255570Strasz
2445255570Straszstatic void
2446255570Straszsigterm_handler(int dummy __unused)
2447255570Strasz{
2448255570Strasz
2449255570Strasz	sigterm_received = true;
2450255570Strasz}
2451255570Strasz
2452255570Straszstatic void
2453263730Straszsigchld_handler(int dummy __unused)
2454263730Strasz{
2455263730Strasz
2456263730Strasz	/*
2457263730Strasz	 * The only purpose of this handler is to make SIGCHLD
2458263730Strasz	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2459263730Strasz	 * wait_for_children().
2460263730Strasz	 */
2461263730Strasz}
2462263730Strasz
2463263730Straszstatic void
2464255570Straszregister_signals(void)
2465255570Strasz{
2466255570Strasz	struct sigaction sa;
2467255570Strasz	int error;
2468255570Strasz
2469255570Strasz	bzero(&sa, sizeof(sa));
2470255570Strasz	sa.sa_handler = sighup_handler;
2471255570Strasz	sigfillset(&sa.sa_mask);
2472255570Strasz	error = sigaction(SIGHUP, &sa, NULL);
2473255570Strasz	if (error != 0)
2474255570Strasz		log_err(1, "sigaction");
2475255570Strasz
2476255570Strasz	sa.sa_handler = sigterm_handler;
2477255570Strasz	error = sigaction(SIGTERM, &sa, NULL);
2478255570Strasz	if (error != 0)
2479255570Strasz		log_err(1, "sigaction");
2480255570Strasz
2481255570Strasz	sa.sa_handler = sigterm_handler;
2482255570Strasz	error = sigaction(SIGINT, &sa, NULL);
2483255570Strasz	if (error != 0)
2484255570Strasz		log_err(1, "sigaction");
2485263730Strasz
2486263730Strasz	sa.sa_handler = sigchld_handler;
2487263730Strasz	error = sigaction(SIGCHLD, &sa, NULL);
2488263730Strasz	if (error != 0)
2489263730Strasz		log_err(1, "sigaction");
2490255570Strasz}
2491255570Strasz
2492255570Straszint
2493255570Straszmain(int argc, char **argv)
2494255570Strasz{
2495255570Strasz	struct conf *oldconf, *newconf, *tmpconf;
2496274939Smav	struct isns *newns;
2497255570Strasz	const char *config_path = DEFAULT_CONFIG_PATH;
2498255570Strasz	int debug = 0, ch, error;
2499255570Strasz	bool dont_daemonize = false;
2500255570Strasz
2501265507Strasz	while ((ch = getopt(argc, argv, "df:R")) != -1) {
2502255570Strasz		switch (ch) {
2503255570Strasz		case 'd':
2504255570Strasz			dont_daemonize = true;
2505255570Strasz			debug++;
2506255570Strasz			break;
2507255570Strasz		case 'f':
2508255570Strasz			config_path = optarg;
2509255570Strasz			break;
2510265507Strasz		case 'R':
2511265507Strasz#ifndef ICL_KERNEL_PROXY
2512265507Strasz			log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2513265507Strasz			    "does not support iSER protocol");
2514265507Strasz#endif
2515265507Strasz			proxy_mode = true;
2516265507Strasz			break;
2517255570Strasz		case '?':
2518255570Strasz		default:
2519255570Strasz			usage();
2520255570Strasz		}
2521255570Strasz	}
2522255570Strasz	argc -= optind;
2523255570Strasz	if (argc != 0)
2524255570Strasz		usage();
2525255570Strasz
2526255570Strasz	log_init(debug);
2527255570Strasz	kernel_init();
2528255570Strasz
2529255570Strasz	oldconf = conf_new_from_kernel();
2530279055Smav	newconf = conf_new_from_file(config_path, oldconf);
2531255570Strasz	if (newconf == NULL)
2532265516Strasz		log_errx(1, "configuration error; exiting");
2533255570Strasz	if (debug > 0) {
2534255570Strasz		oldconf->conf_debug = debug;
2535255570Strasz		newconf->conf_debug = debug;
2536255570Strasz	}
2537255570Strasz
2538255570Strasz	error = conf_apply(oldconf, newconf);
2539255570Strasz	if (error != 0)
2540265516Strasz		log_errx(1, "failed to apply configuration; exiting");
2541265516Strasz
2542255570Strasz	conf_delete(oldconf);
2543255570Strasz	oldconf = NULL;
2544255570Strasz
2545255570Strasz	register_signals();
2546255570Strasz
2547263719Strasz	if (dont_daemonize == false) {
2548263719Strasz		log_debugx("daemonizing");
2549263719Strasz		if (daemon(0, 0) == -1) {
2550263719Strasz			log_warn("cannot daemonize");
2551263719Strasz			pidfile_remove(newconf->conf_pidfh);
2552263719Strasz			exit(1);
2553263719Strasz		}
2554263719Strasz	}
2555263719Strasz
2556274939Smav	/* Schedule iSNS update */
2557274939Smav	if (!TAILQ_EMPTY(&newconf->conf_isns))
2558274939Smav		set_timeout((newconf->conf_isns_period + 2) / 3, false);
2559274939Smav
2560255570Strasz	for (;;) {
2561255570Strasz		main_loop(newconf, dont_daemonize);
2562255570Strasz		if (sighup_received) {
2563255570Strasz			sighup_received = false;
2564255570Strasz			log_debugx("received SIGHUP, reloading configuration");
2565279055Smav			tmpconf = conf_new_from_file(config_path, newconf);
2566255570Strasz			if (tmpconf == NULL) {
2567255570Strasz				log_warnx("configuration error, "
2568255570Strasz				    "continuing with old configuration");
2569255570Strasz			} else {
2570255570Strasz				if (debug > 0)
2571255570Strasz					tmpconf->conf_debug = debug;
2572255570Strasz				oldconf = newconf;
2573255570Strasz				newconf = tmpconf;
2574255570Strasz				error = conf_apply(oldconf, newconf);
2575255570Strasz				if (error != 0)
2576255570Strasz					log_warnx("failed to reload "
2577255570Strasz					    "configuration");
2578255570Strasz				conf_delete(oldconf);
2579255570Strasz				oldconf = NULL;
2580255570Strasz			}
2581255570Strasz		} else if (sigterm_received) {
2582255570Strasz			log_debugx("exiting on signal; "
2583255570Strasz			    "reloading empty configuration");
2584255570Strasz
2585279003Smav			log_debugx("removing CTL iSCSI ports "
2586255570Strasz			    "and terminating all connections");
2587255570Strasz
2588255570Strasz			oldconf = newconf;
2589255570Strasz			newconf = conf_new();
2590255570Strasz			if (debug > 0)
2591255570Strasz				newconf->conf_debug = debug;
2592255570Strasz			error = conf_apply(oldconf, newconf);
2593255570Strasz			if (error != 0)
2594255570Strasz				log_warnx("failed to apply configuration");
2595274939Smav			conf_delete(oldconf);
2596274939Smav			oldconf = NULL;
2597255570Strasz
2598255570Strasz			log_warnx("exiting on signal");
2599255570Strasz			exit(0);
2600255570Strasz		} else {
2601255570Strasz			nchildren -= wait_for_children(false);
2602255570Strasz			assert(nchildren >= 0);
2603274939Smav			if (timed_out()) {
2604274939Smav				set_timeout(0, false);
2605274939Smav				TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2606274939Smav					isns_check(newns);
2607274939Smav				/* Schedule iSNS update */
2608274939Smav				if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2609274939Smav					set_timeout((newconf->conf_isns_period
2610274939Smav					    + 2) / 3,
2611274939Smav					    false);
2612274939Smav				}
2613274939Smav			}
2614255570Strasz		}
2615255570Strasz	}
2616255570Strasz	/* NOTREACHED */
2617255570Strasz}
2618