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> 375c734b04SJakub Wojciech Klama #include <sys/stat.h> 38009ea47eSEdward Tomasz Napierala #include <sys/wait.h> 39009ea47eSEdward Tomasz Napierala #include <netinet/in.h> 40073edb1cSAlexander Motin #include <arpa/inet.h> 41009ea47eSEdward Tomasz Napierala #include <assert.h> 42009ea47eSEdward Tomasz Napierala #include <ctype.h> 43009ea47eSEdward Tomasz Napierala #include <errno.h> 44009ea47eSEdward Tomasz Napierala #include <netdb.h> 45009ea47eSEdward Tomasz Napierala #include <signal.h> 46009ea47eSEdward Tomasz Napierala #include <stdbool.h> 47009ea47eSEdward Tomasz Napierala #include <stdio.h> 48009ea47eSEdward Tomasz Napierala #include <stdint.h> 49009ea47eSEdward Tomasz Napierala #include <stdlib.h> 50009ea47eSEdward Tomasz Napierala #include <string.h> 51009ea47eSEdward Tomasz Napierala #include <unistd.h> 52009ea47eSEdward Tomasz Napierala 53009ea47eSEdward Tomasz Napierala #include "ctld.h" 54829603e2SAlexander Motin #include "isns.h" 55009ea47eSEdward Tomasz Napierala 56ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false; 57ba3a2d31SEdward Tomasz Napierala 58009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false; 59009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false; 60009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false; 61009ea47eSEdward Tomasz Napierala 62009ea47eSEdward Tomasz Napierala static int nchildren = 0; 63db7bf2baSAlexander Motin static uint16_t last_portal_group_tag = 0xff; 64009ea47eSEdward Tomasz Napierala 65009ea47eSEdward Tomasz Napierala static void 66009ea47eSEdward Tomasz Napierala usage(void) 67009ea47eSEdward Tomasz Napierala { 68009ea47eSEdward Tomasz Napierala 69e321146fSEdward Tomasz Napierala fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n"); 70009ea47eSEdward Tomasz Napierala exit(1); 71009ea47eSEdward Tomasz Napierala } 72009ea47eSEdward Tomasz Napierala 73009ea47eSEdward Tomasz Napierala char * 74009ea47eSEdward Tomasz Napierala checked_strdup(const char *s) 75009ea47eSEdward Tomasz Napierala { 76009ea47eSEdward Tomasz Napierala char *c; 77009ea47eSEdward Tomasz Napierala 78009ea47eSEdward Tomasz Napierala c = strdup(s); 79009ea47eSEdward Tomasz Napierala if (c == NULL) 80009ea47eSEdward Tomasz Napierala log_err(1, "strdup"); 81009ea47eSEdward Tomasz Napierala return (c); 82009ea47eSEdward Tomasz Napierala } 83009ea47eSEdward Tomasz Napierala 84009ea47eSEdward Tomasz Napierala struct conf * 85009ea47eSEdward Tomasz Napierala conf_new(void) 86009ea47eSEdward Tomasz Napierala { 87009ea47eSEdward Tomasz Napierala struct conf *conf; 88009ea47eSEdward Tomasz Napierala 89009ea47eSEdward Tomasz Napierala conf = calloc(1, sizeof(*conf)); 90009ea47eSEdward Tomasz Napierala if (conf == NULL) 91009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 92920c6cbaSAlexander Motin TAILQ_INIT(&conf->conf_luns); 93009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_targets); 94009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_auth_groups); 9592847ee1SAlexander Motin TAILQ_INIT(&conf->conf_ports); 96009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_portal_groups); 97057abcb0SAlexander Motin TAILQ_INIT(&conf->conf_pports); 98829603e2SAlexander Motin TAILQ_INIT(&conf->conf_isns); 99009ea47eSEdward Tomasz Napierala 100829603e2SAlexander Motin conf->conf_isns_period = 900; 101829603e2SAlexander Motin conf->conf_isns_timeout = 5; 102009ea47eSEdward Tomasz Napierala conf->conf_debug = 0; 103009ea47eSEdward Tomasz Napierala conf->conf_timeout = 60; 104009ea47eSEdward Tomasz Napierala conf->conf_maxproc = 30; 105009ea47eSEdward Tomasz Napierala 106009ea47eSEdward Tomasz Napierala return (conf); 107009ea47eSEdward Tomasz Napierala } 108009ea47eSEdward Tomasz Napierala 109009ea47eSEdward Tomasz Napierala void 110009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf) 111009ea47eSEdward Tomasz Napierala { 112920c6cbaSAlexander Motin struct lun *lun, *ltmp; 113009ea47eSEdward Tomasz Napierala struct target *targ, *tmp; 114009ea47eSEdward Tomasz Napierala struct auth_group *ag, *cagtmp; 115009ea47eSEdward Tomasz Napierala struct portal_group *pg, *cpgtmp; 116057abcb0SAlexander Motin struct pport *pp, *pptmp; 117829603e2SAlexander Motin struct isns *is, *istmp; 118009ea47eSEdward Tomasz Napierala 119009ea47eSEdward Tomasz Napierala assert(conf->conf_pidfh == NULL); 120009ea47eSEdward Tomasz Napierala 121920c6cbaSAlexander Motin TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp) 122920c6cbaSAlexander Motin lun_delete(lun); 123009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 124009ea47eSEdward Tomasz Napierala target_delete(targ); 125009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 126009ea47eSEdward Tomasz Napierala auth_group_delete(ag); 127009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 128009ea47eSEdward Tomasz Napierala portal_group_delete(pg); 129057abcb0SAlexander Motin TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp) 130057abcb0SAlexander Motin pport_delete(pp); 131829603e2SAlexander Motin TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp) 132829603e2SAlexander Motin isns_delete(is); 13392847ee1SAlexander Motin assert(TAILQ_EMPTY(&conf->conf_ports)); 134009ea47eSEdward Tomasz Napierala free(conf->conf_pidfile_path); 135009ea47eSEdward Tomasz Napierala free(conf); 136009ea47eSEdward Tomasz Napierala } 137009ea47eSEdward Tomasz Napierala 138009ea47eSEdward Tomasz Napierala static struct auth * 139009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag) 140009ea47eSEdward Tomasz Napierala { 141009ea47eSEdward Tomasz Napierala struct auth *auth; 142009ea47eSEdward Tomasz Napierala 143009ea47eSEdward Tomasz Napierala auth = calloc(1, sizeof(*auth)); 144009ea47eSEdward Tomasz Napierala if (auth == NULL) 145009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 146009ea47eSEdward Tomasz Napierala auth->a_auth_group = ag; 147009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); 148009ea47eSEdward Tomasz Napierala return (auth); 149009ea47eSEdward Tomasz Napierala } 150009ea47eSEdward Tomasz Napierala 151009ea47eSEdward Tomasz Napierala static void 152009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth) 153009ea47eSEdward Tomasz Napierala { 154009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); 155009ea47eSEdward Tomasz Napierala 156009ea47eSEdward Tomasz Napierala free(auth->a_user); 157009ea47eSEdward Tomasz Napierala free(auth->a_secret); 158009ea47eSEdward Tomasz Napierala free(auth->a_mutual_user); 159009ea47eSEdward Tomasz Napierala free(auth->a_mutual_secret); 160009ea47eSEdward Tomasz Napierala free(auth); 161009ea47eSEdward Tomasz Napierala } 162009ea47eSEdward Tomasz Napierala 163009ea47eSEdward Tomasz Napierala const struct auth * 164d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user) 165009ea47eSEdward Tomasz Napierala { 166009ea47eSEdward Tomasz Napierala const struct auth *auth; 167009ea47eSEdward Tomasz Napierala 168009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { 169009ea47eSEdward Tomasz Napierala if (strcmp(auth->a_user, user) == 0) 170009ea47eSEdward Tomasz Napierala return (auth); 171009ea47eSEdward Tomasz Napierala } 172009ea47eSEdward Tomasz Napierala 173009ea47eSEdward Tomasz Napierala return (NULL); 174009ea47eSEdward Tomasz Napierala } 175009ea47eSEdward Tomasz Napierala 1761658628dSEdward Tomasz Napierala static void 1771658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth) 1781658628dSEdward Tomasz Napierala { 1791658628dSEdward Tomasz Napierala size_t len; 1801658628dSEdward Tomasz Napierala 1811658628dSEdward Tomasz Napierala len = strlen(auth->a_secret); 1821658628dSEdward Tomasz Napierala if (len > 16) { 1831658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1841658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 1851658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 1861658628dSEdward Tomasz Napierala "long", auth->a_user, 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 long; it should be at most 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 if (len < 12) { 1941658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1951658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 1961658628dSEdward Tomasz Napierala "is too short; it should be at least 12 characters " 1971658628dSEdward Tomasz Napierala "long", auth->a_user, 1981658628dSEdward Tomasz Napierala auth->a_auth_group->ag_name); 1991658628dSEdward Tomasz Napierala else 2001658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 20142f97facSAlexander Motin "is too short; it should be at least 12 characters " 2021658628dSEdward Tomasz Napierala "long", auth->a_user, 203f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2041658628dSEdward Tomasz Napierala } 2051658628dSEdward Tomasz Napierala 2061658628dSEdward Tomasz Napierala if (auth->a_mutual_secret != NULL) { 2077c924aaeSJosh Paetzel len = strlen(auth->a_mutual_secret); 2081658628dSEdward Tomasz Napierala if (len > 16) { 2091658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 2101658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2111658628dSEdward Tomasz Napierala "auth-group \"%s\", is too long; it should " 2121658628dSEdward Tomasz Napierala "be at most 16 characters long", 2131658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 2141658628dSEdward Tomasz Napierala else 2151658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2161658628dSEdward Tomasz Napierala "target \"%s\", is too long; it should " 2171658628dSEdward Tomasz Napierala "be at most 16 characters long", 2181658628dSEdward Tomasz Napierala auth->a_user, 219f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2201658628dSEdward Tomasz Napierala } 2211658628dSEdward Tomasz Napierala if (len < 12) { 2221658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 2231658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2241658628dSEdward Tomasz Napierala "auth-group \"%s\", is too short; it " 2251658628dSEdward Tomasz Napierala "should be at least 12 characters long", 2261658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 2271658628dSEdward Tomasz Napierala else 2281658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2291658628dSEdward Tomasz Napierala "target \"%s\", is too short; it should be " 2302668ec07SAlexander Motin "at least 12 characters long", 2311658628dSEdward Tomasz Napierala auth->a_user, 232f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2331658628dSEdward Tomasz Napierala } 2341658628dSEdward Tomasz Napierala } 2351658628dSEdward Tomasz Napierala } 2361658628dSEdward Tomasz Napierala 2371658628dSEdward Tomasz Napierala const struct auth * 2381658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user, 2391658628dSEdward Tomasz Napierala const char *secret) 2401658628dSEdward Tomasz Napierala { 2411658628dSEdward Tomasz Napierala struct auth *auth; 2421658628dSEdward Tomasz Napierala 2431658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 2441658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP; 2451658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP) { 2461658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 2471658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 2481658628dSEdward Tomasz Napierala "other types for auth-group \"%s\"", ag->ag_name); 2491658628dSEdward Tomasz Napierala else 2501658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 2511658628dSEdward Tomasz Napierala "other types for target \"%s\"", 252f7ae5bf8SEdward Tomasz Napierala ag->ag_target->t_name); 2531658628dSEdward Tomasz Napierala return (NULL); 2541658628dSEdward Tomasz Napierala } 2551658628dSEdward Tomasz Napierala 2561658628dSEdward Tomasz Napierala auth = auth_new(ag); 2571658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 2581658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 2591658628dSEdward Tomasz Napierala 2601658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 2611658628dSEdward Tomasz Napierala 2621658628dSEdward Tomasz Napierala return (auth); 2631658628dSEdward Tomasz Napierala } 2641658628dSEdward Tomasz Napierala 2651658628dSEdward Tomasz Napierala const struct auth * 2661658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user, 2671658628dSEdward Tomasz Napierala const char *secret, const char *user2, const char *secret2) 2681658628dSEdward Tomasz Napierala { 2691658628dSEdward Tomasz Napierala struct auth *auth; 2701658628dSEdward Tomasz Napierala 2711658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 2721658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP_MUTUAL; 2731658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 2741658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 2751658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 2761658628dSEdward Tomasz Napierala "with other types for auth-group \"%s\"", 2771658628dSEdward Tomasz Napierala ag->ag_name); 2781658628dSEdward Tomasz Napierala else 2791658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 2801658628dSEdward Tomasz Napierala "with other types for target \"%s\"", 281f7ae5bf8SEdward Tomasz Napierala ag->ag_target->t_name); 2821658628dSEdward Tomasz Napierala return (NULL); 2831658628dSEdward Tomasz Napierala } 2841658628dSEdward Tomasz Napierala 2851658628dSEdward Tomasz Napierala auth = auth_new(ag); 2861658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 2871658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 2881658628dSEdward Tomasz Napierala auth->a_mutual_user = checked_strdup(user2); 2891658628dSEdward Tomasz Napierala auth->a_mutual_secret = checked_strdup(secret2); 2901658628dSEdward Tomasz Napierala 2911658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 2921658628dSEdward Tomasz Napierala 2931658628dSEdward Tomasz Napierala return (auth); 2941658628dSEdward Tomasz Napierala } 2951658628dSEdward Tomasz Napierala 2968cb2e958SEdward Tomasz Napierala const struct auth_name * 2978cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name) 2988cb2e958SEdward Tomasz Napierala { 2998cb2e958SEdward Tomasz Napierala struct auth_name *an; 3008cb2e958SEdward Tomasz Napierala 3018cb2e958SEdward Tomasz Napierala an = calloc(1, sizeof(*an)); 3028cb2e958SEdward Tomasz Napierala if (an == NULL) 3038cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 3048cb2e958SEdward Tomasz Napierala an->an_auth_group = ag; 3058cb2e958SEdward Tomasz Napierala an->an_initator_name = checked_strdup(name); 3068cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next); 3078cb2e958SEdward Tomasz Napierala return (an); 3088cb2e958SEdward Tomasz Napierala } 3098cb2e958SEdward Tomasz Napierala 3108cb2e958SEdward Tomasz Napierala static void 3118cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an) 3128cb2e958SEdward Tomasz Napierala { 3138cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next); 3148cb2e958SEdward Tomasz Napierala 3158cb2e958SEdward Tomasz Napierala free(an->an_initator_name); 3168cb2e958SEdward Tomasz Napierala free(an); 3178cb2e958SEdward Tomasz Napierala } 3188cb2e958SEdward Tomasz Napierala 3198cb2e958SEdward Tomasz Napierala bool 3208cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag) 3218cb2e958SEdward Tomasz Napierala { 3228cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_names)) 3238cb2e958SEdward Tomasz Napierala return (false); 3248cb2e958SEdward Tomasz Napierala return (true); 3258cb2e958SEdward Tomasz Napierala } 3268cb2e958SEdward Tomasz Napierala 3278cb2e958SEdward Tomasz Napierala const struct auth_name * 3288cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name) 3298cb2e958SEdward Tomasz Napierala { 3308cb2e958SEdward Tomasz Napierala const struct auth_name *auth_name; 3318cb2e958SEdward Tomasz Napierala 3328cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) { 3338cb2e958SEdward Tomasz Napierala if (strcmp(auth_name->an_initator_name, name) == 0) 3348cb2e958SEdward Tomasz Napierala return (auth_name); 3358cb2e958SEdward Tomasz Napierala } 3368cb2e958SEdward Tomasz Napierala 3378cb2e958SEdward Tomasz Napierala return (NULL); 3388cb2e958SEdward Tomasz Napierala } 3398cb2e958SEdward Tomasz Napierala 340e867e162SEdward Tomasz Napierala int 341e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name) 342e867e162SEdward Tomasz Napierala { 343e867e162SEdward Tomasz Napierala if (!auth_name_defined(ag)) 344e867e162SEdward Tomasz Napierala return (0); 345e867e162SEdward Tomasz Napierala 346e867e162SEdward Tomasz Napierala if (auth_name_find(ag, initiator_name) == NULL) 347e867e162SEdward Tomasz Napierala return (1); 348e867e162SEdward Tomasz Napierala 349e867e162SEdward Tomasz Napierala return (0); 350e867e162SEdward Tomasz Napierala } 351e867e162SEdward Tomasz Napierala 3528cb2e958SEdward Tomasz Napierala const struct auth_portal * 3538cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal) 3548cb2e958SEdward Tomasz Napierala { 3558cb2e958SEdward Tomasz Napierala struct auth_portal *ap; 356073edb1cSAlexander Motin char *net, *mask, *str, *tmp; 357073edb1cSAlexander Motin int len, dm, m; 3588cb2e958SEdward Tomasz Napierala 3598cb2e958SEdward Tomasz Napierala ap = calloc(1, sizeof(*ap)); 3608cb2e958SEdward Tomasz Napierala if (ap == NULL) 3618cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 3628cb2e958SEdward Tomasz Napierala ap->ap_auth_group = ag; 3638cb2e958SEdward Tomasz Napierala ap->ap_initator_portal = checked_strdup(portal); 364073edb1cSAlexander Motin mask = str = checked_strdup(portal); 365073edb1cSAlexander Motin net = strsep(&mask, "/"); 366073edb1cSAlexander Motin if (net[0] == '[') 367073edb1cSAlexander Motin net++; 368073edb1cSAlexander Motin len = strlen(net); 369073edb1cSAlexander Motin if (len == 0) 370073edb1cSAlexander Motin goto error; 371073edb1cSAlexander Motin if (net[len - 1] == ']') 372073edb1cSAlexander Motin net[len - 1] = 0; 373073edb1cSAlexander Motin if (strchr(net, ':') != NULL) { 374073edb1cSAlexander Motin struct sockaddr_in6 *sin6 = 375073edb1cSAlexander Motin (struct sockaddr_in6 *)&ap->ap_sa; 376073edb1cSAlexander Motin 377073edb1cSAlexander Motin sin6->sin6_len = sizeof(*sin6); 378073edb1cSAlexander Motin sin6->sin6_family = AF_INET6; 379073edb1cSAlexander Motin if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0) 380073edb1cSAlexander Motin goto error; 381073edb1cSAlexander Motin dm = 128; 382073edb1cSAlexander Motin } else { 383073edb1cSAlexander Motin struct sockaddr_in *sin = 384073edb1cSAlexander Motin (struct sockaddr_in *)&ap->ap_sa; 385073edb1cSAlexander Motin 386073edb1cSAlexander Motin sin->sin_len = sizeof(*sin); 387073edb1cSAlexander Motin sin->sin_family = AF_INET; 388073edb1cSAlexander Motin if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0) 389073edb1cSAlexander Motin goto error; 390073edb1cSAlexander Motin dm = 32; 391073edb1cSAlexander Motin } 392073edb1cSAlexander Motin if (mask != NULL) { 393073edb1cSAlexander Motin m = strtol(mask, &tmp, 0); 394073edb1cSAlexander Motin if (m < 0 || m > dm || tmp[0] != 0) 395073edb1cSAlexander Motin goto error; 396073edb1cSAlexander Motin } else 397073edb1cSAlexander Motin m = dm; 398073edb1cSAlexander Motin ap->ap_mask = m; 399073edb1cSAlexander Motin free(str); 4008cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next); 4018cb2e958SEdward Tomasz Napierala return (ap); 402073edb1cSAlexander Motin 403073edb1cSAlexander Motin error: 4049a913ec2SXin LI free(str); 405678b6879SAlexander Motin free(ap); 406f9a20518SEdward Tomasz Napierala log_warnx("incorrect initiator portal \"%s\"", portal); 407073edb1cSAlexander Motin return (NULL); 4088cb2e958SEdward Tomasz Napierala } 4098cb2e958SEdward Tomasz Napierala 4108cb2e958SEdward Tomasz Napierala static void 4118cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap) 4128cb2e958SEdward Tomasz Napierala { 4138cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next); 4148cb2e958SEdward Tomasz Napierala 4158cb2e958SEdward Tomasz Napierala free(ap->ap_initator_portal); 4168cb2e958SEdward Tomasz Napierala free(ap); 4178cb2e958SEdward Tomasz Napierala } 4188cb2e958SEdward Tomasz Napierala 4198cb2e958SEdward Tomasz Napierala bool 4208cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag) 4218cb2e958SEdward Tomasz Napierala { 4228cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_portals)) 4238cb2e958SEdward Tomasz Napierala return (false); 4248cb2e958SEdward Tomasz Napierala return (true); 4258cb2e958SEdward Tomasz Napierala } 4268cb2e958SEdward Tomasz Napierala 4278cb2e958SEdward Tomasz Napierala const struct auth_portal * 428073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss) 4298cb2e958SEdward Tomasz Napierala { 430073edb1cSAlexander Motin const struct auth_portal *ap; 4315e46a066SAlexander Motin const uint8_t *a, *b; 432073edb1cSAlexander Motin int i; 4335e46a066SAlexander Motin uint8_t bmask; 4348cb2e958SEdward Tomasz Napierala 435073edb1cSAlexander Motin TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) { 436073edb1cSAlexander Motin if (ap->ap_sa.ss_family != ss->ss_family) 437073edb1cSAlexander Motin continue; 438073edb1cSAlexander Motin if (ss->ss_family == AF_INET) { 4395e46a066SAlexander Motin a = (const uint8_t *) 4405e46a066SAlexander Motin &((const struct sockaddr_in *)ss)->sin_addr; 4415e46a066SAlexander Motin b = (const uint8_t *) 4425e46a066SAlexander Motin &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr; 443073edb1cSAlexander Motin } else { 4445e46a066SAlexander Motin a = (const uint8_t *) 4455e46a066SAlexander Motin &((const struct sockaddr_in6 *)ss)->sin6_addr; 4465e46a066SAlexander Motin b = (const uint8_t *) 4475e46a066SAlexander Motin &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr; 448073edb1cSAlexander Motin } 449073edb1cSAlexander Motin for (i = 0; i < ap->ap_mask / 8; i++) { 450073edb1cSAlexander Motin if (a[i] != b[i]) 451073edb1cSAlexander Motin goto next; 452073edb1cSAlexander Motin } 453073edb1cSAlexander Motin if (ap->ap_mask % 8) { 454073edb1cSAlexander Motin bmask = 0xff << (8 - (ap->ap_mask % 8)); 455073edb1cSAlexander Motin if ((a[i] & bmask) != (b[i] & bmask)) 456073edb1cSAlexander Motin goto next; 457073edb1cSAlexander Motin } 458073edb1cSAlexander Motin return (ap); 459073edb1cSAlexander Motin next: 460073edb1cSAlexander Motin ; 4618cb2e958SEdward Tomasz Napierala } 4628cb2e958SEdward Tomasz Napierala 4638cb2e958SEdward Tomasz Napierala return (NULL); 4648cb2e958SEdward Tomasz Napierala } 4658cb2e958SEdward Tomasz Napierala 466e867e162SEdward Tomasz Napierala int 467e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa) 468e867e162SEdward Tomasz Napierala { 469e867e162SEdward Tomasz Napierala 470e867e162SEdward Tomasz Napierala if (!auth_portal_defined(ag)) 471e867e162SEdward Tomasz Napierala return (0); 472e867e162SEdward Tomasz Napierala 473e867e162SEdward Tomasz Napierala if (auth_portal_find(ag, sa) == NULL) 474e867e162SEdward Tomasz Napierala return (1); 475e867e162SEdward Tomasz Napierala 476e867e162SEdward Tomasz Napierala return (0); 477e867e162SEdward Tomasz Napierala } 478e867e162SEdward Tomasz Napierala 479009ea47eSEdward Tomasz Napierala struct auth_group * 480009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name) 481009ea47eSEdward Tomasz Napierala { 482009ea47eSEdward Tomasz Napierala struct auth_group *ag; 483009ea47eSEdward Tomasz Napierala 484009ea47eSEdward Tomasz Napierala if (name != NULL) { 485009ea47eSEdward Tomasz Napierala ag = auth_group_find(conf, name); 486009ea47eSEdward Tomasz Napierala if (ag != NULL) { 487009ea47eSEdward Tomasz Napierala log_warnx("duplicated auth-group \"%s\"", name); 488009ea47eSEdward Tomasz Napierala return (NULL); 489009ea47eSEdward Tomasz Napierala } 490009ea47eSEdward Tomasz Napierala } 491009ea47eSEdward Tomasz Napierala 492009ea47eSEdward Tomasz Napierala ag = calloc(1, sizeof(*ag)); 493009ea47eSEdward Tomasz Napierala if (ag == NULL) 494009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 495009ea47eSEdward Tomasz Napierala if (name != NULL) 496009ea47eSEdward Tomasz Napierala ag->ag_name = checked_strdup(name); 497009ea47eSEdward Tomasz Napierala TAILQ_INIT(&ag->ag_auths); 4988cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_names); 4998cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_portals); 500009ea47eSEdward Tomasz Napierala ag->ag_conf = conf; 501009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next); 502009ea47eSEdward Tomasz Napierala 503009ea47eSEdward Tomasz Napierala return (ag); 504009ea47eSEdward Tomasz Napierala } 505009ea47eSEdward Tomasz Napierala 506009ea47eSEdward Tomasz Napierala void 507009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag) 508009ea47eSEdward Tomasz Napierala { 5098cb2e958SEdward Tomasz Napierala struct auth *auth, *auth_tmp; 5108cb2e958SEdward Tomasz Napierala struct auth_name *auth_name, *auth_name_tmp; 5118cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal, *auth_portal_tmp; 512009ea47eSEdward Tomasz Napierala 513009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); 514009ea47eSEdward Tomasz Napierala 5158cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) 516009ea47eSEdward Tomasz Napierala auth_delete(auth); 5178cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) 5188cb2e958SEdward Tomasz Napierala auth_name_delete(auth_name); 5198cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, 5208cb2e958SEdward Tomasz Napierala auth_portal_tmp) 5218cb2e958SEdward Tomasz Napierala auth_portal_delete(auth_portal); 522009ea47eSEdward Tomasz Napierala free(ag->ag_name); 523009ea47eSEdward Tomasz Napierala free(ag); 524009ea47eSEdward Tomasz Napierala } 525009ea47eSEdward Tomasz Napierala 526009ea47eSEdward Tomasz Napierala struct auth_group * 527d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name) 528009ea47eSEdward Tomasz Napierala { 529009ea47eSEdward Tomasz Napierala struct auth_group *ag; 530009ea47eSEdward Tomasz Napierala 531009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 532009ea47eSEdward Tomasz Napierala if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 533009ea47eSEdward Tomasz Napierala return (ag); 534009ea47eSEdward Tomasz Napierala } 535009ea47eSEdward Tomasz Napierala 536009ea47eSEdward Tomasz Napierala return (NULL); 537009ea47eSEdward Tomasz Napierala } 538009ea47eSEdward Tomasz Napierala 539df9900fbSEdward Tomasz Napierala int 540bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str) 541df9900fbSEdward Tomasz Napierala { 542bd833b11SEdward Tomasz Napierala int type; 543df9900fbSEdward Tomasz Napierala 544df9900fbSEdward Tomasz Napierala if (strcmp(str, "none") == 0) { 545df9900fbSEdward Tomasz Napierala type = AG_TYPE_NO_AUTHENTICATION; 546e76ce448SEdward Tomasz Napierala } else if (strcmp(str, "deny") == 0) { 547e76ce448SEdward Tomasz Napierala type = AG_TYPE_DENY; 548df9900fbSEdward Tomasz Napierala } else if (strcmp(str, "chap") == 0) { 549df9900fbSEdward Tomasz Napierala type = AG_TYPE_CHAP; 550df9900fbSEdward Tomasz Napierala } else if (strcmp(str, "chap-mutual") == 0) { 551df9900fbSEdward Tomasz Napierala type = AG_TYPE_CHAP_MUTUAL; 552df9900fbSEdward Tomasz Napierala } else { 553df9900fbSEdward Tomasz Napierala if (ag->ag_name != NULL) 554df9900fbSEdward Tomasz Napierala log_warnx("invalid auth-type \"%s\" for auth-group " 555df9900fbSEdward Tomasz Napierala "\"%s\"", str, ag->ag_name); 556df9900fbSEdward Tomasz Napierala else 557df9900fbSEdward Tomasz Napierala log_warnx("invalid auth-type \"%s\" for target " 558df9900fbSEdward Tomasz Napierala "\"%s\"", str, ag->ag_target->t_name); 559df9900fbSEdward Tomasz Napierala return (1); 560df9900fbSEdward Tomasz Napierala } 561df9900fbSEdward Tomasz Napierala 562bd833b11SEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) { 563bd833b11SEdward Tomasz Napierala if (ag->ag_name != NULL) { 564df9900fbSEdward Tomasz Napierala log_warnx("cannot set auth-type to \"%s\" for " 565df9900fbSEdward Tomasz Napierala "auth-group \"%s\"; already has a different " 566df9900fbSEdward Tomasz Napierala "type", str, ag->ag_name); 567bd833b11SEdward Tomasz Napierala } else { 568df9900fbSEdward Tomasz Napierala log_warnx("cannot set auth-type to \"%s\" for target " 569df9900fbSEdward Tomasz Napierala "\"%s\"; already has a different type", 570df9900fbSEdward Tomasz Napierala str, ag->ag_target->t_name); 571bd833b11SEdward Tomasz Napierala } 572df9900fbSEdward Tomasz Napierala return (1); 573df9900fbSEdward Tomasz Napierala } 574df9900fbSEdward Tomasz Napierala 575bd833b11SEdward Tomasz Napierala ag->ag_type = type; 576bd833b11SEdward Tomasz Napierala 577bd833b11SEdward Tomasz Napierala return (0); 578df9900fbSEdward Tomasz Napierala } 579df9900fbSEdward Tomasz Napierala 580009ea47eSEdward Tomasz Napierala static struct portal * 581009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg) 582009ea47eSEdward Tomasz Napierala { 583009ea47eSEdward Tomasz Napierala struct portal *portal; 584009ea47eSEdward Tomasz Napierala 585009ea47eSEdward Tomasz Napierala portal = calloc(1, sizeof(*portal)); 586009ea47eSEdward Tomasz Napierala if (portal == NULL) 587009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 588009ea47eSEdward Tomasz Napierala TAILQ_INIT(&portal->p_targets); 589009ea47eSEdward Tomasz Napierala portal->p_portal_group = pg; 590009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 591009ea47eSEdward Tomasz Napierala return (portal); 592009ea47eSEdward Tomasz Napierala } 593009ea47eSEdward Tomasz Napierala 594009ea47eSEdward Tomasz Napierala static void 595009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal) 596009ea47eSEdward Tomasz Napierala { 59744a5953aSEdward Tomasz Napierala 598009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 59944a5953aSEdward Tomasz Napierala if (portal->p_ai != NULL) 600009ea47eSEdward Tomasz Napierala freeaddrinfo(portal->p_ai); 601009ea47eSEdward Tomasz Napierala free(portal->p_listen); 602009ea47eSEdward Tomasz Napierala free(portal); 603009ea47eSEdward Tomasz Napierala } 604009ea47eSEdward Tomasz Napierala 605009ea47eSEdward Tomasz Napierala struct portal_group * 606009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name) 607009ea47eSEdward Tomasz Napierala { 608009ea47eSEdward Tomasz Napierala struct portal_group *pg; 609009ea47eSEdward Tomasz Napierala 610009ea47eSEdward Tomasz Napierala pg = portal_group_find(conf, name); 611009ea47eSEdward Tomasz Napierala if (pg != NULL) { 612009ea47eSEdward Tomasz Napierala log_warnx("duplicated portal-group \"%s\"", name); 613009ea47eSEdward Tomasz Napierala return (NULL); 614009ea47eSEdward Tomasz Napierala } 615009ea47eSEdward Tomasz Napierala 616009ea47eSEdward Tomasz Napierala pg = calloc(1, sizeof(*pg)); 617009ea47eSEdward Tomasz Napierala if (pg == NULL) 618009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 619009ea47eSEdward Tomasz Napierala pg->pg_name = checked_strdup(name); 620398290f2SAlexander Motin TAILQ_INIT(&pg->pg_options); 621009ea47eSEdward Tomasz Napierala TAILQ_INIT(&pg->pg_portals); 62292847ee1SAlexander Motin TAILQ_INIT(&pg->pg_ports); 623009ea47eSEdward Tomasz Napierala pg->pg_conf = conf; 6246b31e130SAlexander Motin pg->pg_tag = 0; /* Assigned later in conf_apply(). */ 625009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 626009ea47eSEdward Tomasz Napierala 627009ea47eSEdward Tomasz Napierala return (pg); 628009ea47eSEdward Tomasz Napierala } 629009ea47eSEdward Tomasz Napierala 630009ea47eSEdward Tomasz Napierala void 631009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg) 632009ea47eSEdward Tomasz Napierala { 633009ea47eSEdward Tomasz Napierala struct portal *portal, *tmp; 63492847ee1SAlexander Motin struct port *port, *tport; 635398290f2SAlexander Motin struct option *o, *otmp; 636009ea47eSEdward Tomasz Napierala 63792847ee1SAlexander Motin TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport) 63892847ee1SAlexander Motin port_delete(port); 639009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 640009ea47eSEdward Tomasz Napierala 641009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 642009ea47eSEdward Tomasz Napierala portal_delete(portal); 643398290f2SAlexander Motin TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp) 644398290f2SAlexander Motin option_delete(&pg->pg_options, o); 645009ea47eSEdward Tomasz Napierala free(pg->pg_name); 646a9d78210SEdward Tomasz Napierala free(pg->pg_offload); 6476dbdbf82SEdward Tomasz Napierala free(pg->pg_redirection); 648009ea47eSEdward Tomasz Napierala free(pg); 649009ea47eSEdward Tomasz Napierala } 650009ea47eSEdward Tomasz Napierala 651009ea47eSEdward Tomasz Napierala struct portal_group * 652d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name) 653009ea47eSEdward Tomasz Napierala { 654009ea47eSEdward Tomasz Napierala struct portal_group *pg; 655009ea47eSEdward Tomasz Napierala 656009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 657009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, name) == 0) 658009ea47eSEdward Tomasz Napierala return (pg); 659009ea47eSEdward Tomasz Napierala } 660009ea47eSEdward Tomasz Napierala 661009ea47eSEdward Tomasz Napierala return (NULL); 662009ea47eSEdward Tomasz Napierala } 663009ea47eSEdward Tomasz Napierala 664829603e2SAlexander Motin static int 665829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai) 666009ea47eSEdward Tomasz Napierala { 667009ea47eSEdward Tomasz Napierala struct addrinfo hints; 6681606ad8cSAlexander Motin char *str, *addr, *ch; 669009ea47eSEdward Tomasz Napierala const char *port; 670009ea47eSEdward Tomasz Napierala int error, colons = 0; 671009ea47eSEdward Tomasz Napierala 6721606ad8cSAlexander Motin str = arg = strdup(arg); 673009ea47eSEdward Tomasz Napierala if (arg[0] == '[') { 674009ea47eSEdward Tomasz Napierala /* 675009ea47eSEdward Tomasz Napierala * IPv6 address in square brackets, perhaps with port. 676009ea47eSEdward Tomasz Napierala */ 677009ea47eSEdward Tomasz Napierala arg++; 678009ea47eSEdward Tomasz Napierala addr = strsep(&arg, "]"); 679203632f2SXin LI if (arg == NULL) { 680203632f2SXin LI free(str); 681009ea47eSEdward Tomasz Napierala return (1); 682203632f2SXin LI } 683009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 684829603e2SAlexander Motin port = def_port; 685009ea47eSEdward Tomasz Napierala } else if (arg[0] == ':') { 686009ea47eSEdward Tomasz Napierala port = arg + 1; 6871606ad8cSAlexander Motin } else { 6881606ad8cSAlexander Motin free(str); 689009ea47eSEdward Tomasz Napierala return (1); 6901606ad8cSAlexander Motin } 691009ea47eSEdward Tomasz Napierala } else { 692009ea47eSEdward Tomasz Napierala /* 693009ea47eSEdward Tomasz Napierala * Either IPv6 address without brackets - and without 694009ea47eSEdward Tomasz Napierala * a port - or IPv4 address. Just count the colons. 695009ea47eSEdward Tomasz Napierala */ 696009ea47eSEdward Tomasz Napierala for (ch = arg; *ch != '\0'; ch++) { 697009ea47eSEdward Tomasz Napierala if (*ch == ':') 698009ea47eSEdward Tomasz Napierala colons++; 699009ea47eSEdward Tomasz Napierala } 700009ea47eSEdward Tomasz Napierala if (colons > 1) { 701009ea47eSEdward Tomasz Napierala addr = arg; 702829603e2SAlexander Motin port = def_port; 703009ea47eSEdward Tomasz Napierala } else { 704009ea47eSEdward Tomasz Napierala addr = strsep(&arg, ":"); 705009ea47eSEdward Tomasz Napierala if (arg == NULL) 706829603e2SAlexander Motin port = def_port; 707009ea47eSEdward Tomasz Napierala else 708009ea47eSEdward Tomasz Napierala port = arg; 709009ea47eSEdward Tomasz Napierala } 710009ea47eSEdward Tomasz Napierala } 711009ea47eSEdward Tomasz Napierala 712009ea47eSEdward Tomasz Napierala memset(&hints, 0, sizeof(hints)); 713009ea47eSEdward Tomasz Napierala hints.ai_family = PF_UNSPEC; 714009ea47eSEdward Tomasz Napierala hints.ai_socktype = SOCK_STREAM; 715009ea47eSEdward Tomasz Napierala hints.ai_flags = AI_PASSIVE; 716829603e2SAlexander Motin error = getaddrinfo(addr, port, &hints, ai); 7171606ad8cSAlexander Motin free(str); 7181606ad8cSAlexander Motin return ((error != 0) ? 1 : 0); 719829603e2SAlexander Motin } 720009ea47eSEdward Tomasz Napierala 721829603e2SAlexander Motin int 722829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 723829603e2SAlexander Motin { 724829603e2SAlexander Motin struct portal *portal; 725829603e2SAlexander Motin 726829603e2SAlexander Motin portal = portal_new(pg); 727829603e2SAlexander Motin portal->p_listen = checked_strdup(value); 728829603e2SAlexander Motin portal->p_iser = iser; 729829603e2SAlexander Motin 730829603e2SAlexander Motin if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) { 731829603e2SAlexander Motin log_warnx("invalid listen address %s", portal->p_listen); 73244a5953aSEdward Tomasz Napierala portal_delete(portal); 733009ea47eSEdward Tomasz Napierala return (1); 734009ea47eSEdward Tomasz Napierala } 735009ea47eSEdward Tomasz Napierala 736009ea47eSEdward Tomasz Napierala /* 737009ea47eSEdward Tomasz Napierala * XXX: getaddrinfo(3) may return multiple addresses; we should turn 738009ea47eSEdward Tomasz Napierala * those into multiple portals. 739009ea47eSEdward Tomasz Napierala */ 740009ea47eSEdward Tomasz Napierala 741009ea47eSEdward Tomasz Napierala return (0); 742009ea47eSEdward Tomasz Napierala } 743009ea47eSEdward Tomasz Napierala 744829603e2SAlexander Motin int 745829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr) 746829603e2SAlexander Motin { 747829603e2SAlexander Motin struct isns *isns; 748829603e2SAlexander Motin 749829603e2SAlexander Motin isns = calloc(1, sizeof(*isns)); 750829603e2SAlexander Motin if (isns == NULL) 751829603e2SAlexander Motin log_err(1, "calloc"); 752829603e2SAlexander Motin isns->i_conf = conf; 753829603e2SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next); 754829603e2SAlexander Motin isns->i_addr = checked_strdup(addr); 755829603e2SAlexander Motin 756829603e2SAlexander Motin if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) { 757829603e2SAlexander Motin log_warnx("invalid iSNS address %s", isns->i_addr); 758829603e2SAlexander Motin isns_delete(isns); 759829603e2SAlexander Motin return (1); 760829603e2SAlexander Motin } 761829603e2SAlexander Motin 762829603e2SAlexander Motin /* 763829603e2SAlexander Motin * XXX: getaddrinfo(3) may return multiple addresses; we should turn 764829603e2SAlexander Motin * those into multiple servers. 765829603e2SAlexander Motin */ 766829603e2SAlexander Motin 767829603e2SAlexander Motin return (0); 768829603e2SAlexander Motin } 769829603e2SAlexander Motin 770829603e2SAlexander Motin void 771829603e2SAlexander Motin isns_delete(struct isns *isns) 772829603e2SAlexander Motin { 773829603e2SAlexander Motin 774829603e2SAlexander Motin TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next); 775829603e2SAlexander Motin free(isns->i_addr); 776829603e2SAlexander Motin if (isns->i_ai != NULL) 777829603e2SAlexander Motin freeaddrinfo(isns->i_ai); 778829603e2SAlexander Motin free(isns); 779829603e2SAlexander Motin } 780829603e2SAlexander Motin 781829603e2SAlexander Motin static int 782829603e2SAlexander Motin isns_do_connect(struct isns *isns) 783829603e2SAlexander Motin { 784829603e2SAlexander Motin int s; 785829603e2SAlexander Motin 786829603e2SAlexander Motin s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype, 787829603e2SAlexander Motin isns->i_ai->ai_protocol); 788829603e2SAlexander Motin if (s < 0) { 789829603e2SAlexander Motin log_warn("socket(2) failed for %s", isns->i_addr); 790829603e2SAlexander Motin return (-1); 791829603e2SAlexander Motin } 792829603e2SAlexander Motin if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) { 793829603e2SAlexander Motin log_warn("connect(2) failed for %s", isns->i_addr); 794829603e2SAlexander Motin close(s); 795829603e2SAlexander Motin return (-1); 796829603e2SAlexander Motin } 797829603e2SAlexander Motin return(s); 798829603e2SAlexander Motin } 799829603e2SAlexander Motin 800829603e2SAlexander Motin static int 801829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname) 802829603e2SAlexander Motin { 803829603e2SAlexander Motin struct conf *conf = isns->i_conf; 804829603e2SAlexander Motin struct target *target; 805829603e2SAlexander Motin struct portal *portal; 806829603e2SAlexander Motin struct portal_group *pg; 80792847ee1SAlexander Motin struct port *port; 808829603e2SAlexander Motin struct isns_req *req; 809829603e2SAlexander Motin int res = 0; 810829603e2SAlexander Motin uint32_t error; 811829603e2SAlexander Motin 812829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT); 813829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 814829603e2SAlexander Motin isns_req_add_delim(req); 815829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 816829603e2SAlexander Motin isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */ 817829603e2SAlexander Motin isns_req_add_32(req, 6, conf->conf_isns_period); 818829603e2SAlexander Motin TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 819829603e2SAlexander Motin if (pg->pg_unassigned) 820829603e2SAlexander Motin continue; 821829603e2SAlexander Motin TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 822829603e2SAlexander Motin isns_req_add_addr(req, 16, portal->p_ai); 823829603e2SAlexander Motin isns_req_add_port(req, 17, portal->p_ai); 824829603e2SAlexander Motin } 825829603e2SAlexander Motin } 826829603e2SAlexander Motin TAILQ_FOREACH(target, &conf->conf_targets, t_next) { 827829603e2SAlexander Motin isns_req_add_str(req, 32, target->t_name); 828829603e2SAlexander Motin isns_req_add_32(req, 33, 1); /* 1 -- Target*/ 829829603e2SAlexander Motin if (target->t_alias != NULL) 830829603e2SAlexander Motin isns_req_add_str(req, 34, target->t_alias); 83192847ee1SAlexander Motin TAILQ_FOREACH(port, &target->t_ports, p_ts) { 83292847ee1SAlexander Motin if ((pg = port->p_portal_group) == NULL) 83392847ee1SAlexander Motin continue; 834829603e2SAlexander Motin isns_req_add_32(req, 51, pg->pg_tag); 835829603e2SAlexander Motin TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 836829603e2SAlexander Motin isns_req_add_addr(req, 49, portal->p_ai); 837829603e2SAlexander Motin isns_req_add_port(req, 50, portal->p_ai); 838829603e2SAlexander Motin } 839829603e2SAlexander Motin } 84092847ee1SAlexander Motin } 841829603e2SAlexander Motin res = isns_req_send(s, req); 842829603e2SAlexander Motin if (res < 0) { 843829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 844829603e2SAlexander Motin goto quit; 845829603e2SAlexander Motin } 846829603e2SAlexander Motin res = isns_req_receive(s, req); 847829603e2SAlexander Motin if (res < 0) { 848829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 849829603e2SAlexander Motin goto quit; 850829603e2SAlexander Motin } 851829603e2SAlexander Motin error = isns_req_get_status(req); 852829603e2SAlexander Motin if (error != 0) { 853829603e2SAlexander Motin log_warnx("iSNS register error %d for %s", error, isns->i_addr); 854829603e2SAlexander Motin res = -1; 855829603e2SAlexander Motin } 856829603e2SAlexander Motin quit: 857829603e2SAlexander Motin isns_req_free(req); 858829603e2SAlexander Motin return (res); 859829603e2SAlexander Motin } 860829603e2SAlexander Motin 861829603e2SAlexander Motin static int 862829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname) 863829603e2SAlexander Motin { 864829603e2SAlexander Motin struct conf *conf = isns->i_conf; 865829603e2SAlexander Motin struct isns_req *req; 866829603e2SAlexander Motin int res = 0; 867829603e2SAlexander Motin uint32_t error; 868829603e2SAlexander Motin 869829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT); 870829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 871829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 872829603e2SAlexander Motin isns_req_add_delim(req); 873829603e2SAlexander Motin isns_req_add(req, 2, 0, NULL); 874829603e2SAlexander Motin res = isns_req_send(s, req); 875829603e2SAlexander Motin if (res < 0) { 876829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 877829603e2SAlexander Motin goto quit; 878829603e2SAlexander Motin } 879829603e2SAlexander Motin res = isns_req_receive(s, req); 880829603e2SAlexander Motin if (res < 0) { 881829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 882829603e2SAlexander Motin goto quit; 883829603e2SAlexander Motin } 884829603e2SAlexander Motin error = isns_req_get_status(req); 885829603e2SAlexander Motin if (error != 0) { 886829603e2SAlexander Motin log_warnx("iSNS check error %d for %s", error, isns->i_addr); 887829603e2SAlexander Motin res = -1; 888829603e2SAlexander Motin } 889829603e2SAlexander Motin quit: 890829603e2SAlexander Motin isns_req_free(req); 891829603e2SAlexander Motin return (res); 892829603e2SAlexander Motin } 893829603e2SAlexander Motin 894829603e2SAlexander Motin static int 895829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname) 896829603e2SAlexander Motin { 897829603e2SAlexander Motin struct conf *conf = isns->i_conf; 898829603e2SAlexander Motin struct isns_req *req; 899829603e2SAlexander Motin int res = 0; 900829603e2SAlexander Motin uint32_t error; 901829603e2SAlexander Motin 902829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT); 903829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 904829603e2SAlexander Motin isns_req_add_delim(req); 905829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 906829603e2SAlexander Motin res = isns_req_send(s, req); 907829603e2SAlexander Motin if (res < 0) { 908829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 909829603e2SAlexander Motin goto quit; 910829603e2SAlexander Motin } 911829603e2SAlexander Motin res = isns_req_receive(s, req); 912829603e2SAlexander Motin if (res < 0) { 913829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 914829603e2SAlexander Motin goto quit; 915829603e2SAlexander Motin } 916829603e2SAlexander Motin error = isns_req_get_status(req); 917829603e2SAlexander Motin if (error != 0) { 918829603e2SAlexander Motin log_warnx("iSNS deregister error %d for %s", error, isns->i_addr); 919829603e2SAlexander Motin res = -1; 920829603e2SAlexander Motin } 921829603e2SAlexander Motin quit: 922829603e2SAlexander Motin isns_req_free(req); 923829603e2SAlexander Motin return (res); 924829603e2SAlexander Motin } 925829603e2SAlexander Motin 926829603e2SAlexander Motin void 927829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns) 928829603e2SAlexander Motin { 929829603e2SAlexander Motin struct conf *conf = isns->i_conf; 930d5cc4a4aSAlexander Motin int s; 931829603e2SAlexander Motin char hostname[256]; 932829603e2SAlexander Motin 933829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 934829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 935829603e2SAlexander Motin return; 936829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 937829603e2SAlexander Motin s = isns_do_connect(isns); 938829603e2SAlexander Motin if (s < 0) { 939829603e2SAlexander Motin set_timeout(0, false); 940829603e2SAlexander Motin return; 941829603e2SAlexander Motin } 942829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 943829603e2SAlexander Motin 944829603e2SAlexander Motin if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets)) 945829603e2SAlexander Motin oldisns = isns; 946d5cc4a4aSAlexander Motin isns_do_deregister(oldisns, s, hostname); 947d5cc4a4aSAlexander Motin isns_do_register(isns, s, hostname); 948829603e2SAlexander Motin close(s); 949829603e2SAlexander Motin set_timeout(0, false); 950829603e2SAlexander Motin } 951829603e2SAlexander Motin 952829603e2SAlexander Motin void 953829603e2SAlexander Motin isns_check(struct isns *isns) 954829603e2SAlexander Motin { 955829603e2SAlexander Motin struct conf *conf = isns->i_conf; 956829603e2SAlexander Motin int s, res; 957829603e2SAlexander Motin char hostname[256]; 958829603e2SAlexander Motin 959829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 960829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 961829603e2SAlexander Motin return; 962829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 963829603e2SAlexander Motin s = isns_do_connect(isns); 964829603e2SAlexander Motin if (s < 0) { 965829603e2SAlexander Motin set_timeout(0, false); 966829603e2SAlexander Motin return; 967829603e2SAlexander Motin } 968829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 969829603e2SAlexander Motin 970829603e2SAlexander Motin res = isns_do_check(isns, s, hostname); 971829603e2SAlexander Motin if (res < 0) { 972d5cc4a4aSAlexander Motin isns_do_deregister(isns, s, hostname); 973d5cc4a4aSAlexander Motin isns_do_register(isns, s, hostname); 974829603e2SAlexander Motin } 975829603e2SAlexander Motin close(s); 976829603e2SAlexander Motin set_timeout(0, false); 977829603e2SAlexander Motin } 978829603e2SAlexander Motin 979829603e2SAlexander Motin void 980829603e2SAlexander Motin isns_deregister(struct isns *isns) 981829603e2SAlexander Motin { 982829603e2SAlexander Motin struct conf *conf = isns->i_conf; 983d5cc4a4aSAlexander Motin int s; 984829603e2SAlexander Motin char hostname[256]; 985829603e2SAlexander Motin 986829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 987829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 988829603e2SAlexander Motin return; 989829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 990829603e2SAlexander Motin s = isns_do_connect(isns); 991829603e2SAlexander Motin if (s < 0) 992829603e2SAlexander Motin return; 993829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 994829603e2SAlexander Motin 995d5cc4a4aSAlexander Motin isns_do_deregister(isns, s, hostname); 996829603e2SAlexander Motin close(s); 997829603e2SAlexander Motin set_timeout(0, false); 998829603e2SAlexander Motin } 999829603e2SAlexander Motin 100005374883SEdward Tomasz Napierala int 1001bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str) 100205374883SEdward Tomasz Napierala { 1003bd833b11SEdward Tomasz Napierala int filter; 100405374883SEdward Tomasz Napierala 100505374883SEdward Tomasz Napierala if (strcmp(str, "none") == 0) { 100605374883SEdward Tomasz Napierala filter = PG_FILTER_NONE; 100705374883SEdward Tomasz Napierala } else if (strcmp(str, "portal") == 0) { 100805374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL; 100905374883SEdward Tomasz Napierala } else if (strcmp(str, "portal-name") == 0) { 101005374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL_NAME; 101105374883SEdward Tomasz Napierala } else if (strcmp(str, "portal-name-auth") == 0) { 101205374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL_NAME_AUTH; 101305374883SEdward Tomasz Napierala } else { 101405374883SEdward Tomasz Napierala log_warnx("invalid discovery-filter \"%s\" for portal-group " 101505374883SEdward Tomasz Napierala "\"%s\"; valid values are \"none\", \"portal\", " 101605374883SEdward Tomasz Napierala "\"portal-name\", and \"portal-name-auth\"", 101705374883SEdward Tomasz Napierala str, pg->pg_name); 101805374883SEdward Tomasz Napierala return (1); 101905374883SEdward Tomasz Napierala } 102005374883SEdward Tomasz Napierala 1021bd833b11SEdward Tomasz Napierala if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN && 1022bd833b11SEdward Tomasz Napierala pg->pg_discovery_filter != filter) { 102305374883SEdward Tomasz Napierala log_warnx("cannot set discovery-filter to \"%s\" for " 102405374883SEdward Tomasz Napierala "portal-group \"%s\"; already has a different " 102505374883SEdward Tomasz Napierala "value", str, pg->pg_name); 102605374883SEdward Tomasz Napierala return (1); 102705374883SEdward Tomasz Napierala } 102805374883SEdward Tomasz Napierala 1029bd833b11SEdward Tomasz Napierala pg->pg_discovery_filter = filter; 1030bd833b11SEdward Tomasz Napierala 1031bd833b11SEdward Tomasz Napierala return (0); 103205374883SEdward Tomasz Napierala } 103305374883SEdward Tomasz Napierala 10346dbdbf82SEdward Tomasz Napierala int 1035a9d78210SEdward Tomasz Napierala portal_group_set_offload(struct portal_group *pg, const char *offload) 1036a9d78210SEdward Tomasz Napierala { 1037a9d78210SEdward Tomasz Napierala 1038a9d78210SEdward Tomasz Napierala if (pg->pg_offload != NULL) { 1039a9d78210SEdward Tomasz Napierala log_warnx("cannot set offload to \"%s\" for " 1040a9d78210SEdward Tomasz Napierala "portal-group \"%s\"; already defined", 1041a9d78210SEdward Tomasz Napierala offload, pg->pg_name); 1042a9d78210SEdward Tomasz Napierala return (1); 1043a9d78210SEdward Tomasz Napierala } 1044a9d78210SEdward Tomasz Napierala 1045a9d78210SEdward Tomasz Napierala pg->pg_offload = checked_strdup(offload); 1046a9d78210SEdward Tomasz Napierala 1047a9d78210SEdward Tomasz Napierala return (0); 1048a9d78210SEdward Tomasz Napierala } 1049a9d78210SEdward Tomasz Napierala 1050a9d78210SEdward Tomasz Napierala int 10516dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr) 10526dbdbf82SEdward Tomasz Napierala { 10536dbdbf82SEdward Tomasz Napierala 10546dbdbf82SEdward Tomasz Napierala if (pg->pg_redirection != NULL) { 10556dbdbf82SEdward Tomasz Napierala log_warnx("cannot set redirection to \"%s\" for " 10566dbdbf82SEdward Tomasz Napierala "portal-group \"%s\"; already defined", 10576dbdbf82SEdward Tomasz Napierala addr, pg->pg_name); 10586dbdbf82SEdward Tomasz Napierala return (1); 10596dbdbf82SEdward Tomasz Napierala } 10606dbdbf82SEdward Tomasz Napierala 10616dbdbf82SEdward Tomasz Napierala pg->pg_redirection = checked_strdup(addr); 10626dbdbf82SEdward Tomasz Napierala 10636dbdbf82SEdward Tomasz Napierala return (0); 10646dbdbf82SEdward Tomasz Napierala } 10656dbdbf82SEdward Tomasz Napierala 1066009ea47eSEdward Tomasz Napierala static bool 1067009ea47eSEdward Tomasz Napierala valid_hex(const char ch) 1068009ea47eSEdward Tomasz Napierala { 1069009ea47eSEdward Tomasz Napierala switch (ch) { 1070009ea47eSEdward Tomasz Napierala case '0': 1071009ea47eSEdward Tomasz Napierala case '1': 1072009ea47eSEdward Tomasz Napierala case '2': 1073009ea47eSEdward Tomasz Napierala case '3': 1074009ea47eSEdward Tomasz Napierala case '4': 1075009ea47eSEdward Tomasz Napierala case '5': 1076009ea47eSEdward Tomasz Napierala case '6': 1077009ea47eSEdward Tomasz Napierala case '7': 1078009ea47eSEdward Tomasz Napierala case '8': 1079009ea47eSEdward Tomasz Napierala case '9': 1080009ea47eSEdward Tomasz Napierala case 'a': 1081009ea47eSEdward Tomasz Napierala case 'A': 1082009ea47eSEdward Tomasz Napierala case 'b': 1083009ea47eSEdward Tomasz Napierala case 'B': 1084009ea47eSEdward Tomasz Napierala case 'c': 1085009ea47eSEdward Tomasz Napierala case 'C': 1086009ea47eSEdward Tomasz Napierala case 'd': 1087009ea47eSEdward Tomasz Napierala case 'D': 1088009ea47eSEdward Tomasz Napierala case 'e': 1089009ea47eSEdward Tomasz Napierala case 'E': 1090009ea47eSEdward Tomasz Napierala case 'f': 1091009ea47eSEdward Tomasz Napierala case 'F': 1092009ea47eSEdward Tomasz Napierala return (true); 1093009ea47eSEdward Tomasz Napierala default: 1094009ea47eSEdward Tomasz Napierala return (false); 1095009ea47eSEdward Tomasz Napierala } 1096009ea47eSEdward Tomasz Napierala } 1097009ea47eSEdward Tomasz Napierala 1098009ea47eSEdward Tomasz Napierala bool 1099009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name) 1100009ea47eSEdward Tomasz Napierala { 1101009ea47eSEdward Tomasz Napierala int i; 1102009ea47eSEdward Tomasz Napierala 1103009ea47eSEdward Tomasz Napierala if (strlen(name) >= MAX_NAME_LEN) { 1104009ea47eSEdward Tomasz Napierala log_warnx("overlong name for target \"%s\"; max length allowed " 1105009ea47eSEdward Tomasz Napierala "by iSCSI specification is %d characters", 1106009ea47eSEdward Tomasz Napierala name, MAX_NAME_LEN); 1107009ea47eSEdward Tomasz Napierala return (false); 1108009ea47eSEdward Tomasz Napierala } 1109009ea47eSEdward Tomasz Napierala 1110009ea47eSEdward Tomasz Napierala /* 1111009ea47eSEdward Tomasz Napierala * In the cases below, we don't return an error, just in case the admin 1112009ea47eSEdward Tomasz Napierala * was right, and we're wrong. 1113009ea47eSEdward Tomasz Napierala */ 1114009ea47eSEdward Tomasz Napierala if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 1115009ea47eSEdward Tomasz Napierala for (i = strlen("iqn."); name[i] != '\0'; i++) { 1116009ea47eSEdward Tomasz Napierala /* 1117009ea47eSEdward Tomasz Napierala * XXX: We should verify UTF-8 normalisation, as defined 1118009ea47eSEdward Tomasz Napierala * by 3.2.6.2: iSCSI Name Encoding. 1119009ea47eSEdward Tomasz Napierala */ 1120009ea47eSEdward Tomasz Napierala if (isalnum(name[i])) 1121009ea47eSEdward Tomasz Napierala continue; 1122009ea47eSEdward Tomasz Napierala if (name[i] == '-' || name[i] == '.' || name[i] == ':') 1123009ea47eSEdward Tomasz Napierala continue; 1124009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target name " 1125009ea47eSEdward Tomasz Napierala "\"%s\"; allowed characters are letters, digits, " 1126009ea47eSEdward Tomasz Napierala "'-', '.', and ':'", name[i], name); 1127009ea47eSEdward Tomasz Napierala break; 1128009ea47eSEdward Tomasz Napierala } 1129009ea47eSEdward Tomasz Napierala /* 1130009ea47eSEdward Tomasz Napierala * XXX: Check more stuff: valid date and a valid reversed domain. 1131009ea47eSEdward Tomasz Napierala */ 1132009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 1133009ea47eSEdward Tomasz Napierala if (strlen(name) != strlen("eui.") + 16) 1134009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"eui.\" " 1135009ea47eSEdward Tomasz Napierala "should be followed by exactly 16 hexadecimal " 1136009ea47eSEdward Tomasz Napierala "digits", name); 1137009ea47eSEdward Tomasz Napierala for (i = strlen("eui."); name[i] != '\0'; i++) { 1138009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 1139009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 1140009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 1141009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 1142009ea47eSEdward Tomasz Napierala break; 1143009ea47eSEdward Tomasz Napierala } 1144009ea47eSEdward Tomasz Napierala } 1145009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 1146009ea47eSEdward Tomasz Napierala if (strlen(name) > strlen("naa.") + 32) 1147009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"naa.\" " 1148009ea47eSEdward Tomasz Napierala "should be followed by at most 32 hexadecimal " 1149009ea47eSEdward Tomasz Napierala "digits", name); 1150009ea47eSEdward Tomasz Napierala for (i = strlen("naa."); name[i] != '\0'; i++) { 1151009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 1152009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 1153009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 1154009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 1155009ea47eSEdward Tomasz Napierala break; 1156009ea47eSEdward Tomasz Napierala } 1157009ea47eSEdward Tomasz Napierala } 1158009ea47eSEdward Tomasz Napierala } else { 1159009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; should start with " 1160d7b87b89SJosh Paetzel "either \"iqn.\", \"eui.\", or \"naa.\"", 1161009ea47eSEdward Tomasz Napierala name); 1162009ea47eSEdward Tomasz Napierala } 1163009ea47eSEdward Tomasz Napierala return (true); 1164009ea47eSEdward Tomasz Napierala } 1165009ea47eSEdward Tomasz Napierala 1166057abcb0SAlexander Motin struct pport * 1167057abcb0SAlexander Motin pport_new(struct conf *conf, const char *name, uint32_t ctl_port) 1168057abcb0SAlexander Motin { 1169057abcb0SAlexander Motin struct pport *pp; 1170057abcb0SAlexander Motin 1171057abcb0SAlexander Motin pp = calloc(1, sizeof(*pp)); 1172057abcb0SAlexander Motin if (pp == NULL) 1173057abcb0SAlexander Motin log_err(1, "calloc"); 1174057abcb0SAlexander Motin pp->pp_conf = conf; 1175057abcb0SAlexander Motin pp->pp_name = checked_strdup(name); 1176057abcb0SAlexander Motin pp->pp_ctl_port = ctl_port; 1177057abcb0SAlexander Motin TAILQ_INIT(&pp->pp_ports); 1178057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next); 1179057abcb0SAlexander Motin return (pp); 1180057abcb0SAlexander Motin } 1181057abcb0SAlexander Motin 1182057abcb0SAlexander Motin struct pport * 1183057abcb0SAlexander Motin pport_find(const struct conf *conf, const char *name) 1184057abcb0SAlexander Motin { 1185057abcb0SAlexander Motin struct pport *pp; 1186057abcb0SAlexander Motin 1187057abcb0SAlexander Motin TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) { 1188057abcb0SAlexander Motin if (strcasecmp(pp->pp_name, name) == 0) 1189057abcb0SAlexander Motin return (pp); 1190057abcb0SAlexander Motin } 1191057abcb0SAlexander Motin return (NULL); 1192057abcb0SAlexander Motin } 1193057abcb0SAlexander Motin 1194057abcb0SAlexander Motin struct pport * 1195057abcb0SAlexander Motin pport_copy(struct pport *pp, struct conf *conf) 1196057abcb0SAlexander Motin { 1197057abcb0SAlexander Motin struct pport *ppnew; 1198057abcb0SAlexander Motin 1199057abcb0SAlexander Motin ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port); 1200057abcb0SAlexander Motin return (ppnew); 1201057abcb0SAlexander Motin } 1202057abcb0SAlexander Motin 1203057abcb0SAlexander Motin void 1204057abcb0SAlexander Motin pport_delete(struct pport *pp) 1205057abcb0SAlexander Motin { 1206057abcb0SAlexander Motin struct port *port, *tport; 1207057abcb0SAlexander Motin 1208057abcb0SAlexander Motin TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport) 1209057abcb0SAlexander Motin port_delete(port); 1210057abcb0SAlexander Motin TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next); 1211057abcb0SAlexander Motin free(pp->pp_name); 1212057abcb0SAlexander Motin free(pp); 1213057abcb0SAlexander Motin } 1214057abcb0SAlexander Motin 121592847ee1SAlexander Motin struct port * 121692847ee1SAlexander Motin port_new(struct conf *conf, struct target *target, struct portal_group *pg) 121792847ee1SAlexander Motin { 121892847ee1SAlexander Motin struct port *port; 1219057abcb0SAlexander Motin char *name; 122055e4579eSAlexander Motin int ret; 122192847ee1SAlexander Motin 122255e4579eSAlexander Motin ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name); 122355e4579eSAlexander Motin if (ret <= 0) 122455e4579eSAlexander Motin log_err(1, "asprintf"); 1225057abcb0SAlexander Motin if (port_find(conf, name) != NULL) { 1226057abcb0SAlexander Motin log_warnx("duplicate port \"%s\"", name); 1227057abcb0SAlexander Motin free(name); 1228057abcb0SAlexander Motin return (NULL); 1229057abcb0SAlexander Motin } 123092847ee1SAlexander Motin port = calloc(1, sizeof(*port)); 123192847ee1SAlexander Motin if (port == NULL) 123292847ee1SAlexander Motin log_err(1, "calloc"); 123392847ee1SAlexander Motin port->p_conf = conf; 1234057abcb0SAlexander Motin port->p_name = name; 123592847ee1SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 123692847ee1SAlexander Motin TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 123792847ee1SAlexander Motin port->p_target = target; 123892847ee1SAlexander Motin TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs); 123992847ee1SAlexander Motin port->p_portal_group = pg; 1240db7bf2baSAlexander Motin port->p_foreign = pg->pg_foreign; 124192847ee1SAlexander Motin return (port); 124292847ee1SAlexander Motin } 124392847ee1SAlexander Motin 124492847ee1SAlexander Motin struct port * 1245057abcb0SAlexander Motin port_new_pp(struct conf *conf, struct target *target, struct pport *pp) 1246057abcb0SAlexander Motin { 1247057abcb0SAlexander Motin struct port *port; 1248057abcb0SAlexander Motin char *name; 124955e4579eSAlexander Motin int ret; 1250057abcb0SAlexander Motin 125155e4579eSAlexander Motin ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name); 125255e4579eSAlexander Motin if (ret <= 0) 125355e4579eSAlexander Motin log_err(1, "asprintf"); 1254057abcb0SAlexander Motin if (port_find(conf, name) != NULL) { 1255057abcb0SAlexander Motin log_warnx("duplicate port \"%s\"", name); 1256057abcb0SAlexander Motin free(name); 1257057abcb0SAlexander Motin return (NULL); 1258057abcb0SAlexander Motin } 1259057abcb0SAlexander Motin port = calloc(1, sizeof(*port)); 1260057abcb0SAlexander Motin if (port == NULL) 1261057abcb0SAlexander Motin log_err(1, "calloc"); 1262057abcb0SAlexander Motin port->p_conf = conf; 1263057abcb0SAlexander Motin port->p_name = name; 1264057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1265057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1266057abcb0SAlexander Motin port->p_target = target; 1267057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps); 1268057abcb0SAlexander Motin port->p_pport = pp; 1269057abcb0SAlexander Motin return (port); 1270057abcb0SAlexander Motin } 1271057abcb0SAlexander Motin 1272057abcb0SAlexander Motin struct port * 127392847ee1SAlexander Motin port_find(const struct conf *conf, const char *name) 127492847ee1SAlexander Motin { 127592847ee1SAlexander Motin struct port *port; 127692847ee1SAlexander Motin 127792847ee1SAlexander Motin TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 127892847ee1SAlexander Motin if (strcasecmp(port->p_name, name) == 0) 127992847ee1SAlexander Motin return (port); 128092847ee1SAlexander Motin } 128192847ee1SAlexander Motin 128292847ee1SAlexander Motin return (NULL); 128392847ee1SAlexander Motin } 128492847ee1SAlexander Motin 128592847ee1SAlexander Motin struct port * 128692847ee1SAlexander Motin port_find_in_pg(const struct portal_group *pg, const char *target) 128792847ee1SAlexander Motin { 128892847ee1SAlexander Motin struct port *port; 128992847ee1SAlexander Motin 129092847ee1SAlexander Motin TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) { 129192847ee1SAlexander Motin if (strcasecmp(port->p_target->t_name, target) == 0) 129292847ee1SAlexander Motin return (port); 129392847ee1SAlexander Motin } 129492847ee1SAlexander Motin 129592847ee1SAlexander Motin return (NULL); 129692847ee1SAlexander Motin } 129792847ee1SAlexander Motin 129892847ee1SAlexander Motin void 129992847ee1SAlexander Motin port_delete(struct port *port) 130092847ee1SAlexander Motin { 130192847ee1SAlexander Motin 130292847ee1SAlexander Motin if (port->p_portal_group) 130392847ee1SAlexander Motin TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs); 1304057abcb0SAlexander Motin if (port->p_pport) 1305057abcb0SAlexander Motin TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps); 130692847ee1SAlexander Motin if (port->p_target) 130792847ee1SAlexander Motin TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts); 130892847ee1SAlexander Motin TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next); 130992847ee1SAlexander Motin free(port->p_name); 131092847ee1SAlexander Motin free(port); 131192847ee1SAlexander Motin } 131292847ee1SAlexander Motin 1313009ea47eSEdward Tomasz Napierala struct target * 1314f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name) 1315009ea47eSEdward Tomasz Napierala { 1316009ea47eSEdward Tomasz Napierala struct target *targ; 1317009ea47eSEdward Tomasz Napierala int i, len; 1318009ea47eSEdward Tomasz Napierala 1319f7ae5bf8SEdward Tomasz Napierala targ = target_find(conf, name); 1320009ea47eSEdward Tomasz Napierala if (targ != NULL) { 1321f7ae5bf8SEdward Tomasz Napierala log_warnx("duplicated target \"%s\"", name); 1322009ea47eSEdward Tomasz Napierala return (NULL); 1323009ea47eSEdward Tomasz Napierala } 1324f7ae5bf8SEdward Tomasz Napierala if (valid_iscsi_name(name) == false) { 1325f7ae5bf8SEdward Tomasz Napierala log_warnx("target name \"%s\" is invalid", name); 1326009ea47eSEdward Tomasz Napierala return (NULL); 1327009ea47eSEdward Tomasz Napierala } 1328009ea47eSEdward Tomasz Napierala targ = calloc(1, sizeof(*targ)); 1329009ea47eSEdward Tomasz Napierala if (targ == NULL) 1330009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1331f7ae5bf8SEdward Tomasz Napierala targ->t_name = checked_strdup(name); 1332009ea47eSEdward Tomasz Napierala 1333009ea47eSEdward Tomasz Napierala /* 1334009ea47eSEdward Tomasz Napierala * RFC 3722 requires us to normalize the name to lowercase. 1335009ea47eSEdward Tomasz Napierala */ 1336f7ae5bf8SEdward Tomasz Napierala len = strlen(name); 1337009ea47eSEdward Tomasz Napierala for (i = 0; i < len; i++) 1338f7ae5bf8SEdward Tomasz Napierala targ->t_name[i] = tolower(targ->t_name[i]); 1339009ea47eSEdward Tomasz Napierala 1340009ea47eSEdward Tomasz Napierala targ->t_conf = conf; 134192847ee1SAlexander Motin TAILQ_INIT(&targ->t_ports); 1342009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 1343009ea47eSEdward Tomasz Napierala 1344009ea47eSEdward Tomasz Napierala return (targ); 1345009ea47eSEdward Tomasz Napierala } 1346009ea47eSEdward Tomasz Napierala 1347009ea47eSEdward Tomasz Napierala void 1348009ea47eSEdward Tomasz Napierala target_delete(struct target *targ) 1349009ea47eSEdward Tomasz Napierala { 135092847ee1SAlexander Motin struct port *port, *tport; 1351009ea47eSEdward Tomasz Napierala 135292847ee1SAlexander Motin TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport) 135392847ee1SAlexander Motin port_delete(port); 1354009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 1355009ea47eSEdward Tomasz Napierala 1356f7ae5bf8SEdward Tomasz Napierala free(targ->t_name); 13576dbdbf82SEdward Tomasz Napierala free(targ->t_redirection); 1358009ea47eSEdward Tomasz Napierala free(targ); 1359009ea47eSEdward Tomasz Napierala } 1360009ea47eSEdward Tomasz Napierala 1361009ea47eSEdward Tomasz Napierala struct target * 1362f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name) 1363009ea47eSEdward Tomasz Napierala { 1364009ea47eSEdward Tomasz Napierala struct target *targ; 1365009ea47eSEdward Tomasz Napierala 1366009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1367f7ae5bf8SEdward Tomasz Napierala if (strcasecmp(targ->t_name, name) == 0) 1368009ea47eSEdward Tomasz Napierala return (targ); 1369009ea47eSEdward Tomasz Napierala } 1370009ea47eSEdward Tomasz Napierala 1371009ea47eSEdward Tomasz Napierala return (NULL); 1372009ea47eSEdward Tomasz Napierala } 1373009ea47eSEdward Tomasz Napierala 13746dbdbf82SEdward Tomasz Napierala int 13756dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr) 13766dbdbf82SEdward Tomasz Napierala { 13776dbdbf82SEdward Tomasz Napierala 13786dbdbf82SEdward Tomasz Napierala if (target->t_redirection != NULL) { 13796dbdbf82SEdward Tomasz Napierala log_warnx("cannot set redirection to \"%s\" for " 13806dbdbf82SEdward Tomasz Napierala "target \"%s\"; already defined", 13816dbdbf82SEdward Tomasz Napierala addr, target->t_name); 13826dbdbf82SEdward Tomasz Napierala return (1); 13836dbdbf82SEdward Tomasz Napierala } 13846dbdbf82SEdward Tomasz Napierala 13856dbdbf82SEdward Tomasz Napierala target->t_redirection = checked_strdup(addr); 13866dbdbf82SEdward Tomasz Napierala 13876dbdbf82SEdward Tomasz Napierala return (0); 13886dbdbf82SEdward Tomasz Napierala } 13896dbdbf82SEdward Tomasz Napierala 1390009ea47eSEdward Tomasz Napierala struct lun * 1391920c6cbaSAlexander Motin lun_new(struct conf *conf, const char *name) 1392009ea47eSEdward Tomasz Napierala { 1393009ea47eSEdward Tomasz Napierala struct lun *lun; 1394009ea47eSEdward Tomasz Napierala 1395920c6cbaSAlexander Motin lun = lun_find(conf, name); 1396009ea47eSEdward Tomasz Napierala if (lun != NULL) { 1397920c6cbaSAlexander Motin log_warnx("duplicated lun \"%s\"", name); 1398009ea47eSEdward Tomasz Napierala return (NULL); 1399009ea47eSEdward Tomasz Napierala } 1400009ea47eSEdward Tomasz Napierala 1401009ea47eSEdward Tomasz Napierala lun = calloc(1, sizeof(*lun)); 1402009ea47eSEdward Tomasz Napierala if (lun == NULL) 1403009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1404920c6cbaSAlexander Motin lun->l_conf = conf; 1405920c6cbaSAlexander Motin lun->l_name = checked_strdup(name); 1406009ea47eSEdward Tomasz Napierala TAILQ_INIT(&lun->l_options); 1407920c6cbaSAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next); 14083933f7b1SAlexander Motin lun->l_ctl_lun = -1; 1409009ea47eSEdward Tomasz Napierala 1410009ea47eSEdward Tomasz Napierala return (lun); 1411009ea47eSEdward Tomasz Napierala } 1412009ea47eSEdward Tomasz Napierala 1413009ea47eSEdward Tomasz Napierala void 1414009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun) 1415009ea47eSEdward Tomasz Napierala { 1416920c6cbaSAlexander Motin struct target *targ; 1417398290f2SAlexander Motin struct option *o, *tmp; 1418920c6cbaSAlexander Motin int i; 1419009ea47eSEdward Tomasz Napierala 1420920c6cbaSAlexander Motin TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) { 1421920c6cbaSAlexander Motin for (i = 0; i < MAX_LUNS; i++) { 1422920c6cbaSAlexander Motin if (targ->t_luns[i] == lun) 1423920c6cbaSAlexander Motin targ->t_luns[i] = NULL; 1424920c6cbaSAlexander Motin } 1425920c6cbaSAlexander Motin } 1426920c6cbaSAlexander Motin TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next); 1427009ea47eSEdward Tomasz Napierala 1428398290f2SAlexander Motin TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp) 1429398290f2SAlexander Motin option_delete(&lun->l_options, o); 1430920c6cbaSAlexander Motin free(lun->l_name); 1431009ea47eSEdward Tomasz Napierala free(lun->l_backend); 1432009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 1433009ea47eSEdward Tomasz Napierala free(lun->l_path); 1434920c6cbaSAlexander Motin free(lun->l_scsiname); 1435009ea47eSEdward Tomasz Napierala free(lun->l_serial); 1436009ea47eSEdward Tomasz Napierala free(lun); 1437009ea47eSEdward Tomasz Napierala } 1438009ea47eSEdward Tomasz Napierala 1439009ea47eSEdward Tomasz Napierala struct lun * 1440920c6cbaSAlexander Motin lun_find(const struct conf *conf, const char *name) 1441009ea47eSEdward Tomasz Napierala { 1442009ea47eSEdward Tomasz Napierala struct lun *lun; 1443009ea47eSEdward Tomasz Napierala 1444920c6cbaSAlexander Motin TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1445920c6cbaSAlexander Motin if (strcmp(lun->l_name, name) == 0) 1446009ea47eSEdward Tomasz Napierala return (lun); 1447009ea47eSEdward Tomasz Napierala } 1448009ea47eSEdward Tomasz Napierala 1449009ea47eSEdward Tomasz Napierala return (NULL); 1450009ea47eSEdward Tomasz Napierala } 1451009ea47eSEdward Tomasz Napierala 1452009ea47eSEdward Tomasz Napierala void 1453009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value) 1454009ea47eSEdward Tomasz Napierala { 1455009ea47eSEdward Tomasz Napierala free(lun->l_backend); 1456009ea47eSEdward Tomasz Napierala lun->l_backend = checked_strdup(value); 1457009ea47eSEdward Tomasz Napierala } 1458009ea47eSEdward Tomasz Napierala 1459009ea47eSEdward Tomasz Napierala void 1460009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value) 1461009ea47eSEdward Tomasz Napierala { 1462009ea47eSEdward Tomasz Napierala 1463009ea47eSEdward Tomasz Napierala lun->l_blocksize = value; 1464009ea47eSEdward Tomasz Napierala } 1465009ea47eSEdward Tomasz Napierala 1466009ea47eSEdward Tomasz Napierala void 146791be33dcSAlexander Motin lun_set_device_type(struct lun *lun, uint8_t value) 146891be33dcSAlexander Motin { 146991be33dcSAlexander Motin 147091be33dcSAlexander Motin lun->l_device_type = value; 147191be33dcSAlexander Motin } 147291be33dcSAlexander Motin 147391be33dcSAlexander Motin void 1474009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value) 1475009ea47eSEdward Tomasz Napierala { 1476009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 1477009ea47eSEdward Tomasz Napierala lun->l_device_id = checked_strdup(value); 1478009ea47eSEdward Tomasz Napierala } 1479009ea47eSEdward Tomasz Napierala 1480009ea47eSEdward Tomasz Napierala void 1481009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value) 1482009ea47eSEdward Tomasz Napierala { 1483009ea47eSEdward Tomasz Napierala free(lun->l_path); 1484009ea47eSEdward Tomasz Napierala lun->l_path = checked_strdup(value); 1485009ea47eSEdward Tomasz Napierala } 1486009ea47eSEdward Tomasz Napierala 1487009ea47eSEdward Tomasz Napierala void 1488920c6cbaSAlexander Motin lun_set_scsiname(struct lun *lun, const char *value) 1489920c6cbaSAlexander Motin { 1490920c6cbaSAlexander Motin free(lun->l_scsiname); 1491920c6cbaSAlexander Motin lun->l_scsiname = checked_strdup(value); 1492920c6cbaSAlexander Motin } 1493920c6cbaSAlexander Motin 1494920c6cbaSAlexander Motin void 1495009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value) 1496009ea47eSEdward Tomasz Napierala { 1497009ea47eSEdward Tomasz Napierala free(lun->l_serial); 1498009ea47eSEdward Tomasz Napierala lun->l_serial = checked_strdup(value); 1499009ea47eSEdward Tomasz Napierala } 1500009ea47eSEdward Tomasz Napierala 1501009ea47eSEdward Tomasz Napierala void 1502009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value) 1503009ea47eSEdward Tomasz Napierala { 1504009ea47eSEdward Tomasz Napierala 1505009ea47eSEdward Tomasz Napierala lun->l_size = value; 1506009ea47eSEdward Tomasz Napierala } 1507009ea47eSEdward Tomasz Napierala 1508009ea47eSEdward Tomasz Napierala void 1509009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value) 1510009ea47eSEdward Tomasz Napierala { 1511009ea47eSEdward Tomasz Napierala 1512009ea47eSEdward Tomasz Napierala lun->l_ctl_lun = value; 1513009ea47eSEdward Tomasz Napierala } 1514009ea47eSEdward Tomasz Napierala 1515398290f2SAlexander Motin struct option * 1516398290f2SAlexander Motin option_new(struct options *options, const char *name, const char *value) 1517009ea47eSEdward Tomasz Napierala { 1518398290f2SAlexander Motin struct option *o; 1519009ea47eSEdward Tomasz Napierala 1520398290f2SAlexander Motin o = option_find(options, name); 1521398290f2SAlexander Motin if (o != NULL) { 1522398290f2SAlexander Motin log_warnx("duplicated option \"%s\"", name); 1523009ea47eSEdward Tomasz Napierala return (NULL); 1524009ea47eSEdward Tomasz Napierala } 1525009ea47eSEdward Tomasz Napierala 1526398290f2SAlexander Motin o = calloc(1, sizeof(*o)); 1527398290f2SAlexander Motin if (o == NULL) 1528009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1529398290f2SAlexander Motin o->o_name = checked_strdup(name); 1530398290f2SAlexander Motin o->o_value = checked_strdup(value); 1531398290f2SAlexander Motin TAILQ_INSERT_TAIL(options, o, o_next); 1532009ea47eSEdward Tomasz Napierala 1533398290f2SAlexander Motin return (o); 1534009ea47eSEdward Tomasz Napierala } 1535009ea47eSEdward Tomasz Napierala 1536009ea47eSEdward Tomasz Napierala void 1537398290f2SAlexander Motin option_delete(struct options *options, struct option *o) 1538009ea47eSEdward Tomasz Napierala { 1539009ea47eSEdward Tomasz Napierala 1540398290f2SAlexander Motin TAILQ_REMOVE(options, o, o_next); 1541398290f2SAlexander Motin free(o->o_name); 1542398290f2SAlexander Motin free(o->o_value); 1543398290f2SAlexander Motin free(o); 1544009ea47eSEdward Tomasz Napierala } 1545009ea47eSEdward Tomasz Napierala 1546398290f2SAlexander Motin struct option * 1547398290f2SAlexander Motin option_find(const struct options *options, const char *name) 1548009ea47eSEdward Tomasz Napierala { 1549398290f2SAlexander Motin struct option *o; 1550009ea47eSEdward Tomasz Napierala 1551398290f2SAlexander Motin TAILQ_FOREACH(o, options, o_next) { 1552398290f2SAlexander Motin if (strcmp(o->o_name, name) == 0) 1553398290f2SAlexander Motin return (o); 1554009ea47eSEdward Tomasz Napierala } 1555009ea47eSEdward Tomasz Napierala 1556009ea47eSEdward Tomasz Napierala return (NULL); 1557009ea47eSEdward Tomasz Napierala } 1558009ea47eSEdward Tomasz Napierala 1559009ea47eSEdward Tomasz Napierala void 1560398290f2SAlexander Motin option_set(struct option *o, const char *value) 1561009ea47eSEdward Tomasz Napierala { 1562009ea47eSEdward Tomasz Napierala 1563398290f2SAlexander Motin free(o->o_value); 1564398290f2SAlexander Motin o->o_value = checked_strdup(value); 1565009ea47eSEdward Tomasz Napierala } 1566009ea47eSEdward Tomasz Napierala 1567009ea47eSEdward Tomasz Napierala static struct connection * 1568073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host, 1569073edb1cSAlexander Motin const struct sockaddr *client_sa) 1570009ea47eSEdward Tomasz Napierala { 1571009ea47eSEdward Tomasz Napierala struct connection *conn; 1572009ea47eSEdward Tomasz Napierala 1573009ea47eSEdward Tomasz Napierala conn = calloc(1, sizeof(*conn)); 1574009ea47eSEdward Tomasz Napierala if (conn == NULL) 1575009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1576009ea47eSEdward Tomasz Napierala conn->conn_portal = portal; 1577009ea47eSEdward Tomasz Napierala conn->conn_socket = fd; 1578009ea47eSEdward Tomasz Napierala conn->conn_initiator_addr = checked_strdup(host); 1579073edb1cSAlexander Motin memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len); 1580009ea47eSEdward Tomasz Napierala 1581009ea47eSEdward Tomasz Napierala /* 1582009ea47eSEdward Tomasz Napierala * Default values, from RFC 3720, section 12. 1583009ea47eSEdward Tomasz Napierala */ 158497b84d34SNavdeep Parhar conn->conn_max_recv_data_segment_length = 8192; 1585*59c6e3a5SAlexander Motin conn->conn_max_send_data_segment_length = 8192; 1586009ea47eSEdward Tomasz Napierala conn->conn_max_burst_length = 262144; 158797b84d34SNavdeep Parhar conn->conn_first_burst_length = 65536; 1588009ea47eSEdward Tomasz Napierala conn->conn_immediate_data = true; 1589009ea47eSEdward Tomasz Napierala 1590009ea47eSEdward Tomasz Napierala return (conn); 1591009ea47eSEdward Tomasz Napierala } 1592009ea47eSEdward Tomasz Napierala 1593009ea47eSEdward Tomasz Napierala #if 0 1594009ea47eSEdward Tomasz Napierala static void 1595009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf) 1596009ea47eSEdward Tomasz Napierala { 1597009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1598009ea47eSEdward Tomasz Napierala struct auth *auth; 15998cb2e958SEdward Tomasz Napierala struct auth_name *auth_name; 16008cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal; 1601009ea47eSEdward Tomasz Napierala struct portal_group *pg; 1602009ea47eSEdward Tomasz Napierala struct portal *portal; 1603009ea47eSEdward Tomasz Napierala struct target *targ; 1604009ea47eSEdward Tomasz Napierala struct lun *lun; 1605398290f2SAlexander Motin struct option *o; 1606009ea47eSEdward Tomasz Napierala 1607009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1608009ea47eSEdward Tomasz Napierala fprintf(stderr, "auth-group %s {\n", ag->ag_name); 1609009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) 1610009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t chap-mutual %s %s %s %s\n", 1611009ea47eSEdward Tomasz Napierala auth->a_user, auth->a_secret, 1612009ea47eSEdward Tomasz Napierala auth->a_mutual_user, auth->a_mutual_secret); 16138cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) 16148cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-name %s\n", 16158cb2e958SEdward Tomasz Napierala auth_name->an_initator_name); 16168cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next) 16178cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-portal %s\n", 16188cb2e958SEdward Tomasz Napierala auth_portal->an_initator_portal); 1619009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1620009ea47eSEdward Tomasz Napierala } 1621009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1622009ea47eSEdward Tomasz Napierala fprintf(stderr, "portal-group %s {\n", pg->pg_name); 1623009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1624009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t listen %s\n", portal->p_listen); 1625009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1626009ea47eSEdward Tomasz Napierala } 1627920c6cbaSAlexander Motin TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1628920c6cbaSAlexander Motin fprintf(stderr, "\tlun %s {\n", lun->l_name); 1629009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\tpath %s\n", lun->l_path); 1630398290f2SAlexander Motin TAILQ_FOREACH(o, &lun->l_options, o_next) 1631009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\toption %s %s\n", 1632398290f2SAlexander Motin lo->o_name, lo->o_value); 1633009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t}\n"); 1634009ea47eSEdward Tomasz Napierala } 1635920c6cbaSAlexander Motin TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1636920c6cbaSAlexander Motin fprintf(stderr, "target %s {\n", targ->t_name); 1637920c6cbaSAlexander Motin if (targ->t_alias != NULL) 1638920c6cbaSAlexander Motin fprintf(stderr, "\t alias %s\n", targ->t_alias); 1639009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1640009ea47eSEdward Tomasz Napierala } 1641009ea47eSEdward Tomasz Napierala } 1642009ea47eSEdward Tomasz Napierala #endif 1643009ea47eSEdward Tomasz Napierala 16442fabfaa5SEdward Tomasz Napierala static int 16452fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun) 16462fabfaa5SEdward Tomasz Napierala { 16472fabfaa5SEdward Tomasz Napierala const struct lun *lun2; 16482fabfaa5SEdward Tomasz Napierala 16492fabfaa5SEdward Tomasz Napierala if (lun->l_backend == NULL) 16502fabfaa5SEdward Tomasz Napierala lun_set_backend(lun, "block"); 16512fabfaa5SEdward Tomasz Napierala if (strcmp(lun->l_backend, "block") == 0) { 16522fabfaa5SEdward Tomasz Napierala if (lun->l_path == NULL) { 1653920c6cbaSAlexander Motin log_warnx("missing path for lun \"%s\"", 1654920c6cbaSAlexander Motin lun->l_name); 16552fabfaa5SEdward Tomasz Napierala return (1); 16562fabfaa5SEdward Tomasz Napierala } 16572fabfaa5SEdward Tomasz Napierala } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 16582fabfaa5SEdward Tomasz Napierala if (lun->l_size == 0) { 1659920c6cbaSAlexander Motin log_warnx("missing size for ramdisk-backed lun \"%s\"", 1660920c6cbaSAlexander Motin lun->l_name); 16612fabfaa5SEdward Tomasz Napierala return (1); 16622fabfaa5SEdward Tomasz Napierala } 16632fabfaa5SEdward Tomasz Napierala if (lun->l_path != NULL) { 16642fabfaa5SEdward Tomasz Napierala log_warnx("path must not be specified " 1665920c6cbaSAlexander Motin "for ramdisk-backed lun \"%s\"", 1666920c6cbaSAlexander Motin lun->l_name); 16672fabfaa5SEdward Tomasz Napierala return (1); 16682fabfaa5SEdward Tomasz Napierala } 16692fabfaa5SEdward Tomasz Napierala } 16702fabfaa5SEdward Tomasz Napierala if (lun->l_blocksize == 0) { 16716f2f92a5SAlexander Motin if (lun->l_device_type == 5) 16726f2f92a5SAlexander Motin lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE); 16736f2f92a5SAlexander Motin else 16742fabfaa5SEdward Tomasz Napierala lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 16752fabfaa5SEdward Tomasz Napierala } else if (lun->l_blocksize < 0) { 1676920c6cbaSAlexander Motin log_warnx("invalid blocksize for lun \"%s\"; " 1677920c6cbaSAlexander Motin "must be larger than 0", lun->l_name); 16782fabfaa5SEdward Tomasz Napierala return (1); 16792fabfaa5SEdward Tomasz Napierala } 16802fabfaa5SEdward Tomasz Napierala if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 1681920c6cbaSAlexander Motin log_warnx("invalid size for lun \"%s\"; " 1682920c6cbaSAlexander Motin "must be multiple of blocksize", lun->l_name); 16832fabfaa5SEdward Tomasz Napierala return (1); 16842fabfaa5SEdward Tomasz Napierala } 1685920c6cbaSAlexander Motin TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) { 1686a19eebb1SEdward Tomasz Napierala if (lun == lun2) 1687a19eebb1SEdward Tomasz Napierala continue; 1688a19eebb1SEdward Tomasz Napierala if (lun->l_path != NULL && lun2->l_path != NULL && 1689a19eebb1SEdward Tomasz Napierala strcmp(lun->l_path, lun2->l_path) == 0) { 1690a19eebb1SEdward Tomasz Napierala log_debugx("WARNING: path \"%s\" duplicated " 1691920c6cbaSAlexander Motin "between lun \"%s\", and " 1692920c6cbaSAlexander Motin "lun \"%s\"", lun->l_path, 1693920c6cbaSAlexander Motin lun->l_name, lun2->l_name); 1694a19eebb1SEdward Tomasz Napierala } 1695a19eebb1SEdward Tomasz Napierala } 16962fabfaa5SEdward Tomasz Napierala 16972fabfaa5SEdward Tomasz Napierala return (0); 16982fabfaa5SEdward Tomasz Napierala } 16992fabfaa5SEdward Tomasz Napierala 1700009ea47eSEdward Tomasz Napierala int 1701009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf) 1702009ea47eSEdward Tomasz Napierala { 1703009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1704009ea47eSEdward Tomasz Napierala struct portal_group *pg; 170592847ee1SAlexander Motin struct port *port; 1706009ea47eSEdward Tomasz Napierala struct target *targ; 17072fabfaa5SEdward Tomasz Napierala struct lun *lun; 17084e5c38f8SEdward Tomasz Napierala bool found; 1709920c6cbaSAlexander Motin int error, i; 1710009ea47eSEdward Tomasz Napierala 1711009ea47eSEdward Tomasz Napierala if (conf->conf_pidfile_path == NULL) 1712009ea47eSEdward Tomasz Napierala conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1713009ea47eSEdward Tomasz Napierala 1714920c6cbaSAlexander Motin TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1715920c6cbaSAlexander Motin error = conf_verify_lun(lun); 1716920c6cbaSAlexander Motin if (error != 0) 1717920c6cbaSAlexander Motin return (error); 1718920c6cbaSAlexander Motin } 1719009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1720009ea47eSEdward Tomasz Napierala if (targ->t_auth_group == NULL) { 1721e7977549SEdward Tomasz Napierala targ->t_auth_group = auth_group_find(conf, 1722e7977549SEdward Tomasz Napierala "default"); 1723e7977549SEdward Tomasz Napierala assert(targ->t_auth_group != NULL); 1724009ea47eSEdward Tomasz Napierala } 172592847ee1SAlexander Motin if (TAILQ_EMPTY(&targ->t_ports)) { 172692847ee1SAlexander Motin pg = portal_group_find(conf, "default"); 172792847ee1SAlexander Motin assert(pg != NULL); 172892847ee1SAlexander Motin port_new(conf, targ, pg); 1729009ea47eSEdward Tomasz Napierala } 17304e5c38f8SEdward Tomasz Napierala found = false; 1731920c6cbaSAlexander Motin for (i = 0; i < MAX_LUNS; i++) { 1732920c6cbaSAlexander Motin if (targ->t_luns[i] != NULL) 17334e5c38f8SEdward Tomasz Napierala found = true; 1734009ea47eSEdward Tomasz Napierala } 17356dbdbf82SEdward Tomasz Napierala if (!found && targ->t_redirection == NULL) { 173619b59f13SEdward Tomasz Napierala log_warnx("no LUNs defined for target \"%s\"", 173719b59f13SEdward Tomasz Napierala targ->t_name); 1738009ea47eSEdward Tomasz Napierala } 17396dbdbf82SEdward Tomasz Napierala if (found && targ->t_redirection != NULL) { 17406dbdbf82SEdward Tomasz Napierala log_debugx("target \"%s\" contains luns, " 17416dbdbf82SEdward Tomasz Napierala " but configured for redirection", 17426dbdbf82SEdward Tomasz Napierala targ->t_name); 17436dbdbf82SEdward Tomasz Napierala } 1744009ea47eSEdward Tomasz Napierala } 1745009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1746009ea47eSEdward Tomasz Napierala assert(pg->pg_name != NULL); 1747009ea47eSEdward Tomasz Napierala if (pg->pg_discovery_auth_group == NULL) { 1748009ea47eSEdward Tomasz Napierala pg->pg_discovery_auth_group = 1749ccb1f04dSEdward Tomasz Napierala auth_group_find(conf, "default"); 1750009ea47eSEdward Tomasz Napierala assert(pg->pg_discovery_auth_group != NULL); 1751009ea47eSEdward Tomasz Napierala } 1752009ea47eSEdward Tomasz Napierala 175305374883SEdward Tomasz Napierala if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN) 175405374883SEdward Tomasz Napierala pg->pg_discovery_filter = PG_FILTER_NONE; 175505374883SEdward Tomasz Napierala 17566dbdbf82SEdward Tomasz Napierala if (pg->pg_redirection != NULL) { 1757db7bf2baSAlexander Motin if (!TAILQ_EMPTY(&pg->pg_ports)) { 17586dbdbf82SEdward Tomasz Napierala log_debugx("portal-group \"%s\" assigned " 175992847ee1SAlexander Motin "to target, but configured " 17606dbdbf82SEdward Tomasz Napierala "for redirection", 176192847ee1SAlexander Motin pg->pg_name); 17626dbdbf82SEdward Tomasz Napierala } 17636dbdbf82SEdward Tomasz Napierala pg->pg_unassigned = false; 1764db7bf2baSAlexander Motin } else if (!TAILQ_EMPTY(&pg->pg_ports)) { 1765db7bf2baSAlexander Motin pg->pg_unassigned = false; 17666dbdbf82SEdward Tomasz Napierala } else { 1767009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, "default") != 0) 1768009ea47eSEdward Tomasz Napierala log_warnx("portal-group \"%s\" not assigned " 1769009ea47eSEdward Tomasz Napierala "to any target", pg->pg_name); 1770009ea47eSEdward Tomasz Napierala pg->pg_unassigned = true; 17716dbdbf82SEdward Tomasz Napierala } 1772009ea47eSEdward Tomasz Napierala } 1773009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1774009ea47eSEdward Tomasz Napierala if (ag->ag_name == NULL) 1775009ea47eSEdward Tomasz Napierala assert(ag->ag_target != NULL); 1776009ea47eSEdward Tomasz Napierala else 1777009ea47eSEdward Tomasz Napierala assert(ag->ag_target == NULL); 1778009ea47eSEdward Tomasz Napierala 17794e5c38f8SEdward Tomasz Napierala found = false; 1780009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 17814e5c38f8SEdward Tomasz Napierala if (targ->t_auth_group == ag) { 17824e5c38f8SEdward Tomasz Napierala found = true; 1783009ea47eSEdward Tomasz Napierala break; 1784009ea47eSEdward Tomasz Napierala } 17854e5c38f8SEdward Tomasz Napierala } 178692847ee1SAlexander Motin TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 178792847ee1SAlexander Motin if (port->p_auth_group == ag) { 178892847ee1SAlexander Motin found = true; 178992847ee1SAlexander Motin break; 179092847ee1SAlexander Motin } 179192847ee1SAlexander Motin } 17924e5c38f8SEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 17934e5c38f8SEdward Tomasz Napierala if (pg->pg_discovery_auth_group == ag) { 17944e5c38f8SEdward Tomasz Napierala found = true; 17954e5c38f8SEdward Tomasz Napierala break; 17964e5c38f8SEdward Tomasz Napierala } 17974e5c38f8SEdward Tomasz Napierala } 17984e5c38f8SEdward Tomasz Napierala if (!found && ag->ag_name != NULL && 1799ccb1f04dSEdward Tomasz Napierala strcmp(ag->ag_name, "default") != 0 && 1800009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-authentication") != 0 && 1801009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-access") != 0) { 1802009ea47eSEdward Tomasz Napierala log_warnx("auth-group \"%s\" not assigned " 1803009ea47eSEdward Tomasz Napierala "to any target", ag->ag_name); 1804009ea47eSEdward Tomasz Napierala } 1805009ea47eSEdward Tomasz Napierala } 1806009ea47eSEdward Tomasz Napierala 1807009ea47eSEdward Tomasz Napierala return (0); 1808009ea47eSEdward Tomasz Napierala } 1809009ea47eSEdward Tomasz Napierala 1810009ea47eSEdward Tomasz Napierala static int 1811009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf) 1812009ea47eSEdward Tomasz Napierala { 1813009ea47eSEdward Tomasz Napierala struct lun *oldlun, *newlun, *tmplun; 1814009ea47eSEdward Tomasz Napierala struct portal_group *oldpg, *newpg; 1815009ea47eSEdward Tomasz Napierala struct portal *oldp, *newp; 181692847ee1SAlexander Motin struct port *oldport, *newport, *tmpport; 1817829603e2SAlexander Motin struct isns *oldns, *newns; 1818009ea47eSEdward Tomasz Napierala pid_t otherpid; 18198b94b583SAlexander Motin int changed, cumulated_error = 0, error, sockbuf; 1820009ea47eSEdward Tomasz Napierala int one = 1; 1821009ea47eSEdward Tomasz Napierala 1822009ea47eSEdward Tomasz Napierala if (oldconf->conf_debug != newconf->conf_debug) { 1823009ea47eSEdward Tomasz Napierala log_debugx("changing debug level to %d", newconf->conf_debug); 1824009ea47eSEdward Tomasz Napierala log_init(newconf->conf_debug); 1825009ea47eSEdward Tomasz Napierala } 1826009ea47eSEdward Tomasz Napierala 1827009ea47eSEdward Tomasz Napierala if (oldconf->conf_pidfh != NULL) { 1828009ea47eSEdward Tomasz Napierala assert(oldconf->conf_pidfile_path != NULL); 1829009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfile_path != NULL && 1830009ea47eSEdward Tomasz Napierala strcmp(oldconf->conf_pidfile_path, 1831009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path) == 0) { 1832009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = oldconf->conf_pidfh; 1833009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1834009ea47eSEdward Tomasz Napierala } else { 1835009ea47eSEdward Tomasz Napierala log_debugx("removing pidfile %s", 1836009ea47eSEdward Tomasz Napierala oldconf->conf_pidfile_path); 1837009ea47eSEdward Tomasz Napierala pidfile_remove(oldconf->conf_pidfh); 1838009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1839009ea47eSEdward Tomasz Napierala } 1840009ea47eSEdward Tomasz Napierala } 1841009ea47eSEdward Tomasz Napierala 1842009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1843009ea47eSEdward Tomasz Napierala log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1844009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = 1845009ea47eSEdward Tomasz Napierala pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1846009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL) { 1847009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 1848009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 1849009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 1850009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 1851009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path); 1852009ea47eSEdward Tomasz Napierala } 1853009ea47eSEdward Tomasz Napierala } 1854009ea47eSEdward Tomasz Napierala 18556b31e130SAlexander Motin /* 18566b31e130SAlexander Motin * Go through the new portal groups, assigning tags or preserving old. 18576b31e130SAlexander Motin */ 18586b31e130SAlexander Motin TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1859db7bf2baSAlexander Motin if (newpg->pg_tag != 0) 1860db7bf2baSAlexander Motin continue; 18616b31e130SAlexander Motin oldpg = portal_group_find(oldconf, newpg->pg_name); 18626b31e130SAlexander Motin if (oldpg != NULL) 18636b31e130SAlexander Motin newpg->pg_tag = oldpg->pg_tag; 18646b31e130SAlexander Motin else 18656b31e130SAlexander Motin newpg->pg_tag = ++last_portal_group_tag; 18666b31e130SAlexander Motin } 18676b31e130SAlexander Motin 1868829603e2SAlexander Motin /* Deregister on removed iSNS servers. */ 1869829603e2SAlexander Motin TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1870829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1871829603e2SAlexander Motin if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1872829603e2SAlexander Motin break; 1873829603e2SAlexander Motin } 1874829603e2SAlexander Motin if (newns == NULL) 1875829603e2SAlexander Motin isns_deregister(oldns); 1876829603e2SAlexander Motin } 1877829603e2SAlexander Motin 1878d5e316e5SEdward Tomasz Napierala /* 1879d5e316e5SEdward Tomasz Napierala * XXX: If target or lun removal fails, we should somehow "move" 1880d5e316e5SEdward Tomasz Napierala * the old lun or target into newconf, so that subsequent 188161a2a354SEdward Tomasz Napierala * conf_apply() would try to remove them again. That would 188261a2a354SEdward Tomasz Napierala * be somewhat hairy, though, and lun deletion failures don't 188361a2a354SEdward Tomasz Napierala * really happen, so leave it as it is for now. 1884d5e316e5SEdward Tomasz Napierala */ 1885009ea47eSEdward Tomasz Napierala /* 188692847ee1SAlexander Motin * First, remove any ports present in the old configuration 1887009ea47eSEdward Tomasz Napierala * and missing in the new one. 1888009ea47eSEdward Tomasz Napierala */ 188992847ee1SAlexander Motin TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) { 1890db7bf2baSAlexander Motin if (oldport->p_foreign) 1891db7bf2baSAlexander Motin continue; 189292847ee1SAlexander Motin newport = port_find(newconf, oldport->p_name); 1893db7bf2baSAlexander Motin if (newport != NULL && !newport->p_foreign) 1894920c6cbaSAlexander Motin continue; 1895057abcb0SAlexander Motin log_debugx("removing port \"%s\"", oldport->p_name); 189692847ee1SAlexander Motin error = kernel_port_remove(oldport); 18976de78f9fSAlexander Motin if (error != 0) { 189892847ee1SAlexander Motin log_warnx("failed to remove port %s", 189992847ee1SAlexander Motin oldport->p_name); 19006de78f9fSAlexander Motin /* 19016de78f9fSAlexander Motin * XXX: Uncomment after fixing the root cause. 19026de78f9fSAlexander Motin * 19036de78f9fSAlexander Motin * cumulated_error++; 19046de78f9fSAlexander Motin */ 19056de78f9fSAlexander Motin } 1906009ea47eSEdward Tomasz Napierala } 1907009ea47eSEdward Tomasz Napierala 1908009ea47eSEdward Tomasz Napierala /* 1909920c6cbaSAlexander Motin * Second, remove any LUNs present in the old configuration 1910009ea47eSEdward Tomasz Napierala * and missing in the new one. 1911009ea47eSEdward Tomasz Napierala */ 1912920c6cbaSAlexander Motin TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) { 1913920c6cbaSAlexander Motin newlun = lun_find(newconf, oldlun->l_name); 1914009ea47eSEdward Tomasz Napierala if (newlun == NULL) { 1915920c6cbaSAlexander Motin log_debugx("lun \"%s\", CTL lun %d " 1916cadf3831SEdward Tomasz Napierala "not found in new configuration; " 1917920c6cbaSAlexander Motin "removing", oldlun->l_name, oldlun->l_ctl_lun); 1918009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1919009ea47eSEdward Tomasz Napierala if (error != 0) { 1920920c6cbaSAlexander Motin log_warnx("failed to remove lun \"%s\", " 1921920c6cbaSAlexander Motin "CTL lun %d", 1922920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1923009ea47eSEdward Tomasz Napierala cumulated_error++; 1924009ea47eSEdward Tomasz Napierala } 1925009ea47eSEdward Tomasz Napierala continue; 1926009ea47eSEdward Tomasz Napierala } 1927009ea47eSEdward Tomasz Napierala 1928009ea47eSEdward Tomasz Napierala /* 1929009ea47eSEdward Tomasz Napierala * Also remove the LUNs changed by more than size. 1930009ea47eSEdward Tomasz Napierala */ 1931009ea47eSEdward Tomasz Napierala changed = 0; 1932009ea47eSEdward Tomasz Napierala assert(oldlun->l_backend != NULL); 1933009ea47eSEdward Tomasz Napierala assert(newlun->l_backend != NULL); 1934009ea47eSEdward Tomasz Napierala if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1935920c6cbaSAlexander Motin log_debugx("backend for lun \"%s\", " 1936009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1937920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1938009ea47eSEdward Tomasz Napierala changed = 1; 1939009ea47eSEdward Tomasz Napierala } 1940009ea47eSEdward Tomasz Napierala if (oldlun->l_blocksize != newlun->l_blocksize) { 1941920c6cbaSAlexander Motin log_debugx("blocksize for lun \"%s\", " 1942009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1943920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1944009ea47eSEdward Tomasz Napierala changed = 1; 1945009ea47eSEdward Tomasz Napierala } 1946009ea47eSEdward Tomasz Napierala if (newlun->l_device_id != NULL && 1947009ea47eSEdward Tomasz Napierala (oldlun->l_device_id == NULL || 1948009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_device_id, newlun->l_device_id) != 1949009ea47eSEdward Tomasz Napierala 0)) { 1950920c6cbaSAlexander Motin log_debugx("device-id for lun \"%s\", " 1951009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1952920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1953009ea47eSEdward Tomasz Napierala changed = 1; 1954009ea47eSEdward Tomasz Napierala } 1955009ea47eSEdward Tomasz Napierala if (newlun->l_path != NULL && 1956009ea47eSEdward Tomasz Napierala (oldlun->l_path == NULL || 1957009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1958920c6cbaSAlexander Motin log_debugx("path for lun \"%s\", " 1959009ea47eSEdward Tomasz Napierala "CTL lun %d, changed; removing", 1960920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1961009ea47eSEdward Tomasz Napierala changed = 1; 1962009ea47eSEdward Tomasz Napierala } 1963009ea47eSEdward Tomasz Napierala if (newlun->l_serial != NULL && 1964009ea47eSEdward Tomasz Napierala (oldlun->l_serial == NULL || 1965009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1966920c6cbaSAlexander Motin log_debugx("serial for lun \"%s\", " 1967009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1968920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1969009ea47eSEdward Tomasz Napierala changed = 1; 1970009ea47eSEdward Tomasz Napierala } 1971009ea47eSEdward Tomasz Napierala if (changed) { 1972009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1973009ea47eSEdward Tomasz Napierala if (error != 0) { 1974920c6cbaSAlexander Motin log_warnx("failed to remove lun \"%s\", " 1975920c6cbaSAlexander Motin "CTL lun %d", 1976920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1977009ea47eSEdward Tomasz Napierala cumulated_error++; 1978009ea47eSEdward Tomasz Napierala } 1979009ea47eSEdward Tomasz Napierala lun_delete(oldlun); 1980009ea47eSEdward Tomasz Napierala continue; 1981009ea47eSEdward Tomasz Napierala } 1982009ea47eSEdward Tomasz Napierala 1983009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1984009ea47eSEdward Tomasz Napierala } 1985920c6cbaSAlexander Motin 1986920c6cbaSAlexander Motin TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) { 1987920c6cbaSAlexander Motin oldlun = lun_find(oldconf, newlun->l_name); 1988920c6cbaSAlexander Motin if (oldlun != NULL) { 1989a3977beaSAlexander Motin log_debugx("modifying lun \"%s\", CTL lun %d", 1990920c6cbaSAlexander Motin newlun->l_name, newlun->l_ctl_lun); 1991a3977beaSAlexander Motin error = kernel_lun_modify(newlun); 1992920c6cbaSAlexander Motin if (error != 0) { 1993920c6cbaSAlexander Motin log_warnx("failed to " 1994a3977beaSAlexander Motin "modify lun \"%s\", CTL lun %d", 1995a3977beaSAlexander Motin newlun->l_name, newlun->l_ctl_lun); 1996920c6cbaSAlexander Motin cumulated_error++; 1997920c6cbaSAlexander Motin } 1998920c6cbaSAlexander Motin continue; 1999920c6cbaSAlexander Motin } 2000920c6cbaSAlexander Motin log_debugx("adding lun \"%s\"", newlun->l_name); 2001920c6cbaSAlexander Motin error = kernel_lun_add(newlun); 2002920c6cbaSAlexander Motin if (error != 0) { 2003920c6cbaSAlexander Motin log_warnx("failed to add lun \"%s\"", newlun->l_name); 2004920c6cbaSAlexander Motin lun_delete(newlun); 2005920c6cbaSAlexander Motin cumulated_error++; 2006920c6cbaSAlexander Motin } 2007009ea47eSEdward Tomasz Napierala } 2008009ea47eSEdward Tomasz Napierala 2009009ea47eSEdward Tomasz Napierala /* 201092847ee1SAlexander Motin * Now add new ports or modify existing ones. 2011009ea47eSEdward Tomasz Napierala */ 201292847ee1SAlexander Motin TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) { 2013db7bf2baSAlexander Motin if (newport->p_foreign) 2014db7bf2baSAlexander Motin continue; 201592847ee1SAlexander Motin oldport = port_find(oldconf, newport->p_name); 2016009ea47eSEdward Tomasz Napierala 2017db7bf2baSAlexander Motin if (oldport == NULL || oldport->p_foreign) { 2018057abcb0SAlexander Motin log_debugx("adding port \"%s\"", newport->p_name); 201992847ee1SAlexander Motin error = kernel_port_add(newport); 202092847ee1SAlexander Motin } else { 2021057abcb0SAlexander Motin log_debugx("updating port \"%s\"", newport->p_name); 202292847ee1SAlexander Motin newport->p_ctl_port = oldport->p_ctl_port; 2023828524c1SAlexander Motin error = kernel_port_update(newport, oldport); 2024920c6cbaSAlexander Motin } 2025e48bb73aSEdward Tomasz Napierala if (error != 0) { 202692847ee1SAlexander Motin log_warnx("failed to %s port %s", 202792847ee1SAlexander Motin (oldport == NULL) ? "add" : "update", 202892847ee1SAlexander Motin newport->p_name); 2029e48bb73aSEdward Tomasz Napierala /* 2030e48bb73aSEdward Tomasz Napierala * XXX: Uncomment after fixing the root cause. 2031e48bb73aSEdward Tomasz Napierala * 2032e48bb73aSEdward Tomasz Napierala * cumulated_error++; 2033e48bb73aSEdward Tomasz Napierala */ 2034e48bb73aSEdward Tomasz Napierala } 2035e48bb73aSEdward Tomasz Napierala } 2036009ea47eSEdward Tomasz Napierala 2037009ea47eSEdward Tomasz Napierala /* 2038463a577bSEitan Adler * Go through the new portals, opening the sockets as necessary. 2039009ea47eSEdward Tomasz Napierala */ 2040009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 2041db7bf2baSAlexander Motin if (newpg->pg_foreign) 2042db7bf2baSAlexander Motin continue; 2043009ea47eSEdward Tomasz Napierala if (newpg->pg_unassigned) { 2044009ea47eSEdward Tomasz Napierala log_debugx("not listening on portal-group \"%s\", " 2045009ea47eSEdward Tomasz Napierala "not assigned to any target", 2046009ea47eSEdward Tomasz Napierala newpg->pg_name); 2047009ea47eSEdward Tomasz Napierala continue; 2048009ea47eSEdward Tomasz Napierala } 2049009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 2050009ea47eSEdward Tomasz Napierala /* 2051009ea47eSEdward Tomasz Napierala * Try to find already open portal and reuse 2052009ea47eSEdward Tomasz Napierala * the listening socket. We don't care about 2053009ea47eSEdward Tomasz Napierala * what portal or portal group that was, what 2054009ea47eSEdward Tomasz Napierala * matters is the listening address. 2055009ea47eSEdward Tomasz Napierala */ 2056009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 2057009ea47eSEdward Tomasz Napierala pg_next) { 2058009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, 2059009ea47eSEdward Tomasz Napierala p_next) { 2060009ea47eSEdward Tomasz Napierala if (strcmp(newp->p_listen, 2061009ea47eSEdward Tomasz Napierala oldp->p_listen) == 0 && 2062009ea47eSEdward Tomasz Napierala oldp->p_socket > 0) { 2063009ea47eSEdward Tomasz Napierala newp->p_socket = 2064009ea47eSEdward Tomasz Napierala oldp->p_socket; 2065009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 2066009ea47eSEdward Tomasz Napierala break; 2067009ea47eSEdward Tomasz Napierala } 2068009ea47eSEdward Tomasz Napierala } 2069009ea47eSEdward Tomasz Napierala } 2070009ea47eSEdward Tomasz Napierala if (newp->p_socket > 0) { 2071009ea47eSEdward Tomasz Napierala /* 2072009ea47eSEdward Tomasz Napierala * We're done with this portal. 2073009ea47eSEdward Tomasz Napierala */ 2074009ea47eSEdward Tomasz Napierala continue; 2075009ea47eSEdward Tomasz Napierala } 2076009ea47eSEdward Tomasz Napierala 2077009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2078ba3a2d31SEdward Tomasz Napierala if (proxy_mode) { 20798cab2ed4SEdward Tomasz Napierala newpg->pg_conf->conf_portal_id++; 20808cab2ed4SEdward Tomasz Napierala newp->p_id = newpg->pg_conf->conf_portal_id; 20818cab2ed4SEdward Tomasz Napierala log_debugx("listening on %s, portal-group " 20828cab2ed4SEdward Tomasz Napierala "\"%s\", portal id %d, using ICL proxy", 20838cab2ed4SEdward Tomasz Napierala newp->p_listen, newpg->pg_name, newp->p_id); 20848cab2ed4SEdward Tomasz Napierala kernel_listen(newp->p_ai, newp->p_iser, 20858cab2ed4SEdward Tomasz Napierala newp->p_id); 2086ba3a2d31SEdward Tomasz Napierala continue; 2087ba3a2d31SEdward Tomasz Napierala } 2088ba3a2d31SEdward Tomasz Napierala #endif 2089ba3a2d31SEdward Tomasz Napierala assert(proxy_mode == false); 2090009ea47eSEdward Tomasz Napierala assert(newp->p_iser == false); 2091009ea47eSEdward Tomasz Napierala 2092009ea47eSEdward Tomasz Napierala log_debugx("listening on %s, portal-group \"%s\"", 2093009ea47eSEdward Tomasz Napierala newp->p_listen, newpg->pg_name); 2094009ea47eSEdward Tomasz Napierala newp->p_socket = socket(newp->p_ai->ai_family, 2095009ea47eSEdward Tomasz Napierala newp->p_ai->ai_socktype, 2096009ea47eSEdward Tomasz Napierala newp->p_ai->ai_protocol); 2097009ea47eSEdward Tomasz Napierala if (newp->p_socket < 0) { 2098009ea47eSEdward Tomasz Napierala log_warn("socket(2) failed for %s", 2099009ea47eSEdward Tomasz Napierala newp->p_listen); 2100009ea47eSEdward Tomasz Napierala cumulated_error++; 2101009ea47eSEdward Tomasz Napierala continue; 2102009ea47eSEdward Tomasz Napierala } 21038b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 21048b94b583SAlexander Motin if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF, 21058b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 21068b94b583SAlexander Motin log_warn("setsockopt(SO_RCVBUF) failed " 21078b94b583SAlexander Motin "for %s", newp->p_listen); 21088b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 21098b94b583SAlexander Motin if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF, 21108b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 21118b94b583SAlexander Motin log_warn("setsockopt(SO_SNDBUF) failed " 21128b94b583SAlexander Motin "for %s", newp->p_listen); 2113009ea47eSEdward Tomasz Napierala error = setsockopt(newp->p_socket, SOL_SOCKET, 2114009ea47eSEdward Tomasz Napierala SO_REUSEADDR, &one, sizeof(one)); 2115009ea47eSEdward Tomasz Napierala if (error != 0) { 2116009ea47eSEdward Tomasz Napierala log_warn("setsockopt(SO_REUSEADDR) failed " 2117009ea47eSEdward Tomasz Napierala "for %s", newp->p_listen); 2118009ea47eSEdward Tomasz Napierala close(newp->p_socket); 2119009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 2120009ea47eSEdward Tomasz Napierala cumulated_error++; 2121009ea47eSEdward Tomasz Napierala continue; 2122009ea47eSEdward Tomasz Napierala } 2123009ea47eSEdward Tomasz Napierala error = bind(newp->p_socket, newp->p_ai->ai_addr, 2124009ea47eSEdward Tomasz Napierala newp->p_ai->ai_addrlen); 2125009ea47eSEdward Tomasz Napierala if (error != 0) { 2126009ea47eSEdward Tomasz Napierala log_warn("bind(2) failed for %s", 2127009ea47eSEdward Tomasz Napierala newp->p_listen); 2128009ea47eSEdward Tomasz Napierala close(newp->p_socket); 2129009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 2130009ea47eSEdward Tomasz Napierala cumulated_error++; 2131009ea47eSEdward Tomasz Napierala continue; 2132009ea47eSEdward Tomasz Napierala } 2133009ea47eSEdward Tomasz Napierala error = listen(newp->p_socket, -1); 2134009ea47eSEdward Tomasz Napierala if (error != 0) { 2135009ea47eSEdward Tomasz Napierala log_warn("listen(2) failed for %s", 2136009ea47eSEdward Tomasz Napierala newp->p_listen); 2137009ea47eSEdward Tomasz Napierala close(newp->p_socket); 2138009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 2139009ea47eSEdward Tomasz Napierala cumulated_error++; 2140009ea47eSEdward Tomasz Napierala continue; 2141009ea47eSEdward Tomasz Napierala } 2142009ea47eSEdward Tomasz Napierala } 2143009ea47eSEdward Tomasz Napierala } 2144009ea47eSEdward Tomasz Napierala 2145009ea47eSEdward Tomasz Napierala /* 2146009ea47eSEdward Tomasz Napierala * Go through the no longer used sockets, closing them. 2147009ea47eSEdward Tomasz Napierala */ 2148009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 2149009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 2150009ea47eSEdward Tomasz Napierala if (oldp->p_socket <= 0) 2151009ea47eSEdward Tomasz Napierala continue; 2152009ea47eSEdward Tomasz Napierala log_debugx("closing socket for %s, portal-group \"%s\"", 2153009ea47eSEdward Tomasz Napierala oldp->p_listen, oldpg->pg_name); 2154009ea47eSEdward Tomasz Napierala close(oldp->p_socket); 2155009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 2156009ea47eSEdward Tomasz Napierala } 2157009ea47eSEdward Tomasz Napierala } 2158009ea47eSEdward Tomasz Napierala 2159829603e2SAlexander Motin /* (Re-)Register on remaining/new iSNS servers. */ 2160829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 2161829603e2SAlexander Motin TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 2162829603e2SAlexander Motin if (strcmp(oldns->i_addr, newns->i_addr) == 0) 2163829603e2SAlexander Motin break; 2164829603e2SAlexander Motin } 2165829603e2SAlexander Motin isns_register(newns, oldns); 2166829603e2SAlexander Motin } 2167829603e2SAlexander Motin 2168829603e2SAlexander Motin /* Schedule iSNS update */ 2169829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) 2170829603e2SAlexander Motin set_timeout((newconf->conf_isns_period + 2) / 3, false); 2171829603e2SAlexander Motin 2172009ea47eSEdward Tomasz Napierala return (cumulated_error); 2173009ea47eSEdward Tomasz Napierala } 2174009ea47eSEdward Tomasz Napierala 2175009ea47eSEdward Tomasz Napierala bool 2176009ea47eSEdward Tomasz Napierala timed_out(void) 2177009ea47eSEdward Tomasz Napierala { 2178009ea47eSEdward Tomasz Napierala 2179009ea47eSEdward Tomasz Napierala return (sigalrm_received); 2180009ea47eSEdward Tomasz Napierala } 2181009ea47eSEdward Tomasz Napierala 2182009ea47eSEdward Tomasz Napierala static void 2183829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused) 2184009ea47eSEdward Tomasz Napierala { 2185009ea47eSEdward Tomasz Napierala /* 2186009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 2187009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 2188009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 2189009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 2190009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 2191009ea47eSEdward Tomasz Napierala */ 2192009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 2193009ea47eSEdward Tomasz Napierala /* 2194009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 2195009ea47eSEdward Tomasz Napierala */ 2196009ea47eSEdward Tomasz Napierala _exit(2); 2197009ea47eSEdward Tomasz Napierala } 2198009ea47eSEdward Tomasz Napierala 2199009ea47eSEdward Tomasz Napierala sigalrm_received = true; 2200009ea47eSEdward Tomasz Napierala } 2201009ea47eSEdward Tomasz Napierala 2202009ea47eSEdward Tomasz Napierala static void 2203829603e2SAlexander Motin sigalrm_handler(int dummy __unused) 2204829603e2SAlexander Motin { 2205829603e2SAlexander Motin 2206829603e2SAlexander Motin sigalrm_received = true; 2207829603e2SAlexander Motin } 2208829603e2SAlexander Motin 2209829603e2SAlexander Motin void 2210829603e2SAlexander Motin set_timeout(int timeout, int fatal) 2211009ea47eSEdward Tomasz Napierala { 2212009ea47eSEdward Tomasz Napierala struct sigaction sa; 2213009ea47eSEdward Tomasz Napierala struct itimerval itv; 2214009ea47eSEdward Tomasz Napierala int error; 2215009ea47eSEdward Tomasz Napierala 2216829603e2SAlexander Motin if (timeout <= 0) { 2217009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 2218829603e2SAlexander Motin bzero(&itv, sizeof(itv)); 2219829603e2SAlexander Motin error = setitimer(ITIMER_REAL, &itv, NULL); 2220829603e2SAlexander Motin if (error != 0) 2221829603e2SAlexander Motin log_err(1, "setitimer"); 2222829603e2SAlexander Motin sigalrm_received = false; 2223009ea47eSEdward Tomasz Napierala return; 2224009ea47eSEdward Tomasz Napierala } 2225009ea47eSEdward Tomasz Napierala 2226829603e2SAlexander Motin sigalrm_received = false; 2227009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 2228829603e2SAlexander Motin if (fatal) 2229829603e2SAlexander Motin sa.sa_handler = sigalrm_handler_fatal; 2230829603e2SAlexander Motin else 2231009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 2232009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 2233009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 2234009ea47eSEdward Tomasz Napierala if (error != 0) 2235009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2236009ea47eSEdward Tomasz Napierala 2237009ea47eSEdward Tomasz Napierala /* 2238009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 2239009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 2240009ea47eSEdward Tomasz Napierala */ 2241829603e2SAlexander Motin log_debugx("setting session timeout to %d seconds", timeout); 2242009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 2243009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 2244829603e2SAlexander Motin itv.it_value.tv_sec = timeout; 2245009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 2246009ea47eSEdward Tomasz Napierala if (error != 0) 2247009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 2248009ea47eSEdward Tomasz Napierala } 2249009ea47eSEdward Tomasz Napierala 2250009ea47eSEdward Tomasz Napierala static int 2251009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 2252009ea47eSEdward Tomasz Napierala { 2253009ea47eSEdward Tomasz Napierala pid_t pid; 2254009ea47eSEdward Tomasz Napierala int status; 2255009ea47eSEdward Tomasz Napierala int num = 0; 2256009ea47eSEdward Tomasz Napierala 2257009ea47eSEdward Tomasz Napierala for (;;) { 2258009ea47eSEdward Tomasz Napierala /* 2259009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 2260009ea47eSEdward Tomasz Napierala */ 2261009ea47eSEdward Tomasz Napierala if (block && num == 0) 2262009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 2263009ea47eSEdward Tomasz Napierala else 2264009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 2265009ea47eSEdward Tomasz Napierala if (pid <= 0) 2266009ea47eSEdward Tomasz Napierala break; 2267009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 2268009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 2269009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 2270009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 2271009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 2272009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 2273009ea47eSEdward Tomasz Napierala } else { 2274009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 2275009ea47eSEdward Tomasz Napierala } 2276009ea47eSEdward Tomasz Napierala num++; 2277009ea47eSEdward Tomasz Napierala } 2278009ea47eSEdward Tomasz Napierala 2279009ea47eSEdward Tomasz Napierala return (num); 2280009ea47eSEdward Tomasz Napierala } 2281009ea47eSEdward Tomasz Napierala 2282009ea47eSEdward Tomasz Napierala static void 22838eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd, 2284073edb1cSAlexander Motin const struct sockaddr *client_sa, bool dont_fork) 2285009ea47eSEdward Tomasz Napierala { 2286009ea47eSEdward Tomasz Napierala struct connection *conn; 2287009ea47eSEdward Tomasz Napierala int error; 2288009ea47eSEdward Tomasz Napierala pid_t pid; 2289009ea47eSEdward Tomasz Napierala char host[NI_MAXHOST + 1]; 2290009ea47eSEdward Tomasz Napierala struct conf *conf; 2291009ea47eSEdward Tomasz Napierala 2292009ea47eSEdward Tomasz Napierala conf = portal->p_portal_group->pg_conf; 2293009ea47eSEdward Tomasz Napierala 2294009ea47eSEdward Tomasz Napierala if (dont_fork) { 2295009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; not forking due to -d flag"); 2296009ea47eSEdward Tomasz Napierala } else { 2297009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 2298009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2299009ea47eSEdward Tomasz Napierala 2300009ea47eSEdward Tomasz Napierala while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 2301009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 2302009ea47eSEdward Tomasz Napierala "waiting for child process to exit", conf->conf_maxproc); 2303009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 2304009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2305009ea47eSEdward Tomasz Napierala } 2306009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 2307009ea47eSEdward Tomasz Napierala nchildren); 2308009ea47eSEdward Tomasz Napierala nchildren++; 2309009ea47eSEdward Tomasz Napierala pid = fork(); 2310009ea47eSEdward Tomasz Napierala if (pid < 0) 2311009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 2312009ea47eSEdward Tomasz Napierala if (pid > 0) { 2313009ea47eSEdward Tomasz Napierala close(fd); 2314009ea47eSEdward Tomasz Napierala return; 2315009ea47eSEdward Tomasz Napierala } 2316009ea47eSEdward Tomasz Napierala } 2317009ea47eSEdward Tomasz Napierala pidfile_close(conf->conf_pidfh); 2318009ea47eSEdward Tomasz Napierala 2319073edb1cSAlexander Motin error = getnameinfo(client_sa, client_sa->sa_len, 2320009ea47eSEdward Tomasz Napierala host, sizeof(host), NULL, 0, NI_NUMERICHOST); 2321009ea47eSEdward Tomasz Napierala if (error != 0) 23229a95cfffSEdward Tomasz Napierala log_errx(1, "getnameinfo: %s", gai_strerror(error)); 2323009ea47eSEdward Tomasz Napierala 2324009ea47eSEdward Tomasz Napierala log_debugx("accepted connection from %s; portal group \"%s\"", 2325009ea47eSEdward Tomasz Napierala host, portal->p_portal_group->pg_name); 2326009ea47eSEdward Tomasz Napierala log_set_peer_addr(host); 2327009ea47eSEdward Tomasz Napierala setproctitle("%s", host); 2328009ea47eSEdward Tomasz Napierala 2329073edb1cSAlexander Motin conn = connection_new(portal, fd, host, client_sa); 2330829603e2SAlexander Motin set_timeout(conf->conf_timeout, true); 2331009ea47eSEdward Tomasz Napierala kernel_capsicate(); 2332009ea47eSEdward Tomasz Napierala login(conn); 2333009ea47eSEdward Tomasz Napierala if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 2334009ea47eSEdward Tomasz Napierala kernel_handoff(conn); 2335009ea47eSEdward Tomasz Napierala log_debugx("connection handed off to the kernel"); 2336009ea47eSEdward Tomasz Napierala } else { 2337009ea47eSEdward Tomasz Napierala assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 2338009ea47eSEdward Tomasz Napierala discovery(conn); 2339009ea47eSEdward Tomasz Napierala } 2340009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 2341009ea47eSEdward Tomasz Napierala exit(0); 2342009ea47eSEdward Tomasz Napierala } 2343009ea47eSEdward Tomasz Napierala 2344009ea47eSEdward Tomasz Napierala static int 2345009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds) 2346009ea47eSEdward Tomasz Napierala { 2347009ea47eSEdward Tomasz Napierala 2348009ea47eSEdward Tomasz Napierala /* 2349009ea47eSEdward Tomasz Napierala * Skip sockets which we failed to bind. 2350009ea47eSEdward Tomasz Napierala */ 2351009ea47eSEdward Tomasz Napierala if (fd <= 0) 2352009ea47eSEdward Tomasz Napierala return (nfds); 2353009ea47eSEdward Tomasz Napierala 2354009ea47eSEdward Tomasz Napierala FD_SET(fd, fdset); 2355009ea47eSEdward Tomasz Napierala if (fd > nfds) 2356009ea47eSEdward Tomasz Napierala nfds = fd; 2357009ea47eSEdward Tomasz Napierala return (nfds); 2358009ea47eSEdward Tomasz Napierala } 2359009ea47eSEdward Tomasz Napierala 2360009ea47eSEdward Tomasz Napierala static void 2361009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork) 2362009ea47eSEdward Tomasz Napierala { 2363009ea47eSEdward Tomasz Napierala struct portal_group *pg; 2364009ea47eSEdward Tomasz Napierala struct portal *portal; 23659a95cfffSEdward Tomasz Napierala struct sockaddr_storage client_sa; 23669a95cfffSEdward Tomasz Napierala socklen_t client_salen; 2367009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2368009ea47eSEdward Tomasz Napierala int connection_id; 23698cab2ed4SEdward Tomasz Napierala int portal_id; 2370ba3a2d31SEdward Tomasz Napierala #endif 2371009ea47eSEdward Tomasz Napierala fd_set fdset; 2372009ea47eSEdward Tomasz Napierala int error, nfds, client_fd; 2373009ea47eSEdward Tomasz Napierala 2374009ea47eSEdward Tomasz Napierala pidfile_write(conf->conf_pidfh); 2375009ea47eSEdward Tomasz Napierala 2376009ea47eSEdward Tomasz Napierala for (;;) { 2377829603e2SAlexander Motin if (sighup_received || sigterm_received || timed_out()) 2378009ea47eSEdward Tomasz Napierala return; 2379009ea47eSEdward Tomasz Napierala 2380009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2381ba3a2d31SEdward Tomasz Napierala if (proxy_mode) { 23828eab95d6SEdward Tomasz Napierala client_salen = sizeof(client_sa); 23838eab95d6SEdward Tomasz Napierala kernel_accept(&connection_id, &portal_id, 23848eab95d6SEdward Tomasz Napierala (struct sockaddr *)&client_sa, &client_salen); 23850488e848SEdward Tomasz Napierala assert(client_salen >= client_sa.ss_len); 2386009ea47eSEdward Tomasz Napierala 23878cab2ed4SEdward Tomasz Napierala log_debugx("incoming connection, id %d, portal id %d", 23888cab2ed4SEdward Tomasz Napierala connection_id, portal_id); 23898cab2ed4SEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 23908cab2ed4SEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 23918cab2ed4SEdward Tomasz Napierala if (portal->p_id == portal_id) { 23928cab2ed4SEdward Tomasz Napierala goto found; 23938cab2ed4SEdward Tomasz Napierala } 23948cab2ed4SEdward Tomasz Napierala } 23958cab2ed4SEdward Tomasz Napierala } 2396009ea47eSEdward Tomasz Napierala 23978cab2ed4SEdward Tomasz Napierala log_errx(1, "kernel returned invalid portal_id %d", 23988cab2ed4SEdward Tomasz Napierala portal_id); 23998cab2ed4SEdward Tomasz Napierala 24008cab2ed4SEdward Tomasz Napierala found: 24018eab95d6SEdward Tomasz Napierala handle_connection(portal, connection_id, 2402073edb1cSAlexander Motin (struct sockaddr *)&client_sa, dont_fork); 2403ba3a2d31SEdward Tomasz Napierala } else { 2404ba3a2d31SEdward Tomasz Napierala #endif 2405ba3a2d31SEdward Tomasz Napierala assert(proxy_mode == false); 2406ba3a2d31SEdward Tomasz Napierala 2407009ea47eSEdward Tomasz Napierala FD_ZERO(&fdset); 2408009ea47eSEdward Tomasz Napierala nfds = 0; 2409009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2410009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 2411009ea47eSEdward Tomasz Napierala nfds = fd_add(portal->p_socket, &fdset, nfds); 2412009ea47eSEdward Tomasz Napierala } 2413009ea47eSEdward Tomasz Napierala error = select(nfds + 1, &fdset, NULL, NULL, NULL); 2414009ea47eSEdward Tomasz Napierala if (error <= 0) { 2415009ea47eSEdward Tomasz Napierala if (errno == EINTR) 2416009ea47eSEdward Tomasz Napierala return; 2417009ea47eSEdward Tomasz Napierala log_err(1, "select"); 2418009ea47eSEdward Tomasz Napierala } 2419009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2420009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2421009ea47eSEdward Tomasz Napierala if (!FD_ISSET(portal->p_socket, &fdset)) 2422009ea47eSEdward Tomasz Napierala continue; 24239a95cfffSEdward Tomasz Napierala client_salen = sizeof(client_sa); 24249a95cfffSEdward Tomasz Napierala client_fd = accept(portal->p_socket, 24259a95cfffSEdward Tomasz Napierala (struct sockaddr *)&client_sa, 24269a95cfffSEdward Tomasz Napierala &client_salen); 242783dd77c3SAlexander Motin if (client_fd < 0) { 242883dd77c3SAlexander Motin if (errno == ECONNABORTED) 242983dd77c3SAlexander Motin continue; 2430009ea47eSEdward Tomasz Napierala log_err(1, "accept"); 243183dd77c3SAlexander Motin } 24320488e848SEdward Tomasz Napierala assert(client_salen >= client_sa.ss_len); 24330488e848SEdward Tomasz Napierala 24349a95cfffSEdward Tomasz Napierala handle_connection(portal, client_fd, 24358eab95d6SEdward Tomasz Napierala (struct sockaddr *)&client_sa, 2436073edb1cSAlexander Motin dont_fork); 2437009ea47eSEdward Tomasz Napierala break; 2438009ea47eSEdward Tomasz Napierala } 2439009ea47eSEdward Tomasz Napierala } 2440ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2441ba3a2d31SEdward Tomasz Napierala } 2442ba3a2d31SEdward Tomasz Napierala #endif 2443009ea47eSEdward Tomasz Napierala } 2444009ea47eSEdward Tomasz Napierala } 2445009ea47eSEdward Tomasz Napierala 2446009ea47eSEdward Tomasz Napierala static void 2447009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused) 2448009ea47eSEdward Tomasz Napierala { 2449009ea47eSEdward Tomasz Napierala 2450009ea47eSEdward Tomasz Napierala sighup_received = true; 2451009ea47eSEdward Tomasz Napierala } 2452009ea47eSEdward Tomasz Napierala 2453009ea47eSEdward Tomasz Napierala static void 2454009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused) 2455009ea47eSEdward Tomasz Napierala { 2456009ea47eSEdward Tomasz Napierala 2457009ea47eSEdward Tomasz Napierala sigterm_received = true; 2458009ea47eSEdward Tomasz Napierala } 2459009ea47eSEdward Tomasz Napierala 2460009ea47eSEdward Tomasz Napierala static void 246170b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused) 246270b939ecSEdward Tomasz Napierala { 246370b939ecSEdward Tomasz Napierala 246470b939ecSEdward Tomasz Napierala /* 246570b939ecSEdward Tomasz Napierala * The only purpose of this handler is to make SIGCHLD 246670b939ecSEdward Tomasz Napierala * interrupt the ISCSIDWAIT ioctl(2), so we can call 246770b939ecSEdward Tomasz Napierala * wait_for_children(). 246870b939ecSEdward Tomasz Napierala */ 246970b939ecSEdward Tomasz Napierala } 247070b939ecSEdward Tomasz Napierala 247170b939ecSEdward Tomasz Napierala static void 2472009ea47eSEdward Tomasz Napierala register_signals(void) 2473009ea47eSEdward Tomasz Napierala { 2474009ea47eSEdward Tomasz Napierala struct sigaction sa; 2475009ea47eSEdward Tomasz Napierala int error; 2476009ea47eSEdward Tomasz Napierala 2477009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 2478009ea47eSEdward Tomasz Napierala sa.sa_handler = sighup_handler; 2479009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 2480009ea47eSEdward Tomasz Napierala error = sigaction(SIGHUP, &sa, NULL); 2481009ea47eSEdward Tomasz Napierala if (error != 0) 2482009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2483009ea47eSEdward Tomasz Napierala 2484009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 2485009ea47eSEdward Tomasz Napierala error = sigaction(SIGTERM, &sa, NULL); 2486009ea47eSEdward Tomasz Napierala if (error != 0) 2487009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2488009ea47eSEdward Tomasz Napierala 2489009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 2490009ea47eSEdward Tomasz Napierala error = sigaction(SIGINT, &sa, NULL); 2491009ea47eSEdward Tomasz Napierala if (error != 0) 2492009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 249370b939ecSEdward Tomasz Napierala 249470b939ecSEdward Tomasz Napierala sa.sa_handler = sigchld_handler; 249570b939ecSEdward Tomasz Napierala error = sigaction(SIGCHLD, &sa, NULL); 249670b939ecSEdward Tomasz Napierala if (error != 0) 249770b939ecSEdward Tomasz Napierala log_err(1, "sigaction"); 2498009ea47eSEdward Tomasz Napierala } 2499009ea47eSEdward Tomasz Napierala 25005c734b04SJakub Wojciech Klama static void 25015c734b04SJakub Wojciech Klama check_perms(const char *path) 25025c734b04SJakub Wojciech Klama { 25035c734b04SJakub Wojciech Klama struct stat sb; 25045c734b04SJakub Wojciech Klama int error; 25055c734b04SJakub Wojciech Klama 25065c734b04SJakub Wojciech Klama error = stat(path, &sb); 25075c734b04SJakub Wojciech Klama if (error != 0) { 25085c734b04SJakub Wojciech Klama log_warn("stat"); 25095c734b04SJakub Wojciech Klama return; 25105c734b04SJakub Wojciech Klama } 25115c734b04SJakub Wojciech Klama if (sb.st_mode & S_IWOTH) { 25125c734b04SJakub Wojciech Klama log_warnx("%s is world-writable", path); 25135c734b04SJakub Wojciech Klama } else if (sb.st_mode & S_IROTH) { 25145c734b04SJakub Wojciech Klama log_warnx("%s is world-readable", path); 25155c734b04SJakub Wojciech Klama } else if (sb.st_mode & S_IXOTH) { 25165c734b04SJakub Wojciech Klama /* 25175c734b04SJakub Wojciech Klama * Ok, this one doesn't matter, but still do it, 25185c734b04SJakub Wojciech Klama * just for consistency. 25195c734b04SJakub Wojciech Klama */ 25205c734b04SJakub Wojciech Klama log_warnx("%s is world-executable", path); 25215c734b04SJakub Wojciech Klama } 25225c734b04SJakub Wojciech Klama 25235c734b04SJakub Wojciech Klama /* 25245c734b04SJakub Wojciech Klama * XXX: Should we also check for owner != 0? 25255c734b04SJakub Wojciech Klama */ 25265c734b04SJakub Wojciech Klama } 25275c734b04SJakub Wojciech Klama 25285c734b04SJakub Wojciech Klama static struct conf * 25295c734b04SJakub Wojciech Klama conf_new_from_file(const char *path, struct conf *oldconf, bool ucl) 25305c734b04SJakub Wojciech Klama { 25315c734b04SJakub Wojciech Klama struct conf *conf; 25325c734b04SJakub Wojciech Klama struct auth_group *ag; 25335c734b04SJakub Wojciech Klama struct portal_group *pg; 25345c734b04SJakub Wojciech Klama struct pport *pp; 25355c734b04SJakub Wojciech Klama int error; 25365c734b04SJakub Wojciech Klama 25375c734b04SJakub Wojciech Klama log_debugx("obtaining configuration from %s", path); 25385c734b04SJakub Wojciech Klama 25395c734b04SJakub Wojciech Klama conf = conf_new(); 25405c734b04SJakub Wojciech Klama 25415c734b04SJakub Wojciech Klama TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next) 25425c734b04SJakub Wojciech Klama pport_copy(pp, conf); 25435c734b04SJakub Wojciech Klama 25445c734b04SJakub Wojciech Klama ag = auth_group_new(conf, "default"); 25455c734b04SJakub Wojciech Klama assert(ag != NULL); 25465c734b04SJakub Wojciech Klama 25475c734b04SJakub Wojciech Klama ag = auth_group_new(conf, "no-authentication"); 25485c734b04SJakub Wojciech Klama assert(ag != NULL); 25495c734b04SJakub Wojciech Klama ag->ag_type = AG_TYPE_NO_AUTHENTICATION; 25505c734b04SJakub Wojciech Klama 25515c734b04SJakub Wojciech Klama ag = auth_group_new(conf, "no-access"); 25525c734b04SJakub Wojciech Klama assert(ag != NULL); 25535c734b04SJakub Wojciech Klama ag->ag_type = AG_TYPE_DENY; 25545c734b04SJakub Wojciech Klama 25555c734b04SJakub Wojciech Klama pg = portal_group_new(conf, "default"); 25565c734b04SJakub Wojciech Klama assert(pg != NULL); 25575c734b04SJakub Wojciech Klama 25585c734b04SJakub Wojciech Klama if (ucl) 25595c734b04SJakub Wojciech Klama error = uclparse_conf(conf, path); 25605c734b04SJakub Wojciech Klama else 25615c734b04SJakub Wojciech Klama error = parse_conf(conf, path); 25625c734b04SJakub Wojciech Klama 25635c734b04SJakub Wojciech Klama if (error != 0) { 25645c734b04SJakub Wojciech Klama conf_delete(conf); 25655c734b04SJakub Wojciech Klama return (NULL); 25665c734b04SJakub Wojciech Klama } 25675c734b04SJakub Wojciech Klama 25685c734b04SJakub Wojciech Klama check_perms(path); 25695c734b04SJakub Wojciech Klama 25705c734b04SJakub Wojciech Klama if (conf->conf_default_ag_defined == false) { 25715c734b04SJakub Wojciech Klama log_debugx("auth-group \"default\" not defined; " 25725c734b04SJakub Wojciech Klama "going with defaults"); 25735c734b04SJakub Wojciech Klama ag = auth_group_find(conf, "default"); 25745c734b04SJakub Wojciech Klama assert(ag != NULL); 25755c734b04SJakub Wojciech Klama ag->ag_type = AG_TYPE_DENY; 25765c734b04SJakub Wojciech Klama } 25775c734b04SJakub Wojciech Klama 25785c734b04SJakub Wojciech Klama if (conf->conf_default_pg_defined == false) { 25795c734b04SJakub Wojciech Klama log_debugx("portal-group \"default\" not defined; " 25805c734b04SJakub Wojciech Klama "going with defaults"); 25815c734b04SJakub Wojciech Klama pg = portal_group_find(conf, "default"); 25825c734b04SJakub Wojciech Klama assert(pg != NULL); 25835c734b04SJakub Wojciech Klama portal_group_add_listen(pg, "0.0.0.0:3260", false); 25845c734b04SJakub Wojciech Klama portal_group_add_listen(pg, "[::]:3260", false); 25855c734b04SJakub Wojciech Klama } 25865c734b04SJakub Wojciech Klama 25875c734b04SJakub Wojciech Klama conf->conf_kernel_port_on = true; 25885c734b04SJakub Wojciech Klama 25895c734b04SJakub Wojciech Klama error = conf_verify(conf); 25905c734b04SJakub Wojciech Klama if (error != 0) { 25915c734b04SJakub Wojciech Klama conf_delete(conf); 25925c734b04SJakub Wojciech Klama return (NULL); 25935c734b04SJakub Wojciech Klama } 25945c734b04SJakub Wojciech Klama 25955c734b04SJakub Wojciech Klama return (conf); 25965c734b04SJakub Wojciech Klama } 25975c734b04SJakub Wojciech Klama 2598009ea47eSEdward Tomasz Napierala int 2599009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 2600009ea47eSEdward Tomasz Napierala { 2601009ea47eSEdward Tomasz Napierala struct conf *oldconf, *newconf, *tmpconf; 2602829603e2SAlexander Motin struct isns *newns; 2603009ea47eSEdward Tomasz Napierala const char *config_path = DEFAULT_CONFIG_PATH; 2604009ea47eSEdward Tomasz Napierala int debug = 0, ch, error; 2605009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 26065c734b04SJakub Wojciech Klama bool use_ucl = false; 2607009ea47eSEdward Tomasz Napierala 26085c734b04SJakub Wojciech Klama while ((ch = getopt(argc, argv, "duf:R")) != -1) { 2609009ea47eSEdward Tomasz Napierala switch (ch) { 2610009ea47eSEdward Tomasz Napierala case 'd': 2611009ea47eSEdward Tomasz Napierala dont_daemonize = true; 2612009ea47eSEdward Tomasz Napierala debug++; 2613009ea47eSEdward Tomasz Napierala break; 26145c734b04SJakub Wojciech Klama case 'u': 26155c734b04SJakub Wojciech Klama use_ucl = true; 26165c734b04SJakub Wojciech Klama break; 2617009ea47eSEdward Tomasz Napierala case 'f': 2618009ea47eSEdward Tomasz Napierala config_path = optarg; 2619009ea47eSEdward Tomasz Napierala break; 2620ba3a2d31SEdward Tomasz Napierala case 'R': 2621ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 2622ba3a2d31SEdward Tomasz Napierala log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY " 2623ba3a2d31SEdward Tomasz Napierala "does not support iSER protocol"); 2624ba3a2d31SEdward Tomasz Napierala #endif 2625ba3a2d31SEdward Tomasz Napierala proxy_mode = true; 2626ba3a2d31SEdward Tomasz Napierala break; 2627009ea47eSEdward Tomasz Napierala case '?': 2628009ea47eSEdward Tomasz Napierala default: 2629009ea47eSEdward Tomasz Napierala usage(); 2630009ea47eSEdward Tomasz Napierala } 2631009ea47eSEdward Tomasz Napierala } 2632009ea47eSEdward Tomasz Napierala argc -= optind; 2633009ea47eSEdward Tomasz Napierala if (argc != 0) 2634009ea47eSEdward Tomasz Napierala usage(); 2635009ea47eSEdward Tomasz Napierala 2636009ea47eSEdward Tomasz Napierala log_init(debug); 2637009ea47eSEdward Tomasz Napierala kernel_init(); 2638009ea47eSEdward Tomasz Napierala 2639009ea47eSEdward Tomasz Napierala oldconf = conf_new_from_kernel(); 26405c734b04SJakub Wojciech Klama newconf = conf_new_from_file(config_path, oldconf, use_ucl); 26415c734b04SJakub Wojciech Klama 2642009ea47eSEdward Tomasz Napierala if (newconf == NULL) 2643fe845cdbSEdward Tomasz Napierala log_errx(1, "configuration error; exiting"); 2644009ea47eSEdward Tomasz Napierala if (debug > 0) { 2645009ea47eSEdward Tomasz Napierala oldconf->conf_debug = debug; 2646009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 2647009ea47eSEdward Tomasz Napierala } 2648009ea47eSEdward Tomasz Napierala 2649009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2650009ea47eSEdward Tomasz Napierala if (error != 0) 2651fe845cdbSEdward Tomasz Napierala log_errx(1, "failed to apply configuration; exiting"); 2652fe845cdbSEdward Tomasz Napierala 2653009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 2654009ea47eSEdward Tomasz Napierala oldconf = NULL; 2655009ea47eSEdward Tomasz Napierala 2656009ea47eSEdward Tomasz Napierala register_signals(); 2657009ea47eSEdward Tomasz Napierala 26585d5a95f1SEdward Tomasz Napierala if (dont_daemonize == false) { 26595d5a95f1SEdward Tomasz Napierala log_debugx("daemonizing"); 26605d5a95f1SEdward Tomasz Napierala if (daemon(0, 0) == -1) { 26615d5a95f1SEdward Tomasz Napierala log_warn("cannot daemonize"); 26625d5a95f1SEdward Tomasz Napierala pidfile_remove(newconf->conf_pidfh); 26635d5a95f1SEdward Tomasz Napierala exit(1); 26645d5a95f1SEdward Tomasz Napierala } 26655d5a95f1SEdward Tomasz Napierala } 26665d5a95f1SEdward Tomasz Napierala 2667829603e2SAlexander Motin /* Schedule iSNS update */ 2668829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) 2669829603e2SAlexander Motin set_timeout((newconf->conf_isns_period + 2) / 3, false); 2670829603e2SAlexander Motin 2671009ea47eSEdward Tomasz Napierala for (;;) { 2672009ea47eSEdward Tomasz Napierala main_loop(newconf, dont_daemonize); 2673009ea47eSEdward Tomasz Napierala if (sighup_received) { 2674009ea47eSEdward Tomasz Napierala sighup_received = false; 2675009ea47eSEdward Tomasz Napierala log_debugx("received SIGHUP, reloading configuration"); 26765c734b04SJakub Wojciech Klama tmpconf = conf_new_from_file(config_path, newconf, 26775c734b04SJakub Wojciech Klama use_ucl); 26785c734b04SJakub Wojciech Klama 2679009ea47eSEdward Tomasz Napierala if (tmpconf == NULL) { 2680009ea47eSEdward Tomasz Napierala log_warnx("configuration error, " 2681009ea47eSEdward Tomasz Napierala "continuing with old configuration"); 2682009ea47eSEdward Tomasz Napierala } else { 2683009ea47eSEdward Tomasz Napierala if (debug > 0) 2684009ea47eSEdward Tomasz Napierala tmpconf->conf_debug = debug; 2685009ea47eSEdward Tomasz Napierala oldconf = newconf; 2686009ea47eSEdward Tomasz Napierala newconf = tmpconf; 2687009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2688009ea47eSEdward Tomasz Napierala if (error != 0) 2689009ea47eSEdward Tomasz Napierala log_warnx("failed to reload " 2690009ea47eSEdward Tomasz Napierala "configuration"); 2691009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 2692009ea47eSEdward Tomasz Napierala oldconf = NULL; 2693009ea47eSEdward Tomasz Napierala } 2694009ea47eSEdward Tomasz Napierala } else if (sigterm_received) { 2695009ea47eSEdward Tomasz Napierala log_debugx("exiting on signal; " 2696009ea47eSEdward Tomasz Napierala "reloading empty configuration"); 2697009ea47eSEdward Tomasz Napierala 26986b31e130SAlexander Motin log_debugx("removing CTL iSCSI ports " 2699009ea47eSEdward Tomasz Napierala "and terminating all connections"); 2700009ea47eSEdward Tomasz Napierala 2701009ea47eSEdward Tomasz Napierala oldconf = newconf; 2702009ea47eSEdward Tomasz Napierala newconf = conf_new(); 2703009ea47eSEdward Tomasz Napierala if (debug > 0) 2704009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 2705009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2706009ea47eSEdward Tomasz Napierala if (error != 0) 2707009ea47eSEdward Tomasz Napierala log_warnx("failed to apply configuration"); 2708829603e2SAlexander Motin conf_delete(oldconf); 2709829603e2SAlexander Motin oldconf = NULL; 2710009ea47eSEdward Tomasz Napierala 2711009ea47eSEdward Tomasz Napierala log_warnx("exiting on signal"); 2712009ea47eSEdward Tomasz Napierala exit(0); 2713009ea47eSEdward Tomasz Napierala } else { 2714009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 2715009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2716829603e2SAlexander Motin if (timed_out()) { 2717829603e2SAlexander Motin set_timeout(0, false); 2718829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) 2719829603e2SAlexander Motin isns_check(newns); 2720829603e2SAlexander Motin /* Schedule iSNS update */ 2721829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) { 2722829603e2SAlexander Motin set_timeout((newconf->conf_isns_period 2723829603e2SAlexander Motin + 2) / 3, 2724829603e2SAlexander Motin false); 2725829603e2SAlexander Motin } 2726829603e2SAlexander Motin } 2727009ea47eSEdward Tomasz Napierala } 2728009ea47eSEdward Tomasz Napierala } 2729009ea47eSEdward Tomasz Napierala /* NOTREACHED */ 2730009ea47eSEdward Tomasz Napierala } 2731