ctld.c revision 281488
1255570Strasz/*- 2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation 3255570Strasz * All rights reserved. 4255570Strasz * 5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship 6255570Strasz * from the FreeBSD Foundation. 7255570Strasz * 8255570Strasz * Redistribution and use in source and binary forms, with or without 9255570Strasz * modification, are permitted provided that the following conditions 10255570Strasz * are met: 11255570Strasz * 1. Redistributions of source code must retain the above copyright 12255570Strasz * notice, this list of conditions and the following disclaimer. 13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright 14255570Strasz * notice, this list of conditions and the following disclaimer in the 15255570Strasz * documentation and/or other materials provided with the distribution. 16255570Strasz * 17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20255570Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27255570Strasz * SUCH DAMAGE. 28255570Strasz * 29255570Strasz */ 30255570Strasz 31270888Strasz#include <sys/cdefs.h> 32270888Strasz__FBSDID("$FreeBSD: stable/10/usr.sbin/ctld/ctld.c 281488 2015-04-13 09:18:56Z mav $"); 33270888Strasz 34255570Strasz#include <sys/types.h> 35255570Strasz#include <sys/time.h> 36255570Strasz#include <sys/socket.h> 37255570Strasz#include <sys/wait.h> 38255570Strasz#include <netinet/in.h> 39270137Smav#include <arpa/inet.h> 40255570Strasz#include <assert.h> 41255570Strasz#include <ctype.h> 42255570Strasz#include <errno.h> 43255570Strasz#include <netdb.h> 44255570Strasz#include <signal.h> 45255570Strasz#include <stdbool.h> 46255570Strasz#include <stdio.h> 47255570Strasz#include <stdint.h> 48255570Strasz#include <stdlib.h> 49255570Strasz#include <string.h> 50255570Strasz#include <unistd.h> 51255570Strasz 52255570Strasz#include "ctld.h" 53274939Smav#include "isns.h" 54255570Strasz 55265507Straszbool proxy_mode = false; 56265507Strasz 57255570Straszstatic volatile bool sighup_received = false; 58255570Straszstatic volatile bool sigterm_received = false; 59255570Straszstatic volatile bool sigalrm_received = false; 60255570Strasz 61255570Straszstatic int nchildren = 0; 62279003Smavstatic uint16_t last_portal_group_tag = 0; 63255570Strasz 64255570Straszstatic void 65255570Straszusage(void) 66255570Strasz{ 67255570Strasz 68255570Strasz fprintf(stderr, "usage: ctld [-d][-f config-file]\n"); 69255570Strasz exit(1); 70255570Strasz} 71255570Strasz 72255570Straszchar * 73255570Straszchecked_strdup(const char *s) 74255570Strasz{ 75255570Strasz char *c; 76255570Strasz 77255570Strasz c = strdup(s); 78255570Strasz if (c == NULL) 79255570Strasz log_err(1, "strdup"); 80255570Strasz return (c); 81255570Strasz} 82255570Strasz 83255570Straszstruct conf * 84255570Straszconf_new(void) 85255570Strasz{ 86255570Strasz struct conf *conf; 87255570Strasz 88255570Strasz conf = calloc(1, sizeof(*conf)); 89255570Strasz if (conf == NULL) 90255570Strasz log_err(1, "calloc"); 91279002Smav TAILQ_INIT(&conf->conf_luns); 92255570Strasz TAILQ_INIT(&conf->conf_targets); 93255570Strasz TAILQ_INIT(&conf->conf_auth_groups); 94279006Smav TAILQ_INIT(&conf->conf_ports); 95255570Strasz TAILQ_INIT(&conf->conf_portal_groups); 96279055Smav TAILQ_INIT(&conf->conf_pports); 97274939Smav TAILQ_INIT(&conf->conf_isns); 98255570Strasz 99274939Smav conf->conf_isns_period = 900; 100274939Smav conf->conf_isns_timeout = 5; 101255570Strasz conf->conf_debug = 0; 102255570Strasz conf->conf_timeout = 60; 103255570Strasz conf->conf_maxproc = 30; 104255570Strasz 105255570Strasz return (conf); 106255570Strasz} 107255570Strasz 108255570Straszvoid 109255570Straszconf_delete(struct conf *conf) 110255570Strasz{ 111279002Smav struct lun *lun, *ltmp; 112255570Strasz struct target *targ, *tmp; 113255570Strasz struct auth_group *ag, *cagtmp; 114255570Strasz struct portal_group *pg, *cpgtmp; 115279055Smav struct pport *pp, *pptmp; 116274939Smav struct isns *is, *istmp; 117255570Strasz 118255570Strasz assert(conf->conf_pidfh == NULL); 119255570Strasz 120279002Smav TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp) 121279002Smav lun_delete(lun); 122255570Strasz TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp) 123255570Strasz target_delete(targ); 124255570Strasz TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp) 125255570Strasz auth_group_delete(ag); 126255570Strasz TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp) 127255570Strasz portal_group_delete(pg); 128279055Smav TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp) 129279055Smav pport_delete(pp); 130274939Smav TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp) 131274939Smav isns_delete(is); 132279006Smav assert(TAILQ_EMPTY(&conf->conf_ports)); 133255570Strasz free(conf->conf_pidfile_path); 134255570Strasz free(conf); 135255570Strasz} 136255570Strasz 137255570Straszstatic struct auth * 138255570Straszauth_new(struct auth_group *ag) 139255570Strasz{ 140255570Strasz struct auth *auth; 141255570Strasz 142255570Strasz auth = calloc(1, sizeof(*auth)); 143255570Strasz if (auth == NULL) 144255570Strasz log_err(1, "calloc"); 145255570Strasz auth->a_auth_group = ag; 146255570Strasz TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); 147255570Strasz return (auth); 148255570Strasz} 149255570Strasz 150255570Straszstatic void 151255570Straszauth_delete(struct auth *auth) 152255570Strasz{ 153255570Strasz TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); 154255570Strasz 155255570Strasz free(auth->a_user); 156255570Strasz free(auth->a_secret); 157255570Strasz free(auth->a_mutual_user); 158255570Strasz free(auth->a_mutual_secret); 159255570Strasz free(auth); 160255570Strasz} 161255570Strasz 162255570Straszconst struct auth * 163265514Straszauth_find(const struct auth_group *ag, const char *user) 164255570Strasz{ 165255570Strasz const struct auth *auth; 166255570Strasz 167255570Strasz TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { 168255570Strasz if (strcmp(auth->a_user, user) == 0) 169255570Strasz return (auth); 170255570Strasz } 171255570Strasz 172255570Strasz return (NULL); 173255570Strasz} 174255570Strasz 175263721Straszstatic void 176263721Straszauth_check_secret_length(struct auth *auth) 177263721Strasz{ 178263721Strasz size_t len; 179263721Strasz 180263721Strasz len = strlen(auth->a_secret); 181263721Strasz if (len > 16) { 182263721Strasz if (auth->a_auth_group->ag_name != NULL) 183263721Strasz log_warnx("secret for user \"%s\", auth-group \"%s\", " 184263721Strasz "is too long; it should be at most 16 characters " 185263721Strasz "long", auth->a_user, auth->a_auth_group->ag_name); 186263721Strasz else 187263721Strasz log_warnx("secret for user \"%s\", target \"%s\", " 188263721Strasz "is too long; it should be at most 16 characters " 189263721Strasz "long", auth->a_user, 190263723Strasz auth->a_auth_group->ag_target->t_name); 191263721Strasz } 192263721Strasz if (len < 12) { 193263721Strasz if (auth->a_auth_group->ag_name != NULL) 194263721Strasz log_warnx("secret for user \"%s\", auth-group \"%s\", " 195263721Strasz "is too short; it should be at least 12 characters " 196263721Strasz "long", auth->a_user, 197263721Strasz auth->a_auth_group->ag_name); 198263721Strasz else 199263721Strasz log_warnx("secret for user \"%s\", target \"%s\", " 200263721Strasz "is too short; it should be at least 16 characters " 201263721Strasz "long", auth->a_user, 202263723Strasz auth->a_auth_group->ag_target->t_name); 203263721Strasz } 204263721Strasz 205263721Strasz if (auth->a_mutual_secret != NULL) { 206281187Sjpaetzel len = strlen(auth->a_mutual_secret); 207263721Strasz if (len > 16) { 208263721Strasz if (auth->a_auth_group->ag_name != NULL) 209263721Strasz log_warnx("mutual secret for user \"%s\", " 210263721Strasz "auth-group \"%s\", is too long; it should " 211263721Strasz "be at most 16 characters long", 212263721Strasz auth->a_user, auth->a_auth_group->ag_name); 213263721Strasz else 214263721Strasz log_warnx("mutual secret for user \"%s\", " 215263721Strasz "target \"%s\", is too long; it should " 216263721Strasz "be at most 16 characters long", 217263721Strasz auth->a_user, 218263723Strasz auth->a_auth_group->ag_target->t_name); 219263721Strasz } 220263721Strasz if (len < 12) { 221263721Strasz if (auth->a_auth_group->ag_name != NULL) 222263721Strasz log_warnx("mutual secret for user \"%s\", " 223263721Strasz "auth-group \"%s\", is too short; it " 224263721Strasz "should be at least 12 characters long", 225263721Strasz auth->a_user, auth->a_auth_group->ag_name); 226263721Strasz else 227263721Strasz log_warnx("mutual secret for user \"%s\", " 228263721Strasz "target \"%s\", is too short; it should be " 229263721Strasz "at least 16 characters long", 230263721Strasz auth->a_user, 231263723Strasz auth->a_auth_group->ag_target->t_name); 232263721Strasz } 233263721Strasz } 234263721Strasz} 235263721Strasz 236263721Straszconst struct auth * 237263721Straszauth_new_chap(struct auth_group *ag, const char *user, 238263721Strasz const char *secret) 239263721Strasz{ 240263721Strasz struct auth *auth; 241263721Strasz 242263721Strasz if (ag->ag_type == AG_TYPE_UNKNOWN) 243263721Strasz ag->ag_type = AG_TYPE_CHAP; 244263721Strasz if (ag->ag_type != AG_TYPE_CHAP) { 245263721Strasz if (ag->ag_name != NULL) 246263721Strasz log_warnx("cannot mix \"chap\" authentication with " 247263721Strasz "other types for auth-group \"%s\"", ag->ag_name); 248263721Strasz else 249263721Strasz log_warnx("cannot mix \"chap\" authentication with " 250263721Strasz "other types for target \"%s\"", 251263723Strasz ag->ag_target->t_name); 252263721Strasz return (NULL); 253263721Strasz } 254263721Strasz 255263721Strasz auth = auth_new(ag); 256263721Strasz auth->a_user = checked_strdup(user); 257263721Strasz auth->a_secret = checked_strdup(secret); 258263721Strasz 259263721Strasz auth_check_secret_length(auth); 260263721Strasz 261263721Strasz return (auth); 262263721Strasz} 263263721Strasz 264263721Straszconst struct auth * 265263721Straszauth_new_chap_mutual(struct auth_group *ag, const char *user, 266263721Strasz const char *secret, const char *user2, const char *secret2) 267263721Strasz{ 268263721Strasz struct auth *auth; 269263721Strasz 270263721Strasz if (ag->ag_type == AG_TYPE_UNKNOWN) 271263721Strasz ag->ag_type = AG_TYPE_CHAP_MUTUAL; 272263721Strasz if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 273263721Strasz if (ag->ag_name != NULL) 274263721Strasz log_warnx("cannot mix \"chap-mutual\" authentication " 275263721Strasz "with other types for auth-group \"%s\"", 276274870Strasz ag->ag_name); 277263721Strasz else 278263721Strasz log_warnx("cannot mix \"chap-mutual\" authentication " 279263721Strasz "with other types for target \"%s\"", 280263723Strasz ag->ag_target->t_name); 281263721Strasz return (NULL); 282263721Strasz } 283263721Strasz 284263721Strasz auth = auth_new(ag); 285263721Strasz auth->a_user = checked_strdup(user); 286263721Strasz auth->a_secret = checked_strdup(secret); 287263721Strasz auth->a_mutual_user = checked_strdup(user2); 288263721Strasz auth->a_mutual_secret = checked_strdup(secret2); 289263721Strasz 290263721Strasz auth_check_secret_length(auth); 291263721Strasz 292263721Strasz return (auth); 293263721Strasz} 294263721Strasz 295263720Straszconst struct auth_name * 296263720Straszauth_name_new(struct auth_group *ag, const char *name) 297263720Strasz{ 298263720Strasz struct auth_name *an; 299263720Strasz 300263720Strasz an = calloc(1, sizeof(*an)); 301263720Strasz if (an == NULL) 302263720Strasz log_err(1, "calloc"); 303263720Strasz an->an_auth_group = ag; 304263720Strasz an->an_initator_name = checked_strdup(name); 305263720Strasz TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next); 306263720Strasz return (an); 307263720Strasz} 308263720Strasz 309263720Straszstatic void 310263720Straszauth_name_delete(struct auth_name *an) 311263720Strasz{ 312263720Strasz TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next); 313263720Strasz 314263720Strasz free(an->an_initator_name); 315263720Strasz free(an); 316263720Strasz} 317263720Strasz 318263720Straszbool 319263720Straszauth_name_defined(const struct auth_group *ag) 320263720Strasz{ 321263720Strasz if (TAILQ_EMPTY(&ag->ag_names)) 322263720Strasz return (false); 323263720Strasz return (true); 324263720Strasz} 325263720Strasz 326263720Straszconst struct auth_name * 327263720Straszauth_name_find(const struct auth_group *ag, const char *name) 328263720Strasz{ 329263720Strasz const struct auth_name *auth_name; 330263720Strasz 331263720Strasz TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) { 332263720Strasz if (strcmp(auth_name->an_initator_name, name) == 0) 333263720Strasz return (auth_name); 334263720Strasz } 335263720Strasz 336263720Strasz return (NULL); 337263720Strasz} 338263720Strasz 339274949Straszint 340274949Straszauth_name_check(const struct auth_group *ag, const char *initiator_name) 341274949Strasz{ 342274949Strasz if (!auth_name_defined(ag)) 343274949Strasz return (0); 344274949Strasz 345274949Strasz if (auth_name_find(ag, initiator_name) == NULL) 346274949Strasz return (1); 347274949Strasz 348274949Strasz return (0); 349274949Strasz} 350274949Strasz 351263720Straszconst struct auth_portal * 352263720Straszauth_portal_new(struct auth_group *ag, const char *portal) 353263720Strasz{ 354263720Strasz struct auth_portal *ap; 355270137Smav char *net, *mask, *str, *tmp; 356270137Smav int len, dm, m; 357263720Strasz 358263720Strasz ap = calloc(1, sizeof(*ap)); 359263720Strasz if (ap == NULL) 360263720Strasz log_err(1, "calloc"); 361263720Strasz ap->ap_auth_group = ag; 362263720Strasz ap->ap_initator_portal = checked_strdup(portal); 363270137Smav mask = str = checked_strdup(portal); 364270137Smav net = strsep(&mask, "/"); 365270137Smav if (net[0] == '[') 366270137Smav net++; 367270137Smav len = strlen(net); 368270137Smav if (len == 0) 369270137Smav goto error; 370270137Smav if (net[len - 1] == ']') 371270137Smav net[len - 1] = 0; 372270137Smav if (strchr(net, ':') != NULL) { 373270137Smav struct sockaddr_in6 *sin6 = 374270137Smav (struct sockaddr_in6 *)&ap->ap_sa; 375270137Smav 376270137Smav sin6->sin6_len = sizeof(*sin6); 377270137Smav sin6->sin6_family = AF_INET6; 378270137Smav if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0) 379270137Smav goto error; 380270137Smav dm = 128; 381270137Smav } else { 382270137Smav struct sockaddr_in *sin = 383270137Smav (struct sockaddr_in *)&ap->ap_sa; 384270137Smav 385270137Smav sin->sin_len = sizeof(*sin); 386270137Smav sin->sin_family = AF_INET; 387270137Smav if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0) 388270137Smav goto error; 389270137Smav dm = 32; 390270137Smav } 391270137Smav if (mask != NULL) { 392270137Smav m = strtol(mask, &tmp, 0); 393270137Smav if (m < 0 || m > dm || tmp[0] != 0) 394270137Smav goto error; 395270137Smav } else 396270137Smav m = dm; 397270137Smav ap->ap_mask = m; 398270137Smav free(str); 399263720Strasz TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next); 400263720Strasz return (ap); 401270137Smav 402270137Smaverror: 403279481Smav free(ap); 404270137Smav log_errx(1, "Incorrect initiator portal '%s'", portal); 405270137Smav return (NULL); 406263720Strasz} 407263720Strasz 408263720Straszstatic void 409263720Straszauth_portal_delete(struct auth_portal *ap) 410263720Strasz{ 411263720Strasz TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next); 412263720Strasz 413263720Strasz free(ap->ap_initator_portal); 414263720Strasz free(ap); 415263720Strasz} 416263720Strasz 417263720Straszbool 418263720Straszauth_portal_defined(const struct auth_group *ag) 419263720Strasz{ 420263720Strasz if (TAILQ_EMPTY(&ag->ag_portals)) 421263720Strasz return (false); 422263720Strasz return (true); 423263720Strasz} 424263720Strasz 425263720Straszconst struct auth_portal * 426270137Smavauth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss) 427263720Strasz{ 428270137Smav const struct auth_portal *ap; 429270137Smav const uint8_t *a, *b; 430270137Smav int i; 431270137Smav uint8_t bmask; 432263720Strasz 433270137Smav TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) { 434270137Smav if (ap->ap_sa.ss_family != ss->ss_family) 435270137Smav continue; 436270137Smav if (ss->ss_family == AF_INET) { 437270137Smav a = (const uint8_t *) 438270137Smav &((const struct sockaddr_in *)ss)->sin_addr; 439270137Smav b = (const uint8_t *) 440270137Smav &((const struct sockaddr_in *)&ap->ap_sa)->sin_addr; 441270137Smav } else { 442270137Smav a = (const uint8_t *) 443270137Smav &((const struct sockaddr_in6 *)ss)->sin6_addr; 444270137Smav b = (const uint8_t *) 445270137Smav &((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr; 446270137Smav } 447270137Smav for (i = 0; i < ap->ap_mask / 8; i++) { 448270137Smav if (a[i] != b[i]) 449270137Smav goto next; 450270137Smav } 451270137Smav if (ap->ap_mask % 8) { 452270137Smav bmask = 0xff << (8 - (ap->ap_mask % 8)); 453270137Smav if ((a[i] & bmask) != (b[i] & bmask)) 454270137Smav goto next; 455270137Smav } 456270137Smav return (ap); 457270137Smavnext: 458270137Smav ; 459263720Strasz } 460263720Strasz 461263720Strasz return (NULL); 462263720Strasz} 463263720Strasz 464274949Straszint 465274949Straszauth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa) 466274949Strasz{ 467274949Strasz 468274949Strasz if (!auth_portal_defined(ag)) 469274949Strasz return (0); 470274949Strasz 471274949Strasz if (auth_portal_find(ag, sa) == NULL) 472274949Strasz return (1); 473274949Strasz 474274949Strasz return (0); 475274949Strasz} 476274949Strasz 477255570Straszstruct auth_group * 478255570Straszauth_group_new(struct conf *conf, const char *name) 479255570Strasz{ 480255570Strasz struct auth_group *ag; 481255570Strasz 482255570Strasz if (name != NULL) { 483255570Strasz ag = auth_group_find(conf, name); 484255570Strasz if (ag != NULL) { 485255570Strasz log_warnx("duplicated auth-group \"%s\"", name); 486255570Strasz return (NULL); 487255570Strasz } 488255570Strasz } 489255570Strasz 490255570Strasz ag = calloc(1, sizeof(*ag)); 491255570Strasz if (ag == NULL) 492255570Strasz log_err(1, "calloc"); 493255570Strasz if (name != NULL) 494255570Strasz ag->ag_name = checked_strdup(name); 495255570Strasz TAILQ_INIT(&ag->ag_auths); 496263720Strasz TAILQ_INIT(&ag->ag_names); 497263720Strasz TAILQ_INIT(&ag->ag_portals); 498255570Strasz ag->ag_conf = conf; 499255570Strasz TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next); 500255570Strasz 501255570Strasz return (ag); 502255570Strasz} 503255570Strasz 504255570Straszvoid 505255570Straszauth_group_delete(struct auth_group *ag) 506255570Strasz{ 507263720Strasz struct auth *auth, *auth_tmp; 508263720Strasz struct auth_name *auth_name, *auth_name_tmp; 509263720Strasz struct auth_portal *auth_portal, *auth_portal_tmp; 510255570Strasz 511255570Strasz TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); 512255570Strasz 513263720Strasz TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) 514255570Strasz auth_delete(auth); 515263720Strasz TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) 516263720Strasz auth_name_delete(auth_name); 517263720Strasz TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, 518263720Strasz auth_portal_tmp) 519263720Strasz auth_portal_delete(auth_portal); 520255570Strasz free(ag->ag_name); 521255570Strasz free(ag); 522255570Strasz} 523255570Strasz 524255570Straszstruct auth_group * 525265514Straszauth_group_find(const struct conf *conf, const char *name) 526255570Strasz{ 527255570Strasz struct auth_group *ag; 528255570Strasz 529255570Strasz TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 530255570Strasz if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 531255570Strasz return (ag); 532255570Strasz } 533255570Strasz 534255570Strasz return (NULL); 535255570Strasz} 536255570Strasz 537263724Straszint 538275245Straszauth_group_set_type(struct auth_group *ag, const char *str) 539263724Strasz{ 540275245Strasz int type; 541263724Strasz 542263724Strasz if (strcmp(str, "none") == 0) { 543263724Strasz type = AG_TYPE_NO_AUTHENTICATION; 544263729Strasz } else if (strcmp(str, "deny") == 0) { 545263729Strasz type = AG_TYPE_DENY; 546263724Strasz } else if (strcmp(str, "chap") == 0) { 547263724Strasz type = AG_TYPE_CHAP; 548263724Strasz } else if (strcmp(str, "chap-mutual") == 0) { 549263724Strasz type = AG_TYPE_CHAP_MUTUAL; 550263724Strasz } else { 551263724Strasz if (ag->ag_name != NULL) 552263724Strasz log_warnx("invalid auth-type \"%s\" for auth-group " 553263724Strasz "\"%s\"", str, ag->ag_name); 554263724Strasz else 555263724Strasz log_warnx("invalid auth-type \"%s\" for target " 556263724Strasz "\"%s\"", str, ag->ag_target->t_name); 557263724Strasz return (1); 558263724Strasz } 559263724Strasz 560275245Strasz if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) { 561275245Strasz if (ag->ag_name != NULL) { 562263724Strasz log_warnx("cannot set auth-type to \"%s\" for " 563263724Strasz "auth-group \"%s\"; already has a different " 564263724Strasz "type", str, ag->ag_name); 565275245Strasz } else { 566263724Strasz log_warnx("cannot set auth-type to \"%s\" for target " 567263724Strasz "\"%s\"; already has a different type", 568263724Strasz str, ag->ag_target->t_name); 569275245Strasz } 570263724Strasz return (1); 571263724Strasz } 572263724Strasz 573275245Strasz ag->ag_type = type; 574275245Strasz 575275245Strasz return (0); 576263724Strasz} 577263724Strasz 578255570Straszstatic struct portal * 579255570Straszportal_new(struct portal_group *pg) 580255570Strasz{ 581255570Strasz struct portal *portal; 582255570Strasz 583255570Strasz portal = calloc(1, sizeof(*portal)); 584255570Strasz if (portal == NULL) 585255570Strasz log_err(1, "calloc"); 586255570Strasz TAILQ_INIT(&portal->p_targets); 587255570Strasz portal->p_portal_group = pg; 588255570Strasz TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 589255570Strasz return (portal); 590255570Strasz} 591255570Strasz 592255570Straszstatic void 593255570Straszportal_delete(struct portal *portal) 594255570Strasz{ 595271632Strasz 596255570Strasz TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 597271632Strasz if (portal->p_ai != NULL) 598271632Strasz freeaddrinfo(portal->p_ai); 599255570Strasz free(portal->p_listen); 600255570Strasz free(portal); 601255570Strasz} 602255570Strasz 603255570Straszstruct portal_group * 604255570Straszportal_group_new(struct conf *conf, const char *name) 605255570Strasz{ 606255570Strasz struct portal_group *pg; 607255570Strasz 608255678Strasz pg = portal_group_find(conf, name); 609255678Strasz if (pg != NULL) { 610255678Strasz log_warnx("duplicated portal-group \"%s\"", name); 611255678Strasz return (NULL); 612255570Strasz } 613255570Strasz 614255570Strasz pg = calloc(1, sizeof(*pg)); 615255570Strasz if (pg == NULL) 616255570Strasz log_err(1, "calloc"); 617255570Strasz pg->pg_name = checked_strdup(name); 618255570Strasz TAILQ_INIT(&pg->pg_portals); 619279006Smav TAILQ_INIT(&pg->pg_ports); 620255570Strasz pg->pg_conf = conf; 621279003Smav pg->pg_tag = 0; /* Assigned later in conf_apply(). */ 622255570Strasz TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 623255570Strasz 624255570Strasz return (pg); 625255570Strasz} 626255570Strasz 627255570Straszvoid 628255570Straszportal_group_delete(struct portal_group *pg) 629255570Strasz{ 630255570Strasz struct portal *portal, *tmp; 631279006Smav struct port *port, *tport; 632255570Strasz 633279006Smav TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport) 634279006Smav port_delete(port); 635255570Strasz TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 636255570Strasz 637255570Strasz TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 638255570Strasz portal_delete(portal); 639255570Strasz free(pg->pg_name); 640275642Strasz free(pg->pg_redirection); 641255570Strasz free(pg); 642255570Strasz} 643255570Strasz 644255570Straszstruct portal_group * 645265514Straszportal_group_find(const struct conf *conf, const char *name) 646255570Strasz{ 647255570Strasz struct portal_group *pg; 648255570Strasz 649255570Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 650255570Strasz if (strcmp(pg->pg_name, name) == 0) 651255570Strasz return (pg); 652255570Strasz } 653255570Strasz 654255570Strasz return (NULL); 655255570Strasz} 656255570Strasz 657274939Smavstatic int 658274939Smavparse_addr_port(char *arg, const char *def_port, struct addrinfo **ai) 659255570Strasz{ 660255570Strasz struct addrinfo hints; 661275674Smav char *str, *addr, *ch; 662255570Strasz const char *port; 663255570Strasz int error, colons = 0; 664255570Strasz 665275674Smav str = arg = strdup(arg); 666255570Strasz if (arg[0] == '[') { 667255570Strasz /* 668255570Strasz * IPv6 address in square brackets, perhaps with port. 669255570Strasz */ 670255570Strasz arg++; 671255570Strasz addr = strsep(&arg, "]"); 672274939Smav if (arg == NULL) 673255570Strasz return (1); 674255570Strasz if (arg[0] == '\0') { 675274939Smav port = def_port; 676255570Strasz } else if (arg[0] == ':') { 677255570Strasz port = arg + 1; 678275674Smav } else { 679275674Smav free(str); 680255570Strasz return (1); 681275674Smav } 682255570Strasz } else { 683255570Strasz /* 684255570Strasz * Either IPv6 address without brackets - and without 685255570Strasz * a port - or IPv4 address. Just count the colons. 686255570Strasz */ 687255570Strasz for (ch = arg; *ch != '\0'; ch++) { 688255570Strasz if (*ch == ':') 689255570Strasz colons++; 690255570Strasz } 691255570Strasz if (colons > 1) { 692255570Strasz addr = arg; 693274939Smav port = def_port; 694255570Strasz } else { 695255570Strasz addr = strsep(&arg, ":"); 696255570Strasz if (arg == NULL) 697274939Smav port = def_port; 698255570Strasz else 699255570Strasz port = arg; 700255570Strasz } 701255570Strasz } 702255570Strasz 703255570Strasz memset(&hints, 0, sizeof(hints)); 704255570Strasz hints.ai_family = PF_UNSPEC; 705255570Strasz hints.ai_socktype = SOCK_STREAM; 706255570Strasz hints.ai_flags = AI_PASSIVE; 707274939Smav error = getaddrinfo(addr, port, &hints, ai); 708275674Smav free(str); 709275674Smav return ((error != 0) ? 1 : 0); 710274939Smav} 711255570Strasz 712274939Smavint 713274939Smavportal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 714274939Smav{ 715274939Smav struct portal *portal; 716274939Smav 717274939Smav portal = portal_new(pg); 718274939Smav portal->p_listen = checked_strdup(value); 719274939Smav portal->p_iser = iser; 720274939Smav 721274939Smav if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) { 722274939Smav log_warnx("invalid listen address %s", portal->p_listen); 723271632Strasz portal_delete(portal); 724255570Strasz return (1); 725255570Strasz } 726255570Strasz 727255570Strasz /* 728255570Strasz * XXX: getaddrinfo(3) may return multiple addresses; we should turn 729255570Strasz * those into multiple portals. 730255570Strasz */ 731255570Strasz 732255570Strasz return (0); 733255570Strasz} 734255570Strasz 735274939Smavint 736274939Smavisns_new(struct conf *conf, const char *addr) 737274939Smav{ 738274939Smav struct isns *isns; 739274939Smav 740274939Smav isns = calloc(1, sizeof(*isns)); 741274939Smav if (isns == NULL) 742274939Smav log_err(1, "calloc"); 743274939Smav isns->i_conf = conf; 744274939Smav TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next); 745274939Smav isns->i_addr = checked_strdup(addr); 746274939Smav 747274939Smav if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) { 748274939Smav log_warnx("invalid iSNS address %s", isns->i_addr); 749274939Smav isns_delete(isns); 750274939Smav return (1); 751274939Smav } 752274939Smav 753274939Smav /* 754274939Smav * XXX: getaddrinfo(3) may return multiple addresses; we should turn 755274939Smav * those into multiple servers. 756274939Smav */ 757274939Smav 758274939Smav return (0); 759274939Smav} 760274939Smav 761274939Smavvoid 762274939Smavisns_delete(struct isns *isns) 763274939Smav{ 764274939Smav 765274939Smav TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next); 766274939Smav free(isns->i_addr); 767274939Smav if (isns->i_ai != NULL) 768274939Smav freeaddrinfo(isns->i_ai); 769274939Smav free(isns); 770274939Smav} 771274939Smav 772274939Smavstatic int 773274939Smavisns_do_connect(struct isns *isns) 774274939Smav{ 775274939Smav int s; 776274939Smav 777274939Smav s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype, 778274939Smav isns->i_ai->ai_protocol); 779274939Smav if (s < 0) { 780274939Smav log_warn("socket(2) failed for %s", isns->i_addr); 781274939Smav return (-1); 782274939Smav } 783274939Smav if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) { 784274939Smav log_warn("connect(2) failed for %s", isns->i_addr); 785274939Smav close(s); 786274939Smav return (-1); 787274939Smav } 788274939Smav return(s); 789274939Smav} 790274939Smav 791274939Smavstatic int 792274939Smavisns_do_register(struct isns *isns, int s, const char *hostname) 793274939Smav{ 794274939Smav struct conf *conf = isns->i_conf; 795274939Smav struct target *target; 796274939Smav struct portal *portal; 797274939Smav struct portal_group *pg; 798279006Smav struct port *port; 799274939Smav struct isns_req *req; 800274939Smav int res = 0; 801274939Smav uint32_t error; 802274939Smav 803274939Smav req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT); 804274939Smav isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 805274939Smav isns_req_add_delim(req); 806274939Smav isns_req_add_str(req, 1, hostname); 807274939Smav isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */ 808274939Smav isns_req_add_32(req, 6, conf->conf_isns_period); 809274939Smav TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 810274939Smav if (pg->pg_unassigned) 811274939Smav continue; 812274939Smav TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 813274939Smav isns_req_add_addr(req, 16, portal->p_ai); 814274939Smav isns_req_add_port(req, 17, portal->p_ai); 815274939Smav } 816274939Smav } 817274939Smav TAILQ_FOREACH(target, &conf->conf_targets, t_next) { 818274939Smav isns_req_add_str(req, 32, target->t_name); 819274939Smav isns_req_add_32(req, 33, 1); /* 1 -- Target*/ 820274939Smav if (target->t_alias != NULL) 821274939Smav isns_req_add_str(req, 34, target->t_alias); 822279006Smav TAILQ_FOREACH(port, &target->t_ports, p_ts) { 823279006Smav if ((pg = port->p_portal_group) == NULL) 824279006Smav continue; 825279006Smav isns_req_add_32(req, 51, pg->pg_tag); 826279006Smav TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 827279006Smav isns_req_add_addr(req, 49, portal->p_ai); 828279006Smav isns_req_add_port(req, 50, portal->p_ai); 829279006Smav } 830274939Smav } 831274939Smav } 832274939Smav res = isns_req_send(s, req); 833274939Smav if (res < 0) { 834274939Smav log_warn("send(2) failed for %s", isns->i_addr); 835274939Smav goto quit; 836274939Smav } 837274939Smav res = isns_req_receive(s, req); 838274939Smav if (res < 0) { 839274939Smav log_warn("receive(2) failed for %s", isns->i_addr); 840274939Smav goto quit; 841274939Smav } 842274939Smav error = isns_req_get_status(req); 843274939Smav if (error != 0) { 844274939Smav log_warnx("iSNS register error %d for %s", error, isns->i_addr); 845274939Smav res = -1; 846274939Smav } 847274939Smavquit: 848274939Smav isns_req_free(req); 849274939Smav return (res); 850274939Smav} 851274939Smav 852274939Smavstatic int 853274939Smavisns_do_check(struct isns *isns, int s, const char *hostname) 854274939Smav{ 855274939Smav struct conf *conf = isns->i_conf; 856274939Smav struct isns_req *req; 857274939Smav int res = 0; 858274939Smav uint32_t error; 859274939Smav 860274939Smav req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT); 861274939Smav isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 862274939Smav isns_req_add_str(req, 1, hostname); 863274939Smav isns_req_add_delim(req); 864274939Smav isns_req_add(req, 2, 0, NULL); 865274939Smav res = isns_req_send(s, req); 866274939Smav if (res < 0) { 867274939Smav log_warn("send(2) failed for %s", isns->i_addr); 868274939Smav goto quit; 869274939Smav } 870274939Smav res = isns_req_receive(s, req); 871274939Smav if (res < 0) { 872274939Smav log_warn("receive(2) failed for %s", isns->i_addr); 873274939Smav goto quit; 874274939Smav } 875274939Smav error = isns_req_get_status(req); 876274939Smav if (error != 0) { 877274939Smav log_warnx("iSNS check error %d for %s", error, isns->i_addr); 878274939Smav res = -1; 879274939Smav } 880274939Smavquit: 881274939Smav isns_req_free(req); 882274939Smav return (res); 883274939Smav} 884274939Smav 885274939Smavstatic int 886274939Smavisns_do_deregister(struct isns *isns, int s, const char *hostname) 887274939Smav{ 888274939Smav struct conf *conf = isns->i_conf; 889274939Smav struct isns_req *req; 890274939Smav int res = 0; 891274939Smav uint32_t error; 892274939Smav 893274939Smav req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT); 894274939Smav isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name); 895274939Smav isns_req_add_delim(req); 896274939Smav isns_req_add_str(req, 1, hostname); 897274939Smav res = isns_req_send(s, req); 898274939Smav if (res < 0) { 899274939Smav log_warn("send(2) failed for %s", isns->i_addr); 900274939Smav goto quit; 901274939Smav } 902274939Smav res = isns_req_receive(s, req); 903274939Smav if (res < 0) { 904274939Smav log_warn("receive(2) failed for %s", isns->i_addr); 905274939Smav goto quit; 906274939Smav } 907274939Smav error = isns_req_get_status(req); 908274939Smav if (error != 0) { 909274939Smav log_warnx("iSNS deregister error %d for %s", error, isns->i_addr); 910274939Smav res = -1; 911274939Smav } 912274939Smavquit: 913274939Smav isns_req_free(req); 914274939Smav return (res); 915274939Smav} 916274939Smav 917274939Smavvoid 918274939Smavisns_register(struct isns *isns, struct isns *oldisns) 919274939Smav{ 920274939Smav struct conf *conf = isns->i_conf; 921275496Smav int s; 922274939Smav char hostname[256]; 923274939Smav 924274939Smav if (TAILQ_EMPTY(&conf->conf_targets) || 925274939Smav TAILQ_EMPTY(&conf->conf_portal_groups)) 926274939Smav return; 927274939Smav set_timeout(conf->conf_isns_timeout, false); 928274939Smav s = isns_do_connect(isns); 929274939Smav if (s < 0) { 930274939Smav set_timeout(0, false); 931274939Smav return; 932274939Smav } 933274939Smav gethostname(hostname, sizeof(hostname)); 934274939Smav 935274939Smav if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets)) 936274939Smav oldisns = isns; 937275496Smav isns_do_deregister(oldisns, s, hostname); 938275496Smav isns_do_register(isns, s, hostname); 939274939Smav close(s); 940274939Smav set_timeout(0, false); 941274939Smav} 942274939Smav 943274939Smavvoid 944274939Smavisns_check(struct isns *isns) 945274939Smav{ 946274939Smav struct conf *conf = isns->i_conf; 947274939Smav int s, res; 948274939Smav char hostname[256]; 949274939Smav 950274939Smav if (TAILQ_EMPTY(&conf->conf_targets) || 951274939Smav TAILQ_EMPTY(&conf->conf_portal_groups)) 952274939Smav return; 953274939Smav set_timeout(conf->conf_isns_timeout, false); 954274939Smav s = isns_do_connect(isns); 955274939Smav if (s < 0) { 956274939Smav set_timeout(0, false); 957274939Smav return; 958274939Smav } 959274939Smav gethostname(hostname, sizeof(hostname)); 960274939Smav 961274939Smav res = isns_do_check(isns, s, hostname); 962274939Smav if (res < 0) { 963275496Smav isns_do_deregister(isns, s, hostname); 964275496Smav isns_do_register(isns, s, hostname); 965274939Smav } 966274939Smav close(s); 967274939Smav set_timeout(0, false); 968274939Smav} 969274939Smav 970274939Smavvoid 971274939Smavisns_deregister(struct isns *isns) 972274939Smav{ 973274939Smav struct conf *conf = isns->i_conf; 974275496Smav int s; 975274939Smav char hostname[256]; 976274939Smav 977274939Smav if (TAILQ_EMPTY(&conf->conf_targets) || 978274939Smav TAILQ_EMPTY(&conf->conf_portal_groups)) 979274939Smav return; 980274939Smav set_timeout(conf->conf_isns_timeout, false); 981274939Smav s = isns_do_connect(isns); 982274939Smav if (s < 0) 983274939Smav return; 984274939Smav gethostname(hostname, sizeof(hostname)); 985274939Smav 986275496Smav isns_do_deregister(isns, s, hostname); 987274939Smav close(s); 988274939Smav set_timeout(0, false); 989274939Smav} 990274939Smav 991275244Straszint 992275245Straszportal_group_set_filter(struct portal_group *pg, const char *str) 993275244Strasz{ 994275245Strasz int filter; 995275244Strasz 996275244Strasz if (strcmp(str, "none") == 0) { 997275244Strasz filter = PG_FILTER_NONE; 998275244Strasz } else if (strcmp(str, "portal") == 0) { 999275244Strasz filter = PG_FILTER_PORTAL; 1000275244Strasz } else if (strcmp(str, "portal-name") == 0) { 1001275244Strasz filter = PG_FILTER_PORTAL_NAME; 1002275244Strasz } else if (strcmp(str, "portal-name-auth") == 0) { 1003275244Strasz filter = PG_FILTER_PORTAL_NAME_AUTH; 1004275244Strasz } else { 1005275244Strasz log_warnx("invalid discovery-filter \"%s\" for portal-group " 1006275244Strasz "\"%s\"; valid values are \"none\", \"portal\", " 1007275244Strasz "\"portal-name\", and \"portal-name-auth\"", 1008275244Strasz str, pg->pg_name); 1009275244Strasz return (1); 1010275244Strasz } 1011275244Strasz 1012275245Strasz if (pg->pg_discovery_filter != PG_FILTER_UNKNOWN && 1013275245Strasz pg->pg_discovery_filter != filter) { 1014275244Strasz log_warnx("cannot set discovery-filter to \"%s\" for " 1015275244Strasz "portal-group \"%s\"; already has a different " 1016275244Strasz "value", str, pg->pg_name); 1017275244Strasz return (1); 1018275244Strasz } 1019275244Strasz 1020275245Strasz pg->pg_discovery_filter = filter; 1021275245Strasz 1022275245Strasz return (0); 1023275244Strasz} 1024275244Strasz 1025275642Straszint 1026275642Straszportal_group_set_redirection(struct portal_group *pg, const char *addr) 1027275642Strasz{ 1028275642Strasz 1029275642Strasz if (pg->pg_redirection != NULL) { 1030275642Strasz log_warnx("cannot set redirection to \"%s\" for " 1031275642Strasz "portal-group \"%s\"; already defined", 1032275642Strasz addr, pg->pg_name); 1033275642Strasz return (1); 1034275642Strasz } 1035275642Strasz 1036275642Strasz pg->pg_redirection = checked_strdup(addr); 1037275642Strasz 1038275642Strasz return (0); 1039275642Strasz} 1040275642Strasz 1041255570Straszstatic bool 1042255570Straszvalid_hex(const char ch) 1043255570Strasz{ 1044255570Strasz switch (ch) { 1045255570Strasz case '0': 1046255570Strasz case '1': 1047255570Strasz case '2': 1048255570Strasz case '3': 1049255570Strasz case '4': 1050255570Strasz case '5': 1051255570Strasz case '6': 1052255570Strasz case '7': 1053255570Strasz case '8': 1054255570Strasz case '9': 1055255570Strasz case 'a': 1056255570Strasz case 'A': 1057255570Strasz case 'b': 1058255570Strasz case 'B': 1059255570Strasz case 'c': 1060255570Strasz case 'C': 1061255570Strasz case 'd': 1062255570Strasz case 'D': 1063255570Strasz case 'e': 1064255570Strasz case 'E': 1065255570Strasz case 'f': 1066255570Strasz case 'F': 1067255570Strasz return (true); 1068255570Strasz default: 1069255570Strasz return (false); 1070255570Strasz } 1071255570Strasz} 1072255570Strasz 1073255570Straszbool 1074255570Straszvalid_iscsi_name(const char *name) 1075255570Strasz{ 1076255570Strasz int i; 1077255570Strasz 1078255570Strasz if (strlen(name) >= MAX_NAME_LEN) { 1079255570Strasz log_warnx("overlong name for target \"%s\"; max length allowed " 1080255570Strasz "by iSCSI specification is %d characters", 1081255570Strasz name, MAX_NAME_LEN); 1082255570Strasz return (false); 1083255570Strasz } 1084255570Strasz 1085255570Strasz /* 1086255570Strasz * In the cases below, we don't return an error, just in case the admin 1087255570Strasz * was right, and we're wrong. 1088255570Strasz */ 1089255570Strasz if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 1090255570Strasz for (i = strlen("iqn."); name[i] != '\0'; i++) { 1091255570Strasz /* 1092255570Strasz * XXX: We should verify UTF-8 normalisation, as defined 1093274870Strasz * by 3.2.6.2: iSCSI Name Encoding. 1094255570Strasz */ 1095255570Strasz if (isalnum(name[i])) 1096255570Strasz continue; 1097255570Strasz if (name[i] == '-' || name[i] == '.' || name[i] == ':') 1098255570Strasz continue; 1099255570Strasz log_warnx("invalid character \"%c\" in target name " 1100255570Strasz "\"%s\"; allowed characters are letters, digits, " 1101255570Strasz "'-', '.', and ':'", name[i], name); 1102255570Strasz break; 1103255570Strasz } 1104255570Strasz /* 1105255570Strasz * XXX: Check more stuff: valid date and a valid reversed domain. 1106255570Strasz */ 1107255570Strasz } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 1108255570Strasz if (strlen(name) != strlen("eui.") + 16) 1109255570Strasz log_warnx("invalid target name \"%s\"; the \"eui.\" " 1110255570Strasz "should be followed by exactly 16 hexadecimal " 1111255570Strasz "digits", name); 1112255570Strasz for (i = strlen("eui."); name[i] != '\0'; i++) { 1113255570Strasz if (!valid_hex(name[i])) { 1114255570Strasz log_warnx("invalid character \"%c\" in target " 1115255570Strasz "name \"%s\"; allowed characters are 1-9 " 1116255570Strasz "and A-F", name[i], name); 1117255570Strasz break; 1118255570Strasz } 1119255570Strasz } 1120255570Strasz } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 1121255570Strasz if (strlen(name) > strlen("naa.") + 32) 1122255570Strasz log_warnx("invalid target name \"%s\"; the \"naa.\" " 1123255570Strasz "should be followed by at most 32 hexadecimal " 1124255570Strasz "digits", name); 1125255570Strasz for (i = strlen("naa."); name[i] != '\0'; i++) { 1126255570Strasz if (!valid_hex(name[i])) { 1127255570Strasz log_warnx("invalid character \"%c\" in target " 1128255570Strasz "name \"%s\"; allowed characters are 1-9 " 1129255570Strasz "and A-F", name[i], name); 1130255570Strasz break; 1131255570Strasz } 1132255570Strasz } 1133255570Strasz } else { 1134255570Strasz log_warnx("invalid target name \"%s\"; should start with " 1135255570Strasz "either \".iqn\", \"eui.\", or \"naa.\"", 1136255570Strasz name); 1137255570Strasz } 1138255570Strasz return (true); 1139255570Strasz} 1140255570Strasz 1141279055Smavstruct pport * 1142279055Smavpport_new(struct conf *conf, const char *name, uint32_t ctl_port) 1143279055Smav{ 1144279055Smav struct pport *pp; 1145279055Smav 1146279055Smav pp = calloc(1, sizeof(*pp)); 1147279055Smav if (pp == NULL) 1148279055Smav log_err(1, "calloc"); 1149279055Smav pp->pp_conf = conf; 1150279055Smav pp->pp_name = checked_strdup(name); 1151279055Smav pp->pp_ctl_port = ctl_port; 1152279055Smav TAILQ_INIT(&pp->pp_ports); 1153279055Smav TAILQ_INSERT_TAIL(&conf->conf_pports, pp, pp_next); 1154279055Smav return (pp); 1155279055Smav} 1156279055Smav 1157279055Smavstruct pport * 1158279055Smavpport_find(const struct conf *conf, const char *name) 1159279055Smav{ 1160279055Smav struct pport *pp; 1161279055Smav 1162279055Smav TAILQ_FOREACH(pp, &conf->conf_pports, pp_next) { 1163279055Smav if (strcasecmp(pp->pp_name, name) == 0) 1164279055Smav return (pp); 1165279055Smav } 1166279055Smav return (NULL); 1167279055Smav} 1168279055Smav 1169279055Smavstruct pport * 1170279055Smavpport_copy(struct pport *pp, struct conf *conf) 1171279055Smav{ 1172279055Smav struct pport *ppnew; 1173279055Smav 1174279055Smav ppnew = pport_new(conf, pp->pp_name, pp->pp_ctl_port); 1175279055Smav return (ppnew); 1176279055Smav} 1177279055Smav 1178279055Smavvoid 1179279055Smavpport_delete(struct pport *pp) 1180279055Smav{ 1181279055Smav struct port *port, *tport; 1182279055Smav 1183279055Smav TAILQ_FOREACH_SAFE(port, &pp->pp_ports, p_ts, tport) 1184279055Smav port_delete(port); 1185279055Smav TAILQ_REMOVE(&pp->pp_conf->conf_pports, pp, pp_next); 1186279055Smav free(pp->pp_name); 1187279055Smav free(pp); 1188279055Smav} 1189279055Smav 1190279006Smavstruct port * 1191279006Smavport_new(struct conf *conf, struct target *target, struct portal_group *pg) 1192279006Smav{ 1193279006Smav struct port *port; 1194279055Smav char *name; 1195279056Smav int ret; 1196279006Smav 1197279056Smav ret = asprintf(&name, "%s-%s", pg->pg_name, target->t_name); 1198279056Smav if (ret <= 0) 1199279056Smav log_err(1, "asprintf"); 1200279055Smav if (port_find(conf, name) != NULL) { 1201279055Smav log_warnx("duplicate port \"%s\"", name); 1202279055Smav free(name); 1203279055Smav return (NULL); 1204279055Smav } 1205279006Smav port = calloc(1, sizeof(*port)); 1206279006Smav if (port == NULL) 1207279006Smav log_err(1, "calloc"); 1208279006Smav port->p_conf = conf; 1209279055Smav port->p_name = name; 1210279006Smav TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1211279006Smav TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1212279006Smav port->p_target = target; 1213279006Smav TAILQ_INSERT_TAIL(&pg->pg_ports, port, p_pgs); 1214279006Smav port->p_portal_group = pg; 1215279006Smav return (port); 1216279006Smav} 1217279006Smav 1218279006Smavstruct port * 1219279055Smavport_new_pp(struct conf *conf, struct target *target, struct pport *pp) 1220279055Smav{ 1221279055Smav struct port *port; 1222279055Smav char *name; 1223279056Smav int ret; 1224279055Smav 1225279056Smav ret = asprintf(&name, "%s-%s", pp->pp_name, target->t_name); 1226279056Smav if (ret <= 0) 1227279056Smav log_err(1, "asprintf"); 1228279055Smav if (port_find(conf, name) != NULL) { 1229279055Smav log_warnx("duplicate port \"%s\"", name); 1230279055Smav free(name); 1231279055Smav return (NULL); 1232279055Smav } 1233279055Smav port = calloc(1, sizeof(*port)); 1234279055Smav if (port == NULL) 1235279055Smav log_err(1, "calloc"); 1236279055Smav port->p_conf = conf; 1237279055Smav port->p_name = name; 1238279055Smav TAILQ_INSERT_TAIL(&conf->conf_ports, port, p_next); 1239279055Smav TAILQ_INSERT_TAIL(&target->t_ports, port, p_ts); 1240279055Smav port->p_target = target; 1241279055Smav TAILQ_INSERT_TAIL(&pp->pp_ports, port, p_pps); 1242279055Smav port->p_pport = pp; 1243279055Smav return (port); 1244279055Smav} 1245279055Smav 1246279055Smavstruct port * 1247279006Smavport_find(const struct conf *conf, const char *name) 1248279006Smav{ 1249279006Smav struct port *port; 1250279006Smav 1251279006Smav TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 1252279006Smav if (strcasecmp(port->p_name, name) == 0) 1253279006Smav return (port); 1254279006Smav } 1255279006Smav 1256279006Smav return (NULL); 1257279006Smav} 1258279006Smav 1259279006Smavstruct port * 1260279006Smavport_find_in_pg(const struct portal_group *pg, const char *target) 1261279006Smav{ 1262279006Smav struct port *port; 1263279006Smav 1264279006Smav TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) { 1265279006Smav if (strcasecmp(port->p_target->t_name, target) == 0) 1266279006Smav return (port); 1267279006Smav } 1268279006Smav 1269279006Smav return (NULL); 1270279006Smav} 1271279006Smav 1272279006Smavvoid 1273279006Smavport_delete(struct port *port) 1274279006Smav{ 1275279006Smav 1276279006Smav if (port->p_portal_group) 1277279006Smav TAILQ_REMOVE(&port->p_portal_group->pg_ports, port, p_pgs); 1278279055Smav if (port->p_pport) 1279279055Smav TAILQ_REMOVE(&port->p_pport->pp_ports, port, p_pps); 1280279006Smav if (port->p_target) 1281279006Smav TAILQ_REMOVE(&port->p_target->t_ports, port, p_ts); 1282279006Smav TAILQ_REMOVE(&port->p_conf->conf_ports, port, p_next); 1283279006Smav free(port->p_name); 1284279006Smav free(port); 1285279006Smav} 1286279006Smav 1287255570Straszstruct target * 1288263723Strasztarget_new(struct conf *conf, const char *name) 1289255570Strasz{ 1290255570Strasz struct target *targ; 1291255570Strasz int i, len; 1292255570Strasz 1293263723Strasz targ = target_find(conf, name); 1294255570Strasz if (targ != NULL) { 1295263723Strasz log_warnx("duplicated target \"%s\"", name); 1296255570Strasz return (NULL); 1297255570Strasz } 1298263723Strasz if (valid_iscsi_name(name) == false) { 1299263723Strasz log_warnx("target name \"%s\" is invalid", name); 1300255570Strasz return (NULL); 1301255570Strasz } 1302255570Strasz targ = calloc(1, sizeof(*targ)); 1303255570Strasz if (targ == NULL) 1304255570Strasz log_err(1, "calloc"); 1305263723Strasz targ->t_name = checked_strdup(name); 1306255570Strasz 1307255570Strasz /* 1308255570Strasz * RFC 3722 requires us to normalize the name to lowercase. 1309255570Strasz */ 1310263723Strasz len = strlen(name); 1311255570Strasz for (i = 0; i < len; i++) 1312263723Strasz targ->t_name[i] = tolower(targ->t_name[i]); 1313255570Strasz 1314255570Strasz targ->t_conf = conf; 1315279006Smav TAILQ_INIT(&targ->t_ports); 1316255570Strasz TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 1317255570Strasz 1318255570Strasz return (targ); 1319255570Strasz} 1320255570Strasz 1321255570Straszvoid 1322255570Strasztarget_delete(struct target *targ) 1323255570Strasz{ 1324279006Smav struct port *port, *tport; 1325255570Strasz 1326279006Smav TAILQ_FOREACH_SAFE(port, &targ->t_ports, p_ts, tport) 1327279006Smav port_delete(port); 1328255570Strasz TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 1329255570Strasz 1330263723Strasz free(targ->t_name); 1331275642Strasz free(targ->t_redirection); 1332255570Strasz free(targ); 1333255570Strasz} 1334255570Strasz 1335255570Straszstruct target * 1336263723Strasztarget_find(struct conf *conf, const char *name) 1337255570Strasz{ 1338255570Strasz struct target *targ; 1339255570Strasz 1340255570Strasz TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1341263723Strasz if (strcasecmp(targ->t_name, name) == 0) 1342255570Strasz return (targ); 1343255570Strasz } 1344255570Strasz 1345255570Strasz return (NULL); 1346255570Strasz} 1347255570Strasz 1348275642Straszint 1349275642Strasztarget_set_redirection(struct target *target, const char *addr) 1350275642Strasz{ 1351275642Strasz 1352275642Strasz if (target->t_redirection != NULL) { 1353275642Strasz log_warnx("cannot set redirection to \"%s\" for " 1354275642Strasz "target \"%s\"; already defined", 1355275642Strasz addr, target->t_name); 1356275642Strasz return (1); 1357275642Strasz } 1358275642Strasz 1359275642Strasz target->t_redirection = checked_strdup(addr); 1360275642Strasz 1361275642Strasz return (0); 1362275642Strasz} 1363275642Strasz 1364255570Straszstruct lun * 1365279002Smavlun_new(struct conf *conf, const char *name) 1366255570Strasz{ 1367255570Strasz struct lun *lun; 1368255570Strasz 1369279002Smav lun = lun_find(conf, name); 1370255570Strasz if (lun != NULL) { 1371279002Smav log_warnx("duplicated lun \"%s\"", name); 1372255570Strasz return (NULL); 1373255570Strasz } 1374255570Strasz 1375255570Strasz lun = calloc(1, sizeof(*lun)); 1376255570Strasz if (lun == NULL) 1377255570Strasz log_err(1, "calloc"); 1378279002Smav lun->l_conf = conf; 1379279002Smav lun->l_name = checked_strdup(name); 1380255570Strasz TAILQ_INIT(&lun->l_options); 1381279002Smav TAILQ_INSERT_TAIL(&conf->conf_luns, lun, l_next); 1382255570Strasz 1383255570Strasz return (lun); 1384255570Strasz} 1385255570Strasz 1386255570Straszvoid 1387255570Straszlun_delete(struct lun *lun) 1388255570Strasz{ 1389279002Smav struct target *targ; 1390255570Strasz struct lun_option *lo, *tmp; 1391279002Smav int i; 1392255570Strasz 1393279002Smav TAILQ_FOREACH(targ, &lun->l_conf->conf_targets, t_next) { 1394279002Smav for (i = 0; i < MAX_LUNS; i++) { 1395279002Smav if (targ->t_luns[i] == lun) 1396279002Smav targ->t_luns[i] = NULL; 1397279002Smav } 1398279002Smav } 1399279002Smav TAILQ_REMOVE(&lun->l_conf->conf_luns, lun, l_next); 1400255570Strasz 1401255570Strasz TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp) 1402255570Strasz lun_option_delete(lo); 1403279002Smav free(lun->l_name); 1404255570Strasz free(lun->l_backend); 1405255570Strasz free(lun->l_device_id); 1406255570Strasz free(lun->l_path); 1407279002Smav free(lun->l_scsiname); 1408255570Strasz free(lun->l_serial); 1409255570Strasz free(lun); 1410255570Strasz} 1411255570Strasz 1412255570Straszstruct lun * 1413279002Smavlun_find(const struct conf *conf, const char *name) 1414255570Strasz{ 1415255570Strasz struct lun *lun; 1416255570Strasz 1417279002Smav TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1418279002Smav if (strcmp(lun->l_name, name) == 0) 1419255570Strasz return (lun); 1420255570Strasz } 1421255570Strasz 1422255570Strasz return (NULL); 1423255570Strasz} 1424255570Strasz 1425255570Straszvoid 1426255570Straszlun_set_backend(struct lun *lun, const char *value) 1427255570Strasz{ 1428255570Strasz free(lun->l_backend); 1429255570Strasz lun->l_backend = checked_strdup(value); 1430255570Strasz} 1431255570Strasz 1432255570Straszvoid 1433255570Straszlun_set_blocksize(struct lun *lun, size_t value) 1434255570Strasz{ 1435255570Strasz 1436255570Strasz lun->l_blocksize = value; 1437255570Strasz} 1438255570Strasz 1439255570Straszvoid 1440255570Straszlun_set_device_id(struct lun *lun, const char *value) 1441255570Strasz{ 1442255570Strasz free(lun->l_device_id); 1443255570Strasz lun->l_device_id = checked_strdup(value); 1444255570Strasz} 1445255570Strasz 1446255570Straszvoid 1447255570Straszlun_set_path(struct lun *lun, const char *value) 1448255570Strasz{ 1449255570Strasz free(lun->l_path); 1450255570Strasz lun->l_path = checked_strdup(value); 1451255570Strasz} 1452255570Strasz 1453255570Straszvoid 1454279002Smavlun_set_scsiname(struct lun *lun, const char *value) 1455279002Smav{ 1456279002Smav free(lun->l_scsiname); 1457279002Smav lun->l_scsiname = checked_strdup(value); 1458279002Smav} 1459279002Smav 1460279002Smavvoid 1461255570Straszlun_set_serial(struct lun *lun, const char *value) 1462255570Strasz{ 1463255570Strasz free(lun->l_serial); 1464255570Strasz lun->l_serial = checked_strdup(value); 1465255570Strasz} 1466255570Strasz 1467255570Straszvoid 1468255570Straszlun_set_size(struct lun *lun, size_t value) 1469255570Strasz{ 1470255570Strasz 1471255570Strasz lun->l_size = value; 1472255570Strasz} 1473255570Strasz 1474255570Straszvoid 1475255570Straszlun_set_ctl_lun(struct lun *lun, uint32_t value) 1476255570Strasz{ 1477255570Strasz 1478255570Strasz lun->l_ctl_lun = value; 1479255570Strasz} 1480255570Strasz 1481255570Straszstruct lun_option * 1482255570Straszlun_option_new(struct lun *lun, const char *name, const char *value) 1483255570Strasz{ 1484255570Strasz struct lun_option *lo; 1485255570Strasz 1486255570Strasz lo = lun_option_find(lun, name); 1487255570Strasz if (lo != NULL) { 1488279002Smav log_warnx("duplicated lun option \"%s\" for lun \"%s\"", 1489279002Smav name, lun->l_name); 1490255570Strasz return (NULL); 1491255570Strasz } 1492255570Strasz 1493255570Strasz lo = calloc(1, sizeof(*lo)); 1494255570Strasz if (lo == NULL) 1495255570Strasz log_err(1, "calloc"); 1496255570Strasz lo->lo_name = checked_strdup(name); 1497255570Strasz lo->lo_value = checked_strdup(value); 1498255570Strasz lo->lo_lun = lun; 1499255570Strasz TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next); 1500255570Strasz 1501255570Strasz return (lo); 1502255570Strasz} 1503255570Strasz 1504255570Straszvoid 1505255570Straszlun_option_delete(struct lun_option *lo) 1506255570Strasz{ 1507255570Strasz 1508255570Strasz TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next); 1509255570Strasz 1510255570Strasz free(lo->lo_name); 1511255570Strasz free(lo->lo_value); 1512255570Strasz free(lo); 1513255570Strasz} 1514255570Strasz 1515255570Straszstruct lun_option * 1516265514Straszlun_option_find(const struct lun *lun, const char *name) 1517255570Strasz{ 1518255570Strasz struct lun_option *lo; 1519255570Strasz 1520255570Strasz TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 1521255570Strasz if (strcmp(lo->lo_name, name) == 0) 1522255570Strasz return (lo); 1523255570Strasz } 1524255570Strasz 1525255570Strasz return (NULL); 1526255570Strasz} 1527255570Strasz 1528255570Straszvoid 1529255570Straszlun_option_set(struct lun_option *lo, const char *value) 1530255570Strasz{ 1531255570Strasz 1532255570Strasz free(lo->lo_value); 1533255570Strasz lo->lo_value = checked_strdup(value); 1534255570Strasz} 1535255570Strasz 1536255570Straszstatic struct connection * 1537270137Smavconnection_new(struct portal *portal, int fd, const char *host, 1538270137Smav const struct sockaddr *client_sa) 1539255570Strasz{ 1540255570Strasz struct connection *conn; 1541255570Strasz 1542255570Strasz conn = calloc(1, sizeof(*conn)); 1543255570Strasz if (conn == NULL) 1544255570Strasz log_err(1, "calloc"); 1545255570Strasz conn->conn_portal = portal; 1546255570Strasz conn->conn_socket = fd; 1547255570Strasz conn->conn_initiator_addr = checked_strdup(host); 1548270137Smav memcpy(&conn->conn_initiator_sa, client_sa, client_sa->sa_len); 1549255570Strasz 1550255570Strasz /* 1551255570Strasz * Default values, from RFC 3720, section 12. 1552255570Strasz */ 1553255570Strasz conn->conn_max_data_segment_length = 8192; 1554255570Strasz conn->conn_max_burst_length = 262144; 1555255570Strasz conn->conn_immediate_data = true; 1556255570Strasz 1557255570Strasz return (conn); 1558255570Strasz} 1559255570Strasz 1560255570Strasz#if 0 1561255570Straszstatic void 1562255570Straszconf_print(struct conf *conf) 1563255570Strasz{ 1564255570Strasz struct auth_group *ag; 1565255570Strasz struct auth *auth; 1566263720Strasz struct auth_name *auth_name; 1567263720Strasz struct auth_portal *auth_portal; 1568255570Strasz struct portal_group *pg; 1569255570Strasz struct portal *portal; 1570255570Strasz struct target *targ; 1571255570Strasz struct lun *lun; 1572255570Strasz struct lun_option *lo; 1573255570Strasz 1574255570Strasz TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1575255570Strasz fprintf(stderr, "auth-group %s {\n", ag->ag_name); 1576255570Strasz TAILQ_FOREACH(auth, &ag->ag_auths, a_next) 1577255570Strasz fprintf(stderr, "\t chap-mutual %s %s %s %s\n", 1578255570Strasz auth->a_user, auth->a_secret, 1579255570Strasz auth->a_mutual_user, auth->a_mutual_secret); 1580263720Strasz TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) 1581263720Strasz fprintf(stderr, "\t initiator-name %s\n", 1582263720Strasz auth_name->an_initator_name); 1583263720Strasz TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next) 1584263720Strasz fprintf(stderr, "\t initiator-portal %s\n", 1585263720Strasz auth_portal->an_initator_portal); 1586255570Strasz fprintf(stderr, "}\n"); 1587255570Strasz } 1588255570Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1589255570Strasz fprintf(stderr, "portal-group %s {\n", pg->pg_name); 1590255570Strasz TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1591255570Strasz fprintf(stderr, "\t listen %s\n", portal->p_listen); 1592255570Strasz fprintf(stderr, "}\n"); 1593255570Strasz } 1594279002Smav TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1595279002Smav fprintf(stderr, "\tlun %s {\n", lun->l_name); 1596279002Smav fprintf(stderr, "\t\tpath %s\n", lun->l_path); 1597279002Smav TAILQ_FOREACH(lo, &lun->l_options, lo_next) 1598279002Smav fprintf(stderr, "\t\toption %s %s\n", 1599279002Smav lo->lo_name, lo->lo_value); 1600279002Smav fprintf(stderr, "\t}\n"); 1601279002Smav } 1602255570Strasz TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1603263723Strasz fprintf(stderr, "target %s {\n", targ->t_name); 1604255570Strasz if (targ->t_alias != NULL) 1605255570Strasz fprintf(stderr, "\t alias %s\n", targ->t_alias); 1606255570Strasz fprintf(stderr, "}\n"); 1607255570Strasz } 1608255570Strasz} 1609255570Strasz#endif 1610255570Strasz 1611263717Straszstatic int 1612263717Straszconf_verify_lun(struct lun *lun) 1613263717Strasz{ 1614263717Strasz const struct lun *lun2; 1615263717Strasz 1616263717Strasz if (lun->l_backend == NULL) 1617263717Strasz lun_set_backend(lun, "block"); 1618263717Strasz if (strcmp(lun->l_backend, "block") == 0) { 1619263717Strasz if (lun->l_path == NULL) { 1620279002Smav log_warnx("missing path for lun \"%s\"", 1621279002Smav lun->l_name); 1622263717Strasz return (1); 1623263717Strasz } 1624263717Strasz } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 1625263717Strasz if (lun->l_size == 0) { 1626279002Smav log_warnx("missing size for ramdisk-backed lun \"%s\"", 1627279002Smav lun->l_name); 1628263717Strasz return (1); 1629263717Strasz } 1630263717Strasz if (lun->l_path != NULL) { 1631263717Strasz log_warnx("path must not be specified " 1632279002Smav "for ramdisk-backed lun \"%s\"", 1633279002Smav lun->l_name); 1634263717Strasz return (1); 1635263717Strasz } 1636263717Strasz } 1637263717Strasz if (lun->l_blocksize == 0) { 1638263717Strasz lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 1639263717Strasz } else if (lun->l_blocksize < 0) { 1640279002Smav log_warnx("invalid blocksize for lun \"%s\"; " 1641279002Smav "must be larger than 0", lun->l_name); 1642263717Strasz return (1); 1643263717Strasz } 1644263717Strasz if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 1645279002Smav log_warnx("invalid size for lun \"%s\"; " 1646279002Smav "must be multiple of blocksize", lun->l_name); 1647263717Strasz return (1); 1648263717Strasz } 1649279002Smav TAILQ_FOREACH(lun2, &lun->l_conf->conf_luns, l_next) { 1650279002Smav if (lun == lun2) 1651279002Smav continue; 1652279002Smav if (lun->l_path != NULL && lun2->l_path != NULL && 1653279002Smav strcmp(lun->l_path, lun2->l_path) == 0) { 1654279002Smav log_debugx("WARNING: path \"%s\" duplicated " 1655279002Smav "between lun \"%s\", and " 1656279002Smav "lun \"%s\"", lun->l_path, 1657279002Smav lun->l_name, lun2->l_name); 1658263718Strasz } 1659263718Strasz } 1660263717Strasz 1661263717Strasz return (0); 1662263717Strasz} 1663263717Strasz 1664255570Straszint 1665255570Straszconf_verify(struct conf *conf) 1666255570Strasz{ 1667255570Strasz struct auth_group *ag; 1668255570Strasz struct portal_group *pg; 1669279006Smav struct port *port; 1670255570Strasz struct target *targ; 1671263717Strasz struct lun *lun; 1672274871Strasz bool found; 1673279002Smav int error, i; 1674255570Strasz 1675255570Strasz if (conf->conf_pidfile_path == NULL) 1676255570Strasz conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1677255570Strasz 1678279002Smav TAILQ_FOREACH(lun, &conf->conf_luns, l_next) { 1679279002Smav error = conf_verify_lun(lun); 1680279002Smav if (error != 0) 1681279002Smav return (error); 1682279002Smav } 1683255570Strasz TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1684255570Strasz if (targ->t_auth_group == NULL) { 1685263726Strasz targ->t_auth_group = auth_group_find(conf, 1686263726Strasz "default"); 1687263726Strasz assert(targ->t_auth_group != NULL); 1688255570Strasz } 1689279006Smav if (TAILQ_EMPTY(&targ->t_ports)) { 1690279006Smav pg = portal_group_find(conf, "default"); 1691279006Smav assert(pg != NULL); 1692279006Smav port_new(conf, targ, pg); 1693255570Strasz } 1694274871Strasz found = false; 1695279002Smav for (i = 0; i < MAX_LUNS; i++) { 1696279002Smav if (targ->t_luns[i] != NULL) 1697279002Smav found = true; 1698255570Strasz } 1699275642Strasz if (!found && targ->t_redirection == NULL) { 1700265506Strasz log_warnx("no LUNs defined for target \"%s\"", 1701265506Strasz targ->t_name); 1702255570Strasz } 1703275642Strasz if (found && targ->t_redirection != NULL) { 1704275642Strasz log_debugx("target \"%s\" contains luns, " 1705275642Strasz " but configured for redirection", 1706275642Strasz targ->t_name); 1707275642Strasz } 1708255570Strasz } 1709255570Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1710255570Strasz assert(pg->pg_name != NULL); 1711255570Strasz if (pg->pg_discovery_auth_group == NULL) { 1712255570Strasz pg->pg_discovery_auth_group = 1713263728Strasz auth_group_find(conf, "default"); 1714255570Strasz assert(pg->pg_discovery_auth_group != NULL); 1715255570Strasz } 1716255570Strasz 1717275244Strasz if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN) 1718275244Strasz pg->pg_discovery_filter = PG_FILTER_NONE; 1719275244Strasz 1720279006Smav if (!TAILQ_EMPTY(&pg->pg_ports)) { 1721279006Smav if (pg->pg_redirection != NULL) { 1722275642Strasz log_debugx("portal-group \"%s\" assigned " 1723279006Smav "to target, but configured " 1724275642Strasz "for redirection", 1725279006Smav pg->pg_name); 1726275642Strasz } 1727275642Strasz pg->pg_unassigned = false; 1728275642Strasz } else { 1729255570Strasz if (strcmp(pg->pg_name, "default") != 0) 1730255570Strasz log_warnx("portal-group \"%s\" not assigned " 1731255570Strasz "to any target", pg->pg_name); 1732255570Strasz pg->pg_unassigned = true; 1733275642Strasz } 1734255570Strasz } 1735255570Strasz TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1736255570Strasz if (ag->ag_name == NULL) 1737255570Strasz assert(ag->ag_target != NULL); 1738255570Strasz else 1739255570Strasz assert(ag->ag_target == NULL); 1740255570Strasz 1741274871Strasz found = false; 1742255570Strasz TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1743274871Strasz if (targ->t_auth_group == ag) { 1744274871Strasz found = true; 1745255570Strasz break; 1746274871Strasz } 1747255570Strasz } 1748279006Smav TAILQ_FOREACH(port, &conf->conf_ports, p_next) { 1749279006Smav if (port->p_auth_group == ag) { 1750279006Smav found = true; 1751279006Smav break; 1752279006Smav } 1753279006Smav } 1754274871Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1755274871Strasz if (pg->pg_discovery_auth_group == ag) { 1756274871Strasz found = true; 1757274871Strasz break; 1758274871Strasz } 1759274871Strasz } 1760274871Strasz if (!found && ag->ag_name != NULL && 1761263728Strasz strcmp(ag->ag_name, "default") != 0 && 1762255570Strasz strcmp(ag->ag_name, "no-authentication") != 0 && 1763255570Strasz strcmp(ag->ag_name, "no-access") != 0) { 1764255570Strasz log_warnx("auth-group \"%s\" not assigned " 1765255570Strasz "to any target", ag->ag_name); 1766255570Strasz } 1767255570Strasz } 1768255570Strasz 1769255570Strasz return (0); 1770255570Strasz} 1771255570Strasz 1772255570Straszstatic int 1773255570Straszconf_apply(struct conf *oldconf, struct conf *newconf) 1774255570Strasz{ 1775255570Strasz struct lun *oldlun, *newlun, *tmplun; 1776255570Strasz struct portal_group *oldpg, *newpg; 1777255570Strasz struct portal *oldp, *newp; 1778279006Smav struct port *oldport, *newport, *tmpport; 1779274939Smav struct isns *oldns, *newns; 1780255570Strasz pid_t otherpid; 1781279001Smav int changed, cumulated_error = 0, error, sockbuf; 1782255570Strasz int one = 1; 1783255570Strasz 1784255570Strasz if (oldconf->conf_debug != newconf->conf_debug) { 1785255570Strasz log_debugx("changing debug level to %d", newconf->conf_debug); 1786255570Strasz log_init(newconf->conf_debug); 1787255570Strasz } 1788255570Strasz 1789255570Strasz if (oldconf->conf_pidfh != NULL) { 1790255570Strasz assert(oldconf->conf_pidfile_path != NULL); 1791255570Strasz if (newconf->conf_pidfile_path != NULL && 1792255570Strasz strcmp(oldconf->conf_pidfile_path, 1793255570Strasz newconf->conf_pidfile_path) == 0) { 1794255570Strasz newconf->conf_pidfh = oldconf->conf_pidfh; 1795255570Strasz oldconf->conf_pidfh = NULL; 1796255570Strasz } else { 1797255570Strasz log_debugx("removing pidfile %s", 1798255570Strasz oldconf->conf_pidfile_path); 1799255570Strasz pidfile_remove(oldconf->conf_pidfh); 1800255570Strasz oldconf->conf_pidfh = NULL; 1801255570Strasz } 1802255570Strasz } 1803255570Strasz 1804255570Strasz if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1805255570Strasz log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1806255570Strasz newconf->conf_pidfh = 1807255570Strasz pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1808255570Strasz if (newconf->conf_pidfh == NULL) { 1809255570Strasz if (errno == EEXIST) 1810255570Strasz log_errx(1, "daemon already running, pid: %jd.", 1811255570Strasz (intmax_t)otherpid); 1812255570Strasz log_err(1, "cannot open or create pidfile \"%s\"", 1813255570Strasz newconf->conf_pidfile_path); 1814255570Strasz } 1815255570Strasz } 1816255570Strasz 1817279003Smav /* 1818279003Smav * Go through the new portal groups, assigning tags or preserving old. 1819279003Smav */ 1820279003Smav TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1821279003Smav oldpg = portal_group_find(oldconf, newpg->pg_name); 1822279003Smav if (oldpg != NULL) 1823279003Smav newpg->pg_tag = oldpg->pg_tag; 1824279003Smav else 1825279003Smav newpg->pg_tag = ++last_portal_group_tag; 1826279003Smav } 1827279003Smav 1828274939Smav /* Deregister on removed iSNS servers. */ 1829274939Smav TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 1830274939Smav TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 1831274939Smav if (strcmp(oldns->i_addr, newns->i_addr) == 0) 1832274939Smav break; 1833274939Smav } 1834274939Smav if (newns == NULL) 1835274939Smav isns_deregister(oldns); 1836274939Smav } 1837274939Smav 1838265518Strasz /* 1839265518Strasz * XXX: If target or lun removal fails, we should somehow "move" 1840274870Strasz * the old lun or target into newconf, so that subsequent 1841274870Strasz * conf_apply() would try to remove them again. That would 1842274870Strasz * be somewhat hairy, though, and lun deletion failures don't 1843274870Strasz * really happen, so leave it as it is for now. 1844265518Strasz */ 1845279002Smav /* 1846279006Smav * First, remove any ports present in the old configuration 1847279002Smav * and missing in the new one. 1848279002Smav */ 1849279006Smav TAILQ_FOREACH_SAFE(oldport, &oldconf->conf_ports, p_next, tmpport) { 1850279006Smav newport = port_find(newconf, oldport->p_name); 1851279006Smav if (newport != NULL) 1852279002Smav continue; 1853279055Smav log_debugx("removing port \"%s\"", oldport->p_name); 1854279006Smav error = kernel_port_remove(oldport); 1855279002Smav if (error != 0) { 1856279006Smav log_warnx("failed to remove port %s", 1857279006Smav oldport->p_name); 1858279002Smav /* 1859279002Smav * XXX: Uncomment after fixing the root cause. 1860279002Smav * 1861279002Smav * cumulated_error++; 1862279002Smav */ 1863279002Smav } 1864279002Smav } 1865279002Smav 1866279002Smav /* 1867279002Smav * Second, remove any LUNs present in the old configuration 1868279002Smav * and missing in the new one. 1869279002Smav */ 1870279002Smav TAILQ_FOREACH_SAFE(oldlun, &oldconf->conf_luns, l_next, tmplun) { 1871279002Smav newlun = lun_find(newconf, oldlun->l_name); 1872279002Smav if (newlun == NULL) { 1873279002Smav log_debugx("lun \"%s\", CTL lun %d " 1874279002Smav "not found in new configuration; " 1875279002Smav "removing", oldlun->l_name, oldlun->l_ctl_lun); 1876279002Smav error = kernel_lun_remove(oldlun); 1877279000Smav if (error != 0) { 1878279002Smav log_warnx("failed to remove lun \"%s\", " 1879279002Smav "CTL lun %d", 1880279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1881279002Smav cumulated_error++; 1882279000Smav } 1883255570Strasz continue; 1884255570Strasz } 1885255570Strasz 1886255570Strasz /* 1887279002Smav * Also remove the LUNs changed by more than size. 1888255570Strasz */ 1889279002Smav changed = 0; 1890279002Smav assert(oldlun->l_backend != NULL); 1891279002Smav assert(newlun->l_backend != NULL); 1892279002Smav if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1893279002Smav log_debugx("backend for lun \"%s\", " 1894279002Smav "CTL lun %d changed; removing", 1895279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1896279002Smav changed = 1; 1897279002Smav } 1898279002Smav if (oldlun->l_blocksize != newlun->l_blocksize) { 1899279002Smav log_debugx("blocksize for lun \"%s\", " 1900279002Smav "CTL lun %d changed; removing", 1901279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1902279002Smav changed = 1; 1903279002Smav } 1904279002Smav if (newlun->l_device_id != NULL && 1905279002Smav (oldlun->l_device_id == NULL || 1906279002Smav strcmp(oldlun->l_device_id, newlun->l_device_id) != 1907279002Smav 0)) { 1908279002Smav log_debugx("device-id for lun \"%s\", " 1909279002Smav "CTL lun %d changed; removing", 1910279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1911279002Smav changed = 1; 1912279002Smav } 1913279002Smav if (newlun->l_path != NULL && 1914279002Smav (oldlun->l_path == NULL || 1915279002Smav strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1916279002Smav log_debugx("path for lun \"%s\", " 1917279002Smav "CTL lun %d, changed; removing", 1918279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1919279002Smav changed = 1; 1920279002Smav } 1921279002Smav if (newlun->l_serial != NULL && 1922279002Smav (oldlun->l_serial == NULL || 1923279002Smav strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1924279002Smav log_debugx("serial for lun \"%s\", " 1925279002Smav "CTL lun %d changed; removing", 1926279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1927279002Smav changed = 1; 1928279002Smav } 1929279002Smav if (changed) { 1930279002Smav error = kernel_lun_remove(oldlun); 1931279002Smav if (error != 0) { 1932279002Smav log_warnx("failed to remove lun \"%s\", " 1933279002Smav "CTL lun %d", 1934279002Smav oldlun->l_name, oldlun->l_ctl_lun); 1935279002Smav cumulated_error++; 1936255570Strasz } 1937279002Smav lun_delete(oldlun); 1938279002Smav continue; 1939279002Smav } 1940255570Strasz 1941279002Smav lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1942279002Smav } 1943279002Smav 1944279002Smav TAILQ_FOREACH_SAFE(newlun, &newconf->conf_luns, l_next, tmplun) { 1945279002Smav oldlun = lun_find(oldconf, newlun->l_name); 1946279002Smav if (oldlun != NULL) { 1947279002Smav if (newlun->l_size != oldlun->l_size || 1948279002Smav newlun->l_size == 0) { 1949279002Smav log_debugx("resizing lun \"%s\", CTL lun %d", 1950279002Smav newlun->l_name, newlun->l_ctl_lun); 1951279002Smav error = kernel_lun_resize(newlun); 1952255570Strasz if (error != 0) { 1953279002Smav log_warnx("failed to " 1954279002Smav "resize lun \"%s\", CTL lun %d", 1955279002Smav newlun->l_name, 1956279002Smav newlun->l_ctl_lun); 1957255570Strasz cumulated_error++; 1958255570Strasz } 1959255570Strasz } 1960279002Smav continue; 1961255570Strasz } 1962279002Smav log_debugx("adding lun \"%s\"", newlun->l_name); 1963279002Smav error = kernel_lun_add(newlun); 1964279002Smav if (error != 0) { 1965279002Smav log_warnx("failed to add lun \"%s\"", newlun->l_name); 1966279002Smav lun_delete(newlun); 1967279002Smav cumulated_error++; 1968279002Smav } 1969255570Strasz } 1970255570Strasz 1971255570Strasz /* 1972279006Smav * Now add new ports or modify existing ones. 1973255570Strasz */ 1974279006Smav TAILQ_FOREACH(newport, &newconf->conf_ports, p_next) { 1975279006Smav oldport = port_find(oldconf, newport->p_name); 1976255570Strasz 1977279006Smav if (oldport == NULL) { 1978279055Smav log_debugx("adding port \"%s\"", newport->p_name); 1979279006Smav error = kernel_port_add(newport); 1980279006Smav } else { 1981279055Smav log_debugx("updating port \"%s\"", newport->p_name); 1982279006Smav newport->p_ctl_port = oldport->p_ctl_port; 1983279006Smav error = kernel_port_update(newport); 1984277749Strasz } 1985279002Smav if (error != 0) { 1986279006Smav log_warnx("failed to %s port %s", 1987279006Smav (oldport == NULL) ? "add" : "update", 1988279006Smav newport->p_name); 1989279002Smav /* 1990279002Smav * XXX: Uncomment after fixing the root cause. 1991279002Smav * 1992279002Smav * cumulated_error++; 1993279002Smav */ 1994279002Smav } 1995255570Strasz } 1996255570Strasz 1997255570Strasz /* 1998255570Strasz * Go through the new portals, opening the sockets as neccessary. 1999255570Strasz */ 2000255570Strasz TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 2001255570Strasz if (newpg->pg_unassigned) { 2002255570Strasz log_debugx("not listening on portal-group \"%s\", " 2003255570Strasz "not assigned to any target", 2004255570Strasz newpg->pg_name); 2005255570Strasz continue; 2006255570Strasz } 2007255570Strasz TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 2008255570Strasz /* 2009255570Strasz * Try to find already open portal and reuse 2010255570Strasz * the listening socket. We don't care about 2011255570Strasz * what portal or portal group that was, what 2012255570Strasz * matters is the listening address. 2013255570Strasz */ 2014255570Strasz TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 2015255570Strasz pg_next) { 2016255570Strasz TAILQ_FOREACH(oldp, &oldpg->pg_portals, 2017255570Strasz p_next) { 2018255570Strasz if (strcmp(newp->p_listen, 2019255570Strasz oldp->p_listen) == 0 && 2020255570Strasz oldp->p_socket > 0) { 2021255570Strasz newp->p_socket = 2022255570Strasz oldp->p_socket; 2023255570Strasz oldp->p_socket = 0; 2024255570Strasz break; 2025255570Strasz } 2026255570Strasz } 2027255570Strasz } 2028255570Strasz if (newp->p_socket > 0) { 2029255570Strasz /* 2030255570Strasz * We're done with this portal. 2031255570Strasz */ 2032255570Strasz continue; 2033255570Strasz } 2034255570Strasz 2035255570Strasz#ifdef ICL_KERNEL_PROXY 2036265507Strasz if (proxy_mode) { 2037265509Strasz newpg->pg_conf->conf_portal_id++; 2038265509Strasz newp->p_id = newpg->pg_conf->conf_portal_id; 2039265509Strasz log_debugx("listening on %s, portal-group " 2040265509Strasz "\"%s\", portal id %d, using ICL proxy", 2041265509Strasz newp->p_listen, newpg->pg_name, newp->p_id); 2042265509Strasz kernel_listen(newp->p_ai, newp->p_iser, 2043265509Strasz newp->p_id); 2044265507Strasz continue; 2045265507Strasz } 2046265507Strasz#endif 2047265507Strasz assert(proxy_mode == false); 2048255570Strasz assert(newp->p_iser == false); 2049255570Strasz 2050255570Strasz log_debugx("listening on %s, portal-group \"%s\"", 2051255570Strasz newp->p_listen, newpg->pg_name); 2052255570Strasz newp->p_socket = socket(newp->p_ai->ai_family, 2053255570Strasz newp->p_ai->ai_socktype, 2054255570Strasz newp->p_ai->ai_protocol); 2055255570Strasz if (newp->p_socket < 0) { 2056255570Strasz log_warn("socket(2) failed for %s", 2057255570Strasz newp->p_listen); 2058255570Strasz cumulated_error++; 2059255570Strasz continue; 2060255570Strasz } 2061279001Smav sockbuf = SOCKBUF_SIZE; 2062279001Smav if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF, 2063279001Smav &sockbuf, sizeof(sockbuf)) == -1) 2064279001Smav log_warn("setsockopt(SO_RCVBUF) failed " 2065279001Smav "for %s", newp->p_listen); 2066279001Smav sockbuf = SOCKBUF_SIZE; 2067279001Smav if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF, 2068279001Smav &sockbuf, sizeof(sockbuf)) == -1) 2069279001Smav log_warn("setsockopt(SO_SNDBUF) failed " 2070279001Smav "for %s", newp->p_listen); 2071255570Strasz error = setsockopt(newp->p_socket, SOL_SOCKET, 2072255570Strasz SO_REUSEADDR, &one, sizeof(one)); 2073255570Strasz if (error != 0) { 2074255570Strasz log_warn("setsockopt(SO_REUSEADDR) failed " 2075255570Strasz "for %s", newp->p_listen); 2076255570Strasz close(newp->p_socket); 2077255570Strasz newp->p_socket = 0; 2078255570Strasz cumulated_error++; 2079255570Strasz continue; 2080255570Strasz } 2081255570Strasz error = bind(newp->p_socket, newp->p_ai->ai_addr, 2082255570Strasz newp->p_ai->ai_addrlen); 2083255570Strasz if (error != 0) { 2084255570Strasz log_warn("bind(2) failed for %s", 2085255570Strasz newp->p_listen); 2086255570Strasz close(newp->p_socket); 2087255570Strasz newp->p_socket = 0; 2088255570Strasz cumulated_error++; 2089255570Strasz continue; 2090255570Strasz } 2091255570Strasz error = listen(newp->p_socket, -1); 2092255570Strasz if (error != 0) { 2093255570Strasz log_warn("listen(2) failed for %s", 2094255570Strasz newp->p_listen); 2095255570Strasz close(newp->p_socket); 2096255570Strasz newp->p_socket = 0; 2097255570Strasz cumulated_error++; 2098255570Strasz continue; 2099255570Strasz } 2100255570Strasz } 2101255570Strasz } 2102255570Strasz 2103255570Strasz /* 2104255570Strasz * Go through the no longer used sockets, closing them. 2105255570Strasz */ 2106255570Strasz TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 2107255570Strasz TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 2108255570Strasz if (oldp->p_socket <= 0) 2109255570Strasz continue; 2110255570Strasz log_debugx("closing socket for %s, portal-group \"%s\"", 2111255570Strasz oldp->p_listen, oldpg->pg_name); 2112255570Strasz close(oldp->p_socket); 2113255570Strasz oldp->p_socket = 0; 2114255570Strasz } 2115255570Strasz } 2116255570Strasz 2117274939Smav /* (Re-)Register on remaining/new iSNS servers. */ 2118274939Smav TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) { 2119274939Smav TAILQ_FOREACH(oldns, &oldconf->conf_isns, i_next) { 2120274939Smav if (strcmp(oldns->i_addr, newns->i_addr) == 0) 2121274939Smav break; 2122274939Smav } 2123274939Smav isns_register(newns, oldns); 2124274939Smav } 2125274939Smav 2126274939Smav /* Schedule iSNS update */ 2127274939Smav if (!TAILQ_EMPTY(&newconf->conf_isns)) 2128274939Smav set_timeout((newconf->conf_isns_period + 2) / 3, false); 2129274939Smav 2130255570Strasz return (cumulated_error); 2131255570Strasz} 2132255570Strasz 2133255570Straszbool 2134255570Strasztimed_out(void) 2135255570Strasz{ 2136255570Strasz 2137255570Strasz return (sigalrm_received); 2138255570Strasz} 2139255570Strasz 2140255570Straszstatic void 2141274939Smavsigalrm_handler_fatal(int dummy __unused) 2142255570Strasz{ 2143255570Strasz /* 2144255570Strasz * It would be easiest to just log an error and exit. We can't 2145255570Strasz * do this, though, because log_errx() is not signal safe, since 2146255570Strasz * it calls syslog(3). Instead, set a flag checked by pdu_send() 2147255570Strasz * and pdu_receive(), to call log_errx() there. Should they fail 2148255570Strasz * to notice, we'll exit here one second later. 2149255570Strasz */ 2150255570Strasz if (sigalrm_received) { 2151255570Strasz /* 2152255570Strasz * Oh well. Just give up and quit. 2153255570Strasz */ 2154255570Strasz _exit(2); 2155255570Strasz } 2156255570Strasz 2157255570Strasz sigalrm_received = true; 2158255570Strasz} 2159255570Strasz 2160255570Straszstatic void 2161274939Smavsigalrm_handler(int dummy __unused) 2162255570Strasz{ 2163274939Smav 2164274939Smav sigalrm_received = true; 2165274939Smav} 2166274939Smav 2167274939Smavvoid 2168274939Smavset_timeout(int timeout, int fatal) 2169274939Smav{ 2170255570Strasz struct sigaction sa; 2171255570Strasz struct itimerval itv; 2172255570Strasz int error; 2173255570Strasz 2174274939Smav if (timeout <= 0) { 2175255570Strasz log_debugx("session timeout disabled"); 2176274939Smav bzero(&itv, sizeof(itv)); 2177274939Smav error = setitimer(ITIMER_REAL, &itv, NULL); 2178274939Smav if (error != 0) 2179274939Smav log_err(1, "setitimer"); 2180274939Smav sigalrm_received = false; 2181255570Strasz return; 2182255570Strasz } 2183255570Strasz 2184274939Smav sigalrm_received = false; 2185255570Strasz bzero(&sa, sizeof(sa)); 2186274939Smav if (fatal) 2187274939Smav sa.sa_handler = sigalrm_handler_fatal; 2188274939Smav else 2189274939Smav sa.sa_handler = sigalrm_handler; 2190255570Strasz sigfillset(&sa.sa_mask); 2191255570Strasz error = sigaction(SIGALRM, &sa, NULL); 2192255570Strasz if (error != 0) 2193255570Strasz log_err(1, "sigaction"); 2194255570Strasz 2195255570Strasz /* 2196255570Strasz * First SIGALRM will arive after conf_timeout seconds. 2197255570Strasz * If we do nothing, another one will arrive a second later. 2198255570Strasz */ 2199274939Smav log_debugx("setting session timeout to %d seconds", timeout); 2200255570Strasz bzero(&itv, sizeof(itv)); 2201255570Strasz itv.it_interval.tv_sec = 1; 2202274939Smav itv.it_value.tv_sec = timeout; 2203255570Strasz error = setitimer(ITIMER_REAL, &itv, NULL); 2204255570Strasz if (error != 0) 2205255570Strasz log_err(1, "setitimer"); 2206255570Strasz} 2207255570Strasz 2208255570Straszstatic int 2209255570Straszwait_for_children(bool block) 2210255570Strasz{ 2211255570Strasz pid_t pid; 2212255570Strasz int status; 2213255570Strasz int num = 0; 2214255570Strasz 2215255570Strasz for (;;) { 2216255570Strasz /* 2217255570Strasz * If "block" is true, wait for at least one process. 2218255570Strasz */ 2219255570Strasz if (block && num == 0) 2220255570Strasz pid = wait4(-1, &status, 0, NULL); 2221255570Strasz else 2222255570Strasz pid = wait4(-1, &status, WNOHANG, NULL); 2223255570Strasz if (pid <= 0) 2224255570Strasz break; 2225255570Strasz if (WIFSIGNALED(status)) { 2226255570Strasz log_warnx("child process %d terminated with signal %d", 2227255570Strasz pid, WTERMSIG(status)); 2228255570Strasz } else if (WEXITSTATUS(status) != 0) { 2229255570Strasz log_warnx("child process %d terminated with exit status %d", 2230255570Strasz pid, WEXITSTATUS(status)); 2231255570Strasz } else { 2232255570Strasz log_debugx("child process %d terminated gracefully", pid); 2233255570Strasz } 2234255570Strasz num++; 2235255570Strasz } 2236255570Strasz 2237255570Strasz return (num); 2238255570Strasz} 2239255570Strasz 2240255570Straszstatic void 2241265513Straszhandle_connection(struct portal *portal, int fd, 2242270137Smav const struct sockaddr *client_sa, bool dont_fork) 2243255570Strasz{ 2244255570Strasz struct connection *conn; 2245255570Strasz int error; 2246255570Strasz pid_t pid; 2247255570Strasz char host[NI_MAXHOST + 1]; 2248255570Strasz struct conf *conf; 2249255570Strasz 2250255570Strasz conf = portal->p_portal_group->pg_conf; 2251255570Strasz 2252255570Strasz if (dont_fork) { 2253255570Strasz log_debugx("incoming connection; not forking due to -d flag"); 2254255570Strasz } else { 2255255570Strasz nchildren -= wait_for_children(false); 2256255570Strasz assert(nchildren >= 0); 2257255570Strasz 2258255570Strasz while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 2259255570Strasz log_debugx("maxproc limit of %d child processes hit; " 2260255570Strasz "waiting for child process to exit", conf->conf_maxproc); 2261255570Strasz nchildren -= wait_for_children(true); 2262255570Strasz assert(nchildren >= 0); 2263255570Strasz } 2264255570Strasz log_debugx("incoming connection; forking child process #%d", 2265255570Strasz nchildren); 2266255570Strasz nchildren++; 2267255570Strasz pid = fork(); 2268255570Strasz if (pid < 0) 2269255570Strasz log_err(1, "fork"); 2270255570Strasz if (pid > 0) { 2271255570Strasz close(fd); 2272255570Strasz return; 2273255570Strasz } 2274255570Strasz } 2275255570Strasz pidfile_close(conf->conf_pidfh); 2276255570Strasz 2277270137Smav error = getnameinfo(client_sa, client_sa->sa_len, 2278265513Strasz host, sizeof(host), NULL, 0, NI_NUMERICHOST); 2279265513Strasz if (error != 0) 2280265513Strasz log_errx(1, "getnameinfo: %s", gai_strerror(error)); 2281255570Strasz 2282265513Strasz log_debugx("accepted connection from %s; portal group \"%s\"", 2283265513Strasz host, portal->p_portal_group->pg_name); 2284265513Strasz log_set_peer_addr(host); 2285265513Strasz setproctitle("%s", host); 2286255570Strasz 2287270137Smav conn = connection_new(portal, fd, host, client_sa); 2288274939Smav set_timeout(conf->conf_timeout, true); 2289255570Strasz kernel_capsicate(); 2290255570Strasz login(conn); 2291255570Strasz if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 2292255570Strasz kernel_handoff(conn); 2293255570Strasz log_debugx("connection handed off to the kernel"); 2294255570Strasz } else { 2295255570Strasz assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 2296255570Strasz discovery(conn); 2297255570Strasz } 2298255570Strasz log_debugx("nothing more to do; exiting"); 2299255570Strasz exit(0); 2300255570Strasz} 2301255570Strasz 2302255570Straszstatic int 2303255570Straszfd_add(int fd, fd_set *fdset, int nfds) 2304255570Strasz{ 2305255570Strasz 2306255570Strasz /* 2307255570Strasz * Skip sockets which we failed to bind. 2308255570Strasz */ 2309255570Strasz if (fd <= 0) 2310255570Strasz return (nfds); 2311255570Strasz 2312255570Strasz FD_SET(fd, fdset); 2313255570Strasz if (fd > nfds) 2314255570Strasz nfds = fd; 2315255570Strasz return (nfds); 2316255570Strasz} 2317255570Strasz 2318255570Straszstatic void 2319255570Straszmain_loop(struct conf *conf, bool dont_fork) 2320255570Strasz{ 2321255570Strasz struct portal_group *pg; 2322255570Strasz struct portal *portal; 2323265512Strasz struct sockaddr_storage client_sa; 2324265512Strasz socklen_t client_salen; 2325255570Strasz#ifdef ICL_KERNEL_PROXY 2326255570Strasz int connection_id; 2327265509Strasz int portal_id; 2328265507Strasz#endif 2329255570Strasz fd_set fdset; 2330255570Strasz int error, nfds, client_fd; 2331255570Strasz 2332255570Strasz pidfile_write(conf->conf_pidfh); 2333255570Strasz 2334255570Strasz for (;;) { 2335274939Smav if (sighup_received || sigterm_received || timed_out()) 2336255570Strasz return; 2337255570Strasz 2338255570Strasz#ifdef ICL_KERNEL_PROXY 2339265507Strasz if (proxy_mode) { 2340265513Strasz client_salen = sizeof(client_sa); 2341265513Strasz kernel_accept(&connection_id, &portal_id, 2342265513Strasz (struct sockaddr *)&client_sa, &client_salen); 2343271627Strasz assert(client_salen >= client_sa.ss_len); 2344255570Strasz 2345265509Strasz log_debugx("incoming connection, id %d, portal id %d", 2346265509Strasz connection_id, portal_id); 2347265509Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2348265509Strasz TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2349265509Strasz if (portal->p_id == portal_id) { 2350265509Strasz goto found; 2351265509Strasz } 2352265509Strasz } 2353265509Strasz } 2354255570Strasz 2355265509Strasz log_errx(1, "kernel returned invalid portal_id %d", 2356265509Strasz portal_id); 2357265509Strasz 2358265509Straszfound: 2359265513Strasz handle_connection(portal, connection_id, 2360270137Smav (struct sockaddr *)&client_sa, dont_fork); 2361265507Strasz } else { 2362265507Strasz#endif 2363265507Strasz assert(proxy_mode == false); 2364265507Strasz 2365265507Strasz FD_ZERO(&fdset); 2366265507Strasz nfds = 0; 2367265507Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2368265507Strasz TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 2369265507Strasz nfds = fd_add(portal->p_socket, &fdset, nfds); 2370255570Strasz } 2371265507Strasz error = select(nfds + 1, &fdset, NULL, NULL, NULL); 2372265507Strasz if (error <= 0) { 2373265507Strasz if (errno == EINTR) 2374265507Strasz return; 2375265507Strasz log_err(1, "select"); 2376265507Strasz } 2377265507Strasz TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 2378265507Strasz TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 2379265507Strasz if (!FD_ISSET(portal->p_socket, &fdset)) 2380265507Strasz continue; 2381265512Strasz client_salen = sizeof(client_sa); 2382265512Strasz client_fd = accept(portal->p_socket, 2383265512Strasz (struct sockaddr *)&client_sa, 2384265512Strasz &client_salen); 2385281488Smav if (client_fd < 0) { 2386281488Smav if (errno == ECONNABORTED) 2387281488Smav continue; 2388265507Strasz log_err(1, "accept"); 2389281488Smav } 2390271627Strasz assert(client_salen >= client_sa.ss_len); 2391271627Strasz 2392265512Strasz handle_connection(portal, client_fd, 2393265513Strasz (struct sockaddr *)&client_sa, 2394270137Smav dont_fork); 2395265507Strasz break; 2396265507Strasz } 2397265507Strasz } 2398265507Strasz#ifdef ICL_KERNEL_PROXY 2399255570Strasz } 2400265507Strasz#endif 2401255570Strasz } 2402255570Strasz} 2403255570Strasz 2404255570Straszstatic void 2405255570Straszsighup_handler(int dummy __unused) 2406255570Strasz{ 2407255570Strasz 2408255570Strasz sighup_received = true; 2409255570Strasz} 2410255570Strasz 2411255570Straszstatic void 2412255570Straszsigterm_handler(int dummy __unused) 2413255570Strasz{ 2414255570Strasz 2415255570Strasz sigterm_received = true; 2416255570Strasz} 2417255570Strasz 2418255570Straszstatic void 2419263730Straszsigchld_handler(int dummy __unused) 2420263730Strasz{ 2421263730Strasz 2422263730Strasz /* 2423263730Strasz * The only purpose of this handler is to make SIGCHLD 2424263730Strasz * interrupt the ISCSIDWAIT ioctl(2), so we can call 2425263730Strasz * wait_for_children(). 2426263730Strasz */ 2427263730Strasz} 2428263730Strasz 2429263730Straszstatic void 2430255570Straszregister_signals(void) 2431255570Strasz{ 2432255570Strasz struct sigaction sa; 2433255570Strasz int error; 2434255570Strasz 2435255570Strasz bzero(&sa, sizeof(sa)); 2436255570Strasz sa.sa_handler = sighup_handler; 2437255570Strasz sigfillset(&sa.sa_mask); 2438255570Strasz error = sigaction(SIGHUP, &sa, NULL); 2439255570Strasz if (error != 0) 2440255570Strasz log_err(1, "sigaction"); 2441255570Strasz 2442255570Strasz sa.sa_handler = sigterm_handler; 2443255570Strasz error = sigaction(SIGTERM, &sa, NULL); 2444255570Strasz if (error != 0) 2445255570Strasz log_err(1, "sigaction"); 2446255570Strasz 2447255570Strasz sa.sa_handler = sigterm_handler; 2448255570Strasz error = sigaction(SIGINT, &sa, NULL); 2449255570Strasz if (error != 0) 2450255570Strasz log_err(1, "sigaction"); 2451263730Strasz 2452263730Strasz sa.sa_handler = sigchld_handler; 2453263730Strasz error = sigaction(SIGCHLD, &sa, NULL); 2454263730Strasz if (error != 0) 2455263730Strasz log_err(1, "sigaction"); 2456255570Strasz} 2457255570Strasz 2458255570Straszint 2459255570Straszmain(int argc, char **argv) 2460255570Strasz{ 2461255570Strasz struct conf *oldconf, *newconf, *tmpconf; 2462274939Smav struct isns *newns; 2463255570Strasz const char *config_path = DEFAULT_CONFIG_PATH; 2464255570Strasz int debug = 0, ch, error; 2465255570Strasz bool dont_daemonize = false; 2466255570Strasz 2467265507Strasz while ((ch = getopt(argc, argv, "df:R")) != -1) { 2468255570Strasz switch (ch) { 2469255570Strasz case 'd': 2470255570Strasz dont_daemonize = true; 2471255570Strasz debug++; 2472255570Strasz break; 2473255570Strasz case 'f': 2474255570Strasz config_path = optarg; 2475255570Strasz break; 2476265507Strasz case 'R': 2477265507Strasz#ifndef ICL_KERNEL_PROXY 2478265507Strasz log_errx(1, "ctld(8) compiled without ICL_KERNEL_PROXY " 2479265507Strasz "does not support iSER protocol"); 2480265507Strasz#endif 2481265507Strasz proxy_mode = true; 2482265507Strasz break; 2483255570Strasz case '?': 2484255570Strasz default: 2485255570Strasz usage(); 2486255570Strasz } 2487255570Strasz } 2488255570Strasz argc -= optind; 2489255570Strasz if (argc != 0) 2490255570Strasz usage(); 2491255570Strasz 2492255570Strasz log_init(debug); 2493255570Strasz kernel_init(); 2494255570Strasz 2495255570Strasz oldconf = conf_new_from_kernel(); 2496279055Smav newconf = conf_new_from_file(config_path, oldconf); 2497255570Strasz if (newconf == NULL) 2498265516Strasz log_errx(1, "configuration error; exiting"); 2499255570Strasz if (debug > 0) { 2500255570Strasz oldconf->conf_debug = debug; 2501255570Strasz newconf->conf_debug = debug; 2502255570Strasz } 2503255570Strasz 2504255570Strasz error = conf_apply(oldconf, newconf); 2505255570Strasz if (error != 0) 2506265516Strasz log_errx(1, "failed to apply configuration; exiting"); 2507265516Strasz 2508255570Strasz conf_delete(oldconf); 2509255570Strasz oldconf = NULL; 2510255570Strasz 2511255570Strasz register_signals(); 2512255570Strasz 2513263719Strasz if (dont_daemonize == false) { 2514263719Strasz log_debugx("daemonizing"); 2515263719Strasz if (daemon(0, 0) == -1) { 2516263719Strasz log_warn("cannot daemonize"); 2517263719Strasz pidfile_remove(newconf->conf_pidfh); 2518263719Strasz exit(1); 2519263719Strasz } 2520263719Strasz } 2521263719Strasz 2522274939Smav /* Schedule iSNS update */ 2523274939Smav if (!TAILQ_EMPTY(&newconf->conf_isns)) 2524274939Smav set_timeout((newconf->conf_isns_period + 2) / 3, false); 2525274939Smav 2526255570Strasz for (;;) { 2527255570Strasz main_loop(newconf, dont_daemonize); 2528255570Strasz if (sighup_received) { 2529255570Strasz sighup_received = false; 2530255570Strasz log_debugx("received SIGHUP, reloading configuration"); 2531279055Smav tmpconf = conf_new_from_file(config_path, newconf); 2532255570Strasz if (tmpconf == NULL) { 2533255570Strasz log_warnx("configuration error, " 2534255570Strasz "continuing with old configuration"); 2535255570Strasz } else { 2536255570Strasz if (debug > 0) 2537255570Strasz tmpconf->conf_debug = debug; 2538255570Strasz oldconf = newconf; 2539255570Strasz newconf = tmpconf; 2540255570Strasz error = conf_apply(oldconf, newconf); 2541255570Strasz if (error != 0) 2542255570Strasz log_warnx("failed to reload " 2543255570Strasz "configuration"); 2544255570Strasz conf_delete(oldconf); 2545255570Strasz oldconf = NULL; 2546255570Strasz } 2547255570Strasz } else if (sigterm_received) { 2548255570Strasz log_debugx("exiting on signal; " 2549255570Strasz "reloading empty configuration"); 2550255570Strasz 2551279003Smav log_debugx("removing CTL iSCSI ports " 2552255570Strasz "and terminating all connections"); 2553255570Strasz 2554255570Strasz oldconf = newconf; 2555255570Strasz newconf = conf_new(); 2556255570Strasz if (debug > 0) 2557255570Strasz newconf->conf_debug = debug; 2558255570Strasz error = conf_apply(oldconf, newconf); 2559255570Strasz if (error != 0) 2560255570Strasz log_warnx("failed to apply configuration"); 2561274939Smav conf_delete(oldconf); 2562274939Smav oldconf = NULL; 2563255570Strasz 2564255570Strasz log_warnx("exiting on signal"); 2565255570Strasz exit(0); 2566255570Strasz } else { 2567255570Strasz nchildren -= wait_for_children(false); 2568255570Strasz assert(nchildren >= 0); 2569274939Smav if (timed_out()) { 2570274939Smav set_timeout(0, false); 2571274939Smav TAILQ_FOREACH(newns, &newconf->conf_isns, i_next) 2572274939Smav isns_check(newns); 2573274939Smav /* Schedule iSNS update */ 2574274939Smav if (!TAILQ_EMPTY(&newconf->conf_isns)) { 2575274939Smav set_timeout((newconf->conf_isns_period 2576274939Smav + 2) / 3, 2577274939Smav false); 2578274939Smav } 2579274939Smav } 2580255570Strasz } 2581255570Strasz } 2582255570Strasz /* NOTREACHED */ 2583255570Strasz} 2584