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
conf_start(struct conf * new_conf)54 conf_start(struct conf *new_conf)
55 {
56 assert(conf == NULL);
57 conf = new_conf;
58 }
59
60 void
conf_finish(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
isns_add_server(const char * addr)71 isns_add_server(const char *addr)
72 {
73 return (conf->add_isns(addr));
74 }
75
76 void
conf_set_debug(int debug)77 conf_set_debug(int debug)
78 {
79 conf->set_debug(debug);
80 }
81
82 void
conf_set_isns_period(int period)83 conf_set_isns_period(int period)
84 {
85 conf->set_isns_period(period);
86 }
87
88 void
conf_set_isns_timeout(int timeout)89 conf_set_isns_timeout(int timeout)
90 {
91 conf->set_isns_timeout(timeout);
92 }
93
94 void
conf_set_maxproc(int maxproc)95 conf_set_maxproc(int maxproc)
96 {
97 conf->set_maxproc(maxproc);
98 }
99
100 bool
conf_set_pidfile_path(const char * path)101 conf_set_pidfile_path(const char *path)
102 {
103 return (conf->set_pidfile_path(path));
104 }
105
106 void
conf_set_timeout(int timeout)107 conf_set_timeout(int timeout)
108 {
109 conf->set_timeout(timeout);
110 }
111
112 bool
auth_group_add_chap(const char * user,const char * secret)113 auth_group_add_chap(const char *user, const char *secret)
114 {
115 return (auth_group->add_chap(user, secret));
116 }
117
118 bool
auth_group_add_chap_mutual(const char * user,const char * secret,const char * user2,const char * secret2)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
auth_group_add_host_address(const char * portal)126 auth_group_add_host_address(const char *portal)
127 {
128 return (auth_group->add_host_address(portal));
129 }
130
131 bool
auth_group_add_host_nqn(const char * name)132 auth_group_add_host_nqn(const char *name)
133 {
134 return (auth_group->add_host_nqn(name));
135 }
136
137 bool
auth_group_add_initiator_name(const char * name)138 auth_group_add_initiator_name(const char *name)
139 {
140 return (auth_group->add_initiator_name(name));
141 }
142
143 bool
auth_group_add_initiator_portal(const char * portal)144 auth_group_add_initiator_portal(const char *portal)
145 {
146 return (auth_group->add_initiator_portal(portal));
147 }
148
149 bool
auth_group_set_type(const char * type)150 auth_group_set_type(const char *type)
151 {
152 return (auth_group->set_type(type));
153 }
154
155 bool
auth_group_start(const char * name)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
auth_group_finish(void)166 auth_group_finish(void)
167 {
168 auth_group = NULL;
169 }
170
171 bool
portal_group_start(const char * name)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
portal_group_finish(void)182 portal_group_finish(void)
183 {
184 portal_group = NULL;
185 }
186
187 bool
portal_group_add_listen(const char * listen,bool iser)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
portal_group_add_option(const char * name,const char * value)195 portal_group_add_option(const char *name, const char *value)
196 {
197 return (portal_group->add_option(name, value));
198 }
199
200 bool
portal_group_set_discovery_auth_group(const char * name)201 portal_group_set_discovery_auth_group(const char *name)
202 {
203 return (portal_group->set_discovery_auth_group(name));
204 }
205
206 bool
portal_group_set_dscp(u_int dscp)207 portal_group_set_dscp(u_int dscp)
208 {
209 return (portal_group->set_dscp(dscp));
210 }
211
212 bool
portal_group_set_filter(const char * str)213 portal_group_set_filter(const char *str)
214 {
215 return (portal_group->set_filter(str));
216 }
217
218 void
portal_group_set_foreign(void)219 portal_group_set_foreign(void)
220 {
221 portal_group->set_foreign();
222 }
223
224 bool
portal_group_set_offload(const char * offload)225 portal_group_set_offload(const char *offload)
226 {
227 return (portal_group->set_offload(offload));
228 }
229
230 bool
portal_group_set_pcp(u_int pcp)231 portal_group_set_pcp(u_int pcp)
232 {
233 return (portal_group->set_pcp(pcp));
234 }
235
236 bool
portal_group_set_redirection(const char * addr)237 portal_group_set_redirection(const char *addr)
238 {
239 return (portal_group->set_redirection(addr));
240 }
241
242 void
portal_group_set_tag(uint16_t tag)243 portal_group_set_tag(uint16_t tag)
244 {
245 portal_group->set_tag(tag);
246 }
247
248 bool
transport_group_start(const char * name)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
transport_group_add_listen_discovery_tcp(const char * listen)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
transport_group_add_listen_tcp(const char * listen)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
lun_start(const char * name)272 lun_start(const char *name)
273 {
274 lun = conf->add_lun(name);
275 return (lun != NULL);
276 }
277
278 void
lun_finish(void)279 lun_finish(void)
280 {
281 lun = NULL;
282 }
283
284 bool
lun_add_option(const char * name,const char * value)285 lun_add_option(const char *name, const char *value)
286 {
287 return (lun->add_option(name, value));
288 }
289
290 bool
lun_set_backend(const char * value)291 lun_set_backend(const char *value)
292 {
293 return (lun->set_backend(value));
294 }
295
296 bool
lun_set_blocksize(size_t value)297 lun_set_blocksize(size_t value)
298 {
299 return (lun->set_blocksize(value));
300 }
301
302 bool
lun_set_device_type(const char * value)303 lun_set_device_type(const char *value)
304 {
305 return (lun->set_device_type(value));
306 }
307
308 bool
lun_set_device_id(const char * value)309 lun_set_device_id(const char *value)
310 {
311 return (lun->set_device_id(value));
312 }
313
314 bool
lun_set_path(const char * value)315 lun_set_path(const char *value)
316 {
317 return (lun->set_path(value));
318 }
319
320 bool
lun_set_serial(const char * value)321 lun_set_serial(const char *value)
322 {
323 return (lun->set_serial(value));
324 }
325
326 bool
lun_set_size(uint64_t value)327 lun_set_size(uint64_t value)
328 {
329 return (lun->set_size(value));
330 }
331
332 bool
lun_set_ctl_lun(uint32_t value)333 lun_set_ctl_lun(uint32_t value)
334 {
335 return (lun->set_ctl_lun(value));
336 }
337
338 bool
target_start(const char * name)339 target_start(const char *name)
340 {
341 target = conf->add_target(name);
342 return (target != NULL);
343 }
344
345 void
target_finish(void)346 target_finish(void)
347 {
348 target = NULL;
349 }
350
351 bool
target_add_chap(const char * user,const char * secret)352 target_add_chap(const char *user, const char *secret)
353 {
354 return (target->add_chap(user, secret));
355 }
356
357 bool
target_add_chap_mutual(const char * user,const char * secret,const char * user2,const char * secret2)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
target_add_initiator_name(const char * name)365 target_add_initiator_name(const char *name)
366 {
367 return (target->add_initiator_name(name));
368 }
369
370 bool
target_add_initiator_portal(const char * addr)371 target_add_initiator_portal(const char *addr)
372 {
373 return (target->add_initiator_portal(addr));
374 }
375
376 bool
target_add_lun(u_int id,const char * name)377 target_add_lun(u_int id, const char *name)
378 {
379 return (target->add_lun(id, name));
380 }
381
382 bool
target_add_portal_group(const char * pg_name,const char * ag_name)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
target_set_alias(const char * alias)389 target_set_alias(const char *alias)
390 {
391 return (target->set_alias(alias));
392 }
393
394 bool
target_set_auth_group(const char * name)395 target_set_auth_group(const char *name)
396 {
397 return (target->set_auth_group(name));
398 }
399
400 bool
target_set_auth_type(const char * type)401 target_set_auth_type(const char *type)
402 {
403 return (target->set_auth_type(type));
404 }
405
406 bool
target_set_physical_port(const char * pport)407 target_set_physical_port(const char *pport)
408 {
409 return (target->set_physical_port(pport));
410 }
411
412 bool
target_set_redirection(const char * addr)413 target_set_redirection(const char *addr)
414 {
415 return (target->set_redirection(addr));
416 }
417
418 bool
target_start_lun(u_int id)419 target_start_lun(u_int id)
420 {
421 lun = target->start_lun(id);
422 return (lun != nullptr);
423 }
424
425 bool
controller_start(const char * name)426 controller_start(const char *name)
427 {
428 target = conf->add_controller(name);
429 return (target != nullptr);
430 }
431
432 bool
controller_add_host_address(const char * addr)433 controller_add_host_address(const char *addr)
434 {
435 return (target->add_host_address(addr));
436 }
437
438 bool
controller_add_host_nqn(const char * name)439 controller_add_host_nqn(const char *name)
440 {
441 return (target->add_host_nqn(name));
442 }
443
444 bool
controller_add_namespace(u_int id,const char * name)445 controller_add_namespace(u_int id, const char *name)
446 {
447 return (target->add_namespace(id, name));
448 }
449
450 bool
controller_start_namespace(u_int id)451 controller_start_namespace(u_int id)
452 {
453 lun = target->start_namespace(id);
454 return (lun != nullptr);
455 }
456
457 bool
parse_conf(const char * path)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