1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * 6 * This software was developed by Edward Tomasz Napierala under sponsorship 7 * from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #include <sys/types.h> 33 #include <assert.h> 34 #include <stddef.h> 35 #include <stdint.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <stdbool.h> 39 #include <string.h> 40 #include <cam/scsi/scsi_all.h> 41 42 #include <libutil++.hh> 43 44 #include "conf.h" 45 #include "ctld.hh" 46 47 static struct conf *conf = NULL; 48 static struct auth_group *auth_group = NULL; 49 static struct portal_group *portal_group = NULL; 50 static struct target *target = NULL; 51 static struct lun *lun = NULL; 52 53 void 54 conf_start(struct conf *new_conf) 55 { 56 assert(conf == NULL); 57 conf = new_conf; 58 } 59 60 void 61 conf_finish(void) 62 { 63 auth_group = NULL; 64 portal_group = NULL; 65 target = NULL; 66 lun = NULL; 67 conf = NULL; 68 } 69 70 bool 71 isns_add_server(const char *addr) 72 { 73 return (conf->add_isns(addr)); 74 } 75 76 void 77 conf_set_debug(int debug) 78 { 79 conf->set_debug(debug); 80 } 81 82 void 83 conf_set_isns_period(int period) 84 { 85 conf->set_isns_period(period); 86 } 87 88 void 89 conf_set_isns_timeout(int timeout) 90 { 91 conf->set_isns_timeout(timeout); 92 } 93 94 void 95 conf_set_maxproc(int maxproc) 96 { 97 conf->set_maxproc(maxproc); 98 } 99 100 bool 101 conf_set_pidfile_path(const char *path) 102 { 103 return (conf->set_pidfile_path(path)); 104 } 105 106 void 107 conf_set_timeout(int timeout) 108 { 109 conf->set_timeout(timeout); 110 } 111 112 bool 113 auth_group_add_chap(const char *user, const char *secret) 114 { 115 return (auth_group->add_chap(user, secret)); 116 } 117 118 bool 119 auth_group_add_chap_mutual(const char *user, const char *secret, 120 const char *user2, const char *secret2) 121 { 122 return (auth_group->add_chap_mutual(user, secret, user2, secret2)); 123 } 124 125 bool 126 auth_group_add_host_address(const char *portal) 127 { 128 return (auth_group->add_host_address(portal)); 129 } 130 131 bool 132 auth_group_add_host_nqn(const char *name) 133 { 134 return (auth_group->add_host_nqn(name)); 135 } 136 137 bool 138 auth_group_add_initiator_name(const char *name) 139 { 140 return (auth_group->add_initiator_name(name)); 141 } 142 143 bool 144 auth_group_add_initiator_portal(const char *portal) 145 { 146 return (auth_group->add_initiator_portal(portal)); 147 } 148 149 bool 150 auth_group_set_type(const char *type) 151 { 152 return (auth_group->set_type(type)); 153 } 154 155 bool 156 auth_group_start(const char *name) 157 { 158 if (strcmp(name, "default") == 0) 159 auth_group = conf->define_default_auth_group(); 160 else 161 auth_group = conf->add_auth_group(name); 162 return (auth_group != nullptr); 163 } 164 165 void 166 auth_group_finish(void) 167 { 168 auth_group = NULL; 169 } 170 171 bool 172 portal_group_start(const char *name) 173 { 174 if (strcmp(name, "default") == 0) 175 portal_group = conf->define_default_portal_group(); 176 else 177 portal_group = conf->add_portal_group(name); 178 return (portal_group != NULL); 179 } 180 181 void 182 portal_group_finish(void) 183 { 184 portal_group = NULL; 185 } 186 187 bool 188 portal_group_add_listen(const char *listen, bool iser) 189 { 190 return (portal_group->add_portal(listen, iser ? portal_protocol::ISER : 191 portal_protocol::ISCSI)); 192 } 193 194 bool 195 portal_group_add_option(const char *name, const char *value) 196 { 197 return (portal_group->add_option(name, value)); 198 } 199 200 bool 201 portal_group_set_discovery_auth_group(const char *name) 202 { 203 return (portal_group->set_discovery_auth_group(name)); 204 } 205 206 bool 207 portal_group_set_dscp(u_int dscp) 208 { 209 return (portal_group->set_dscp(dscp)); 210 } 211 212 bool 213 portal_group_set_filter(const char *str) 214 { 215 return (portal_group->set_filter(str)); 216 } 217 218 void 219 portal_group_set_foreign(void) 220 { 221 portal_group->set_foreign(); 222 } 223 224 bool 225 portal_group_set_offload(const char *offload) 226 { 227 return (portal_group->set_offload(offload)); 228 } 229 230 bool 231 portal_group_set_pcp(u_int pcp) 232 { 233 return (portal_group->set_pcp(pcp)); 234 } 235 236 bool 237 portal_group_set_redirection(const char *addr) 238 { 239 return (portal_group->set_redirection(addr)); 240 } 241 242 void 243 portal_group_set_tag(uint16_t tag) 244 { 245 portal_group->set_tag(tag); 246 } 247 248 bool 249 transport_group_start(const char *name) 250 { 251 if (strcmp(name, "default") == 0) 252 portal_group = conf->define_default_transport_group(); 253 else 254 portal_group = conf->add_transport_group(name); 255 return (portal_group != NULL); 256 } 257 258 bool 259 transport_group_add_listen_discovery_tcp(const char *listen) 260 { 261 return portal_group->add_portal(listen, 262 portal_protocol::NVME_DISCOVERY_TCP); 263 } 264 265 bool 266 transport_group_add_listen_tcp(const char *listen) 267 { 268 return portal_group->add_portal(listen, portal_protocol::NVME_TCP); 269 } 270 271 bool 272 lun_start(const char *name) 273 { 274 lun = conf->add_lun(name); 275 return (lun != NULL); 276 } 277 278 void 279 lun_finish(void) 280 { 281 lun = NULL; 282 } 283 284 bool 285 lun_add_option(const char *name, const char *value) 286 { 287 return (lun->add_option(name, value)); 288 } 289 290 bool 291 lun_set_backend(const char *value) 292 { 293 return (lun->set_backend(value)); 294 } 295 296 bool 297 lun_set_blocksize(size_t value) 298 { 299 return (lun->set_blocksize(value)); 300 } 301 302 bool 303 lun_set_device_type(const char *value) 304 { 305 return (lun->set_device_type(value)); 306 } 307 308 bool 309 lun_set_device_id(const char *value) 310 { 311 return (lun->set_device_id(value)); 312 } 313 314 bool 315 lun_set_path(const char *value) 316 { 317 return (lun->set_path(value)); 318 } 319 320 bool 321 lun_set_serial(const char *value) 322 { 323 return (lun->set_serial(value)); 324 } 325 326 bool 327 lun_set_size(uint64_t value) 328 { 329 return (lun->set_size(value)); 330 } 331 332 bool 333 lun_set_ctl_lun(uint32_t value) 334 { 335 return (lun->set_ctl_lun(value)); 336 } 337 338 bool 339 target_start(const char *name) 340 { 341 target = conf->add_target(name); 342 return (target != NULL); 343 } 344 345 void 346 target_finish(void) 347 { 348 target = NULL; 349 } 350 351 bool 352 target_add_chap(const char *user, const char *secret) 353 { 354 return (target->add_chap(user, secret)); 355 } 356 357 bool 358 target_add_chap_mutual(const char *user, const char *secret, 359 const char *user2, const char *secret2) 360 { 361 return (target->add_chap_mutual(user, secret, user2, secret2)); 362 } 363 364 bool 365 target_add_initiator_name(const char *name) 366 { 367 return (target->add_initiator_name(name)); 368 } 369 370 bool 371 target_add_initiator_portal(const char *addr) 372 { 373 return (target->add_initiator_portal(addr)); 374 } 375 376 bool 377 target_add_lun(u_int id, const char *name) 378 { 379 return (target->add_lun(id, name)); 380 } 381 382 bool 383 target_add_portal_group(const char *pg_name, const char *ag_name) 384 { 385 return (target->add_portal_group(pg_name, ag_name)); 386 } 387 388 bool 389 target_set_alias(const char *alias) 390 { 391 return (target->set_alias(alias)); 392 } 393 394 bool 395 target_set_auth_group(const char *name) 396 { 397 return (target->set_auth_group(name)); 398 } 399 400 bool 401 target_set_auth_type(const char *type) 402 { 403 return (target->set_auth_type(type)); 404 } 405 406 bool 407 target_set_physical_port(const char *pport) 408 { 409 return (target->set_physical_port(pport)); 410 } 411 412 bool 413 target_set_redirection(const char *addr) 414 { 415 return (target->set_redirection(addr)); 416 } 417 418 bool 419 target_start_lun(u_int id) 420 { 421 lun = target->start_lun(id); 422 return (lun != nullptr); 423 } 424 425 bool 426 controller_start(const char *name) 427 { 428 target = conf->add_controller(name); 429 return (target != nullptr); 430 } 431 432 bool 433 controller_add_host_address(const char *addr) 434 { 435 return (target->add_host_address(addr)); 436 } 437 438 bool 439 controller_add_host_nqn(const char *name) 440 { 441 return (target->add_host_nqn(name)); 442 } 443 444 bool 445 controller_add_namespace(u_int id, const char *name) 446 { 447 return (target->add_namespace(id, name)); 448 } 449 450 bool 451 controller_start_namespace(u_int id) 452 { 453 lun = target->start_namespace(id); 454 return (lun != nullptr); 455 } 456 457 bool 458 parse_conf(const char *path) 459 { 460 freebsd::FILE_up fp(fopen(path, "r")); 461 if (fp == nullptr) { 462 log_warn("unable to open configuration file %s", path); 463 return (false); 464 } 465 466 bool parsed; 467 try { 468 parsed = yyparse_conf(fp.get()); 469 } catch (std::bad_alloc &) { 470 log_warnx("failed to allocate memory parsing %s", path); 471 return (false); 472 } catch (...) { 473 log_warnx("unknown exception parsing %s", path); 474 return (false); 475 } 476 477 return (parsed); 478 } 479