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