1009ea47eSEdward Tomasz Napierala /*- 2009ea47eSEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 3009ea47eSEdward Tomasz Napierala * All rights reserved. 4009ea47eSEdward Tomasz Napierala * 5009ea47eSEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship 6009ea47eSEdward Tomasz Napierala * from the FreeBSD Foundation. 7009ea47eSEdward Tomasz Napierala * 8009ea47eSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without 9009ea47eSEdward Tomasz Napierala * modification, are permitted provided that the following conditions 10009ea47eSEdward Tomasz Napierala * are met: 11009ea47eSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright 12009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer. 13009ea47eSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright 14009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the 15009ea47eSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution. 16009ea47eSEdward Tomasz Napierala * 17009ea47eSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18009ea47eSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19009ea47eSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20009ea47eSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21009ea47eSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22009ea47eSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23009ea47eSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24009ea47eSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25009ea47eSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26009ea47eSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27009ea47eSEdward Tomasz Napierala * SUCH DAMAGE. 28009ea47eSEdward Tomasz Napierala * 29009ea47eSEdward Tomasz Napierala * $FreeBSD$ 30009ea47eSEdward Tomasz Napierala */ 31009ea47eSEdward Tomasz Napierala 32009ea47eSEdward Tomasz Napierala #include <sys/types.h> 33009ea47eSEdward Tomasz Napierala #include <sys/time.h> 34009ea47eSEdward Tomasz Napierala #include <sys/socket.h> 35009ea47eSEdward Tomasz Napierala #include <sys/wait.h> 36009ea47eSEdward Tomasz Napierala #include <netinet/in.h> 37009ea47eSEdward Tomasz Napierala #include <assert.h> 38009ea47eSEdward Tomasz Napierala #include <ctype.h> 39009ea47eSEdward Tomasz Napierala #include <errno.h> 40009ea47eSEdward Tomasz Napierala #include <netdb.h> 41009ea47eSEdward Tomasz Napierala #include <signal.h> 42009ea47eSEdward Tomasz Napierala #include <stdbool.h> 43009ea47eSEdward Tomasz Napierala #include <stdio.h> 44009ea47eSEdward Tomasz Napierala #include <stdint.h> 45009ea47eSEdward Tomasz Napierala #include <stdlib.h> 46009ea47eSEdward Tomasz Napierala #include <string.h> 47009ea47eSEdward Tomasz Napierala #include <unistd.h> 48009ea47eSEdward Tomasz Napierala 49009ea47eSEdward Tomasz Napierala #include "ctld.h" 50009ea47eSEdward Tomasz Napierala 51009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false; 52009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false; 53009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false; 54009ea47eSEdward Tomasz Napierala 55009ea47eSEdward Tomasz Napierala static int nchildren = 0; 56009ea47eSEdward Tomasz Napierala 57009ea47eSEdward Tomasz Napierala static void 58009ea47eSEdward Tomasz Napierala usage(void) 59009ea47eSEdward Tomasz Napierala { 60009ea47eSEdward Tomasz Napierala 61009ea47eSEdward Tomasz Napierala fprintf(stderr, "usage: ctld [-d][-f config-file]\n"); 62009ea47eSEdward Tomasz Napierala exit(1); 63009ea47eSEdward Tomasz Napierala } 64009ea47eSEdward Tomasz Napierala 65009ea47eSEdward Tomasz Napierala char * 66009ea47eSEdward Tomasz Napierala checked_strdup(const char *s) 67009ea47eSEdward Tomasz Napierala { 68009ea47eSEdward Tomasz Napierala char *c; 69009ea47eSEdward Tomasz Napierala 70009ea47eSEdward Tomasz Napierala c = strdup(s); 71009ea47eSEdward Tomasz Napierala if (c == NULL) 72009ea47eSEdward Tomasz Napierala log_err(1, "strdup"); 73009ea47eSEdward Tomasz Napierala return (c); 74009ea47eSEdward Tomasz Napierala } 75009ea47eSEdward Tomasz Napierala 76009ea47eSEdward Tomasz Napierala struct conf * 77009ea47eSEdward Tomasz Napierala conf_new(void) 78009ea47eSEdward Tomasz Napierala { 79009ea47eSEdward Tomasz Napierala struct conf *conf; 80009ea47eSEdward Tomasz Napierala 81009ea47eSEdward Tomasz Napierala conf = calloc(1, sizeof(*conf)); 82009ea47eSEdward Tomasz Napierala if (conf == NULL) 83009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 84009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_targets); 85009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_auth_groups); 86009ea47eSEdward Tomasz Napierala TAILQ_INIT(&conf->conf_portal_groups); 87009ea47eSEdward Tomasz Napierala 88009ea47eSEdward Tomasz Napierala conf->conf_debug = 0; 89009ea47eSEdward Tomasz Napierala conf->conf_timeout = 60; 90009ea47eSEdward Tomasz Napierala conf->conf_maxproc = 30; 91009ea47eSEdward Tomasz Napierala 92009ea47eSEdward Tomasz Napierala return (conf); 93009ea47eSEdward Tomasz Napierala } 94009ea47eSEdward Tomasz Napierala 95009ea47eSEdward Tomasz Napierala void 96009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf) 97009ea47eSEdward Tomasz Napierala { 98009ea47eSEdward Tomasz Napierala struct target *targ, *tmp; 99009ea47eSEdward Tomasz Napierala struct auth_group *ag, *cagtmp; 100009ea47eSEdward Tomasz Napierala struct portal_group *pg, *cpgtmp; 101009ea47eSEdward Tomasz Napierala 102009ea47eSEdward Tomasz Napierala assert(conf->conf_pidfh == NULL); 103009ea47eSEdward Tomasz Napierala 104009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 105009ea47eSEdward Tomasz Napierala target_delete(targ); 106009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 107009ea47eSEdward Tomasz Napierala auth_group_delete(ag); 108009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 109009ea47eSEdward Tomasz Napierala portal_group_delete(pg); 110009ea47eSEdward Tomasz Napierala free(conf->conf_pidfile_path); 111009ea47eSEdward Tomasz Napierala free(conf); 112009ea47eSEdward Tomasz Napierala } 113009ea47eSEdward Tomasz Napierala 114009ea47eSEdward Tomasz Napierala static struct auth * 115009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag) 116009ea47eSEdward Tomasz Napierala { 117009ea47eSEdward Tomasz Napierala struct auth *auth; 118009ea47eSEdward Tomasz Napierala 119009ea47eSEdward Tomasz Napierala auth = calloc(1, sizeof(*auth)); 120009ea47eSEdward Tomasz Napierala if (auth == NULL) 121009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 122009ea47eSEdward Tomasz Napierala auth->a_auth_group = ag; 123009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); 124009ea47eSEdward Tomasz Napierala return (auth); 125009ea47eSEdward Tomasz Napierala } 126009ea47eSEdward Tomasz Napierala 127009ea47eSEdward Tomasz Napierala static void 128009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth) 129009ea47eSEdward Tomasz Napierala { 130009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); 131009ea47eSEdward Tomasz Napierala 132009ea47eSEdward Tomasz Napierala free(auth->a_user); 133009ea47eSEdward Tomasz Napierala free(auth->a_secret); 134009ea47eSEdward Tomasz Napierala free(auth->a_mutual_user); 135009ea47eSEdward Tomasz Napierala free(auth->a_mutual_secret); 136009ea47eSEdward Tomasz Napierala free(auth); 137009ea47eSEdward Tomasz Napierala } 138009ea47eSEdward Tomasz Napierala 139009ea47eSEdward Tomasz Napierala const struct auth * 140009ea47eSEdward Tomasz Napierala auth_find(struct auth_group *ag, const char *user) 141009ea47eSEdward Tomasz Napierala { 142009ea47eSEdward Tomasz Napierala const struct auth *auth; 143009ea47eSEdward Tomasz Napierala 144009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { 145009ea47eSEdward Tomasz Napierala if (strcmp(auth->a_user, user) == 0) 146009ea47eSEdward Tomasz Napierala return (auth); 147009ea47eSEdward Tomasz Napierala } 148009ea47eSEdward Tomasz Napierala 149009ea47eSEdward Tomasz Napierala return (NULL); 150009ea47eSEdward Tomasz Napierala } 151009ea47eSEdward Tomasz Napierala 152*1658628dSEdward Tomasz Napierala static void 153*1658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth) 154*1658628dSEdward Tomasz Napierala { 155*1658628dSEdward Tomasz Napierala size_t len; 156*1658628dSEdward Tomasz Napierala 157*1658628dSEdward Tomasz Napierala len = strlen(auth->a_secret); 158*1658628dSEdward Tomasz Napierala if (len > 16) { 159*1658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 160*1658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 161*1658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 162*1658628dSEdward Tomasz Napierala "long", auth->a_user, auth->a_auth_group->ag_name); 163*1658628dSEdward Tomasz Napierala else 164*1658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 165*1658628dSEdward Tomasz Napierala "is too long; it should be at most 16 characters " 166*1658628dSEdward Tomasz Napierala "long", auth->a_user, 167*1658628dSEdward Tomasz Napierala auth->a_auth_group->ag_target->t_iqn); 168*1658628dSEdward Tomasz Napierala } 169*1658628dSEdward Tomasz Napierala if (len < 12) { 170*1658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 171*1658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", auth-group \"%s\", " 172*1658628dSEdward Tomasz Napierala "is too short; it should be at least 12 characters " 173*1658628dSEdward Tomasz Napierala "long", auth->a_user, 174*1658628dSEdward Tomasz Napierala auth->a_auth_group->ag_name); 175*1658628dSEdward Tomasz Napierala else 176*1658628dSEdward Tomasz Napierala log_warnx("secret for user \"%s\", target \"%s\", " 177*1658628dSEdward Tomasz Napierala "is too short; it should be at least 16 characters " 178*1658628dSEdward Tomasz Napierala "long", auth->a_user, 179*1658628dSEdward Tomasz Napierala auth->a_auth_group->ag_target->t_iqn); 180*1658628dSEdward Tomasz Napierala } 181*1658628dSEdward Tomasz Napierala 182*1658628dSEdward Tomasz Napierala if (auth->a_mutual_secret != NULL) { 183*1658628dSEdward Tomasz Napierala len = strlen(auth->a_secret); 184*1658628dSEdward Tomasz Napierala if (len > 16) { 185*1658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 186*1658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 187*1658628dSEdward Tomasz Napierala "auth-group \"%s\", is too long; it should " 188*1658628dSEdward Tomasz Napierala "be at most 16 characters long", 189*1658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 190*1658628dSEdward Tomasz Napierala else 191*1658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 192*1658628dSEdward Tomasz Napierala "target \"%s\", is too long; it should " 193*1658628dSEdward Tomasz Napierala "be at most 16 characters long", 194*1658628dSEdward Tomasz Napierala auth->a_user, 195*1658628dSEdward Tomasz Napierala auth->a_auth_group->ag_target->t_iqn); 196*1658628dSEdward Tomasz Napierala } 197*1658628dSEdward Tomasz Napierala if (len < 12) { 198*1658628dSEdward Tomasz Napierala if (auth->a_auth_group->ag_name != NULL) 199*1658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 200*1658628dSEdward Tomasz Napierala "auth-group \"%s\", is too short; it " 201*1658628dSEdward Tomasz Napierala "should be at least 12 characters long", 202*1658628dSEdward Tomasz Napierala auth->a_user, auth->a_auth_group->ag_name); 203*1658628dSEdward Tomasz Napierala else 204*1658628dSEdward Tomasz Napierala log_warnx("mutual secret for user \"%s\", " 205*1658628dSEdward Tomasz Napierala "target \"%s\", is too short; it should be " 206*1658628dSEdward Tomasz Napierala "at least 16 characters long", 207*1658628dSEdward Tomasz Napierala auth->a_user, 208*1658628dSEdward Tomasz Napierala auth->a_auth_group->ag_target->t_iqn); 209*1658628dSEdward Tomasz Napierala } 210*1658628dSEdward Tomasz Napierala } 211*1658628dSEdward Tomasz Napierala } 212*1658628dSEdward Tomasz Napierala 213*1658628dSEdward Tomasz Napierala const struct auth * 214*1658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user, 215*1658628dSEdward Tomasz Napierala const char *secret) 216*1658628dSEdward Tomasz Napierala { 217*1658628dSEdward Tomasz Napierala struct auth *auth; 218*1658628dSEdward Tomasz Napierala 219*1658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 220*1658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP; 221*1658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP) { 222*1658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 223*1658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 224*1658628dSEdward Tomasz Napierala "other types for auth-group \"%s\"", ag->ag_name); 225*1658628dSEdward Tomasz Napierala else 226*1658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap\" authentication with " 227*1658628dSEdward Tomasz Napierala "other types for target \"%s\"", 228*1658628dSEdward Tomasz Napierala ag->ag_target->t_iqn); 229*1658628dSEdward Tomasz Napierala return (NULL); 230*1658628dSEdward Tomasz Napierala } 231*1658628dSEdward Tomasz Napierala 232*1658628dSEdward Tomasz Napierala auth = auth_new(ag); 233*1658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 234*1658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 235*1658628dSEdward Tomasz Napierala 236*1658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 237*1658628dSEdward Tomasz Napierala 238*1658628dSEdward Tomasz Napierala return (auth); 239*1658628dSEdward Tomasz Napierala } 240*1658628dSEdward Tomasz Napierala 241*1658628dSEdward Tomasz Napierala const struct auth * 242*1658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user, 243*1658628dSEdward Tomasz Napierala const char *secret, const char *user2, const char *secret2) 244*1658628dSEdward Tomasz Napierala { 245*1658628dSEdward Tomasz Napierala struct auth *auth; 246*1658628dSEdward Tomasz Napierala 247*1658628dSEdward Tomasz Napierala if (ag->ag_type == AG_TYPE_UNKNOWN) 248*1658628dSEdward Tomasz Napierala ag->ag_type = AG_TYPE_CHAP_MUTUAL; 249*1658628dSEdward Tomasz Napierala if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 250*1658628dSEdward Tomasz Napierala if (ag->ag_name != NULL) 251*1658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 252*1658628dSEdward Tomasz Napierala "with other types for auth-group \"%s\"", 253*1658628dSEdward Tomasz Napierala ag->ag_name); 254*1658628dSEdward Tomasz Napierala else 255*1658628dSEdward Tomasz Napierala log_warnx("cannot mix \"chap-mutual\" authentication " 256*1658628dSEdward Tomasz Napierala "with other types for target \"%s\"", 257*1658628dSEdward Tomasz Napierala ag->ag_target->t_iqn); 258*1658628dSEdward Tomasz Napierala return (NULL); 259*1658628dSEdward Tomasz Napierala } 260*1658628dSEdward Tomasz Napierala 261*1658628dSEdward Tomasz Napierala auth = auth_new(ag); 262*1658628dSEdward Tomasz Napierala auth->a_user = checked_strdup(user); 263*1658628dSEdward Tomasz Napierala auth->a_secret = checked_strdup(secret); 264*1658628dSEdward Tomasz Napierala auth->a_mutual_user = checked_strdup(user2); 265*1658628dSEdward Tomasz Napierala auth->a_mutual_secret = checked_strdup(secret2); 266*1658628dSEdward Tomasz Napierala 267*1658628dSEdward Tomasz Napierala auth_check_secret_length(auth); 268*1658628dSEdward Tomasz Napierala 269*1658628dSEdward Tomasz Napierala return (auth); 270*1658628dSEdward Tomasz Napierala } 271*1658628dSEdward Tomasz Napierala 2728cb2e958SEdward Tomasz Napierala const struct auth_name * 2738cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name) 2748cb2e958SEdward Tomasz Napierala { 2758cb2e958SEdward Tomasz Napierala struct auth_name *an; 2768cb2e958SEdward Tomasz Napierala 2778cb2e958SEdward Tomasz Napierala an = calloc(1, sizeof(*an)); 2788cb2e958SEdward Tomasz Napierala if (an == NULL) 2798cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 2808cb2e958SEdward Tomasz Napierala an->an_auth_group = ag; 2818cb2e958SEdward Tomasz Napierala an->an_initator_name = checked_strdup(name); 2828cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next); 2838cb2e958SEdward Tomasz Napierala return (an); 2848cb2e958SEdward Tomasz Napierala } 2858cb2e958SEdward Tomasz Napierala 2868cb2e958SEdward Tomasz Napierala static void 2878cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an) 2888cb2e958SEdward Tomasz Napierala { 2898cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next); 2908cb2e958SEdward Tomasz Napierala 2918cb2e958SEdward Tomasz Napierala free(an->an_initator_name); 2928cb2e958SEdward Tomasz Napierala free(an); 2938cb2e958SEdward Tomasz Napierala } 2948cb2e958SEdward Tomasz Napierala 2958cb2e958SEdward Tomasz Napierala bool 2968cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag) 2978cb2e958SEdward Tomasz Napierala { 2988cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_names)) 2998cb2e958SEdward Tomasz Napierala return (false); 3008cb2e958SEdward Tomasz Napierala return (true); 3018cb2e958SEdward Tomasz Napierala } 3028cb2e958SEdward Tomasz Napierala 3038cb2e958SEdward Tomasz Napierala const struct auth_name * 3048cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name) 3058cb2e958SEdward Tomasz Napierala { 3068cb2e958SEdward Tomasz Napierala const struct auth_name *auth_name; 3078cb2e958SEdward Tomasz Napierala 3088cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) { 3098cb2e958SEdward Tomasz Napierala if (strcmp(auth_name->an_initator_name, name) == 0) 3108cb2e958SEdward Tomasz Napierala return (auth_name); 3118cb2e958SEdward Tomasz Napierala } 3128cb2e958SEdward Tomasz Napierala 3138cb2e958SEdward Tomasz Napierala return (NULL); 3148cb2e958SEdward Tomasz Napierala } 3158cb2e958SEdward Tomasz Napierala 3168cb2e958SEdward Tomasz Napierala const struct auth_portal * 3178cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal) 3188cb2e958SEdward Tomasz Napierala { 3198cb2e958SEdward Tomasz Napierala struct auth_portal *ap; 3208cb2e958SEdward Tomasz Napierala 3218cb2e958SEdward Tomasz Napierala ap = calloc(1, sizeof(*ap)); 3228cb2e958SEdward Tomasz Napierala if (ap == NULL) 3238cb2e958SEdward Tomasz Napierala log_err(1, "calloc"); 3248cb2e958SEdward Tomasz Napierala ap->ap_auth_group = ag; 3258cb2e958SEdward Tomasz Napierala ap->ap_initator_portal = checked_strdup(portal); 3268cb2e958SEdward Tomasz Napierala TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next); 3278cb2e958SEdward Tomasz Napierala return (ap); 3288cb2e958SEdward Tomasz Napierala } 3298cb2e958SEdward Tomasz Napierala 3308cb2e958SEdward Tomasz Napierala static void 3318cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap) 3328cb2e958SEdward Tomasz Napierala { 3338cb2e958SEdward Tomasz Napierala TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next); 3348cb2e958SEdward Tomasz Napierala 3358cb2e958SEdward Tomasz Napierala free(ap->ap_initator_portal); 3368cb2e958SEdward Tomasz Napierala free(ap); 3378cb2e958SEdward Tomasz Napierala } 3388cb2e958SEdward Tomasz Napierala 3398cb2e958SEdward Tomasz Napierala bool 3408cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag) 3418cb2e958SEdward Tomasz Napierala { 3428cb2e958SEdward Tomasz Napierala if (TAILQ_EMPTY(&ag->ag_portals)) 3438cb2e958SEdward Tomasz Napierala return (false); 3448cb2e958SEdward Tomasz Napierala return (true); 3458cb2e958SEdward Tomasz Napierala } 3468cb2e958SEdward Tomasz Napierala 3478cb2e958SEdward Tomasz Napierala const struct auth_portal * 3488cb2e958SEdward Tomasz Napierala auth_portal_find(const struct auth_group *ag, const char *portal) 3498cb2e958SEdward Tomasz Napierala { 3508cb2e958SEdward Tomasz Napierala const struct auth_portal *auth_portal; 3518cb2e958SEdward Tomasz Napierala 3528cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next) { 3538cb2e958SEdward Tomasz Napierala if (strcmp(auth_portal->ap_initator_portal, portal) == 0) 3548cb2e958SEdward Tomasz Napierala return (auth_portal); 3558cb2e958SEdward Tomasz Napierala } 3568cb2e958SEdward Tomasz Napierala 3578cb2e958SEdward Tomasz Napierala return (NULL); 3588cb2e958SEdward Tomasz Napierala } 3598cb2e958SEdward Tomasz Napierala 360009ea47eSEdward Tomasz Napierala struct auth_group * 361009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name) 362009ea47eSEdward Tomasz Napierala { 363009ea47eSEdward Tomasz Napierala struct auth_group *ag; 364009ea47eSEdward Tomasz Napierala 365009ea47eSEdward Tomasz Napierala if (name != NULL) { 366009ea47eSEdward Tomasz Napierala ag = auth_group_find(conf, name); 367009ea47eSEdward Tomasz Napierala if (ag != NULL) { 368009ea47eSEdward Tomasz Napierala log_warnx("duplicated auth-group \"%s\"", name); 369009ea47eSEdward Tomasz Napierala return (NULL); 370009ea47eSEdward Tomasz Napierala } 371009ea47eSEdward Tomasz Napierala } 372009ea47eSEdward Tomasz Napierala 373009ea47eSEdward Tomasz Napierala ag = calloc(1, sizeof(*ag)); 374009ea47eSEdward Tomasz Napierala if (ag == NULL) 375009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 376009ea47eSEdward Tomasz Napierala if (name != NULL) 377009ea47eSEdward Tomasz Napierala ag->ag_name = checked_strdup(name); 378009ea47eSEdward Tomasz Napierala TAILQ_INIT(&ag->ag_auths); 3798cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_names); 3808cb2e958SEdward Tomasz Napierala TAILQ_INIT(&ag->ag_portals); 381009ea47eSEdward Tomasz Napierala ag->ag_conf = conf; 382009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next); 383009ea47eSEdward Tomasz Napierala 384009ea47eSEdward Tomasz Napierala return (ag); 385009ea47eSEdward Tomasz Napierala } 386009ea47eSEdward Tomasz Napierala 387009ea47eSEdward Tomasz Napierala void 388009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag) 389009ea47eSEdward Tomasz Napierala { 3908cb2e958SEdward Tomasz Napierala struct auth *auth, *auth_tmp; 3918cb2e958SEdward Tomasz Napierala struct auth_name *auth_name, *auth_name_tmp; 3928cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal, *auth_portal_tmp; 393009ea47eSEdward Tomasz Napierala 394009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); 395009ea47eSEdward Tomasz Napierala 3968cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) 397009ea47eSEdward Tomasz Napierala auth_delete(auth); 3988cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) 3998cb2e958SEdward Tomasz Napierala auth_name_delete(auth_name); 4008cb2e958SEdward Tomasz Napierala TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, 4018cb2e958SEdward Tomasz Napierala auth_portal_tmp) 4028cb2e958SEdward Tomasz Napierala auth_portal_delete(auth_portal); 403009ea47eSEdward Tomasz Napierala free(ag->ag_name); 404009ea47eSEdward Tomasz Napierala free(ag); 405009ea47eSEdward Tomasz Napierala } 406009ea47eSEdward Tomasz Napierala 407009ea47eSEdward Tomasz Napierala struct auth_group * 408009ea47eSEdward Tomasz Napierala auth_group_find(struct conf *conf, const char *name) 409009ea47eSEdward Tomasz Napierala { 410009ea47eSEdward Tomasz Napierala struct auth_group *ag; 411009ea47eSEdward Tomasz Napierala 412009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 413009ea47eSEdward Tomasz Napierala if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 414009ea47eSEdward Tomasz Napierala return (ag); 415009ea47eSEdward Tomasz Napierala } 416009ea47eSEdward Tomasz Napierala 417009ea47eSEdward Tomasz Napierala return (NULL); 418009ea47eSEdward Tomasz Napierala } 419009ea47eSEdward Tomasz Napierala 420009ea47eSEdward Tomasz Napierala static struct portal * 421009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg) 422009ea47eSEdward Tomasz Napierala { 423009ea47eSEdward Tomasz Napierala struct portal *portal; 424009ea47eSEdward Tomasz Napierala 425009ea47eSEdward Tomasz Napierala portal = calloc(1, sizeof(*portal)); 426009ea47eSEdward Tomasz Napierala if (portal == NULL) 427009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 428009ea47eSEdward Tomasz Napierala TAILQ_INIT(&portal->p_targets); 429009ea47eSEdward Tomasz Napierala portal->p_portal_group = pg; 430009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 431009ea47eSEdward Tomasz Napierala return (portal); 432009ea47eSEdward Tomasz Napierala } 433009ea47eSEdward Tomasz Napierala 434009ea47eSEdward Tomasz Napierala static void 435009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal) 436009ea47eSEdward Tomasz Napierala { 437009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 438009ea47eSEdward Tomasz Napierala freeaddrinfo(portal->p_ai); 439009ea47eSEdward Tomasz Napierala free(portal->p_listen); 440009ea47eSEdward Tomasz Napierala free(portal); 441009ea47eSEdward Tomasz Napierala } 442009ea47eSEdward Tomasz Napierala 443009ea47eSEdward Tomasz Napierala struct portal_group * 444009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name) 445009ea47eSEdward Tomasz Napierala { 446009ea47eSEdward Tomasz Napierala struct portal_group *pg; 447009ea47eSEdward Tomasz Napierala 448009ea47eSEdward Tomasz Napierala pg = portal_group_find(conf, name); 449009ea47eSEdward Tomasz Napierala if (pg != NULL) { 450009ea47eSEdward Tomasz Napierala log_warnx("duplicated portal-group \"%s\"", name); 451009ea47eSEdward Tomasz Napierala return (NULL); 452009ea47eSEdward Tomasz Napierala } 453009ea47eSEdward Tomasz Napierala 454009ea47eSEdward Tomasz Napierala pg = calloc(1, sizeof(*pg)); 455009ea47eSEdward Tomasz Napierala if (pg == NULL) 456009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 457009ea47eSEdward Tomasz Napierala pg->pg_name = checked_strdup(name); 458009ea47eSEdward Tomasz Napierala TAILQ_INIT(&pg->pg_portals); 459009ea47eSEdward Tomasz Napierala pg->pg_conf = conf; 460009ea47eSEdward Tomasz Napierala conf->conf_last_portal_group_tag++; 461009ea47eSEdward Tomasz Napierala pg->pg_tag = conf->conf_last_portal_group_tag; 462009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 463009ea47eSEdward Tomasz Napierala 464009ea47eSEdward Tomasz Napierala return (pg); 465009ea47eSEdward Tomasz Napierala } 466009ea47eSEdward Tomasz Napierala 467009ea47eSEdward Tomasz Napierala void 468009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg) 469009ea47eSEdward Tomasz Napierala { 470009ea47eSEdward Tomasz Napierala struct portal *portal, *tmp; 471009ea47eSEdward Tomasz Napierala 472009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 473009ea47eSEdward Tomasz Napierala 474009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 475009ea47eSEdward Tomasz Napierala portal_delete(portal); 476009ea47eSEdward Tomasz Napierala free(pg->pg_name); 477009ea47eSEdward Tomasz Napierala free(pg); 478009ea47eSEdward Tomasz Napierala } 479009ea47eSEdward Tomasz Napierala 480009ea47eSEdward Tomasz Napierala struct portal_group * 481009ea47eSEdward Tomasz Napierala portal_group_find(struct conf *conf, const char *name) 482009ea47eSEdward Tomasz Napierala { 483009ea47eSEdward Tomasz Napierala struct portal_group *pg; 484009ea47eSEdward Tomasz Napierala 485009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 486009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, name) == 0) 487009ea47eSEdward Tomasz Napierala return (pg); 488009ea47eSEdward Tomasz Napierala } 489009ea47eSEdward Tomasz Napierala 490009ea47eSEdward Tomasz Napierala return (NULL); 491009ea47eSEdward Tomasz Napierala } 492009ea47eSEdward Tomasz Napierala 493009ea47eSEdward Tomasz Napierala int 494009ea47eSEdward Tomasz Napierala portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 495009ea47eSEdward Tomasz Napierala { 496009ea47eSEdward Tomasz Napierala struct addrinfo hints; 497009ea47eSEdward Tomasz Napierala struct portal *portal; 498009ea47eSEdward Tomasz Napierala char *addr, *ch, *arg; 499009ea47eSEdward Tomasz Napierala const char *port; 500009ea47eSEdward Tomasz Napierala int error, colons = 0; 501009ea47eSEdward Tomasz Napierala 502009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 503009ea47eSEdward Tomasz Napierala if (iser) { 504009ea47eSEdward Tomasz Napierala log_warnx("ctld(8) compiled without ICL_KERNEL_PROXY " 505009ea47eSEdward Tomasz Napierala "does not support iSER protocol"); 506009ea47eSEdward Tomasz Napierala return (-1); 507009ea47eSEdward Tomasz Napierala } 508009ea47eSEdward Tomasz Napierala #endif 509009ea47eSEdward Tomasz Napierala 510009ea47eSEdward Tomasz Napierala portal = portal_new(pg); 511009ea47eSEdward Tomasz Napierala portal->p_listen = checked_strdup(value); 512009ea47eSEdward Tomasz Napierala portal->p_iser = iser; 513009ea47eSEdward Tomasz Napierala 514009ea47eSEdward Tomasz Napierala arg = portal->p_listen; 515009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 516009ea47eSEdward Tomasz Napierala log_warnx("empty listen address"); 517009ea47eSEdward Tomasz Napierala free(portal->p_listen); 518009ea47eSEdward Tomasz Napierala free(portal); 519009ea47eSEdward Tomasz Napierala return (1); 520009ea47eSEdward Tomasz Napierala } 521009ea47eSEdward Tomasz Napierala if (arg[0] == '[') { 522009ea47eSEdward Tomasz Napierala /* 523009ea47eSEdward Tomasz Napierala * IPv6 address in square brackets, perhaps with port. 524009ea47eSEdward Tomasz Napierala */ 525009ea47eSEdward Tomasz Napierala arg++; 526009ea47eSEdward Tomasz Napierala addr = strsep(&arg, "]"); 527009ea47eSEdward Tomasz Napierala if (arg == NULL) { 528009ea47eSEdward Tomasz Napierala log_warnx("invalid listen address %s", 529009ea47eSEdward Tomasz Napierala portal->p_listen); 530009ea47eSEdward Tomasz Napierala free(portal->p_listen); 531009ea47eSEdward Tomasz Napierala free(portal); 532009ea47eSEdward Tomasz Napierala return (1); 533009ea47eSEdward Tomasz Napierala } 534009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 535009ea47eSEdward Tomasz Napierala port = "3260"; 536009ea47eSEdward Tomasz Napierala } else if (arg[0] == ':') { 537009ea47eSEdward Tomasz Napierala port = arg + 1; 538009ea47eSEdward Tomasz Napierala } else { 539009ea47eSEdward Tomasz Napierala log_warnx("invalid listen address %s", 540009ea47eSEdward Tomasz Napierala portal->p_listen); 541009ea47eSEdward Tomasz Napierala free(portal->p_listen); 542009ea47eSEdward Tomasz Napierala free(portal); 543009ea47eSEdward Tomasz Napierala return (1); 544009ea47eSEdward Tomasz Napierala } 545009ea47eSEdward Tomasz Napierala } else { 546009ea47eSEdward Tomasz Napierala /* 547009ea47eSEdward Tomasz Napierala * Either IPv6 address without brackets - and without 548009ea47eSEdward Tomasz Napierala * a port - or IPv4 address. Just count the colons. 549009ea47eSEdward Tomasz Napierala */ 550009ea47eSEdward Tomasz Napierala for (ch = arg; *ch != '\0'; ch++) { 551009ea47eSEdward Tomasz Napierala if (*ch == ':') 552009ea47eSEdward Tomasz Napierala colons++; 553009ea47eSEdward Tomasz Napierala } 554009ea47eSEdward Tomasz Napierala if (colons > 1) { 555009ea47eSEdward Tomasz Napierala addr = arg; 556009ea47eSEdward Tomasz Napierala port = "3260"; 557009ea47eSEdward Tomasz Napierala } else { 558009ea47eSEdward Tomasz Napierala addr = strsep(&arg, ":"); 559009ea47eSEdward Tomasz Napierala if (arg == NULL) 560009ea47eSEdward Tomasz Napierala port = "3260"; 561009ea47eSEdward Tomasz Napierala else 562009ea47eSEdward Tomasz Napierala port = arg; 563009ea47eSEdward Tomasz Napierala } 564009ea47eSEdward Tomasz Napierala } 565009ea47eSEdward Tomasz Napierala 566009ea47eSEdward Tomasz Napierala memset(&hints, 0, sizeof(hints)); 567009ea47eSEdward Tomasz Napierala hints.ai_family = PF_UNSPEC; 568009ea47eSEdward Tomasz Napierala hints.ai_socktype = SOCK_STREAM; 569009ea47eSEdward Tomasz Napierala hints.ai_flags = AI_PASSIVE; 570009ea47eSEdward Tomasz Napierala 571009ea47eSEdward Tomasz Napierala error = getaddrinfo(addr, port, &hints, &portal->p_ai); 572009ea47eSEdward Tomasz Napierala if (error != 0) { 573009ea47eSEdward Tomasz Napierala log_warnx("getaddrinfo for %s failed: %s", 574009ea47eSEdward Tomasz Napierala portal->p_listen, gai_strerror(error)); 575009ea47eSEdward Tomasz Napierala free(portal->p_listen); 576009ea47eSEdward Tomasz Napierala free(portal); 577009ea47eSEdward Tomasz Napierala return (1); 578009ea47eSEdward Tomasz Napierala } 579009ea47eSEdward Tomasz Napierala 580009ea47eSEdward Tomasz Napierala /* 581009ea47eSEdward Tomasz Napierala * XXX: getaddrinfo(3) may return multiple addresses; we should turn 582009ea47eSEdward Tomasz Napierala * those into multiple portals. 583009ea47eSEdward Tomasz Napierala */ 584009ea47eSEdward Tomasz Napierala 585009ea47eSEdward Tomasz Napierala return (0); 586009ea47eSEdward Tomasz Napierala } 587009ea47eSEdward Tomasz Napierala 588009ea47eSEdward Tomasz Napierala static bool 589009ea47eSEdward Tomasz Napierala valid_hex(const char ch) 590009ea47eSEdward Tomasz Napierala { 591009ea47eSEdward Tomasz Napierala switch (ch) { 592009ea47eSEdward Tomasz Napierala case '0': 593009ea47eSEdward Tomasz Napierala case '1': 594009ea47eSEdward Tomasz Napierala case '2': 595009ea47eSEdward Tomasz Napierala case '3': 596009ea47eSEdward Tomasz Napierala case '4': 597009ea47eSEdward Tomasz Napierala case '5': 598009ea47eSEdward Tomasz Napierala case '6': 599009ea47eSEdward Tomasz Napierala case '7': 600009ea47eSEdward Tomasz Napierala case '8': 601009ea47eSEdward Tomasz Napierala case '9': 602009ea47eSEdward Tomasz Napierala case 'a': 603009ea47eSEdward Tomasz Napierala case 'A': 604009ea47eSEdward Tomasz Napierala case 'b': 605009ea47eSEdward Tomasz Napierala case 'B': 606009ea47eSEdward Tomasz Napierala case 'c': 607009ea47eSEdward Tomasz Napierala case 'C': 608009ea47eSEdward Tomasz Napierala case 'd': 609009ea47eSEdward Tomasz Napierala case 'D': 610009ea47eSEdward Tomasz Napierala case 'e': 611009ea47eSEdward Tomasz Napierala case 'E': 612009ea47eSEdward Tomasz Napierala case 'f': 613009ea47eSEdward Tomasz Napierala case 'F': 614009ea47eSEdward Tomasz Napierala return (true); 615009ea47eSEdward Tomasz Napierala default: 616009ea47eSEdward Tomasz Napierala return (false); 617009ea47eSEdward Tomasz Napierala } 618009ea47eSEdward Tomasz Napierala } 619009ea47eSEdward Tomasz Napierala 620009ea47eSEdward Tomasz Napierala bool 621009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name) 622009ea47eSEdward Tomasz Napierala { 623009ea47eSEdward Tomasz Napierala int i; 624009ea47eSEdward Tomasz Napierala 625009ea47eSEdward Tomasz Napierala if (strlen(name) >= MAX_NAME_LEN) { 626009ea47eSEdward Tomasz Napierala log_warnx("overlong name for target \"%s\"; max length allowed " 627009ea47eSEdward Tomasz Napierala "by iSCSI specification is %d characters", 628009ea47eSEdward Tomasz Napierala name, MAX_NAME_LEN); 629009ea47eSEdward Tomasz Napierala return (false); 630009ea47eSEdward Tomasz Napierala } 631009ea47eSEdward Tomasz Napierala 632009ea47eSEdward Tomasz Napierala /* 633009ea47eSEdward Tomasz Napierala * In the cases below, we don't return an error, just in case the admin 634009ea47eSEdward Tomasz Napierala * was right, and we're wrong. 635009ea47eSEdward Tomasz Napierala */ 636009ea47eSEdward Tomasz Napierala if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 637009ea47eSEdward Tomasz Napierala for (i = strlen("iqn."); name[i] != '\0'; i++) { 638009ea47eSEdward Tomasz Napierala /* 639009ea47eSEdward Tomasz Napierala * XXX: We should verify UTF-8 normalisation, as defined 640009ea47eSEdward Tomasz Napierala * by 3.2.6.2: iSCSI Name Encoding. 641009ea47eSEdward Tomasz Napierala */ 642009ea47eSEdward Tomasz Napierala if (isalnum(name[i])) 643009ea47eSEdward Tomasz Napierala continue; 644009ea47eSEdward Tomasz Napierala if (name[i] == '-' || name[i] == '.' || name[i] == ':') 645009ea47eSEdward Tomasz Napierala continue; 646009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target name " 647009ea47eSEdward Tomasz Napierala "\"%s\"; allowed characters are letters, digits, " 648009ea47eSEdward Tomasz Napierala "'-', '.', and ':'", name[i], name); 649009ea47eSEdward Tomasz Napierala break; 650009ea47eSEdward Tomasz Napierala } 651009ea47eSEdward Tomasz Napierala /* 652009ea47eSEdward Tomasz Napierala * XXX: Check more stuff: valid date and a valid reversed domain. 653009ea47eSEdward Tomasz Napierala */ 654009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 655009ea47eSEdward Tomasz Napierala if (strlen(name) != strlen("eui.") + 16) 656009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"eui.\" " 657009ea47eSEdward Tomasz Napierala "should be followed by exactly 16 hexadecimal " 658009ea47eSEdward Tomasz Napierala "digits", name); 659009ea47eSEdward Tomasz Napierala for (i = strlen("eui."); name[i] != '\0'; i++) { 660009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 661009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 662009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 663009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 664009ea47eSEdward Tomasz Napierala break; 665009ea47eSEdward Tomasz Napierala } 666009ea47eSEdward Tomasz Napierala } 667009ea47eSEdward Tomasz Napierala } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 668009ea47eSEdward Tomasz Napierala if (strlen(name) > strlen("naa.") + 32) 669009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; the \"naa.\" " 670009ea47eSEdward Tomasz Napierala "should be followed by at most 32 hexadecimal " 671009ea47eSEdward Tomasz Napierala "digits", name); 672009ea47eSEdward Tomasz Napierala for (i = strlen("naa."); name[i] != '\0'; i++) { 673009ea47eSEdward Tomasz Napierala if (!valid_hex(name[i])) { 674009ea47eSEdward Tomasz Napierala log_warnx("invalid character \"%c\" in target " 675009ea47eSEdward Tomasz Napierala "name \"%s\"; allowed characters are 1-9 " 676009ea47eSEdward Tomasz Napierala "and A-F", name[i], name); 677009ea47eSEdward Tomasz Napierala break; 678009ea47eSEdward Tomasz Napierala } 679009ea47eSEdward Tomasz Napierala } 680009ea47eSEdward Tomasz Napierala } else { 681009ea47eSEdward Tomasz Napierala log_warnx("invalid target name \"%s\"; should start with " 682009ea47eSEdward Tomasz Napierala "either \".iqn\", \"eui.\", or \"naa.\"", 683009ea47eSEdward Tomasz Napierala name); 684009ea47eSEdward Tomasz Napierala } 685009ea47eSEdward Tomasz Napierala return (true); 686009ea47eSEdward Tomasz Napierala } 687009ea47eSEdward Tomasz Napierala 688009ea47eSEdward Tomasz Napierala struct target * 689009ea47eSEdward Tomasz Napierala target_new(struct conf *conf, const char *iqn) 690009ea47eSEdward Tomasz Napierala { 691009ea47eSEdward Tomasz Napierala struct target *targ; 692009ea47eSEdward Tomasz Napierala int i, len; 693009ea47eSEdward Tomasz Napierala 694009ea47eSEdward Tomasz Napierala targ = target_find(conf, iqn); 695009ea47eSEdward Tomasz Napierala if (targ != NULL) { 696009ea47eSEdward Tomasz Napierala log_warnx("duplicated target \"%s\"", iqn); 697009ea47eSEdward Tomasz Napierala return (NULL); 698009ea47eSEdward Tomasz Napierala } 699009ea47eSEdward Tomasz Napierala if (valid_iscsi_name(iqn) == false) { 700009ea47eSEdward Tomasz Napierala log_warnx("target name \"%s\" is invalid", iqn); 701009ea47eSEdward Tomasz Napierala return (NULL); 702009ea47eSEdward Tomasz Napierala } 703009ea47eSEdward Tomasz Napierala targ = calloc(1, sizeof(*targ)); 704009ea47eSEdward Tomasz Napierala if (targ == NULL) 705009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 706009ea47eSEdward Tomasz Napierala targ->t_iqn = checked_strdup(iqn); 707009ea47eSEdward Tomasz Napierala 708009ea47eSEdward Tomasz Napierala /* 709009ea47eSEdward Tomasz Napierala * RFC 3722 requires us to normalize the name to lowercase. 710009ea47eSEdward Tomasz Napierala */ 711009ea47eSEdward Tomasz Napierala len = strlen(iqn); 712009ea47eSEdward Tomasz Napierala for (i = 0; i < len; i++) 713009ea47eSEdward Tomasz Napierala targ->t_iqn[i] = tolower(targ->t_iqn[i]); 714009ea47eSEdward Tomasz Napierala 715009ea47eSEdward Tomasz Napierala TAILQ_INIT(&targ->t_luns); 716009ea47eSEdward Tomasz Napierala targ->t_conf = conf; 717009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 718009ea47eSEdward Tomasz Napierala 719009ea47eSEdward Tomasz Napierala return (targ); 720009ea47eSEdward Tomasz Napierala } 721009ea47eSEdward Tomasz Napierala 722009ea47eSEdward Tomasz Napierala void 723009ea47eSEdward Tomasz Napierala target_delete(struct target *targ) 724009ea47eSEdward Tomasz Napierala { 725009ea47eSEdward Tomasz Napierala struct lun *lun, *tmp; 726009ea47eSEdward Tomasz Napierala 727009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 728009ea47eSEdward Tomasz Napierala 729009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp) 730009ea47eSEdward Tomasz Napierala lun_delete(lun); 731009ea47eSEdward Tomasz Napierala free(targ->t_iqn); 732009ea47eSEdward Tomasz Napierala free(targ); 733009ea47eSEdward Tomasz Napierala } 734009ea47eSEdward Tomasz Napierala 735009ea47eSEdward Tomasz Napierala struct target * 736009ea47eSEdward Tomasz Napierala target_find(struct conf *conf, const char *iqn) 737009ea47eSEdward Tomasz Napierala { 738009ea47eSEdward Tomasz Napierala struct target *targ; 739009ea47eSEdward Tomasz Napierala 740009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 741009ea47eSEdward Tomasz Napierala if (strcasecmp(targ->t_iqn, iqn) == 0) 742009ea47eSEdward Tomasz Napierala return (targ); 743009ea47eSEdward Tomasz Napierala } 744009ea47eSEdward Tomasz Napierala 745009ea47eSEdward Tomasz Napierala return (NULL); 746009ea47eSEdward Tomasz Napierala } 747009ea47eSEdward Tomasz Napierala 748009ea47eSEdward Tomasz Napierala struct lun * 749009ea47eSEdward Tomasz Napierala lun_new(struct target *targ, int lun_id) 750009ea47eSEdward Tomasz Napierala { 751009ea47eSEdward Tomasz Napierala struct lun *lun; 752009ea47eSEdward Tomasz Napierala 753009ea47eSEdward Tomasz Napierala lun = lun_find(targ, lun_id); 754009ea47eSEdward Tomasz Napierala if (lun != NULL) { 755009ea47eSEdward Tomasz Napierala log_warnx("duplicated lun %d for target \"%s\"", 756009ea47eSEdward Tomasz Napierala lun_id, targ->t_iqn); 757009ea47eSEdward Tomasz Napierala return (NULL); 758009ea47eSEdward Tomasz Napierala } 759009ea47eSEdward Tomasz Napierala 760009ea47eSEdward Tomasz Napierala lun = calloc(1, sizeof(*lun)); 761009ea47eSEdward Tomasz Napierala if (lun == NULL) 762009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 763009ea47eSEdward Tomasz Napierala lun->l_lun = lun_id; 764009ea47eSEdward Tomasz Napierala TAILQ_INIT(&lun->l_options); 765009ea47eSEdward Tomasz Napierala lun->l_target = targ; 766009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next); 767009ea47eSEdward Tomasz Napierala 768009ea47eSEdward Tomasz Napierala return (lun); 769009ea47eSEdward Tomasz Napierala } 770009ea47eSEdward Tomasz Napierala 771009ea47eSEdward Tomasz Napierala void 772009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun) 773009ea47eSEdward Tomasz Napierala { 774009ea47eSEdward Tomasz Napierala struct lun_option *lo, *tmp; 775009ea47eSEdward Tomasz Napierala 776009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next); 777009ea47eSEdward Tomasz Napierala 778009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp) 779009ea47eSEdward Tomasz Napierala lun_option_delete(lo); 780009ea47eSEdward Tomasz Napierala free(lun->l_backend); 781009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 782009ea47eSEdward Tomasz Napierala free(lun->l_path); 783009ea47eSEdward Tomasz Napierala free(lun->l_serial); 784009ea47eSEdward Tomasz Napierala free(lun); 785009ea47eSEdward Tomasz Napierala } 786009ea47eSEdward Tomasz Napierala 787009ea47eSEdward Tomasz Napierala struct lun * 788009ea47eSEdward Tomasz Napierala lun_find(struct target *targ, int lun_id) 789009ea47eSEdward Tomasz Napierala { 790009ea47eSEdward Tomasz Napierala struct lun *lun; 791009ea47eSEdward Tomasz Napierala 792009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 793009ea47eSEdward Tomasz Napierala if (lun->l_lun == lun_id) 794009ea47eSEdward Tomasz Napierala return (lun); 795009ea47eSEdward Tomasz Napierala } 796009ea47eSEdward Tomasz Napierala 797009ea47eSEdward Tomasz Napierala return (NULL); 798009ea47eSEdward Tomasz Napierala } 799009ea47eSEdward Tomasz Napierala 800009ea47eSEdward Tomasz Napierala void 801009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value) 802009ea47eSEdward Tomasz Napierala { 803009ea47eSEdward Tomasz Napierala free(lun->l_backend); 804009ea47eSEdward Tomasz Napierala lun->l_backend = checked_strdup(value); 805009ea47eSEdward Tomasz Napierala } 806009ea47eSEdward Tomasz Napierala 807009ea47eSEdward Tomasz Napierala void 808009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value) 809009ea47eSEdward Tomasz Napierala { 810009ea47eSEdward Tomasz Napierala 811009ea47eSEdward Tomasz Napierala lun->l_blocksize = value; 812009ea47eSEdward Tomasz Napierala } 813009ea47eSEdward Tomasz Napierala 814009ea47eSEdward Tomasz Napierala void 815009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value) 816009ea47eSEdward Tomasz Napierala { 817009ea47eSEdward Tomasz Napierala free(lun->l_device_id); 818009ea47eSEdward Tomasz Napierala lun->l_device_id = checked_strdup(value); 819009ea47eSEdward Tomasz Napierala } 820009ea47eSEdward Tomasz Napierala 821009ea47eSEdward Tomasz Napierala void 822009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value) 823009ea47eSEdward Tomasz Napierala { 824009ea47eSEdward Tomasz Napierala free(lun->l_path); 825009ea47eSEdward Tomasz Napierala lun->l_path = checked_strdup(value); 826009ea47eSEdward Tomasz Napierala } 827009ea47eSEdward Tomasz Napierala 828009ea47eSEdward Tomasz Napierala void 829009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value) 830009ea47eSEdward Tomasz Napierala { 831009ea47eSEdward Tomasz Napierala free(lun->l_serial); 832009ea47eSEdward Tomasz Napierala lun->l_serial = checked_strdup(value); 833009ea47eSEdward Tomasz Napierala } 834009ea47eSEdward Tomasz Napierala 835009ea47eSEdward Tomasz Napierala void 836009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value) 837009ea47eSEdward Tomasz Napierala { 838009ea47eSEdward Tomasz Napierala 839009ea47eSEdward Tomasz Napierala lun->l_size = value; 840009ea47eSEdward Tomasz Napierala } 841009ea47eSEdward Tomasz Napierala 842009ea47eSEdward Tomasz Napierala void 843009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value) 844009ea47eSEdward Tomasz Napierala { 845009ea47eSEdward Tomasz Napierala 846009ea47eSEdward Tomasz Napierala lun->l_ctl_lun = value; 847009ea47eSEdward Tomasz Napierala } 848009ea47eSEdward Tomasz Napierala 849009ea47eSEdward Tomasz Napierala struct lun_option * 850009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value) 851009ea47eSEdward Tomasz Napierala { 852009ea47eSEdward Tomasz Napierala struct lun_option *lo; 853009ea47eSEdward Tomasz Napierala 854009ea47eSEdward Tomasz Napierala lo = lun_option_find(lun, name); 855009ea47eSEdward Tomasz Napierala if (lo != NULL) { 856009ea47eSEdward Tomasz Napierala log_warnx("duplicated lun option %s for lun %d, target \"%s\"", 857009ea47eSEdward Tomasz Napierala name, lun->l_lun, lun->l_target->t_iqn); 858009ea47eSEdward Tomasz Napierala return (NULL); 859009ea47eSEdward Tomasz Napierala } 860009ea47eSEdward Tomasz Napierala 861009ea47eSEdward Tomasz Napierala lo = calloc(1, sizeof(*lo)); 862009ea47eSEdward Tomasz Napierala if (lo == NULL) 863009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 864009ea47eSEdward Tomasz Napierala lo->lo_name = checked_strdup(name); 865009ea47eSEdward Tomasz Napierala lo->lo_value = checked_strdup(value); 866009ea47eSEdward Tomasz Napierala lo->lo_lun = lun; 867009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next); 868009ea47eSEdward Tomasz Napierala 869009ea47eSEdward Tomasz Napierala return (lo); 870009ea47eSEdward Tomasz Napierala } 871009ea47eSEdward Tomasz Napierala 872009ea47eSEdward Tomasz Napierala void 873009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo) 874009ea47eSEdward Tomasz Napierala { 875009ea47eSEdward Tomasz Napierala 876009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next); 877009ea47eSEdward Tomasz Napierala 878009ea47eSEdward Tomasz Napierala free(lo->lo_name); 879009ea47eSEdward Tomasz Napierala free(lo->lo_value); 880009ea47eSEdward Tomasz Napierala free(lo); 881009ea47eSEdward Tomasz Napierala } 882009ea47eSEdward Tomasz Napierala 883009ea47eSEdward Tomasz Napierala struct lun_option * 884009ea47eSEdward Tomasz Napierala lun_option_find(struct lun *lun, const char *name) 885009ea47eSEdward Tomasz Napierala { 886009ea47eSEdward Tomasz Napierala struct lun_option *lo; 887009ea47eSEdward Tomasz Napierala 888009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 889009ea47eSEdward Tomasz Napierala if (strcmp(lo->lo_name, name) == 0) 890009ea47eSEdward Tomasz Napierala return (lo); 891009ea47eSEdward Tomasz Napierala } 892009ea47eSEdward Tomasz Napierala 893009ea47eSEdward Tomasz Napierala return (NULL); 894009ea47eSEdward Tomasz Napierala } 895009ea47eSEdward Tomasz Napierala 896009ea47eSEdward Tomasz Napierala void 897009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value) 898009ea47eSEdward Tomasz Napierala { 899009ea47eSEdward Tomasz Napierala 900009ea47eSEdward Tomasz Napierala free(lo->lo_value); 901009ea47eSEdward Tomasz Napierala lo->lo_value = checked_strdup(value); 902009ea47eSEdward Tomasz Napierala } 903009ea47eSEdward Tomasz Napierala 904009ea47eSEdward Tomasz Napierala static struct connection * 905009ea47eSEdward Tomasz Napierala connection_new(struct portal *portal, int fd, const char *host) 906009ea47eSEdward Tomasz Napierala { 907009ea47eSEdward Tomasz Napierala struct connection *conn; 908009ea47eSEdward Tomasz Napierala 909009ea47eSEdward Tomasz Napierala conn = calloc(1, sizeof(*conn)); 910009ea47eSEdward Tomasz Napierala if (conn == NULL) 911009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 912009ea47eSEdward Tomasz Napierala conn->conn_portal = portal; 913009ea47eSEdward Tomasz Napierala conn->conn_socket = fd; 914009ea47eSEdward Tomasz Napierala conn->conn_initiator_addr = checked_strdup(host); 915009ea47eSEdward Tomasz Napierala 916009ea47eSEdward Tomasz Napierala /* 917009ea47eSEdward Tomasz Napierala * Default values, from RFC 3720, section 12. 918009ea47eSEdward Tomasz Napierala */ 919009ea47eSEdward Tomasz Napierala conn->conn_max_data_segment_length = 8192; 920009ea47eSEdward Tomasz Napierala conn->conn_max_burst_length = 262144; 921009ea47eSEdward Tomasz Napierala conn->conn_immediate_data = true; 922009ea47eSEdward Tomasz Napierala 923009ea47eSEdward Tomasz Napierala return (conn); 924009ea47eSEdward Tomasz Napierala } 925009ea47eSEdward Tomasz Napierala 926009ea47eSEdward Tomasz Napierala #if 0 927009ea47eSEdward Tomasz Napierala static void 928009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf) 929009ea47eSEdward Tomasz Napierala { 930009ea47eSEdward Tomasz Napierala struct auth_group *ag; 931009ea47eSEdward Tomasz Napierala struct auth *auth; 9328cb2e958SEdward Tomasz Napierala struct auth_name *auth_name; 9338cb2e958SEdward Tomasz Napierala struct auth_portal *auth_portal; 934009ea47eSEdward Tomasz Napierala struct portal_group *pg; 935009ea47eSEdward Tomasz Napierala struct portal *portal; 936009ea47eSEdward Tomasz Napierala struct target *targ; 937009ea47eSEdward Tomasz Napierala struct lun *lun; 938009ea47eSEdward Tomasz Napierala struct lun_option *lo; 939009ea47eSEdward Tomasz Napierala 940009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 941009ea47eSEdward Tomasz Napierala fprintf(stderr, "auth-group %s {\n", ag->ag_name); 942009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(auth, &ag->ag_auths, a_next) 943009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t chap-mutual %s %s %s %s\n", 944009ea47eSEdward Tomasz Napierala auth->a_user, auth->a_secret, 945009ea47eSEdward Tomasz Napierala auth->a_mutual_user, auth->a_mutual_secret); 9468cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) 9478cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-name %s\n", 9488cb2e958SEdward Tomasz Napierala auth_name->an_initator_name); 9498cb2e958SEdward Tomasz Napierala TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next) 9508cb2e958SEdward Tomasz Napierala fprintf(stderr, "\t initiator-portal %s\n", 9518cb2e958SEdward Tomasz Napierala auth_portal->an_initator_portal); 952009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 953009ea47eSEdward Tomasz Napierala } 954009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 955009ea47eSEdward Tomasz Napierala fprintf(stderr, "portal-group %s {\n", pg->pg_name); 956009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 957009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t listen %s\n", portal->p_listen); 958009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 959009ea47eSEdward Tomasz Napierala } 960009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 961009ea47eSEdward Tomasz Napierala fprintf(stderr, "target %s {\n", targ->t_iqn); 962009ea47eSEdward Tomasz Napierala if (targ->t_alias != NULL) 963009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t alias %s\n", targ->t_alias); 964009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 965009ea47eSEdward Tomasz Napierala fprintf(stderr, "\tlun %d {\n", lun->l_lun); 966009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\tpath %s\n", lun->l_path); 967009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lo, &lun->l_options, lo_next) 968009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t\toption %s %s\n", 969009ea47eSEdward Tomasz Napierala lo->lo_name, lo->lo_value); 970009ea47eSEdward Tomasz Napierala fprintf(stderr, "\t}\n"); 971009ea47eSEdward Tomasz Napierala } 972009ea47eSEdward Tomasz Napierala fprintf(stderr, "}\n"); 973009ea47eSEdward Tomasz Napierala } 974009ea47eSEdward Tomasz Napierala } 975009ea47eSEdward Tomasz Napierala #endif 976009ea47eSEdward Tomasz Napierala 9772fabfaa5SEdward Tomasz Napierala static int 9782fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun) 9792fabfaa5SEdward Tomasz Napierala { 9802fabfaa5SEdward Tomasz Napierala const struct lun *lun2; 981a19eebb1SEdward Tomasz Napierala const struct target *targ2; 9822fabfaa5SEdward Tomasz Napierala 9832fabfaa5SEdward Tomasz Napierala if (lun->l_backend == NULL) 9842fabfaa5SEdward Tomasz Napierala lun_set_backend(lun, "block"); 9852fabfaa5SEdward Tomasz Napierala if (strcmp(lun->l_backend, "block") == 0) { 9862fabfaa5SEdward Tomasz Napierala if (lun->l_path == NULL) { 9872fabfaa5SEdward Tomasz Napierala log_warnx("missing path for lun %d, target \"%s\"", 9882fabfaa5SEdward Tomasz Napierala lun->l_lun, lun->l_target->t_iqn); 9892fabfaa5SEdward Tomasz Napierala return (1); 9902fabfaa5SEdward Tomasz Napierala } 9912fabfaa5SEdward Tomasz Napierala } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 9922fabfaa5SEdward Tomasz Napierala if (lun->l_size == 0) { 9932fabfaa5SEdward Tomasz Napierala log_warnx("missing size for ramdisk-backed lun %d, " 9942fabfaa5SEdward Tomasz Napierala "target \"%s\"", lun->l_lun, lun->l_target->t_iqn); 9952fabfaa5SEdward Tomasz Napierala return (1); 9962fabfaa5SEdward Tomasz Napierala } 9972fabfaa5SEdward Tomasz Napierala if (lun->l_path != NULL) { 9982fabfaa5SEdward Tomasz Napierala log_warnx("path must not be specified " 9992fabfaa5SEdward Tomasz Napierala "for ramdisk-backed lun %d, target \"%s\"", 10002fabfaa5SEdward Tomasz Napierala lun->l_lun, lun->l_target->t_iqn); 10012fabfaa5SEdward Tomasz Napierala return (1); 10022fabfaa5SEdward Tomasz Napierala } 10032fabfaa5SEdward Tomasz Napierala } 10042fabfaa5SEdward Tomasz Napierala if (lun->l_lun < 0 || lun->l_lun > 255) { 10052fabfaa5SEdward Tomasz Napierala log_warnx("invalid lun number for lun %d, target \"%s\"; " 10062fabfaa5SEdward Tomasz Napierala "must be between 0 and 255", lun->l_lun, 10072fabfaa5SEdward Tomasz Napierala lun->l_target->t_iqn); 10082fabfaa5SEdward Tomasz Napierala return (1); 10092fabfaa5SEdward Tomasz Napierala } 10102fabfaa5SEdward Tomasz Napierala if (lun->l_blocksize == 0) { 10112fabfaa5SEdward Tomasz Napierala lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 10122fabfaa5SEdward Tomasz Napierala } else if (lun->l_blocksize < 0) { 10132fabfaa5SEdward Tomasz Napierala log_warnx("invalid blocksize for lun %d, target \"%s\"; " 10142fabfaa5SEdward Tomasz Napierala "must be larger than 0", lun->l_lun, lun->l_target->t_iqn); 10152fabfaa5SEdward Tomasz Napierala return (1); 10162fabfaa5SEdward Tomasz Napierala } 10172fabfaa5SEdward Tomasz Napierala if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 10182fabfaa5SEdward Tomasz Napierala log_warnx("invalid size for lun %d, target \"%s\"; " 10192fabfaa5SEdward Tomasz Napierala "must be multiple of blocksize", lun->l_lun, 10202fabfaa5SEdward Tomasz Napierala lun->l_target->t_iqn); 10212fabfaa5SEdward Tomasz Napierala return (1); 10222fabfaa5SEdward Tomasz Napierala } 1023a19eebb1SEdward Tomasz Napierala TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) { 1024a19eebb1SEdward Tomasz Napierala TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) { 1025a19eebb1SEdward Tomasz Napierala if (lun == lun2) 1026a19eebb1SEdward Tomasz Napierala continue; 1027a19eebb1SEdward Tomasz Napierala if (lun->l_path != NULL && lun2->l_path != NULL && 1028a19eebb1SEdward Tomasz Napierala strcmp(lun->l_path, lun2->l_path) == 0) { 1029a19eebb1SEdward Tomasz Napierala log_debugx("WARNING: path \"%s\" duplicated " 1030a19eebb1SEdward Tomasz Napierala "between lun %d, target \"%s\", and " 1031a19eebb1SEdward Tomasz Napierala "lun %d, target \"%s\"", lun->l_path, 1032a19eebb1SEdward Tomasz Napierala lun->l_lun, lun->l_target->t_iqn, 1033a19eebb1SEdward Tomasz Napierala lun2->l_lun, lun2->l_target->t_iqn); 1034a19eebb1SEdward Tomasz Napierala } 1035a19eebb1SEdward Tomasz Napierala } 1036a19eebb1SEdward Tomasz Napierala } 10372fabfaa5SEdward Tomasz Napierala 10382fabfaa5SEdward Tomasz Napierala return (0); 10392fabfaa5SEdward Tomasz Napierala } 10402fabfaa5SEdward Tomasz Napierala 1041009ea47eSEdward Tomasz Napierala int 1042009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf) 1043009ea47eSEdward Tomasz Napierala { 1044009ea47eSEdward Tomasz Napierala struct auth_group *ag; 1045009ea47eSEdward Tomasz Napierala struct portal_group *pg; 1046009ea47eSEdward Tomasz Napierala struct target *targ; 10472fabfaa5SEdward Tomasz Napierala struct lun *lun; 1048009ea47eSEdward Tomasz Napierala bool found_lun0; 10492fabfaa5SEdward Tomasz Napierala int error; 1050009ea47eSEdward Tomasz Napierala 1051009ea47eSEdward Tomasz Napierala if (conf->conf_pidfile_path == NULL) 1052009ea47eSEdward Tomasz Napierala conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1053009ea47eSEdward Tomasz Napierala 1054009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1055009ea47eSEdward Tomasz Napierala if (targ->t_auth_group == NULL) { 1056009ea47eSEdward Tomasz Napierala log_warnx("missing authentication for target \"%s\"; " 1057009ea47eSEdward Tomasz Napierala "must specify either \"auth-group\", \"chap\", " 1058009ea47eSEdward Tomasz Napierala "or \"chap-mutual\"", targ->t_iqn); 1059009ea47eSEdward Tomasz Napierala return (1); 1060009ea47eSEdward Tomasz Napierala } 1061009ea47eSEdward Tomasz Napierala if (targ->t_portal_group == NULL) { 1062009ea47eSEdward Tomasz Napierala targ->t_portal_group = portal_group_find(conf, 1063009ea47eSEdward Tomasz Napierala "default"); 1064009ea47eSEdward Tomasz Napierala assert(targ->t_portal_group != NULL); 1065009ea47eSEdward Tomasz Napierala } 1066009ea47eSEdward Tomasz Napierala found_lun0 = false; 1067009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 10682fabfaa5SEdward Tomasz Napierala error = conf_verify_lun(lun); 10692fabfaa5SEdward Tomasz Napierala if (error != 0) 10702fabfaa5SEdward Tomasz Napierala return (error); 1071009ea47eSEdward Tomasz Napierala if (lun->l_lun == 0) 1072009ea47eSEdward Tomasz Napierala found_lun0 = true; 1073009ea47eSEdward Tomasz Napierala } 1074009ea47eSEdward Tomasz Napierala if (!found_lun0) { 1075009ea47eSEdward Tomasz Napierala log_warnx("mandatory LUN 0 not configured " 1076009ea47eSEdward Tomasz Napierala "for target \"%s\"", targ->t_iqn); 1077009ea47eSEdward Tomasz Napierala return (1); 1078009ea47eSEdward Tomasz Napierala } 1079009ea47eSEdward Tomasz Napierala } 1080009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1081009ea47eSEdward Tomasz Napierala assert(pg->pg_name != NULL); 1082009ea47eSEdward Tomasz Napierala if (pg->pg_discovery_auth_group == NULL) { 1083009ea47eSEdward Tomasz Napierala pg->pg_discovery_auth_group = 1084009ea47eSEdward Tomasz Napierala auth_group_find(conf, "no-access"); 1085009ea47eSEdward Tomasz Napierala assert(pg->pg_discovery_auth_group != NULL); 1086009ea47eSEdward Tomasz Napierala } 1087009ea47eSEdward Tomasz Napierala 1088009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1089009ea47eSEdward Tomasz Napierala if (targ->t_portal_group == pg) 1090009ea47eSEdward Tomasz Napierala break; 1091009ea47eSEdward Tomasz Napierala } 1092009ea47eSEdward Tomasz Napierala if (targ == NULL) { 1093009ea47eSEdward Tomasz Napierala if (strcmp(pg->pg_name, "default") != 0) 1094009ea47eSEdward Tomasz Napierala log_warnx("portal-group \"%s\" not assigned " 1095009ea47eSEdward Tomasz Napierala "to any target", pg->pg_name); 1096009ea47eSEdward Tomasz Napierala pg->pg_unassigned = true; 1097009ea47eSEdward Tomasz Napierala } else 1098009ea47eSEdward Tomasz Napierala pg->pg_unassigned = false; 1099009ea47eSEdward Tomasz Napierala } 1100009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1101009ea47eSEdward Tomasz Napierala if (ag->ag_name == NULL) 1102009ea47eSEdward Tomasz Napierala assert(ag->ag_target != NULL); 1103009ea47eSEdward Tomasz Napierala else 1104009ea47eSEdward Tomasz Napierala assert(ag->ag_target == NULL); 1105009ea47eSEdward Tomasz Napierala 1106009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1107009ea47eSEdward Tomasz Napierala if (targ->t_auth_group == ag) 1108009ea47eSEdward Tomasz Napierala break; 1109009ea47eSEdward Tomasz Napierala } 1110009ea47eSEdward Tomasz Napierala if (targ == NULL && ag->ag_name != NULL && 1111009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-authentication") != 0 && 1112009ea47eSEdward Tomasz Napierala strcmp(ag->ag_name, "no-access") != 0) { 1113009ea47eSEdward Tomasz Napierala log_warnx("auth-group \"%s\" not assigned " 1114009ea47eSEdward Tomasz Napierala "to any target", ag->ag_name); 1115009ea47eSEdward Tomasz Napierala } 1116009ea47eSEdward Tomasz Napierala } 1117009ea47eSEdward Tomasz Napierala 1118009ea47eSEdward Tomasz Napierala return (0); 1119009ea47eSEdward Tomasz Napierala } 1120009ea47eSEdward Tomasz Napierala 1121009ea47eSEdward Tomasz Napierala static int 1122009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf) 1123009ea47eSEdward Tomasz Napierala { 1124009ea47eSEdward Tomasz Napierala struct target *oldtarg, *newtarg, *tmptarg; 1125009ea47eSEdward Tomasz Napierala struct lun *oldlun, *newlun, *tmplun; 1126009ea47eSEdward Tomasz Napierala struct portal_group *oldpg, *newpg; 1127009ea47eSEdward Tomasz Napierala struct portal *oldp, *newp; 1128009ea47eSEdward Tomasz Napierala pid_t otherpid; 1129009ea47eSEdward Tomasz Napierala int changed, cumulated_error = 0, error; 1130009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 1131009ea47eSEdward Tomasz Napierala int one = 1; 1132009ea47eSEdward Tomasz Napierala #endif 1133009ea47eSEdward Tomasz Napierala 1134009ea47eSEdward Tomasz Napierala if (oldconf->conf_debug != newconf->conf_debug) { 1135009ea47eSEdward Tomasz Napierala log_debugx("changing debug level to %d", newconf->conf_debug); 1136009ea47eSEdward Tomasz Napierala log_init(newconf->conf_debug); 1137009ea47eSEdward Tomasz Napierala } 1138009ea47eSEdward Tomasz Napierala 1139009ea47eSEdward Tomasz Napierala if (oldconf->conf_pidfh != NULL) { 1140009ea47eSEdward Tomasz Napierala assert(oldconf->conf_pidfile_path != NULL); 1141009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfile_path != NULL && 1142009ea47eSEdward Tomasz Napierala strcmp(oldconf->conf_pidfile_path, 1143009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path) == 0) { 1144009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = oldconf->conf_pidfh; 1145009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1146009ea47eSEdward Tomasz Napierala } else { 1147009ea47eSEdward Tomasz Napierala log_debugx("removing pidfile %s", 1148009ea47eSEdward Tomasz Napierala oldconf->conf_pidfile_path); 1149009ea47eSEdward Tomasz Napierala pidfile_remove(oldconf->conf_pidfh); 1150009ea47eSEdward Tomasz Napierala oldconf->conf_pidfh = NULL; 1151009ea47eSEdward Tomasz Napierala } 1152009ea47eSEdward Tomasz Napierala } 1153009ea47eSEdward Tomasz Napierala 1154009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1155009ea47eSEdward Tomasz Napierala log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1156009ea47eSEdward Tomasz Napierala newconf->conf_pidfh = 1157009ea47eSEdward Tomasz Napierala pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1158009ea47eSEdward Tomasz Napierala if (newconf->conf_pidfh == NULL) { 1159009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 1160009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 1161009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 1162009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 1163009ea47eSEdward Tomasz Napierala newconf->conf_pidfile_path); 1164009ea47eSEdward Tomasz Napierala } 1165009ea47eSEdward Tomasz Napierala } 1166009ea47eSEdward Tomasz Napierala 1167009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) { 1168009ea47eSEdward Tomasz Napierala /* 1169009ea47eSEdward Tomasz Napierala * First, remove any targets present in the old configuration 1170009ea47eSEdward Tomasz Napierala * and missing in the new one. 1171009ea47eSEdward Tomasz Napierala */ 1172009ea47eSEdward Tomasz Napierala newtarg = target_find(newconf, oldtarg->t_iqn); 1173009ea47eSEdward Tomasz Napierala if (newtarg == NULL) { 1174009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, 1175009ea47eSEdward Tomasz Napierala tmplun) { 1176009ea47eSEdward Tomasz Napierala log_debugx("target %s not found in the " 1177009ea47eSEdward Tomasz Napierala "configuration file; removing its lun %d, " 1178009ea47eSEdward Tomasz Napierala "backed by CTL lun %d", 1179009ea47eSEdward Tomasz Napierala oldtarg->t_iqn, oldlun->l_lun, 1180009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1181009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1182009ea47eSEdward Tomasz Napierala if (error != 0) { 1183009ea47eSEdward Tomasz Napierala log_warnx("failed to remove lun %d, " 1184009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1185009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1186009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1187009ea47eSEdward Tomasz Napierala cumulated_error++; 1188009ea47eSEdward Tomasz Napierala } 1189009ea47eSEdward Tomasz Napierala lun_delete(oldlun); 1190009ea47eSEdward Tomasz Napierala } 1191009ea47eSEdward Tomasz Napierala target_delete(oldtarg); 1192009ea47eSEdward Tomasz Napierala continue; 1193009ea47eSEdward Tomasz Napierala } 1194009ea47eSEdward Tomasz Napierala 1195009ea47eSEdward Tomasz Napierala /* 1196009ea47eSEdward Tomasz Napierala * Second, remove any LUNs present in the old target 1197009ea47eSEdward Tomasz Napierala * and missing in the new one. 1198009ea47eSEdward Tomasz Napierala */ 1199009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) { 1200009ea47eSEdward Tomasz Napierala newlun = lun_find(newtarg, oldlun->l_lun); 1201009ea47eSEdward Tomasz Napierala if (newlun == NULL) { 1202009ea47eSEdward Tomasz Napierala log_debugx("lun %d, target %s, CTL lun %d " 1203009ea47eSEdward Tomasz Napierala "not found in the configuration file; " 1204009ea47eSEdward Tomasz Napierala "removing", oldlun->l_lun, oldtarg->t_iqn, 1205009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1206009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1207009ea47eSEdward Tomasz Napierala if (error != 0) { 1208009ea47eSEdward Tomasz Napierala log_warnx("failed to remove lun %d, " 1209009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1210009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1211009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1212009ea47eSEdward Tomasz Napierala cumulated_error++; 1213009ea47eSEdward Tomasz Napierala } 1214009ea47eSEdward Tomasz Napierala lun_delete(oldlun); 1215009ea47eSEdward Tomasz Napierala continue; 1216009ea47eSEdward Tomasz Napierala } 1217009ea47eSEdward Tomasz Napierala 1218009ea47eSEdward Tomasz Napierala /* 1219009ea47eSEdward Tomasz Napierala * Also remove the LUNs changed by more than size. 1220009ea47eSEdward Tomasz Napierala */ 1221009ea47eSEdward Tomasz Napierala changed = 0; 1222009ea47eSEdward Tomasz Napierala assert(oldlun->l_backend != NULL); 1223009ea47eSEdward Tomasz Napierala assert(newlun->l_backend != NULL); 1224009ea47eSEdward Tomasz Napierala if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1225009ea47eSEdward Tomasz Napierala log_debugx("backend for lun %d, target %s, " 1226009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1227009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1228009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1229009ea47eSEdward Tomasz Napierala changed = 1; 1230009ea47eSEdward Tomasz Napierala } 1231009ea47eSEdward Tomasz Napierala if (oldlun->l_blocksize != newlun->l_blocksize) { 1232009ea47eSEdward Tomasz Napierala log_debugx("blocksize for lun %d, target %s, " 1233009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1234009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1235009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1236009ea47eSEdward Tomasz Napierala changed = 1; 1237009ea47eSEdward Tomasz Napierala } 1238009ea47eSEdward Tomasz Napierala if (newlun->l_device_id != NULL && 1239009ea47eSEdward Tomasz Napierala (oldlun->l_device_id == NULL || 1240009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_device_id, newlun->l_device_id) != 1241009ea47eSEdward Tomasz Napierala 0)) { 1242009ea47eSEdward Tomasz Napierala log_debugx("device-id for lun %d, target %s, " 1243009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1244009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1245009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1246009ea47eSEdward Tomasz Napierala changed = 1; 1247009ea47eSEdward Tomasz Napierala } 1248009ea47eSEdward Tomasz Napierala if (newlun->l_path != NULL && 1249009ea47eSEdward Tomasz Napierala (oldlun->l_path == NULL || 1250009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1251009ea47eSEdward Tomasz Napierala log_debugx("path for lun %d, target %s, " 1252009ea47eSEdward Tomasz Napierala "CTL lun %d, changed; removing", 1253009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1254009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1255009ea47eSEdward Tomasz Napierala changed = 1; 1256009ea47eSEdward Tomasz Napierala } 1257009ea47eSEdward Tomasz Napierala if (newlun->l_serial != NULL && 1258009ea47eSEdward Tomasz Napierala (oldlun->l_serial == NULL || 1259009ea47eSEdward Tomasz Napierala strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1260009ea47eSEdward Tomasz Napierala log_debugx("serial for lun %d, target %s, " 1261009ea47eSEdward Tomasz Napierala "CTL lun %d changed; removing", 1262009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1263009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1264009ea47eSEdward Tomasz Napierala changed = 1; 1265009ea47eSEdward Tomasz Napierala } 1266009ea47eSEdward Tomasz Napierala if (changed) { 1267009ea47eSEdward Tomasz Napierala error = kernel_lun_remove(oldlun); 1268009ea47eSEdward Tomasz Napierala if (error != 0) { 1269009ea47eSEdward Tomasz Napierala log_warnx("failed to remove lun %d, " 1270009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1271009ea47eSEdward Tomasz Napierala oldlun->l_lun, oldtarg->t_iqn, 1272009ea47eSEdward Tomasz Napierala oldlun->l_ctl_lun); 1273009ea47eSEdward Tomasz Napierala cumulated_error++; 1274009ea47eSEdward Tomasz Napierala } 1275009ea47eSEdward Tomasz Napierala lun_delete(oldlun); 1276009ea47eSEdward Tomasz Napierala continue; 1277009ea47eSEdward Tomasz Napierala } 1278009ea47eSEdward Tomasz Napierala 1279009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1280009ea47eSEdward Tomasz Napierala } 1281009ea47eSEdward Tomasz Napierala } 1282009ea47eSEdward Tomasz Napierala 1283009ea47eSEdward Tomasz Napierala /* 1284009ea47eSEdward Tomasz Napierala * Now add new targets or modify existing ones. 1285009ea47eSEdward Tomasz Napierala */ 1286009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) { 1287009ea47eSEdward Tomasz Napierala oldtarg = target_find(oldconf, newtarg->t_iqn); 1288009ea47eSEdward Tomasz Napierala 1289009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newlun, &newtarg->t_luns, l_next) { 1290009ea47eSEdward Tomasz Napierala if (oldtarg != NULL) { 1291009ea47eSEdward Tomasz Napierala oldlun = lun_find(oldtarg, newlun->l_lun); 1292009ea47eSEdward Tomasz Napierala if (oldlun != NULL) { 1293009ea47eSEdward Tomasz Napierala if (newlun->l_size != oldlun->l_size) { 1294009ea47eSEdward Tomasz Napierala log_debugx("resizing lun %d, " 1295009ea47eSEdward Tomasz Napierala "target %s, CTL lun %d", 1296009ea47eSEdward Tomasz Napierala newlun->l_lun, 1297009ea47eSEdward Tomasz Napierala newtarg->t_iqn, 1298009ea47eSEdward Tomasz Napierala newlun->l_ctl_lun); 1299009ea47eSEdward Tomasz Napierala error = 1300009ea47eSEdward Tomasz Napierala kernel_lun_resize(newlun); 1301009ea47eSEdward Tomasz Napierala if (error != 0) { 1302009ea47eSEdward Tomasz Napierala log_warnx("failed to " 1303009ea47eSEdward Tomasz Napierala "resize lun %d, " 1304009ea47eSEdward Tomasz Napierala "target %s, " 1305009ea47eSEdward Tomasz Napierala "CTL lun %d", 1306009ea47eSEdward Tomasz Napierala newlun->l_lun, 1307009ea47eSEdward Tomasz Napierala newtarg->t_iqn, 1308009ea47eSEdward Tomasz Napierala newlun->l_lun); 1309009ea47eSEdward Tomasz Napierala cumulated_error++; 1310009ea47eSEdward Tomasz Napierala } 1311009ea47eSEdward Tomasz Napierala } 1312009ea47eSEdward Tomasz Napierala continue; 1313009ea47eSEdward Tomasz Napierala } 1314009ea47eSEdward Tomasz Napierala } 1315009ea47eSEdward Tomasz Napierala log_debugx("adding lun %d, target %s", 1316009ea47eSEdward Tomasz Napierala newlun->l_lun, newtarg->t_iqn); 1317009ea47eSEdward Tomasz Napierala error = kernel_lun_add(newlun); 1318009ea47eSEdward Tomasz Napierala if (error != 0) { 1319009ea47eSEdward Tomasz Napierala log_warnx("failed to add lun %d, target %s", 1320009ea47eSEdward Tomasz Napierala newlun->l_lun, newtarg->t_iqn); 1321009ea47eSEdward Tomasz Napierala cumulated_error++; 1322009ea47eSEdward Tomasz Napierala } 1323009ea47eSEdward Tomasz Napierala } 1324009ea47eSEdward Tomasz Napierala } 1325009ea47eSEdward Tomasz Napierala 1326009ea47eSEdward Tomasz Napierala /* 1327009ea47eSEdward Tomasz Napierala * Go through the new portals, opening the sockets as neccessary. 1328009ea47eSEdward Tomasz Napierala */ 1329009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1330009ea47eSEdward Tomasz Napierala if (newpg->pg_unassigned) { 1331009ea47eSEdward Tomasz Napierala log_debugx("not listening on portal-group \"%s\", " 1332009ea47eSEdward Tomasz Napierala "not assigned to any target", 1333009ea47eSEdward Tomasz Napierala newpg->pg_name); 1334009ea47eSEdward Tomasz Napierala continue; 1335009ea47eSEdward Tomasz Napierala } 1336009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 1337009ea47eSEdward Tomasz Napierala /* 1338009ea47eSEdward Tomasz Napierala * Try to find already open portal and reuse 1339009ea47eSEdward Tomasz Napierala * the listening socket. We don't care about 1340009ea47eSEdward Tomasz Napierala * what portal or portal group that was, what 1341009ea47eSEdward Tomasz Napierala * matters is the listening address. 1342009ea47eSEdward Tomasz Napierala */ 1343009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 1344009ea47eSEdward Tomasz Napierala pg_next) { 1345009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, 1346009ea47eSEdward Tomasz Napierala p_next) { 1347009ea47eSEdward Tomasz Napierala if (strcmp(newp->p_listen, 1348009ea47eSEdward Tomasz Napierala oldp->p_listen) == 0 && 1349009ea47eSEdward Tomasz Napierala oldp->p_socket > 0) { 1350009ea47eSEdward Tomasz Napierala newp->p_socket = 1351009ea47eSEdward Tomasz Napierala oldp->p_socket; 1352009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 1353009ea47eSEdward Tomasz Napierala break; 1354009ea47eSEdward Tomasz Napierala } 1355009ea47eSEdward Tomasz Napierala } 1356009ea47eSEdward Tomasz Napierala } 1357009ea47eSEdward Tomasz Napierala if (newp->p_socket > 0) { 1358009ea47eSEdward Tomasz Napierala /* 1359009ea47eSEdward Tomasz Napierala * We're done with this portal. 1360009ea47eSEdward Tomasz Napierala */ 1361009ea47eSEdward Tomasz Napierala continue; 1362009ea47eSEdward Tomasz Napierala } 1363009ea47eSEdward Tomasz Napierala 1364009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1365009ea47eSEdward Tomasz Napierala log_debugx("listening on %s, portal-group \"%s\" using ICL proxy", 1366009ea47eSEdward Tomasz Napierala newp->p_listen, newpg->pg_name); 1367009ea47eSEdward Tomasz Napierala kernel_listen(newp->p_ai, newp->p_iser); 1368009ea47eSEdward Tomasz Napierala #else 1369009ea47eSEdward Tomasz Napierala assert(newp->p_iser == false); 1370009ea47eSEdward Tomasz Napierala 1371009ea47eSEdward Tomasz Napierala log_debugx("listening on %s, portal-group \"%s\"", 1372009ea47eSEdward Tomasz Napierala newp->p_listen, newpg->pg_name); 1373009ea47eSEdward Tomasz Napierala newp->p_socket = socket(newp->p_ai->ai_family, 1374009ea47eSEdward Tomasz Napierala newp->p_ai->ai_socktype, 1375009ea47eSEdward Tomasz Napierala newp->p_ai->ai_protocol); 1376009ea47eSEdward Tomasz Napierala if (newp->p_socket < 0) { 1377009ea47eSEdward Tomasz Napierala log_warn("socket(2) failed for %s", 1378009ea47eSEdward Tomasz Napierala newp->p_listen); 1379009ea47eSEdward Tomasz Napierala cumulated_error++; 1380009ea47eSEdward Tomasz Napierala continue; 1381009ea47eSEdward Tomasz Napierala } 1382009ea47eSEdward Tomasz Napierala error = setsockopt(newp->p_socket, SOL_SOCKET, 1383009ea47eSEdward Tomasz Napierala SO_REUSEADDR, &one, sizeof(one)); 1384009ea47eSEdward Tomasz Napierala if (error != 0) { 1385009ea47eSEdward Tomasz Napierala log_warn("setsockopt(SO_REUSEADDR) failed " 1386009ea47eSEdward Tomasz Napierala "for %s", newp->p_listen); 1387009ea47eSEdward Tomasz Napierala close(newp->p_socket); 1388009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 1389009ea47eSEdward Tomasz Napierala cumulated_error++; 1390009ea47eSEdward Tomasz Napierala continue; 1391009ea47eSEdward Tomasz Napierala } 1392009ea47eSEdward Tomasz Napierala error = bind(newp->p_socket, newp->p_ai->ai_addr, 1393009ea47eSEdward Tomasz Napierala newp->p_ai->ai_addrlen); 1394009ea47eSEdward Tomasz Napierala if (error != 0) { 1395009ea47eSEdward Tomasz Napierala log_warn("bind(2) failed for %s", 1396009ea47eSEdward Tomasz Napierala newp->p_listen); 1397009ea47eSEdward Tomasz Napierala close(newp->p_socket); 1398009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 1399009ea47eSEdward Tomasz Napierala cumulated_error++; 1400009ea47eSEdward Tomasz Napierala continue; 1401009ea47eSEdward Tomasz Napierala } 1402009ea47eSEdward Tomasz Napierala error = listen(newp->p_socket, -1); 1403009ea47eSEdward Tomasz Napierala if (error != 0) { 1404009ea47eSEdward Tomasz Napierala log_warn("listen(2) failed for %s", 1405009ea47eSEdward Tomasz Napierala newp->p_listen); 1406009ea47eSEdward Tomasz Napierala close(newp->p_socket); 1407009ea47eSEdward Tomasz Napierala newp->p_socket = 0; 1408009ea47eSEdward Tomasz Napierala cumulated_error++; 1409009ea47eSEdward Tomasz Napierala continue; 1410009ea47eSEdward Tomasz Napierala } 1411009ea47eSEdward Tomasz Napierala #endif /* !ICL_KERNEL_PROXY */ 1412009ea47eSEdward Tomasz Napierala } 1413009ea47eSEdward Tomasz Napierala } 1414009ea47eSEdward Tomasz Napierala 1415009ea47eSEdward Tomasz Napierala /* 1416009ea47eSEdward Tomasz Napierala * Go through the no longer used sockets, closing them. 1417009ea47eSEdward Tomasz Napierala */ 1418009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 1419009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 1420009ea47eSEdward Tomasz Napierala if (oldp->p_socket <= 0) 1421009ea47eSEdward Tomasz Napierala continue; 1422009ea47eSEdward Tomasz Napierala log_debugx("closing socket for %s, portal-group \"%s\"", 1423009ea47eSEdward Tomasz Napierala oldp->p_listen, oldpg->pg_name); 1424009ea47eSEdward Tomasz Napierala close(oldp->p_socket); 1425009ea47eSEdward Tomasz Napierala oldp->p_socket = 0; 1426009ea47eSEdward Tomasz Napierala } 1427009ea47eSEdward Tomasz Napierala } 1428009ea47eSEdward Tomasz Napierala 1429009ea47eSEdward Tomasz Napierala return (cumulated_error); 1430009ea47eSEdward Tomasz Napierala } 1431009ea47eSEdward Tomasz Napierala 1432009ea47eSEdward Tomasz Napierala bool 1433009ea47eSEdward Tomasz Napierala timed_out(void) 1434009ea47eSEdward Tomasz Napierala { 1435009ea47eSEdward Tomasz Napierala 1436009ea47eSEdward Tomasz Napierala return (sigalrm_received); 1437009ea47eSEdward Tomasz Napierala } 1438009ea47eSEdward Tomasz Napierala 1439009ea47eSEdward Tomasz Napierala static void 1440009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused) 1441009ea47eSEdward Tomasz Napierala { 1442009ea47eSEdward Tomasz Napierala /* 1443009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 1444009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 1445009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 1446009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 1447009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 1448009ea47eSEdward Tomasz Napierala */ 1449009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 1450009ea47eSEdward Tomasz Napierala /* 1451009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 1452009ea47eSEdward Tomasz Napierala */ 1453009ea47eSEdward Tomasz Napierala _exit(2); 1454009ea47eSEdward Tomasz Napierala } 1455009ea47eSEdward Tomasz Napierala 1456009ea47eSEdward Tomasz Napierala sigalrm_received = true; 1457009ea47eSEdward Tomasz Napierala } 1458009ea47eSEdward Tomasz Napierala 1459009ea47eSEdward Tomasz Napierala static void 1460009ea47eSEdward Tomasz Napierala set_timeout(const struct conf *conf) 1461009ea47eSEdward Tomasz Napierala { 1462009ea47eSEdward Tomasz Napierala struct sigaction sa; 1463009ea47eSEdward Tomasz Napierala struct itimerval itv; 1464009ea47eSEdward Tomasz Napierala int error; 1465009ea47eSEdward Tomasz Napierala 1466009ea47eSEdward Tomasz Napierala if (conf->conf_timeout <= 0) { 1467009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 1468009ea47eSEdward Tomasz Napierala return; 1469009ea47eSEdward Tomasz Napierala } 1470009ea47eSEdward Tomasz Napierala 1471009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 1472009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 1473009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 1474009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 1475009ea47eSEdward Tomasz Napierala if (error != 0) 1476009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 1477009ea47eSEdward Tomasz Napierala 1478009ea47eSEdward Tomasz Napierala /* 1479009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 1480009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 1481009ea47eSEdward Tomasz Napierala */ 1482009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 1483009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 1484009ea47eSEdward Tomasz Napierala itv.it_value.tv_sec = conf->conf_timeout; 1485009ea47eSEdward Tomasz Napierala 1486009ea47eSEdward Tomasz Napierala log_debugx("setting session timeout to %d seconds", 1487009ea47eSEdward Tomasz Napierala conf->conf_timeout); 1488009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 1489009ea47eSEdward Tomasz Napierala if (error != 0) 1490009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 1491009ea47eSEdward Tomasz Napierala } 1492009ea47eSEdward Tomasz Napierala 1493009ea47eSEdward Tomasz Napierala static int 1494009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 1495009ea47eSEdward Tomasz Napierala { 1496009ea47eSEdward Tomasz Napierala pid_t pid; 1497009ea47eSEdward Tomasz Napierala int status; 1498009ea47eSEdward Tomasz Napierala int num = 0; 1499009ea47eSEdward Tomasz Napierala 1500009ea47eSEdward Tomasz Napierala for (;;) { 1501009ea47eSEdward Tomasz Napierala /* 1502009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 1503009ea47eSEdward Tomasz Napierala */ 1504009ea47eSEdward Tomasz Napierala if (block && num == 0) 1505009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 1506009ea47eSEdward Tomasz Napierala else 1507009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 1508009ea47eSEdward Tomasz Napierala if (pid <= 0) 1509009ea47eSEdward Tomasz Napierala break; 1510009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 1511009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 1512009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 1513009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 1514009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 1515009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 1516009ea47eSEdward Tomasz Napierala } else { 1517009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 1518009ea47eSEdward Tomasz Napierala } 1519009ea47eSEdward Tomasz Napierala num++; 1520009ea47eSEdward Tomasz Napierala } 1521009ea47eSEdward Tomasz Napierala 1522009ea47eSEdward Tomasz Napierala return (num); 1523009ea47eSEdward Tomasz Napierala } 1524009ea47eSEdward Tomasz Napierala 1525009ea47eSEdward Tomasz Napierala static void 1526009ea47eSEdward Tomasz Napierala handle_connection(struct portal *portal, int fd, bool dont_fork) 1527009ea47eSEdward Tomasz Napierala { 1528009ea47eSEdward Tomasz Napierala struct connection *conn; 1529009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 1530009ea47eSEdward Tomasz Napierala struct sockaddr_storage ss; 1531009ea47eSEdward Tomasz Napierala socklen_t sslen = sizeof(ss); 1532009ea47eSEdward Tomasz Napierala int error; 1533009ea47eSEdward Tomasz Napierala #endif 1534009ea47eSEdward Tomasz Napierala pid_t pid; 1535009ea47eSEdward Tomasz Napierala char host[NI_MAXHOST + 1]; 1536009ea47eSEdward Tomasz Napierala struct conf *conf; 1537009ea47eSEdward Tomasz Napierala 1538009ea47eSEdward Tomasz Napierala conf = portal->p_portal_group->pg_conf; 1539009ea47eSEdward Tomasz Napierala 1540009ea47eSEdward Tomasz Napierala if (dont_fork) { 1541009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; not forking due to -d flag"); 1542009ea47eSEdward Tomasz Napierala } else { 1543009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 1544009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 1545009ea47eSEdward Tomasz Napierala 1546009ea47eSEdward Tomasz Napierala while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 1547009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 1548009ea47eSEdward Tomasz Napierala "waiting for child process to exit", conf->conf_maxproc); 1549009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 1550009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 1551009ea47eSEdward Tomasz Napierala } 1552009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 1553009ea47eSEdward Tomasz Napierala nchildren); 1554009ea47eSEdward Tomasz Napierala nchildren++; 1555009ea47eSEdward Tomasz Napierala pid = fork(); 1556009ea47eSEdward Tomasz Napierala if (pid < 0) 1557009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 1558009ea47eSEdward Tomasz Napierala if (pid > 0) { 1559009ea47eSEdward Tomasz Napierala close(fd); 1560009ea47eSEdward Tomasz Napierala return; 1561009ea47eSEdward Tomasz Napierala } 1562009ea47eSEdward Tomasz Napierala } 1563009ea47eSEdward Tomasz Napierala pidfile_close(conf->conf_pidfh); 1564009ea47eSEdward Tomasz Napierala 1565009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1566009ea47eSEdward Tomasz Napierala /* 1567009ea47eSEdward Tomasz Napierala * XXX 1568009ea47eSEdward Tomasz Napierala */ 1569009ea47eSEdward Tomasz Napierala log_set_peer_addr("XXX"); 1570009ea47eSEdward Tomasz Napierala #else 1571009ea47eSEdward Tomasz Napierala error = getpeername(fd, (struct sockaddr *)&ss, &sslen); 1572009ea47eSEdward Tomasz Napierala if (error != 0) 1573009ea47eSEdward Tomasz Napierala log_err(1, "getpeername"); 1574009ea47eSEdward Tomasz Napierala error = getnameinfo((struct sockaddr *)&ss, sslen, 1575009ea47eSEdward Tomasz Napierala host, sizeof(host), NULL, 0, NI_NUMERICHOST); 1576009ea47eSEdward Tomasz Napierala if (error != 0) 1577009ea47eSEdward Tomasz Napierala log_errx(1, "getaddrinfo: %s", gai_strerror(error)); 1578009ea47eSEdward Tomasz Napierala 1579009ea47eSEdward Tomasz Napierala log_debugx("accepted connection from %s; portal group \"%s\"", 1580009ea47eSEdward Tomasz Napierala host, portal->p_portal_group->pg_name); 1581009ea47eSEdward Tomasz Napierala log_set_peer_addr(host); 1582009ea47eSEdward Tomasz Napierala setproctitle("%s", host); 1583009ea47eSEdward Tomasz Napierala #endif 1584009ea47eSEdward Tomasz Napierala 1585009ea47eSEdward Tomasz Napierala conn = connection_new(portal, fd, host); 1586009ea47eSEdward Tomasz Napierala set_timeout(conf); 1587009ea47eSEdward Tomasz Napierala kernel_capsicate(); 1588009ea47eSEdward Tomasz Napierala login(conn); 1589009ea47eSEdward Tomasz Napierala if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 1590009ea47eSEdward Tomasz Napierala kernel_handoff(conn); 1591009ea47eSEdward Tomasz Napierala log_debugx("connection handed off to the kernel"); 1592009ea47eSEdward Tomasz Napierala } else { 1593009ea47eSEdward Tomasz Napierala assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 1594009ea47eSEdward Tomasz Napierala discovery(conn); 1595009ea47eSEdward Tomasz Napierala } 1596009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 1597009ea47eSEdward Tomasz Napierala exit(0); 1598009ea47eSEdward Tomasz Napierala } 1599009ea47eSEdward Tomasz Napierala 1600009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 1601009ea47eSEdward Tomasz Napierala static int 1602009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds) 1603009ea47eSEdward Tomasz Napierala { 1604009ea47eSEdward Tomasz Napierala 1605009ea47eSEdward Tomasz Napierala /* 1606009ea47eSEdward Tomasz Napierala * Skip sockets which we failed to bind. 1607009ea47eSEdward Tomasz Napierala */ 1608009ea47eSEdward Tomasz Napierala if (fd <= 0) 1609009ea47eSEdward Tomasz Napierala return (nfds); 1610009ea47eSEdward Tomasz Napierala 1611009ea47eSEdward Tomasz Napierala FD_SET(fd, fdset); 1612009ea47eSEdward Tomasz Napierala if (fd > nfds) 1613009ea47eSEdward Tomasz Napierala nfds = fd; 1614009ea47eSEdward Tomasz Napierala return (nfds); 1615009ea47eSEdward Tomasz Napierala } 1616009ea47eSEdward Tomasz Napierala #endif 1617009ea47eSEdward Tomasz Napierala 1618009ea47eSEdward Tomasz Napierala static void 1619009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork) 1620009ea47eSEdward Tomasz Napierala { 1621009ea47eSEdward Tomasz Napierala struct portal_group *pg; 1622009ea47eSEdward Tomasz Napierala struct portal *portal; 1623009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1624009ea47eSEdward Tomasz Napierala int connection_id; 1625009ea47eSEdward Tomasz Napierala #else 1626009ea47eSEdward Tomasz Napierala fd_set fdset; 1627009ea47eSEdward Tomasz Napierala int error, nfds, client_fd; 1628009ea47eSEdward Tomasz Napierala #endif 1629009ea47eSEdward Tomasz Napierala 1630009ea47eSEdward Tomasz Napierala pidfile_write(conf->conf_pidfh); 1631009ea47eSEdward Tomasz Napierala 1632009ea47eSEdward Tomasz Napierala for (;;) { 1633009ea47eSEdward Tomasz Napierala if (sighup_received || sigterm_received) 1634009ea47eSEdward Tomasz Napierala return; 1635009ea47eSEdward Tomasz Napierala 1636009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1637009ea47eSEdward Tomasz Napierala connection_id = kernel_accept(); 1638009ea47eSEdward Tomasz Napierala if (connection_id == 0) 1639009ea47eSEdward Tomasz Napierala continue; 1640009ea47eSEdward Tomasz Napierala 1641009ea47eSEdward Tomasz Napierala /* 1642009ea47eSEdward Tomasz Napierala * XXX: This is obviously temporary. 1643009ea47eSEdward Tomasz Napierala */ 1644009ea47eSEdward Tomasz Napierala pg = TAILQ_FIRST(&conf->conf_portal_groups); 1645009ea47eSEdward Tomasz Napierala portal = TAILQ_FIRST(&pg->pg_portals); 1646009ea47eSEdward Tomasz Napierala 1647009ea47eSEdward Tomasz Napierala handle_connection(portal, connection_id, dont_fork); 1648009ea47eSEdward Tomasz Napierala #else 1649009ea47eSEdward Tomasz Napierala FD_ZERO(&fdset); 1650009ea47eSEdward Tomasz Napierala nfds = 0; 1651009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1652009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1653009ea47eSEdward Tomasz Napierala nfds = fd_add(portal->p_socket, &fdset, nfds); 1654009ea47eSEdward Tomasz Napierala } 1655009ea47eSEdward Tomasz Napierala error = select(nfds + 1, &fdset, NULL, NULL, NULL); 1656009ea47eSEdward Tomasz Napierala if (error <= 0) { 1657009ea47eSEdward Tomasz Napierala if (errno == EINTR) 1658009ea47eSEdward Tomasz Napierala return; 1659009ea47eSEdward Tomasz Napierala log_err(1, "select"); 1660009ea47eSEdward Tomasz Napierala } 1661009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1662009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 1663009ea47eSEdward Tomasz Napierala if (!FD_ISSET(portal->p_socket, &fdset)) 1664009ea47eSEdward Tomasz Napierala continue; 1665009ea47eSEdward Tomasz Napierala client_fd = accept(portal->p_socket, NULL, 0); 1666009ea47eSEdward Tomasz Napierala if (client_fd < 0) 1667009ea47eSEdward Tomasz Napierala log_err(1, "accept"); 1668009ea47eSEdward Tomasz Napierala handle_connection(portal, client_fd, dont_fork); 1669009ea47eSEdward Tomasz Napierala break; 1670009ea47eSEdward Tomasz Napierala } 1671009ea47eSEdward Tomasz Napierala } 1672009ea47eSEdward Tomasz Napierala #endif /* !ICL_KERNEL_PROXY */ 1673009ea47eSEdward Tomasz Napierala } 1674009ea47eSEdward Tomasz Napierala } 1675009ea47eSEdward Tomasz Napierala 1676009ea47eSEdward Tomasz Napierala static void 1677009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused) 1678009ea47eSEdward Tomasz Napierala { 1679009ea47eSEdward Tomasz Napierala 1680009ea47eSEdward Tomasz Napierala sighup_received = true; 1681009ea47eSEdward Tomasz Napierala } 1682009ea47eSEdward Tomasz Napierala 1683009ea47eSEdward Tomasz Napierala static void 1684009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused) 1685009ea47eSEdward Tomasz Napierala { 1686009ea47eSEdward Tomasz Napierala 1687009ea47eSEdward Tomasz Napierala sigterm_received = true; 1688009ea47eSEdward Tomasz Napierala } 1689009ea47eSEdward Tomasz Napierala 1690009ea47eSEdward Tomasz Napierala static void 1691009ea47eSEdward Tomasz Napierala register_signals(void) 1692009ea47eSEdward Tomasz Napierala { 1693009ea47eSEdward Tomasz Napierala struct sigaction sa; 1694009ea47eSEdward Tomasz Napierala int error; 1695009ea47eSEdward Tomasz Napierala 1696009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 1697009ea47eSEdward Tomasz Napierala sa.sa_handler = sighup_handler; 1698009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 1699009ea47eSEdward Tomasz Napierala error = sigaction(SIGHUP, &sa, NULL); 1700009ea47eSEdward Tomasz Napierala if (error != 0) 1701009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 1702009ea47eSEdward Tomasz Napierala 1703009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 1704009ea47eSEdward Tomasz Napierala error = sigaction(SIGTERM, &sa, NULL); 1705009ea47eSEdward Tomasz Napierala if (error != 0) 1706009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 1707009ea47eSEdward Tomasz Napierala 1708009ea47eSEdward Tomasz Napierala sa.sa_handler = sigterm_handler; 1709009ea47eSEdward Tomasz Napierala error = sigaction(SIGINT, &sa, NULL); 1710009ea47eSEdward Tomasz Napierala if (error != 0) 1711009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 1712009ea47eSEdward Tomasz Napierala } 1713009ea47eSEdward Tomasz Napierala 1714009ea47eSEdward Tomasz Napierala int 1715009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 1716009ea47eSEdward Tomasz Napierala { 1717009ea47eSEdward Tomasz Napierala struct conf *oldconf, *newconf, *tmpconf; 1718009ea47eSEdward Tomasz Napierala const char *config_path = DEFAULT_CONFIG_PATH; 1719009ea47eSEdward Tomasz Napierala int debug = 0, ch, error; 1720009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 1721009ea47eSEdward Tomasz Napierala 1722009ea47eSEdward Tomasz Napierala while ((ch = getopt(argc, argv, "df:")) != -1) { 1723009ea47eSEdward Tomasz Napierala switch (ch) { 1724009ea47eSEdward Tomasz Napierala case 'd': 1725009ea47eSEdward Tomasz Napierala dont_daemonize = true; 1726009ea47eSEdward Tomasz Napierala debug++; 1727009ea47eSEdward Tomasz Napierala break; 1728009ea47eSEdward Tomasz Napierala case 'f': 1729009ea47eSEdward Tomasz Napierala config_path = optarg; 1730009ea47eSEdward Tomasz Napierala break; 1731009ea47eSEdward Tomasz Napierala case '?': 1732009ea47eSEdward Tomasz Napierala default: 1733009ea47eSEdward Tomasz Napierala usage(); 1734009ea47eSEdward Tomasz Napierala } 1735009ea47eSEdward Tomasz Napierala } 1736009ea47eSEdward Tomasz Napierala argc -= optind; 1737009ea47eSEdward Tomasz Napierala if (argc != 0) 1738009ea47eSEdward Tomasz Napierala usage(); 1739009ea47eSEdward Tomasz Napierala 1740009ea47eSEdward Tomasz Napierala log_init(debug); 1741009ea47eSEdward Tomasz Napierala kernel_init(); 1742009ea47eSEdward Tomasz Napierala 1743009ea47eSEdward Tomasz Napierala oldconf = conf_new_from_kernel(); 1744009ea47eSEdward Tomasz Napierala newconf = conf_new_from_file(config_path); 1745009ea47eSEdward Tomasz Napierala if (newconf == NULL) 1746009ea47eSEdward Tomasz Napierala log_errx(1, "configuration error, exiting"); 1747009ea47eSEdward Tomasz Napierala if (debug > 0) { 1748009ea47eSEdward Tomasz Napierala oldconf->conf_debug = debug; 1749009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 1750009ea47eSEdward Tomasz Napierala } 1751009ea47eSEdward Tomasz Napierala 1752009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1753009ea47eSEdward Tomasz Napierala log_debugx("enabling CTL iSCSI port"); 1754009ea47eSEdward Tomasz Napierala error = kernel_port_on(); 1755009ea47eSEdward Tomasz Napierala if (error != 0) 1756009ea47eSEdward Tomasz Napierala log_errx(1, "failed to enable CTL iSCSI port, exiting"); 1757009ea47eSEdward Tomasz Napierala #endif 1758009ea47eSEdward Tomasz Napierala 1759009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 1760009ea47eSEdward Tomasz Napierala if (error != 0) 1761009ea47eSEdward Tomasz Napierala log_errx(1, "failed to apply configuration, exiting"); 1762009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 1763009ea47eSEdward Tomasz Napierala oldconf = NULL; 1764009ea47eSEdward Tomasz Napierala 1765009ea47eSEdward Tomasz Napierala register_signals(); 1766009ea47eSEdward Tomasz Napierala 1767009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY 1768009ea47eSEdward Tomasz Napierala log_debugx("enabling CTL iSCSI port"); 1769009ea47eSEdward Tomasz Napierala error = kernel_port_on(); 1770009ea47eSEdward Tomasz Napierala if (error != 0) 1771009ea47eSEdward Tomasz Napierala log_errx(1, "failed to enable CTL iSCSI port, exiting"); 1772009ea47eSEdward Tomasz Napierala #endif 1773009ea47eSEdward Tomasz Napierala 17745d5a95f1SEdward Tomasz Napierala if (dont_daemonize == false) { 17755d5a95f1SEdward Tomasz Napierala log_debugx("daemonizing"); 17765d5a95f1SEdward Tomasz Napierala if (daemon(0, 0) == -1) { 17775d5a95f1SEdward Tomasz Napierala log_warn("cannot daemonize"); 17785d5a95f1SEdward Tomasz Napierala pidfile_remove(newconf->conf_pidfh); 17795d5a95f1SEdward Tomasz Napierala exit(1); 17805d5a95f1SEdward Tomasz Napierala } 17815d5a95f1SEdward Tomasz Napierala } 17825d5a95f1SEdward Tomasz Napierala 1783009ea47eSEdward Tomasz Napierala for (;;) { 1784009ea47eSEdward Tomasz Napierala main_loop(newconf, dont_daemonize); 1785009ea47eSEdward Tomasz Napierala if (sighup_received) { 1786009ea47eSEdward Tomasz Napierala sighup_received = false; 1787009ea47eSEdward Tomasz Napierala log_debugx("received SIGHUP, reloading configuration"); 1788009ea47eSEdward Tomasz Napierala tmpconf = conf_new_from_file(config_path); 1789009ea47eSEdward Tomasz Napierala if (tmpconf == NULL) { 1790009ea47eSEdward Tomasz Napierala log_warnx("configuration error, " 1791009ea47eSEdward Tomasz Napierala "continuing with old configuration"); 1792009ea47eSEdward Tomasz Napierala } else { 1793009ea47eSEdward Tomasz Napierala if (debug > 0) 1794009ea47eSEdward Tomasz Napierala tmpconf->conf_debug = debug; 1795009ea47eSEdward Tomasz Napierala oldconf = newconf; 1796009ea47eSEdward Tomasz Napierala newconf = tmpconf; 1797009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 1798009ea47eSEdward Tomasz Napierala if (error != 0) 1799009ea47eSEdward Tomasz Napierala log_warnx("failed to reload " 1800009ea47eSEdward Tomasz Napierala "configuration"); 1801009ea47eSEdward Tomasz Napierala conf_delete(oldconf); 1802009ea47eSEdward Tomasz Napierala oldconf = NULL; 1803009ea47eSEdward Tomasz Napierala } 1804009ea47eSEdward Tomasz Napierala } else if (sigterm_received) { 1805009ea47eSEdward Tomasz Napierala log_debugx("exiting on signal; " 1806009ea47eSEdward Tomasz Napierala "reloading empty configuration"); 1807009ea47eSEdward Tomasz Napierala 1808009ea47eSEdward Tomasz Napierala log_debugx("disabling CTL iSCSI port " 1809009ea47eSEdward Tomasz Napierala "and terminating all connections"); 1810009ea47eSEdward Tomasz Napierala error = kernel_port_off(); 1811009ea47eSEdward Tomasz Napierala if (error != 0) 1812009ea47eSEdward Tomasz Napierala log_warnx("failed to disable CTL iSCSI port"); 1813009ea47eSEdward Tomasz Napierala 1814009ea47eSEdward Tomasz Napierala oldconf = newconf; 1815009ea47eSEdward Tomasz Napierala newconf = conf_new(); 1816009ea47eSEdward Tomasz Napierala if (debug > 0) 1817009ea47eSEdward Tomasz Napierala newconf->conf_debug = debug; 1818009ea47eSEdward Tomasz Napierala error = conf_apply(oldconf, newconf); 1819009ea47eSEdward Tomasz Napierala if (error != 0) 1820009ea47eSEdward Tomasz Napierala log_warnx("failed to apply configuration"); 1821009ea47eSEdward Tomasz Napierala 1822009ea47eSEdward Tomasz Napierala log_warnx("exiting on signal"); 1823009ea47eSEdward Tomasz Napierala exit(0); 1824009ea47eSEdward Tomasz Napierala } else { 1825009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 1826009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 1827009ea47eSEdward Tomasz Napierala } 1828009ea47eSEdward Tomasz Napierala } 1829009ea47eSEdward Tomasz Napierala /* NOTREACHED */ 1830009ea47eSEdward Tomasz Napierala } 1831