3 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/queue.h>
34 #include <sys/types.h>
48 static struct conf *conf = NULL;
49 static struct auth_group *auth_group = NULL;
50 static struct portal_group *portal_group = NULL;
51 static struct target *target = NULL;
52 static struct lun *lun = NULL;
54 extern void yyerror(const char *);
55 extern int yylex(void);
56 extern void yyrestart(FILE *);
60 %token ALIAS AUTH_GROUP AUTH_TYPE BACKEND BLOCKSIZE CHAP CHAP_MUTUAL
61 %token CLOSING_BRACKET DEBUG DEVICE_ID DISCOVERY_AUTH_GROUP DISCOVERY_FILTER
62 %token INITIATOR_NAME INITIATOR_PORTAL ISNS_SERVER ISNS_PERIOD ISNS_TIMEOUT
63 %token LISTEN LISTEN_ISER LUN MAXPROC OPENING_BRACKET OPTION
64 %token PATH PIDFILE PORT PORTAL_GROUP REDIRECT SEMICOLON SERIAL SIZE STR
80 statements statement SEMICOLON
111 if (expand_number($2, &tmp) != 0) {
112 yyerror("invalid numeric value");
117 conf->conf_debug = tmp;
125 if (expand_number($2, &tmp) != 0) {
126 yyerror("invalid numeric value");
131 conf->conf_timeout = tmp;
139 if (expand_number($2, &tmp) != 0) {
140 yyerror("invalid numeric value");
145 conf->conf_maxproc = tmp;
151 if (conf->conf_pidfile_path != NULL) {
152 log_warnx("pidfile specified more than once");
156 conf->conf_pidfile_path = $2;
160 isns_server: ISNS_SERVER STR
164 error = isns_new(conf, $2);
171 isns_period: ISNS_PERIOD STR
175 if (expand_number($2, &tmp) != 0) {
176 yyerror("invalid numeric value");
181 conf->conf_isns_period = tmp;
185 isns_timeout: ISNS_TIMEOUT STR
189 if (expand_number($2, &tmp) != 0) {
190 yyerror("invalid numeric value");
195 conf->conf_isns_timeout = tmp;
199 auth_group: AUTH_GROUP auth_group_name
200 OPENING_BRACKET auth_group_entries CLOSING_BRACKET
209 * Make it possible to redefine default
210 * auth-group. but only once.
212 if (strcmp($1, "default") == 0 &&
213 conf->conf_default_ag_defined == false) {
214 auth_group = auth_group_find(conf, $1);
215 conf->conf_default_ag_defined = true;
217 auth_group = auth_group_new(conf, $1);
220 if (auth_group == NULL)
227 auth_group_entries auth_group_entry
229 auth_group_entries auth_group_entry SEMICOLON
237 auth_group_chap_mutual
239 auth_group_initiator_name
241 auth_group_initiator_portal
244 auth_group_auth_type: AUTH_TYPE STR
248 error = auth_group_set_type(auth_group, $2);
255 auth_group_chap: CHAP STR STR
257 const struct auth *ca;
259 ca = auth_new_chap(auth_group, $2, $3);
267 auth_group_chap_mutual: CHAP_MUTUAL STR STR STR STR
269 const struct auth *ca;
271 ca = auth_new_chap_mutual(auth_group, $2, $3, $4, $5);
281 auth_group_initiator_name: INITIATOR_NAME STR
283 const struct auth_name *an;
285 an = auth_name_new(auth_group, $2);
292 auth_group_initiator_portal: INITIATOR_PORTAL STR
294 const struct auth_portal *ap;
296 ap = auth_portal_new(auth_group, $2);
303 portal_group: PORTAL_GROUP portal_group_name
304 OPENING_BRACKET portal_group_entries CLOSING_BRACKET
310 portal_group_name: STR
313 * Make it possible to redefine default
314 * portal-group. but only once.
316 if (strcmp($1, "default") == 0 &&
317 conf->conf_default_pg_defined == false) {
318 portal_group = portal_group_find(conf, $1);
319 conf->conf_default_pg_defined = true;
321 portal_group = portal_group_new(conf, $1);
324 if (portal_group == NULL)
329 portal_group_entries:
331 portal_group_entries portal_group_entry
333 portal_group_entries portal_group_entry SEMICOLON
337 portal_group_discovery_auth_group
339 portal_group_discovery_filter
343 portal_group_listen_iser
345 portal_group_redirect
348 portal_group_discovery_auth_group: DISCOVERY_AUTH_GROUP STR
350 if (portal_group->pg_discovery_auth_group != NULL) {
351 log_warnx("discovery-auth-group for portal-group "
352 "\"%s\" specified more than once",
353 portal_group->pg_name);
356 portal_group->pg_discovery_auth_group =
357 auth_group_find(conf, $2);
358 if (portal_group->pg_discovery_auth_group == NULL) {
359 log_warnx("unknown discovery-auth-group \"%s\" "
360 "for portal-group \"%s\"",
361 $2, portal_group->pg_name);
368 portal_group_discovery_filter: DISCOVERY_FILTER STR
372 error = portal_group_set_filter(portal_group, $2);
379 portal_group_listen: LISTEN STR
383 error = portal_group_add_listen(portal_group, $2, false);
390 portal_group_listen_iser: LISTEN_ISER STR
394 error = portal_group_add_listen(portal_group, $2, true);
401 portal_group_redirect: REDIRECT STR
405 error = portal_group_set_redirection(portal_group, $2);
413 OPENING_BRACKET lun_entries CLOSING_BRACKET
421 lun = lun_new(conf, $1);
428 target: TARGET target_name
429 OPENING_BRACKET target_entries CLOSING_BRACKET
437 target = target_new(conf, $1);
446 target_entries target_entry
448 target_entries target_entry SEMICOLON
462 target_initiator_name
464 target_initiator_portal
477 target_alias: ALIAS STR
479 if (target->t_alias != NULL) {
480 log_warnx("alias for target \"%s\" "
481 "specified more than once", target->t_name);
484 target->t_alias = $2;
488 target_auth_group: AUTH_GROUP STR
490 if (target->t_auth_group != NULL) {
491 if (target->t_auth_group->ag_name != NULL)
492 log_warnx("auth-group for target \"%s\" "
493 "specified more than once", target->t_name);
495 log_warnx("cannot use both auth-group and explicit "
496 "authorisations for target \"%s\"",
500 target->t_auth_group = auth_group_find(conf, $2);
501 if (target->t_auth_group == NULL) {
502 log_warnx("unknown auth-group \"%s\" for target "
503 "\"%s\"", $2, target->t_name);
510 target_auth_type: AUTH_TYPE STR
514 if (target->t_auth_group != NULL) {
515 if (target->t_auth_group->ag_name != NULL) {
516 log_warnx("cannot use both auth-group and "
517 "auth-type for target \"%s\"",
522 target->t_auth_group = auth_group_new(conf, NULL);
523 if (target->t_auth_group == NULL) {
527 target->t_auth_group->ag_target = target;
529 error = auth_group_set_type(target->t_auth_group, $2);
536 target_chap: CHAP STR STR
538 const struct auth *ca;
540 if (target->t_auth_group != NULL) {
541 if (target->t_auth_group->ag_name != NULL) {
542 log_warnx("cannot use both auth-group and "
543 "chap for target \"%s\"",
550 target->t_auth_group = auth_group_new(conf, NULL);
551 if (target->t_auth_group == NULL) {
556 target->t_auth_group->ag_target = target;
558 ca = auth_new_chap(target->t_auth_group, $2, $3);
566 target_chap_mutual: CHAP_MUTUAL STR STR STR STR
568 const struct auth *ca;
570 if (target->t_auth_group != NULL) {
571 if (target->t_auth_group->ag_name != NULL) {
572 log_warnx("cannot use both auth-group and "
573 "chap-mutual for target \"%s\"",
582 target->t_auth_group = auth_group_new(conf, NULL);
583 if (target->t_auth_group == NULL) {
590 target->t_auth_group->ag_target = target;
592 ca = auth_new_chap_mutual(target->t_auth_group,
603 target_initiator_name: INITIATOR_NAME STR
605 const struct auth_name *an;
607 if (target->t_auth_group != NULL) {
608 if (target->t_auth_group->ag_name != NULL) {
609 log_warnx("cannot use both auth-group and "
610 "initiator-name for target \"%s\"",
616 target->t_auth_group = auth_group_new(conf, NULL);
617 if (target->t_auth_group == NULL) {
621 target->t_auth_group->ag_target = target;
623 an = auth_name_new(target->t_auth_group, $2);
630 target_initiator_portal: INITIATOR_PORTAL STR
632 const struct auth_portal *ap;
634 if (target->t_auth_group != NULL) {
635 if (target->t_auth_group->ag_name != NULL) {
636 log_warnx("cannot use both auth-group and "
637 "initiator-portal for target \"%s\"",
643 target->t_auth_group = auth_group_new(conf, NULL);
644 if (target->t_auth_group == NULL) {
648 target->t_auth_group->ag_target = target;
650 ap = auth_portal_new(target->t_auth_group, $2);
657 target_portal_group: PORTAL_GROUP STR STR
659 struct portal_group *tpg;
660 struct auth_group *tag;
663 tpg = portal_group_find(conf, $2);
665 log_warnx("unknown portal-group \"%s\" for target "
666 "\"%s\"", $2, target->t_name);
671 tag = auth_group_find(conf, $3);
673 log_warnx("unknown auth-group \"%s\" for target "
674 "\"%s\"", $3, target->t_name);
679 tp = port_new(conf, target, tpg);
681 log_warnx("can't link portal-group \"%s\" to target "
682 "\"%s\"", $2, target->t_name);
686 tp->p_auth_group = tag;
692 struct portal_group *tpg;
695 tpg = portal_group_find(conf, $2);
697 log_warnx("unknown portal-group \"%s\" for target "
698 "\"%s\"", $2, target->t_name);
702 tp = port_new(conf, target, tpg);
704 log_warnx("can't link portal-group \"%s\" to target "
705 "\"%s\"", $2, target->t_name);
713 target_port: PORT STR
718 pp = pport_find(conf, $2);
720 log_warnx("unknown port \"%s\" for target \"%s\"",
725 if (!TAILQ_EMPTY(&pp->pp_ports)) {
726 log_warnx("can't link port \"%s\" to target \"%s\", "
727 "port already linked to some target",
732 tp = port_new_pp(conf, target, pp);
734 log_warnx("can't link port \"%s\" to target \"%s\"",
743 target_redirect: REDIRECT STR
747 error = target_set_redirection(target, $2);
754 target_lun: LUN lun_number
755 OPENING_BRACKET lun_entries CLOSING_BRACKET
767 if (expand_number($1, &tmp) != 0) {
768 yyerror("invalid numeric value");
773 ret = asprintf(&name, "%s,lun,%ju", target->t_name, tmp);
775 log_err(1, "asprintf");
776 lun = lun_new(conf, name);
780 lun_set_scsiname(lun, name);
781 target->t_luns[tmp] = lun;
785 target_lun_ref: LUN STR STR
789 if (expand_number($2, &tmp) != 0) {
790 yyerror("invalid numeric value");
797 lun = lun_find(conf, $3);
802 target->t_luns[tmp] = lun;
808 lun_entries lun_entry
810 lun_entries lun_entry SEMICOLON
829 lun_backend: BACKEND STR
831 if (lun->l_backend != NULL) {
832 log_warnx("backend for lun \"%s\" "
833 "specified more than once",
838 lun_set_backend(lun, $2);
843 lun_blocksize: BLOCKSIZE STR
847 if (expand_number($2, &tmp) != 0) {
848 yyerror("invalid numeric value");
853 if (lun->l_blocksize != 0) {
854 log_warnx("blocksize for lun \"%s\" "
855 "specified more than once",
859 lun_set_blocksize(lun, tmp);
863 lun_device_id: DEVICE_ID STR
865 if (lun->l_device_id != NULL) {
866 log_warnx("device_id for lun \"%s\" "
867 "specified more than once",
872 lun_set_device_id(lun, $2);
877 lun_option: OPTION STR STR
879 struct lun_option *clo;
881 clo = lun_option_new(lun, $2, $3);
891 if (lun->l_path != NULL) {
892 log_warnx("path for lun \"%s\" "
893 "specified more than once",
898 lun_set_path(lun, $2);
903 lun_serial: SERIAL STR
905 if (lun->l_serial != NULL) {
906 log_warnx("serial for lun \"%s\" "
907 "specified more than once",
912 lun_set_serial(lun, $2);
921 if (expand_number($2, &tmp) != 0) {
922 yyerror("invalid numeric value");
927 if (lun->l_size != 0) {
928 log_warnx("size for lun \"%s\" "
929 "specified more than once",
933 lun_set_size(lun, tmp);
939 yyerror(const char *str)
942 log_warnx("error in configuration file at line %d near '%s': %s",
943 lineno, yytext, str);
947 check_perms(const char *path)
952 error = stat(path, &sb);
957 if (sb.st_mode & S_IWOTH) {
958 log_warnx("%s is world-writable", path);
959 } else if (sb.st_mode & S_IROTH) {
960 log_warnx("%s is world-readable", path);
961 } else if (sb.st_mode & S_IXOTH) {
963 * Ok, this one doesn't matter, but still do it,
964 * just for consistency.
966 log_warnx("%s is world-executable", path);
970 * XXX: Should we also check for owner != 0?
975 conf_new_from_file(const char *path, struct conf *oldconf)
977 struct auth_group *ag;
978 struct portal_group *pg;
982 log_debugx("obtaining configuration from %s", path);
986 TAILQ_FOREACH(pp, &oldconf->conf_pports, pp_next)
987 pport_copy(pp, conf);
989 ag = auth_group_new(conf, "default");
992 ag = auth_group_new(conf, "no-authentication");
994 ag->ag_type = AG_TYPE_NO_AUTHENTICATION;
996 ag = auth_group_new(conf, "no-access");
998 ag->ag_type = AG_TYPE_DENY;
1000 pg = portal_group_new(conf, "default");
1003 yyin = fopen(path, "r");
1005 log_warn("unable to open configuration file %s", path);
1014 portal_group = NULL;
1023 if (conf->conf_default_ag_defined == false) {
1024 log_debugx("auth-group \"default\" not defined; "
1025 "going with defaults");
1026 ag = auth_group_find(conf, "default");
1028 ag->ag_type = AG_TYPE_DENY;
1031 if (conf->conf_default_pg_defined == false) {
1032 log_debugx("portal-group \"default\" not defined; "
1033 "going with defaults");
1034 pg = portal_group_find(conf, "default");
1036 portal_group_add_listen(pg, "0.0.0.0:3260", false);
1037 portal_group_add_listen(pg, "[::]:3260", false);
1040 conf->conf_kernel_port_on = true;
1042 error = conf_verify(conf);