Deleted Added
sdiff udiff text old ( 279003 ) new ( 279006 )
full compact
1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/10/usr.sbin/ctld/ctld.c 279003 2015-02-19 14:33:46Z mav $");
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/socket.h>
37#include <sys/wait.h>
38#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <assert.h>
41#include <ctype.h>
42#include <errno.h>
43#include <netdb.h>
44#include <signal.h>
45#include <stdbool.h>
46#include <stdio.h>
47#include <stdint.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51
52#include "ctld.h"
53#include "isns.h"
54
55bool proxy_mode = false;
56
57static volatile bool sighup_received = false;
58static volatile bool sigterm_received = false;
59static volatile bool sigalrm_received = false;
60
61static int nchildren = 0;
62static uint16_t last_portal_group_tag = 0;
63
64static void
65usage(void)
66{
67
68 fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
69 exit(1);
70}
71
72char *
73checked_strdup(const char *s)
74{
75 char *c;
76
77 c = strdup(s);
78 if (c == NULL)
79 log_err(1, "strdup");
80 return (c);
81}
82
83struct conf *
84conf_new(void)
85{
86 struct conf *conf;
87
88 conf = calloc(1, sizeof(*conf));
89 if (conf == NULL)
90 log_err(1, "calloc");
91 TAILQ_INIT(&conf->conf_luns);
92 TAILQ_INIT(&conf->conf_targets);
93 TAILQ_INIT(&conf->conf_auth_groups);
94 TAILQ_INIT(&conf->conf_portal_groups);
95 TAILQ_INIT(&conf->conf_isns);
96
97 conf->conf_isns_period = 900;
98 conf->conf_isns_timeout = 5;
99 conf->conf_debug = 0;
100 conf->conf_timeout = 60;
101 conf->conf_maxproc = 30;
102
103 return (conf);
104}
105
106void
107conf_delete(struct conf *conf)
108{
109 struct lun *lun, *ltmp;
110 struct target *targ, *tmp;
111 struct auth_group *ag, *cagtmp;
112 struct portal_group *pg, *cpgtmp;
113 struct isns *is, *istmp;
114
115 assert(conf->conf_pidfh == NULL);
116
117 TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
118 lun_delete(lun);
119 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
120 target_delete(targ);
121 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
122 auth_group_delete(ag);
123 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
124 portal_group_delete(pg);
125 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
126 isns_delete(is);
127 free(conf->conf_pidfile_path);
128 free(conf);
129}
130
131static struct auth *
132auth_new(struct auth_group *ag)
133{
134 struct auth *auth;
135
136 auth = calloc(1, sizeof(*auth));
137 if (auth == NULL)
138 log_err(1, "calloc");
139 auth->a_auth_group = ag;
140 TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
141 return (auth);
142}
143
144static void
145auth_delete(struct auth *auth)
146{
147 TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
148
149 free(auth->a_user);
150 free(auth->a_secret);
151 free(auth->a_mutual_user);
152 free(auth->a_mutual_secret);
153 free(auth);
154}
155
156const struct auth *
157auth_find(const struct auth_group *ag, const char *user)
158{
159 const struct auth *auth;
160
161 TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
162 if (strcmp(auth->a_user, user) == 0)
163 return (auth);
164 }
165
166 return (NULL);
167}
168
169static void
170auth_check_secret_length(struct auth *auth)
171{
172 size_t len;
173
174 len = strlen(auth->a_secret);
175 if (len > 16) {
176 if (auth->a_auth_group->ag_name != NULL)
177 log_warnx("secret for user \"%s\", auth-group \"%s\", "
178 "is too long; it should be at most 16 characters "
179 "long", auth->a_user, auth->a_auth_group->ag_name);
180 else
181 log_warnx("secret for user \"%s\", target \"%s\", "
182 "is too long; it should be at most 16 characters "
183 "long", auth->a_user,
184 auth->a_auth_group->ag_target->t_name);
185 }
186 if (len < 12) {
187 if (auth->a_auth_group->ag_name != NULL)
188 log_warnx("secret for user \"%s\", auth-group \"%s\", "
189 "is too short; it should be at least 12 characters "
190 "long", auth->a_user,
191 auth->a_auth_group->ag_name);
192 else
193 log_warnx("secret for user \"%s\", target \"%s\", "
194 "is too short; it should be at least 16 characters "
195 "long", auth->a_user,
196 auth->a_auth_group->ag_target->t_name);
197 }
198
199 if (auth->a_mutual_secret != NULL) {
200 len = strlen(auth->a_secret);
201 if (len > 16) {
202 if (auth->a_auth_group->ag_name != NULL)
203 log_warnx("mutual secret for user \"%s\", "
204 "auth-group \"%s\", is too long; it should "
205 "be at most 16 characters long",
206 auth->a_user, auth->a_auth_group->ag_name);
207 else
208 log_warnx("mutual secret for user \"%s\", "
209 "target \"%s\", is too long; it should "
210 "be at most 16 characters long",
211 auth->a_user,
212 auth->a_auth_group->ag_target->t_name);
213 }
214 if (len < 12) {
215 if (auth->a_auth_group->ag_name != NULL)
216 log_warnx("mutual secret for user \"%s\", "
217 "auth-group \"%s\", is too short; it "
218 "should be at least 12 characters long",
219 auth->a_user, auth->a_auth_group->ag_name);
220 else
221 log_warnx("mutual secret for user \"%s\", "
222 "target \"%s\", is too short; it should be "
223 "at least 16 characters long",
224 auth->a_user,
225 auth->a_auth_group->ag_target->t_name);
226 }
227 }
228}
229
230const struct auth *
231auth_new_chap(struct auth_group *ag, const char *user,
232 const char *secret)
233{
234 struct auth *auth;
235
236 if (ag->ag_type == AG_TYPE_UNKNOWN)
237 ag->ag_type = AG_TYPE_CHAP;
238 if (ag->ag_type != AG_TYPE_CHAP) {
239 if (ag->ag_name != NULL)
240 log_warnx("cannot mix \"chap\" authentication with "
241 "other types for auth-group \"%s\"", ag->ag_name);
242 else
243 log_warnx("cannot mix \"chap\" authentication with "
244 "other types for target \"%s\"",
245 ag->ag_target->t_name);
246 return (NULL);
247 }
248
249 auth = auth_new(ag);
250 auth->a_user = checked_strdup(user);
251 auth->a_secret = checked_strdup(secret);
252
253 auth_check_secret_length(auth);
254
255 return (auth);
256}
257
258const struct auth *
259auth_new_chap_mutual(struct auth_group *ag, const char *user,
260 const char *secret, const char *user2, const char *secret2)
261{
262 struct auth *auth;
263
264 if (ag->ag_type == AG_TYPE_UNKNOWN)
265 ag->ag_type = AG_TYPE_CHAP_MUTUAL;
266 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
267 if (ag->ag_name != NULL)
268 log_warnx("cannot mix \"chap-mutual\" authentication "
269 "with other types for auth-group \"%s\"",
270 ag->ag_name);
271 else
272 log_warnx("cannot mix \"chap-mutual\" authentication "
273 "with other types for target \"%s\"",
274 ag->ag_target->t_name);
275 return (NULL);
276 }
277
278 auth = auth_new(ag);
279 auth->a_user = checked_strdup(user);
280 auth->a_secret = checked_strdup(secret);
281 auth->a_mutual_user = checked_strdup(user2);
282 auth->a_mutual_secret = checked_strdup(secret2);
283
284 auth_check_secret_length(auth);
285
286 return (auth);
287}
288
289const struct auth_name *
290auth_name_new(struct auth_group *ag, const char *name)
291{
292 struct auth_name *an;
293
294 an = calloc(1, sizeof(*an));
295 if (an == NULL)
296 log_err(1, "calloc");
297 an->an_auth_group = ag;
298 an->an_initator_name = checked_strdup(name);
299 TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
300 return (an);
301}
302
303static void
304auth_name_delete(struct auth_name *an)
305{
306 TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
307
308 free(an->an_initator_name);
309 free(an);
310}
311
312bool
313auth_name_defined(const struct auth_group *ag)
314{
315 if (TAILQ_EMPTY(&ag->ag_names))
316 return (false);
317 return (true);
318}
319
320const struct auth_name *
321auth_name_find(const struct auth_group *ag, const char *name)
322{
323 const struct auth_name *auth_name;
324
325 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
326 if (strcmp(auth_name->an_initator_name, name) == 0)
327 return (auth_name);
328 }
329
330 return (NULL);
331}
332
333int
334auth_name_check(const struct auth_group *ag, const char *initiator_name)
335{
336 if (!auth_name_defined(ag))
337 return (0);
338
339 if (auth_name_find(ag, initiator_name) == NULL)
340 return (1);
341
342 return (0);
343}
344
345const struct auth_portal *
346auth_portal_new(struct auth_group *ag, const char *portal)
347{
348 struct auth_portal *ap;
349 char *net, *mask, *str, *tmp;
350 int len, dm, m;
351
352 ap = calloc(1, sizeof(*ap));
353 if (ap == NULL)
354 log_err(1, "calloc");
355 ap->ap_auth_group = ag;
356 ap->ap_initator_portal = checked_strdup(portal);
357 mask = str = checked_strdup(portal);
358 net = strsep(&mask, "/");
359 if (net[0] == '[')
360 net++;
361 len = strlen(net);
362 if (len == 0)
363 goto error;
364 if (net[len - 1] == ']')
365 net[len - 1] = 0;
366 if (strchr(net, ':') != NULL) {
367 struct sockaddr_in6 *sin6 =
368 (struct sockaddr_in6 *)&ap->ap_sa;
369
370 sin6->sin6_len = sizeof(*sin6);
371 sin6->sin6_family = AF_INET6;
372 if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
373 goto error;
374 dm = 128;
375 } else {
376 struct sockaddr_in *sin =
377 (struct sockaddr_in *)&ap->ap_sa;
378
379 sin->sin_len = sizeof(*sin);
380 sin->sin_family = AF_INET;
381 if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
382 goto error;
383 dm = 32;
384 }
385 if (mask != NULL) {
386 m = strtol(mask, &tmp, 0);
387 if (m < 0 || m > dm || tmp[0] != 0)
388 goto error;
389 } else
390 m = dm;
391 ap->ap_mask = m;
392 free(str);
393 TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
394 return (ap);
395
396error:
397 log_errx(1, "Incorrect initiator portal '%s'", portal);
398 return (NULL);
399}
400
401static void
402auth_portal_delete(struct auth_portal *ap)
403{
404 TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
405
406 free(ap->ap_initator_portal);
407 free(ap);
408}
409
410bool
411auth_portal_defined(const struct auth_group *ag)
412{
413 if (TAILQ_EMPTY(&ag->ag_portals))
414 return (false);
415 return (true);
416}
417
418const struct auth_portal *
419auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
420{
421 const struct auth_portal *ap;
422 const uint8_t *a, *b;
423 int i;
424 uint8_t bmask;
425
426 TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
427 if (ap->ap_sa.ss_family != ss->ss_family)
428 continue;
429 if (ss->ss_family == AF_INET) {
430 a = (const uint8_t *)
431 &((const struct sockaddr_in *)ss)->sin_addr;
432 b = (const uint8_t *)
433 &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
434 } else {
435 a = (const uint8_t *)
436 &((const struct sockaddr_in6 *)ss)->sin6_addr;
437 b = (const uint8_t *)
438 &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
439 }
440 for (i = 0; i < ap->ap_mask / 8; i++) {
441 if (a[i] != b[i])
442 goto next;
443 }
444 if (ap->ap_mask % 8) {
445 bmask = 0xff << (8 - (ap->ap_mask % 8));
446 if ((a[i] & bmask) != (b[i] & bmask))
447 goto next;
448 }
449 return (ap);
450next:
451 ;
452 }
453
454 return (NULL);
455}
456
457int
458auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
459{
460
461 if (!auth_portal_defined(ag))
462 return (0);
463
464 if (auth_portal_find(ag, sa) == NULL)
465 return (1);
466
467 return (0);
468}
469
470struct auth_group *
471auth_group_new(struct conf *conf, const char *name)
472{
473 struct auth_group *ag;
474
475 if (name != NULL) {
476 ag = auth_group_find(conf, name);
477 if (ag != NULL) {
478 log_warnx("duplicated auth-group \"%s\"", name);
479 return (NULL);
480 }
481 }
482
483 ag = calloc(1, sizeof(*ag));
484 if (ag == NULL)
485 log_err(1, "calloc");
486 if (name != NULL)
487 ag->ag_name = checked_strdup(name);
488 TAILQ_INIT(&ag->ag_auths);
489 TAILQ_INIT(&ag->ag_names);
490 TAILQ_INIT(&ag->ag_portals);
491 ag->ag_conf = conf;
492 TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
493
494 return (ag);
495}
496
497void
498auth_group_delete(struct auth_group *ag)
499{
500 struct auth *auth, *auth_tmp;
501 struct auth_name *auth_name, *auth_name_tmp;
502 struct auth_portal *auth_portal, *auth_portal_tmp;
503
504 TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
505
506 TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
507 auth_delete(auth);
508 TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
509 auth_name_delete(auth_name);
510 TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
511 auth_portal_tmp)
512 auth_portal_delete(auth_portal);
513 free(ag->ag_name);
514 free(ag);
515}
516
517struct auth_group *
518auth_group_find(const struct conf *conf, const char *name)
519{
520 struct auth_group *ag;
521
522 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
523 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
524 return (ag);
525 }
526
527 return (NULL);
528}
529
530int
531auth_group_set_type(struct auth_group *ag, const char *str)
532{
533 int type;
534
535 if (strcmp(str, "none") == 0) {
536 type = AG_TYPE_NO_AUTHENTICATION;
537 } else if (strcmp(str, "deny") == 0) {
538 type = AG_TYPE_DENY;
539 } else if (strcmp(str, "chap") == 0) {
540 type = AG_TYPE_CHAP;
541 } else if (strcmp(str, "chap-mutual") == 0) {
542 type = AG_TYPE_CHAP_MUTUAL;
543 } else {
544 if (ag->ag_name != NULL)
545 log_warnx("invalid auth-type \"%s\" for auth-group "
546 "\"%s\"", str, ag->ag_name);
547 else
548 log_warnx("invalid auth-type \"%s\" for target "
549 "\"%s\"", str, ag->ag_target->t_name);
550 return (1);
551 }
552
553 if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
554 if (ag->ag_name != NULL) {
555 log_warnx("cannot set auth-type to \"%s\" for "
556 "auth-group \"%s\"; already has a different "
557 "type", str, ag->ag_name);
558 } else {
559 log_warnx("cannot set auth-type to \"%s\" for target "
560 "\"%s\"; already has a different type",
561 str, ag->ag_target->t_name);
562 }
563 return (1);
564 }
565
566 ag->ag_type = type;
567
568 return (0);
569}
570
571static struct portal *
572portal_new(struct portal_group *pg)
573{
574 struct portal *portal;
575
576 portal = calloc(1, sizeof(*portal));
577 if (portal == NULL)
578 log_err(1, "calloc");
579 TAILQ_INIT(&portal->p_targets);
580 portal->p_portal_group = pg;
581 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
582 return (portal);
583}
584
585static void
586portal_delete(struct portal *portal)
587{
588
589 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
590 if (portal->p_ai != NULL)
591 freeaddrinfo(portal->p_ai);
592 free(portal->p_listen);
593 free(portal);
594}
595
596struct portal_group *
597portal_group_new(struct conf *conf, const char *name)
598{
599 struct portal_group *pg;
600
601 pg = portal_group_find(conf, name);
602 if (pg != NULL) {
603 log_warnx("duplicated portal-group \"%s\"", name);
604 return (NULL);
605 }
606
607 pg = calloc(1, sizeof(*pg));
608 if (pg == NULL)
609 log_err(1, "calloc");
610 pg->pg_name = checked_strdup(name);
611 TAILQ_INIT(&pg->pg_portals);
612 pg->pg_conf = conf;
613 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
614 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
615
616 return (pg);
617}
618
619void
620portal_group_delete(struct portal_group *pg)
621{
622 struct portal *portal, *tmp;
623
624 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
625
626 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
627 portal_delete(portal);
628 free(pg->pg_name);
629 free(pg->pg_redirection);
630 free(pg);
631}
632
633struct portal_group *
634portal_group_find(const struct conf *conf, const char *name)
635{
636 struct portal_group *pg;
637
638 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
639 if (strcmp(pg->pg_name, name) == 0)
640 return (pg);
641 }
642
643 return (NULL);
644}
645
646static int
647parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
648{
649 struct addrinfo hints;
650 char *str, *addr, *ch;
651 const char *port;
652 int error, colons = 0;
653
654 str = arg = strdup(arg);
655 if (arg[0] == '[') {
656 /*
657 * IPv6 address in square brackets, perhaps with port.
658 */
659 arg++;
660 addr = strsep(&arg, "]");
661 if (arg == NULL)
662 return (1);
663 if (arg[0] == '\0') {
664 port = def_port;
665 } else if (arg[0] == ':') {
666 port = arg + 1;
667 } else {
668 free(str);
669 return (1);
670 }
671 } else {
672 /*
673 * Either IPv6 address without brackets - and without
674 * a port - or IPv4 address. Just count the colons.
675 */
676 for (ch = arg; *ch != '\0'; ch++) {
677 if (*ch == ':')
678 colons++;
679 }
680 if (colons > 1) {
681 addr = arg;
682 port = def_port;
683 } else {
684 addr = strsep(&arg, ":");
685 if (arg == NULL)
686 port = def_port;
687 else
688 port = arg;
689 }
690 }
691
692 memset(&hints, 0, sizeof(hints));
693 hints.ai_family = PF_UNSPEC;
694 hints.ai_socktype = SOCK_STREAM;
695 hints.ai_flags = AI_PASSIVE;
696 error = getaddrinfo(addr, port, &hints, ai);
697 free(str);
698 return ((error != 0) ? 1 : 0);
699}
700
701int
702portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
703{
704 struct portal *portal;
705
706 portal = portal_new(pg);
707 portal->p_listen = checked_strdup(value);
708 portal->p_iser = iser;
709
710 if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
711 log_warnx("invalid listen address %s", portal->p_listen);
712 portal_delete(portal);
713 return (1);
714 }
715
716 /*
717 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
718 * those into multiple portals.
719 */
720
721 return (0);
722}
723
724int
725isns_new(struct conf *conf, const char *addr)
726{
727 struct isns *isns;
728
729 isns = calloc(1, sizeof(*isns));
730 if (isns == NULL)
731 log_err(1, "calloc");
732 isns->i_conf = conf;
733 TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
734 isns->i_addr = checked_strdup(addr);
735
736 if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
737 log_warnx("invalid iSNS address %s", isns->i_addr);
738 isns_delete(isns);
739 return (1);
740 }
741
742 /*
743 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
744 * those into multiple servers.
745 */
746
747 return (0);
748}
749
750void
751isns_delete(struct isns *isns)
752{
753
754 TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
755 free(isns->i_addr);
756 if (isns->i_ai != NULL)
757 freeaddrinfo(isns->i_ai);
758 free(isns);
759}
760
761static int
762isns_do_connect(struct isns *isns)
763{
764 int s;
765
766 s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
767 isns->i_ai->ai_protocol);
768 if (s < 0) {
769 log_warn("socket(2) failed for %s", isns->i_addr);
770 return (-1);
771 }
772 if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
773 log_warn("connect(2) failed for %s", isns->i_addr);
774 close(s);
775 return (-1);
776 }
777 return(s);
778}
779
780static int
781isns_do_register(struct isns *isns, int s, const char *hostname)
782{
783 struct conf *conf = isns->i_conf;
784 struct target *target;
785 struct portal *portal;
786 struct portal_group *pg;
787 struct isns_req *req;
788 int res = 0;
789 uint32_t error;
790
791 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
792 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
793 isns_req_add_delim(req);
794 isns_req_add_str(req, 1, hostname);
795 isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
796 isns_req_add_32(req, 6, conf->conf_isns_period);
797 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
798 if (pg->pg_unassigned)
799 continue;
800 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
801 isns_req_add_addr(req, 16, portal->p_ai);
802 isns_req_add_port(req, 17, portal->p_ai);
803 }
804 }
805 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
806 isns_req_add_str(req, 32, target->t_name);
807 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
808 if (target->t_alias != NULL)
809 isns_req_add_str(req, 34, target->t_alias);
810 pg = target->t_portal_group;
811 isns_req_add_32(req, 51, pg->pg_tag);
812 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
813 isns_req_add_addr(req, 49, portal->p_ai);
814 isns_req_add_port(req, 50, portal->p_ai);
815 }
816 }
817 res = isns_req_send(s, req);
818 if (res < 0) {
819 log_warn("send(2) failed for %s", isns->i_addr);
820 goto quit;
821 }
822 res = isns_req_receive(s, req);
823 if (res < 0) {
824 log_warn("receive(2) failed for %s", isns->i_addr);
825 goto quit;
826 }
827 error = isns_req_get_status(req);
828 if (error != 0) {
829 log_warnx("iSNS register error %d for %s", error, isns->i_addr);
830 res = -1;
831 }
832quit:
833 isns_req_free(req);
834 return (res);
835}
836
837static int
838isns_do_check(struct isns *isns, int s, const char *hostname)
839{
840 struct conf *conf = isns->i_conf;
841 struct isns_req *req;
842 int res = 0;
843 uint32_t error;
844
845 req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
846 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
847 isns_req_add_str(req, 1, hostname);
848 isns_req_add_delim(req);
849 isns_req_add(req, 2, 0, NULL);
850 res = isns_req_send(s, req);
851 if (res < 0) {
852 log_warn("send(2) failed for %s", isns->i_addr);
853 goto quit;
854 }
855 res = isns_req_receive(s, req);
856 if (res < 0) {
857 log_warn("receive(2) failed for %s", isns->i_addr);
858 goto quit;
859 }
860 error = isns_req_get_status(req);
861 if (error != 0) {
862 log_warnx("iSNS check error %d for %s", error, isns->i_addr);
863 res = -1;
864 }
865quit:
866 isns_req_free(req);
867 return (res);
868}
869
870static int
871isns_do_deregister(struct isns *isns, int s, const char *hostname)
872{
873 struct conf *conf = isns->i_conf;
874 struct isns_req *req;
875 int res = 0;
876 uint32_t error;
877
878 req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
879 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
880 isns_req_add_delim(req);
881 isns_req_add_str(req, 1, hostname);
882 res = isns_req_send(s, req);
883 if (res < 0) {
884 log_warn("send(2) failed for %s", isns->i_addr);
885 goto quit;
886 }
887 res = isns_req_receive(s, req);
888 if (res < 0) {
889 log_warn("receive(2) failed for %s", isns->i_addr);
890 goto quit;
891 }
892 error = isns_req_get_status(req);
893 if (error != 0) {
894 log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
895 res = -1;
896 }
897quit:
898 isns_req_free(req);
899 return (res);
900}
901
902void
903isns_register(struct isns *isns, struct isns *oldisns)
904{
905 struct conf *conf = isns->i_conf;
906 int s;
907 char hostname[256];
908
909 if (TAILQ_EMPTY(&conf->conf_targets) ||
910 TAILQ_EMPTY(&conf->conf_portal_groups))
911 return;
912 set_timeout(conf->conf_isns_timeout, false);
913 s = isns_do_connect(isns);
914 if (s < 0) {
915 set_timeout(0, false);
916 return;
917 }
918 gethostname(hostname, sizeof(hostname));
919
920 if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
921 oldisns = isns;
922 isns_do_deregister(oldisns, s, hostname);
923 isns_do_register(isns, s, hostname);
924 close(s);
925 set_timeout(0, false);
926}
927
928void
929isns_check(struct isns *isns)
930{
931 struct conf *conf = isns->i_conf;
932 int s, res;
933 char hostname[256];
934
935 if (TAILQ_EMPTY(&conf->conf_targets) ||
936 TAILQ_EMPTY(&conf->conf_portal_groups))
937 return;
938 set_timeout(conf->conf_isns_timeout, false);
939 s = isns_do_connect(isns);
940 if (s < 0) {
941 set_timeout(0, false);
942 return;
943 }
944 gethostname(hostname, sizeof(hostname));
945
946 res = isns_do_check(isns, s, hostname);
947 if (res < 0) {
948 isns_do_deregister(isns, s, hostname);
949 isns_do_register(isns, s, hostname);
950 }
951 close(s);
952 set_timeout(0, false);
953}
954
955void
956isns_deregister(struct isns *isns)
957{
958 struct conf *conf = isns->i_conf;
959 int s;
960 char hostname[256];
961
962 if (TAILQ_EMPTY(&conf->conf_targets) ||
963 TAILQ_EMPTY(&conf->conf_portal_groups))
964 return;
965 set_timeout(conf->conf_isns_timeout, false);
966 s = isns_do_connect(isns);
967 if (s < 0)
968 return;
969 gethostname(hostname, sizeof(hostname));
970
971 isns_do_deregister(isns, s, hostname);
972 close(s);
973 set_timeout(0, false);
974}
975
976int
977portal_group_set_filter(struct portal_group *pg, const char *str)
978{
979 int filter;
980
981 if (strcmp(str, "none") == 0) {
982 filter = PG_FILTER_NONE;
983 } else if (strcmp(str, "portal") == 0) {
984 filter = PG_FILTER_PORTAL;
985 } else if (strcmp(str, "portal-name") == 0) {
986 filter = PG_FILTER_PORTAL_NAME;
987 } else if (strcmp(str, "portal-name-auth") == 0) {
988 filter = PG_FILTER_PORTAL_NAME_AUTH;
989 } else {
990 log_warnx("invalid discovery-filter \"%s\" for portal-group "
991 "\"%s\"; valid values are \"none\", \"portal\", "
992 "\"portal-name\", and \"portal-name-auth\"",
993 str, pg->pg_name);
994 return (1);
995 }
996
997 if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN &&
998 pg->pg_discovery_filter != filter) {
999 log_warnx("cannot set discovery-filter to \"%s\" for "
1000 "portal-group \"%s\"; already has a different "
1001 "value", str, pg->pg_name);
1002 return (1);
1003 }
1004
1005 pg->pg_discovery_filter = filter;
1006
1007 return (0);
1008}
1009
1010int
1011portal_group_set_redirection(struct portal_group *pg, const char *addr)
1012{
1013
1014 if (pg->pg_redirection != NULL) {
1015 log_warnx("cannot set redirection to \"%s\" for "
1016 "portal-group \"%s\"; already defined",
1017 addr, pg->pg_name);
1018 return (1);
1019 }
1020
1021 pg->pg_redirection = checked_strdup(addr);
1022
1023 return (0);
1024}
1025
1026static bool
1027valid_hex(const char ch)
1028{
1029 switch (ch) {
1030 case '0':
1031 case '1':
1032 case '2':
1033 case '3':
1034 case '4':
1035 case '5':
1036 case '6':
1037 case '7':
1038 case '8':
1039 case '9':
1040 case 'a':
1041 case 'A':
1042 case 'b':
1043 case 'B':
1044 case 'c':
1045 case 'C':
1046 case 'd':
1047 case 'D':
1048 case 'e':
1049 case 'E':
1050 case 'f':
1051 case 'F':
1052 return (true);
1053 default:
1054 return (false);
1055 }
1056}
1057
1058bool
1059valid_iscsi_name(const char *name)
1060{
1061 int i;
1062
1063 if (strlen(name) >= MAX_NAME_LEN) {
1064 log_warnx("overlong name for target \"%s\"; max length allowed "
1065 "by iSCSI specification is %d characters",
1066 name, MAX_NAME_LEN);
1067 return (false);
1068 }
1069
1070 /*
1071 * In the cases below, we don't return an error, just in case the admin
1072 * was right, and we're wrong.
1073 */
1074 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
1075 for (i = strlen("iqn."); name[i] != '\0'; i++) {
1076 /*
1077 * XXX: We should verify UTF-8 normalisation, as defined
1078 * by 3.2.6.2: iSCSI Name Encoding.
1079 */
1080 if (isalnum(name[i]))
1081 continue;
1082 if (name[i] == '-' || name[i] == '.' || name[i] == ':')
1083 continue;
1084 log_warnx("invalid character \"%c\" in target name "
1085 "\"%s\"; allowed characters are letters, digits, "
1086 "'-', '.', and ':'", name[i], name);
1087 break;
1088 }
1089 /*
1090 * XXX: Check more stuff: valid date and a valid reversed domain.
1091 */
1092 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
1093 if (strlen(name) != strlen("eui.") + 16)
1094 log_warnx("invalid target name \"%s\"; the \"eui.\" "
1095 "should be followed by exactly 16 hexadecimal "
1096 "digits", name);
1097 for (i = strlen("eui."); name[i] != '\0'; i++) {
1098 if (!valid_hex(name[i])) {
1099 log_warnx("invalid character \"%c\" in target "
1100 "name \"%s\"; allowed characters are 1-9 "
1101 "and A-F", name[i], name);
1102 break;
1103 }
1104 }
1105 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
1106 if (strlen(name) > strlen("naa.") + 32)
1107 log_warnx("invalid target name \"%s\"; the \"naa.\" "
1108 "should be followed by at most 32 hexadecimal "
1109 "digits", name);
1110 for (i = strlen("naa."); name[i] != '\0'; i++) {
1111 if (!valid_hex(name[i])) {
1112 log_warnx("invalid character \"%c\" in target "
1113 "name \"%s\"; allowed characters are 1-9 "
1114 "and A-F", name[i], name);
1115 break;
1116 }
1117 }
1118 } else {
1119 log_warnx("invalid target name \"%s\"; should start with "
1120 "either \".iqn\", \"eui.\", or \"naa.\"",
1121 name);
1122 }
1123 return (true);
1124}
1125
1126struct target *
1127target_new(struct conf *conf, const char *name)
1128{
1129 struct target *targ;
1130 int i, len;
1131
1132 targ = target_find(conf, name);
1133 if (targ != NULL) {
1134 log_warnx("duplicated target \"%s\"", name);
1135 return (NULL);
1136 }
1137 if (valid_iscsi_name(name) == false) {
1138 log_warnx("target name \"%s\" is invalid", name);
1139 return (NULL);
1140 }
1141 targ = calloc(1, sizeof(*targ));
1142 if (targ == NULL)
1143 log_err(1, "calloc");
1144 targ->t_name = checked_strdup(name);
1145
1146 /*
1147 * RFC 3722 requires us to normalize the name to lowercase.
1148 */
1149 len = strlen(name);
1150 for (i = 0; i < len; i++)
1151 targ->t_name[i] = tolower(targ->t_name[i]);
1152
1153 targ->t_conf = conf;
1154 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1155
1156 return (targ);
1157}
1158
1159void
1160target_delete(struct target *targ)
1161{
1162
1163 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1164
1165 free(targ->t_name);
1166 free(targ->t_redirection);
1167 free(targ);
1168}
1169
1170struct target *
1171target_find(struct conf *conf, const char *name)
1172{
1173 struct target *targ;
1174
1175 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1176 if (strcasecmp(targ->t_name, name) == 0)
1177 return (targ);
1178 }
1179
1180 return (NULL);
1181}
1182
1183int
1184target_set_redirection(struct target *target, const char *addr)
1185{
1186
1187 if (target->t_redirection != NULL) {
1188 log_warnx("cannot set redirection to \"%s\" for "
1189 "target \"%s\"; already defined",
1190 addr, target->t_name);
1191 return (1);
1192 }
1193
1194 target->t_redirection = checked_strdup(addr);
1195
1196 return (0);
1197}
1198
1199void
1200target_set_ctl_port(struct target *target, uint32_t value)
1201{
1202
1203 target->t_ctl_port = value;
1204}
1205
1206struct lun *
1207lun_new(struct conf *conf, const char *name)
1208{
1209 struct lun *lun;
1210
1211 lun = lun_find(conf, name);
1212 if (lun != NULL) {
1213 log_warnx("duplicated lun \"%s\"", name);
1214 return (NULL);
1215 }
1216
1217 lun = calloc(1, sizeof(*lun));
1218 if (lun == NULL)
1219 log_err(1, "calloc");
1220 lun->l_conf = conf;
1221 lun->l_name = checked_strdup(name);
1222 TAILQ_INIT(&lun->l_options);
1223 TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next);
1224
1225 return (lun);
1226}
1227
1228void
1229lun_delete(struct lun *lun)
1230{
1231 struct target *targ;
1232 struct lun_option *lo, *tmp;
1233 int i;
1234
1235 TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) {
1236 for (i = 0; i < MAX_LUNS; i++) {
1237 if (targ->t_luns[i] == lun)
1238 targ->t_luns[i] = NULL;
1239 }
1240 }
1241 TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next);
1242
1243 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
1244 lun_option_delete(lo);
1245 free(lun->l_name);
1246 free(lun->l_backend);
1247 free(lun->l_device_id);
1248 free(lun->l_path);
1249 free(lun->l_scsiname);
1250 free(lun->l_serial);
1251 free(lun);
1252}
1253
1254struct lun *
1255lun_find(const struct conf *conf, const char *name)
1256{
1257 struct lun *lun;
1258
1259 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1260 if (strcmp(lun->l_name, name) == 0)
1261 return (lun);
1262 }
1263
1264 return (NULL);
1265}
1266
1267void
1268lun_set_backend(struct lun *lun, const char *value)
1269{
1270 free(lun->l_backend);
1271 lun->l_backend = checked_strdup(value);
1272}
1273
1274void
1275lun_set_blocksize(struct lun *lun, size_t value)
1276{
1277
1278 lun->l_blocksize = value;
1279}
1280
1281void
1282lun_set_device_id(struct lun *lun, const char *value)
1283{
1284 free(lun->l_device_id);
1285 lun->l_device_id = checked_strdup(value);
1286}
1287
1288void
1289lun_set_path(struct lun *lun, const char *value)
1290{
1291 free(lun->l_path);
1292 lun->l_path = checked_strdup(value);
1293}
1294
1295void
1296lun_set_scsiname(struct lun *lun, const char *value)
1297{
1298 free(lun->l_scsiname);
1299 lun->l_scsiname = checked_strdup(value);
1300}
1301
1302void
1303lun_set_serial(struct lun *lun, const char *value)
1304{
1305 free(lun->l_serial);
1306 lun->l_serial = checked_strdup(value);
1307}
1308
1309void
1310lun_set_size(struct lun *lun, size_t value)
1311{
1312
1313 lun->l_size = value;
1314}
1315
1316void
1317lun_set_ctl_lun(struct lun *lun, uint32_t value)
1318{
1319
1320 lun->l_ctl_lun = value;
1321}
1322
1323struct lun_option *
1324lun_option_new(struct lun *lun, const char *name, const char *value)
1325{
1326 struct lun_option *lo;
1327
1328 lo = lun_option_find(lun, name);
1329 if (lo != NULL) {
1330 log_warnx("duplicated lun option \"%s\" for lun \"%s\"",
1331 name, lun->l_name);
1332 return (NULL);
1333 }
1334
1335 lo = calloc(1, sizeof(*lo));
1336 if (lo == NULL)
1337 log_err(1, "calloc");
1338 lo->lo_name = checked_strdup(name);
1339 lo->lo_value = checked_strdup(value);
1340 lo->lo_lun = lun;
1341 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
1342
1343 return (lo);
1344}
1345
1346void
1347lun_option_delete(struct lun_option *lo)
1348{
1349
1350 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
1351
1352 free(lo->lo_name);
1353 free(lo->lo_value);
1354 free(lo);
1355}
1356
1357struct lun_option *
1358lun_option_find(const struct lun *lun, const char *name)
1359{
1360 struct lun_option *lo;
1361
1362 TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
1363 if (strcmp(lo->lo_name, name) == 0)
1364 return (lo);
1365 }
1366
1367 return (NULL);
1368}
1369
1370void
1371lun_option_set(struct lun_option *lo, const char *value)
1372{
1373
1374 free(lo->lo_value);
1375 lo->lo_value = checked_strdup(value);
1376}
1377
1378static struct connection *
1379connection_new(struct portal *portal, int fd, const char *host,
1380 const struct sockaddr *client_sa)
1381{
1382 struct connection *conn;
1383
1384 conn = calloc(1, sizeof(*conn));
1385 if (conn == NULL)
1386 log_err(1, "calloc");
1387 conn->conn_portal = portal;
1388 conn->conn_socket = fd;
1389 conn->conn_initiator_addr = checked_strdup(host);
1390 memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len);
1391
1392 /*
1393 * Default values, from RFC 3720, section 12.
1394 */
1395 conn->conn_max_data_segment_length = 8192;
1396 conn->conn_max_burst_length = 262144;
1397 conn->conn_immediate_data = true;
1398
1399 return (conn);
1400}
1401
1402#if 0
1403static void
1404conf_print(struct conf *conf)
1405{
1406 struct auth_group *ag;
1407 struct auth *auth;
1408 struct auth_name *auth_name;
1409 struct auth_portal *auth_portal;
1410 struct portal_group *pg;
1411 struct portal *portal;
1412 struct target *targ;
1413 struct lun *lun;
1414 struct lun_option *lo;
1415
1416 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1417 fprintf(stderr, "auth-group %s {\n", ag->ag_name);
1418 TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
1419 fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
1420 auth->a_user, auth->a_secret,
1421 auth->a_mutual_user, auth->a_mutual_secret);
1422 TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
1423 fprintf(stderr, "\t initiator-name %s\n",
1424 auth_name->an_initator_name);
1425 TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
1426 fprintf(stderr, "\t initiator-portal %s\n",
1427 auth_portal->an_initator_portal);
1428 fprintf(stderr, "}\n");
1429 }
1430 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1431 fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1432 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1433 fprintf(stderr, "\t listen %s\n", portal->p_listen);
1434 fprintf(stderr, "}\n");
1435 }
1436 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1437 fprintf(stderr, "\tlun %s {\n", lun->l_name);
1438 fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1439 TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1440 fprintf(stderr, "\t\toption %s %s\n",
1441 lo->lo_name, lo->lo_value);
1442 fprintf(stderr, "\t}\n");
1443 }
1444 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1445 fprintf(stderr, "target %s {\n", targ->t_name);
1446 if (targ->t_alias != NULL)
1447 fprintf(stderr, "\t alias %s\n", targ->t_alias);
1448 fprintf(stderr, "}\n");
1449 }
1450}
1451#endif
1452
1453static int
1454conf_verify_lun(struct lun *lun)
1455{
1456 const struct lun *lun2;
1457
1458 if (lun->l_backend == NULL)
1459 lun_set_backend(lun, "block");
1460 if (strcmp(lun->l_backend, "block") == 0) {
1461 if (lun->l_path == NULL) {
1462 log_warnx("missing path for lun \"%s\"",
1463 lun->l_name);
1464 return (1);
1465 }
1466 } else if (strcmp(lun->l_backend, "ramdisk") == 0) {
1467 if (lun->l_size == 0) {
1468 log_warnx("missing size for ramdisk-backed lun \"%s\"",
1469 lun->l_name);
1470 return (1);
1471 }
1472 if (lun->l_path != NULL) {
1473 log_warnx("path must not be specified "
1474 "for ramdisk-backed lun \"%s\"",
1475 lun->l_name);
1476 return (1);
1477 }
1478 }
1479 if (lun->l_blocksize == 0) {
1480 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
1481 } else if (lun->l_blocksize < 0) {
1482 log_warnx("invalid blocksize for lun \"%s\"; "
1483 "must be larger than 0", lun->l_name);
1484 return (1);
1485 }
1486 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
1487 log_warnx("invalid size for lun \"%s\"; "
1488 "must be multiple of blocksize", lun->l_name);
1489 return (1);
1490 }
1491 TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) {
1492 if (lun == lun2)
1493 continue;
1494 if (lun->l_path != NULL && lun2->l_path != NULL &&
1495 strcmp(lun->l_path, lun2->l_path) == 0) {
1496 log_debugx("WARNING: path \"%s\" duplicated "
1497 "between lun \"%s\", and "
1498 "lun \"%s\"", lun->l_path,
1499 lun->l_name, lun2->l_name);
1500 }
1501 }
1502
1503 return (0);
1504}
1505
1506int
1507conf_verify(struct conf *conf)
1508{
1509 struct auth_group *ag;
1510 struct portal_group *pg;
1511 struct target *targ;
1512 struct lun *lun;
1513 bool found;
1514 int error, i;
1515
1516 if (conf->conf_pidfile_path == NULL)
1517 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1518
1519 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1520 error = conf_verify_lun(lun);
1521 if (error != 0)
1522 return (error);
1523 }
1524 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1525 if (targ->t_auth_group == NULL) {
1526 targ->t_auth_group = auth_group_find(conf,
1527 "default");
1528 assert(targ->t_auth_group != NULL);
1529 }
1530 if (targ->t_portal_group == NULL) {
1531 targ->t_portal_group = portal_group_find(conf,
1532 "default");
1533 assert(targ->t_portal_group != NULL);
1534 }
1535 found = false;
1536 for (i = 0; i < MAX_LUNS; i++) {
1537 if (targ->t_luns[i] != NULL)
1538 found = true;
1539 }
1540 if (!found && targ->t_redirection == NULL) {
1541 log_warnx("no LUNs defined for target \"%s\"",
1542 targ->t_name);
1543 }
1544 if (found && targ->t_redirection != NULL) {
1545 log_debugx("target \"%s\" contains luns, "
1546 " but configured for redirection",
1547 targ->t_name);
1548 }
1549 }
1550 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1551 assert(pg->pg_name != NULL);
1552 if (pg->pg_discovery_auth_group == NULL) {
1553 pg->pg_discovery_auth_group =
1554 auth_group_find(conf, "default");
1555 assert(pg->pg_discovery_auth_group != NULL);
1556 }
1557
1558 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1559 pg->pg_discovery_filter = PG_FILTER_NONE;
1560
1561 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1562 if (targ->t_portal_group == pg)
1563 break;
1564 }
1565 if (pg->pg_redirection != NULL) {
1566 if (targ != NULL) {
1567 log_debugx("portal-group \"%s\" assigned "
1568 "to target \"%s\", but configured "
1569 "for redirection",
1570 pg->pg_name, targ->t_name);
1571 }
1572 pg->pg_unassigned = false;
1573 } else if (targ != NULL) {
1574 pg->pg_unassigned = false;
1575 } else {
1576 if (strcmp(pg->pg_name, "default") != 0)
1577 log_warnx("portal-group \"%s\" not assigned "
1578 "to any target", pg->pg_name);
1579 pg->pg_unassigned = true;
1580 }
1581 }
1582 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1583 if (ag->ag_name == NULL)
1584 assert(ag->ag_target != NULL);
1585 else
1586 assert(ag->ag_target == NULL);
1587
1588 found = false;
1589 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1590 if (targ->t_auth_group == ag) {
1591 found = true;
1592 break;
1593 }
1594 }
1595 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1596 if (pg->pg_discovery_auth_group == ag) {
1597 found = true;
1598 break;
1599 }
1600 }
1601 if (!found && ag->ag_name != NULL &&
1602 strcmp(ag->ag_name, "default") != 0 &&
1603 strcmp(ag->ag_name, "no-authentication") != 0 &&
1604 strcmp(ag->ag_name, "no-access") != 0) {
1605 log_warnx("auth-group \"%s\" not assigned "
1606 "to any target", ag->ag_name);
1607 }
1608 }
1609
1610 return (0);
1611}
1612
1613static int
1614conf_apply(struct conf *oldconf, struct conf *newconf)
1615{
1616 struct target *oldtarg, *newtarg, *tmptarg;
1617 struct lun *oldlun, *newlun, *tmplun;
1618 struct portal_group *oldpg, *newpg;
1619 struct portal *oldp, *newp;
1620 struct isns *oldns, *newns;
1621 pid_t otherpid;
1622 int changed, cumulated_error = 0, error, sockbuf;
1623 int one = 1;
1624
1625 if (oldconf->conf_debug != newconf->conf_debug) {
1626 log_debugx("changing debug level to %d", newconf->conf_debug);
1627 log_init(newconf->conf_debug);
1628 }
1629
1630 if (oldconf->conf_pidfh != NULL) {
1631 assert(oldconf->conf_pidfile_path != NULL);
1632 if (newconf->conf_pidfile_path != NULL &&
1633 strcmp(oldconf->conf_pidfile_path,
1634 newconf->conf_pidfile_path) == 0) {
1635 newconf->conf_pidfh = oldconf->conf_pidfh;
1636 oldconf->conf_pidfh = NULL;
1637 } else {
1638 log_debugx("removing pidfile %s",
1639 oldconf->conf_pidfile_path);
1640 pidfile_remove(oldconf->conf_pidfh);
1641 oldconf->conf_pidfh = NULL;
1642 }
1643 }
1644
1645 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1646 log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1647 newconf->conf_pidfh =
1648 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1649 if (newconf->conf_pidfh == NULL) {
1650 if (errno == EEXIST)
1651 log_errx(1, "daemon already running, pid: %jd.",
1652 (intmax_t)otherpid);
1653 log_err(1, "cannot open or create pidfile \"%s\"",
1654 newconf->conf_pidfile_path);
1655 }
1656 }
1657
1658 /*
1659 * Go through the new portal groups, assigning tags or preserving old.
1660 */
1661 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1662 oldpg = portal_group_find(oldconf, newpg->pg_name);
1663 if (oldpg != NULL)
1664 newpg->pg_tag = oldpg->pg_tag;
1665 else
1666 newpg->pg_tag = ++last_portal_group_tag;
1667 }
1668
1669 /* Deregister on removed iSNS servers. */
1670 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1671 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1672 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1673 break;
1674 }
1675 if (newns == NULL)
1676 isns_deregister(oldns);
1677 }
1678
1679 /*
1680 * XXX: If target or lun removal fails, we should somehow "move"
1681 * the old lun or target into newconf, so that subsequent
1682 * conf_apply() would try to remove them again. That would
1683 * be somewhat hairy, though, and lun deletion failures don't
1684 * really happen, so leave it as it is for now.
1685 */
1686 /*
1687 * First, remove any targets present in the old configuration
1688 * and missing in the new one.
1689 */
1690 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1691 newtarg = target_find(newconf, oldtarg->t_name);
1692 if (newtarg != NULL)
1693 continue;
1694 error = kernel_port_remove(oldtarg);
1695 if (error != 0) {
1696 log_warnx("failed to remove target %s",
1697 oldtarg->t_name);
1698 /*
1699 * XXX: Uncomment after fixing the root cause.
1700 *
1701 * cumulated_error++;
1702 */
1703 }
1704 }
1705
1706 /*
1707 * Second, remove any LUNs present in the old configuration
1708 * and missing in the new one.
1709 */
1710 TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) {
1711 newlun = lun_find(newconf, oldlun->l_name);
1712 if (newlun == NULL) {
1713 log_debugx("lun \"%s\", CTL lun %d "
1714 "not found in new configuration; "
1715 "removing", oldlun->l_name, oldlun->l_ctl_lun);
1716 error = kernel_lun_remove(oldlun);
1717 if (error != 0) {
1718 log_warnx("failed to remove lun \"%s\", "
1719 "CTL lun %d",
1720 oldlun->l_name, oldlun->l_ctl_lun);
1721 cumulated_error++;
1722 }
1723 continue;
1724 }
1725
1726 /*
1727 * Also remove the LUNs changed by more than size.
1728 */
1729 changed = 0;
1730 assert(oldlun->l_backend != NULL);
1731 assert(newlun->l_backend != NULL);
1732 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1733 log_debugx("backend for lun \"%s\", "
1734 "CTL lun %d changed; removing",
1735 oldlun->l_name, oldlun->l_ctl_lun);
1736 changed = 1;
1737 }
1738 if (oldlun->l_blocksize != newlun->l_blocksize) {
1739 log_debugx("blocksize for lun \"%s\", "
1740 "CTL lun %d changed; removing",
1741 oldlun->l_name, oldlun->l_ctl_lun);
1742 changed = 1;
1743 }
1744 if (newlun->l_device_id != NULL &&
1745 (oldlun->l_device_id == NULL ||
1746 strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1747 0)) {
1748 log_debugx("device-id for lun \"%s\", "
1749 "CTL lun %d changed; removing",
1750 oldlun->l_name, oldlun->l_ctl_lun);
1751 changed = 1;
1752 }
1753 if (newlun->l_path != NULL &&
1754 (oldlun->l_path == NULL ||
1755 strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1756 log_debugx("path for lun \"%s\", "
1757 "CTL lun %d, changed; removing",
1758 oldlun->l_name, oldlun->l_ctl_lun);
1759 changed = 1;
1760 }
1761 if (newlun->l_serial != NULL &&
1762 (oldlun->l_serial == NULL ||
1763 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1764 log_debugx("serial for lun \"%s\", "
1765 "CTL lun %d changed; removing",
1766 oldlun->l_name, oldlun->l_ctl_lun);
1767 changed = 1;
1768 }
1769 if (changed) {
1770 error = kernel_lun_remove(oldlun);
1771 if (error != 0) {
1772 log_warnx("failed to remove lun \"%s\", "
1773 "CTL lun %d",
1774 oldlun->l_name, oldlun->l_ctl_lun);
1775 cumulated_error++;
1776 }
1777 lun_delete(oldlun);
1778 continue;
1779 }
1780
1781 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1782 }
1783
1784 TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) {
1785 oldlun = lun_find(oldconf, newlun->l_name);
1786 if (oldlun != NULL) {
1787 if (newlun->l_size != oldlun->l_size ||
1788 newlun->l_size == 0) {
1789 log_debugx("resizing lun \"%s\", CTL lun %d",
1790 newlun->l_name, newlun->l_ctl_lun);
1791 error = kernel_lun_resize(newlun);
1792 if (error != 0) {
1793 log_warnx("failed to "
1794 "resize lun \"%s\", CTL lun %d",
1795 newlun->l_name,
1796 newlun->l_ctl_lun);
1797 cumulated_error++;
1798 }
1799 }
1800 continue;
1801 }
1802 log_debugx("adding lun \"%s\"", newlun->l_name);
1803 error = kernel_lun_add(newlun);
1804 if (error != 0) {
1805 log_warnx("failed to add lun \"%s\"", newlun->l_name);
1806 lun_delete(newlun);
1807 cumulated_error++;
1808 }
1809 }
1810
1811 /*
1812 * Now add new targets or modify existing ones.
1813 */
1814 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1815 oldtarg = target_find(oldconf, newtarg->t_name);
1816
1817 if (oldtarg == NULL)
1818 error = kernel_port_add(newtarg);
1819 else {
1820 target_set_ctl_port(newtarg, oldtarg->t_ctl_port);
1821 error = kernel_port_update(newtarg);
1822 }
1823 if (error != 0) {
1824 log_warnx("failed to %s target %s",
1825 (oldtarg == NULL) ? "add" : "update",
1826 newtarg->t_name);
1827 /*
1828 * XXX: Uncomment after fixing the root cause.
1829 *
1830 * cumulated_error++;
1831 */
1832 }
1833 }
1834
1835 /*
1836 * Go through the new portals, opening the sockets as neccessary.
1837 */
1838 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1839 if (newpg->pg_unassigned) {
1840 log_debugx("not listening on portal-group \"%s\", "
1841 "not assigned to any target",
1842 newpg->pg_name);
1843 continue;
1844 }
1845 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1846 /*
1847 * Try to find already open portal and reuse
1848 * the listening socket. We don't care about
1849 * what portal or portal group that was, what
1850 * matters is the listening address.
1851 */
1852 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1853 pg_next) {
1854 TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1855 p_next) {
1856 if (strcmp(newp->p_listen,
1857 oldp->p_listen) == 0 &&
1858 oldp->p_socket > 0) {
1859 newp->p_socket =
1860 oldp->p_socket;
1861 oldp->p_socket = 0;
1862 break;
1863 }
1864 }
1865 }
1866 if (newp->p_socket > 0) {
1867 /*
1868 * We're done with this portal.
1869 */
1870 continue;
1871 }
1872
1873#ifdef ICL_KERNEL_PROXY
1874 if (proxy_mode) {
1875 newpg->pg_conf->conf_portal_id++;
1876 newp->p_id = newpg->pg_conf->conf_portal_id;
1877 log_debugx("listening on %s, portal-group "
1878 "\"%s\", portal id %d, using ICL proxy",
1879 newp->p_listen, newpg->pg_name, newp->p_id);
1880 kernel_listen(newp->p_ai, newp->p_iser,
1881 newp->p_id);
1882 continue;
1883 }
1884#endif
1885 assert(proxy_mode == false);
1886 assert(newp->p_iser == false);
1887
1888 log_debugx("listening on %s, portal-group \"%s\"",
1889 newp->p_listen, newpg->pg_name);
1890 newp->p_socket = socket(newp->p_ai->ai_family,
1891 newp->p_ai->ai_socktype,
1892 newp->p_ai->ai_protocol);
1893 if (newp->p_socket < 0) {
1894 log_warn("socket(2) failed for %s",
1895 newp->p_listen);
1896 cumulated_error++;
1897 continue;
1898 }
1899 sockbuf = SOCKBUF_SIZE;
1900 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF,
1901 &sockbuf, sizeof(sockbuf)) == -1)
1902 log_warn("setsockopt(SO_RCVBUF) failed "
1903 "for %s", newp->p_listen);
1904 sockbuf = SOCKBUF_SIZE;
1905 if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF,
1906 &sockbuf, sizeof(sockbuf)) == -1)
1907 log_warn("setsockopt(SO_SNDBUF) failed "
1908 "for %s", newp->p_listen);
1909 error = setsockopt(newp->p_socket, SOL_SOCKET,
1910 SO_REUSEADDR, &one, sizeof(one));
1911 if (error != 0) {
1912 log_warn("setsockopt(SO_REUSEADDR) failed "
1913 "for %s", newp->p_listen);
1914 close(newp->p_socket);
1915 newp->p_socket = 0;
1916 cumulated_error++;
1917 continue;
1918 }
1919 error = bind(newp->p_socket, newp->p_ai->ai_addr,
1920 newp->p_ai->ai_addrlen);
1921 if (error != 0) {
1922 log_warn("bind(2) failed for %s",
1923 newp->p_listen);
1924 close(newp->p_socket);
1925 newp->p_socket = 0;
1926 cumulated_error++;
1927 continue;
1928 }
1929 error = listen(newp->p_socket, -1);
1930 if (error != 0) {
1931 log_warn("listen(2) failed for %s",
1932 newp->p_listen);
1933 close(newp->p_socket);
1934 newp->p_socket = 0;
1935 cumulated_error++;
1936 continue;
1937 }
1938 }
1939 }
1940
1941 /*
1942 * Go through the no longer used sockets, closing them.
1943 */
1944 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1945 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1946 if (oldp->p_socket <= 0)
1947 continue;
1948 log_debugx("closing socket for %s, portal-group \"%s\"",
1949 oldp->p_listen, oldpg->pg_name);
1950 close(oldp->p_socket);
1951 oldp->p_socket = 0;
1952 }
1953 }
1954
1955 /* (Re-)Register on remaining/new iSNS servers. */
1956 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) {
1957 TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) {
1958 if (strcmp(oldns->i_addr, newns->i_addr) == 0)
1959 break;
1960 }
1961 isns_register(newns, oldns);
1962 }
1963
1964 /* Schedule iSNS update */
1965 if (!TAILQ_EMPTY(&newconf->conf_isns))
1966 set_timeout((newconf->conf_isns_period + 2) / 3, false);
1967
1968 return (cumulated_error);
1969}
1970
1971bool
1972timed_out(void)
1973{
1974
1975 return (sigalrm_received);
1976}
1977
1978static void
1979sigalrm_handler_fatal(int dummy __unused)
1980{
1981 /*
1982 * It would be easiest to just log an error and exit. We can't
1983 * do this, though, because log_errx() is not signal safe, since
1984 * it calls syslog(3). Instead, set a flag checked by pdu_send()
1985 * and pdu_receive(), to call log_errx() there. Should they fail
1986 * to notice, we'll exit here one second later.
1987 */
1988 if (sigalrm_received) {
1989 /*
1990 * Oh well. Just give up and quit.
1991 */
1992 _exit(2);
1993 }
1994
1995 sigalrm_received = true;
1996}
1997
1998static void
1999sigalrm_handler(int dummy __unused)
2000{
2001
2002 sigalrm_received = true;
2003}
2004
2005void
2006set_timeout(int timeout, int fatal)
2007{
2008 struct sigaction sa;
2009 struct itimerval itv;
2010 int error;
2011
2012 if (timeout <= 0) {
2013 log_debugx("session timeout disabled");
2014 bzero(&itv, sizeof(itv));
2015 error = setitimer(ITIMER_REAL, &itv, NULL);
2016 if (error != 0)
2017 log_err(1, "setitimer");
2018 sigalrm_received = false;
2019 return;
2020 }
2021
2022 sigalrm_received = false;
2023 bzero(&sa, sizeof(sa));
2024 if (fatal)
2025 sa.sa_handler = sigalrm_handler_fatal;
2026 else
2027 sa.sa_handler = sigalrm_handler;
2028 sigfillset(&sa.sa_mask);
2029 error = sigaction(SIGALRM, &sa, NULL);
2030 if (error != 0)
2031 log_err(1, "sigaction");
2032
2033 /*
2034 * First SIGALRM will arive after conf_timeout seconds.
2035 * If we do nothing, another one will arrive a second later.
2036 */
2037 log_debugx("setting session timeout to %d seconds", timeout);
2038 bzero(&itv, sizeof(itv));
2039 itv.it_interval.tv_sec = 1;
2040 itv.it_value.tv_sec = timeout;
2041 error = setitimer(ITIMER_REAL, &itv, NULL);
2042 if (error != 0)
2043 log_err(1, "setitimer");
2044}
2045
2046static int
2047wait_for_children(bool block)
2048{
2049 pid_t pid;
2050 int status;
2051 int num = 0;
2052
2053 for (;;) {
2054 /*
2055 * If "block" is true, wait for at least one process.
2056 */
2057 if (block && num == 0)
2058 pid = wait4(-1, &status, 0, NULL);
2059 else
2060 pid = wait4(-1, &status, WNOHANG, NULL);
2061 if (pid <= 0)
2062 break;
2063 if (WIFSIGNALED(status)) {
2064 log_warnx("child process %d terminated with signal %d",
2065 pid, WTERMSIG(status));
2066 } else if (WEXITSTATUS(status) != 0) {
2067 log_warnx("child process %d terminated with exit status %d",
2068 pid, WEXITSTATUS(status));
2069 } else {
2070 log_debugx("child process %d terminated gracefully", pid);
2071 }
2072 num++;
2073 }
2074
2075 return (num);
2076}
2077
2078static void
2079handle_connection(struct portal *portal, int fd,
2080 const struct sockaddr *client_sa, bool dont_fork)
2081{
2082 struct connection *conn;
2083 int error;
2084 pid_t pid;
2085 char host[NI_MAXHOST + 1];
2086 struct conf *conf;
2087
2088 conf = portal->p_portal_group->pg_conf;
2089
2090 if (dont_fork) {
2091 log_debugx("incoming connection; not forking due to -d flag");
2092 } else {
2093 nchildren -= wait_for_children(false);
2094 assert(nchildren >= 0);
2095
2096 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
2097 log_debugx("maxproc limit of %d child processes hit; "
2098 "waiting for child process to exit", conf->conf_maxproc);
2099 nchildren -= wait_for_children(true);
2100 assert(nchildren >= 0);
2101 }
2102 log_debugx("incoming connection; forking child process #%d",
2103 nchildren);
2104 nchildren++;
2105 pid = fork();
2106 if (pid < 0)
2107 log_err(1, "fork");
2108 if (pid > 0) {
2109 close(fd);
2110 return;
2111 }
2112 }
2113 pidfile_close(conf->conf_pidfh);
2114
2115 error = getnameinfo(client_sa, client_sa->sa_len,
2116 host, sizeof(host), NULL, 0, NI_NUMERICHOST);
2117 if (error != 0)
2118 log_errx(1, "getnameinfo: %s", gai_strerror(error));
2119
2120 log_debugx("accepted connection from %s; portal group \"%s\"",
2121 host, portal->p_portal_group->pg_name);
2122 log_set_peer_addr(host);
2123 setproctitle("%s", host);
2124
2125 conn = connection_new(portal, fd, host, client_sa);
2126 set_timeout(conf->conf_timeout, true);
2127 kernel_capsicate();
2128 login(conn);
2129 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
2130 kernel_handoff(conn);
2131 log_debugx("connection handed off to the kernel");
2132 } else {
2133 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
2134 discovery(conn);
2135 }
2136 log_debugx("nothing more to do; exiting");
2137 exit(0);
2138}
2139
2140static int
2141fd_add(int fd, fd_set *fdset, int nfds)
2142{
2143
2144 /*
2145 * Skip sockets which we failed to bind.
2146 */
2147 if (fd <= 0)
2148 return (nfds);
2149
2150 FD_SET(fd, fdset);
2151 if (fd > nfds)
2152 nfds = fd;
2153 return (nfds);
2154}
2155
2156static void
2157main_loop(struct conf *conf, bool dont_fork)
2158{
2159 struct portal_group *pg;
2160 struct portal *portal;
2161 struct sockaddr_storage client_sa;
2162 socklen_t client_salen;
2163#ifdef ICL_KERNEL_PROXY
2164 int connection_id;
2165 int portal_id;
2166#endif
2167 fd_set fdset;
2168 int error, nfds, client_fd;
2169
2170 pidfile_write(conf->conf_pidfh);
2171
2172 for (;;) {
2173 if (sighup_received || sigterm_received || timed_out())
2174 return;
2175
2176#ifdef ICL_KERNEL_PROXY
2177 if (proxy_mode) {
2178 client_salen = sizeof(client_sa);
2179 kernel_accept(&connection_id, &portal_id,
2180 (struct sockaddr *)&client_sa, &client_salen);
2181 assert(client_salen >= client_sa.ss_len);
2182
2183 log_debugx("incoming connection, id %d, portal id %d",
2184 connection_id, portal_id);
2185 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2186 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2187 if (portal->p_id == portal_id) {
2188 goto found;
2189 }
2190 }
2191 }
2192
2193 log_errx(1, "kernel returned invalid portal_id %d",
2194 portal_id);
2195
2196found:
2197 handle_connection(portal, connection_id,
2198 (struct sockaddr *)&client_sa, dont_fork);
2199 } else {
2200#endif
2201 assert(proxy_mode == false);
2202
2203 FD_ZERO(&fdset);
2204 nfds = 0;
2205 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2206 TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
2207 nfds = fd_add(portal->p_socket, &fdset, nfds);
2208 }
2209 error = select(nfds + 1, &fdset, NULL, NULL, NULL);
2210 if (error <= 0) {
2211 if (errno == EINTR)
2212 return;
2213 log_err(1, "select");
2214 }
2215 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
2216 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
2217 if (!FD_ISSET(portal->p_socket, &fdset))
2218 continue;
2219 client_salen = sizeof(client_sa);
2220 client_fd = accept(portal->p_socket,
2221 (struct sockaddr *)&client_sa,
2222 &client_salen);
2223 if (client_fd < 0)
2224 log_err(1, "accept");
2225 assert(client_salen >= client_sa.ss_len);
2226
2227 handle_connection(portal, client_fd,
2228 (struct sockaddr *)&client_sa,
2229 dont_fork);
2230 break;
2231 }
2232 }
2233#ifdef ICL_KERNEL_PROXY
2234 }
2235#endif
2236 }
2237}
2238
2239static void
2240sighup_handler(int dummy __unused)
2241{
2242
2243 sighup_received = true;
2244}
2245
2246static void
2247sigterm_handler(int dummy __unused)
2248{
2249
2250 sigterm_received = true;
2251}
2252
2253static void
2254sigchld_handler(int dummy __unused)
2255{
2256
2257 /*
2258 * The only purpose of this handler is to make SIGCHLD
2259 * interrupt the ISCSIDWAIT ioctl(2), so we can call
2260 * wait_for_children().
2261 */
2262}
2263
2264static void
2265register_signals(void)
2266{
2267 struct sigaction sa;
2268 int error;
2269
2270 bzero(&sa, sizeof(sa));
2271 sa.sa_handler = sighup_handler;
2272 sigfillset(&sa.sa_mask);
2273 error = sigaction(SIGHUP, &sa, NULL);
2274 if (error != 0)
2275 log_err(1, "sigaction");
2276
2277 sa.sa_handler = sigterm_handler;
2278 error = sigaction(SIGTERM, &sa, NULL);
2279 if (error != 0)
2280 log_err(1, "sigaction");
2281
2282 sa.sa_handler = sigterm_handler;
2283 error = sigaction(SIGINT, &sa, NULL);
2284 if (error != 0)
2285 log_err(1, "sigaction");
2286
2287 sa.sa_handler = sigchld_handler;
2288 error = sigaction(SIGCHLD, &sa, NULL);
2289 if (error != 0)
2290 log_err(1, "sigaction");
2291}
2292
2293int
2294main(int argc, char **argv)
2295{
2296 struct conf *oldconf, *newconf, *tmpconf;
2297 struct isns *newns;
2298 const char *config_path = DEFAULT_CONFIG_PATH;
2299 int debug = 0, ch, error;
2300 bool dont_daemonize = false;
2301
2302 while ((ch = getopt(argc, argv, "df:R")) != -1) {
2303 switch (ch) {
2304 case 'd':
2305 dont_daemonize = true;
2306 debug++;
2307 break;
2308 case 'f':
2309 config_path = optarg;
2310 break;
2311 case 'R':
2312#ifndef ICL_KERNEL_PROXY
2313 log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY "
2314 "does not support iSER protocol");
2315#endif
2316 proxy_mode = true;
2317 break;
2318 case '?':
2319 default:
2320 usage();
2321 }
2322 }
2323 argc -= optind;
2324 if (argc != 0)
2325 usage();
2326
2327 log_init(debug);
2328 kernel_init();
2329
2330 oldconf = conf_new_from_kernel();
2331 newconf = conf_new_from_file(config_path);
2332 if (newconf == NULL)
2333 log_errx(1, "configuration error; exiting");
2334 if (debug > 0) {
2335 oldconf->conf_debug = debug;
2336 newconf->conf_debug = debug;
2337 }
2338
2339 error = conf_apply(oldconf, newconf);
2340 if (error != 0)
2341 log_errx(1, "failed to apply configuration; exiting");
2342
2343 conf_delete(oldconf);
2344 oldconf = NULL;
2345
2346 register_signals();
2347
2348 if (dont_daemonize == false) {
2349 log_debugx("daemonizing");
2350 if (daemon(0, 0) == -1) {
2351 log_warn("cannot daemonize");
2352 pidfile_remove(newconf->conf_pidfh);
2353 exit(1);
2354 }
2355 }
2356
2357 /* Schedule iSNS update */
2358 if (!TAILQ_EMPTY(&newconf->conf_isns))
2359 set_timeout((newconf->conf_isns_period + 2) / 3, false);
2360
2361 for (;;) {
2362 main_loop(newconf, dont_daemonize);
2363 if (sighup_received) {
2364 sighup_received = false;
2365 log_debugx("received SIGHUP, reloading configuration");
2366 tmpconf = conf_new_from_file(config_path);
2367 if (tmpconf == NULL) {
2368 log_warnx("configuration error, "
2369 "continuing with old configuration");
2370 } else {
2371 if (debug > 0)
2372 tmpconf->conf_debug = debug;
2373 oldconf = newconf;
2374 newconf = tmpconf;
2375 error = conf_apply(oldconf, newconf);
2376 if (error != 0)
2377 log_warnx("failed to reload "
2378 "configuration");
2379 conf_delete(oldconf);
2380 oldconf = NULL;
2381 }
2382 } else if (sigterm_received) {
2383 log_debugx("exiting on signal; "
2384 "reloading empty configuration");
2385
2386 log_debugx("removing CTL iSCSI ports "
2387 "and terminating all connections");
2388
2389 oldconf = newconf;
2390 newconf = conf_new();
2391 if (debug > 0)
2392 newconf->conf_debug = debug;
2393 error = conf_apply(oldconf, newconf);
2394 if (error != 0)
2395 log_warnx("failed to apply configuration");
2396 conf_delete(oldconf);
2397 oldconf = NULL;
2398
2399 log_warnx("exiting on signal");
2400 exit(0);
2401 } else {
2402 nchildren -= wait_for_children(false);
2403 assert(nchildren >= 0);
2404 if (timed_out()) {
2405 set_timeout(0, false);
2406 TAILQ_FOREACH(newns, &newconf->conf_isns, i_next)
2407 isns_check(newns);
2408 /* Schedule iSNS update */
2409 if (!TAILQ_EMPTY(&newconf->conf_isns)) {
2410 set_timeout((newconf->conf_isns_period
2411 + 2) / 3,
2412 false);
2413 }
2414 }
2415 }
2416 }
2417 /* NOTREACHED */
2418}