1009ea47eSEdward Tomasz Napierala /*- 2009ea47eSEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 3009ea47eSEdward Tomasz Napierala * All rights reserved. 4009ea47eSEdward Tomasz Napierala * 5009ea47eSEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship 6009ea47eSEdward Tomasz Napierala * from the FreeBSD Foundation. 7009ea47eSEdward Tomasz Napierala * 8009ea47eSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without 9009ea47eSEdward Tomasz Napierala * modification, are permitted provided that the following conditions 10009ea47eSEdward Tomasz Napierala * are met: 11009ea47eSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright 12009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer. 13009ea47eSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright 14009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the 15009ea47eSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution. 16009ea47eSEdward Tomasz Napierala * 17009ea47eSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18009ea47eSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19009ea47eSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20009ea47eSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21009ea47eSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22009ea47eSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23009ea47eSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24009ea47eSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25009ea47eSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26009ea47eSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27009ea47eSEdward Tomasz Napierala * SUCH DAMAGE. 28009ea47eSEdward Tomasz Napierala * 29009ea47eSEdward Tomasz Napierala */ 30009ea47eSEdward Tomasz Napierala 31b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h> 32b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$"); 33b7a65e39SEdward Tomasz Napierala 34009ea47eSEdward Tomasz Napierala #include <sys/types.h> 35009ea47eSEdward Tomasz Napierala #include <sys/time.h> 36009ea47eSEdward Tomasz Napierala #include <sys/socket.h> 37009ea47eSEdward Tomasz Napierala #include <sys/wait.h> 38009ea47eSEdward Tomasz Napierala #include <netinet/in.h> 39073edb1cSAlexander Motin #include <arpa/inet.h> 40009ea47eSEdward Tomasz Napierala #include <assert.h> 41009ea47eSEdward Tomasz Napierala #include <ctype.h> 42009ea47eSEdward Tomasz Napierala #include <errno.h> 43009ea47eSEdward Tomasz Napierala #include <netdb.h> 44009ea47eSEdward Tomasz Napierala #include <signal.h> 45009ea47eSEdward Tomasz Napierala #include <stdbool.h> 46009ea47eSEdward Tomasz Napierala #include <stdio.h> 47009ea47eSEdward Tomasz Napierala #include <stdint.h> 48009ea47eSEdward Tomasz Napierala #include <stdlib.h> 49009ea47eSEdward Tomasz Napierala #include <string.h> 50009ea47eSEdward Tomasz Napierala #include <unistd.h> 51009ea47eSEdward Tomasz Napierala 52009ea47eSEdward Tomasz Napierala #include "ctld.h" 53829603e2SAlexander Motin #include "isns.h" 54009ea47eSEdward Tomasz Napierala 55ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false; 56ba3a2d31SEdward Tomasz Napierala 57009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false; 58009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false; 59009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false; 60009ea47eSEdward Tomasz Napierala 61009ea47eSEdward Tomasz Napierala static int nchildren = 0; 62009ea47eSEdward Tomasz Napierala 63009ea47eSEdward Tomasz Napierala static void 64009ea47eSEdward Tomasz Napierala usage(void) 65009ea47eSEdward Tomasz Napierala { 66009ea47eSEdward Tomasz Napierala 67009ea47eSEdward Tomasz Napierala fprintf(stderr, "usage: ctld [-d][-f config-file]\n"); 68009ea47eSEdward Tomasz Napierala exit(1); 69009ea47eSEdward Tomasz Napierala } 70009ea47eSEdward Tomasz Napierala 71009ea47eSEdward Tomasz Napierala char * 72009ea47eSEdward Tomasz Napierala checked_strdup(const char *s) 73009ea47eSEdward Tomasz Napierala { 74009ea47eSEdward Tomasz Napierala char *c; 75009ea47eSEdward Tomasz Napierala 76009ea47eSEdward Tomasz Napierala c = strdup(s); 77009ea47eSEdward Tomasz Napierala if (c == NULL) 78009ea47eSEdward Tomasz Napierala log_err(1, "strdup"); 79009ea47eSEdward Tomasz Napierala return (c); 80009ea47eSEdward Tomasz Napierala } 81009ea47eSEdward Tomasz Napierala 82009ea47eSEdward Tomasz Napierala struct conf * 83009ea47eSEdward Tomasz Napierala conf_new(void) 84009ea47eSEdward Tomasz Napierala { 85009ea47eSEdward Tomasz Napierala struct conf *conf; 86009ea47eSEdward Tomasz Napierala 87009ea47eSEdward Tomasz Napierala conf = calloc(1, sizeof(*conf)); 88009ea47eSEdward Tomasz Napierala if (conf == NULL) 89009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 90009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_targets); 91009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_auth_groups); 92009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_portal_groups); 93829603e2SAlexander Motin TAILQ_INIT(&conf->conf_isns); 94009ea47eSEdward Tomasz Napierala 95829603e2SAlexander Motin conf->conf_isns_period = 900; 96829603e2SAlexander Motin conf->conf_isns_timeout = 5; 97009ea47eSEdward Tomasz Napierala conf->conf_debug = 0; 98009ea47eSEdward Tomasz Napierala conf->conf_timeout = 60; 99009ea47eSEdward Tomasz Napierala conf->conf_maxproc = 30; 100009ea47eSEdward Tomasz Napierala 101009ea47eSEdward Tomasz Napierala return (conf); 102009ea47eSEdward Tomasz Napierala } 103009ea47eSEdward Tomasz Napierala 104009ea47eSEdward Tomasz Napierala void 105009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf) 106009ea47eSEdward Tomasz Napierala { 107009ea47eSEdward Tomasz Napierala struct target *targ, *tmp; 108009ea47eSEdward Tomasz Napierala struct auth_group *ag, *cagtmp; 109009ea47eSEdward Tomasz Napierala struct portal_group *pg, *cpgtmp; 110829603e2SAlexander Motin struct isns *is, *istmp; 111009ea47eSEdward Tomasz Napierala 112009ea47eSEdward Tomasz Napierala assert(conf->conf_pidfh == NULL); 113009ea47eSEdward Tomasz Napierala 114009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 115009ea47eSEdward Tomasz Napierala target_delete(targ); 116009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 117009ea47eSEdward Tomasz Napierala auth_group_delete(ag); 118009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 119009ea47eSEdward Tomasz Napierala portal_group_delete(pg); 120829603e2SAlexander Motin TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp) 121829603e2SAlexander Motin isns_delete(is); 122009ea47eSEdward Tomasz Napierala free(conf->conf_pidfile_path); 123009ea47eSEdward Tomasz Napierala free(conf); 124009ea47eSEdward Tomasz Napierala } 125009ea47eSEdward Tomasz Napierala 126009ea47eSEdward Tomasz Napierala static struct auth * 127009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag) 128009ea47eSEdward Tomasz Napierala { 129009ea47eSEdward Tomasz Napierala struct auth *auth; 130009ea47eSEdward Tomasz Napierala 131009ea47eSEdward Tomasz Napierala auth = calloc(1, sizeof(*auth)); 132009ea47eSEdward Tomasz Napierala if (auth == NULL) 133009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 134009ea47eSEdward Tomasz Napierala auth->a_auth_group = ag; 135009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); 136009ea47eSEdward Tomasz Napierala return (auth); 137009ea47eSEdward Tomasz Napierala } 138009ea47eSEdward Tomasz Napierala 139009ea47eSEdward Tomasz Napierala static void 140009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth) 141009ea47eSEdward Tomasz Napierala { 142009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); 143009ea47eSEdward Tomasz Napierala 144009ea47eSEdward Tomasz Napierala free(auth->a_user); 145009ea47eSEdward Tomasz Napierala free(auth->a_secret); 146009ea47eSEdward Tomasz Napierala free(auth->a_mutual_user); 147009ea47eSEdward Tomasz Napierala free(auth->a_mutual_secret); 148009ea47eSEdward Tomasz Napierala free(auth); 149009ea47eSEdward Tomasz Napierala } 150009ea47eSEdward Tomasz Napierala 151009ea47eSEdward Tomasz Napierala const struct auth * 152d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user) 153009ea47eSEdward Tomasz Napierala { 154009ea47eSEdward Tomasz Napierala const struct auth *auth; 155009ea47eSEdward Tomasz Napierala 156009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { 157009ea47eSEdward Tomasz Napierala if (strcmp(auth->a_user, user) == 0) 158009ea47eSEdward Tomasz Napierala return (auth); 159009ea47eSEdward Tomasz Napierala } 160009ea47eSEdward Tomasz Napierala 161009ea47eSEdward Tomasz Napierala return (NULL); 162009ea47eSEdward Tomasz Napierala } 163009ea47eSEdward Tomasz Napierala 1641658628dSEdward Tomasz Napierala static void 1651658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth) 1661658628dSEdward Tomasz Napierala { 1671658628dSEdward Tomasz Napierala size_t len; 1681658628dSEdward Tomasz Napierala 1691658628dSEdward Tomasz Napierala len = strlen(auth->a_secret); 1701658628dSEdward Tomasz Napierala if (len > 16) { 1711658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1721658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 1731658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 1741658628dSEdward Tomasz Napierala "long", auth->a_user, auth->a_auth_group->ag_name); 1751658628dSEdward Tomasz Napierala else 1761658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 1771658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 1781658628dSEdward Tomasz Napierala "long", auth->a_user, 179f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 1801658628dSEdward Tomasz Napierala } 1811658628dSEdward Tomasz Napierala if (len < 12) { 1821658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1831658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 1841658628dSEdward Tomasz Napierala "is too short; it should be at least 12 characters " 1851658628dSEdward Tomasz Napierala "long", auth->a_user, 1861658628dSEdward Tomasz Napierala auth->a_auth_group->ag_name); 1871658628dSEdward Tomasz Napierala else 1881658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 1891658628dSEdward Tomasz Napierala "is too short; it should be at least 16 characters " 1901658628dSEdward Tomasz Napierala "long", auth->a_user, 191f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 1921658628dSEdward Tomasz Napierala } 1931658628dSEdward Tomasz Napierala 1941658628dSEdward Tomasz Napierala if (auth->a_mutual_secret != NULL) { 1951658628dSEdward Tomasz Napierala len = strlen(auth->a_secret); 1961658628dSEdward Tomasz Napierala if (len > 16) { 1971658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1981658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 1991658628dSEdward Tomasz Napierala "auth-group \"%s\", is too long; it should " 2001658628dSEdward Tomasz Napierala "be at most 16 characters long", 2011658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 2021658628dSEdward Tomasz Napierala else 2031658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2041658628dSEdward Tomasz Napierala "target \"%s\", is too long; it should " 2051658628dSEdward Tomasz Napierala "be at most 16 characters long", 2061658628dSEdward Tomasz Napierala auth->a_user, 207f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2081658628dSEdward Tomasz Napierala } 2091658628dSEdward Tomasz Napierala if (len < 12) { 2101658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 2111658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2121658628dSEdward Tomasz Napierala "auth-group \"%s\", is too short; it " 2131658628dSEdward Tomasz Napierala "should be at least 12 characters long", 2141658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 2151658628dSEdward Tomasz Napierala else 2161658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2171658628dSEdward Tomasz Napierala "target \"%s\", is too short; it should be " 2181658628dSEdward Tomasz Napierala "at least 16 characters long", 2191658628dSEdward Tomasz Napierala auth->a_user, 220f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2211658628dSEdward Tomasz Napierala } 2221658628dSEdward Tomasz Napierala } 2231658628dSEdward Tomasz Napierala } 2241658628dSEdward Tomasz Napierala 2251658628dSEdward Tomasz Napierala const struct auth * 2261658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user, 2271658628dSEdward Tomasz Napierala const char *secret) 2281658628dSEdward Tomasz Napierala { 2291658628dSEdward Tomasz Napierala struct auth *auth; 2301658628dSEdward Tomasz Napierala 2311658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 2321658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP; 2331658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP) { 2341658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 2351658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 2361658628dSEdward Tomasz Napierala "other types for auth-group \"%s\"", ag->ag_name); 2371658628dSEdward Tomasz Napierala else 2381658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 2391658628dSEdward Tomasz Napierala "other types for target \"%s\"", 240f7ae5bf8SEdward Tomasz Napierala ag->ag_target->t_name); 2411658628dSEdward Tomasz Napierala return (NULL); 2421658628dSEdward Tomasz Napierala } 2431658628dSEdward Tomasz Napierala 2441658628dSEdward Tomasz Napierala auth = auth_new(ag); 2451658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 2461658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 2471658628dSEdward Tomasz Napierala 2481658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 2491658628dSEdward Tomasz Napierala 2501658628dSEdward Tomasz Napierala return (auth); 2511658628dSEdward Tomasz Napierala } 2521658628dSEdward Tomasz Napierala 2531658628dSEdward Tomasz Napierala const struct auth * 2541658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user, 2551658628dSEdward Tomasz Napierala const char *secret, const char *user2, const char *secret2) 2561658628dSEdward Tomasz Napierala { 2571658628dSEdward Tomasz Napierala struct auth *auth; 2581658628dSEdward Tomasz Napierala 2591658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 2601658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP_MUTUAL; 2611658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 2621658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 2631658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 2641658628dSEdward Tomasz Napierala "with other types for auth-group \"%s\"", 2651658628dSEdward Tomasz Napierala ag->ag_name); 2661658628dSEdward Tomasz Napierala else 2671658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 2681658628dSEdward Tomasz Napierala "with other types for target \"%s\"", 269f7ae5bf8SEdward Tomasz Napierala ag->ag_target->t_name); 2701658628dSEdward Tomasz Napierala return (NULL); 2711658628dSEdward Tomasz Napierala } 2721658628dSEdward Tomasz Napierala 2731658628dSEdward Tomasz Napierala auth = auth_new(ag); 2741658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 2751658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 2761658628dSEdward Tomasz Napierala auth->a_mutual_user = checked_strdup(user2); 2771658628dSEdward Tomasz Napierala auth->a_mutual_secret = checked_strdup(secret2); 2781658628dSEdward Tomasz Napierala 2791658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 2801658628dSEdward Tomasz Napierala 2811658628dSEdward Tomasz Napierala return (auth); 2821658628dSEdward Tomasz Napierala } 2831658628dSEdward Tomasz Napierala 2848cb2e958SEdward Tomasz Napierala const struct auth_name * 2858cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name) 2868cb2e958SEdward Tomasz Napierala { 2878cb2e958SEdward Tomasz Napierala struct auth_name *an; 2888cb2e958SEdward Tomasz Napierala 2898cb2e958SEdward Tomasz Napierala an = calloc(1, sizeof(*an)); 2908cb2e958SEdward Tomasz Napierala if (an == NULL) 2918cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 2928cb2e958SEdward Tomasz Napierala an->an_auth_group = ag; 2938cb2e958SEdward Tomasz Napierala an->an_initator_name = checked_strdup(name); 2948cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next); 2958cb2e958SEdward Tomasz Napierala return (an); 2968cb2e958SEdward Tomasz Napierala } 2978cb2e958SEdward Tomasz Napierala 2988cb2e958SEdward Tomasz Napierala static void 2998cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an) 3008cb2e958SEdward Tomasz Napierala { 3018cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next); 3028cb2e958SEdward Tomasz Napierala 3038cb2e958SEdward Tomasz Napierala free(an->an_initator_name); 3048cb2e958SEdward Tomasz Napierala free(an); 3058cb2e958SEdward Tomasz Napierala } 3068cb2e958SEdward Tomasz Napierala 3078cb2e958SEdward Tomasz Napierala bool 3088cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag) 3098cb2e958SEdward Tomasz Napierala { 3108cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_names)) 3118cb2e958SEdward Tomasz Napierala return (false); 3128cb2e958SEdward Tomasz Napierala return (true); 3138cb2e958SEdward Tomasz Napierala } 3148cb2e958SEdward Tomasz Napierala 3158cb2e958SEdward Tomasz Napierala const struct auth_name * 3168cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name) 3178cb2e958SEdward Tomasz Napierala { 3188cb2e958SEdward Tomasz Napierala const struct auth_name *auth_name; 3198cb2e958SEdward Tomasz Napierala 3208cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) { 3218cb2e958SEdward Tomasz Napierala if (strcmp(auth_name->an_initator_name, name) == 0) 3228cb2e958SEdward Tomasz Napierala return (auth_name); 3238cb2e958SEdward Tomasz Napierala } 3248cb2e958SEdward Tomasz Napierala 3258cb2e958SEdward Tomasz Napierala return (NULL); 3268cb2e958SEdward Tomasz Napierala } 3278cb2e958SEdward Tomasz Napierala 328e867e162SEdward Tomasz Napierala int 329e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name) 330e867e162SEdward Tomasz Napierala { 331e867e162SEdward Tomasz Napierala if (!auth_name_defined(ag)) 332e867e162SEdward Tomasz Napierala return (0); 333e867e162SEdward Tomasz Napierala 334e867e162SEdward Tomasz Napierala if (auth_name_find(ag, initiator_name) == NULL) 335e867e162SEdward Tomasz Napierala return (1); 336e867e162SEdward Tomasz Napierala 337e867e162SEdward Tomasz Napierala return (0); 338e867e162SEdward Tomasz Napierala } 339e867e162SEdward Tomasz Napierala 3408cb2e958SEdward Tomasz Napierala const struct auth_portal * 3418cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal) 3428cb2e958SEdward Tomasz Napierala { 3438cb2e958SEdward Tomasz Napierala struct auth_portal *ap; 344073edb1cSAlexander Motin char *net, *mask, *str, *tmp; 345073edb1cSAlexander Motin int len, dm, m; 3468cb2e958SEdward Tomasz Napierala 3478cb2e958SEdward Tomasz Napierala ap = calloc(1, sizeof(*ap)); 3488cb2e958SEdward Tomasz Napierala if (ap == NULL) 3498cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 3508cb2e958SEdward Tomasz Napierala ap->ap_auth_group = ag; 3518cb2e958SEdward Tomasz Napierala ap->ap_initator_portal = checked_strdup(portal); 352073edb1cSAlexander Motin mask = str = checked_strdup(portal); 353073edb1cSAlexander Motin net = strsep(&mask, "/"); 354073edb1cSAlexander Motin if (net[0] == '[') 355073edb1cSAlexander Motin net++; 356073edb1cSAlexander Motin len = strlen(net); 357073edb1cSAlexander Motin if (len == 0) 358073edb1cSAlexander Motin goto error; 359073edb1cSAlexander Motin if (net[len - 1] == ']') 360073edb1cSAlexander Motin net[len - 1] = 0; 361073edb1cSAlexander Motin if (strchr(net, ':') != NULL) { 362073edb1cSAlexander Motin struct sockaddr_in6 *sin6 = 363073edb1cSAlexander Motin (struct sockaddr_in6 *)&ap->ap_sa; 364073edb1cSAlexander Motin 365073edb1cSAlexander Motin sin6->sin6_len = sizeof(*sin6); 366073edb1cSAlexander Motin sin6->sin6_family = AF_INET6; 367073edb1cSAlexander Motin if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0) 368073edb1cSAlexander Motin goto error; 369073edb1cSAlexander Motin dm = 128; 370073edb1cSAlexander Motin } else { 371073edb1cSAlexander Motin struct sockaddr_in *sin = 372073edb1cSAlexander Motin (struct sockaddr_in *)&ap->ap_sa; 373073edb1cSAlexander Motin 374073edb1cSAlexander Motin sin->sin_len = sizeof(*sin); 375073edb1cSAlexander Motin sin->sin_family = AF_INET; 376073edb1cSAlexander Motin if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0) 377073edb1cSAlexander Motin goto error; 378073edb1cSAlexander Motin dm = 32; 379073edb1cSAlexander Motin } 380073edb1cSAlexander Motin if (mask != NULL) { 381073edb1cSAlexander Motin m = strtol(mask, &tmp, 0); 382073edb1cSAlexander Motin if (m < 0 || m > dm || tmp[0] != 0) 383073edb1cSAlexander Motin goto error; 384073edb1cSAlexander Motin } else 385073edb1cSAlexander Motin m = dm; 386073edb1cSAlexander Motin ap->ap_mask = m; 387073edb1cSAlexander Motin free(str); 3888cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next); 3898cb2e958SEdward Tomasz Napierala return (ap); 390073edb1cSAlexander Motin 391073edb1cSAlexander Motin error: 392073edb1cSAlexander Motin log_errx(1, "Incorrect initiator portal '%s'", portal); 393073edb1cSAlexander Motin return (NULL); 3948cb2e958SEdward Tomasz Napierala } 3958cb2e958SEdward Tomasz Napierala 3968cb2e958SEdward Tomasz Napierala static void 3978cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap) 3988cb2e958SEdward Tomasz Napierala { 3998cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next); 4008cb2e958SEdward Tomasz Napierala 4018cb2e958SEdward Tomasz Napierala free(ap->ap_initator_portal); 4028cb2e958SEdward Tomasz Napierala free(ap); 4038cb2e958SEdward Tomasz Napierala } 4048cb2e958SEdward Tomasz Napierala 4058cb2e958SEdward Tomasz Napierala bool 4068cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag) 4078cb2e958SEdward Tomasz Napierala { 4088cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_portals)) 4098cb2e958SEdward Tomasz Napierala return (false); 4108cb2e958SEdward Tomasz Napierala return (true); 4118cb2e958SEdward Tomasz Napierala } 4128cb2e958SEdward Tomasz Napierala 4138cb2e958SEdward Tomasz Napierala const struct auth_portal * 414073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss) 4158cb2e958SEdward Tomasz Napierala { 416073edb1cSAlexander Motin const struct auth_portal *ap; 4175e46a066SAlexander Motin const uint8_t *a, *b; 418073edb1cSAlexander Motin int i; 4195e46a066SAlexander Motin uint8_t bmask; 4208cb2e958SEdward Tomasz Napierala 421073edb1cSAlexander Motin TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) { 422073edb1cSAlexander Motin if (ap->ap_sa.ss_family != ss->ss_family) 423073edb1cSAlexander Motin continue; 424073edb1cSAlexander Motin if (ss->ss_family == AF_INET) { 4255e46a066SAlexander Motin a = (const uint8_t *) 4265e46a066SAlexander Motin &((const struct sockaddr_in *)ss)->sin_addr; 4275e46a066SAlexander Motin b = (const uint8_t *) 4285e46a066SAlexander Motin &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr; 429073edb1cSAlexander Motin } else { 4305e46a066SAlexander Motin a = (const uint8_t *) 4315e46a066SAlexander Motin &((const struct sockaddr_in6 *)ss)->sin6_addr; 4325e46a066SAlexander Motin b = (const uint8_t *) 4335e46a066SAlexander Motin &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr; 434073edb1cSAlexander Motin } 435073edb1cSAlexander Motin for (i = 0; i < ap->ap_mask / 8; i++) { 436073edb1cSAlexander Motin if (a[i] != b[i]) 437073edb1cSAlexander Motin goto next; 438073edb1cSAlexander Motin } 439073edb1cSAlexander Motin if (ap->ap_mask % 8) { 440073edb1cSAlexander Motin bmask = 0xff << (8 - (ap->ap_mask % 8)); 441073edb1cSAlexander Motin if ((a[i] & bmask) != (b[i] & bmask)) 442073edb1cSAlexander Motin goto next; 443073edb1cSAlexander Motin } 444073edb1cSAlexander Motin return (ap); 445073edb1cSAlexander Motin next: 446073edb1cSAlexander Motin ; 4478cb2e958SEdward Tomasz Napierala } 4488cb2e958SEdward Tomasz Napierala 4498cb2e958SEdward Tomasz Napierala return (NULL); 4508cb2e958SEdward Tomasz Napierala } 4518cb2e958SEdward Tomasz Napierala 452e867e162SEdward Tomasz Napierala int 453e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa) 454e867e162SEdward Tomasz Napierala { 455e867e162SEdward Tomasz Napierala 456e867e162SEdward Tomasz Napierala if (!auth_portal_defined(ag)) 457e867e162SEdward Tomasz Napierala return (0); 458e867e162SEdward Tomasz Napierala 459e867e162SEdward Tomasz Napierala if (auth_portal_find(ag, sa) == NULL) 460e867e162SEdward Tomasz Napierala return (1); 461e867e162SEdward Tomasz Napierala 462e867e162SEdward Tomasz Napierala return (0); 463e867e162SEdward Tomasz Napierala } 464e867e162SEdward Tomasz Napierala 465009ea47eSEdward Tomasz Napierala struct auth_group * 466009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name) 467009ea47eSEdward Tomasz Napierala { 468009ea47eSEdward Tomasz Napierala struct auth_group *ag; 469009ea47eSEdward Tomasz Napierala 470009ea47eSEdward Tomasz Napierala if (name != NULL) { 471009ea47eSEdward Tomasz Napierala ag = auth_group_find(conf, name); 472009ea47eSEdward Tomasz Napierala if (ag != NULL) { 473009ea47eSEdward Tomasz Napierala log_warnx("duplicated auth-group \"%s\"", name); 474009ea47eSEdward Tomasz Napierala return (NULL); 475009ea47eSEdward Tomasz Napierala } 476009ea47eSEdward Tomasz Napierala } 477009ea47eSEdward Tomasz Napierala 478009ea47eSEdward Tomasz Napierala ag = calloc(1, sizeof(*ag)); 479009ea47eSEdward Tomasz Napierala if (ag == NULL) 480009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 481009ea47eSEdward Tomasz Napierala if (name != NULL) 482009ea47eSEdward Tomasz Napierala ag->ag_name = checked_strdup(name); 483009ea47eSEdward Tomasz Napierala TAILQ_INIT(&ag->ag_auths); 4848cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_names); 4858cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_portals); 486009ea47eSEdward Tomasz Napierala ag->ag_conf = conf; 487009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next); 488009ea47eSEdward Tomasz Napierala 489009ea47eSEdward Tomasz Napierala return (ag); 490009ea47eSEdward Tomasz Napierala } 491009ea47eSEdward Tomasz Napierala 492009ea47eSEdward Tomasz Napierala void 493009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag) 494009ea47eSEdward Tomasz Napierala { 4958cb2e958SEdward Tomasz Napierala struct auth *auth, *auth_tmp; 4968cb2e958SEdward Tomasz Napierala struct auth_name *auth_name, *auth_name_tmp; 4978cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal, *auth_portal_tmp; 498009ea47eSEdward Tomasz Napierala 499009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); 500009ea47eSEdward Tomasz Napierala 5018cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) 502009ea47eSEdward Tomasz Napierala auth_delete(auth); 5038cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) 5048cb2e958SEdward Tomasz Napierala auth_name_delete(auth_name); 5058cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, 5068cb2e958SEdward Tomasz Napierala auth_portal_tmp) 5078cb2e958SEdward Tomasz Napierala auth_portal_delete(auth_portal); 508009ea47eSEdward Tomasz Napierala free(ag->ag_name); 509009ea47eSEdward Tomasz Napierala free(ag); 510009ea47eSEdward Tomasz Napierala } 511009ea47eSEdward Tomasz Napierala 512009ea47eSEdward Tomasz Napierala struct auth_group * 513d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name) 514009ea47eSEdward Tomasz Napierala { 515009ea47eSEdward Tomasz Napierala struct auth_group *ag; 516009ea47eSEdward Tomasz Napierala 517009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 518009ea47eSEdward Tomasz Napierala if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 519009ea47eSEdward Tomasz Napierala return (ag); 520009ea47eSEdward Tomasz Napierala } 521009ea47eSEdward Tomasz Napierala 522009ea47eSEdward Tomasz Napierala return (NULL); 523009ea47eSEdward Tomasz Napierala } 524009ea47eSEdward Tomasz Napierala 525df9900fbSEdward Tomasz Napierala int 526bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str) 527df9900fbSEdward Tomasz Napierala { 528bd833b11SEdward Tomasz Napierala int type; 529df9900fbSEdward Tomasz Napierala 530df9900fbSEdward Tomasz Napierala if (strcmp(str, "none") == 0) { 531df9900fbSEdward Tomasz Napierala type = AG_TYPE_NO_AUTHENTICATION; 532e76ce448SEdward Tomasz Napierala } else if (strcmp(str, "deny") == 0) { 533e76ce448SEdward Tomasz Napierala type = AG_TYPE_DENY; 534df9900fbSEdward Tomasz Napierala } else if (strcmp(str, "chap") == 0) { 535df9900fbSEdward Tomasz Napierala type = AG_TYPE_CHAP; 536df9900fbSEdward Tomasz Napierala } else if (strcmp(str, "chap-mutual") == 0) { 537df9900fbSEdward Tomasz Napierala type = AG_TYPE_CHAP_MUTUAL; 538df9900fbSEdward Tomasz Napierala } else { 539df9900fbSEdward Tomasz Napierala if (ag->ag_name != NULL) 540df9900fbSEdward Tomasz Napierala log_warnx("invalid auth-type \"%s\" for auth-group " 541df9900fbSEdward Tomasz Napierala "\"%s\"", str, ag->ag_name); 542df9900fbSEdward Tomasz Napierala else 543df9900fbSEdward Tomasz Napierala log_warnx("invalid auth-type \"%s\" for target " 544df9900fbSEdward Tomasz Napierala "\"%s\"", str, ag->ag_target->t_name); 545df9900fbSEdward Tomasz Napierala return (1); 546df9900fbSEdward Tomasz Napierala } 547df9900fbSEdward Tomasz Napierala 548bd833b11SEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) { 549bd833b11SEdward Tomasz Napierala if (ag->ag_name != NULL) { 550df9900fbSEdward Tomasz Napierala log_warnx("cannot set auth-type to \"%s\" for " 551df9900fbSEdward Tomasz Napierala "auth-group \"%s\"; already has a different " 552df9900fbSEdward Tomasz Napierala "type", str, ag->ag_name); 553bd833b11SEdward Tomasz Napierala } else { 554df9900fbSEdward Tomasz Napierala log_warnx("cannot set auth-type to \"%s\" for target " 555df9900fbSEdward Tomasz Napierala "\"%s\"; already has a different type", 556df9900fbSEdward Tomasz Napierala str, ag->ag_target->t_name); 557bd833b11SEdward Tomasz Napierala } 558df9900fbSEdward Tomasz Napierala return (1); 559df9900fbSEdward Tomasz Napierala } 560df9900fbSEdward Tomasz Napierala 561bd833b11SEdward Tomasz Napierala ag->ag_type = type; 562bd833b11SEdward Tomasz Napierala 563bd833b11SEdward Tomasz Napierala return (0); 564df9900fbSEdward Tomasz Napierala } 565df9900fbSEdward Tomasz Napierala 566009ea47eSEdward Tomasz Napierala static struct portal * 567009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg) 568009ea47eSEdward Tomasz Napierala { 569009ea47eSEdward Tomasz Napierala struct portal *portal; 570009ea47eSEdward Tomasz Napierala 571009ea47eSEdward Tomasz Napierala portal = calloc(1, sizeof(*portal)); 572009ea47eSEdward Tomasz Napierala if (portal == NULL) 573009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 574009ea47eSEdward Tomasz Napierala TAILQ_INIT(&portal->p_targets); 575009ea47eSEdward Tomasz Napierala portal->p_portal_group = pg; 576009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 577009ea47eSEdward Tomasz Napierala return (portal); 578009ea47eSEdward Tomasz Napierala } 579009ea47eSEdward Tomasz Napierala 580009ea47eSEdward Tomasz Napierala static void 581009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal) 582009ea47eSEdward Tomasz Napierala { 58344a5953aSEdward Tomasz Napierala 584009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 58544a5953aSEdward Tomasz Napierala if (portal->p_ai != NULL) 586009ea47eSEdward Tomasz Napierala freeaddrinfo(portal->p_ai); 587009ea47eSEdward Tomasz Napierala free(portal->p_listen); 588009ea47eSEdward Tomasz Napierala free(portal); 589009ea47eSEdward Tomasz Napierala } 590009ea47eSEdward Tomasz Napierala 591009ea47eSEdward Tomasz Napierala struct portal_group * 592009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name) 593009ea47eSEdward Tomasz Napierala { 594009ea47eSEdward Tomasz Napierala struct portal_group *pg; 595009ea47eSEdward Tomasz Napierala 596009ea47eSEdward Tomasz Napierala pg = portal_group_find(conf, name); 597009ea47eSEdward Tomasz Napierala if (pg != NULL) { 598009ea47eSEdward Tomasz Napierala log_warnx("duplicated portal-group \"%s\"", name); 599009ea47eSEdward Tomasz Napierala return (NULL); 600009ea47eSEdward Tomasz Napierala } 601009ea47eSEdward Tomasz Napierala 602009ea47eSEdward Tomasz Napierala pg = calloc(1, sizeof(*pg)); 603009ea47eSEdward Tomasz Napierala if (pg == NULL) 604009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 605009ea47eSEdward Tomasz Napierala pg->pg_name = checked_strdup(name); 606009ea47eSEdward Tomasz Napierala TAILQ_INIT(&pg->pg_portals); 607009ea47eSEdward Tomasz Napierala pg->pg_conf = conf; 608009ea47eSEdward Tomasz Napierala conf->conf_last_portal_group_tag++; 609009ea47eSEdward Tomasz Napierala pg->pg_tag = conf->conf_last_portal_group_tag; 610009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 611009ea47eSEdward Tomasz Napierala 612009ea47eSEdward Tomasz Napierala return (pg); 613009ea47eSEdward Tomasz Napierala } 614009ea47eSEdward Tomasz Napierala 615009ea47eSEdward Tomasz Napierala void 616009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg) 617009ea47eSEdward Tomasz Napierala { 618009ea47eSEdward Tomasz Napierala struct portal *portal, *tmp; 619009ea47eSEdward Tomasz Napierala 620009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 621009ea47eSEdward Tomasz Napierala 622009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 623009ea47eSEdward Tomasz Napierala portal_delete(portal); 624009ea47eSEdward Tomasz Napierala free(pg->pg_name); 6256dbdbf82SEdward Tomasz Napierala free(pg->pg_redirection); 626009ea47eSEdward Tomasz Napierala free(pg); 627009ea47eSEdward Tomasz Napierala } 628009ea47eSEdward Tomasz Napierala 629009ea47eSEdward Tomasz Napierala struct portal_group * 630d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name) 631009ea47eSEdward Tomasz Napierala { 632009ea47eSEdward Tomasz Napierala struct portal_group *pg; 633009ea47eSEdward Tomasz Napierala 634009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 635009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, name) == 0) 636009ea47eSEdward Tomasz Napierala return (pg); 637009ea47eSEdward Tomasz Napierala } 638009ea47eSEdward Tomasz Napierala 639009ea47eSEdward Tomasz Napierala return (NULL); 640009ea47eSEdward Tomasz Napierala } 641009ea47eSEdward Tomasz Napierala 642829603e2SAlexander Motin static int 643829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai) 644009ea47eSEdward Tomasz Napierala { 645009ea47eSEdward Tomasz Napierala struct addrinfo hints; 646*1606ad8cSAlexander Motin char *str, *addr, *ch; 647009ea47eSEdward Tomasz Napierala const char *port; 648009ea47eSEdward Tomasz Napierala int error, colons = 0; 649009ea47eSEdward Tomasz Napierala 650*1606ad8cSAlexander Motin str = arg = strdup(arg); 651009ea47eSEdward Tomasz Napierala if (arg[0] == '[') { 652009ea47eSEdward Tomasz Napierala /* 653009ea47eSEdward Tomasz Napierala * IPv6 address in square brackets, perhaps with port. 654009ea47eSEdward Tomasz Napierala */ 655009ea47eSEdward Tomasz Napierala arg++; 656009ea47eSEdward Tomasz Napierala addr = strsep(&arg, "]"); 657829603e2SAlexander Motin if (arg == NULL) 658009ea47eSEdward Tomasz Napierala return (1); 659009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 660829603e2SAlexander Motin port = def_port; 661009ea47eSEdward Tomasz Napierala } else if (arg[0] == ':') { 662009ea47eSEdward Tomasz Napierala port = arg + 1; 663*1606ad8cSAlexander Motin } else { 664*1606ad8cSAlexander Motin free(str); 665009ea47eSEdward Tomasz Napierala return (1); 666*1606ad8cSAlexander Motin } 667009ea47eSEdward Tomasz Napierala } else { 668009ea47eSEdward Tomasz Napierala /* 669009ea47eSEdward Tomasz Napierala * Either IPv6 address without brackets - and without 670009ea47eSEdward Tomasz Napierala * a port - or IPv4 address. Just count the colons. 671009ea47eSEdward Tomasz Napierala */ 672009ea47eSEdward Tomasz Napierala for (ch = arg; *ch != '\0'; ch++) { 673009ea47eSEdward Tomasz Napierala if (*ch == ':') 674009ea47eSEdward Tomasz Napierala colons++; 675009ea47eSEdward Tomasz Napierala } 676009ea47eSEdward Tomasz Napierala if (colons > 1) { 677009ea47eSEdward Tomasz Napierala addr = arg; 678829603e2SAlexander Motin port = def_port; 679009ea47eSEdward Tomasz Napierala } else { 680009ea47eSEdward Tomasz Napierala addr = strsep(&arg, ":"); 681009ea47eSEdward Tomasz Napierala if (arg == NULL) 682829603e2SAlexander Motin port = def_port; 683009ea47eSEdward Tomasz Napierala else 684009ea47eSEdward Tomasz Napierala port = arg; 685009ea47eSEdward Tomasz Napierala } 686009ea47eSEdward Tomasz Napierala } 687009ea47eSEdward Tomasz Napierala 688009ea47eSEdward Tomasz Napierala memset(&hints, 0, sizeof(hints)); 689009ea47eSEdward Tomasz Napierala hints.ai_family = PF_UNSPEC; 690009ea47eSEdward Tomasz Napierala hints.ai_socktype = SOCK_STREAM; 691009ea47eSEdward Tomasz Napierala hints.ai_flags = AI_PASSIVE; 692829603e2SAlexander Motin error = getaddrinfo(addr, port, &hints, ai); 693*1606ad8cSAlexander Motin free(str); 694*1606ad8cSAlexander Motin return ((error != 0) ? 1 : 0); 695829603e2SAlexander Motin } 696009ea47eSEdward Tomasz Napierala 697829603e2SAlexander Motin int 698829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 699829603e2SAlexander Motin { 700829603e2SAlexander Motin struct portal *portal; 701829603e2SAlexander Motin 702829603e2SAlexander Motin portal = portal_new(pg); 703829603e2SAlexander Motin portal->p_listen = checked_strdup(value); 704829603e2SAlexander Motin portal->p_iser = iser; 705829603e2SAlexander Motin 706829603e2SAlexander Motin if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) { 707829603e2SAlexander Motin log_warnx("invalid listen address %s", portal->p_listen); 70844a5953aSEdward Tomasz Napierala portal_delete(portal); 709009ea47eSEdward Tomasz Napierala return (1); 710009ea47eSEdward Tomasz Napierala } 711009ea47eSEdward Tomasz Napierala 712009ea47eSEdward Tomasz Napierala /* 713009ea47eSEdward Tomasz Napierala * XXX: getaddrinfo(3) may return multiple addresses; we should turn 714009ea47eSEdward Tomasz Napierala * those into multiple portals. 715009ea47eSEdward Tomasz Napierala */ 716009ea47eSEdward Tomasz Napierala 717009ea47eSEdward Tomasz Napierala return (0); 718009ea47eSEdward Tomasz Napierala } 719009ea47eSEdward Tomasz Napierala 720829603e2SAlexander Motin int 721829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr) 722829603e2SAlexander Motin { 723829603e2SAlexander Motin struct isns *isns; 724829603e2SAlexander Motin 725829603e2SAlexander Motin isns = calloc(1, sizeof(*isns)); 726829603e2SAlexander Motin if (isns == NULL) 727829603e2SAlexander Motin log_err(1, "calloc"); 728829603e2SAlexander Motin isns->i_conf = conf; 729829603e2SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next); 730829603e2SAlexander Motin isns->i_addr = checked_strdup(addr); 731829603e2SAlexander Motin 732829603e2SAlexander Motin if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) { 733829603e2SAlexander Motin log_warnx("invalid iSNS address %s", isns->i_addr); 734829603e2SAlexander Motin isns_delete(isns); 735829603e2SAlexander Motin return (1); 736829603e2SAlexander Motin } 737829603e2SAlexander Motin 738829603e2SAlexander Motin /* 739829603e2SAlexander Motin * XXX: getaddrinfo(3) may return multiple addresses; we should turn 740829603e2SAlexander Motin * those into multiple servers. 741829603e2SAlexander Motin */ 742829603e2SAlexander Motin 743829603e2SAlexander Motin return (0); 744829603e2SAlexander Motin } 745829603e2SAlexander Motin 746829603e2SAlexander Motin void 747829603e2SAlexander Motin isns_delete(struct isns *isns) 748829603e2SAlexander Motin { 749829603e2SAlexander Motin 750829603e2SAlexander Motin TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next); 751829603e2SAlexander Motin free(isns->i_addr); 752829603e2SAlexander Motin if (isns->i_ai != NULL) 753829603e2SAlexander Motin freeaddrinfo(isns->i_ai); 754829603e2SAlexander Motin free(isns); 755829603e2SAlexander Motin } 756829603e2SAlexander Motin 757829603e2SAlexander Motin static int 758829603e2SAlexander Motin isns_do_connect(struct isns *isns) 759829603e2SAlexander Motin { 760829603e2SAlexander Motin int s; 761829603e2SAlexander Motin 762829603e2SAlexander Motin s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype, 763829603e2SAlexander Motin isns->i_ai->ai_protocol); 764829603e2SAlexander Motin if (s < 0) { 765829603e2SAlexander Motin log_warn("socket(2) failed for %s", isns->i_addr); 766829603e2SAlexander Motin return (-1); 767829603e2SAlexander Motin } 768829603e2SAlexander Motin if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) { 769829603e2SAlexander Motin log_warn("connect(2) failed for %s", isns->i_addr); 770829603e2SAlexander Motin close(s); 771829603e2SAlexander Motin return (-1); 772829603e2SAlexander Motin } 773829603e2SAlexander Motin return(s); 774829603e2SAlexander Motin } 775829603e2SAlexander Motin 776829603e2SAlexander Motin static int 777829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname) 778829603e2SAlexander Motin { 779829603e2SAlexander Motin struct conf *conf = isns->i_conf; 780829603e2SAlexander Motin struct target *target; 781829603e2SAlexander Motin struct portal *portal; 782829603e2SAlexander Motin struct portal_group *pg; 783829603e2SAlexander Motin struct isns_req *req; 784829603e2SAlexander Motin int res = 0; 785829603e2SAlexander Motin uint32_t error; 786829603e2SAlexander Motin 787829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT); 788829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 789829603e2SAlexander Motin isns_req_add_delim(req); 790829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 791829603e2SAlexander Motin isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */ 792829603e2SAlexander Motin isns_req_add_32(req, 6, conf->conf_isns_period); 793829603e2SAlexander Motin TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 794829603e2SAlexander Motin if (pg->pg_unassigned) 795829603e2SAlexander Motin continue; 796829603e2SAlexander Motin TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 797829603e2SAlexander Motin isns_req_add_addr(req, 16, portal->p_ai); 798829603e2SAlexander Motin isns_req_add_port(req, 17, portal->p_ai); 799829603e2SAlexander Motin } 800829603e2SAlexander Motin } 801829603e2SAlexander Motin TAILQ_FOREACH(target, &conf->conf_targets, t_next) { 802829603e2SAlexander Motin isns_req_add_str(req, 32, target->t_name); 803829603e2SAlexander Motin isns_req_add_32(req, 33, 1); /* 1 -- Target*/ 804829603e2SAlexander Motin if (target->t_alias != NULL) 805829603e2SAlexander Motin isns_req_add_str(req, 34, target->t_alias); 806829603e2SAlexander Motin pg = target->t_portal_group; 807829603e2SAlexander Motin isns_req_add_32(req, 51, pg->pg_tag); 808829603e2SAlexander Motin TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 809829603e2SAlexander Motin isns_req_add_addr(req, 49, portal->p_ai); 810829603e2SAlexander Motin isns_req_add_port(req, 50, portal->p_ai); 811829603e2SAlexander Motin } 812829603e2SAlexander Motin } 813829603e2SAlexander Motin res = isns_req_send(s, req); 814829603e2SAlexander Motin if (res < 0) { 815829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 816829603e2SAlexander Motin goto quit; 817829603e2SAlexander Motin } 818829603e2SAlexander Motin res = isns_req_receive(s, req); 819829603e2SAlexander Motin if (res < 0) { 820829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 821829603e2SAlexander Motin goto quit; 822829603e2SAlexander Motin } 823829603e2SAlexander Motin error = isns_req_get_status(req); 824829603e2SAlexander Motin if (error != 0) { 825829603e2SAlexander Motin log_warnx("iSNS register error %d for %s", error, isns->i_addr); 826829603e2SAlexander Motin res = -1; 827829603e2SAlexander Motin } 828829603e2SAlexander Motin quit: 829829603e2SAlexander Motin isns_req_free(req); 830829603e2SAlexander Motin return (res); 831829603e2SAlexander Motin } 832829603e2SAlexander Motin 833829603e2SAlexander Motin static int 834829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname) 835829603e2SAlexander Motin { 836829603e2SAlexander Motin struct conf *conf = isns->i_conf; 837829603e2SAlexander Motin struct isns_req *req; 838829603e2SAlexander Motin int res = 0; 839829603e2SAlexander Motin uint32_t error; 840829603e2SAlexander Motin 841829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT); 842829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 843829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 844829603e2SAlexander Motin isns_req_add_delim(req); 845829603e2SAlexander Motin isns_req_add(req, 2, 0, NULL); 846829603e2SAlexander Motin res = isns_req_send(s, req); 847829603e2SAlexander Motin if (res < 0) { 848829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 849829603e2SAlexander Motin goto quit; 850829603e2SAlexander Motin } 851829603e2SAlexander Motin res = isns_req_receive(s, req); 852829603e2SAlexander Motin if (res < 0) { 853829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 854829603e2SAlexander Motin goto quit; 855829603e2SAlexander Motin } 856829603e2SAlexander Motin error = isns_req_get_status(req); 857829603e2SAlexander Motin if (error != 0) { 858829603e2SAlexander Motin log_warnx("iSNS check error %d for %s", error, isns->i_addr); 859829603e2SAlexander Motin res = -1; 860829603e2SAlexander Motin } 861829603e2SAlexander Motin quit: 862829603e2SAlexander Motin isns_req_free(req); 863829603e2SAlexander Motin return (res); 864829603e2SAlexander Motin } 865829603e2SAlexander Motin 866829603e2SAlexander Motin static int 867829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname) 868829603e2SAlexander Motin { 869829603e2SAlexander Motin struct conf *conf = isns->i_conf; 870829603e2SAlexander Motin struct isns_req *req; 871829603e2SAlexander Motin int res = 0; 872829603e2SAlexander Motin uint32_t error; 873829603e2SAlexander Motin 874829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT); 875829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 876829603e2SAlexander Motin isns_req_add_delim(req); 877829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 878829603e2SAlexander Motin res = isns_req_send(s, req); 879829603e2SAlexander Motin if (res < 0) { 880829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 881829603e2SAlexander Motin goto quit; 882829603e2SAlexander Motin } 883829603e2SAlexander Motin res = isns_req_receive(s, req); 884829603e2SAlexander Motin if (res < 0) { 885829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 886829603e2SAlexander Motin goto quit; 887829603e2SAlexander Motin } 888829603e2SAlexander Motin error = isns_req_get_status(req); 889829603e2SAlexander Motin if (error != 0) { 890829603e2SAlexander Motin log_warnx("iSNS deregister error %d for %s", error, isns->i_addr); 891829603e2SAlexander Motin res = -1; 892829603e2SAlexander Motin } 893829603e2SAlexander Motin quit: 894829603e2SAlexander Motin isns_req_free(req); 895829603e2SAlexander Motin return (res); 896829603e2SAlexander Motin } 897829603e2SAlexander Motin 898829603e2SAlexander Motin void 899829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns) 900829603e2SAlexander Motin { 901829603e2SAlexander Motin struct conf *conf = isns->i_conf; 902d5cc4a4aSAlexander Motin int s; 903829603e2SAlexander Motin char hostname[256]; 904829603e2SAlexander Motin 905829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 906829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 907829603e2SAlexander Motin return; 908829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 909829603e2SAlexander Motin s = isns_do_connect(isns); 910829603e2SAlexander Motin if (s < 0) { 911829603e2SAlexander Motin set_timeout(0, false); 912829603e2SAlexander Motin return; 913829603e2SAlexander Motin } 914829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 915829603e2SAlexander Motin 916829603e2SAlexander Motin if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets)) 917829603e2SAlexander Motin oldisns = isns; 918d5cc4a4aSAlexander Motin isns_do_deregister(oldisns, s, hostname); 919d5cc4a4aSAlexander Motin isns_do_register(isns, s, hostname); 920829603e2SAlexander Motin close(s); 921829603e2SAlexander Motin set_timeout(0, false); 922829603e2SAlexander Motin } 923829603e2SAlexander Motin 924829603e2SAlexander Motin void 925829603e2SAlexander Motin isns_check(struct isns *isns) 926829603e2SAlexander Motin { 927829603e2SAlexander Motin struct conf *conf = isns->i_conf; 928829603e2SAlexander Motin int s, res; 929829603e2SAlexander Motin char hostname[256]; 930829603e2SAlexander Motin 931829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 932829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 933829603e2SAlexander Motin return; 934829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 935829603e2SAlexander Motin s = isns_do_connect(isns); 936829603e2SAlexander Motin if (s < 0) { 937829603e2SAlexander Motin set_timeout(0, false); 938829603e2SAlexander Motin return; 939829603e2SAlexander Motin } 940829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 941829603e2SAlexander Motin 942829603e2SAlexander Motin res = isns_do_check(isns, s, hostname); 943829603e2SAlexander Motin if (res < 0) { 944d5cc4a4aSAlexander Motin isns_do_deregister(isns, s, hostname); 945d5cc4a4aSAlexander Motin isns_do_register(isns, s, hostname); 946829603e2SAlexander Motin } 947829603e2SAlexander Motin close(s); 948829603e2SAlexander Motin set_timeout(0, false); 949829603e2SAlexander Motin } 950829603e2SAlexander Motin 951829603e2SAlexander Motin void 952829603e2SAlexander Motin isns_deregister(struct isns *isns) 953829603e2SAlexander Motin { 954829603e2SAlexander Motin struct conf *conf = isns->i_conf; 955d5cc4a4aSAlexander Motin int s; 956829603e2SAlexander Motin char hostname[256]; 957829603e2SAlexander Motin 958829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 959829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 960829603e2SAlexander Motin return; 961829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 962829603e2SAlexander Motin s = isns_do_connect(isns); 963829603e2SAlexander Motin if (s < 0) 964829603e2SAlexander Motin return; 965829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 966829603e2SAlexander Motin 967d5cc4a4aSAlexander Motin isns_do_deregister(isns, s, hostname); 968829603e2SAlexander Motin close(s); 969829603e2SAlexander Motin set_timeout(0, false); 970829603e2SAlexander Motin } 971829603e2SAlexander Motin 97205374883SEdward Tomasz Napierala int 973bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str) 97405374883SEdward Tomasz Napierala { 975bd833b11SEdward Tomasz Napierala int filter; 97605374883SEdward Tomasz Napierala 97705374883SEdward Tomasz Napierala if (strcmp(str, "none") == 0) { 97805374883SEdward Tomasz Napierala filter = PG_FILTER_NONE; 97905374883SEdward Tomasz Napierala } else if (strcmp(str, "portal") == 0) { 98005374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL; 98105374883SEdward Tomasz Napierala } else if (strcmp(str, "portal-name") == 0) { 98205374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL_NAME; 98305374883SEdward Tomasz Napierala } else if (strcmp(str, "portal-name-auth") == 0) { 98405374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL_NAME_AUTH; 98505374883SEdward Tomasz Napierala } else { 98605374883SEdward Tomasz Napierala log_warnx("invalid discovery-filter \"%s\" for portal-group " 98705374883SEdward Tomasz Napierala "\"%s\"; valid values are \"none\", \"portal\", " 98805374883SEdward Tomasz Napierala "\"portal-name\", and \"portal-name-auth\"", 98905374883SEdward Tomasz Napierala str, pg->pg_name); 99005374883SEdward Tomasz Napierala return (1); 99105374883SEdward Tomasz Napierala } 99205374883SEdward Tomasz Napierala 993bd833b11SEdward Tomasz Napierala if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN && 994bd833b11SEdward Tomasz Napierala pg->pg_discovery_filter != filter) { 99505374883SEdward Tomasz Napierala log_warnx("cannot set discovery-filter to \"%s\" for " 99605374883SEdward Tomasz Napierala "portal-group \"%s\"; already has a different " 99705374883SEdward Tomasz Napierala "value", str, pg->pg_name); 99805374883SEdward Tomasz Napierala return (1); 99905374883SEdward Tomasz Napierala } 100005374883SEdward Tomasz Napierala 1001bd833b11SEdward Tomasz Napierala pg->pg_discovery_filter = filter; 1002bd833b11SEdward Tomasz Napierala 1003bd833b11SEdward Tomasz Napierala return (0); 100405374883SEdward Tomasz Napierala } 100505374883SEdward Tomasz Napierala 10066dbdbf82SEdward Tomasz Napierala int 10076dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr) 10086dbdbf82SEdward Tomasz Napierala { 10096dbdbf82SEdward Tomasz Napierala 10106dbdbf82SEdward Tomasz Napierala if (pg->pg_redirection != NULL) { 10116dbdbf82SEdward Tomasz Napierala log_warnx("cannot set redirection to \"%s\" for " 10126dbdbf82SEdward Tomasz Napierala "portal-group \"%s\"; already defined", 10136dbdbf82SEdward Tomasz Napierala addr, pg->pg_name); 10146dbdbf82SEdward Tomasz Napierala return (1); 10156dbdbf82SEdward Tomasz Napierala } 10166dbdbf82SEdward Tomasz Napierala 10176dbdbf82SEdward Tomasz Napierala pg->pg_redirection = checked_strdup(addr); 10186dbdbf82SEdward Tomasz Napierala 10196dbdbf82SEdward Tomasz Napierala return (0); 10206dbdbf82SEdward Tomasz Napierala } 10216dbdbf82SEdward Tomasz Napierala 1022009ea47eSEdward Tomasz Napierala static bool 1023009ea47eSEdward Tomasz Napierala valid_hex(const char ch) 1024009ea47eSEdward Tomasz Napierala { 1025009ea47eSEdward Tomasz Napierala switch (ch) { 1026009ea47eSEdward Tomasz Napierala case '0': 1027009ea47eSEdward Tomasz Napierala case '1': 1028009ea47eSEdward Tomasz Napierala case '2': 1029009ea47eSEdward Tomasz Napierala case '3': 1030009ea47eSEdward Tomasz Napierala case '4': 1031009ea47eSEdward Tomasz Napierala case '5': 1032009ea47eSEdward Tomasz Napierala case '6': 1033009ea47eSEdward Tomasz Napierala case '7': 1034009ea47eSEdward Tomasz Napierala case '8': 1035009ea47eSEdward Tomasz Napierala case '9': 1036009ea47eSEdward Tomasz Napierala case 'a': 1037009ea47eSEdward Tomasz Napierala case 'A': 1038009ea47eSEdward Tomasz Napierala case 'b': 1039009ea47eSEdward Tomasz Napierala case 'B': 1040009ea47eSEdward Tomasz Napierala case 'c': 1041009ea47eSEdward Tomasz Napierala case 'C': 1042009ea47eSEdward Tomasz Napierala case 'd': 1043009ea47eSEdward Tomasz Napierala case 'D': 1044009ea47eSEdward Tomasz Napierala case 'e': 1045009ea47eSEdward Tomasz Napierala case 'E': 1046009ea47eSEdward Tomasz Napierala case 'f': 1047009ea47eSEdward Tomasz Napierala case 'F': 1048009ea47eSEdward Tomasz Napierala return (true); 1049009ea47eSEdward Tomasz Napierala default: 1050009ea47eSEdward Tomasz Napierala return (false); 1051009ea47eSEdward Tomasz Napierala } 1052009ea47eSEdward Tomasz Napierala } 1053009ea47eSEdward Tomasz Napierala 1054009ea47eSEdward Tomasz Napierala bool 1055009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name) 1056009ea47eSEdward Tomasz Napierala { 1057009ea47eSEdward Tomasz Napierala int i; 1058009ea47eSEdward Tomasz Napierala 1059009ea47eSEdward Tomasz Napierala if (strlen(name) >= MAX_NAME_LEN) { 1060009ea47eSEdward Tomasz Napierala log_warnx("overlong name for target \"%s\"; max length allowed " 1061009ea47eSEdward Tomasz Napierala "by iSCSI specification is %d characters", 1062009ea47eSEdward Tomasz Napierala name, MAX_NAME_LEN); 1063009ea47eSEdward Tomasz Napierala return (false); 1064009ea47eSEdward Tomasz Napierala } 1065009ea47eSEdward Tomasz Napierala 1066009ea47eSEdward Tomasz Napierala /* 1067009ea47eSEdward Tomasz Napierala * In the cases below, we don't return an error, just in case the admin 1068009ea47eSEdward Tomasz Napierala * was right, and we're wrong. 1069009ea47eSEdward Tomasz Napierala */ 1070009ea47eSEdward Tomasz Napierala if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 1071009ea47eSEdward Tomasz Napierala for (i = strlen("iqn."); name[i] != '\0'; i++) { 1072009ea47eSEdward Tomasz Napierala /* 1073009ea47eSEdward Tomasz Napierala * XXX: We should verify UTF-8 normalisation, as defined 1074009ea47eSEdward Tomasz Napierala * by 3.2.6.2: iSCSI Name Encoding. 1075009ea47eSEdward Tomasz Napierala */ 1076009ea47eSEdward Tomasz Napierala if (isalnum(name[i])) 1077009ea47eSEdward Tomasz Napierala continue; 1078009ea47eSEdward Tomasz Napierala if (name[i] == '-' || name[i] == '.' || name[i] == ':') 1079009ea47eSEdward Tomasz Napierala continue; 1080009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target name " 1081009ea47eSEdward Tomasz Napierala "\"%s\"; allowed characters are letters, digits, " 1082009ea47eSEdward Tomasz Napierala "'-', '.', and ':'", name[i], name); 1083009ea47eSEdward Tomasz Napierala break; 1084009ea47eSEdward Tomasz Napierala } 1085009ea47eSEdward Tomasz Napierala /* 1086009ea47eSEdward Tomasz Napierala * XXX: Check more stuff: valid date and a valid reversed domain. 1087009ea47eSEdward Tomasz Napierala */ 1088009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 1089009ea47eSEdward Tomasz Napierala if (strlen(name) != strlen("eui.") + 16) 1090009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"eui.\" " 1091009ea47eSEdward Tomasz Napierala "should be followed by exactly 16 hexadecimal " 1092009ea47eSEdward Tomasz Napierala "digits", name); 1093009ea47eSEdward Tomasz Napierala for (i = strlen("eui."); name[i] != '\0'; i++) { 1094009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 1095009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 1096009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 1097009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 1098009ea47eSEdward Tomasz Napierala break; 1099009ea47eSEdward Tomasz Napierala } 1100009ea47eSEdward Tomasz Napierala } 1101009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 1102009ea47eSEdward Tomasz Napierala if (strlen(name) > strlen("naa.") + 32) 1103009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"naa.\" " 1104009ea47eSEdward Tomasz Napierala "should be followed by at most 32 hexadecimal " 1105009ea47eSEdward Tomasz Napierala "digits", name); 1106009ea47eSEdward Tomasz Napierala for (i = strlen("naa."); name[i] != '\0'; i++) { 1107009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 1108009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 1109009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 1110009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 1111009ea47eSEdward Tomasz Napierala break; 1112009ea47eSEdward Tomasz Napierala } 1113009ea47eSEdward Tomasz Napierala } 1114009ea47eSEdward Tomasz Napierala } else { 1115009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; should start with " 1116009ea47eSEdward Tomasz Napierala "either \".iqn\", \"eui.\", or \"naa.\"", 1117009ea47eSEdward Tomasz Napierala name); 1118009ea47eSEdward Tomasz Napierala } 1119009ea47eSEdward Tomasz Napierala return (true); 1120009ea47eSEdward Tomasz Napierala } 1121009ea47eSEdward Tomasz Napierala 1122009ea47eSEdward Tomasz Napierala struct target * 1123f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name) 1124009ea47eSEdward Tomasz Napierala { 1125009ea47eSEdward Tomasz Napierala struct target *targ; 1126009ea47eSEdward Tomasz Napierala int i, len; 1127009ea47eSEdward Tomasz Napierala 1128f7ae5bf8SEdward Tomasz Napierala targ = target_find(conf, name); 1129009ea47eSEdward Tomasz Napierala if (targ != NULL) { 1130f7ae5bf8SEdward Tomasz Napierala log_warnx("duplicated target \"%s\"", name); 1131009ea47eSEdward Tomasz Napierala return (NULL); 1132009ea47eSEdward Tomasz Napierala } 1133f7ae5bf8SEdward Tomasz Napierala if (valid_iscsi_name(name) == false) { 1134f7ae5bf8SEdward Tomasz Napierala log_warnx("target name \"%s\" is invalid", name); 1135009ea47eSEdward Tomasz Napierala return (NULL); 1136009ea47eSEdward Tomasz Napierala } 1137009ea47eSEdward Tomasz Napierala targ = calloc(1, sizeof(*targ)); 1138009ea47eSEdward Tomasz Napierala if (targ == NULL) 1139009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1140f7ae5bf8SEdward Tomasz Napierala targ->t_name = checked_strdup(name); 1141009ea47eSEdward Tomasz Napierala 1142009ea47eSEdward Tomasz Napierala /* 1143009ea47eSEdward Tomasz Napierala * RFC 3722 requires us to normalize the name to lowercase. 1144009ea47eSEdward Tomasz Napierala */ 1145f7ae5bf8SEdward Tomasz Napierala len = strlen(name); 1146009ea47eSEdward Tomasz Napierala for (i = 0; i < len; i++) 1147f7ae5bf8SEdward Tomasz Napierala targ->t_name[i] = tolower(targ->t_name[i]); 1148009ea47eSEdward Tomasz Napierala 1149009ea47eSEdward Tomasz Napierala TAILQ_INIT(&targ->t_luns); 1150009ea47eSEdward Tomasz Napierala targ->t_conf = conf; 1151009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 1152009ea47eSEdward Tomasz Napierala 1153009ea47eSEdward Tomasz Napierala return (targ); 1154009ea47eSEdward Tomasz Napierala } 1155009ea47eSEdward Tomasz Napierala 1156009ea47eSEdward Tomasz Napierala void 1157009ea47eSEdward Tomasz Napierala target_delete(struct target *targ) 1158009ea47eSEdward Tomasz Napierala { 1159009ea47eSEdward Tomasz Napierala struct lun *lun, *tmp; 1160009ea47eSEdward Tomasz Napierala 1161009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 1162009ea47eSEdward Tomasz Napierala 1163009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp) 1164009ea47eSEdward Tomasz Napierala lun_delete(lun); 1165f7ae5bf8SEdward Tomasz Napierala free(targ->t_name); 11666dbdbf82SEdward Tomasz Napierala free(targ->t_redirection); 1167009ea47eSEdward Tomasz Napierala free(targ); 1168009ea47eSEdward Tomasz Napierala } 1169009ea47eSEdward Tomasz Napierala 1170009ea47eSEdward Tomasz Napierala struct target * 1171f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name) 1172009ea47eSEdward Tomasz Napierala { 1173009ea47eSEdward Tomasz Napierala struct target *targ; 1174009ea47eSEdward Tomasz Napierala 1175009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1176f7ae5bf8SEdward Tomasz Napierala if (strcasecmp(targ->t_name, name) == 0) 1177009ea47eSEdward Tomasz Napierala return (targ); 1178009ea47eSEdward Tomasz Napierala } 1179009ea47eSEdward Tomasz Napierala 1180009ea47eSEdward Tomasz Napierala return (NULL); 1181009ea47eSEdward Tomasz Napierala } 1182009ea47eSEdward Tomasz Napierala 11836dbdbf82SEdward Tomasz Napierala int 11846dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr) 11856dbdbf82SEdward Tomasz Napierala { 11866dbdbf82SEdward Tomasz Napierala 11876dbdbf82SEdward Tomasz Napierala if (target->t_redirection != NULL) { 11886dbdbf82SEdward Tomasz Napierala log_warnx("cannot set redirection to \"%s\" for " 11896dbdbf82SEdward Tomasz Napierala "target \"%s\"; already defined", 11906dbdbf82SEdward Tomasz Napierala addr, target->t_name); 11916dbdbf82SEdward Tomasz Napierala return (1); 11926dbdbf82SEdward Tomasz Napierala } 11936dbdbf82SEdward Tomasz Napierala 11946dbdbf82SEdward Tomasz Napierala target->t_redirection = checked_strdup(addr); 11956dbdbf82SEdward Tomasz Napierala 11966dbdbf82SEdward Tomasz Napierala return (0); 11976dbdbf82SEdward Tomasz Napierala } 11986dbdbf82SEdward Tomasz Napierala 1199009ea47eSEdward Tomasz Napierala struct lun * 1200009ea47eSEdward Tomasz Napierala lun_new(struct target *targ, int lun_id) 1201009ea47eSEdward Tomasz Napierala { 1202009ea47eSEdward Tomasz Napierala struct lun *lun; 1203009ea47eSEdward Tomasz Napierala 1204009ea47eSEdward Tomasz Napierala lun = lun_find(targ, lun_id); 1205009ea47eSEdward Tomasz Napierala if (lun != NULL) { 1206009ea47eSEdward Tomasz Napierala log_warnx("duplicated lun %d for target \"%s\"", 1207f7ae5bf8SEdward Tomasz Napierala lun_id, targ->t_name); 1208009ea47eSEdward Tomasz Napierala return (NULL); 1209009ea47eSEdward Tomasz Napierala } 1210009ea47eSEdward Tomasz Napierala 1211009ea47eSEdward Tomasz Napierala lun = calloc(1, sizeof(*lun)); 1212009ea47eSEdward Tomasz Napierala if (lun == NULL) 1213009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1214009ea47eSEdward Tomasz Napierala lun->l_lun = lun_id; 1215009ea47eSEdward Tomasz Napierala TAILQ_INIT(&lun->l_options); 1216009ea47eSEdward Tomasz Napierala lun->l_target = targ; 1217009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next); 1218009ea47eSEdward Tomasz Napierala 1219009ea47eSEdward Tomasz Napierala return (lun); 1220009ea47eSEdward Tomasz Napierala } 1221009ea47eSEdward Tomasz Napierala 1222009ea47eSEdward Tomasz Napierala void 1223009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun) 1224009ea47eSEdward Tomasz Napierala { 1225009ea47eSEdward Tomasz Napierala struct lun_option *lo, *tmp; 1226009ea47eSEdward Tomasz Napierala 1227009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next); 1228009ea47eSEdward Tomasz Napierala 1229009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp) 1230009ea47eSEdward Tomasz Napierala lun_option_delete(lo); 1231009ea47eSEdward Tomasz Napierala free(lun->l_backend); 1232009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 1233009ea47eSEdward Tomasz Napierala free(lun->l_path); 1234009ea47eSEdward Tomasz Napierala free(lun->l_serial); 1235009ea47eSEdward Tomasz Napierala free(lun); 1236009ea47eSEdward Tomasz Napierala } 1237009ea47eSEdward Tomasz Napierala 1238009ea47eSEdward Tomasz Napierala struct lun * 1239d6093026SEdward Tomasz Napierala lun_find(const struct target *targ, int lun_id) 1240009ea47eSEdward Tomasz Napierala { 1241009ea47eSEdward Tomasz Napierala struct lun *lun; 1242009ea47eSEdward Tomasz Napierala 1243009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 1244009ea47eSEdward Tomasz Napierala if (lun->l_lun == lun_id) 1245009ea47eSEdward Tomasz Napierala return (lun); 1246009ea47eSEdward Tomasz Napierala } 1247009ea47eSEdward Tomasz Napierala 1248009ea47eSEdward Tomasz Napierala return (NULL); 1249009ea47eSEdward Tomasz Napierala } 1250009ea47eSEdward Tomasz Napierala 1251009ea47eSEdward Tomasz Napierala void 1252009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value) 1253009ea47eSEdward Tomasz Napierala { 1254009ea47eSEdward Tomasz Napierala free(lun->l_backend); 1255009ea47eSEdward Tomasz Napierala lun->l_backend = checked_strdup(value); 1256009ea47eSEdward Tomasz Napierala } 1257009ea47eSEdward Tomasz Napierala 1258009ea47eSEdward Tomasz Napierala void 1259009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value) 1260009ea47eSEdward Tomasz Napierala { 1261009ea47eSEdward Tomasz Napierala 1262009ea47eSEdward Tomasz Napierala lun->l_blocksize = value; 1263009ea47eSEdward Tomasz Napierala } 1264009ea47eSEdward Tomasz Napierala 1265009ea47eSEdward Tomasz Napierala void 1266009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value) 1267009ea47eSEdward Tomasz Napierala { 1268009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 1269009ea47eSEdward Tomasz Napierala lun->l_device_id = checked_strdup(value); 1270009ea47eSEdward Tomasz Napierala } 1271009ea47eSEdward Tomasz Napierala 1272009ea47eSEdward Tomasz Napierala void 1273009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value) 1274009ea47eSEdward Tomasz Napierala { 1275009ea47eSEdward Tomasz Napierala free(lun->l_path); 1276009ea47eSEdward Tomasz Napierala lun->l_path = checked_strdup(value); 1277009ea47eSEdward Tomasz Napierala } 1278009ea47eSEdward Tomasz Napierala 1279009ea47eSEdward Tomasz Napierala void 1280009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value) 1281009ea47eSEdward Tomasz Napierala { 1282009ea47eSEdward Tomasz Napierala free(lun->l_serial); 1283009ea47eSEdward Tomasz Napierala lun->l_serial = checked_strdup(value); 1284009ea47eSEdward Tomasz Napierala } 1285009ea47eSEdward Tomasz Napierala 1286009ea47eSEdward Tomasz Napierala void 1287009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value) 1288009ea47eSEdward Tomasz Napierala { 1289009ea47eSEdward Tomasz Napierala 1290009ea47eSEdward Tomasz Napierala lun->l_size = value; 1291009ea47eSEdward Tomasz Napierala } 1292009ea47eSEdward Tomasz Napierala 1293009ea47eSEdward Tomasz Napierala void 1294009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value) 1295009ea47eSEdward Tomasz Napierala { 1296009ea47eSEdward Tomasz Napierala 1297009ea47eSEdward Tomasz Napierala lun->l_ctl_lun = value; 1298009ea47eSEdward Tomasz Napierala } 1299009ea47eSEdward Tomasz Napierala 1300009ea47eSEdward Tomasz Napierala struct lun_option * 1301009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value) 1302009ea47eSEdward Tomasz Napierala { 1303009ea47eSEdward Tomasz Napierala struct lun_option *lo; 1304009ea47eSEdward Tomasz Napierala 1305009ea47eSEdward Tomasz Napierala lo = lun_option_find(lun, name); 1306009ea47eSEdward Tomasz Napierala if (lo != NULL) { 1307009ea47eSEdward Tomasz Napierala log_warnx("duplicated lun option %s for lun %d, target \"%s\"", 1308f7ae5bf8SEdward Tomasz Napierala name, lun->l_lun, lun->l_target->t_name); 1309009ea47eSEdward Tomasz Napierala return (NULL); 1310009ea47eSEdward Tomasz Napierala } 1311009ea47eSEdward Tomasz Napierala 1312009ea47eSEdward Tomasz Napierala lo = calloc(1, sizeof(*lo)); 1313009ea47eSEdward Tomasz Napierala if (lo == NULL) 1314009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1315009ea47eSEdward Tomasz Napierala lo->lo_name = checked_strdup(name); 1316009ea47eSEdward Tomasz Napierala lo->lo_value = checked_strdup(value); 1317009ea47eSEdward Tomasz Napierala lo->lo_lun = lun; 1318009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next); 1319009ea47eSEdward Tomasz Napierala 1320009ea47eSEdward Tomasz Napierala return (lo); 1321009ea47eSEdward Tomasz Napierala } 1322009ea47eSEdward Tomasz Napierala 1323009ea47eSEdward Tomasz Napierala void 1324009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo) 1325009ea47eSEdward Tomasz Napierala { 1326009ea47eSEdward Tomasz Napierala 1327009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next); 1328009ea47eSEdward Tomasz Napierala 1329009ea47eSEdward Tomasz Napierala free(lo->lo_name); 1330009ea47eSEdward Tomasz Napierala free(lo->lo_value); 1331009ea47eSEdward Tomasz Napierala free(lo); 1332009ea47eSEdward Tomasz Napierala } 1333009ea47eSEdward Tomasz Napierala 1334009ea47eSEdward Tomasz Napierala struct lun_option * 1335d6093026SEdward Tomasz Napierala lun_option_find(const struct lun *lun, const char *name) 1336009ea47eSEdward Tomasz Napierala { 1337009ea47eSEdward Tomasz Napierala struct lun_option *lo; 1338009ea47eSEdward Tomasz Napierala 1339009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 1340009ea47eSEdward Tomasz Napierala if (strcmp(lo->lo_name, name) == 0) 1341009ea47eSEdward Tomasz Napierala return (lo); 1342009ea47eSEdward Tomasz Napierala } 1343009ea47eSEdward Tomasz Napierala 1344009ea47eSEdward Tomasz Napierala return (NULL); 1345009ea47eSEdward Tomasz Napierala } 1346009ea47eSEdward Tomasz Napierala 1347009ea47eSEdward Tomasz Napierala void 1348009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value) 1349009ea47eSEdward Tomasz Napierala { 1350009ea47eSEdward Tomasz Napierala 1351009ea47eSEdward Tomasz Napierala free(lo->lo_value); 1352009ea47eSEdward Tomasz Napierala lo->lo_value = checked_strdup(value); 1353009ea47eSEdward Tomasz Napierala } 1354009ea47eSEdward Tomasz Napierala 1355009ea47eSEdward Tomasz Napierala static struct connection * 1356073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host, 1357073edb1cSAlexander Motin const struct sockaddr *client_sa) 1358009ea47eSEdward Tomasz Napierala { 1359009ea47eSEdward Tomasz Napierala struct connection *conn; 1360009ea47eSEdward Tomasz Napierala 1361009ea47eSEdward Tomasz Napierala conn = calloc(1, sizeof(*conn)); 1362009ea47eSEdward Tomasz Napierala if (conn == NULL) 1363009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1364009ea47eSEdward Tomasz Napierala conn->conn_portal = portal; 1365009ea47eSEdward Tomasz Napierala conn->conn_socket = fd; 1366009ea47eSEdward Tomasz Napierala conn->conn_initiator_addr = checked_strdup(host); 1367073edb1cSAlexander Motin memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len); 1368009ea47eSEdward Tomasz Napierala 1369009ea47eSEdward Tomasz Napierala /* 1370009ea47eSEdward Tomasz Napierala * Default values, from RFC 3720, section 12. 1371009ea47eSEdward Tomasz Napierala */ 1372009ea47eSEdward Tomasz Napierala conn->conn_max_data_segment_length = 8192; 1373009ea47eSEdward Tomasz Napierala conn->conn_max_burst_length = 262144; 1374009ea47eSEdward Tomasz Napierala conn->conn_immediate_data = true; 1375009ea47eSEdward Tomasz Napierala 1376009ea47eSEdward Tomasz Napierala return (conn); 1377009ea47eSEdward Tomasz Napierala } 1378009ea47eSEdward Tomasz Napierala 1379009ea47eSEdward Tomasz Napierala #if 0 1380009ea47eSEdward Tomasz Napierala static void 1381009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf) 1382009ea47eSEdward Tomasz Napierala { 1383009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1384009ea47eSEdward Tomasz Napierala struct auth *auth; 13858cb2e958SEdward Tomasz Napierala struct auth_name *auth_name; 13868cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal; 1387009ea47eSEdward Tomasz Napierala struct portal_group *pg; 1388009ea47eSEdward Tomasz Napierala struct portal *portal; 1389009ea47eSEdward Tomasz Napierala struct target *targ; 1390009ea47eSEdward Tomasz Napierala struct lun *lun; 1391009ea47eSEdward Tomasz Napierala struct lun_option *lo; 1392009ea47eSEdward Tomasz Napierala 1393009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1394009ea47eSEdward Tomasz Napierala fprintf(stderr, "auth-group %s {\n", ag->ag_name); 1395009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) 1396009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t chap-mutual %s %s %s %s\n", 1397009ea47eSEdward Tomasz Napierala auth->a_user, auth->a_secret, 1398009ea47eSEdward Tomasz Napierala auth->a_mutual_user, auth->a_mutual_secret); 13998cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) 14008cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-name %s\n", 14018cb2e958SEdward Tomasz Napierala auth_name->an_initator_name); 14028cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next) 14038cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-portal %s\n", 14048cb2e958SEdward Tomasz Napierala auth_portal->an_initator_portal); 1405009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1406009ea47eSEdward Tomasz Napierala } 1407009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1408009ea47eSEdward Tomasz Napierala fprintf(stderr, "portal-group %s {\n", pg->pg_name); 1409009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1410009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t listen %s\n", portal->p_listen); 1411009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1412009ea47eSEdward Tomasz Napierala } 1413009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1414f7ae5bf8SEdward Tomasz Napierala fprintf(stderr, "target %s {\n", targ->t_name); 1415009ea47eSEdward Tomasz Napierala if (targ->t_alias != NULL) 1416009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t alias %s\n", targ->t_alias); 1417009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 1418009ea47eSEdward Tomasz Napierala fprintf(stderr, "\tlun %d {\n", lun->l_lun); 1419009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\tpath %s\n", lun->l_path); 1420009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lo, &lun->l_options, lo_next) 1421009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\toption %s %s\n", 1422009ea47eSEdward Tomasz Napierala lo->lo_name, lo->lo_value); 1423009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t}\n"); 1424009ea47eSEdward Tomasz Napierala } 1425009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1426009ea47eSEdward Tomasz Napierala } 1427009ea47eSEdward Tomasz Napierala } 1428009ea47eSEdward Tomasz Napierala #endif 1429009ea47eSEdward Tomasz Napierala 14302fabfaa5SEdward Tomasz Napierala static int 14312fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun) 14322fabfaa5SEdward Tomasz Napierala { 14332fabfaa5SEdward Tomasz Napierala const struct lun *lun2; 1434a19eebb1SEdward Tomasz Napierala const struct target *targ2; 14352fabfaa5SEdward Tomasz Napierala 14362fabfaa5SEdward Tomasz Napierala if (lun->l_backend == NULL) 14372fabfaa5SEdward Tomasz Napierala lun_set_backend(lun, "block"); 14382fabfaa5SEdward Tomasz Napierala if (strcmp(lun->l_backend, "block") == 0) { 14392fabfaa5SEdward Tomasz Napierala if (lun->l_path == NULL) { 14402fabfaa5SEdward Tomasz Napierala log_warnx("missing path for lun %d, target \"%s\"", 1441f7ae5bf8SEdward Tomasz Napierala lun->l_lun, lun->l_target->t_name); 14422fabfaa5SEdward Tomasz Napierala return (1); 14432fabfaa5SEdward Tomasz Napierala } 14442fabfaa5SEdward Tomasz Napierala } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 14452fabfaa5SEdward Tomasz Napierala if (lun->l_size == 0) { 14462fabfaa5SEdward Tomasz Napierala log_warnx("missing size for ramdisk-backed lun %d, " 1447f7ae5bf8SEdward Tomasz Napierala "target \"%s\"", lun->l_lun, lun->l_target->t_name); 14482fabfaa5SEdward Tomasz Napierala return (1); 14492fabfaa5SEdward Tomasz Napierala } 14502fabfaa5SEdward Tomasz Napierala if (lun->l_path != NULL) { 14512fabfaa5SEdward Tomasz Napierala log_warnx("path must not be specified " 14522fabfaa5SEdward Tomasz Napierala "for ramdisk-backed lun %d, target \"%s\"", 1453f7ae5bf8SEdward Tomasz Napierala lun->l_lun, lun->l_target->t_name); 14542fabfaa5SEdward Tomasz Napierala return (1); 14552fabfaa5SEdward Tomasz Napierala } 14562fabfaa5SEdward Tomasz Napierala } 14572fabfaa5SEdward Tomasz Napierala if (lun->l_lun < 0 || lun->l_lun > 255) { 14582fabfaa5SEdward Tomasz Napierala log_warnx("invalid lun number for lun %d, target \"%s\"; " 14592fabfaa5SEdward Tomasz Napierala "must be between 0 and 255", lun->l_lun, 1460f7ae5bf8SEdward Tomasz Napierala lun->l_target->t_name); 14612fabfaa5SEdward Tomasz Napierala return (1); 14622fabfaa5SEdward Tomasz Napierala } 14632fabfaa5SEdward Tomasz Napierala if (lun->l_blocksize == 0) { 14642fabfaa5SEdward Tomasz Napierala lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 14652fabfaa5SEdward Tomasz Napierala } else if (lun->l_blocksize < 0) { 14662fabfaa5SEdward Tomasz Napierala log_warnx("invalid blocksize for lun %d, target \"%s\"; " 1467f7ae5bf8SEdward Tomasz Napierala "must be larger than 0", lun->l_lun, lun->l_target->t_name); 14682fabfaa5SEdward Tomasz Napierala return (1); 14692fabfaa5SEdward Tomasz Napierala } 14702fabfaa5SEdward Tomasz Napierala if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 14712fabfaa5SEdward Tomasz Napierala log_warnx("invalid size for lun %d, target \"%s\"; " 14722fabfaa5SEdward Tomasz Napierala "must be multiple of blocksize", lun->l_lun, 1473f7ae5bf8SEdward Tomasz Napierala lun->l_target->t_name); 14742fabfaa5SEdward Tomasz Napierala return (1); 14752fabfaa5SEdward Tomasz Napierala } 1476a19eebb1SEdward Tomasz Napierala TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) { 1477a19eebb1SEdward Tomasz Napierala TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) { 1478a19eebb1SEdward Tomasz Napierala if (lun == lun2) 1479a19eebb1SEdward Tomasz Napierala continue; 1480a19eebb1SEdward Tomasz Napierala if (lun->l_path != NULL && lun2->l_path != NULL && 1481a19eebb1SEdward Tomasz Napierala strcmp(lun->l_path, lun2->l_path) == 0) { 1482a19eebb1SEdward Tomasz Napierala log_debugx("WARNING: path \"%s\" duplicated " 1483a19eebb1SEdward Tomasz Napierala "between lun %d, target \"%s\", and " 1484a19eebb1SEdward Tomasz Napierala "lun %d, target \"%s\"", lun->l_path, 1485f7ae5bf8SEdward Tomasz Napierala lun->l_lun, lun->l_target->t_name, 1486f7ae5bf8SEdward Tomasz Napierala lun2->l_lun, lun2->l_target->t_name); 1487a19eebb1SEdward Tomasz Napierala } 1488a19eebb1SEdward Tomasz Napierala } 1489a19eebb1SEdward Tomasz Napierala } 14902fabfaa5SEdward Tomasz Napierala 14912fabfaa5SEdward Tomasz Napierala return (0); 14922fabfaa5SEdward Tomasz Napierala } 14932fabfaa5SEdward Tomasz Napierala 1494009ea47eSEdward Tomasz Napierala int 1495009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf) 1496009ea47eSEdward Tomasz Napierala { 1497009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1498009ea47eSEdward Tomasz Napierala struct portal_group *pg; 1499009ea47eSEdward Tomasz Napierala struct target *targ; 15002fabfaa5SEdward Tomasz Napierala struct lun *lun; 15014e5c38f8SEdward Tomasz Napierala bool found; 15022fabfaa5SEdward Tomasz Napierala int error; 1503009ea47eSEdward Tomasz Napierala 1504009ea47eSEdward Tomasz Napierala if (conf->conf_pidfile_path == NULL) 1505009ea47eSEdward Tomasz Napierala conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1506009ea47eSEdward Tomasz Napierala 1507009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1508009ea47eSEdward Tomasz Napierala if (targ->t_auth_group == NULL) { 1509e7977549SEdward Tomasz Napierala targ->t_auth_group = auth_group_find(conf, 1510e7977549SEdward Tomasz Napierala "default"); 1511e7977549SEdward Tomasz Napierala assert(targ->t_auth_group != NULL); 1512009ea47eSEdward Tomasz Napierala } 1513009ea47eSEdward Tomasz Napierala if (targ->t_portal_group == NULL) { 1514009ea47eSEdward Tomasz Napierala targ->t_portal_group = portal_group_find(conf, 1515009ea47eSEdward Tomasz Napierala "default"); 1516009ea47eSEdward Tomasz Napierala assert(targ->t_portal_group != NULL); 1517009ea47eSEdward Tomasz Napierala } 15184e5c38f8SEdward Tomasz Napierala found = false; 1519009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 15202fabfaa5SEdward Tomasz Napierala error = conf_verify_lun(lun); 15212fabfaa5SEdward Tomasz Napierala if (error != 0) 15222fabfaa5SEdward Tomasz Napierala return (error); 15234e5c38f8SEdward Tomasz Napierala found = true; 1524009ea47eSEdward Tomasz Napierala } 15256dbdbf82SEdward Tomasz Napierala if (!found && targ->t_redirection == NULL) { 152619b59f13SEdward Tomasz Napierala log_warnx("no LUNs defined for target \"%s\"", 152719b59f13SEdward Tomasz Napierala targ->t_name); 1528009ea47eSEdward Tomasz Napierala } 15296dbdbf82SEdward Tomasz Napierala if (found && targ->t_redirection != NULL) { 15306dbdbf82SEdward Tomasz Napierala log_debugx("target \"%s\" contains luns, " 15316dbdbf82SEdward Tomasz Napierala " but configured for redirection", 15326dbdbf82SEdward Tomasz Napierala targ->t_name); 15336dbdbf82SEdward Tomasz Napierala } 1534009ea47eSEdward Tomasz Napierala } 1535009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1536009ea47eSEdward Tomasz Napierala assert(pg->pg_name != NULL); 1537009ea47eSEdward Tomasz Napierala if (pg->pg_discovery_auth_group == NULL) { 1538009ea47eSEdward Tomasz Napierala pg->pg_discovery_auth_group = 1539ccb1f04dSEdward Tomasz Napierala auth_group_find(conf, "default"); 1540009ea47eSEdward Tomasz Napierala assert(pg->pg_discovery_auth_group != NULL); 1541009ea47eSEdward Tomasz Napierala } 1542009ea47eSEdward Tomasz Napierala 154305374883SEdward Tomasz Napierala if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN) 154405374883SEdward Tomasz Napierala pg->pg_discovery_filter = PG_FILTER_NONE; 154505374883SEdward Tomasz Napierala 1546009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1547009ea47eSEdward Tomasz Napierala if (targ->t_portal_group == pg) 1548009ea47eSEdward Tomasz Napierala break; 1549009ea47eSEdward Tomasz Napierala } 15506dbdbf82SEdward Tomasz Napierala if (pg->pg_redirection != NULL) { 15516dbdbf82SEdward Tomasz Napierala if (targ != NULL) { 15526dbdbf82SEdward Tomasz Napierala log_debugx("portal-group \"%s\" assigned " 15536dbdbf82SEdward Tomasz Napierala "to target \"%s\", but configured " 15546dbdbf82SEdward Tomasz Napierala "for redirection", 15556dbdbf82SEdward Tomasz Napierala pg->pg_name, targ->t_name); 15566dbdbf82SEdward Tomasz Napierala } 15576dbdbf82SEdward Tomasz Napierala pg->pg_unassigned = false; 15586dbdbf82SEdward Tomasz Napierala } else if (targ != NULL) { 15596dbdbf82SEdward Tomasz Napierala pg->pg_unassigned = false; 15606dbdbf82SEdward Tomasz Napierala } else { 1561009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, "default") != 0) 1562009ea47eSEdward Tomasz Napierala log_warnx("portal-group \"%s\" not assigned " 1563009ea47eSEdward Tomasz Napierala "to any target", pg->pg_name); 1564009ea47eSEdward Tomasz Napierala pg->pg_unassigned = true; 15656dbdbf82SEdward Tomasz Napierala } 1566009ea47eSEdward Tomasz Napierala } 1567009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1568009ea47eSEdward Tomasz Napierala if (ag->ag_name == NULL) 1569009ea47eSEdward Tomasz Napierala assert(ag->ag_target != NULL); 1570009ea47eSEdward Tomasz Napierala else 1571009ea47eSEdward Tomasz Napierala assert(ag->ag_target == NULL); 1572009ea47eSEdward Tomasz Napierala 15734e5c38f8SEdward Tomasz Napierala found = false; 1574009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 15754e5c38f8SEdward Tomasz Napierala if (targ->t_auth_group == ag) { 15764e5c38f8SEdward Tomasz Napierala found = true; 1577009ea47eSEdward Tomasz Napierala break; 1578009ea47eSEdward Tomasz Napierala } 15794e5c38f8SEdward Tomasz Napierala } 15804e5c38f8SEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 15814e5c38f8SEdward Tomasz Napierala if (pg->pg_discovery_auth_group == ag) { 15824e5c38f8SEdward Tomasz Napierala found = true; 15834e5c38f8SEdward Tomasz Napierala break; 15844e5c38f8SEdward Tomasz Napierala } 15854e5c38f8SEdward Tomasz Napierala } 15864e5c38f8SEdward Tomasz Napierala if (!found && ag->ag_name != NULL && 1587ccb1f04dSEdward Tomasz Napierala strcmp(ag->ag_name, "default") != 0 && 1588009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-authentication") != 0 && 1589009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-access") != 0) { 1590009ea47eSEdward Tomasz Napierala log_warnx("auth-group \"%s\" not assigned " 1591009ea47eSEdward Tomasz Napierala "to any target", ag->ag_name); 1592009ea47eSEdward Tomasz Napierala } 1593009ea47eSEdward Tomasz Napierala } 1594009ea47eSEdward Tomasz Napierala 1595009ea47eSEdward Tomasz Napierala return (0); 1596009ea47eSEdward Tomasz Napierala } 1597009ea47eSEdward Tomasz Napierala 1598009ea47eSEdward Tomasz Napierala static int 1599009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf) 1600009ea47eSEdward Tomasz Napierala { 1601009ea47eSEdward Tomasz Napierala struct target *oldtarg, *newtarg, *tmptarg; 1602009ea47eSEdward Tomasz Napierala struct lun *oldlun, *newlun, *tmplun; 1603009ea47eSEdward Tomasz Napierala struct portal_group *oldpg, *newpg; 1604009ea47eSEdward Tomasz Napierala struct portal *oldp, *newp; 1605829603e2SAlexander Motin struct isns *oldns, *newns; 1606009ea47eSEdward Tomasz Napierala pid_t otherpid; 16078b94b583SAlexander Motin int changed, cumulated_error = 0, error, sockbuf; 1608009ea47eSEdward Tomasz Napierala int one = 1; 1609009ea47eSEdward Tomasz Napierala 1610009ea47eSEdward Tomasz Napierala if (oldconf->conf_debug != newconf->conf_debug) { 1611009ea47eSEdward Tomasz Napierala log_debugx("changing debug level to %d", newconf->conf_debug); 1612009ea47eSEdward Tomasz Napierala log_init(newconf->conf_debug); 1613009ea47eSEdward Tomasz Napierala } 1614009ea47eSEdward Tomasz Napierala 1615009ea47eSEdward Tomasz Napierala if (oldconf->conf_pidfh != NULL) { 1616009ea47eSEdward Tomasz Napierala assert(oldconf->conf_pidfile_path != NULL); 1617009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfile_path != NULL && 1618009ea47eSEdward Tomasz Napierala strcmp(oldconf->conf_pidfile_path, 1619009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path) == 0) { 1620009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = oldconf->conf_pidfh; 1621009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1622009ea47eSEdward Tomasz Napierala } else { 1623009ea47eSEdward Tomasz Napierala log_debugx("removing pidfile %s", 1624009ea47eSEdward Tomasz Napierala oldconf->conf_pidfile_path); 1625009ea47eSEdward Tomasz Napierala pidfile_remove(oldconf->conf_pidfh); 1626009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1627009ea47eSEdward Tomasz Napierala } 1628009ea47eSEdward Tomasz Napierala } 1629009ea47eSEdward Tomasz Napierala 1630009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1631009ea47eSEdward Tomasz Napierala log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1632009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = 1633009ea47eSEdward Tomasz Napierala pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1634009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL) { 1635009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 1636009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 1637009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 1638009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 1639009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path); 1640009ea47eSEdward Tomasz Napierala } 1641009ea47eSEdward Tomasz Napierala } 1642009ea47eSEdward Tomasz Napierala 1643829603e2SAlexander Motin /* Deregister on removed iSNS servers. */ 1644829603e2SAlexander Motin TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1645829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1646829603e2SAlexander Motin if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1647829603e2SAlexander Motin break; 1648829603e2SAlexander Motin } 1649829603e2SAlexander Motin if (newns == NULL) 1650829603e2SAlexander Motin isns_deregister(oldns); 1651829603e2SAlexander Motin } 1652829603e2SAlexander Motin 1653d5e316e5SEdward Tomasz Napierala /* 1654d5e316e5SEdward Tomasz Napierala * XXX: If target or lun removal fails, we should somehow "move" 1655d5e316e5SEdward Tomasz Napierala * the old lun or target into newconf, so that subsequent 165661a2a354SEdward Tomasz Napierala * conf_apply() would try to remove them again. That would 165761a2a354SEdward Tomasz Napierala * be somewhat hairy, though, and lun deletion failures don't 165861a2a354SEdward Tomasz Napierala * really happen, so leave it as it is for now. 1659d5e316e5SEdward Tomasz Napierala */ 1660009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) { 1661009ea47eSEdward Tomasz Napierala /* 1662009ea47eSEdward Tomasz Napierala * First, remove any targets present in the old configuration 1663009ea47eSEdward Tomasz Napierala * and missing in the new one. 1664009ea47eSEdward Tomasz Napierala */ 1665f7ae5bf8SEdward Tomasz Napierala newtarg = target_find(newconf, oldtarg->t_name); 1666009ea47eSEdward Tomasz Napierala if (newtarg == NULL) { 16676de78f9fSAlexander Motin error = kernel_port_remove(oldtarg); 16686de78f9fSAlexander Motin if (error != 0) { 16696de78f9fSAlexander Motin log_warnx("failed to remove target %s", 16706de78f9fSAlexander Motin oldtarg->t_name); 16716de78f9fSAlexander Motin /* 16726de78f9fSAlexander Motin * XXX: Uncomment after fixing the root cause. 16736de78f9fSAlexander Motin * 16746de78f9fSAlexander Motin * cumulated_error++; 16756de78f9fSAlexander Motin */ 16766de78f9fSAlexander Motin } 1677009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, 1678009ea47eSEdward Tomasz Napierala tmplun) { 1679cadf3831SEdward Tomasz Napierala log_debugx("target %s not found in new " 1680cadf3831SEdward Tomasz Napierala "configuration; removing its lun %d, " 1681009ea47eSEdward Tomasz Napierala "backed by CTL lun %d", 1682f7ae5bf8SEdward Tomasz Napierala oldtarg->t_name, oldlun->l_lun, 1683009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1684009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1685009ea47eSEdward Tomasz Napierala if (error != 0) { 1686009ea47eSEdward Tomasz Napierala log_warnx("failed to remove lun %d, " 1687009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1688f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1689009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1690009ea47eSEdward Tomasz Napierala cumulated_error++; 1691009ea47eSEdward Tomasz Napierala } 1692009ea47eSEdward Tomasz Napierala } 1693009ea47eSEdward Tomasz Napierala continue; 1694009ea47eSEdward Tomasz Napierala } 1695009ea47eSEdward Tomasz Napierala 1696009ea47eSEdward Tomasz Napierala /* 1697009ea47eSEdward Tomasz Napierala * Second, remove any LUNs present in the old target 1698009ea47eSEdward Tomasz Napierala * and missing in the new one. 1699009ea47eSEdward Tomasz Napierala */ 1700009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) { 1701009ea47eSEdward Tomasz Napierala newlun = lun_find(newtarg, oldlun->l_lun); 1702009ea47eSEdward Tomasz Napierala if (newlun == NULL) { 1703009ea47eSEdward Tomasz Napierala log_debugx("lun %d, target %s, CTL lun %d " 1704cadf3831SEdward Tomasz Napierala "not found in new configuration; " 1705f7ae5bf8SEdward Tomasz Napierala "removing", oldlun->l_lun, oldtarg->t_name, 1706009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1707009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1708009ea47eSEdward Tomasz Napierala if (error != 0) { 1709009ea47eSEdward Tomasz Napierala log_warnx("failed to remove lun %d, " 1710009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1711f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1712009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1713009ea47eSEdward Tomasz Napierala cumulated_error++; 1714009ea47eSEdward Tomasz Napierala } 1715009ea47eSEdward Tomasz Napierala continue; 1716009ea47eSEdward Tomasz Napierala } 1717009ea47eSEdward Tomasz Napierala 1718009ea47eSEdward Tomasz Napierala /* 1719009ea47eSEdward Tomasz Napierala * Also remove the LUNs changed by more than size. 1720009ea47eSEdward Tomasz Napierala */ 1721009ea47eSEdward Tomasz Napierala changed = 0; 1722009ea47eSEdward Tomasz Napierala assert(oldlun->l_backend != NULL); 1723009ea47eSEdward Tomasz Napierala assert(newlun->l_backend != NULL); 1724009ea47eSEdward Tomasz Napierala if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1725009ea47eSEdward Tomasz Napierala log_debugx("backend for lun %d, target %s, " 1726009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1727f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1728009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1729009ea47eSEdward Tomasz Napierala changed = 1; 1730009ea47eSEdward Tomasz Napierala } 1731009ea47eSEdward Tomasz Napierala if (oldlun->l_blocksize != newlun->l_blocksize) { 1732009ea47eSEdward Tomasz Napierala log_debugx("blocksize for lun %d, target %s, " 1733009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1734f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1735009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1736009ea47eSEdward Tomasz Napierala changed = 1; 1737009ea47eSEdward Tomasz Napierala } 1738009ea47eSEdward Tomasz Napierala if (newlun->l_device_id != NULL && 1739009ea47eSEdward Tomasz Napierala (oldlun->l_device_id == NULL || 1740009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_device_id, newlun->l_device_id) != 1741009ea47eSEdward Tomasz Napierala 0)) { 1742009ea47eSEdward Tomasz Napierala log_debugx("device-id for lun %d, target %s, " 1743009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1744f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1745009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1746009ea47eSEdward Tomasz Napierala changed = 1; 1747009ea47eSEdward Tomasz Napierala } 1748009ea47eSEdward Tomasz Napierala if (newlun->l_path != NULL && 1749009ea47eSEdward Tomasz Napierala (oldlun->l_path == NULL || 1750009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1751009ea47eSEdward Tomasz Napierala log_debugx("path for lun %d, target %s, " 1752009ea47eSEdward Tomasz Napierala "CTL lun %d, changed; removing", 1753f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1754009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1755009ea47eSEdward Tomasz Napierala changed = 1; 1756009ea47eSEdward Tomasz Napierala } 1757009ea47eSEdward Tomasz Napierala if (newlun->l_serial != NULL && 1758009ea47eSEdward Tomasz Napierala (oldlun->l_serial == NULL || 1759009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1760009ea47eSEdward Tomasz Napierala log_debugx("serial for lun %d, target %s, " 1761009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1762f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1763009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1764009ea47eSEdward Tomasz Napierala changed = 1; 1765009ea47eSEdward Tomasz Napierala } 1766009ea47eSEdward Tomasz Napierala if (changed) { 1767009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1768009ea47eSEdward Tomasz Napierala if (error != 0) { 1769009ea47eSEdward Tomasz Napierala log_warnx("failed to remove lun %d, " 1770009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1771f7ae5bf8SEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_name, 1772009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1773009ea47eSEdward Tomasz Napierala cumulated_error++; 1774009ea47eSEdward Tomasz Napierala } 1775009ea47eSEdward Tomasz Napierala lun_delete(oldlun); 1776009ea47eSEdward Tomasz Napierala continue; 1777009ea47eSEdward Tomasz Napierala } 1778009ea47eSEdward Tomasz Napierala 1779009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1780009ea47eSEdward Tomasz Napierala } 1781009ea47eSEdward Tomasz Napierala } 1782009ea47eSEdward Tomasz Napierala 1783009ea47eSEdward Tomasz Napierala /* 1784009ea47eSEdward Tomasz Napierala * Now add new targets or modify existing ones. 1785009ea47eSEdward Tomasz Napierala */ 1786009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) { 1787f7ae5bf8SEdward Tomasz Napierala oldtarg = target_find(oldconf, newtarg->t_name); 1788009ea47eSEdward Tomasz Napierala 1789d5e316e5SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(newlun, &newtarg->t_luns, l_next, tmplun) { 1790009ea47eSEdward Tomasz Napierala if (oldtarg != NULL) { 1791009ea47eSEdward Tomasz Napierala oldlun = lun_find(oldtarg, newlun->l_lun); 1792009ea47eSEdward Tomasz Napierala if (oldlun != NULL) { 17938cf98331SAlexander Motin if (newlun->l_size != oldlun->l_size || 17948cf98331SAlexander Motin newlun->l_size == 0) { 1795009ea47eSEdward Tomasz Napierala log_debugx("resizing lun %d, " 1796009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1797009ea47eSEdward Tomasz Napierala newlun->l_lun, 1798f7ae5bf8SEdward Tomasz Napierala newtarg->t_name, 1799009ea47eSEdward Tomasz Napierala newlun->l_ctl_lun); 1800009ea47eSEdward Tomasz Napierala error = 1801009ea47eSEdward Tomasz Napierala kernel_lun_resize(newlun); 1802009ea47eSEdward Tomasz Napierala if (error != 0) { 1803009ea47eSEdward Tomasz Napierala log_warnx("failed to " 1804009ea47eSEdward Tomasz Napierala "resize lun %d, " 1805009ea47eSEdward Tomasz Napierala "target %s, " 1806009ea47eSEdward Tomasz Napierala "CTL lun %d", 1807009ea47eSEdward Tomasz Napierala newlun->l_lun, 1808f7ae5bf8SEdward Tomasz Napierala newtarg->t_name, 1809009ea47eSEdward Tomasz Napierala newlun->l_lun); 1810009ea47eSEdward Tomasz Napierala cumulated_error++; 1811009ea47eSEdward Tomasz Napierala } 1812009ea47eSEdward Tomasz Napierala } 1813009ea47eSEdward Tomasz Napierala continue; 1814009ea47eSEdward Tomasz Napierala } 1815009ea47eSEdward Tomasz Napierala } 1816009ea47eSEdward Tomasz Napierala log_debugx("adding lun %d, target %s", 1817f7ae5bf8SEdward Tomasz Napierala newlun->l_lun, newtarg->t_name); 1818009ea47eSEdward Tomasz Napierala error = kernel_lun_add(newlun); 1819009ea47eSEdward Tomasz Napierala if (error != 0) { 1820009ea47eSEdward Tomasz Napierala log_warnx("failed to add lun %d, target %s", 1821f7ae5bf8SEdward Tomasz Napierala newlun->l_lun, newtarg->t_name); 1822d5e316e5SEdward Tomasz Napierala lun_delete(newlun); 1823009ea47eSEdward Tomasz Napierala cumulated_error++; 1824009ea47eSEdward Tomasz Napierala } 1825009ea47eSEdward Tomasz Napierala } 1826e48bb73aSEdward Tomasz Napierala if (oldtarg == NULL) { 1827e48bb73aSEdward Tomasz Napierala error = kernel_port_add(newtarg); 1828e48bb73aSEdward Tomasz Napierala if (error != 0) { 1829e48bb73aSEdward Tomasz Napierala log_warnx("failed to add target %s", 18300dd2bee4SEdward Tomasz Napierala newtarg->t_name); 1831e48bb73aSEdward Tomasz Napierala /* 1832e48bb73aSEdward Tomasz Napierala * XXX: Uncomment after fixing the root cause. 1833e48bb73aSEdward Tomasz Napierala * 1834e48bb73aSEdward Tomasz Napierala * cumulated_error++; 1835e48bb73aSEdward Tomasz Napierala */ 1836e48bb73aSEdward Tomasz Napierala } 1837e48bb73aSEdward Tomasz Napierala } 1838009ea47eSEdward Tomasz Napierala } 1839009ea47eSEdward Tomasz Napierala 1840009ea47eSEdward Tomasz Napierala /* 1841009ea47eSEdward Tomasz Napierala * Go through the new portals, opening the sockets as neccessary. 1842009ea47eSEdward Tomasz Napierala */ 1843009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1844009ea47eSEdward Tomasz Napierala if (newpg->pg_unassigned) { 1845009ea47eSEdward Tomasz Napierala log_debugx("not listening on portal-group \"%s\", " 1846009ea47eSEdward Tomasz Napierala "not assigned to any target", 1847009ea47eSEdward Tomasz Napierala newpg->pg_name); 1848009ea47eSEdward Tomasz Napierala continue; 1849009ea47eSEdward Tomasz Napierala } 1850009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 1851009ea47eSEdward Tomasz Napierala /* 1852009ea47eSEdward Tomasz Napierala * Try to find already open portal and reuse 1853009ea47eSEdward Tomasz Napierala * the listening socket. We don't care about 1854009ea47eSEdward Tomasz Napierala * what portal or portal group that was, what 1855009ea47eSEdward Tomasz Napierala * matters is the listening address. 1856009ea47eSEdward Tomasz Napierala */ 1857009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 1858009ea47eSEdward Tomasz Napierala pg_next) { 1859009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, 1860009ea47eSEdward Tomasz Napierala p_next) { 1861009ea47eSEdward Tomasz Napierala if (strcmp(newp->p_listen, 1862009ea47eSEdward Tomasz Napierala oldp->p_listen) == 0 && 1863009ea47eSEdward Tomasz Napierala oldp->p_socket > 0) { 1864009ea47eSEdward Tomasz Napierala newp->p_socket = 1865009ea47eSEdward Tomasz Napierala oldp->p_socket; 1866009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 1867009ea47eSEdward Tomasz Napierala break; 1868009ea47eSEdward Tomasz Napierala } 1869009ea47eSEdward Tomasz Napierala } 1870009ea47eSEdward Tomasz Napierala } 1871009ea47eSEdward Tomasz Napierala if (newp->p_socket > 0) { 1872009ea47eSEdward Tomasz Napierala /* 1873009ea47eSEdward Tomasz Napierala * We're done with this portal. 1874009ea47eSEdward Tomasz Napierala */ 1875009ea47eSEdward Tomasz Napierala continue; 1876009ea47eSEdward Tomasz Napierala } 1877009ea47eSEdward Tomasz Napierala 1878009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1879ba3a2d31SEdward Tomasz Napierala if (proxy_mode) { 18808cab2ed4SEdward Tomasz Napierala newpg->pg_conf->conf_portal_id++; 18818cab2ed4SEdward Tomasz Napierala newp->p_id = newpg->pg_conf->conf_portal_id; 18828cab2ed4SEdward Tomasz Napierala log_debugx("listening on %s, portal-group " 18838cab2ed4SEdward Tomasz Napierala "\"%s\", portal id %d, using ICL proxy", 18848cab2ed4SEdward Tomasz Napierala newp->p_listen, newpg->pg_name, newp->p_id); 18858cab2ed4SEdward Tomasz Napierala kernel_listen(newp->p_ai, newp->p_iser, 18868cab2ed4SEdward Tomasz Napierala newp->p_id); 1887ba3a2d31SEdward Tomasz Napierala continue; 1888ba3a2d31SEdward Tomasz Napierala } 1889ba3a2d31SEdward Tomasz Napierala #endif 1890ba3a2d31SEdward Tomasz Napierala assert(proxy_mode == false); 1891009ea47eSEdward Tomasz Napierala assert(newp->p_iser == false); 1892009ea47eSEdward Tomasz Napierala 1893009ea47eSEdward Tomasz Napierala log_debugx("listening on %s, portal-group \"%s\"", 1894009ea47eSEdward Tomasz Napierala newp->p_listen, newpg->pg_name); 1895009ea47eSEdward Tomasz Napierala newp->p_socket = socket(newp->p_ai->ai_family, 1896009ea47eSEdward Tomasz Napierala newp->p_ai->ai_socktype, 1897009ea47eSEdward Tomasz Napierala newp->p_ai->ai_protocol); 1898009ea47eSEdward Tomasz Napierala if (newp->p_socket < 0) { 1899009ea47eSEdward Tomasz Napierala log_warn("socket(2) failed for %s", 1900009ea47eSEdward Tomasz Napierala newp->p_listen); 1901009ea47eSEdward Tomasz Napierala cumulated_error++; 1902009ea47eSEdward Tomasz Napierala continue; 1903009ea47eSEdward Tomasz Napierala } 19048b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 19058b94b583SAlexander Motin if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF, 19068b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 19078b94b583SAlexander Motin log_warn("setsockopt(SO_RCVBUF) failed " 19088b94b583SAlexander Motin "for %s", newp->p_listen); 19098b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 19108b94b583SAlexander Motin if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF, 19118b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 19128b94b583SAlexander Motin log_warn("setsockopt(SO_SNDBUF) failed " 19138b94b583SAlexander Motin "for %s", newp->p_listen); 1914009ea47eSEdward Tomasz Napierala error = setsockopt(newp->p_socket, SOL_SOCKET, 1915009ea47eSEdward Tomasz Napierala SO_REUSEADDR, &one, sizeof(one)); 1916009ea47eSEdward Tomasz Napierala if (error != 0) { 1917009ea47eSEdward Tomasz Napierala log_warn("setsockopt(SO_REUSEADDR) failed " 1918009ea47eSEdward Tomasz Napierala "for %s", newp->p_listen); 1919009ea47eSEdward Tomasz Napierala close(newp->p_socket); 1920009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 1921009ea47eSEdward Tomasz Napierala cumulated_error++; 1922009ea47eSEdward Tomasz Napierala continue; 1923009ea47eSEdward Tomasz Napierala } 1924009ea47eSEdward Tomasz Napierala error = bind(newp->p_socket, newp->p_ai->ai_addr, 1925009ea47eSEdward Tomasz Napierala newp->p_ai->ai_addrlen); 1926009ea47eSEdward Tomasz Napierala if (error != 0) { 1927009ea47eSEdward Tomasz Napierala log_warn("bind(2) failed for %s", 1928009ea47eSEdward Tomasz Napierala newp->p_listen); 1929009ea47eSEdward Tomasz Napierala close(newp->p_socket); 1930009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 1931009ea47eSEdward Tomasz Napierala cumulated_error++; 1932009ea47eSEdward Tomasz Napierala continue; 1933009ea47eSEdward Tomasz Napierala } 1934009ea47eSEdward Tomasz Napierala error = listen(newp->p_socket, -1); 1935009ea47eSEdward Tomasz Napierala if (error != 0) { 1936009ea47eSEdward Tomasz Napierala log_warn("listen(2) failed for %s", 1937009ea47eSEdward Tomasz Napierala newp->p_listen); 1938009ea47eSEdward Tomasz Napierala close(newp->p_socket); 1939009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 1940009ea47eSEdward Tomasz Napierala cumulated_error++; 1941009ea47eSEdward Tomasz Napierala continue; 1942009ea47eSEdward Tomasz Napierala } 1943009ea47eSEdward Tomasz Napierala } 1944009ea47eSEdward Tomasz Napierala } 1945009ea47eSEdward Tomasz Napierala 1946009ea47eSEdward Tomasz Napierala /* 1947009ea47eSEdward Tomasz Napierala * Go through the no longer used sockets, closing them. 1948009ea47eSEdward Tomasz Napierala */ 1949009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 1950009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 1951009ea47eSEdward Tomasz Napierala if (oldp->p_socket <= 0) 1952009ea47eSEdward Tomasz Napierala continue; 1953009ea47eSEdward Tomasz Napierala log_debugx("closing socket for %s, portal-group \"%s\"", 1954009ea47eSEdward Tomasz Napierala oldp->p_listen, oldpg->pg_name); 1955009ea47eSEdward Tomasz Napierala close(oldp->p_socket); 1956009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 1957009ea47eSEdward Tomasz Napierala } 1958009ea47eSEdward Tomasz Napierala } 1959009ea47eSEdward Tomasz Napierala 1960829603e2SAlexander Motin /* (Re-)Register on remaining/new iSNS servers. */ 1961829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1962829603e2SAlexander Motin TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1963829603e2SAlexander Motin if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1964829603e2SAlexander Motin break; 1965829603e2SAlexander Motin } 1966829603e2SAlexander Motin isns_register(newns, oldns); 1967829603e2SAlexander Motin } 1968829603e2SAlexander Motin 1969829603e2SAlexander Motin /* Schedule iSNS update */ 1970829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) 1971829603e2SAlexander Motin set_timeout((newconf->conf_isns_period + 2) / 3, false); 1972829603e2SAlexander Motin 1973009ea47eSEdward Tomasz Napierala return (cumulated_error); 1974009ea47eSEdward Tomasz Napierala } 1975009ea47eSEdward Tomasz Napierala 1976009ea47eSEdward Tomasz Napierala bool 1977009ea47eSEdward Tomasz Napierala timed_out(void) 1978009ea47eSEdward Tomasz Napierala { 1979009ea47eSEdward Tomasz Napierala 1980009ea47eSEdward Tomasz Napierala return (sigalrm_received); 1981009ea47eSEdward Tomasz Napierala } 1982009ea47eSEdward Tomasz Napierala 1983009ea47eSEdward Tomasz Napierala static void 1984829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused) 1985009ea47eSEdward Tomasz Napierala { 1986009ea47eSEdward Tomasz Napierala /* 1987009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 1988009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 1989009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 1990009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 1991009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 1992009ea47eSEdward Tomasz Napierala */ 1993009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 1994009ea47eSEdward Tomasz Napierala /* 1995009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 1996009ea47eSEdward Tomasz Napierala */ 1997009ea47eSEdward Tomasz Napierala _exit(2); 1998009ea47eSEdward Tomasz Napierala } 1999009ea47eSEdward Tomasz Napierala 2000009ea47eSEdward Tomasz Napierala sigalrm_received = true; 2001009ea47eSEdward Tomasz Napierala } 2002009ea47eSEdward Tomasz Napierala 2003009ea47eSEdward Tomasz Napierala static void 2004829603e2SAlexander Motin sigalrm_handler(int dummy __unused) 2005829603e2SAlexander Motin { 2006829603e2SAlexander Motin 2007829603e2SAlexander Motin sigalrm_received = true; 2008829603e2SAlexander Motin } 2009829603e2SAlexander Motin 2010829603e2SAlexander Motin void 2011829603e2SAlexander Motin set_timeout(int timeout, int fatal) 2012009ea47eSEdward Tomasz Napierala { 2013009ea47eSEdward Tomasz Napierala struct sigaction sa; 2014009ea47eSEdward Tomasz Napierala struct itimerval itv; 2015009ea47eSEdward Tomasz Napierala int error; 2016009ea47eSEdward Tomasz Napierala 2017829603e2SAlexander Motin if (timeout <= 0) { 2018009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 2019829603e2SAlexander Motin bzero(&itv, sizeof(itv)); 2020829603e2SAlexander Motin error = setitimer(ITIMER_REAL, &itv, NULL); 2021829603e2SAlexander Motin if (error != 0) 2022829603e2SAlexander Motin log_err(1, "setitimer"); 2023829603e2SAlexander Motin sigalrm_received = false; 2024009ea47eSEdward Tomasz Napierala return; 2025009ea47eSEdward Tomasz Napierala } 2026009ea47eSEdward Tomasz Napierala 2027829603e2SAlexander Motin sigalrm_received = false; 2028009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 2029829603e2SAlexander Motin if (fatal) 2030829603e2SAlexander Motin sa.sa_handler = sigalrm_handler_fatal; 2031829603e2SAlexander Motin else 2032009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 2033009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 2034009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 2035009ea47eSEdward Tomasz Napierala if (error != 0) 2036009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2037009ea47eSEdward Tomasz Napierala 2038009ea47eSEdward Tomasz Napierala /* 2039009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 2040009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 2041009ea47eSEdward Tomasz Napierala */ 2042829603e2SAlexander Motin log_debugx("setting session timeout to %d seconds", timeout); 2043009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 2044009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 2045829603e2SAlexander Motin itv.it_value.tv_sec = timeout; 2046009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 2047009ea47eSEdward Tomasz Napierala if (error != 0) 2048009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 2049009ea47eSEdward Tomasz Napierala } 2050009ea47eSEdward Tomasz Napierala 2051009ea47eSEdward Tomasz Napierala static int 2052009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 2053009ea47eSEdward Tomasz Napierala { 2054009ea47eSEdward Tomasz Napierala pid_t pid; 2055009ea47eSEdward Tomasz Napierala int status; 2056009ea47eSEdward Tomasz Napierala int num = 0; 2057009ea47eSEdward Tomasz Napierala 2058009ea47eSEdward Tomasz Napierala for (;;) { 2059009ea47eSEdward Tomasz Napierala /* 2060009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 2061009ea47eSEdward Tomasz Napierala */ 2062009ea47eSEdward Tomasz Napierala if (block && num == 0) 2063009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 2064009ea47eSEdward Tomasz Napierala else 2065009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 2066009ea47eSEdward Tomasz Napierala if (pid <= 0) 2067009ea47eSEdward Tomasz Napierala break; 2068009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 2069009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 2070009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 2071009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 2072009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 2073009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 2074009ea47eSEdward Tomasz Napierala } else { 2075009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 2076009ea47eSEdward Tomasz Napierala } 2077009ea47eSEdward Tomasz Napierala num++; 2078009ea47eSEdward Tomasz Napierala } 2079009ea47eSEdward Tomasz Napierala 2080009ea47eSEdward Tomasz Napierala return (num); 2081009ea47eSEdward Tomasz Napierala } 2082009ea47eSEdward Tomasz Napierala 2083009ea47eSEdward Tomasz Napierala static void 20848eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd, 2085073edb1cSAlexander Motin const struct sockaddr *client_sa, bool dont_fork) 2086009ea47eSEdward Tomasz Napierala { 2087009ea47eSEdward Tomasz Napierala struct connection *conn; 2088009ea47eSEdward Tomasz Napierala int error; 2089009ea47eSEdward Tomasz Napierala pid_t pid; 2090009ea47eSEdward Tomasz Napierala char host[NI_MAXHOST + 1]; 2091009ea47eSEdward Tomasz Napierala struct conf *conf; 2092009ea47eSEdward Tomasz Napierala 2093009ea47eSEdward Tomasz Napierala conf = portal->p_portal_group->pg_conf; 2094009ea47eSEdward Tomasz Napierala 2095009ea47eSEdward Tomasz Napierala if (dont_fork) { 2096009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; not forking due to -d flag"); 2097009ea47eSEdward Tomasz Napierala } else { 2098009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 2099009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2100009ea47eSEdward Tomasz Napierala 2101009ea47eSEdward Tomasz Napierala while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 2102009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 2103009ea47eSEdward Tomasz Napierala "waiting for child process to exit", conf->conf_maxproc); 2104009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 2105009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2106009ea47eSEdward Tomasz Napierala } 2107009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 2108009ea47eSEdward Tomasz Napierala nchildren); 2109009ea47eSEdward Tomasz Napierala nchildren++; 2110009ea47eSEdward Tomasz Napierala pid = fork(); 2111009ea47eSEdward Tomasz Napierala if (pid < 0) 2112009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 2113009ea47eSEdward Tomasz Napierala if (pid > 0) { 2114009ea47eSEdward Tomasz Napierala close(fd); 2115009ea47eSEdward Tomasz Napierala return; 2116009ea47eSEdward Tomasz Napierala } 2117009ea47eSEdward Tomasz Napierala } 2118009ea47eSEdward Tomasz Napierala pidfile_close(conf->conf_pidfh); 2119009ea47eSEdward Tomasz Napierala 2120073edb1cSAlexander Motin error = getnameinfo(client_sa, client_sa->sa_len, 2121009ea47eSEdward Tomasz Napierala host, sizeof(host), NULL, 0, NI_NUMERICHOST); 2122009ea47eSEdward Tomasz Napierala if (error != 0) 21239a95cfffSEdward Tomasz Napierala log_errx(1, "getnameinfo: %s", gai_strerror(error)); 2124009ea47eSEdward Tomasz Napierala 2125009ea47eSEdward Tomasz Napierala log_debugx("accepted connection from %s; portal group \"%s\"", 2126009ea47eSEdward Tomasz Napierala host, portal->p_portal_group->pg_name); 2127009ea47eSEdward Tomasz Napierala log_set_peer_addr(host); 2128009ea47eSEdward Tomasz Napierala setproctitle("%s", host); 2129009ea47eSEdward Tomasz Napierala 2130073edb1cSAlexander Motin conn = connection_new(portal, fd, host, client_sa); 2131829603e2SAlexander Motin set_timeout(conf->conf_timeout, true); 2132009ea47eSEdward Tomasz Napierala kernel_capsicate(); 2133009ea47eSEdward Tomasz Napierala login(conn); 2134009ea47eSEdward Tomasz Napierala if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 2135009ea47eSEdward Tomasz Napierala kernel_handoff(conn); 2136009ea47eSEdward Tomasz Napierala log_debugx("connection handed off to the kernel"); 2137009ea47eSEdward Tomasz Napierala } else { 2138009ea47eSEdward Tomasz Napierala assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 2139009ea47eSEdward Tomasz Napierala discovery(conn); 2140009ea47eSEdward Tomasz Napierala } 2141009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 2142009ea47eSEdward Tomasz Napierala exit(0); 2143009ea47eSEdward Tomasz Napierala } 2144009ea47eSEdward Tomasz Napierala 2145009ea47eSEdward Tomasz Napierala static int 2146009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds) 2147009ea47eSEdward Tomasz Napierala { 2148009ea47eSEdward Tomasz Napierala 2149009ea47eSEdward Tomasz Napierala /* 2150009ea47eSEdward Tomasz Napierala * Skip sockets which we failed to bind. 2151009ea47eSEdward Tomasz Napierala */ 2152009ea47eSEdward Tomasz Napierala if (fd <= 0) 2153009ea47eSEdward Tomasz Napierala return (nfds); 2154009ea47eSEdward Tomasz Napierala 2155009ea47eSEdward Tomasz Napierala FD_SET(fd, fdset); 2156009ea47eSEdward Tomasz Napierala if (fd > nfds) 2157009ea47eSEdward Tomasz Napierala nfds = fd; 2158009ea47eSEdward Tomasz Napierala return (nfds); 2159009ea47eSEdward Tomasz Napierala } 2160009ea47eSEdward Tomasz Napierala 2161009ea47eSEdward Tomasz Napierala static void 2162009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork) 2163009ea47eSEdward Tomasz Napierala { 2164009ea47eSEdward Tomasz Napierala struct portal_group *pg; 2165009ea47eSEdward Tomasz Napierala struct portal *portal; 21669a95cfffSEdward Tomasz Napierala struct sockaddr_storage client_sa; 21679a95cfffSEdward Tomasz Napierala socklen_t client_salen; 2168009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2169009ea47eSEdward Tomasz Napierala int connection_id; 21708cab2ed4SEdward Tomasz Napierala int portal_id; 2171ba3a2d31SEdward Tomasz Napierala #endif 2172009ea47eSEdward Tomasz Napierala fd_set fdset; 2173009ea47eSEdward Tomasz Napierala int error, nfds, client_fd; 2174009ea47eSEdward Tomasz Napierala 2175009ea47eSEdward Tomasz Napierala pidfile_write(conf->conf_pidfh); 2176009ea47eSEdward Tomasz Napierala 2177009ea47eSEdward Tomasz Napierala for (;;) { 2178829603e2SAlexander Motin if (sighup_received || sigterm_received || timed_out()) 2179009ea47eSEdward Tomasz Napierala return; 2180009ea47eSEdward Tomasz Napierala 2181009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2182ba3a2d31SEdward Tomasz Napierala if (proxy_mode) { 21838eab95d6SEdward Tomasz Napierala client_salen = sizeof(client_sa); 21848eab95d6SEdward Tomasz Napierala kernel_accept(&connection_id, &portal_id, 21858eab95d6SEdward Tomasz Napierala (struct sockaddr *)&client_sa, &client_salen); 21860488e848SEdward Tomasz Napierala assert(client_salen >= client_sa.ss_len); 2187009ea47eSEdward Tomasz Napierala 21888cab2ed4SEdward Tomasz Napierala log_debugx("incoming connection, id %d, portal id %d", 21898cab2ed4SEdward Tomasz Napierala connection_id, portal_id); 21908cab2ed4SEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 21918cab2ed4SEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 21928cab2ed4SEdward Tomasz Napierala if (portal->p_id == portal_id) { 21938cab2ed4SEdward Tomasz Napierala goto found; 21948cab2ed4SEdward Tomasz Napierala } 21958cab2ed4SEdward Tomasz Napierala } 21968cab2ed4SEdward Tomasz Napierala } 2197009ea47eSEdward Tomasz Napierala 21988cab2ed4SEdward Tomasz Napierala log_errx(1, "kernel returned invalid portal_id %d", 21998cab2ed4SEdward Tomasz Napierala portal_id); 22008cab2ed4SEdward Tomasz Napierala 22018cab2ed4SEdward Tomasz Napierala found: 22028eab95d6SEdward Tomasz Napierala handle_connection(portal, connection_id, 2203073edb1cSAlexander Motin (struct sockaddr *)&client_sa, dont_fork); 2204ba3a2d31SEdward Tomasz Napierala } else { 2205ba3a2d31SEdward Tomasz Napierala #endif 2206ba3a2d31SEdward Tomasz Napierala assert(proxy_mode == false); 2207ba3a2d31SEdward Tomasz Napierala 2208009ea47eSEdward Tomasz Napierala FD_ZERO(&fdset); 2209009ea47eSEdward Tomasz Napierala nfds = 0; 2210009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2211009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 2212009ea47eSEdward Tomasz Napierala nfds = fd_add(portal->p_socket, &fdset, nfds); 2213009ea47eSEdward Tomasz Napierala } 2214009ea47eSEdward Tomasz Napierala error = select(nfds + 1, &fdset, NULL, NULL, NULL); 2215009ea47eSEdward Tomasz Napierala if (error <= 0) { 2216009ea47eSEdward Tomasz Napierala if (errno == EINTR) 2217009ea47eSEdward Tomasz Napierala return; 2218009ea47eSEdward Tomasz Napierala log_err(1, "select"); 2219009ea47eSEdward Tomasz Napierala } 2220009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2221009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2222009ea47eSEdward Tomasz Napierala if (!FD_ISSET(portal->p_socket, &fdset)) 2223009ea47eSEdward Tomasz Napierala continue; 22249a95cfffSEdward Tomasz Napierala client_salen = sizeof(client_sa); 22259a95cfffSEdward Tomasz Napierala client_fd = accept(portal->p_socket, 22269a95cfffSEdward Tomasz Napierala (struct sockaddr *)&client_sa, 22279a95cfffSEdward Tomasz Napierala &client_salen); 2228009ea47eSEdward Tomasz Napierala if (client_fd < 0) 2229009ea47eSEdward Tomasz Napierala log_err(1, "accept"); 22300488e848SEdward Tomasz Napierala assert(client_salen >= client_sa.ss_len); 22310488e848SEdward Tomasz Napierala 22329a95cfffSEdward Tomasz Napierala handle_connection(portal, client_fd, 22338eab95d6SEdward Tomasz Napierala (struct sockaddr *)&client_sa, 2234073edb1cSAlexander Motin dont_fork); 2235009ea47eSEdward Tomasz Napierala break; 2236009ea47eSEdward Tomasz Napierala } 2237009ea47eSEdward Tomasz Napierala } 2238ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2239ba3a2d31SEdward Tomasz Napierala } 2240ba3a2d31SEdward Tomasz Napierala #endif 2241009ea47eSEdward Tomasz Napierala } 2242009ea47eSEdward Tomasz Napierala } 2243009ea47eSEdward Tomasz Napierala 2244009ea47eSEdward Tomasz Napierala static void 2245009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused) 2246009ea47eSEdward Tomasz Napierala { 2247009ea47eSEdward Tomasz Napierala 2248009ea47eSEdward Tomasz Napierala sighup_received = true; 2249009ea47eSEdward Tomasz Napierala } 2250009ea47eSEdward Tomasz Napierala 2251009ea47eSEdward Tomasz Napierala static void 2252009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused) 2253009ea47eSEdward Tomasz Napierala { 2254009ea47eSEdward Tomasz Napierala 2255009ea47eSEdward Tomasz Napierala sigterm_received = true; 2256009ea47eSEdward Tomasz Napierala } 2257009ea47eSEdward Tomasz Napierala 2258009ea47eSEdward Tomasz Napierala static void 225970b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused) 226070b939ecSEdward Tomasz Napierala { 226170b939ecSEdward Tomasz Napierala 226270b939ecSEdward Tomasz Napierala /* 226370b939ecSEdward Tomasz Napierala * The only purpose of this handler is to make SIGCHLD 226470b939ecSEdward Tomasz Napierala * interrupt the ISCSIDWAIT ioctl(2), so we can call 226570b939ecSEdward Tomasz Napierala * wait_for_children(). 226670b939ecSEdward Tomasz Napierala */ 226770b939ecSEdward Tomasz Napierala } 226870b939ecSEdward Tomasz Napierala 226970b939ecSEdward Tomasz Napierala static void 2270009ea47eSEdward Tomasz Napierala register_signals(void) 2271009ea47eSEdward Tomasz Napierala { 2272009ea47eSEdward Tomasz Napierala struct sigaction sa; 2273009ea47eSEdward Tomasz Napierala int error; 2274009ea47eSEdward Tomasz Napierala 2275009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 2276009ea47eSEdward Tomasz Napierala sa.sa_handler = sighup_handler; 2277009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 2278009ea47eSEdward Tomasz Napierala error = sigaction(SIGHUP, &sa, NULL); 2279009ea47eSEdward Tomasz Napierala if (error != 0) 2280009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2281009ea47eSEdward Tomasz Napierala 2282009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 2283009ea47eSEdward Tomasz Napierala error = sigaction(SIGTERM, &sa, NULL); 2284009ea47eSEdward Tomasz Napierala if (error != 0) 2285009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2286009ea47eSEdward Tomasz Napierala 2287009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 2288009ea47eSEdward Tomasz Napierala error = sigaction(SIGINT, &sa, NULL); 2289009ea47eSEdward Tomasz Napierala if (error != 0) 2290009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 229170b939ecSEdward Tomasz Napierala 229270b939ecSEdward Tomasz Napierala sa.sa_handler = sigchld_handler; 229370b939ecSEdward Tomasz Napierala error = sigaction(SIGCHLD, &sa, NULL); 229470b939ecSEdward Tomasz Napierala if (error != 0) 229570b939ecSEdward Tomasz Napierala log_err(1, "sigaction"); 2296009ea47eSEdward Tomasz Napierala } 2297009ea47eSEdward Tomasz Napierala 2298009ea47eSEdward Tomasz Napierala int 2299009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 2300009ea47eSEdward Tomasz Napierala { 2301009ea47eSEdward Tomasz Napierala struct conf *oldconf, *newconf, *tmpconf; 2302829603e2SAlexander Motin struct isns *newns; 2303009ea47eSEdward Tomasz Napierala const char *config_path = DEFAULT_CONFIG_PATH; 2304009ea47eSEdward Tomasz Napierala int debug = 0, ch, error; 2305009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 2306009ea47eSEdward Tomasz Napierala 2307ba3a2d31SEdward Tomasz Napierala while ((ch = getopt(argc, argv, "df:R")) != -1) { 2308009ea47eSEdward Tomasz Napierala switch (ch) { 2309009ea47eSEdward Tomasz Napierala case 'd': 2310009ea47eSEdward Tomasz Napierala dont_daemonize = true; 2311009ea47eSEdward Tomasz Napierala debug++; 2312009ea47eSEdward Tomasz Napierala break; 2313009ea47eSEdward Tomasz Napierala case 'f': 2314009ea47eSEdward Tomasz Napierala config_path = optarg; 2315009ea47eSEdward Tomasz Napierala break; 2316ba3a2d31SEdward Tomasz Napierala case 'R': 2317ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 2318ba3a2d31SEdward Tomasz Napierala log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY " 2319ba3a2d31SEdward Tomasz Napierala "does not support iSER protocol"); 2320ba3a2d31SEdward Tomasz Napierala #endif 2321ba3a2d31SEdward Tomasz Napierala proxy_mode = true; 2322ba3a2d31SEdward Tomasz Napierala break; 2323009ea47eSEdward Tomasz Napierala case '?': 2324009ea47eSEdward Tomasz Napierala default: 2325009ea47eSEdward Tomasz Napierala usage(); 2326009ea47eSEdward Tomasz Napierala } 2327009ea47eSEdward Tomasz Napierala } 2328009ea47eSEdward Tomasz Napierala argc -= optind; 2329009ea47eSEdward Tomasz Napierala if (argc != 0) 2330009ea47eSEdward Tomasz Napierala usage(); 2331009ea47eSEdward Tomasz Napierala 2332009ea47eSEdward Tomasz Napierala log_init(debug); 2333009ea47eSEdward Tomasz Napierala kernel_init(); 2334009ea47eSEdward Tomasz Napierala 2335009ea47eSEdward Tomasz Napierala oldconf = conf_new_from_kernel(); 2336009ea47eSEdward Tomasz Napierala newconf = conf_new_from_file(config_path); 2337009ea47eSEdward Tomasz Napierala if (newconf == NULL) 2338fe845cdbSEdward Tomasz Napierala log_errx(1, "configuration error; exiting"); 2339009ea47eSEdward Tomasz Napierala if (debug > 0) { 2340009ea47eSEdward Tomasz Napierala oldconf->conf_debug = debug; 2341009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 2342009ea47eSEdward Tomasz Napierala } 2343009ea47eSEdward Tomasz Napierala 2344009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2345009ea47eSEdward Tomasz Napierala if (error != 0) 2346fe845cdbSEdward Tomasz Napierala log_errx(1, "failed to apply configuration; exiting"); 2347fe845cdbSEdward Tomasz Napierala 2348009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 2349009ea47eSEdward Tomasz Napierala oldconf = NULL; 2350009ea47eSEdward Tomasz Napierala 2351009ea47eSEdward Tomasz Napierala register_signals(); 2352009ea47eSEdward Tomasz Napierala 23535d5a95f1SEdward Tomasz Napierala if (dont_daemonize == false) { 23545d5a95f1SEdward Tomasz Napierala log_debugx("daemonizing"); 23555d5a95f1SEdward Tomasz Napierala if (daemon(0, 0) == -1) { 23565d5a95f1SEdward Tomasz Napierala log_warn("cannot daemonize"); 23575d5a95f1SEdward Tomasz Napierala pidfile_remove(newconf->conf_pidfh); 23585d5a95f1SEdward Tomasz Napierala exit(1); 23595d5a95f1SEdward Tomasz Napierala } 23605d5a95f1SEdward Tomasz Napierala } 23615d5a95f1SEdward Tomasz Napierala 2362829603e2SAlexander Motin /* Schedule iSNS update */ 2363829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) 2364829603e2SAlexander Motin set_timeout((newconf->conf_isns_period + 2) / 3, false); 2365829603e2SAlexander Motin 2366009ea47eSEdward Tomasz Napierala for (;;) { 2367009ea47eSEdward Tomasz Napierala main_loop(newconf, dont_daemonize); 2368009ea47eSEdward Tomasz Napierala if (sighup_received) { 2369009ea47eSEdward Tomasz Napierala sighup_received = false; 2370009ea47eSEdward Tomasz Napierala log_debugx("received SIGHUP, reloading configuration"); 2371009ea47eSEdward Tomasz Napierala tmpconf = conf_new_from_file(config_path); 2372009ea47eSEdward Tomasz Napierala if (tmpconf == NULL) { 2373009ea47eSEdward Tomasz Napierala log_warnx("configuration error, " 2374009ea47eSEdward Tomasz Napierala "continuing with old configuration"); 2375009ea47eSEdward Tomasz Napierala } else { 2376009ea47eSEdward Tomasz Napierala if (debug > 0) 2377009ea47eSEdward Tomasz Napierala tmpconf->conf_debug = debug; 2378009ea47eSEdward Tomasz Napierala oldconf = newconf; 2379009ea47eSEdward Tomasz Napierala newconf = tmpconf; 2380009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2381009ea47eSEdward Tomasz Napierala if (error != 0) 2382009ea47eSEdward Tomasz Napierala log_warnx("failed to reload " 2383009ea47eSEdward Tomasz Napierala "configuration"); 2384009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 2385009ea47eSEdward Tomasz Napierala oldconf = NULL; 2386009ea47eSEdward Tomasz Napierala } 2387009ea47eSEdward Tomasz Napierala } else if (sigterm_received) { 2388009ea47eSEdward Tomasz Napierala log_debugx("exiting on signal; " 2389009ea47eSEdward Tomasz Napierala "reloading empty configuration"); 2390009ea47eSEdward Tomasz Napierala 2391009ea47eSEdward Tomasz Napierala log_debugx("disabling CTL iSCSI port " 2392009ea47eSEdward Tomasz Napierala "and terminating all connections"); 2393009ea47eSEdward Tomasz Napierala 2394009ea47eSEdward Tomasz Napierala oldconf = newconf; 2395009ea47eSEdward Tomasz Napierala newconf = conf_new(); 2396009ea47eSEdward Tomasz Napierala if (debug > 0) 2397009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 2398009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2399009ea47eSEdward Tomasz Napierala if (error != 0) 2400009ea47eSEdward Tomasz Napierala log_warnx("failed to apply configuration"); 2401829603e2SAlexander Motin conf_delete(oldconf); 2402829603e2SAlexander Motin oldconf = NULL; 2403009ea47eSEdward Tomasz Napierala 2404009ea47eSEdward Tomasz Napierala log_warnx("exiting on signal"); 2405009ea47eSEdward Tomasz Napierala exit(0); 2406009ea47eSEdward Tomasz Napierala } else { 2407009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 2408009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2409829603e2SAlexander Motin if (timed_out()) { 2410829603e2SAlexander Motin set_timeout(0, false); 2411829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) 2412829603e2SAlexander Motin isns_check(newns); 2413829603e2SAlexander Motin /* Schedule iSNS update */ 2414829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) { 2415829603e2SAlexander Motin set_timeout((newconf->conf_isns_period 2416829603e2SAlexander Motin + 2) / 3, 2417829603e2SAlexander Motin false); 2418829603e2SAlexander Motin } 2419829603e2SAlexander Motin } 2420009ea47eSEdward Tomasz Napierala } 2421009ea47eSEdward Tomasz Napierala } 2422009ea47eSEdward Tomasz Napierala /* NOTREACHED */ 2423009ea47eSEdward Tomasz Napierala } 2424