1009ea47eSEdward Tomasz Napierala /*- 2*1de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*1de7b4b8SPedro F. Giffuni * 4009ea47eSEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 5009ea47eSEdward Tomasz Napierala * All rights reserved. 6009ea47eSEdward Tomasz Napierala * 7009ea47eSEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship 8009ea47eSEdward Tomasz Napierala * from the FreeBSD Foundation. 9009ea47eSEdward Tomasz Napierala * 10009ea47eSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without 11009ea47eSEdward Tomasz Napierala * modification, are permitted provided that the following conditions 12009ea47eSEdward Tomasz Napierala * are met: 13009ea47eSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright 14009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer. 15009ea47eSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright 16009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the 17009ea47eSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution. 18009ea47eSEdward Tomasz Napierala * 19009ea47eSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20009ea47eSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21009ea47eSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22009ea47eSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23009ea47eSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24009ea47eSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25009ea47eSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26009ea47eSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27009ea47eSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28009ea47eSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29009ea47eSEdward Tomasz Napierala * SUCH DAMAGE. 30009ea47eSEdward Tomasz Napierala * 31009ea47eSEdward Tomasz Napierala */ 32009ea47eSEdward Tomasz Napierala 33b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h> 34b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$"); 35b7a65e39SEdward Tomasz Napierala 36009ea47eSEdward Tomasz Napierala #include <sys/types.h> 37009ea47eSEdward Tomasz Napierala #include <sys/time.h> 38009ea47eSEdward Tomasz Napierala #include <sys/socket.h> 395c734b04SJakub Wojciech Klama #include <sys/stat.h> 40009ea47eSEdward Tomasz Napierala #include <sys/wait.h> 41009ea47eSEdward Tomasz Napierala #include <netinet/in.h> 42073edb1cSAlexander Motin #include <arpa/inet.h> 43009ea47eSEdward Tomasz Napierala #include <assert.h> 44009ea47eSEdward Tomasz Napierala #include <ctype.h> 45009ea47eSEdward Tomasz Napierala #include <errno.h> 46009ea47eSEdward Tomasz Napierala #include <netdb.h> 47009ea47eSEdward Tomasz Napierala #include <signal.h> 48009ea47eSEdward Tomasz Napierala #include <stdbool.h> 49009ea47eSEdward Tomasz Napierala #include <stdio.h> 50009ea47eSEdward Tomasz Napierala #include <stdint.h> 51009ea47eSEdward Tomasz Napierala #include <stdlib.h> 52009ea47eSEdward Tomasz Napierala #include <string.h> 53009ea47eSEdward Tomasz Napierala #include <unistd.h> 54009ea47eSEdward Tomasz Napierala 55009ea47eSEdward Tomasz Napierala #include "ctld.h" 56829603e2SAlexander Motin #include "isns.h" 57009ea47eSEdward Tomasz Napierala 58ba3a2d31SEdward Tomasz Napierala bool proxy_mode = false; 59ba3a2d31SEdward Tomasz Napierala 60009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false; 61009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false; 62009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false; 63009ea47eSEdward Tomasz Napierala 64009ea47eSEdward Tomasz Napierala static int nchildren = 0; 65db7bf2baSAlexander Motin static uint16_t last_portal_group_tag = 0xff; 66009ea47eSEdward Tomasz Napierala 67009ea47eSEdward Tomasz Napierala static void 68009ea47eSEdward Tomasz Napierala usage(void) 69009ea47eSEdward Tomasz Napierala { 70009ea47eSEdward Tomasz Napierala 71e321146fSEdward Tomasz Napierala fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n"); 72009ea47eSEdward Tomasz Napierala exit(1); 73009ea47eSEdward Tomasz Napierala } 74009ea47eSEdward Tomasz Napierala 75009ea47eSEdward Tomasz Napierala char * 76009ea47eSEdward Tomasz Napierala checked_strdup(const char *s) 77009ea47eSEdward Tomasz Napierala { 78009ea47eSEdward Tomasz Napierala char *c; 79009ea47eSEdward Tomasz Napierala 80009ea47eSEdward Tomasz Napierala c = strdup(s); 81009ea47eSEdward Tomasz Napierala if (c == NULL) 82009ea47eSEdward Tomasz Napierala log_err(1, "strdup"); 83009ea47eSEdward Tomasz Napierala return (c); 84009ea47eSEdward Tomasz Napierala } 85009ea47eSEdward Tomasz Napierala 86009ea47eSEdward Tomasz Napierala struct conf * 87009ea47eSEdward Tomasz Napierala conf_new(void) 88009ea47eSEdward Tomasz Napierala { 89009ea47eSEdward Tomasz Napierala struct conf *conf; 90009ea47eSEdward Tomasz Napierala 91009ea47eSEdward Tomasz Napierala conf = calloc(1, sizeof(*conf)); 92009ea47eSEdward Tomasz Napierala if (conf == NULL) 93009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 94920c6cbaSAlexander Motin TAILQ_INIT(&conf->conf_luns); 95009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_targets); 96009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_auth_groups); 9792847ee1SAlexander Motin TAILQ_INIT(&conf->conf_ports); 98009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_portal_groups); 99057abcb0SAlexander Motin TAILQ_INIT(&conf->conf_pports); 100829603e2SAlexander Motin TAILQ_INIT(&conf->conf_isns); 101009ea47eSEdward Tomasz Napierala 102829603e2SAlexander Motin conf->conf_isns_period = 900; 103829603e2SAlexander Motin conf->conf_isns_timeout = 5; 104009ea47eSEdward Tomasz Napierala conf->conf_debug = 0; 105009ea47eSEdward Tomasz Napierala conf->conf_timeout = 60; 106009ea47eSEdward Tomasz Napierala conf->conf_maxproc = 30; 107009ea47eSEdward Tomasz Napierala 108009ea47eSEdward Tomasz Napierala return (conf); 109009ea47eSEdward Tomasz Napierala } 110009ea47eSEdward Tomasz Napierala 111009ea47eSEdward Tomasz Napierala void 112009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf) 113009ea47eSEdward Tomasz Napierala { 114920c6cbaSAlexander Motin struct lun *lun, *ltmp; 115009ea47eSEdward Tomasz Napierala struct target *targ, *tmp; 116009ea47eSEdward Tomasz Napierala struct auth_group *ag, *cagtmp; 117009ea47eSEdward Tomasz Napierala struct portal_group *pg, *cpgtmp; 118057abcb0SAlexander Motin struct pport *pp, *pptmp; 119829603e2SAlexander Motin struct isns *is, *istmp; 120009ea47eSEdward Tomasz Napierala 121009ea47eSEdward Tomasz Napierala assert(conf->conf_pidfh == NULL); 122009ea47eSEdward Tomasz Napierala 123920c6cbaSAlexander Motin TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp) 124920c6cbaSAlexander Motin lun_delete(lun); 125009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 126009ea47eSEdward Tomasz Napierala target_delete(targ); 127009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 128009ea47eSEdward Tomasz Napierala auth_group_delete(ag); 129009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 130009ea47eSEdward Tomasz Napierala portal_group_delete(pg); 131057abcb0SAlexander Motin TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp) 132057abcb0SAlexander Motin pport_delete(pp); 133829603e2SAlexander Motin TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp) 134829603e2SAlexander Motin isns_delete(is); 13592847ee1SAlexander Motin assert(TAILQ_EMPTY(&conf->conf_ports)); 136009ea47eSEdward Tomasz Napierala free(conf->conf_pidfile_path); 137009ea47eSEdward Tomasz Napierala free(conf); 138009ea47eSEdward Tomasz Napierala } 139009ea47eSEdward Tomasz Napierala 140009ea47eSEdward Tomasz Napierala static struct auth * 141009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag) 142009ea47eSEdward Tomasz Napierala { 143009ea47eSEdward Tomasz Napierala struct auth *auth; 144009ea47eSEdward Tomasz Napierala 145009ea47eSEdward Tomasz Napierala auth = calloc(1, sizeof(*auth)); 146009ea47eSEdward Tomasz Napierala if (auth == NULL) 147009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 148009ea47eSEdward Tomasz Napierala auth->a_auth_group = ag; 149009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); 150009ea47eSEdward Tomasz Napierala return (auth); 151009ea47eSEdward Tomasz Napierala } 152009ea47eSEdward Tomasz Napierala 153009ea47eSEdward Tomasz Napierala static void 154009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth) 155009ea47eSEdward Tomasz Napierala { 156009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); 157009ea47eSEdward Tomasz Napierala 158009ea47eSEdward Tomasz Napierala free(auth->a_user); 159009ea47eSEdward Tomasz Napierala free(auth->a_secret); 160009ea47eSEdward Tomasz Napierala free(auth->a_mutual_user); 161009ea47eSEdward Tomasz Napierala free(auth->a_mutual_secret); 162009ea47eSEdward Tomasz Napierala free(auth); 163009ea47eSEdward Tomasz Napierala } 164009ea47eSEdward Tomasz Napierala 165009ea47eSEdward Tomasz Napierala const struct auth * 166d6093026SEdward Tomasz Napierala auth_find(const struct auth_group *ag, const char *user) 167009ea47eSEdward Tomasz Napierala { 168009ea47eSEdward Tomasz Napierala const struct auth *auth; 169009ea47eSEdward Tomasz Napierala 170009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { 171009ea47eSEdward Tomasz Napierala if (strcmp(auth->a_user, user) == 0) 172009ea47eSEdward Tomasz Napierala return (auth); 173009ea47eSEdward Tomasz Napierala } 174009ea47eSEdward Tomasz Napierala 175009ea47eSEdward Tomasz Napierala return (NULL); 176009ea47eSEdward Tomasz Napierala } 177009ea47eSEdward Tomasz Napierala 1781658628dSEdward Tomasz Napierala static void 1791658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth) 1801658628dSEdward Tomasz Napierala { 1811658628dSEdward Tomasz Napierala size_t len; 1821658628dSEdward Tomasz Napierala 1831658628dSEdward Tomasz Napierala len = strlen(auth->a_secret); 1841658628dSEdward Tomasz Napierala if (len > 16) { 1851658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1861658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 1871658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 1881658628dSEdward Tomasz Napierala "long", auth->a_user, auth->a_auth_group->ag_name); 1891658628dSEdward Tomasz Napierala else 1901658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 1911658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 1921658628dSEdward Tomasz Napierala "long", auth->a_user, 193f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 1941658628dSEdward Tomasz Napierala } 1951658628dSEdward Tomasz Napierala if (len < 12) { 1961658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 1971658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 1981658628dSEdward Tomasz Napierala "is too short; it should be at least 12 characters " 1991658628dSEdward Tomasz Napierala "long", auth->a_user, 2001658628dSEdward Tomasz Napierala auth->a_auth_group->ag_name); 2011658628dSEdward Tomasz Napierala else 2021658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 20342f97facSAlexander Motin "is too short; it should be at least 12 characters " 2041658628dSEdward Tomasz Napierala "long", auth->a_user, 205f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2061658628dSEdward Tomasz Napierala } 2071658628dSEdward Tomasz Napierala 2081658628dSEdward Tomasz Napierala if (auth->a_mutual_secret != NULL) { 2097c924aaeSJosh Paetzel len = strlen(auth->a_mutual_secret); 2101658628dSEdward Tomasz Napierala if (len > 16) { 2111658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 2121658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2131658628dSEdward Tomasz Napierala "auth-group \"%s\", is too long; it should " 2141658628dSEdward Tomasz Napierala "be at most 16 characters long", 2151658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 2161658628dSEdward Tomasz Napierala else 2171658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2181658628dSEdward Tomasz Napierala "target \"%s\", is too long; it should " 2191658628dSEdward Tomasz Napierala "be at most 16 characters long", 2201658628dSEdward Tomasz Napierala auth->a_user, 221f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2221658628dSEdward Tomasz Napierala } 2231658628dSEdward Tomasz Napierala if (len < 12) { 2241658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 2251658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2261658628dSEdward Tomasz Napierala "auth-group \"%s\", is too short; it " 2271658628dSEdward Tomasz Napierala "should be at least 12 characters long", 2281658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 2291658628dSEdward Tomasz Napierala else 2301658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 2311658628dSEdward Tomasz Napierala "target \"%s\", is too short; it should be " 2322668ec07SAlexander Motin "at least 12 characters long", 2331658628dSEdward Tomasz Napierala auth->a_user, 234f7ae5bf8SEdward Tomasz Napierala auth->a_auth_group->ag_target->t_name); 2351658628dSEdward Tomasz Napierala } 2361658628dSEdward Tomasz Napierala } 2371658628dSEdward Tomasz Napierala } 2381658628dSEdward Tomasz Napierala 2391658628dSEdward Tomasz Napierala const struct auth * 2401658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user, 2411658628dSEdward Tomasz Napierala const char *secret) 2421658628dSEdward Tomasz Napierala { 2431658628dSEdward Tomasz Napierala struct auth *auth; 2441658628dSEdward Tomasz Napierala 2451658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 2461658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP; 2471658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP) { 2481658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 2491658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 2501658628dSEdward Tomasz Napierala "other types for auth-group \"%s\"", ag->ag_name); 2511658628dSEdward Tomasz Napierala else 2521658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 2531658628dSEdward Tomasz Napierala "other types for target \"%s\"", 254f7ae5bf8SEdward Tomasz Napierala ag->ag_target->t_name); 2551658628dSEdward Tomasz Napierala return (NULL); 2561658628dSEdward Tomasz Napierala } 2571658628dSEdward Tomasz Napierala 2581658628dSEdward Tomasz Napierala auth = auth_new(ag); 2591658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 2601658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 2611658628dSEdward Tomasz Napierala 2621658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 2631658628dSEdward Tomasz Napierala 2641658628dSEdward Tomasz Napierala return (auth); 2651658628dSEdward Tomasz Napierala } 2661658628dSEdward Tomasz Napierala 2671658628dSEdward Tomasz Napierala const struct auth * 2681658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user, 2691658628dSEdward Tomasz Napierala const char *secret, const char *user2, const char *secret2) 2701658628dSEdward Tomasz Napierala { 2711658628dSEdward Tomasz Napierala struct auth *auth; 2721658628dSEdward Tomasz Napierala 2731658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 2741658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP_MUTUAL; 2751658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 2761658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 2771658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 2781658628dSEdward Tomasz Napierala "with other types for auth-group \"%s\"", 2791658628dSEdward Tomasz Napierala ag->ag_name); 2801658628dSEdward Tomasz Napierala else 2811658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 2821658628dSEdward Tomasz Napierala "with other types for target \"%s\"", 283f7ae5bf8SEdward Tomasz Napierala ag->ag_target->t_name); 2841658628dSEdward Tomasz Napierala return (NULL); 2851658628dSEdward Tomasz Napierala } 2861658628dSEdward Tomasz Napierala 2871658628dSEdward Tomasz Napierala auth = auth_new(ag); 2881658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 2891658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 2901658628dSEdward Tomasz Napierala auth->a_mutual_user = checked_strdup(user2); 2911658628dSEdward Tomasz Napierala auth->a_mutual_secret = checked_strdup(secret2); 2921658628dSEdward Tomasz Napierala 2931658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 2941658628dSEdward Tomasz Napierala 2951658628dSEdward Tomasz Napierala return (auth); 2961658628dSEdward Tomasz Napierala } 2971658628dSEdward Tomasz Napierala 2988cb2e958SEdward Tomasz Napierala const struct auth_name * 2998cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name) 3008cb2e958SEdward Tomasz Napierala { 3018cb2e958SEdward Tomasz Napierala struct auth_name *an; 3028cb2e958SEdward Tomasz Napierala 3038cb2e958SEdward Tomasz Napierala an = calloc(1, sizeof(*an)); 3048cb2e958SEdward Tomasz Napierala if (an == NULL) 3058cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 3068cb2e958SEdward Tomasz Napierala an->an_auth_group = ag; 3078cb2e958SEdward Tomasz Napierala an->an_initator_name = checked_strdup(name); 3088cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next); 3098cb2e958SEdward Tomasz Napierala return (an); 3108cb2e958SEdward Tomasz Napierala } 3118cb2e958SEdward Tomasz Napierala 3128cb2e958SEdward Tomasz Napierala static void 3138cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an) 3148cb2e958SEdward Tomasz Napierala { 3158cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next); 3168cb2e958SEdward Tomasz Napierala 3178cb2e958SEdward Tomasz Napierala free(an->an_initator_name); 3188cb2e958SEdward Tomasz Napierala free(an); 3198cb2e958SEdward Tomasz Napierala } 3208cb2e958SEdward Tomasz Napierala 3218cb2e958SEdward Tomasz Napierala bool 3228cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag) 3238cb2e958SEdward Tomasz Napierala { 3248cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_names)) 3258cb2e958SEdward Tomasz Napierala return (false); 3268cb2e958SEdward Tomasz Napierala return (true); 3278cb2e958SEdward Tomasz Napierala } 3288cb2e958SEdward Tomasz Napierala 3298cb2e958SEdward Tomasz Napierala const struct auth_name * 3308cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name) 3318cb2e958SEdward Tomasz Napierala { 3328cb2e958SEdward Tomasz Napierala const struct auth_name *auth_name; 3338cb2e958SEdward Tomasz Napierala 3348cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) { 3358cb2e958SEdward Tomasz Napierala if (strcmp(auth_name->an_initator_name, name) == 0) 3368cb2e958SEdward Tomasz Napierala return (auth_name); 3378cb2e958SEdward Tomasz Napierala } 3388cb2e958SEdward Tomasz Napierala 3398cb2e958SEdward Tomasz Napierala return (NULL); 3408cb2e958SEdward Tomasz Napierala } 3418cb2e958SEdward Tomasz Napierala 342e867e162SEdward Tomasz Napierala int 343e867e162SEdward Tomasz Napierala auth_name_check(const struct auth_group *ag, const char *initiator_name) 344e867e162SEdward Tomasz Napierala { 345e867e162SEdward Tomasz Napierala if (!auth_name_defined(ag)) 346e867e162SEdward Tomasz Napierala return (0); 347e867e162SEdward Tomasz Napierala 348e867e162SEdward Tomasz Napierala if (auth_name_find(ag, initiator_name) == NULL) 349e867e162SEdward Tomasz Napierala return (1); 350e867e162SEdward Tomasz Napierala 351e867e162SEdward Tomasz Napierala return (0); 352e867e162SEdward Tomasz Napierala } 353e867e162SEdward Tomasz Napierala 3548cb2e958SEdward Tomasz Napierala const struct auth_portal * 3558cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal) 3568cb2e958SEdward Tomasz Napierala { 3578cb2e958SEdward Tomasz Napierala struct auth_portal *ap; 358073edb1cSAlexander Motin char *net, *mask, *str, *tmp; 359073edb1cSAlexander Motin int len, dm, m; 3608cb2e958SEdward Tomasz Napierala 3618cb2e958SEdward Tomasz Napierala ap = calloc(1, sizeof(*ap)); 3628cb2e958SEdward Tomasz Napierala if (ap == NULL) 3638cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 3648cb2e958SEdward Tomasz Napierala ap->ap_auth_group = ag; 3658cb2e958SEdward Tomasz Napierala ap->ap_initator_portal = checked_strdup(portal); 366073edb1cSAlexander Motin mask = str = checked_strdup(portal); 367073edb1cSAlexander Motin net = strsep(&mask, "/"); 368073edb1cSAlexander Motin if (net[0] == '[') 369073edb1cSAlexander Motin net++; 370073edb1cSAlexander Motin len = strlen(net); 371073edb1cSAlexander Motin if (len == 0) 372073edb1cSAlexander Motin goto error; 373073edb1cSAlexander Motin if (net[len - 1] == ']') 374073edb1cSAlexander Motin net[len - 1] = 0; 375073edb1cSAlexander Motin if (strchr(net, ':') != NULL) { 376073edb1cSAlexander Motin struct sockaddr_in6 *sin6 = 377073edb1cSAlexander Motin (struct sockaddr_in6 *)&ap->ap_sa; 378073edb1cSAlexander Motin 379073edb1cSAlexander Motin sin6->sin6_len = sizeof(*sin6); 380073edb1cSAlexander Motin sin6->sin6_family = AF_INET6; 381073edb1cSAlexander Motin if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0) 382073edb1cSAlexander Motin goto error; 383073edb1cSAlexander Motin dm = 128; 384073edb1cSAlexander Motin } else { 385073edb1cSAlexander Motin struct sockaddr_in *sin = 386073edb1cSAlexander Motin (struct sockaddr_in *)&ap->ap_sa; 387073edb1cSAlexander Motin 388073edb1cSAlexander Motin sin->sin_len = sizeof(*sin); 389073edb1cSAlexander Motin sin->sin_family = AF_INET; 390073edb1cSAlexander Motin if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0) 391073edb1cSAlexander Motin goto error; 392073edb1cSAlexander Motin dm = 32; 393073edb1cSAlexander Motin } 394073edb1cSAlexander Motin if (mask != NULL) { 395073edb1cSAlexander Motin m = strtol(mask, &tmp, 0); 396073edb1cSAlexander Motin if (m < 0 || m > dm || tmp[0] != 0) 397073edb1cSAlexander Motin goto error; 398073edb1cSAlexander Motin } else 399073edb1cSAlexander Motin m = dm; 400073edb1cSAlexander Motin ap->ap_mask = m; 401073edb1cSAlexander Motin free(str); 4028cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next); 4038cb2e958SEdward Tomasz Napierala return (ap); 404073edb1cSAlexander Motin 405073edb1cSAlexander Motin error: 4069a913ec2SXin LI free(str); 407678b6879SAlexander Motin free(ap); 408f9a20518SEdward Tomasz Napierala log_warnx("incorrect initiator portal \"%s\"", portal); 409073edb1cSAlexander Motin return (NULL); 4108cb2e958SEdward Tomasz Napierala } 4118cb2e958SEdward Tomasz Napierala 4128cb2e958SEdward Tomasz Napierala static void 4138cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap) 4148cb2e958SEdward Tomasz Napierala { 4158cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next); 4168cb2e958SEdward Tomasz Napierala 4178cb2e958SEdward Tomasz Napierala free(ap->ap_initator_portal); 4188cb2e958SEdward Tomasz Napierala free(ap); 4198cb2e958SEdward Tomasz Napierala } 4208cb2e958SEdward Tomasz Napierala 4218cb2e958SEdward Tomasz Napierala bool 4228cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag) 4238cb2e958SEdward Tomasz Napierala { 4248cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_portals)) 4258cb2e958SEdward Tomasz Napierala return (false); 4268cb2e958SEdward Tomasz Napierala return (true); 4278cb2e958SEdward Tomasz Napierala } 4288cb2e958SEdward Tomasz Napierala 4298cb2e958SEdward Tomasz Napierala const struct auth_portal * 430073edb1cSAlexander Motin auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss) 4318cb2e958SEdward Tomasz Napierala { 432073edb1cSAlexander Motin const struct auth_portal *ap; 4335e46a066SAlexander Motin const uint8_t *a, *b; 434073edb1cSAlexander Motin int i; 4355e46a066SAlexander Motin uint8_t bmask; 4368cb2e958SEdward Tomasz Napierala 437073edb1cSAlexander Motin TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) { 438073edb1cSAlexander Motin if (ap->ap_sa.ss_family != ss->ss_family) 439073edb1cSAlexander Motin continue; 440073edb1cSAlexander Motin if (ss->ss_family == AF_INET) { 4415e46a066SAlexander Motin a = (const uint8_t *) 4425e46a066SAlexander Motin &((const struct sockaddr_in *)ss)->sin_addr; 4435e46a066SAlexander Motin b = (const uint8_t *) 4445e46a066SAlexander Motin &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr; 445073edb1cSAlexander Motin } else { 4465e46a066SAlexander Motin a = (const uint8_t *) 4475e46a066SAlexander Motin &((const struct sockaddr_in6 *)ss)->sin6_addr; 4485e46a066SAlexander Motin b = (const uint8_t *) 4495e46a066SAlexander Motin &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr; 450073edb1cSAlexander Motin } 451073edb1cSAlexander Motin for (i = 0; i < ap->ap_mask / 8; i++) { 452073edb1cSAlexander Motin if (a[i] != b[i]) 453073edb1cSAlexander Motin goto next; 454073edb1cSAlexander Motin } 455073edb1cSAlexander Motin if (ap->ap_mask % 8) { 456073edb1cSAlexander Motin bmask = 0xff << (8 - (ap->ap_mask % 8)); 457073edb1cSAlexander Motin if ((a[i] & bmask) != (b[i] & bmask)) 458073edb1cSAlexander Motin goto next; 459073edb1cSAlexander Motin } 460073edb1cSAlexander Motin return (ap); 461073edb1cSAlexander Motin next: 462073edb1cSAlexander Motin ; 4638cb2e958SEdward Tomasz Napierala } 4648cb2e958SEdward Tomasz Napierala 4658cb2e958SEdward Tomasz Napierala return (NULL); 4668cb2e958SEdward Tomasz Napierala } 4678cb2e958SEdward Tomasz Napierala 468e867e162SEdward Tomasz Napierala int 469e867e162SEdward Tomasz Napierala auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa) 470e867e162SEdward Tomasz Napierala { 471e867e162SEdward Tomasz Napierala 472e867e162SEdward Tomasz Napierala if (!auth_portal_defined(ag)) 473e867e162SEdward Tomasz Napierala return (0); 474e867e162SEdward Tomasz Napierala 475e867e162SEdward Tomasz Napierala if (auth_portal_find(ag, sa) == NULL) 476e867e162SEdward Tomasz Napierala return (1); 477e867e162SEdward Tomasz Napierala 478e867e162SEdward Tomasz Napierala return (0); 479e867e162SEdward Tomasz Napierala } 480e867e162SEdward Tomasz Napierala 481009ea47eSEdward Tomasz Napierala struct auth_group * 482009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name) 483009ea47eSEdward Tomasz Napierala { 484009ea47eSEdward Tomasz Napierala struct auth_group *ag; 485009ea47eSEdward Tomasz Napierala 486009ea47eSEdward Tomasz Napierala if (name != NULL) { 487009ea47eSEdward Tomasz Napierala ag = auth_group_find(conf, name); 488009ea47eSEdward Tomasz Napierala if (ag != NULL) { 489009ea47eSEdward Tomasz Napierala log_warnx("duplicated auth-group \"%s\"", name); 490009ea47eSEdward Tomasz Napierala return (NULL); 491009ea47eSEdward Tomasz Napierala } 492009ea47eSEdward Tomasz Napierala } 493009ea47eSEdward Tomasz Napierala 494009ea47eSEdward Tomasz Napierala ag = calloc(1, sizeof(*ag)); 495009ea47eSEdward Tomasz Napierala if (ag == NULL) 496009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 497009ea47eSEdward Tomasz Napierala if (name != NULL) 498009ea47eSEdward Tomasz Napierala ag->ag_name = checked_strdup(name); 499009ea47eSEdward Tomasz Napierala TAILQ_INIT(&ag->ag_auths); 5008cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_names); 5018cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_portals); 502009ea47eSEdward Tomasz Napierala ag->ag_conf = conf; 503009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next); 504009ea47eSEdward Tomasz Napierala 505009ea47eSEdward Tomasz Napierala return (ag); 506009ea47eSEdward Tomasz Napierala } 507009ea47eSEdward Tomasz Napierala 508009ea47eSEdward Tomasz Napierala void 509009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag) 510009ea47eSEdward Tomasz Napierala { 5118cb2e958SEdward Tomasz Napierala struct auth *auth, *auth_tmp; 5128cb2e958SEdward Tomasz Napierala struct auth_name *auth_name, *auth_name_tmp; 5138cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal, *auth_portal_tmp; 514009ea47eSEdward Tomasz Napierala 515009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); 516009ea47eSEdward Tomasz Napierala 5178cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) 518009ea47eSEdward Tomasz Napierala auth_delete(auth); 5198cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) 5208cb2e958SEdward Tomasz Napierala auth_name_delete(auth_name); 5218cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, 5228cb2e958SEdward Tomasz Napierala auth_portal_tmp) 5238cb2e958SEdward Tomasz Napierala auth_portal_delete(auth_portal); 524009ea47eSEdward Tomasz Napierala free(ag->ag_name); 525009ea47eSEdward Tomasz Napierala free(ag); 526009ea47eSEdward Tomasz Napierala } 527009ea47eSEdward Tomasz Napierala 528009ea47eSEdward Tomasz Napierala struct auth_group * 529d6093026SEdward Tomasz Napierala auth_group_find(const struct conf *conf, const char *name) 530009ea47eSEdward Tomasz Napierala { 531009ea47eSEdward Tomasz Napierala struct auth_group *ag; 532009ea47eSEdward Tomasz Napierala 533009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 534009ea47eSEdward Tomasz Napierala if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 535009ea47eSEdward Tomasz Napierala return (ag); 536009ea47eSEdward Tomasz Napierala } 537009ea47eSEdward Tomasz Napierala 538009ea47eSEdward Tomasz Napierala return (NULL); 539009ea47eSEdward Tomasz Napierala } 540009ea47eSEdward Tomasz Napierala 541df9900fbSEdward Tomasz Napierala int 542bd833b11SEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, const char *str) 543df9900fbSEdward Tomasz Napierala { 544bd833b11SEdward Tomasz Napierala int type; 545df9900fbSEdward Tomasz Napierala 546df9900fbSEdward Tomasz Napierala if (strcmp(str, "none") == 0) { 547df9900fbSEdward Tomasz Napierala type = AG_TYPE_NO_AUTHENTICATION; 548e76ce448SEdward Tomasz Napierala } else if (strcmp(str, "deny") == 0) { 549e76ce448SEdward Tomasz Napierala type = AG_TYPE_DENY; 550df9900fbSEdward Tomasz Napierala } else if (strcmp(str, "chap") == 0) { 551df9900fbSEdward Tomasz Napierala type = AG_TYPE_CHAP; 552df9900fbSEdward Tomasz Napierala } else if (strcmp(str, "chap-mutual") == 0) { 553df9900fbSEdward Tomasz Napierala type = AG_TYPE_CHAP_MUTUAL; 554df9900fbSEdward Tomasz Napierala } else { 555df9900fbSEdward Tomasz Napierala if (ag->ag_name != NULL) 556df9900fbSEdward Tomasz Napierala log_warnx("invalid auth-type \"%s\" for auth-group " 557df9900fbSEdward Tomasz Napierala "\"%s\"", str, ag->ag_name); 558df9900fbSEdward Tomasz Napierala else 559df9900fbSEdward Tomasz Napierala log_warnx("invalid auth-type \"%s\" for target " 560df9900fbSEdward Tomasz Napierala "\"%s\"", str, ag->ag_target->t_name); 561df9900fbSEdward Tomasz Napierala return (1); 562df9900fbSEdward Tomasz Napierala } 563df9900fbSEdward Tomasz Napierala 564bd833b11SEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) { 565bd833b11SEdward Tomasz Napierala if (ag->ag_name != NULL) { 566df9900fbSEdward Tomasz Napierala log_warnx("cannot set auth-type to \"%s\" for " 567df9900fbSEdward Tomasz Napierala "auth-group \"%s\"; already has a different " 568df9900fbSEdward Tomasz Napierala "type", str, ag->ag_name); 569bd833b11SEdward Tomasz Napierala } else { 570df9900fbSEdward Tomasz Napierala log_warnx("cannot set auth-type to \"%s\" for target " 571df9900fbSEdward Tomasz Napierala "\"%s\"; already has a different type", 572df9900fbSEdward Tomasz Napierala str, ag->ag_target->t_name); 573bd833b11SEdward Tomasz Napierala } 574df9900fbSEdward Tomasz Napierala return (1); 575df9900fbSEdward Tomasz Napierala } 576df9900fbSEdward Tomasz Napierala 577bd833b11SEdward Tomasz Napierala ag->ag_type = type; 578bd833b11SEdward Tomasz Napierala 579bd833b11SEdward Tomasz Napierala return (0); 580df9900fbSEdward Tomasz Napierala } 581df9900fbSEdward Tomasz Napierala 582009ea47eSEdward Tomasz Napierala static struct portal * 583009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg) 584009ea47eSEdward Tomasz Napierala { 585009ea47eSEdward Tomasz Napierala struct portal *portal; 586009ea47eSEdward Tomasz Napierala 587009ea47eSEdward Tomasz Napierala portal = calloc(1, sizeof(*portal)); 588009ea47eSEdward Tomasz Napierala if (portal == NULL) 589009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 590009ea47eSEdward Tomasz Napierala TAILQ_INIT(&portal->p_targets); 591009ea47eSEdward Tomasz Napierala portal->p_portal_group = pg; 592009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 593009ea47eSEdward Tomasz Napierala return (portal); 594009ea47eSEdward Tomasz Napierala } 595009ea47eSEdward Tomasz Napierala 596009ea47eSEdward Tomasz Napierala static void 597009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal) 598009ea47eSEdward Tomasz Napierala { 59944a5953aSEdward Tomasz Napierala 600009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 60144a5953aSEdward Tomasz Napierala if (portal->p_ai != NULL) 602009ea47eSEdward Tomasz Napierala freeaddrinfo(portal->p_ai); 603009ea47eSEdward Tomasz Napierala free(portal->p_listen); 604009ea47eSEdward Tomasz Napierala free(portal); 605009ea47eSEdward Tomasz Napierala } 606009ea47eSEdward Tomasz Napierala 607009ea47eSEdward Tomasz Napierala struct portal_group * 608009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name) 609009ea47eSEdward Tomasz Napierala { 610009ea47eSEdward Tomasz Napierala struct portal_group *pg; 611009ea47eSEdward Tomasz Napierala 612009ea47eSEdward Tomasz Napierala pg = portal_group_find(conf, name); 613009ea47eSEdward Tomasz Napierala if (pg != NULL) { 614009ea47eSEdward Tomasz Napierala log_warnx("duplicated portal-group \"%s\"", name); 615009ea47eSEdward Tomasz Napierala return (NULL); 616009ea47eSEdward Tomasz Napierala } 617009ea47eSEdward Tomasz Napierala 618009ea47eSEdward Tomasz Napierala pg = calloc(1, sizeof(*pg)); 619009ea47eSEdward Tomasz Napierala if (pg == NULL) 620009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 621009ea47eSEdward Tomasz Napierala pg->pg_name = checked_strdup(name); 622398290f2SAlexander Motin TAILQ_INIT(&pg->pg_options); 623009ea47eSEdward Tomasz Napierala TAILQ_INIT(&pg->pg_portals); 62492847ee1SAlexander Motin TAILQ_INIT(&pg->pg_ports); 625009ea47eSEdward Tomasz Napierala pg->pg_conf = conf; 6266b31e130SAlexander Motin pg->pg_tag = 0; /* Assigned later in conf_apply(). */ 627009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 628009ea47eSEdward Tomasz Napierala 629009ea47eSEdward Tomasz Napierala return (pg); 630009ea47eSEdward Tomasz Napierala } 631009ea47eSEdward Tomasz Napierala 632009ea47eSEdward Tomasz Napierala void 633009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg) 634009ea47eSEdward Tomasz Napierala { 635009ea47eSEdward Tomasz Napierala struct portal *portal, *tmp; 63692847ee1SAlexander Motin struct port *port, *tport; 637398290f2SAlexander Motin struct option *o, *otmp; 638009ea47eSEdward Tomasz Napierala 63992847ee1SAlexander Motin TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport) 64092847ee1SAlexander Motin port_delete(port); 641009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 642009ea47eSEdward Tomasz Napierala 643009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 644009ea47eSEdward Tomasz Napierala portal_delete(portal); 645398290f2SAlexander Motin TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp) 646398290f2SAlexander Motin option_delete(&pg->pg_options, o); 647009ea47eSEdward Tomasz Napierala free(pg->pg_name); 648a9d78210SEdward Tomasz Napierala free(pg->pg_offload); 6496dbdbf82SEdward Tomasz Napierala free(pg->pg_redirection); 650009ea47eSEdward Tomasz Napierala free(pg); 651009ea47eSEdward Tomasz Napierala } 652009ea47eSEdward Tomasz Napierala 653009ea47eSEdward Tomasz Napierala struct portal_group * 654d6093026SEdward Tomasz Napierala portal_group_find(const struct conf *conf, const char *name) 655009ea47eSEdward Tomasz Napierala { 656009ea47eSEdward Tomasz Napierala struct portal_group *pg; 657009ea47eSEdward Tomasz Napierala 658009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 659009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, name) == 0) 660009ea47eSEdward Tomasz Napierala return (pg); 661009ea47eSEdward Tomasz Napierala } 662009ea47eSEdward Tomasz Napierala 663009ea47eSEdward Tomasz Napierala return (NULL); 664009ea47eSEdward Tomasz Napierala } 665009ea47eSEdward Tomasz Napierala 666829603e2SAlexander Motin static int 667829603e2SAlexander Motin parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai) 668009ea47eSEdward Tomasz Napierala { 669009ea47eSEdward Tomasz Napierala struct addrinfo hints; 6701606ad8cSAlexander Motin char *str, *addr, *ch; 671009ea47eSEdward Tomasz Napierala const char *port; 672009ea47eSEdward Tomasz Napierala int error, colons = 0; 673009ea47eSEdward Tomasz Napierala 6741606ad8cSAlexander Motin str = arg = strdup(arg); 675009ea47eSEdward Tomasz Napierala if (arg[0] == '[') { 676009ea47eSEdward Tomasz Napierala /* 677009ea47eSEdward Tomasz Napierala * IPv6 address in square brackets, perhaps with port. 678009ea47eSEdward Tomasz Napierala */ 679009ea47eSEdward Tomasz Napierala arg++; 680009ea47eSEdward Tomasz Napierala addr = strsep(&arg, "]"); 681203632f2SXin LI if (arg == NULL) { 682203632f2SXin LI free(str); 683009ea47eSEdward Tomasz Napierala return (1); 684203632f2SXin LI } 685009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 686829603e2SAlexander Motin port = def_port; 687009ea47eSEdward Tomasz Napierala } else if (arg[0] == ':') { 688009ea47eSEdward Tomasz Napierala port = arg + 1; 6891606ad8cSAlexander Motin } else { 6901606ad8cSAlexander Motin free(str); 691009ea47eSEdward Tomasz Napierala return (1); 6921606ad8cSAlexander Motin } 693009ea47eSEdward Tomasz Napierala } else { 694009ea47eSEdward Tomasz Napierala /* 695009ea47eSEdward Tomasz Napierala * Either IPv6 address without brackets - and without 696009ea47eSEdward Tomasz Napierala * a port - or IPv4 address. Just count the colons. 697009ea47eSEdward Tomasz Napierala */ 698009ea47eSEdward Tomasz Napierala for (ch = arg; *ch != '\0'; ch++) { 699009ea47eSEdward Tomasz Napierala if (*ch == ':') 700009ea47eSEdward Tomasz Napierala colons++; 701009ea47eSEdward Tomasz Napierala } 702009ea47eSEdward Tomasz Napierala if (colons > 1) { 703009ea47eSEdward Tomasz Napierala addr = arg; 704829603e2SAlexander Motin port = def_port; 705009ea47eSEdward Tomasz Napierala } else { 706009ea47eSEdward Tomasz Napierala addr = strsep(&arg, ":"); 707009ea47eSEdward Tomasz Napierala if (arg == NULL) 708829603e2SAlexander Motin port = def_port; 709009ea47eSEdward Tomasz Napierala else 710009ea47eSEdward Tomasz Napierala port = arg; 711009ea47eSEdward Tomasz Napierala } 712009ea47eSEdward Tomasz Napierala } 713009ea47eSEdward Tomasz Napierala 714009ea47eSEdward Tomasz Napierala memset(&hints, 0, sizeof(hints)); 715009ea47eSEdward Tomasz Napierala hints.ai_family = PF_UNSPEC; 716009ea47eSEdward Tomasz Napierala hints.ai_socktype = SOCK_STREAM; 717009ea47eSEdward Tomasz Napierala hints.ai_flags = AI_PASSIVE; 718829603e2SAlexander Motin error = getaddrinfo(addr, port, &hints, ai); 7191606ad8cSAlexander Motin free(str); 7201606ad8cSAlexander Motin return ((error != 0) ? 1 : 0); 721829603e2SAlexander Motin } 722009ea47eSEdward Tomasz Napierala 723829603e2SAlexander Motin int 724829603e2SAlexander Motin portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 725829603e2SAlexander Motin { 726829603e2SAlexander Motin struct portal *portal; 727829603e2SAlexander Motin 728829603e2SAlexander Motin portal = portal_new(pg); 729829603e2SAlexander Motin portal->p_listen = checked_strdup(value); 730829603e2SAlexander Motin portal->p_iser = iser; 731829603e2SAlexander Motin 732829603e2SAlexander Motin if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) { 733829603e2SAlexander Motin log_warnx("invalid listen address %s", portal->p_listen); 73444a5953aSEdward Tomasz Napierala portal_delete(portal); 735009ea47eSEdward Tomasz Napierala return (1); 736009ea47eSEdward Tomasz Napierala } 737009ea47eSEdward Tomasz Napierala 738009ea47eSEdward Tomasz Napierala /* 739009ea47eSEdward Tomasz Napierala * XXX: getaddrinfo(3) may return multiple addresses; we should turn 740009ea47eSEdward Tomasz Napierala * those into multiple portals. 741009ea47eSEdward Tomasz Napierala */ 742009ea47eSEdward Tomasz Napierala 743009ea47eSEdward Tomasz Napierala return (0); 744009ea47eSEdward Tomasz Napierala } 745009ea47eSEdward Tomasz Napierala 746829603e2SAlexander Motin int 747829603e2SAlexander Motin isns_new(struct conf *conf, const char *addr) 748829603e2SAlexander Motin { 749829603e2SAlexander Motin struct isns *isns; 750829603e2SAlexander Motin 751829603e2SAlexander Motin isns = calloc(1, sizeof(*isns)); 752829603e2SAlexander Motin if (isns == NULL) 753829603e2SAlexander Motin log_err(1, "calloc"); 754829603e2SAlexander Motin isns->i_conf = conf; 755829603e2SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next); 756829603e2SAlexander Motin isns->i_addr = checked_strdup(addr); 757829603e2SAlexander Motin 758829603e2SAlexander Motin if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) { 759829603e2SAlexander Motin log_warnx("invalid iSNS address %s", isns->i_addr); 760829603e2SAlexander Motin isns_delete(isns); 761829603e2SAlexander Motin return (1); 762829603e2SAlexander Motin } 763829603e2SAlexander Motin 764829603e2SAlexander Motin /* 765829603e2SAlexander Motin * XXX: getaddrinfo(3) may return multiple addresses; we should turn 766829603e2SAlexander Motin * those into multiple servers. 767829603e2SAlexander Motin */ 768829603e2SAlexander Motin 769829603e2SAlexander Motin return (0); 770829603e2SAlexander Motin } 771829603e2SAlexander Motin 772829603e2SAlexander Motin void 773829603e2SAlexander Motin isns_delete(struct isns *isns) 774829603e2SAlexander Motin { 775829603e2SAlexander Motin 776829603e2SAlexander Motin TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next); 777829603e2SAlexander Motin free(isns->i_addr); 778829603e2SAlexander Motin if (isns->i_ai != NULL) 779829603e2SAlexander Motin freeaddrinfo(isns->i_ai); 780829603e2SAlexander Motin free(isns); 781829603e2SAlexander Motin } 782829603e2SAlexander Motin 783829603e2SAlexander Motin static int 784829603e2SAlexander Motin isns_do_connect(struct isns *isns) 785829603e2SAlexander Motin { 786829603e2SAlexander Motin int s; 787829603e2SAlexander Motin 788829603e2SAlexander Motin s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype, 789829603e2SAlexander Motin isns->i_ai->ai_protocol); 790829603e2SAlexander Motin if (s < 0) { 791829603e2SAlexander Motin log_warn("socket(2) failed for %s", isns->i_addr); 792829603e2SAlexander Motin return (-1); 793829603e2SAlexander Motin } 794829603e2SAlexander Motin if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) { 795829603e2SAlexander Motin log_warn("connect(2) failed for %s", isns->i_addr); 796829603e2SAlexander Motin close(s); 797829603e2SAlexander Motin return (-1); 798829603e2SAlexander Motin } 799829603e2SAlexander Motin return(s); 800829603e2SAlexander Motin } 801829603e2SAlexander Motin 802829603e2SAlexander Motin static int 803829603e2SAlexander Motin isns_do_register(struct isns *isns, int s, const char *hostname) 804829603e2SAlexander Motin { 805829603e2SAlexander Motin struct conf *conf = isns->i_conf; 806829603e2SAlexander Motin struct target *target; 807829603e2SAlexander Motin struct portal *portal; 808829603e2SAlexander Motin struct portal_group *pg; 80992847ee1SAlexander Motin struct port *port; 810829603e2SAlexander Motin struct isns_req *req; 811829603e2SAlexander Motin int res = 0; 812829603e2SAlexander Motin uint32_t error; 813829603e2SAlexander Motin 814829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT); 815829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 816829603e2SAlexander Motin isns_req_add_delim(req); 817829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 818829603e2SAlexander Motin isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */ 819829603e2SAlexander Motin isns_req_add_32(req, 6, conf->conf_isns_period); 820829603e2SAlexander Motin TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 821829603e2SAlexander Motin if (pg->pg_unassigned) 822829603e2SAlexander Motin continue; 823829603e2SAlexander Motin TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 824829603e2SAlexander Motin isns_req_add_addr(req, 16, portal->p_ai); 825829603e2SAlexander Motin isns_req_add_port(req, 17, portal->p_ai); 826829603e2SAlexander Motin } 827829603e2SAlexander Motin } 828829603e2SAlexander Motin TAILQ_FOREACH(target, &conf->conf_targets, t_next) { 829829603e2SAlexander Motin isns_req_add_str(req, 32, target->t_name); 830829603e2SAlexander Motin isns_req_add_32(req, 33, 1); /* 1 -- Target*/ 831829603e2SAlexander Motin if (target->t_alias != NULL) 832829603e2SAlexander Motin isns_req_add_str(req, 34, target->t_alias); 83392847ee1SAlexander Motin TAILQ_FOREACH(port, &target->t_ports, p_ts) { 83492847ee1SAlexander Motin if ((pg = port->p_portal_group) == NULL) 83592847ee1SAlexander Motin continue; 836829603e2SAlexander Motin isns_req_add_32(req, 51, pg->pg_tag); 837829603e2SAlexander Motin TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 838829603e2SAlexander Motin isns_req_add_addr(req, 49, portal->p_ai); 839829603e2SAlexander Motin isns_req_add_port(req, 50, portal->p_ai); 840829603e2SAlexander Motin } 841829603e2SAlexander Motin } 84292847ee1SAlexander Motin } 843829603e2SAlexander Motin res = isns_req_send(s, req); 844829603e2SAlexander Motin if (res < 0) { 845829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 846829603e2SAlexander Motin goto quit; 847829603e2SAlexander Motin } 848829603e2SAlexander Motin res = isns_req_receive(s, req); 849829603e2SAlexander Motin if (res < 0) { 850829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 851829603e2SAlexander Motin goto quit; 852829603e2SAlexander Motin } 853829603e2SAlexander Motin error = isns_req_get_status(req); 854829603e2SAlexander Motin if (error != 0) { 855829603e2SAlexander Motin log_warnx("iSNS register error %d for %s", error, isns->i_addr); 856829603e2SAlexander Motin res = -1; 857829603e2SAlexander Motin } 858829603e2SAlexander Motin quit: 859829603e2SAlexander Motin isns_req_free(req); 860829603e2SAlexander Motin return (res); 861829603e2SAlexander Motin } 862829603e2SAlexander Motin 863829603e2SAlexander Motin static int 864829603e2SAlexander Motin isns_do_check(struct isns *isns, int s, const char *hostname) 865829603e2SAlexander Motin { 866829603e2SAlexander Motin struct conf *conf = isns->i_conf; 867829603e2SAlexander Motin struct isns_req *req; 868829603e2SAlexander Motin int res = 0; 869829603e2SAlexander Motin uint32_t error; 870829603e2SAlexander Motin 871829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT); 872829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 873829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 874829603e2SAlexander Motin isns_req_add_delim(req); 875829603e2SAlexander Motin isns_req_add(req, 2, 0, NULL); 876829603e2SAlexander Motin res = isns_req_send(s, req); 877829603e2SAlexander Motin if (res < 0) { 878829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 879829603e2SAlexander Motin goto quit; 880829603e2SAlexander Motin } 881829603e2SAlexander Motin res = isns_req_receive(s, req); 882829603e2SAlexander Motin if (res < 0) { 883829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 884829603e2SAlexander Motin goto quit; 885829603e2SAlexander Motin } 886829603e2SAlexander Motin error = isns_req_get_status(req); 887829603e2SAlexander Motin if (error != 0) { 888829603e2SAlexander Motin log_warnx("iSNS check error %d for %s", error, isns->i_addr); 889829603e2SAlexander Motin res = -1; 890829603e2SAlexander Motin } 891829603e2SAlexander Motin quit: 892829603e2SAlexander Motin isns_req_free(req); 893829603e2SAlexander Motin return (res); 894829603e2SAlexander Motin } 895829603e2SAlexander Motin 896829603e2SAlexander Motin static int 897829603e2SAlexander Motin isns_do_deregister(struct isns *isns, int s, const char *hostname) 898829603e2SAlexander Motin { 899829603e2SAlexander Motin struct conf *conf = isns->i_conf; 900829603e2SAlexander Motin struct isns_req *req; 901829603e2SAlexander Motin int res = 0; 902829603e2SAlexander Motin uint32_t error; 903829603e2SAlexander Motin 904829603e2SAlexander Motin req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT); 905829603e2SAlexander Motin isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 906829603e2SAlexander Motin isns_req_add_delim(req); 907829603e2SAlexander Motin isns_req_add_str(req, 1, hostname); 908829603e2SAlexander Motin res = isns_req_send(s, req); 909829603e2SAlexander Motin if (res < 0) { 910829603e2SAlexander Motin log_warn("send(2) failed for %s", isns->i_addr); 911829603e2SAlexander Motin goto quit; 912829603e2SAlexander Motin } 913829603e2SAlexander Motin res = isns_req_receive(s, req); 914829603e2SAlexander Motin if (res < 0) { 915829603e2SAlexander Motin log_warn("receive(2) failed for %s", isns->i_addr); 916829603e2SAlexander Motin goto quit; 917829603e2SAlexander Motin } 918829603e2SAlexander Motin error = isns_req_get_status(req); 919829603e2SAlexander Motin if (error != 0) { 920829603e2SAlexander Motin log_warnx("iSNS deregister error %d for %s", error, isns->i_addr); 921829603e2SAlexander Motin res = -1; 922829603e2SAlexander Motin } 923829603e2SAlexander Motin quit: 924829603e2SAlexander Motin isns_req_free(req); 925829603e2SAlexander Motin return (res); 926829603e2SAlexander Motin } 927829603e2SAlexander Motin 928829603e2SAlexander Motin void 929829603e2SAlexander Motin isns_register(struct isns *isns, struct isns *oldisns) 930829603e2SAlexander Motin { 931829603e2SAlexander Motin struct conf *conf = isns->i_conf; 932d5cc4a4aSAlexander Motin int s; 933829603e2SAlexander Motin char hostname[256]; 934829603e2SAlexander Motin 935829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 936829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 937829603e2SAlexander Motin return; 938829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 939829603e2SAlexander Motin s = isns_do_connect(isns); 940829603e2SAlexander Motin if (s < 0) { 941829603e2SAlexander Motin set_timeout(0, false); 942829603e2SAlexander Motin return; 943829603e2SAlexander Motin } 944829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 945829603e2SAlexander Motin 946829603e2SAlexander Motin if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets)) 947829603e2SAlexander Motin oldisns = isns; 948d5cc4a4aSAlexander Motin isns_do_deregister(oldisns, s, hostname); 949d5cc4a4aSAlexander Motin isns_do_register(isns, s, hostname); 950829603e2SAlexander Motin close(s); 951829603e2SAlexander Motin set_timeout(0, false); 952829603e2SAlexander Motin } 953829603e2SAlexander Motin 954829603e2SAlexander Motin void 955829603e2SAlexander Motin isns_check(struct isns *isns) 956829603e2SAlexander Motin { 957829603e2SAlexander Motin struct conf *conf = isns->i_conf; 958829603e2SAlexander Motin int s, res; 959829603e2SAlexander Motin char hostname[256]; 960829603e2SAlexander Motin 961829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 962829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 963829603e2SAlexander Motin return; 964829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 965829603e2SAlexander Motin s = isns_do_connect(isns); 966829603e2SAlexander Motin if (s < 0) { 967829603e2SAlexander Motin set_timeout(0, false); 968829603e2SAlexander Motin return; 969829603e2SAlexander Motin } 970829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 971829603e2SAlexander Motin 972829603e2SAlexander Motin res = isns_do_check(isns, s, hostname); 973829603e2SAlexander Motin if (res < 0) { 974d5cc4a4aSAlexander Motin isns_do_deregister(isns, s, hostname); 975d5cc4a4aSAlexander Motin isns_do_register(isns, s, hostname); 976829603e2SAlexander Motin } 977829603e2SAlexander Motin close(s); 978829603e2SAlexander Motin set_timeout(0, false); 979829603e2SAlexander Motin } 980829603e2SAlexander Motin 981829603e2SAlexander Motin void 982829603e2SAlexander Motin isns_deregister(struct isns *isns) 983829603e2SAlexander Motin { 984829603e2SAlexander Motin struct conf *conf = isns->i_conf; 985d5cc4a4aSAlexander Motin int s; 986829603e2SAlexander Motin char hostname[256]; 987829603e2SAlexander Motin 988829603e2SAlexander Motin if (TAILQ_EMPTY(&conf->conf_targets) || 989829603e2SAlexander Motin TAILQ_EMPTY(&conf->conf_portal_groups)) 990829603e2SAlexander Motin return; 991829603e2SAlexander Motin set_timeout(conf->conf_isns_timeout, false); 992829603e2SAlexander Motin s = isns_do_connect(isns); 993829603e2SAlexander Motin if (s < 0) 994829603e2SAlexander Motin return; 995829603e2SAlexander Motin gethostname(hostname, sizeof(hostname)); 996829603e2SAlexander Motin 997d5cc4a4aSAlexander Motin isns_do_deregister(isns, s, hostname); 998829603e2SAlexander Motin close(s); 999829603e2SAlexander Motin set_timeout(0, false); 1000829603e2SAlexander Motin } 1001829603e2SAlexander Motin 100205374883SEdward Tomasz Napierala int 1003bd833b11SEdward Tomasz Napierala portal_group_set_filter(struct portal_group *pg, const char *str) 100405374883SEdward Tomasz Napierala { 1005bd833b11SEdward Tomasz Napierala int filter; 100605374883SEdward Tomasz Napierala 100705374883SEdward Tomasz Napierala if (strcmp(str, "none") == 0) { 100805374883SEdward Tomasz Napierala filter = PG_FILTER_NONE; 100905374883SEdward Tomasz Napierala } else if (strcmp(str, "portal") == 0) { 101005374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL; 101105374883SEdward Tomasz Napierala } else if (strcmp(str, "portal-name") == 0) { 101205374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL_NAME; 101305374883SEdward Tomasz Napierala } else if (strcmp(str, "portal-name-auth") == 0) { 101405374883SEdward Tomasz Napierala filter = PG_FILTER_PORTAL_NAME_AUTH; 101505374883SEdward Tomasz Napierala } else { 101605374883SEdward Tomasz Napierala log_warnx("invalid discovery-filter \"%s\" for portal-group " 101705374883SEdward Tomasz Napierala "\"%s\"; valid values are \"none\", \"portal\", " 101805374883SEdward Tomasz Napierala "\"portal-name\", and \"portal-name-auth\"", 101905374883SEdward Tomasz Napierala str, pg->pg_name); 102005374883SEdward Tomasz Napierala return (1); 102105374883SEdward Tomasz Napierala } 102205374883SEdward Tomasz Napierala 1023bd833b11SEdward Tomasz Napierala if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN && 1024bd833b11SEdward Tomasz Napierala pg->pg_discovery_filter != filter) { 102505374883SEdward Tomasz Napierala log_warnx("cannot set discovery-filter to \"%s\" for " 102605374883SEdward Tomasz Napierala "portal-group \"%s\"; already has a different " 102705374883SEdward Tomasz Napierala "value", str, pg->pg_name); 102805374883SEdward Tomasz Napierala return (1); 102905374883SEdward Tomasz Napierala } 103005374883SEdward Tomasz Napierala 1031bd833b11SEdward Tomasz Napierala pg->pg_discovery_filter = filter; 1032bd833b11SEdward Tomasz Napierala 1033bd833b11SEdward Tomasz Napierala return (0); 103405374883SEdward Tomasz Napierala } 103505374883SEdward Tomasz Napierala 10366dbdbf82SEdward Tomasz Napierala int 1037a9d78210SEdward Tomasz Napierala portal_group_set_offload(struct portal_group *pg, const char *offload) 1038a9d78210SEdward Tomasz Napierala { 1039a9d78210SEdward Tomasz Napierala 1040a9d78210SEdward Tomasz Napierala if (pg->pg_offload != NULL) { 1041a9d78210SEdward Tomasz Napierala log_warnx("cannot set offload to \"%s\" for " 1042a9d78210SEdward Tomasz Napierala "portal-group \"%s\"; already defined", 1043a9d78210SEdward Tomasz Napierala offload, pg->pg_name); 1044a9d78210SEdward Tomasz Napierala return (1); 1045a9d78210SEdward Tomasz Napierala } 1046a9d78210SEdward Tomasz Napierala 1047a9d78210SEdward Tomasz Napierala pg->pg_offload = checked_strdup(offload); 1048a9d78210SEdward Tomasz Napierala 1049a9d78210SEdward Tomasz Napierala return (0); 1050a9d78210SEdward Tomasz Napierala } 1051a9d78210SEdward Tomasz Napierala 1052a9d78210SEdward Tomasz Napierala int 10536dbdbf82SEdward Tomasz Napierala portal_group_set_redirection(struct portal_group *pg, const char *addr) 10546dbdbf82SEdward Tomasz Napierala { 10556dbdbf82SEdward Tomasz Napierala 10566dbdbf82SEdward Tomasz Napierala if (pg->pg_redirection != NULL) { 10576dbdbf82SEdward Tomasz Napierala log_warnx("cannot set redirection to \"%s\" for " 10586dbdbf82SEdward Tomasz Napierala "portal-group \"%s\"; already defined", 10596dbdbf82SEdward Tomasz Napierala addr, pg->pg_name); 10606dbdbf82SEdward Tomasz Napierala return (1); 10616dbdbf82SEdward Tomasz Napierala } 10626dbdbf82SEdward Tomasz Napierala 10636dbdbf82SEdward Tomasz Napierala pg->pg_redirection = checked_strdup(addr); 10646dbdbf82SEdward Tomasz Napierala 10656dbdbf82SEdward Tomasz Napierala return (0); 10666dbdbf82SEdward Tomasz Napierala } 10676dbdbf82SEdward Tomasz Napierala 1068009ea47eSEdward Tomasz Napierala static bool 1069009ea47eSEdward Tomasz Napierala valid_hex(const char ch) 1070009ea47eSEdward Tomasz Napierala { 1071009ea47eSEdward Tomasz Napierala switch (ch) { 1072009ea47eSEdward Tomasz Napierala case '0': 1073009ea47eSEdward Tomasz Napierala case '1': 1074009ea47eSEdward Tomasz Napierala case '2': 1075009ea47eSEdward Tomasz Napierala case '3': 1076009ea47eSEdward Tomasz Napierala case '4': 1077009ea47eSEdward Tomasz Napierala case '5': 1078009ea47eSEdward Tomasz Napierala case '6': 1079009ea47eSEdward Tomasz Napierala case '7': 1080009ea47eSEdward Tomasz Napierala case '8': 1081009ea47eSEdward Tomasz Napierala case '9': 1082009ea47eSEdward Tomasz Napierala case 'a': 1083009ea47eSEdward Tomasz Napierala case 'A': 1084009ea47eSEdward Tomasz Napierala case 'b': 1085009ea47eSEdward Tomasz Napierala case 'B': 1086009ea47eSEdward Tomasz Napierala case 'c': 1087009ea47eSEdward Tomasz Napierala case 'C': 1088009ea47eSEdward Tomasz Napierala case 'd': 1089009ea47eSEdward Tomasz Napierala case 'D': 1090009ea47eSEdward Tomasz Napierala case 'e': 1091009ea47eSEdward Tomasz Napierala case 'E': 1092009ea47eSEdward Tomasz Napierala case 'f': 1093009ea47eSEdward Tomasz Napierala case 'F': 1094009ea47eSEdward Tomasz Napierala return (true); 1095009ea47eSEdward Tomasz Napierala default: 1096009ea47eSEdward Tomasz Napierala return (false); 1097009ea47eSEdward Tomasz Napierala } 1098009ea47eSEdward Tomasz Napierala } 1099009ea47eSEdward Tomasz Napierala 1100009ea47eSEdward Tomasz Napierala bool 1101009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name) 1102009ea47eSEdward Tomasz Napierala { 1103009ea47eSEdward Tomasz Napierala int i; 1104009ea47eSEdward Tomasz Napierala 1105009ea47eSEdward Tomasz Napierala if (strlen(name) >= MAX_NAME_LEN) { 1106009ea47eSEdward Tomasz Napierala log_warnx("overlong name for target \"%s\"; max length allowed " 1107009ea47eSEdward Tomasz Napierala "by iSCSI specification is %d characters", 1108009ea47eSEdward Tomasz Napierala name, MAX_NAME_LEN); 1109009ea47eSEdward Tomasz Napierala return (false); 1110009ea47eSEdward Tomasz Napierala } 1111009ea47eSEdward Tomasz Napierala 1112009ea47eSEdward Tomasz Napierala /* 1113009ea47eSEdward Tomasz Napierala * In the cases below, we don't return an error, just in case the admin 1114009ea47eSEdward Tomasz Napierala * was right, and we're wrong. 1115009ea47eSEdward Tomasz Napierala */ 1116009ea47eSEdward Tomasz Napierala if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 1117009ea47eSEdward Tomasz Napierala for (i = strlen("iqn."); name[i] != '\0'; i++) { 1118009ea47eSEdward Tomasz Napierala /* 1119009ea47eSEdward Tomasz Napierala * XXX: We should verify UTF-8 normalisation, as defined 1120009ea47eSEdward Tomasz Napierala * by 3.2.6.2: iSCSI Name Encoding. 1121009ea47eSEdward Tomasz Napierala */ 1122009ea47eSEdward Tomasz Napierala if (isalnum(name[i])) 1123009ea47eSEdward Tomasz Napierala continue; 1124009ea47eSEdward Tomasz Napierala if (name[i] == '-' || name[i] == '.' || name[i] == ':') 1125009ea47eSEdward Tomasz Napierala continue; 1126009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target name " 1127009ea47eSEdward Tomasz Napierala "\"%s\"; allowed characters are letters, digits, " 1128009ea47eSEdward Tomasz Napierala "'-', '.', and ':'", name[i], name); 1129009ea47eSEdward Tomasz Napierala break; 1130009ea47eSEdward Tomasz Napierala } 1131009ea47eSEdward Tomasz Napierala /* 1132009ea47eSEdward Tomasz Napierala * XXX: Check more stuff: valid date and a valid reversed domain. 1133009ea47eSEdward Tomasz Napierala */ 1134009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 1135009ea47eSEdward Tomasz Napierala if (strlen(name) != strlen("eui.") + 16) 1136009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"eui.\" " 1137009ea47eSEdward Tomasz Napierala "should be followed by exactly 16 hexadecimal " 1138009ea47eSEdward Tomasz Napierala "digits", name); 1139009ea47eSEdward Tomasz Napierala for (i = strlen("eui."); name[i] != '\0'; i++) { 1140009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 1141009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 1142009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 1143009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 1144009ea47eSEdward Tomasz Napierala break; 1145009ea47eSEdward Tomasz Napierala } 1146009ea47eSEdward Tomasz Napierala } 1147009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 1148009ea47eSEdward Tomasz Napierala if (strlen(name) > strlen("naa.") + 32) 1149009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"naa.\" " 1150009ea47eSEdward Tomasz Napierala "should be followed by at most 32 hexadecimal " 1151009ea47eSEdward Tomasz Napierala "digits", name); 1152009ea47eSEdward Tomasz Napierala for (i = strlen("naa."); name[i] != '\0'; i++) { 1153009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 1154009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 1155009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 1156009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 1157009ea47eSEdward Tomasz Napierala break; 1158009ea47eSEdward Tomasz Napierala } 1159009ea47eSEdward Tomasz Napierala } 1160009ea47eSEdward Tomasz Napierala } else { 1161009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; should start with " 1162d7b87b89SJosh Paetzel "either \"iqn.\", \"eui.\", or \"naa.\"", 1163009ea47eSEdward Tomasz Napierala name); 1164009ea47eSEdward Tomasz Napierala } 1165009ea47eSEdward Tomasz Napierala return (true); 1166009ea47eSEdward Tomasz Napierala } 1167009ea47eSEdward Tomasz Napierala 1168057abcb0SAlexander Motin struct pport * 1169057abcb0SAlexander Motin pport_new(struct conf *conf, const char *name, uint32_t ctl_port) 1170057abcb0SAlexander Motin { 1171057abcb0SAlexander Motin struct pport *pp; 1172057abcb0SAlexander Motin 1173057abcb0SAlexander Motin pp = calloc(1, sizeof(*pp)); 1174057abcb0SAlexander Motin if (pp == NULL) 1175057abcb0SAlexander Motin log_err(1, "calloc"); 1176057abcb0SAlexander Motin pp->pp_conf = conf; 1177057abcb0SAlexander Motin pp->pp_name = checked_strdup(name); 1178057abcb0SAlexander Motin pp->pp_ctl_port = ctl_port; 1179057abcb0SAlexander Motin TAILQ_INIT(&pp->pp_ports); 1180057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next); 1181057abcb0SAlexander Motin return (pp); 1182057abcb0SAlexander Motin } 1183057abcb0SAlexander Motin 1184057abcb0SAlexander Motin struct pport * 1185057abcb0SAlexander Motin pport_find(const struct conf *conf, const char *name) 1186057abcb0SAlexander Motin { 1187057abcb0SAlexander Motin struct pport *pp; 1188057abcb0SAlexander Motin 1189057abcb0SAlexander Motin TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) { 1190057abcb0SAlexander Motin if (strcasecmp(pp->pp_name, name) == 0) 1191057abcb0SAlexander Motin return (pp); 1192057abcb0SAlexander Motin } 1193057abcb0SAlexander Motin return (NULL); 1194057abcb0SAlexander Motin } 1195057abcb0SAlexander Motin 1196057abcb0SAlexander Motin struct pport * 1197057abcb0SAlexander Motin pport_copy(struct pport *pp, struct conf *conf) 1198057abcb0SAlexander Motin { 1199057abcb0SAlexander Motin struct pport *ppnew; 1200057abcb0SAlexander Motin 1201057abcb0SAlexander Motin ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port); 1202057abcb0SAlexander Motin return (ppnew); 1203057abcb0SAlexander Motin } 1204057abcb0SAlexander Motin 1205057abcb0SAlexander Motin void 1206057abcb0SAlexander Motin pport_delete(struct pport *pp) 1207057abcb0SAlexander Motin { 1208057abcb0SAlexander Motin struct port *port, *tport; 1209057abcb0SAlexander Motin 1210057abcb0SAlexander Motin TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport) 1211057abcb0SAlexander Motin port_delete(port); 1212057abcb0SAlexander Motin TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next); 1213057abcb0SAlexander Motin free(pp->pp_name); 1214057abcb0SAlexander Motin free(pp); 1215057abcb0SAlexander Motin } 1216057abcb0SAlexander Motin 121792847ee1SAlexander Motin struct port * 121892847ee1SAlexander Motin port_new(struct conf *conf, struct target *target, struct portal_group *pg) 121992847ee1SAlexander Motin { 122092847ee1SAlexander Motin struct port *port; 1221057abcb0SAlexander Motin char *name; 122255e4579eSAlexander Motin int ret; 122392847ee1SAlexander Motin 122455e4579eSAlexander Motin ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name); 122555e4579eSAlexander Motin if (ret <= 0) 122655e4579eSAlexander Motin log_err(1, "asprintf"); 1227057abcb0SAlexander Motin if (port_find(conf, name) != NULL) { 1228057abcb0SAlexander Motin log_warnx("duplicate port \"%s\"", name); 1229057abcb0SAlexander Motin free(name); 1230057abcb0SAlexander Motin return (NULL); 1231057abcb0SAlexander Motin } 123292847ee1SAlexander Motin port = calloc(1, sizeof(*port)); 123392847ee1SAlexander Motin if (port == NULL) 123492847ee1SAlexander Motin log_err(1, "calloc"); 123592847ee1SAlexander Motin port->p_conf = conf; 1236057abcb0SAlexander Motin port->p_name = name; 123792847ee1SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 123892847ee1SAlexander Motin TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 123992847ee1SAlexander Motin port->p_target = target; 124092847ee1SAlexander Motin TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs); 124192847ee1SAlexander Motin port->p_portal_group = pg; 124292847ee1SAlexander Motin return (port); 124392847ee1SAlexander Motin } 124492847ee1SAlexander Motin 124592847ee1SAlexander Motin struct port * 1246057abcb0SAlexander Motin port_new_pp(struct conf *conf, struct target *target, struct pport *pp) 1247057abcb0SAlexander Motin { 1248057abcb0SAlexander Motin struct port *port; 1249057abcb0SAlexander Motin char *name; 125055e4579eSAlexander Motin int ret; 1251057abcb0SAlexander Motin 125255e4579eSAlexander Motin ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name); 125355e4579eSAlexander Motin if (ret <= 0) 125455e4579eSAlexander Motin log_err(1, "asprintf"); 1255057abcb0SAlexander Motin if (port_find(conf, name) != NULL) { 1256057abcb0SAlexander Motin log_warnx("duplicate port \"%s\"", name); 1257057abcb0SAlexander Motin free(name); 1258057abcb0SAlexander Motin return (NULL); 1259057abcb0SAlexander Motin } 1260057abcb0SAlexander Motin port = calloc(1, sizeof(*port)); 1261057abcb0SAlexander Motin if (port == NULL) 1262057abcb0SAlexander Motin log_err(1, "calloc"); 1263057abcb0SAlexander Motin port->p_conf = conf; 1264057abcb0SAlexander Motin port->p_name = name; 1265057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1266057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1267057abcb0SAlexander Motin port->p_target = target; 1268057abcb0SAlexander Motin TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps); 1269057abcb0SAlexander Motin port->p_pport = pp; 1270057abcb0SAlexander Motin return (port); 1271057abcb0SAlexander Motin } 1272057abcb0SAlexander Motin 1273057abcb0SAlexander Motin struct port * 127492847ee1SAlexander Motin port_find(const struct conf *conf, const char *name) 127592847ee1SAlexander Motin { 127692847ee1SAlexander Motin struct port *port; 127792847ee1SAlexander Motin 127892847ee1SAlexander Motin TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 127992847ee1SAlexander Motin if (strcasecmp(port->p_name, name) == 0) 128092847ee1SAlexander Motin return (port); 128192847ee1SAlexander Motin } 128292847ee1SAlexander Motin 128392847ee1SAlexander Motin return (NULL); 128492847ee1SAlexander Motin } 128592847ee1SAlexander Motin 128692847ee1SAlexander Motin struct port * 128792847ee1SAlexander Motin port_find_in_pg(const struct portal_group *pg, const char *target) 128892847ee1SAlexander Motin { 128992847ee1SAlexander Motin struct port *port; 129092847ee1SAlexander Motin 129192847ee1SAlexander Motin TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) { 129292847ee1SAlexander Motin if (strcasecmp(port->p_target->t_name, target) == 0) 129392847ee1SAlexander Motin return (port); 129492847ee1SAlexander Motin } 129592847ee1SAlexander Motin 129692847ee1SAlexander Motin return (NULL); 129792847ee1SAlexander Motin } 129892847ee1SAlexander Motin 129992847ee1SAlexander Motin void 130092847ee1SAlexander Motin port_delete(struct port *port) 130192847ee1SAlexander Motin { 130292847ee1SAlexander Motin 130392847ee1SAlexander Motin if (port->p_portal_group) 130492847ee1SAlexander Motin TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs); 1305057abcb0SAlexander Motin if (port->p_pport) 1306057abcb0SAlexander Motin TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps); 130792847ee1SAlexander Motin if (port->p_target) 130892847ee1SAlexander Motin TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts); 130992847ee1SAlexander Motin TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next); 131092847ee1SAlexander Motin free(port->p_name); 131192847ee1SAlexander Motin free(port); 131292847ee1SAlexander Motin } 131392847ee1SAlexander Motin 13149215e501SAlexander Motin int 13159215e501SAlexander Motin port_is_dummy(struct port *port) 13169215e501SAlexander Motin { 13179215e501SAlexander Motin 13189215e501SAlexander Motin if (port->p_portal_group) { 13199215e501SAlexander Motin if (port->p_portal_group->pg_foreign) 13209215e501SAlexander Motin return (1); 13219215e501SAlexander Motin if (TAILQ_EMPTY(&port->p_portal_group->pg_portals)) 13229215e501SAlexander Motin return (1); 13239215e501SAlexander Motin } 13249215e501SAlexander Motin return (0); 13259215e501SAlexander Motin } 13269215e501SAlexander Motin 1327009ea47eSEdward Tomasz Napierala struct target * 1328f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name) 1329009ea47eSEdward Tomasz Napierala { 1330009ea47eSEdward Tomasz Napierala struct target *targ; 1331009ea47eSEdward Tomasz Napierala int i, len; 1332009ea47eSEdward Tomasz Napierala 1333f7ae5bf8SEdward Tomasz Napierala targ = target_find(conf, name); 1334009ea47eSEdward Tomasz Napierala if (targ != NULL) { 1335f7ae5bf8SEdward Tomasz Napierala log_warnx("duplicated target \"%s\"", name); 1336009ea47eSEdward Tomasz Napierala return (NULL); 1337009ea47eSEdward Tomasz Napierala } 1338f7ae5bf8SEdward Tomasz Napierala if (valid_iscsi_name(name) == false) { 1339f7ae5bf8SEdward Tomasz Napierala log_warnx("target name \"%s\" is invalid", name); 1340009ea47eSEdward Tomasz Napierala return (NULL); 1341009ea47eSEdward Tomasz Napierala } 1342009ea47eSEdward Tomasz Napierala targ = calloc(1, sizeof(*targ)); 1343009ea47eSEdward Tomasz Napierala if (targ == NULL) 1344009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1345f7ae5bf8SEdward Tomasz Napierala targ->t_name = checked_strdup(name); 1346009ea47eSEdward Tomasz Napierala 1347009ea47eSEdward Tomasz Napierala /* 1348009ea47eSEdward Tomasz Napierala * RFC 3722 requires us to normalize the name to lowercase. 1349009ea47eSEdward Tomasz Napierala */ 1350f7ae5bf8SEdward Tomasz Napierala len = strlen(name); 1351009ea47eSEdward Tomasz Napierala for (i = 0; i < len; i++) 1352f7ae5bf8SEdward Tomasz Napierala targ->t_name[i] = tolower(targ->t_name[i]); 1353009ea47eSEdward Tomasz Napierala 1354009ea47eSEdward Tomasz Napierala targ->t_conf = conf; 135592847ee1SAlexander Motin TAILQ_INIT(&targ->t_ports); 1356009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 1357009ea47eSEdward Tomasz Napierala 1358009ea47eSEdward Tomasz Napierala return (targ); 1359009ea47eSEdward Tomasz Napierala } 1360009ea47eSEdward Tomasz Napierala 1361009ea47eSEdward Tomasz Napierala void 1362009ea47eSEdward Tomasz Napierala target_delete(struct target *targ) 1363009ea47eSEdward Tomasz Napierala { 136492847ee1SAlexander Motin struct port *port, *tport; 1365009ea47eSEdward Tomasz Napierala 136692847ee1SAlexander Motin TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport) 136792847ee1SAlexander Motin port_delete(port); 1368009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 1369009ea47eSEdward Tomasz Napierala 1370f7ae5bf8SEdward Tomasz Napierala free(targ->t_name); 13716dbdbf82SEdward Tomasz Napierala free(targ->t_redirection); 1372009ea47eSEdward Tomasz Napierala free(targ); 1373009ea47eSEdward Tomasz Napierala } 1374009ea47eSEdward Tomasz Napierala 1375009ea47eSEdward Tomasz Napierala struct target * 1376f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name) 1377009ea47eSEdward Tomasz Napierala { 1378009ea47eSEdward Tomasz Napierala struct target *targ; 1379009ea47eSEdward Tomasz Napierala 1380009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1381f7ae5bf8SEdward Tomasz Napierala if (strcasecmp(targ->t_name, name) == 0) 1382009ea47eSEdward Tomasz Napierala return (targ); 1383009ea47eSEdward Tomasz Napierala } 1384009ea47eSEdward Tomasz Napierala 1385009ea47eSEdward Tomasz Napierala return (NULL); 1386009ea47eSEdward Tomasz Napierala } 1387009ea47eSEdward Tomasz Napierala 13886dbdbf82SEdward Tomasz Napierala int 13896dbdbf82SEdward Tomasz Napierala target_set_redirection(struct target *target, const char *addr) 13906dbdbf82SEdward Tomasz Napierala { 13916dbdbf82SEdward Tomasz Napierala 13926dbdbf82SEdward Tomasz Napierala if (target->t_redirection != NULL) { 13936dbdbf82SEdward Tomasz Napierala log_warnx("cannot set redirection to \"%s\" for " 13946dbdbf82SEdward Tomasz Napierala "target \"%s\"; already defined", 13956dbdbf82SEdward Tomasz Napierala addr, target->t_name); 13966dbdbf82SEdward Tomasz Napierala return (1); 13976dbdbf82SEdward Tomasz Napierala } 13986dbdbf82SEdward Tomasz Napierala 13996dbdbf82SEdward Tomasz Napierala target->t_redirection = checked_strdup(addr); 14006dbdbf82SEdward Tomasz Napierala 14016dbdbf82SEdward Tomasz Napierala return (0); 14026dbdbf82SEdward Tomasz Napierala } 14036dbdbf82SEdward Tomasz Napierala 1404009ea47eSEdward Tomasz Napierala struct lun * 1405920c6cbaSAlexander Motin lun_new(struct conf *conf, const char *name) 1406009ea47eSEdward Tomasz Napierala { 1407009ea47eSEdward Tomasz Napierala struct lun *lun; 1408009ea47eSEdward Tomasz Napierala 1409920c6cbaSAlexander Motin lun = lun_find(conf, name); 1410009ea47eSEdward Tomasz Napierala if (lun != NULL) { 1411920c6cbaSAlexander Motin log_warnx("duplicated lun \"%s\"", name); 1412009ea47eSEdward Tomasz Napierala return (NULL); 1413009ea47eSEdward Tomasz Napierala } 1414009ea47eSEdward Tomasz Napierala 1415009ea47eSEdward Tomasz Napierala lun = calloc(1, sizeof(*lun)); 1416009ea47eSEdward Tomasz Napierala if (lun == NULL) 1417009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1418920c6cbaSAlexander Motin lun->l_conf = conf; 1419920c6cbaSAlexander Motin lun->l_name = checked_strdup(name); 1420009ea47eSEdward Tomasz Napierala TAILQ_INIT(&lun->l_options); 1421920c6cbaSAlexander Motin TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next); 14223933f7b1SAlexander Motin lun->l_ctl_lun = -1; 1423009ea47eSEdward Tomasz Napierala 1424009ea47eSEdward Tomasz Napierala return (lun); 1425009ea47eSEdward Tomasz Napierala } 1426009ea47eSEdward Tomasz Napierala 1427009ea47eSEdward Tomasz Napierala void 1428009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun) 1429009ea47eSEdward Tomasz Napierala { 1430920c6cbaSAlexander Motin struct target *targ; 1431398290f2SAlexander Motin struct option *o, *tmp; 1432920c6cbaSAlexander Motin int i; 1433009ea47eSEdward Tomasz Napierala 1434920c6cbaSAlexander Motin TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) { 1435920c6cbaSAlexander Motin for (i = 0; i < MAX_LUNS; i++) { 1436920c6cbaSAlexander Motin if (targ->t_luns[i] == lun) 1437920c6cbaSAlexander Motin targ->t_luns[i] = NULL; 1438920c6cbaSAlexander Motin } 1439920c6cbaSAlexander Motin } 1440920c6cbaSAlexander Motin TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next); 1441009ea47eSEdward Tomasz Napierala 1442398290f2SAlexander Motin TAILQ_FOREACH_SAFE(o, &lun->l_options, o_next, tmp) 1443398290f2SAlexander Motin option_delete(&lun->l_options, o); 1444920c6cbaSAlexander Motin free(lun->l_name); 1445009ea47eSEdward Tomasz Napierala free(lun->l_backend); 1446009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 1447009ea47eSEdward Tomasz Napierala free(lun->l_path); 1448920c6cbaSAlexander Motin free(lun->l_scsiname); 1449009ea47eSEdward Tomasz Napierala free(lun->l_serial); 1450009ea47eSEdward Tomasz Napierala free(lun); 1451009ea47eSEdward Tomasz Napierala } 1452009ea47eSEdward Tomasz Napierala 1453009ea47eSEdward Tomasz Napierala struct lun * 1454920c6cbaSAlexander Motin lun_find(const struct conf *conf, const char *name) 1455009ea47eSEdward Tomasz Napierala { 1456009ea47eSEdward Tomasz Napierala struct lun *lun; 1457009ea47eSEdward Tomasz Napierala 1458920c6cbaSAlexander Motin TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1459920c6cbaSAlexander Motin if (strcmp(lun->l_name, name) == 0) 1460009ea47eSEdward Tomasz Napierala return (lun); 1461009ea47eSEdward Tomasz Napierala } 1462009ea47eSEdward Tomasz Napierala 1463009ea47eSEdward Tomasz Napierala return (NULL); 1464009ea47eSEdward Tomasz Napierala } 1465009ea47eSEdward Tomasz Napierala 1466009ea47eSEdward Tomasz Napierala void 1467009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value) 1468009ea47eSEdward Tomasz Napierala { 1469009ea47eSEdward Tomasz Napierala free(lun->l_backend); 1470009ea47eSEdward Tomasz Napierala lun->l_backend = checked_strdup(value); 1471009ea47eSEdward Tomasz Napierala } 1472009ea47eSEdward Tomasz Napierala 1473009ea47eSEdward Tomasz Napierala void 1474009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value) 1475009ea47eSEdward Tomasz Napierala { 1476009ea47eSEdward Tomasz Napierala 1477009ea47eSEdward Tomasz Napierala lun->l_blocksize = value; 1478009ea47eSEdward Tomasz Napierala } 1479009ea47eSEdward Tomasz Napierala 1480009ea47eSEdward Tomasz Napierala void 148191be33dcSAlexander Motin lun_set_device_type(struct lun *lun, uint8_t value) 148291be33dcSAlexander Motin { 148391be33dcSAlexander Motin 148491be33dcSAlexander Motin lun->l_device_type = value; 148591be33dcSAlexander Motin } 148691be33dcSAlexander Motin 148791be33dcSAlexander Motin void 1488009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value) 1489009ea47eSEdward Tomasz Napierala { 1490009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 1491009ea47eSEdward Tomasz Napierala lun->l_device_id = checked_strdup(value); 1492009ea47eSEdward Tomasz Napierala } 1493009ea47eSEdward Tomasz Napierala 1494009ea47eSEdward Tomasz Napierala void 1495009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value) 1496009ea47eSEdward Tomasz Napierala { 1497009ea47eSEdward Tomasz Napierala free(lun->l_path); 1498009ea47eSEdward Tomasz Napierala lun->l_path = checked_strdup(value); 1499009ea47eSEdward Tomasz Napierala } 1500009ea47eSEdward Tomasz Napierala 1501009ea47eSEdward Tomasz Napierala void 1502920c6cbaSAlexander Motin lun_set_scsiname(struct lun *lun, const char *value) 1503920c6cbaSAlexander Motin { 1504920c6cbaSAlexander Motin free(lun->l_scsiname); 1505920c6cbaSAlexander Motin lun->l_scsiname = checked_strdup(value); 1506920c6cbaSAlexander Motin } 1507920c6cbaSAlexander Motin 1508920c6cbaSAlexander Motin void 1509009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value) 1510009ea47eSEdward Tomasz Napierala { 1511009ea47eSEdward Tomasz Napierala free(lun->l_serial); 1512009ea47eSEdward Tomasz Napierala lun->l_serial = checked_strdup(value); 1513009ea47eSEdward Tomasz Napierala } 1514009ea47eSEdward Tomasz Napierala 1515009ea47eSEdward Tomasz Napierala void 1516009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value) 1517009ea47eSEdward Tomasz Napierala { 1518009ea47eSEdward Tomasz Napierala 1519009ea47eSEdward Tomasz Napierala lun->l_size = value; 1520009ea47eSEdward Tomasz Napierala } 1521009ea47eSEdward Tomasz Napierala 1522009ea47eSEdward Tomasz Napierala void 1523009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value) 1524009ea47eSEdward Tomasz Napierala { 1525009ea47eSEdward Tomasz Napierala 1526009ea47eSEdward Tomasz Napierala lun->l_ctl_lun = value; 1527009ea47eSEdward Tomasz Napierala } 1528009ea47eSEdward Tomasz Napierala 1529398290f2SAlexander Motin struct option * 1530398290f2SAlexander Motin option_new(struct options *options, const char *name, const char *value) 1531009ea47eSEdward Tomasz Napierala { 1532398290f2SAlexander Motin struct option *o; 1533009ea47eSEdward Tomasz Napierala 1534398290f2SAlexander Motin o = option_find(options, name); 1535398290f2SAlexander Motin if (o != NULL) { 1536398290f2SAlexander Motin log_warnx("duplicated option \"%s\"", name); 1537009ea47eSEdward Tomasz Napierala return (NULL); 1538009ea47eSEdward Tomasz Napierala } 1539009ea47eSEdward Tomasz Napierala 1540398290f2SAlexander Motin o = calloc(1, sizeof(*o)); 1541398290f2SAlexander Motin if (o == NULL) 1542009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1543398290f2SAlexander Motin o->o_name = checked_strdup(name); 1544398290f2SAlexander Motin o->o_value = checked_strdup(value); 1545398290f2SAlexander Motin TAILQ_INSERT_TAIL(options, o, o_next); 1546009ea47eSEdward Tomasz Napierala 1547398290f2SAlexander Motin return (o); 1548009ea47eSEdward Tomasz Napierala } 1549009ea47eSEdward Tomasz Napierala 1550009ea47eSEdward Tomasz Napierala void 1551398290f2SAlexander Motin option_delete(struct options *options, struct option *o) 1552009ea47eSEdward Tomasz Napierala { 1553009ea47eSEdward Tomasz Napierala 1554398290f2SAlexander Motin TAILQ_REMOVE(options, o, o_next); 1555398290f2SAlexander Motin free(o->o_name); 1556398290f2SAlexander Motin free(o->o_value); 1557398290f2SAlexander Motin free(o); 1558009ea47eSEdward Tomasz Napierala } 1559009ea47eSEdward Tomasz Napierala 1560398290f2SAlexander Motin struct option * 1561398290f2SAlexander Motin option_find(const struct options *options, const char *name) 1562009ea47eSEdward Tomasz Napierala { 1563398290f2SAlexander Motin struct option *o; 1564009ea47eSEdward Tomasz Napierala 1565398290f2SAlexander Motin TAILQ_FOREACH(o, options, o_next) { 1566398290f2SAlexander Motin if (strcmp(o->o_name, name) == 0) 1567398290f2SAlexander Motin return (o); 1568009ea47eSEdward Tomasz Napierala } 1569009ea47eSEdward Tomasz Napierala 1570009ea47eSEdward Tomasz Napierala return (NULL); 1571009ea47eSEdward Tomasz Napierala } 1572009ea47eSEdward Tomasz Napierala 1573009ea47eSEdward Tomasz Napierala void 1574398290f2SAlexander Motin option_set(struct option *o, const char *value) 1575009ea47eSEdward Tomasz Napierala { 1576009ea47eSEdward Tomasz Napierala 1577398290f2SAlexander Motin free(o->o_value); 1578398290f2SAlexander Motin o->o_value = checked_strdup(value); 1579009ea47eSEdward Tomasz Napierala } 1580009ea47eSEdward Tomasz Napierala 1581009ea47eSEdward Tomasz Napierala static struct connection * 1582073edb1cSAlexander Motin connection_new(struct portal *portal, int fd, const char *host, 1583073edb1cSAlexander Motin const struct sockaddr *client_sa) 1584009ea47eSEdward Tomasz Napierala { 1585009ea47eSEdward Tomasz Napierala struct connection *conn; 1586009ea47eSEdward Tomasz Napierala 1587009ea47eSEdward Tomasz Napierala conn = calloc(1, sizeof(*conn)); 1588009ea47eSEdward Tomasz Napierala if (conn == NULL) 1589009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 1590009ea47eSEdward Tomasz Napierala conn->conn_portal = portal; 1591009ea47eSEdward Tomasz Napierala conn->conn_socket = fd; 1592009ea47eSEdward Tomasz Napierala conn->conn_initiator_addr = checked_strdup(host); 1593073edb1cSAlexander Motin memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len); 1594009ea47eSEdward Tomasz Napierala 1595009ea47eSEdward Tomasz Napierala /* 1596009ea47eSEdward Tomasz Napierala * Default values, from RFC 3720, section 12. 1597009ea47eSEdward Tomasz Napierala */ 159897b84d34SNavdeep Parhar conn->conn_max_recv_data_segment_length = 8192; 159959c6e3a5SAlexander Motin conn->conn_max_send_data_segment_length = 8192; 1600009ea47eSEdward Tomasz Napierala conn->conn_max_burst_length = 262144; 160197b84d34SNavdeep Parhar conn->conn_first_burst_length = 65536; 1602009ea47eSEdward Tomasz Napierala conn->conn_immediate_data = true; 1603009ea47eSEdward Tomasz Napierala 1604009ea47eSEdward Tomasz Napierala return (conn); 1605009ea47eSEdward Tomasz Napierala } 1606009ea47eSEdward Tomasz Napierala 1607009ea47eSEdward Tomasz Napierala #if 0 1608009ea47eSEdward Tomasz Napierala static void 1609009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf) 1610009ea47eSEdward Tomasz Napierala { 1611009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1612009ea47eSEdward Tomasz Napierala struct auth *auth; 16138cb2e958SEdward Tomasz Napierala struct auth_name *auth_name; 16148cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal; 1615009ea47eSEdward Tomasz Napierala struct portal_group *pg; 1616009ea47eSEdward Tomasz Napierala struct portal *portal; 1617009ea47eSEdward Tomasz Napierala struct target *targ; 1618009ea47eSEdward Tomasz Napierala struct lun *lun; 1619398290f2SAlexander Motin struct option *o; 1620009ea47eSEdward Tomasz Napierala 1621009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1622009ea47eSEdward Tomasz Napierala fprintf(stderr, "auth-group %s {\n", ag->ag_name); 1623009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) 1624009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t chap-mutual %s %s %s %s\n", 1625009ea47eSEdward Tomasz Napierala auth->a_user, auth->a_secret, 1626009ea47eSEdward Tomasz Napierala auth->a_mutual_user, auth->a_mutual_secret); 16278cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) 16288cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-name %s\n", 16298cb2e958SEdward Tomasz Napierala auth_name->an_initator_name); 16308cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next) 16318cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-portal %s\n", 16328cb2e958SEdward Tomasz Napierala auth_portal->an_initator_portal); 1633009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1634009ea47eSEdward Tomasz Napierala } 1635009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1636009ea47eSEdward Tomasz Napierala fprintf(stderr, "portal-group %s {\n", pg->pg_name); 1637009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1638009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t listen %s\n", portal->p_listen); 1639009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1640009ea47eSEdward Tomasz Napierala } 1641920c6cbaSAlexander Motin TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1642920c6cbaSAlexander Motin fprintf(stderr, "\tlun %s {\n", lun->l_name); 1643009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\tpath %s\n", lun->l_path); 1644398290f2SAlexander Motin TAILQ_FOREACH(o, &lun->l_options, o_next) 1645009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\toption %s %s\n", 1646398290f2SAlexander Motin lo->o_name, lo->o_value); 1647009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t}\n"); 1648009ea47eSEdward Tomasz Napierala } 1649920c6cbaSAlexander Motin TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1650920c6cbaSAlexander Motin fprintf(stderr, "target %s {\n", targ->t_name); 1651920c6cbaSAlexander Motin if (targ->t_alias != NULL) 1652920c6cbaSAlexander Motin fprintf(stderr, "\t alias %s\n", targ->t_alias); 1653009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 1654009ea47eSEdward Tomasz Napierala } 1655009ea47eSEdward Tomasz Napierala } 1656009ea47eSEdward Tomasz Napierala #endif 1657009ea47eSEdward Tomasz Napierala 16582fabfaa5SEdward Tomasz Napierala static int 16592fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun) 16602fabfaa5SEdward Tomasz Napierala { 16612fabfaa5SEdward Tomasz Napierala const struct lun *lun2; 16622fabfaa5SEdward Tomasz Napierala 16632fabfaa5SEdward Tomasz Napierala if (lun->l_backend == NULL) 16642fabfaa5SEdward Tomasz Napierala lun_set_backend(lun, "block"); 16652fabfaa5SEdward Tomasz Napierala if (strcmp(lun->l_backend, "block") == 0) { 16662fabfaa5SEdward Tomasz Napierala if (lun->l_path == NULL) { 1667920c6cbaSAlexander Motin log_warnx("missing path for lun \"%s\"", 1668920c6cbaSAlexander Motin lun->l_name); 16692fabfaa5SEdward Tomasz Napierala return (1); 16702fabfaa5SEdward Tomasz Napierala } 16712fabfaa5SEdward Tomasz Napierala } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 16722fabfaa5SEdward Tomasz Napierala if (lun->l_size == 0) { 1673920c6cbaSAlexander Motin log_warnx("missing size for ramdisk-backed lun \"%s\"", 1674920c6cbaSAlexander Motin lun->l_name); 16752fabfaa5SEdward Tomasz Napierala return (1); 16762fabfaa5SEdward Tomasz Napierala } 16772fabfaa5SEdward Tomasz Napierala if (lun->l_path != NULL) { 16782fabfaa5SEdward Tomasz Napierala log_warnx("path must not be specified " 1679920c6cbaSAlexander Motin "for ramdisk-backed lun \"%s\"", 1680920c6cbaSAlexander Motin lun->l_name); 16812fabfaa5SEdward Tomasz Napierala return (1); 16822fabfaa5SEdward Tomasz Napierala } 16832fabfaa5SEdward Tomasz Napierala } 16842fabfaa5SEdward Tomasz Napierala if (lun->l_blocksize == 0) { 16856f2f92a5SAlexander Motin if (lun->l_device_type == 5) 16866f2f92a5SAlexander Motin lun_set_blocksize(lun, DEFAULT_CD_BLOCKSIZE); 16876f2f92a5SAlexander Motin else 16882fabfaa5SEdward Tomasz Napierala lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 16892fabfaa5SEdward Tomasz Napierala } else if (lun->l_blocksize < 0) { 1690920c6cbaSAlexander Motin log_warnx("invalid blocksize for lun \"%s\"; " 1691920c6cbaSAlexander Motin "must be larger than 0", lun->l_name); 16922fabfaa5SEdward Tomasz Napierala return (1); 16932fabfaa5SEdward Tomasz Napierala } 16942fabfaa5SEdward Tomasz Napierala if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 1695920c6cbaSAlexander Motin log_warnx("invalid size for lun \"%s\"; " 1696920c6cbaSAlexander Motin "must be multiple of blocksize", lun->l_name); 16972fabfaa5SEdward Tomasz Napierala return (1); 16982fabfaa5SEdward Tomasz Napierala } 1699920c6cbaSAlexander Motin TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) { 1700a19eebb1SEdward Tomasz Napierala if (lun == lun2) 1701a19eebb1SEdward Tomasz Napierala continue; 1702a19eebb1SEdward Tomasz Napierala if (lun->l_path != NULL && lun2->l_path != NULL && 1703a19eebb1SEdward Tomasz Napierala strcmp(lun->l_path, lun2->l_path) == 0) { 1704a19eebb1SEdward Tomasz Napierala log_debugx("WARNING: path \"%s\" duplicated " 1705920c6cbaSAlexander Motin "between lun \"%s\", and " 1706920c6cbaSAlexander Motin "lun \"%s\"", lun->l_path, 1707920c6cbaSAlexander Motin lun->l_name, lun2->l_name); 1708a19eebb1SEdward Tomasz Napierala } 1709a19eebb1SEdward Tomasz Napierala } 17102fabfaa5SEdward Tomasz Napierala 17112fabfaa5SEdward Tomasz Napierala return (0); 17122fabfaa5SEdward Tomasz Napierala } 17132fabfaa5SEdward Tomasz Napierala 1714009ea47eSEdward Tomasz Napierala int 1715009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf) 1716009ea47eSEdward Tomasz Napierala { 1717009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1718009ea47eSEdward Tomasz Napierala struct portal_group *pg; 171992847ee1SAlexander Motin struct port *port; 1720009ea47eSEdward Tomasz Napierala struct target *targ; 17212fabfaa5SEdward Tomasz Napierala struct lun *lun; 17224e5c38f8SEdward Tomasz Napierala bool found; 1723920c6cbaSAlexander Motin int error, i; 1724009ea47eSEdward Tomasz Napierala 1725009ea47eSEdward Tomasz Napierala if (conf->conf_pidfile_path == NULL) 1726009ea47eSEdward Tomasz Napierala conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1727009ea47eSEdward Tomasz Napierala 1728920c6cbaSAlexander Motin TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1729920c6cbaSAlexander Motin error = conf_verify_lun(lun); 1730920c6cbaSAlexander Motin if (error != 0) 1731920c6cbaSAlexander Motin return (error); 1732920c6cbaSAlexander Motin } 1733009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1734009ea47eSEdward Tomasz Napierala if (targ->t_auth_group == NULL) { 1735e7977549SEdward Tomasz Napierala targ->t_auth_group = auth_group_find(conf, 1736e7977549SEdward Tomasz Napierala "default"); 1737e7977549SEdward Tomasz Napierala assert(targ->t_auth_group != NULL); 1738009ea47eSEdward Tomasz Napierala } 173992847ee1SAlexander Motin if (TAILQ_EMPTY(&targ->t_ports)) { 174092847ee1SAlexander Motin pg = portal_group_find(conf, "default"); 174192847ee1SAlexander Motin assert(pg != NULL); 174292847ee1SAlexander Motin port_new(conf, targ, pg); 1743009ea47eSEdward Tomasz Napierala } 17444e5c38f8SEdward Tomasz Napierala found = false; 1745920c6cbaSAlexander Motin for (i = 0; i < MAX_LUNS; i++) { 1746920c6cbaSAlexander Motin if (targ->t_luns[i] != NULL) 17474e5c38f8SEdward Tomasz Napierala found = true; 1748009ea47eSEdward Tomasz Napierala } 17496dbdbf82SEdward Tomasz Napierala if (!found && targ->t_redirection == NULL) { 175019b59f13SEdward Tomasz Napierala log_warnx("no LUNs defined for target \"%s\"", 175119b59f13SEdward Tomasz Napierala targ->t_name); 1752009ea47eSEdward Tomasz Napierala } 17536dbdbf82SEdward Tomasz Napierala if (found && targ->t_redirection != NULL) { 17546dbdbf82SEdward Tomasz Napierala log_debugx("target \"%s\" contains luns, " 17556dbdbf82SEdward Tomasz Napierala " but configured for redirection", 17566dbdbf82SEdward Tomasz Napierala targ->t_name); 17576dbdbf82SEdward Tomasz Napierala } 1758009ea47eSEdward Tomasz Napierala } 1759009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1760009ea47eSEdward Tomasz Napierala assert(pg->pg_name != NULL); 1761009ea47eSEdward Tomasz Napierala if (pg->pg_discovery_auth_group == NULL) { 1762009ea47eSEdward Tomasz Napierala pg->pg_discovery_auth_group = 1763ccb1f04dSEdward Tomasz Napierala auth_group_find(conf, "default"); 1764009ea47eSEdward Tomasz Napierala assert(pg->pg_discovery_auth_group != NULL); 1765009ea47eSEdward Tomasz Napierala } 1766009ea47eSEdward Tomasz Napierala 176705374883SEdward Tomasz Napierala if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN) 176805374883SEdward Tomasz Napierala pg->pg_discovery_filter = PG_FILTER_NONE; 176905374883SEdward Tomasz Napierala 17706dbdbf82SEdward Tomasz Napierala if (pg->pg_redirection != NULL) { 1771db7bf2baSAlexander Motin if (!TAILQ_EMPTY(&pg->pg_ports)) { 17726dbdbf82SEdward Tomasz Napierala log_debugx("portal-group \"%s\" assigned " 177392847ee1SAlexander Motin "to target, but configured " 17746dbdbf82SEdward Tomasz Napierala "for redirection", 177592847ee1SAlexander Motin pg->pg_name); 17766dbdbf82SEdward Tomasz Napierala } 17776dbdbf82SEdward Tomasz Napierala pg->pg_unassigned = false; 1778db7bf2baSAlexander Motin } else if (!TAILQ_EMPTY(&pg->pg_ports)) { 1779db7bf2baSAlexander Motin pg->pg_unassigned = false; 17806dbdbf82SEdward Tomasz Napierala } else { 1781009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, "default") != 0) 1782009ea47eSEdward Tomasz Napierala log_warnx("portal-group \"%s\" not assigned " 1783009ea47eSEdward Tomasz Napierala "to any target", pg->pg_name); 1784009ea47eSEdward Tomasz Napierala pg->pg_unassigned = true; 17856dbdbf82SEdward Tomasz Napierala } 1786009ea47eSEdward Tomasz Napierala } 1787009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1788009ea47eSEdward Tomasz Napierala if (ag->ag_name == NULL) 1789009ea47eSEdward Tomasz Napierala assert(ag->ag_target != NULL); 1790009ea47eSEdward Tomasz Napierala else 1791009ea47eSEdward Tomasz Napierala assert(ag->ag_target == NULL); 1792009ea47eSEdward Tomasz Napierala 17934e5c38f8SEdward Tomasz Napierala found = false; 1794009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 17954e5c38f8SEdward Tomasz Napierala if (targ->t_auth_group == ag) { 17964e5c38f8SEdward Tomasz Napierala found = true; 1797009ea47eSEdward Tomasz Napierala break; 1798009ea47eSEdward Tomasz Napierala } 17994e5c38f8SEdward Tomasz Napierala } 180092847ee1SAlexander Motin TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 180192847ee1SAlexander Motin if (port->p_auth_group == ag) { 180292847ee1SAlexander Motin found = true; 180392847ee1SAlexander Motin break; 180492847ee1SAlexander Motin } 180592847ee1SAlexander Motin } 18064e5c38f8SEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 18074e5c38f8SEdward Tomasz Napierala if (pg->pg_discovery_auth_group == ag) { 18084e5c38f8SEdward Tomasz Napierala found = true; 18094e5c38f8SEdward Tomasz Napierala break; 18104e5c38f8SEdward Tomasz Napierala } 18114e5c38f8SEdward Tomasz Napierala } 18124e5c38f8SEdward Tomasz Napierala if (!found && ag->ag_name != NULL && 1813ccb1f04dSEdward Tomasz Napierala strcmp(ag->ag_name, "default") != 0 && 1814009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-authentication") != 0 && 1815009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-access") != 0) { 1816009ea47eSEdward Tomasz Napierala log_warnx("auth-group \"%s\" not assigned " 1817009ea47eSEdward Tomasz Napierala "to any target", ag->ag_name); 1818009ea47eSEdward Tomasz Napierala } 1819009ea47eSEdward Tomasz Napierala } 1820009ea47eSEdward Tomasz Napierala 1821009ea47eSEdward Tomasz Napierala return (0); 1822009ea47eSEdward Tomasz Napierala } 1823009ea47eSEdward Tomasz Napierala 1824009ea47eSEdward Tomasz Napierala static int 1825009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf) 1826009ea47eSEdward Tomasz Napierala { 1827009ea47eSEdward Tomasz Napierala struct lun *oldlun, *newlun, *tmplun; 1828009ea47eSEdward Tomasz Napierala struct portal_group *oldpg, *newpg; 1829009ea47eSEdward Tomasz Napierala struct portal *oldp, *newp; 183092847ee1SAlexander Motin struct port *oldport, *newport, *tmpport; 1831829603e2SAlexander Motin struct isns *oldns, *newns; 1832009ea47eSEdward Tomasz Napierala pid_t otherpid; 18338b94b583SAlexander Motin int changed, cumulated_error = 0, error, sockbuf; 1834009ea47eSEdward Tomasz Napierala int one = 1; 1835009ea47eSEdward Tomasz Napierala 1836009ea47eSEdward Tomasz Napierala if (oldconf->conf_debug != newconf->conf_debug) { 1837009ea47eSEdward Tomasz Napierala log_debugx("changing debug level to %d", newconf->conf_debug); 1838009ea47eSEdward Tomasz Napierala log_init(newconf->conf_debug); 1839009ea47eSEdward Tomasz Napierala } 1840009ea47eSEdward Tomasz Napierala 1841009ea47eSEdward Tomasz Napierala if (oldconf->conf_pidfh != NULL) { 1842009ea47eSEdward Tomasz Napierala assert(oldconf->conf_pidfile_path != NULL); 1843009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfile_path != NULL && 1844009ea47eSEdward Tomasz Napierala strcmp(oldconf->conf_pidfile_path, 1845009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path) == 0) { 1846009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = oldconf->conf_pidfh; 1847009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1848009ea47eSEdward Tomasz Napierala } else { 1849009ea47eSEdward Tomasz Napierala log_debugx("removing pidfile %s", 1850009ea47eSEdward Tomasz Napierala oldconf->conf_pidfile_path); 1851009ea47eSEdward Tomasz Napierala pidfile_remove(oldconf->conf_pidfh); 1852009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1853009ea47eSEdward Tomasz Napierala } 1854009ea47eSEdward Tomasz Napierala } 1855009ea47eSEdward Tomasz Napierala 1856009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1857009ea47eSEdward Tomasz Napierala log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1858009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = 1859009ea47eSEdward Tomasz Napierala pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1860009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL) { 1861009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 1862009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 1863009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 1864009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 1865009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path); 1866009ea47eSEdward Tomasz Napierala } 1867009ea47eSEdward Tomasz Napierala } 1868009ea47eSEdward Tomasz Napierala 18696b31e130SAlexander Motin /* 18706b31e130SAlexander Motin * Go through the new portal groups, assigning tags or preserving old. 18716b31e130SAlexander Motin */ 18726b31e130SAlexander Motin TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1873db7bf2baSAlexander Motin if (newpg->pg_tag != 0) 1874db7bf2baSAlexander Motin continue; 18756b31e130SAlexander Motin oldpg = portal_group_find(oldconf, newpg->pg_name); 18766b31e130SAlexander Motin if (oldpg != NULL) 18776b31e130SAlexander Motin newpg->pg_tag = oldpg->pg_tag; 18786b31e130SAlexander Motin else 18796b31e130SAlexander Motin newpg->pg_tag = ++last_portal_group_tag; 18806b31e130SAlexander Motin } 18816b31e130SAlexander Motin 1882829603e2SAlexander Motin /* Deregister on removed iSNS servers. */ 1883829603e2SAlexander Motin TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1884829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1885829603e2SAlexander Motin if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1886829603e2SAlexander Motin break; 1887829603e2SAlexander Motin } 1888829603e2SAlexander Motin if (newns == NULL) 1889829603e2SAlexander Motin isns_deregister(oldns); 1890829603e2SAlexander Motin } 1891829603e2SAlexander Motin 1892d5e316e5SEdward Tomasz Napierala /* 1893d5e316e5SEdward Tomasz Napierala * XXX: If target or lun removal fails, we should somehow "move" 1894d5e316e5SEdward Tomasz Napierala * the old lun or target into newconf, so that subsequent 189561a2a354SEdward Tomasz Napierala * conf_apply() would try to remove them again. That would 189661a2a354SEdward Tomasz Napierala * be somewhat hairy, though, and lun deletion failures don't 189761a2a354SEdward Tomasz Napierala * really happen, so leave it as it is for now. 1898d5e316e5SEdward Tomasz Napierala */ 1899009ea47eSEdward Tomasz Napierala /* 190092847ee1SAlexander Motin * First, remove any ports present in the old configuration 1901009ea47eSEdward Tomasz Napierala * and missing in the new one. 1902009ea47eSEdward Tomasz Napierala */ 190392847ee1SAlexander Motin TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) { 19049215e501SAlexander Motin if (port_is_dummy(oldport)) 1905db7bf2baSAlexander Motin continue; 190692847ee1SAlexander Motin newport = port_find(newconf, oldport->p_name); 19079215e501SAlexander Motin if (newport != NULL && !port_is_dummy(newport)) 1908920c6cbaSAlexander Motin continue; 1909057abcb0SAlexander Motin log_debugx("removing port \"%s\"", oldport->p_name); 191092847ee1SAlexander Motin error = kernel_port_remove(oldport); 19116de78f9fSAlexander Motin if (error != 0) { 191292847ee1SAlexander Motin log_warnx("failed to remove port %s", 191392847ee1SAlexander Motin oldport->p_name); 19146de78f9fSAlexander Motin /* 19156de78f9fSAlexander Motin * XXX: Uncomment after fixing the root cause. 19166de78f9fSAlexander Motin * 19176de78f9fSAlexander Motin * cumulated_error++; 19186de78f9fSAlexander Motin */ 19196de78f9fSAlexander Motin } 1920009ea47eSEdward Tomasz Napierala } 1921009ea47eSEdward Tomasz Napierala 1922009ea47eSEdward Tomasz Napierala /* 1923920c6cbaSAlexander Motin * Second, remove any LUNs present in the old configuration 1924009ea47eSEdward Tomasz Napierala * and missing in the new one. 1925009ea47eSEdward Tomasz Napierala */ 1926920c6cbaSAlexander Motin TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) { 1927920c6cbaSAlexander Motin newlun = lun_find(newconf, oldlun->l_name); 1928009ea47eSEdward Tomasz Napierala if (newlun == NULL) { 1929920c6cbaSAlexander Motin log_debugx("lun \"%s\", CTL lun %d " 1930cadf3831SEdward Tomasz Napierala "not found in new configuration; " 1931920c6cbaSAlexander Motin "removing", oldlun->l_name, oldlun->l_ctl_lun); 1932009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1933009ea47eSEdward Tomasz Napierala if (error != 0) { 1934920c6cbaSAlexander Motin log_warnx("failed to remove lun \"%s\", " 1935920c6cbaSAlexander Motin "CTL lun %d", 1936920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1937009ea47eSEdward Tomasz Napierala cumulated_error++; 1938009ea47eSEdward Tomasz Napierala } 1939009ea47eSEdward Tomasz Napierala continue; 1940009ea47eSEdward Tomasz Napierala } 1941009ea47eSEdward Tomasz Napierala 1942009ea47eSEdward Tomasz Napierala /* 1943009ea47eSEdward Tomasz Napierala * Also remove the LUNs changed by more than size. 1944009ea47eSEdward Tomasz Napierala */ 1945009ea47eSEdward Tomasz Napierala changed = 0; 1946009ea47eSEdward Tomasz Napierala assert(oldlun->l_backend != NULL); 1947009ea47eSEdward Tomasz Napierala assert(newlun->l_backend != NULL); 1948009ea47eSEdward Tomasz Napierala if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1949920c6cbaSAlexander Motin log_debugx("backend for lun \"%s\", " 1950009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1951920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1952009ea47eSEdward Tomasz Napierala changed = 1; 1953009ea47eSEdward Tomasz Napierala } 1954009ea47eSEdward Tomasz Napierala if (oldlun->l_blocksize != newlun->l_blocksize) { 1955920c6cbaSAlexander Motin log_debugx("blocksize for lun \"%s\", " 1956009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1957920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1958009ea47eSEdward Tomasz Napierala changed = 1; 1959009ea47eSEdward Tomasz Napierala } 1960009ea47eSEdward Tomasz Napierala if (newlun->l_device_id != NULL && 1961009ea47eSEdward Tomasz Napierala (oldlun->l_device_id == NULL || 1962009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_device_id, newlun->l_device_id) != 1963009ea47eSEdward Tomasz Napierala 0)) { 1964920c6cbaSAlexander Motin log_debugx("device-id for lun \"%s\", " 1965009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1966920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1967009ea47eSEdward Tomasz Napierala changed = 1; 1968009ea47eSEdward Tomasz Napierala } 1969009ea47eSEdward Tomasz Napierala if (newlun->l_path != NULL && 1970009ea47eSEdward Tomasz Napierala (oldlun->l_path == NULL || 1971009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1972920c6cbaSAlexander Motin log_debugx("path for lun \"%s\", " 1973009ea47eSEdward Tomasz Napierala "CTL lun %d, changed; removing", 1974920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1975009ea47eSEdward Tomasz Napierala changed = 1; 1976009ea47eSEdward Tomasz Napierala } 1977009ea47eSEdward Tomasz Napierala if (newlun->l_serial != NULL && 1978009ea47eSEdward Tomasz Napierala (oldlun->l_serial == NULL || 1979009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1980920c6cbaSAlexander Motin log_debugx("serial for lun \"%s\", " 1981009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1982920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1983009ea47eSEdward Tomasz Napierala changed = 1; 1984009ea47eSEdward Tomasz Napierala } 1985009ea47eSEdward Tomasz Napierala if (changed) { 1986009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1987009ea47eSEdward Tomasz Napierala if (error != 0) { 1988920c6cbaSAlexander Motin log_warnx("failed to remove lun \"%s\", " 1989920c6cbaSAlexander Motin "CTL lun %d", 1990920c6cbaSAlexander Motin oldlun->l_name, oldlun->l_ctl_lun); 1991009ea47eSEdward Tomasz Napierala cumulated_error++; 1992009ea47eSEdward Tomasz Napierala } 1993009ea47eSEdward Tomasz Napierala lun_delete(oldlun); 1994009ea47eSEdward Tomasz Napierala continue; 1995009ea47eSEdward Tomasz Napierala } 1996009ea47eSEdward Tomasz Napierala 1997009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1998009ea47eSEdward Tomasz Napierala } 1999920c6cbaSAlexander Motin 2000920c6cbaSAlexander Motin TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) { 2001920c6cbaSAlexander Motin oldlun = lun_find(oldconf, newlun->l_name); 2002920c6cbaSAlexander Motin if (oldlun != NULL) { 2003a3977beaSAlexander Motin log_debugx("modifying lun \"%s\", CTL lun %d", 2004920c6cbaSAlexander Motin newlun->l_name, newlun->l_ctl_lun); 2005a3977beaSAlexander Motin error = kernel_lun_modify(newlun); 2006920c6cbaSAlexander Motin if (error != 0) { 2007920c6cbaSAlexander Motin log_warnx("failed to " 2008a3977beaSAlexander Motin "modify lun \"%s\", CTL lun %d", 2009a3977beaSAlexander Motin newlun->l_name, newlun->l_ctl_lun); 2010920c6cbaSAlexander Motin cumulated_error++; 2011920c6cbaSAlexander Motin } 2012920c6cbaSAlexander Motin continue; 2013920c6cbaSAlexander Motin } 2014920c6cbaSAlexander Motin log_debugx("adding lun \"%s\"", newlun->l_name); 2015920c6cbaSAlexander Motin error = kernel_lun_add(newlun); 2016920c6cbaSAlexander Motin if (error != 0) { 2017920c6cbaSAlexander Motin log_warnx("failed to add lun \"%s\"", newlun->l_name); 2018920c6cbaSAlexander Motin lun_delete(newlun); 2019920c6cbaSAlexander Motin cumulated_error++; 2020920c6cbaSAlexander Motin } 2021009ea47eSEdward Tomasz Napierala } 2022009ea47eSEdward Tomasz Napierala 2023009ea47eSEdward Tomasz Napierala /* 202492847ee1SAlexander Motin * Now add new ports or modify existing ones. 2025009ea47eSEdward Tomasz Napierala */ 202692847ee1SAlexander Motin TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) { 20279215e501SAlexander Motin if (port_is_dummy(newport)) 2028db7bf2baSAlexander Motin continue; 202992847ee1SAlexander Motin oldport = port_find(oldconf, newport->p_name); 2030009ea47eSEdward Tomasz Napierala 20319215e501SAlexander Motin if (oldport == NULL || port_is_dummy(oldport)) { 2032057abcb0SAlexander Motin log_debugx("adding port \"%s\"", newport->p_name); 203392847ee1SAlexander Motin error = kernel_port_add(newport); 203492847ee1SAlexander Motin } else { 2035057abcb0SAlexander Motin log_debugx("updating port \"%s\"", newport->p_name); 203692847ee1SAlexander Motin newport->p_ctl_port = oldport->p_ctl_port; 2037828524c1SAlexander Motin error = kernel_port_update(newport, oldport); 2038920c6cbaSAlexander Motin } 2039e48bb73aSEdward Tomasz Napierala if (error != 0) { 204092847ee1SAlexander Motin log_warnx("failed to %s port %s", 204192847ee1SAlexander Motin (oldport == NULL) ? "add" : "update", 204292847ee1SAlexander Motin newport->p_name); 2043e48bb73aSEdward Tomasz Napierala /* 2044e48bb73aSEdward Tomasz Napierala * XXX: Uncomment after fixing the root cause. 2045e48bb73aSEdward Tomasz Napierala * 2046e48bb73aSEdward Tomasz Napierala * cumulated_error++; 2047e48bb73aSEdward Tomasz Napierala */ 2048e48bb73aSEdward Tomasz Napierala } 2049e48bb73aSEdward Tomasz Napierala } 2050009ea47eSEdward Tomasz Napierala 2051009ea47eSEdward Tomasz Napierala /* 2052463a577bSEitan Adler * Go through the new portals, opening the sockets as necessary. 2053009ea47eSEdward Tomasz Napierala */ 2054009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 2055db7bf2baSAlexander Motin if (newpg->pg_foreign) 2056db7bf2baSAlexander Motin continue; 2057009ea47eSEdward Tomasz Napierala if (newpg->pg_unassigned) { 2058009ea47eSEdward Tomasz Napierala log_debugx("not listening on portal-group \"%s\", " 2059009ea47eSEdward Tomasz Napierala "not assigned to any target", 2060009ea47eSEdward Tomasz Napierala newpg->pg_name); 2061009ea47eSEdward Tomasz Napierala continue; 2062009ea47eSEdward Tomasz Napierala } 2063009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 2064009ea47eSEdward Tomasz Napierala /* 2065009ea47eSEdward Tomasz Napierala * Try to find already open portal and reuse 2066009ea47eSEdward Tomasz Napierala * the listening socket. We don't care about 2067009ea47eSEdward Tomasz Napierala * what portal or portal group that was, what 2068009ea47eSEdward Tomasz Napierala * matters is the listening address. 2069009ea47eSEdward Tomasz Napierala */ 2070009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 2071009ea47eSEdward Tomasz Napierala pg_next) { 2072009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, 2073009ea47eSEdward Tomasz Napierala p_next) { 2074009ea47eSEdward Tomasz Napierala if (strcmp(newp->p_listen, 2075009ea47eSEdward Tomasz Napierala oldp->p_listen) == 0 && 2076009ea47eSEdward Tomasz Napierala oldp->p_socket > 0) { 2077009ea47eSEdward Tomasz Napierala newp->p_socket = 2078009ea47eSEdward Tomasz Napierala oldp->p_socket; 2079009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 2080009ea47eSEdward Tomasz Napierala break; 2081009ea47eSEdward Tomasz Napierala } 2082009ea47eSEdward Tomasz Napierala } 2083009ea47eSEdward Tomasz Napierala } 2084009ea47eSEdward Tomasz Napierala if (newp->p_socket > 0) { 2085009ea47eSEdward Tomasz Napierala /* 2086009ea47eSEdward Tomasz Napierala * We're done with this portal. 2087009ea47eSEdward Tomasz Napierala */ 2088009ea47eSEdward Tomasz Napierala continue; 2089009ea47eSEdward Tomasz Napierala } 2090009ea47eSEdward Tomasz Napierala 2091009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2092ba3a2d31SEdward Tomasz Napierala if (proxy_mode) { 20938cab2ed4SEdward Tomasz Napierala newpg->pg_conf->conf_portal_id++; 20948cab2ed4SEdward Tomasz Napierala newp->p_id = newpg->pg_conf->conf_portal_id; 20958cab2ed4SEdward Tomasz Napierala log_debugx("listening on %s, portal-group " 20968cab2ed4SEdward Tomasz Napierala "\"%s\", portal id %d, using ICL proxy", 20978cab2ed4SEdward Tomasz Napierala newp->p_listen, newpg->pg_name, newp->p_id); 20988cab2ed4SEdward Tomasz Napierala kernel_listen(newp->p_ai, newp->p_iser, 20998cab2ed4SEdward Tomasz Napierala newp->p_id); 2100ba3a2d31SEdward Tomasz Napierala continue; 2101ba3a2d31SEdward Tomasz Napierala } 2102ba3a2d31SEdward Tomasz Napierala #endif 2103ba3a2d31SEdward Tomasz Napierala assert(proxy_mode == false); 2104009ea47eSEdward Tomasz Napierala assert(newp->p_iser == false); 2105009ea47eSEdward Tomasz Napierala 2106009ea47eSEdward Tomasz Napierala log_debugx("listening on %s, portal-group \"%s\"", 2107009ea47eSEdward Tomasz Napierala newp->p_listen, newpg->pg_name); 2108009ea47eSEdward Tomasz Napierala newp->p_socket = socket(newp->p_ai->ai_family, 2109009ea47eSEdward Tomasz Napierala newp->p_ai->ai_socktype, 2110009ea47eSEdward Tomasz Napierala newp->p_ai->ai_protocol); 2111009ea47eSEdward Tomasz Napierala if (newp->p_socket < 0) { 2112009ea47eSEdward Tomasz Napierala log_warn("socket(2) failed for %s", 2113009ea47eSEdward Tomasz Napierala newp->p_listen); 2114009ea47eSEdward Tomasz Napierala cumulated_error++; 2115009ea47eSEdward Tomasz Napierala continue; 2116009ea47eSEdward Tomasz Napierala } 21178b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 21188b94b583SAlexander Motin if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF, 21198b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 21208b94b583SAlexander Motin log_warn("setsockopt(SO_RCVBUF) failed " 21218b94b583SAlexander Motin "for %s", newp->p_listen); 21228b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 21238b94b583SAlexander Motin if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF, 21248b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 21258b94b583SAlexander Motin log_warn("setsockopt(SO_SNDBUF) failed " 21268b94b583SAlexander Motin "for %s", newp->p_listen); 2127009ea47eSEdward Tomasz Napierala error = setsockopt(newp->p_socket, SOL_SOCKET, 2128009ea47eSEdward Tomasz Napierala SO_REUSEADDR, &one, sizeof(one)); 2129009ea47eSEdward Tomasz Napierala if (error != 0) { 2130009ea47eSEdward Tomasz Napierala log_warn("setsockopt(SO_REUSEADDR) failed " 2131009ea47eSEdward Tomasz Napierala "for %s", newp->p_listen); 2132009ea47eSEdward Tomasz Napierala close(newp->p_socket); 2133009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 2134009ea47eSEdward Tomasz Napierala cumulated_error++; 2135009ea47eSEdward Tomasz Napierala continue; 2136009ea47eSEdward Tomasz Napierala } 2137009ea47eSEdward Tomasz Napierala error = bind(newp->p_socket, newp->p_ai->ai_addr, 2138009ea47eSEdward Tomasz Napierala newp->p_ai->ai_addrlen); 2139009ea47eSEdward Tomasz Napierala if (error != 0) { 2140009ea47eSEdward Tomasz Napierala log_warn("bind(2) failed for %s", 2141009ea47eSEdward Tomasz Napierala newp->p_listen); 2142009ea47eSEdward Tomasz Napierala close(newp->p_socket); 2143009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 2144009ea47eSEdward Tomasz Napierala cumulated_error++; 2145009ea47eSEdward Tomasz Napierala continue; 2146009ea47eSEdward Tomasz Napierala } 2147009ea47eSEdward Tomasz Napierala error = listen(newp->p_socket, -1); 2148009ea47eSEdward Tomasz Napierala if (error != 0) { 2149009ea47eSEdward Tomasz Napierala log_warn("listen(2) failed for %s", 2150009ea47eSEdward Tomasz Napierala newp->p_listen); 2151009ea47eSEdward Tomasz Napierala close(newp->p_socket); 2152009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 2153009ea47eSEdward Tomasz Napierala cumulated_error++; 2154009ea47eSEdward Tomasz Napierala continue; 2155009ea47eSEdward Tomasz Napierala } 2156009ea47eSEdward Tomasz Napierala } 2157009ea47eSEdward Tomasz Napierala } 2158009ea47eSEdward Tomasz Napierala 2159009ea47eSEdward Tomasz Napierala /* 2160009ea47eSEdward Tomasz Napierala * Go through the no longer used sockets, closing them. 2161009ea47eSEdward Tomasz Napierala */ 2162009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 2163009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 2164009ea47eSEdward Tomasz Napierala if (oldp->p_socket <= 0) 2165009ea47eSEdward Tomasz Napierala continue; 2166009ea47eSEdward Tomasz Napierala log_debugx("closing socket for %s, portal-group \"%s\"", 2167009ea47eSEdward Tomasz Napierala oldp->p_listen, oldpg->pg_name); 2168009ea47eSEdward Tomasz Napierala close(oldp->p_socket); 2169009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 2170009ea47eSEdward Tomasz Napierala } 2171009ea47eSEdward Tomasz Napierala } 2172009ea47eSEdward Tomasz Napierala 2173829603e2SAlexander Motin /* (Re-)Register on remaining/new iSNS servers. */ 2174829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 2175829603e2SAlexander Motin TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 2176829603e2SAlexander Motin if (strcmp(oldns->i_addr, newns->i_addr) == 0) 2177829603e2SAlexander Motin break; 2178829603e2SAlexander Motin } 2179829603e2SAlexander Motin isns_register(newns, oldns); 2180829603e2SAlexander Motin } 2181829603e2SAlexander Motin 2182829603e2SAlexander Motin /* Schedule iSNS update */ 2183829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) 2184829603e2SAlexander Motin set_timeout((newconf->conf_isns_period + 2) / 3, false); 2185829603e2SAlexander Motin 2186009ea47eSEdward Tomasz Napierala return (cumulated_error); 2187009ea47eSEdward Tomasz Napierala } 2188009ea47eSEdward Tomasz Napierala 2189009ea47eSEdward Tomasz Napierala bool 2190009ea47eSEdward Tomasz Napierala timed_out(void) 2191009ea47eSEdward Tomasz Napierala { 2192009ea47eSEdward Tomasz Napierala 2193009ea47eSEdward Tomasz Napierala return (sigalrm_received); 2194009ea47eSEdward Tomasz Napierala } 2195009ea47eSEdward Tomasz Napierala 2196009ea47eSEdward Tomasz Napierala static void 2197829603e2SAlexander Motin sigalrm_handler_fatal(int dummy __unused) 2198009ea47eSEdward Tomasz Napierala { 2199009ea47eSEdward Tomasz Napierala /* 2200009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 2201009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 2202009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 2203009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 2204009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 2205009ea47eSEdward Tomasz Napierala */ 2206009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 2207009ea47eSEdward Tomasz Napierala /* 2208009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 2209009ea47eSEdward Tomasz Napierala */ 2210009ea47eSEdward Tomasz Napierala _exit(2); 2211009ea47eSEdward Tomasz Napierala } 2212009ea47eSEdward Tomasz Napierala 2213009ea47eSEdward Tomasz Napierala sigalrm_received = true; 2214009ea47eSEdward Tomasz Napierala } 2215009ea47eSEdward Tomasz Napierala 2216009ea47eSEdward Tomasz Napierala static void 2217829603e2SAlexander Motin sigalrm_handler(int dummy __unused) 2218829603e2SAlexander Motin { 2219829603e2SAlexander Motin 2220829603e2SAlexander Motin sigalrm_received = true; 2221829603e2SAlexander Motin } 2222829603e2SAlexander Motin 2223829603e2SAlexander Motin void 2224829603e2SAlexander Motin set_timeout(int timeout, int fatal) 2225009ea47eSEdward Tomasz Napierala { 2226009ea47eSEdward Tomasz Napierala struct sigaction sa; 2227009ea47eSEdward Tomasz Napierala struct itimerval itv; 2228009ea47eSEdward Tomasz Napierala int error; 2229009ea47eSEdward Tomasz Napierala 2230829603e2SAlexander Motin if (timeout <= 0) { 2231009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 2232829603e2SAlexander Motin bzero(&itv, sizeof(itv)); 2233829603e2SAlexander Motin error = setitimer(ITIMER_REAL, &itv, NULL); 2234829603e2SAlexander Motin if (error != 0) 2235829603e2SAlexander Motin log_err(1, "setitimer"); 2236829603e2SAlexander Motin sigalrm_received = false; 2237009ea47eSEdward Tomasz Napierala return; 2238009ea47eSEdward Tomasz Napierala } 2239009ea47eSEdward Tomasz Napierala 2240829603e2SAlexander Motin sigalrm_received = false; 2241009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 2242829603e2SAlexander Motin if (fatal) 2243829603e2SAlexander Motin sa.sa_handler = sigalrm_handler_fatal; 2244829603e2SAlexander Motin else 2245009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 2246009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 2247009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 2248009ea47eSEdward Tomasz Napierala if (error != 0) 2249009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2250009ea47eSEdward Tomasz Napierala 2251009ea47eSEdward Tomasz Napierala /* 2252009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 2253009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 2254009ea47eSEdward Tomasz Napierala */ 2255829603e2SAlexander Motin log_debugx("setting session timeout to %d seconds", timeout); 2256009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 2257009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 2258829603e2SAlexander Motin itv.it_value.tv_sec = timeout; 2259009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 2260009ea47eSEdward Tomasz Napierala if (error != 0) 2261009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 2262009ea47eSEdward Tomasz Napierala } 2263009ea47eSEdward Tomasz Napierala 2264009ea47eSEdward Tomasz Napierala static int 2265009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 2266009ea47eSEdward Tomasz Napierala { 2267009ea47eSEdward Tomasz Napierala pid_t pid; 2268009ea47eSEdward Tomasz Napierala int status; 2269009ea47eSEdward Tomasz Napierala int num = 0; 2270009ea47eSEdward Tomasz Napierala 2271009ea47eSEdward Tomasz Napierala for (;;) { 2272009ea47eSEdward Tomasz Napierala /* 2273009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 2274009ea47eSEdward Tomasz Napierala */ 2275009ea47eSEdward Tomasz Napierala if (block && num == 0) 2276009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 2277009ea47eSEdward Tomasz Napierala else 2278009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 2279009ea47eSEdward Tomasz Napierala if (pid <= 0) 2280009ea47eSEdward Tomasz Napierala break; 2281009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 2282009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 2283009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 2284009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 2285009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 2286009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 2287009ea47eSEdward Tomasz Napierala } else { 2288009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 2289009ea47eSEdward Tomasz Napierala } 2290009ea47eSEdward Tomasz Napierala num++; 2291009ea47eSEdward Tomasz Napierala } 2292009ea47eSEdward Tomasz Napierala 2293009ea47eSEdward Tomasz Napierala return (num); 2294009ea47eSEdward Tomasz Napierala } 2295009ea47eSEdward Tomasz Napierala 2296009ea47eSEdward Tomasz Napierala static void 22978eab95d6SEdward Tomasz Napierala handle_connection(struct portal *portal, int fd, 2298073edb1cSAlexander Motin const struct sockaddr *client_sa, bool dont_fork) 2299009ea47eSEdward Tomasz Napierala { 2300009ea47eSEdward Tomasz Napierala struct connection *conn; 2301009ea47eSEdward Tomasz Napierala int error; 2302009ea47eSEdward Tomasz Napierala pid_t pid; 2303009ea47eSEdward Tomasz Napierala char host[NI_MAXHOST + 1]; 2304009ea47eSEdward Tomasz Napierala struct conf *conf; 2305009ea47eSEdward Tomasz Napierala 2306009ea47eSEdward Tomasz Napierala conf = portal->p_portal_group->pg_conf; 2307009ea47eSEdward Tomasz Napierala 2308009ea47eSEdward Tomasz Napierala if (dont_fork) { 2309009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; not forking due to -d flag"); 2310009ea47eSEdward Tomasz Napierala } else { 2311009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 2312009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2313009ea47eSEdward Tomasz Napierala 2314009ea47eSEdward Tomasz Napierala while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 2315009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 2316009ea47eSEdward Tomasz Napierala "waiting for child process to exit", conf->conf_maxproc); 2317009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 2318009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2319009ea47eSEdward Tomasz Napierala } 2320009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 2321009ea47eSEdward Tomasz Napierala nchildren); 2322009ea47eSEdward Tomasz Napierala nchildren++; 2323009ea47eSEdward Tomasz Napierala pid = fork(); 2324009ea47eSEdward Tomasz Napierala if (pid < 0) 2325009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 2326009ea47eSEdward Tomasz Napierala if (pid > 0) { 2327009ea47eSEdward Tomasz Napierala close(fd); 2328009ea47eSEdward Tomasz Napierala return; 2329009ea47eSEdward Tomasz Napierala } 2330009ea47eSEdward Tomasz Napierala } 2331009ea47eSEdward Tomasz Napierala pidfile_close(conf->conf_pidfh); 2332009ea47eSEdward Tomasz Napierala 2333073edb1cSAlexander Motin error = getnameinfo(client_sa, client_sa->sa_len, 2334009ea47eSEdward Tomasz Napierala host, sizeof(host), NULL, 0, NI_NUMERICHOST); 2335009ea47eSEdward Tomasz Napierala if (error != 0) 23369a95cfffSEdward Tomasz Napierala log_errx(1, "getnameinfo: %s", gai_strerror(error)); 2337009ea47eSEdward Tomasz Napierala 2338009ea47eSEdward Tomasz Napierala log_debugx("accepted connection from %s; portal group \"%s\"", 2339009ea47eSEdward Tomasz Napierala host, portal->p_portal_group->pg_name); 2340009ea47eSEdward Tomasz Napierala log_set_peer_addr(host); 2341009ea47eSEdward Tomasz Napierala setproctitle("%s", host); 2342009ea47eSEdward Tomasz Napierala 2343073edb1cSAlexander Motin conn = connection_new(portal, fd, host, client_sa); 2344829603e2SAlexander Motin set_timeout(conf->conf_timeout, true); 2345009ea47eSEdward Tomasz Napierala kernel_capsicate(); 2346009ea47eSEdward Tomasz Napierala login(conn); 2347009ea47eSEdward Tomasz Napierala if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 2348009ea47eSEdward Tomasz Napierala kernel_handoff(conn); 2349009ea47eSEdward Tomasz Napierala log_debugx("connection handed off to the kernel"); 2350009ea47eSEdward Tomasz Napierala } else { 2351009ea47eSEdward Tomasz Napierala assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 2352009ea47eSEdward Tomasz Napierala discovery(conn); 2353009ea47eSEdward Tomasz Napierala } 2354009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 2355009ea47eSEdward Tomasz Napierala exit(0); 2356009ea47eSEdward Tomasz Napierala } 2357009ea47eSEdward Tomasz Napierala 2358009ea47eSEdward Tomasz Napierala static int 2359009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds) 2360009ea47eSEdward Tomasz Napierala { 2361009ea47eSEdward Tomasz Napierala 2362009ea47eSEdward Tomasz Napierala /* 2363009ea47eSEdward Tomasz Napierala * Skip sockets which we failed to bind. 2364009ea47eSEdward Tomasz Napierala */ 2365009ea47eSEdward Tomasz Napierala if (fd <= 0) 2366009ea47eSEdward Tomasz Napierala return (nfds); 2367009ea47eSEdward Tomasz Napierala 2368009ea47eSEdward Tomasz Napierala FD_SET(fd, fdset); 2369009ea47eSEdward Tomasz Napierala if (fd > nfds) 2370009ea47eSEdward Tomasz Napierala nfds = fd; 2371009ea47eSEdward Tomasz Napierala return (nfds); 2372009ea47eSEdward Tomasz Napierala } 2373009ea47eSEdward Tomasz Napierala 2374009ea47eSEdward Tomasz Napierala static void 2375009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork) 2376009ea47eSEdward Tomasz Napierala { 2377009ea47eSEdward Tomasz Napierala struct portal_group *pg; 2378009ea47eSEdward Tomasz Napierala struct portal *portal; 23799a95cfffSEdward Tomasz Napierala struct sockaddr_storage client_sa; 23809a95cfffSEdward Tomasz Napierala socklen_t client_salen; 2381009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2382009ea47eSEdward Tomasz Napierala int connection_id; 23838cab2ed4SEdward Tomasz Napierala int portal_id; 2384ba3a2d31SEdward Tomasz Napierala #endif 2385009ea47eSEdward Tomasz Napierala fd_set fdset; 2386009ea47eSEdward Tomasz Napierala int error, nfds, client_fd; 2387009ea47eSEdward Tomasz Napierala 2388009ea47eSEdward Tomasz Napierala pidfile_write(conf->conf_pidfh); 2389009ea47eSEdward Tomasz Napierala 2390009ea47eSEdward Tomasz Napierala for (;;) { 2391829603e2SAlexander Motin if (sighup_received || sigterm_received || timed_out()) 2392009ea47eSEdward Tomasz Napierala return; 2393009ea47eSEdward Tomasz Napierala 2394009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2395ba3a2d31SEdward Tomasz Napierala if (proxy_mode) { 23968eab95d6SEdward Tomasz Napierala client_salen = sizeof(client_sa); 23978eab95d6SEdward Tomasz Napierala kernel_accept(&connection_id, &portal_id, 23988eab95d6SEdward Tomasz Napierala (struct sockaddr *)&client_sa, &client_salen); 23990488e848SEdward Tomasz Napierala assert(client_salen >= client_sa.ss_len); 2400009ea47eSEdward Tomasz Napierala 24018cab2ed4SEdward Tomasz Napierala log_debugx("incoming connection, id %d, portal id %d", 24028cab2ed4SEdward Tomasz Napierala connection_id, portal_id); 24038cab2ed4SEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 24048cab2ed4SEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 24058cab2ed4SEdward Tomasz Napierala if (portal->p_id == portal_id) { 24068cab2ed4SEdward Tomasz Napierala goto found; 24078cab2ed4SEdward Tomasz Napierala } 24088cab2ed4SEdward Tomasz Napierala } 24098cab2ed4SEdward Tomasz Napierala } 2410009ea47eSEdward Tomasz Napierala 24118cab2ed4SEdward Tomasz Napierala log_errx(1, "kernel returned invalid portal_id %d", 24128cab2ed4SEdward Tomasz Napierala portal_id); 24138cab2ed4SEdward Tomasz Napierala 24148cab2ed4SEdward Tomasz Napierala found: 24158eab95d6SEdward Tomasz Napierala handle_connection(portal, connection_id, 2416073edb1cSAlexander Motin (struct sockaddr *)&client_sa, dont_fork); 2417ba3a2d31SEdward Tomasz Napierala } else { 2418ba3a2d31SEdward Tomasz Napierala #endif 2419ba3a2d31SEdward Tomasz Napierala assert(proxy_mode == false); 2420ba3a2d31SEdward Tomasz Napierala 2421009ea47eSEdward Tomasz Napierala FD_ZERO(&fdset); 2422009ea47eSEdward Tomasz Napierala nfds = 0; 2423009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2424009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 2425009ea47eSEdward Tomasz Napierala nfds = fd_add(portal->p_socket, &fdset, nfds); 2426009ea47eSEdward Tomasz Napierala } 2427009ea47eSEdward Tomasz Napierala error = select(nfds + 1, &fdset, NULL, NULL, NULL); 2428009ea47eSEdward Tomasz Napierala if (error <= 0) { 2429009ea47eSEdward Tomasz Napierala if (errno == EINTR) 2430009ea47eSEdward Tomasz Napierala return; 2431009ea47eSEdward Tomasz Napierala log_err(1, "select"); 2432009ea47eSEdward Tomasz Napierala } 2433009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2434009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2435009ea47eSEdward Tomasz Napierala if (!FD_ISSET(portal->p_socket, &fdset)) 2436009ea47eSEdward Tomasz Napierala continue; 24379a95cfffSEdward Tomasz Napierala client_salen = sizeof(client_sa); 24389a95cfffSEdward Tomasz Napierala client_fd = accept(portal->p_socket, 24399a95cfffSEdward Tomasz Napierala (struct sockaddr *)&client_sa, 24409a95cfffSEdward Tomasz Napierala &client_salen); 244183dd77c3SAlexander Motin if (client_fd < 0) { 244283dd77c3SAlexander Motin if (errno == ECONNABORTED) 244383dd77c3SAlexander Motin continue; 2444009ea47eSEdward Tomasz Napierala log_err(1, "accept"); 244583dd77c3SAlexander Motin } 24460488e848SEdward Tomasz Napierala assert(client_salen >= client_sa.ss_len); 24470488e848SEdward Tomasz Napierala 24489a95cfffSEdward Tomasz Napierala handle_connection(portal, client_fd, 24498eab95d6SEdward Tomasz Napierala (struct sockaddr *)&client_sa, 2450073edb1cSAlexander Motin dont_fork); 2451009ea47eSEdward Tomasz Napierala break; 2452009ea47eSEdward Tomasz Napierala } 2453009ea47eSEdward Tomasz Napierala } 2454ba3a2d31SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 2455ba3a2d31SEdward Tomasz Napierala } 2456ba3a2d31SEdward Tomasz Napierala #endif 2457009ea47eSEdward Tomasz Napierala } 2458009ea47eSEdward Tomasz Napierala } 2459009ea47eSEdward Tomasz Napierala 2460009ea47eSEdward Tomasz Napierala static void 2461009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused) 2462009ea47eSEdward Tomasz Napierala { 2463009ea47eSEdward Tomasz Napierala 2464009ea47eSEdward Tomasz Napierala sighup_received = true; 2465009ea47eSEdward Tomasz Napierala } 2466009ea47eSEdward Tomasz Napierala 2467009ea47eSEdward Tomasz Napierala static void 2468009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused) 2469009ea47eSEdward Tomasz Napierala { 2470009ea47eSEdward Tomasz Napierala 2471009ea47eSEdward Tomasz Napierala sigterm_received = true; 2472009ea47eSEdward Tomasz Napierala } 2473009ea47eSEdward Tomasz Napierala 2474009ea47eSEdward Tomasz Napierala static void 247570b939ecSEdward Tomasz Napierala sigchld_handler(int dummy __unused) 247670b939ecSEdward Tomasz Napierala { 247770b939ecSEdward Tomasz Napierala 247870b939ecSEdward Tomasz Napierala /* 247970b939ecSEdward Tomasz Napierala * The only purpose of this handler is to make SIGCHLD 248070b939ecSEdward Tomasz Napierala * interrupt the ISCSIDWAIT ioctl(2), so we can call 248170b939ecSEdward Tomasz Napierala * wait_for_children(). 248270b939ecSEdward Tomasz Napierala */ 248370b939ecSEdward Tomasz Napierala } 248470b939ecSEdward Tomasz Napierala 248570b939ecSEdward Tomasz Napierala static void 2486009ea47eSEdward Tomasz Napierala register_signals(void) 2487009ea47eSEdward Tomasz Napierala { 2488009ea47eSEdward Tomasz Napierala struct sigaction sa; 2489009ea47eSEdward Tomasz Napierala int error; 2490009ea47eSEdward Tomasz Napierala 2491009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 2492009ea47eSEdward Tomasz Napierala sa.sa_handler = sighup_handler; 2493009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 2494009ea47eSEdward Tomasz Napierala error = sigaction(SIGHUP, &sa, NULL); 2495009ea47eSEdward Tomasz Napierala if (error != 0) 2496009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2497009ea47eSEdward Tomasz Napierala 2498009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 2499009ea47eSEdward Tomasz Napierala error = sigaction(SIGTERM, &sa, NULL); 2500009ea47eSEdward Tomasz Napierala if (error != 0) 2501009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 2502009ea47eSEdward Tomasz Napierala 2503009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 2504009ea47eSEdward Tomasz Napierala error = sigaction(SIGINT, &sa, NULL); 2505009ea47eSEdward Tomasz Napierala if (error != 0) 2506009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 250770b939ecSEdward Tomasz Napierala 250870b939ecSEdward Tomasz Napierala sa.sa_handler = sigchld_handler; 250970b939ecSEdward Tomasz Napierala error = sigaction(SIGCHLD, &sa, NULL); 251070b939ecSEdward Tomasz Napierala if (error != 0) 251170b939ecSEdward Tomasz Napierala log_err(1, "sigaction"); 2512009ea47eSEdward Tomasz Napierala } 2513009ea47eSEdward Tomasz Napierala 25145c734b04SJakub Wojciech Klama static void 25155c734b04SJakub Wojciech Klama check_perms(const char *path) 25165c734b04SJakub Wojciech Klama { 25175c734b04SJakub Wojciech Klama struct stat sb; 25185c734b04SJakub Wojciech Klama int error; 25195c734b04SJakub Wojciech Klama 25205c734b04SJakub Wojciech Klama error = stat(path, &sb); 25215c734b04SJakub Wojciech Klama if (error != 0) { 25225c734b04SJakub Wojciech Klama log_warn("stat"); 25235c734b04SJakub Wojciech Klama return; 25245c734b04SJakub Wojciech Klama } 25255c734b04SJakub Wojciech Klama if (sb.st_mode & S_IWOTH) { 25265c734b04SJakub Wojciech Klama log_warnx("%s is world-writable", path); 25275c734b04SJakub Wojciech Klama } else if (sb.st_mode & S_IROTH) { 25285c734b04SJakub Wojciech Klama log_warnx("%s is world-readable", path); 25295c734b04SJakub Wojciech Klama } else if (sb.st_mode & S_IXOTH) { 25305c734b04SJakub Wojciech Klama /* 25315c734b04SJakub Wojciech Klama * Ok, this one doesn't matter, but still do it, 25325c734b04SJakub Wojciech Klama * just for consistency. 25335c734b04SJakub Wojciech Klama */ 25345c734b04SJakub Wojciech Klama log_warnx("%s is world-executable", path); 25355c734b04SJakub Wojciech Klama } 25365c734b04SJakub Wojciech Klama 25375c734b04SJakub Wojciech Klama /* 25385c734b04SJakub Wojciech Klama * XXX: Should we also check for owner != 0? 25395c734b04SJakub Wojciech Klama */ 25405c734b04SJakub Wojciech Klama } 25415c734b04SJakub Wojciech Klama 25425c734b04SJakub Wojciech Klama static struct conf * 25435c734b04SJakub Wojciech Klama conf_new_from_file(const char *path, struct conf *oldconf, bool ucl) 25445c734b04SJakub Wojciech Klama { 25455c734b04SJakub Wojciech Klama struct conf *conf; 25465c734b04SJakub Wojciech Klama struct auth_group *ag; 25475c734b04SJakub Wojciech Klama struct portal_group *pg; 25485c734b04SJakub Wojciech Klama struct pport *pp; 25495c734b04SJakub Wojciech Klama int error; 25505c734b04SJakub Wojciech Klama 25515c734b04SJakub Wojciech Klama log_debugx("obtaining configuration from %s", path); 25525c734b04SJakub Wojciech Klama 25535c734b04SJakub Wojciech Klama conf = conf_new(); 25545c734b04SJakub Wojciech Klama 25555c734b04SJakub Wojciech Klama TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next) 25565c734b04SJakub Wojciech Klama pport_copy(pp, conf); 25575c734b04SJakub Wojciech Klama 25585c734b04SJakub Wojciech Klama ag = auth_group_new(conf, "default"); 25595c734b04SJakub Wojciech Klama assert(ag != NULL); 25605c734b04SJakub Wojciech Klama 25615c734b04SJakub Wojciech Klama ag = auth_group_new(conf, "no-authentication"); 25625c734b04SJakub Wojciech Klama assert(ag != NULL); 25635c734b04SJakub Wojciech Klama ag->ag_type = AG_TYPE_NO_AUTHENTICATION; 25645c734b04SJakub Wojciech Klama 25655c734b04SJakub Wojciech Klama ag = auth_group_new(conf, "no-access"); 25665c734b04SJakub Wojciech Klama assert(ag != NULL); 25675c734b04SJakub Wojciech Klama ag->ag_type = AG_TYPE_DENY; 25685c734b04SJakub Wojciech Klama 25695c734b04SJakub Wojciech Klama pg = portal_group_new(conf, "default"); 25705c734b04SJakub Wojciech Klama assert(pg != NULL); 25715c734b04SJakub Wojciech Klama 25725c734b04SJakub Wojciech Klama if (ucl) 25735c734b04SJakub Wojciech Klama error = uclparse_conf(conf, path); 25745c734b04SJakub Wojciech Klama else 25755c734b04SJakub Wojciech Klama error = parse_conf(conf, path); 25765c734b04SJakub Wojciech Klama 25775c734b04SJakub Wojciech Klama if (error != 0) { 25785c734b04SJakub Wojciech Klama conf_delete(conf); 25795c734b04SJakub Wojciech Klama return (NULL); 25805c734b04SJakub Wojciech Klama } 25815c734b04SJakub Wojciech Klama 25825c734b04SJakub Wojciech Klama check_perms(path); 25835c734b04SJakub Wojciech Klama 25845c734b04SJakub Wojciech Klama if (conf->conf_default_ag_defined == false) { 25855c734b04SJakub Wojciech Klama log_debugx("auth-group \"default\" not defined; " 25865c734b04SJakub Wojciech Klama "going with defaults"); 25875c734b04SJakub Wojciech Klama ag = auth_group_find(conf, "default"); 25885c734b04SJakub Wojciech Klama assert(ag != NULL); 25895c734b04SJakub Wojciech Klama ag->ag_type = AG_TYPE_DENY; 25905c734b04SJakub Wojciech Klama } 25915c734b04SJakub Wojciech Klama 25925c734b04SJakub Wojciech Klama if (conf->conf_default_pg_defined == false) { 25935c734b04SJakub Wojciech Klama log_debugx("portal-group \"default\" not defined; " 25945c734b04SJakub Wojciech Klama "going with defaults"); 25955c734b04SJakub Wojciech Klama pg = portal_group_find(conf, "default"); 25965c734b04SJakub Wojciech Klama assert(pg != NULL); 25975c734b04SJakub Wojciech Klama portal_group_add_listen(pg, "0.0.0.0:3260", false); 25985c734b04SJakub Wojciech Klama portal_group_add_listen(pg, "[::]:3260", false); 25995c734b04SJakub Wojciech Klama } 26005c734b04SJakub Wojciech Klama 26015c734b04SJakub Wojciech Klama conf->conf_kernel_port_on = true; 26025c734b04SJakub Wojciech Klama 26035c734b04SJakub Wojciech Klama error = conf_verify(conf); 26045c734b04SJakub Wojciech Klama if (error != 0) { 26055c734b04SJakub Wojciech Klama conf_delete(conf); 26065c734b04SJakub Wojciech Klama return (NULL); 26075c734b04SJakub Wojciech Klama } 26085c734b04SJakub Wojciech Klama 26095c734b04SJakub Wojciech Klama return (conf); 26105c734b04SJakub Wojciech Klama } 26115c734b04SJakub Wojciech Klama 2612009ea47eSEdward Tomasz Napierala int 2613009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 2614009ea47eSEdward Tomasz Napierala { 2615009ea47eSEdward Tomasz Napierala struct conf *oldconf, *newconf, *tmpconf; 2616829603e2SAlexander Motin struct isns *newns; 2617009ea47eSEdward Tomasz Napierala const char *config_path = DEFAULT_CONFIG_PATH; 2618009ea47eSEdward Tomasz Napierala int debug = 0, ch, error; 2619009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 26205c734b04SJakub Wojciech Klama bool use_ucl = false; 2621009ea47eSEdward Tomasz Napierala 26225c734b04SJakub Wojciech Klama while ((ch = getopt(argc, argv, "duf:R")) != -1) { 2623009ea47eSEdward Tomasz Napierala switch (ch) { 2624009ea47eSEdward Tomasz Napierala case 'd': 2625009ea47eSEdward Tomasz Napierala dont_daemonize = true; 2626009ea47eSEdward Tomasz Napierala debug++; 2627009ea47eSEdward Tomasz Napierala break; 26285c734b04SJakub Wojciech Klama case 'u': 26295c734b04SJakub Wojciech Klama use_ucl = true; 26305c734b04SJakub Wojciech Klama break; 2631009ea47eSEdward Tomasz Napierala case 'f': 2632009ea47eSEdward Tomasz Napierala config_path = optarg; 2633009ea47eSEdward Tomasz Napierala break; 2634ba3a2d31SEdward Tomasz Napierala case 'R': 2635ba3a2d31SEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 2636ba3a2d31SEdward Tomasz Napierala log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY " 2637ba3a2d31SEdward Tomasz Napierala "does not support iSER protocol"); 2638ba3a2d31SEdward Tomasz Napierala #endif 2639ba3a2d31SEdward Tomasz Napierala proxy_mode = true; 2640ba3a2d31SEdward Tomasz Napierala break; 2641009ea47eSEdward Tomasz Napierala case '?': 2642009ea47eSEdward Tomasz Napierala default: 2643009ea47eSEdward Tomasz Napierala usage(); 2644009ea47eSEdward Tomasz Napierala } 2645009ea47eSEdward Tomasz Napierala } 2646009ea47eSEdward Tomasz Napierala argc -= optind; 2647009ea47eSEdward Tomasz Napierala if (argc != 0) 2648009ea47eSEdward Tomasz Napierala usage(); 2649009ea47eSEdward Tomasz Napierala 2650009ea47eSEdward Tomasz Napierala log_init(debug); 2651009ea47eSEdward Tomasz Napierala kernel_init(); 2652009ea47eSEdward Tomasz Napierala 2653009ea47eSEdward Tomasz Napierala oldconf = conf_new_from_kernel(); 26545c734b04SJakub Wojciech Klama newconf = conf_new_from_file(config_path, oldconf, use_ucl); 26555c734b04SJakub Wojciech Klama 2656009ea47eSEdward Tomasz Napierala if (newconf == NULL) 2657fe845cdbSEdward Tomasz Napierala log_errx(1, "configuration error; exiting"); 2658009ea47eSEdward Tomasz Napierala if (debug > 0) { 2659009ea47eSEdward Tomasz Napierala oldconf->conf_debug = debug; 2660009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 2661009ea47eSEdward Tomasz Napierala } 2662009ea47eSEdward Tomasz Napierala 2663009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2664009ea47eSEdward Tomasz Napierala if (error != 0) 2665fe845cdbSEdward Tomasz Napierala log_errx(1, "failed to apply configuration; exiting"); 2666fe845cdbSEdward Tomasz Napierala 2667009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 2668009ea47eSEdward Tomasz Napierala oldconf = NULL; 2669009ea47eSEdward Tomasz Napierala 2670009ea47eSEdward Tomasz Napierala register_signals(); 2671009ea47eSEdward Tomasz Napierala 26725d5a95f1SEdward Tomasz Napierala if (dont_daemonize == false) { 26735d5a95f1SEdward Tomasz Napierala log_debugx("daemonizing"); 26745d5a95f1SEdward Tomasz Napierala if (daemon(0, 0) == -1) { 26755d5a95f1SEdward Tomasz Napierala log_warn("cannot daemonize"); 26765d5a95f1SEdward Tomasz Napierala pidfile_remove(newconf->conf_pidfh); 26775d5a95f1SEdward Tomasz Napierala exit(1); 26785d5a95f1SEdward Tomasz Napierala } 26795d5a95f1SEdward Tomasz Napierala } 26805d5a95f1SEdward Tomasz Napierala 2681829603e2SAlexander Motin /* Schedule iSNS update */ 2682829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) 2683829603e2SAlexander Motin set_timeout((newconf->conf_isns_period + 2) / 3, false); 2684829603e2SAlexander Motin 2685009ea47eSEdward Tomasz Napierala for (;;) { 2686009ea47eSEdward Tomasz Napierala main_loop(newconf, dont_daemonize); 2687009ea47eSEdward Tomasz Napierala if (sighup_received) { 2688009ea47eSEdward Tomasz Napierala sighup_received = false; 2689009ea47eSEdward Tomasz Napierala log_debugx("received SIGHUP, reloading configuration"); 26905c734b04SJakub Wojciech Klama tmpconf = conf_new_from_file(config_path, newconf, 26915c734b04SJakub Wojciech Klama use_ucl); 26925c734b04SJakub Wojciech Klama 2693009ea47eSEdward Tomasz Napierala if (tmpconf == NULL) { 2694009ea47eSEdward Tomasz Napierala log_warnx("configuration error, " 2695009ea47eSEdward Tomasz Napierala "continuing with old configuration"); 2696009ea47eSEdward Tomasz Napierala } else { 2697009ea47eSEdward Tomasz Napierala if (debug > 0) 2698009ea47eSEdward Tomasz Napierala tmpconf->conf_debug = debug; 2699009ea47eSEdward Tomasz Napierala oldconf = newconf; 2700009ea47eSEdward Tomasz Napierala newconf = tmpconf; 2701009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2702009ea47eSEdward Tomasz Napierala if (error != 0) 2703009ea47eSEdward Tomasz Napierala log_warnx("failed to reload " 2704009ea47eSEdward Tomasz Napierala "configuration"); 2705009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 2706009ea47eSEdward Tomasz Napierala oldconf = NULL; 2707009ea47eSEdward Tomasz Napierala } 2708009ea47eSEdward Tomasz Napierala } else if (sigterm_received) { 2709009ea47eSEdward Tomasz Napierala log_debugx("exiting on signal; " 2710009ea47eSEdward Tomasz Napierala "reloading empty configuration"); 2711009ea47eSEdward Tomasz Napierala 27126b31e130SAlexander Motin log_debugx("removing CTL iSCSI ports " 2713009ea47eSEdward Tomasz Napierala "and terminating all connections"); 2714009ea47eSEdward Tomasz Napierala 2715009ea47eSEdward Tomasz Napierala oldconf = newconf; 2716009ea47eSEdward Tomasz Napierala newconf = conf_new(); 2717009ea47eSEdward Tomasz Napierala if (debug > 0) 2718009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 2719009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 2720009ea47eSEdward Tomasz Napierala if (error != 0) 2721009ea47eSEdward Tomasz Napierala log_warnx("failed to apply configuration"); 2722829603e2SAlexander Motin conf_delete(oldconf); 2723829603e2SAlexander Motin oldconf = NULL; 2724009ea47eSEdward Tomasz Napierala 2725009ea47eSEdward Tomasz Napierala log_warnx("exiting on signal"); 2726009ea47eSEdward Tomasz Napierala exit(0); 2727009ea47eSEdward Tomasz Napierala } else { 2728009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 2729009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 2730829603e2SAlexander Motin if (timed_out()) { 2731829603e2SAlexander Motin set_timeout(0, false); 2732829603e2SAlexander Motin TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) 2733829603e2SAlexander Motin isns_check(newns); 2734829603e2SAlexander Motin /* Schedule iSNS update */ 2735829603e2SAlexander Motin if (!TAILQ_EMPTY(&newconf->conf_isns)) { 2736829603e2SAlexander Motin set_timeout((newconf->conf_isns_period 2737829603e2SAlexander Motin + 2) / 3, 2738829603e2SAlexander Motin false); 2739829603e2SAlexander Motin } 2740829603e2SAlexander Motin } 2741009ea47eSEdward Tomasz Napierala } 2742009ea47eSEdward Tomasz Napierala } 2743009ea47eSEdward Tomasz Napierala /* NOTREACHED */ 2744009ea47eSEdward Tomasz Napierala } 2745