dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libshare / nfs / libshare_nfs.c
blobad5f3ae693402b97275257bb09131625f96fe0db
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2016 Nexenta Systems, Inc.
28 * NFS specific functions
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <zone.h>
36 #include <errno.h>
37 #include <locale.h>
38 #include <signal.h>
39 #include <strings.h>
40 #include <priv.h>
41 #include "libshare.h"
42 #include "libshare_impl.h"
43 #include <nfs/export.h>
44 #include <pwd.h>
45 #include <grp.h>
46 #include <limits.h>
47 #include <libscf.h>
48 #include <syslog.h>
49 #include <rpcsvc/daemon_utils.h>
50 #include "nfslog_config.h"
51 #include "nfslogtab.h"
52 #include "libshare_nfs.h"
53 #include <nfs/nfs.h>
54 #include <nfs/nfssys.h>
55 #include <netconfig.h>
56 #include "smfcfg.h"
58 /* should really be in some global place */
59 #define DEF_WIN 30000
60 #define OPT_CHUNK 1024
62 int debug = 0;
64 #define NFS_SERVER_SVC "svc:/network/nfs/server:default"
65 #define NFS_CLIENT_SVC (char *)"svc:/network/nfs/client:default"
67 /* internal functions */
68 static int nfs_init();
69 static void nfs_fini();
70 static int nfs_enable_share(sa_share_t);
71 static int nfs_disable_share(sa_share_t, char *);
72 static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
73 static int nfs_validate_security_mode(char *);
74 static int nfs_is_security_opt(char *);
75 static int nfs_parse_legacy_options(sa_group_t, char *);
76 static char *nfs_format_options(sa_group_t, int);
77 static int nfs_set_proto_prop(sa_property_t);
78 static sa_protocol_properties_t nfs_get_proto_set();
79 static char *nfs_get_status();
80 static char *nfs_space_alias(char *);
81 static uint64_t nfs_features();
84 * ops vector that provides the protocol specific info and operations
85 * for share management.
88 struct sa_plugin_ops sa_plugin_ops = {
89 SA_PLUGIN_VERSION,
90 "nfs",
91 nfs_init,
92 nfs_fini,
93 nfs_enable_share,
94 nfs_disable_share,
95 nfs_validate_property,
96 nfs_validate_security_mode,
97 nfs_is_security_opt,
98 nfs_parse_legacy_options,
99 nfs_format_options,
100 nfs_set_proto_prop,
101 nfs_get_proto_set,
102 nfs_get_status,
103 nfs_space_alias,
104 NULL, /* update_legacy */
105 NULL, /* delete_legacy */
106 NULL, /* change_notify */
107 NULL, /* enable_resource */
108 NULL, /* disable_resource */
109 nfs_features,
110 NULL, /* transient shares */
111 NULL, /* notify resource */
112 NULL, /* rename_resource */
113 NULL, /* run_command */
114 NULL, /* command_help */
115 NULL /* delete_proto_section */
119 * list of support services needed
120 * defines should come from head/rpcsvc/daemon_utils.h
123 static char *service_list_default[] =
124 { STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
125 static char *service_list_logging[] =
126 { STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
127 NULL };
130 * option definitions. Make sure to keep the #define for the option
131 * index just before the entry it is the index for. Changing the order
132 * can cause breakage. E.g OPT_RW is index 1 and must precede the
133 * line that includes the SHOPT_RW and OPT_RW entries.
136 struct option_defs optdefs[] = {
137 #define OPT_RO 0
138 {SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
139 #define OPT_RW 1
140 {SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
141 #define OPT_ROOT 2
142 {SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
143 #define OPT_SECURE 3
144 {SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
145 #define OPT_ANON 4
146 {SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
147 #define OPT_WINDOW 5
148 {SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
149 #define OPT_NOSUID 6
150 {SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
151 #define OPT_ACLOK 7
152 {SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
153 #define OPT_NOSUB 8
154 {SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
155 #define OPT_SEC 9
156 {SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
157 #define OPT_PUBLIC 10
158 {SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
159 #define OPT_INDEX 11
160 {SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
161 #define OPT_LOG 12
162 {SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
163 #define OPT_CKSUM 13
164 {SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
165 #define OPT_NONE 14
166 {SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
167 #define OPT_ROOT_MAPPING 15
168 {SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
169 #define OPT_CHARSET_MAP 16
170 {"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
171 #define OPT_NOACLFAB 17
172 {SHOPT_NOACLFAB, OPT_NOACLFAB, OPT_TYPE_BOOLEAN},
173 #define OPT_UIDMAP 18
174 {SHOPT_UIDMAP, OPT_UIDMAP, OPT_TYPE_MAPPING},
175 #define OPT_GIDMAP 19
176 {SHOPT_GIDMAP, OPT_GIDMAP, OPT_TYPE_MAPPING},
177 #ifdef VOLATILE_FH_TEST /* XXX added for testing volatile fh's only */
178 #define OPT_VOLFH 20
179 {SHOPT_VOLFH, OPT_VOLFH},
180 #endif /* VOLATILE_FH_TEST */
181 NULL
185 * Codesets that may need to be converted to UTF-8 for file paths.
186 * Add new names here to add new property support. If we ever get a
187 * way to query the kernel for character sets, this should become
188 * dynamically loaded. Make sure changes here are reflected in
189 * cmd/fs.d/nfs/mountd/nfscmd.c
192 static char *legal_conv[] = {
193 "euc-cn",
194 "euc-jp",
195 "euc-jpms",
196 "euc-kr",
197 "euc-tw",
198 "iso8859-1",
199 "iso8859-2",
200 "iso8859-5",
201 "iso8859-6",
202 "iso8859-7",
203 "iso8859-8",
204 "iso8859-9",
205 "iso8859-13",
206 "iso8859-15",
207 "koi8-r",
208 NULL
212 * list of properties that are related to security flavors.
214 static char *seclist[] = {
215 SHOPT_RO,
216 SHOPT_RW,
217 SHOPT_ROOT,
218 SHOPT_WINDOW,
219 SHOPT_NONE,
220 SHOPT_ROOT_MAPPING,
221 SHOPT_UIDMAP,
222 SHOPT_GIDMAP,
223 NULL
226 /* structure for list of securities */
227 struct securities {
228 sa_security_t security;
229 struct securities *next;
233 * findcharset(charset)
235 * Returns B_TRUE if the charset is a legal conversion otherwise
236 * B_FALSE. This will need to be rewritten to be more efficient when
237 * we have a dynamic list of legal conversions.
240 static boolean_t
241 findcharset(char *charset)
243 int i;
245 for (i = 0; legal_conv[i] != NULL; i++)
246 if (strcmp(charset, legal_conv[i]) == 0)
247 return (B_TRUE);
248 return (B_FALSE);
252 * findopt(name)
254 * Lookup option "name" in the option table and return the table
255 * index.
258 static int
259 findopt(char *name)
261 int i;
262 if (name != NULL) {
263 for (i = 0; optdefs[i].tag != NULL; i++) {
264 if (strcmp(optdefs[i].tag, name) == 0)
265 return (optdefs[i].index);
267 if (findcharset(name))
268 return (OPT_CHARSET_MAP);
270 return (-1);
274 * gettype(name)
276 * Return the type of option "name".
279 static int
280 gettype(char *name)
282 int optdef;
284 optdef = findopt(name);
285 if (optdef != -1)
286 return (optdefs[optdef].type);
287 return (OPT_TYPE_ANY);
291 * nfs_validate_security_mode(mode)
293 * is the specified mode string a valid one for use with NFS?
296 static int
297 nfs_validate_security_mode(char *mode)
299 seconfig_t secinfo;
300 int err;
302 (void) memset(&secinfo, '\0', sizeof (secinfo));
303 err = nfs_getseconfig_byname(mode, &secinfo);
304 if (err == SC_NOERROR)
305 return (1);
306 return (0);
310 * nfs_is_security_opt(tok)
312 * check to see if tok represents an option that is only valid in some
313 * security flavor.
316 static int
317 nfs_is_security_opt(char *tok)
319 int i;
321 for (i = 0; seclist[i] != NULL; i++) {
322 if (strcmp(tok, seclist[i]) == 0)
323 return (1);
325 return (0);
329 * find_security(seclist, sec)
331 * Walk the current list of security flavors and return true if it is
332 * present, else return false.
335 static int
336 find_security(struct securities *seclist, sa_security_t sec)
338 while (seclist != NULL) {
339 if (seclist->security == sec)
340 return (1);
341 seclist = seclist->next;
343 return (0);
347 * make_security_list(group, securitymodes, proto)
348 * go through the list of securitymodes and add them to the
349 * group's list of security optionsets. We also keep a list of
350 * those optionsets so we don't have to find them later. All of
351 * these will get copies of the same properties.
354 static struct securities *
355 make_security_list(sa_group_t group, char *securitymodes, char *proto)
357 char *tok, *next = NULL;
358 struct securities *curp, *headp = NULL, *prev;
359 sa_security_t check;
360 int freetok = 0;
362 for (tok = securitymodes; tok != NULL; tok = next) {
363 next = strchr(tok, ':');
364 if (next != NULL)
365 *next++ = '\0';
366 if (strcmp(tok, "default") == 0) {
367 /* resolve default into the real type */
368 tok = nfs_space_alias(tok);
369 freetok = 1;
371 check = sa_get_security(group, tok, proto);
373 /* add to the security list if it isn't there already */
374 if (check == NULL || !find_security(headp, check)) {
375 curp = (struct securities *)calloc(1,
376 sizeof (struct securities));
377 if (curp != NULL) {
378 if (check == NULL) {
379 curp->security = sa_create_security(
380 group, tok, proto);
381 } else {
382 curp->security = check;
385 * note that the first time through the loop,
386 * headp will be NULL and prev will be
387 * undefined. Since headp is NULL, we set
388 * both it and prev to the curp (first
389 * structure to be allocated).
391 * later passes through the loop will have
392 * headp not being NULL and prev will be used
393 * to allocate at the end of the list.
395 if (headp == NULL) {
396 headp = curp;
397 prev = curp;
398 } else {
399 prev->next = curp;
400 prev = curp;
405 if (freetok) {
406 freetok = 0;
407 sa_free_attr_string(tok);
410 return (headp);
413 static void
414 free_security_list(struct securities *sec)
416 struct securities *next;
417 if (sec != NULL) {
418 for (next = sec->next; sec != NULL; sec = next) {
419 next = sec->next;
420 free(sec);
426 * nfs_alistcat(str1, str2, sep)
428 * concatenate str1 and str2 into a new string using sep as a separate
429 * character. If memory allocation fails, return NULL;
432 static char *
433 nfs_alistcat(char *str1, char *str2, char sep)
435 char *newstr;
436 size_t len;
438 len = strlen(str1) + strlen(str2) + 2;
439 newstr = (char *)malloc(len);
440 if (newstr != NULL)
441 (void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
442 return (newstr);
446 * add_security_prop(sec, name, value, persist, iszfs)
448 * Add the property to the securities structure. This accumulates
449 * properties for as part of parsing legacy options.
452 static int
453 add_security_prop(struct securities *sec, char *name, char *value,
454 int persist, int iszfs)
456 sa_property_t prop;
457 int ret = SA_OK;
459 for (; sec != NULL; sec = sec->next) {
460 if (value == NULL) {
461 if (strcmp(name, SHOPT_RW) == 0 ||
462 strcmp(name, SHOPT_RO) == 0)
463 value = "*";
464 else
465 value = "true";
469 * Get the existing property, if it exists, so we can
470 * determine what to do with it. The ro/rw/root
471 * properties can be merged if multiple instances of
472 * these properies are given. For example, if "rw"
473 * exists with a value "host1" and a later token of
474 * rw="host2" is seen, the values are merged into a
475 * single rw="host1:host2".
477 prop = sa_get_property(sec->security, name);
479 if (prop != NULL) {
480 char *oldvalue;
481 char *newvalue;
484 * The security options of ro/rw/root/uidmap/gidmap
485 * might appear multiple times. If they do, the values
486 * need to be merged. If it was previously empty, the
487 * new value alone is added.
489 oldvalue = sa_get_property_attr(prop, "value");
490 if (oldvalue != NULL) {
491 char sep = ':';
493 if (strcmp(name, SHOPT_UIDMAP) == 0 ||
494 strcmp(name, SHOPT_GIDMAP) == 0)
495 sep = '~';
498 * The general case is to concatenate the new
499 * value onto the old value for multiple
500 * rw(ro/root/uidmap/gidmap) properties. For
501 * rw/ro/root a special case exists when either
502 * the old or new is the "all" case. In the
503 * special case, if both are "all", then it is
504 * "all", else if one is an access-list, that
505 * replaces the "all".
507 if (strcmp(oldvalue, "*") == 0) {
508 /* Replace old value with new value. */
509 newvalue = strdup(value);
510 } else if (strcmp(value, "*") == 0 ||
511 strcmp(oldvalue, value) == 0) {
513 * Keep old value and ignore
514 * the new value.
516 newvalue = NULL;
517 } else {
519 * Make a new list of old plus new
520 * access-list.
522 newvalue = nfs_alistcat(oldvalue,
523 value, sep);
526 if (newvalue != NULL) {
527 (void) sa_remove_property(prop);
528 prop = sa_create_property(name,
529 newvalue);
530 ret = sa_add_property(sec->security,
531 prop);
532 free(newvalue);
535 sa_free_attr_string(oldvalue);
537 } else {
538 prop = sa_create_property(name, value);
539 ret = sa_add_property(sec->security, prop);
541 if (ret == SA_OK && !iszfs) {
542 ret = sa_commit_properties(sec->security, !persist);
545 return (ret);
549 * check to see if group/share is persistent.
551 static int
552 is_persistent(sa_group_t group)
554 char *type;
555 int persist = 1;
557 type = sa_get_group_attr(group, "type");
558 if (type != NULL && strcmp(type, "persist") != 0)
559 persist = 0;
560 if (type != NULL)
561 sa_free_attr_string(type);
562 return (persist);
566 * invalid_security(options)
568 * search option string for any invalid sec= type.
569 * return true (1) if any are not valid else false (0)
571 static int
572 invalid_security(char *options)
574 char *copy, *base, *token, *value;
575 int ret = 0;
577 copy = strdup(options);
578 token = base = copy;
579 while (token != NULL && ret == 0) {
580 token = strtok(base, ",");
581 base = NULL;
582 if (token != NULL) {
583 value = strchr(token, '=');
584 if (value != NULL)
585 *value++ = '\0';
586 if (strcmp(token, SHOPT_SEC) == 0) {
587 /* HAVE security flavors so check them */
588 char *tok, *next;
589 for (next = NULL, tok = value; tok != NULL;
590 tok = next) {
591 next = strchr(tok, ':');
592 if (next != NULL)
593 *next++ = '\0';
594 ret = !nfs_validate_security_mode(tok);
595 if (ret)
596 break;
601 free(copy);
602 return (ret);
606 * nfs_parse_legacy_options(group, options)
608 * Parse the old style options into internal format and store on the
609 * specified group. Group could be a share for full legacy support.
612 static int
613 nfs_parse_legacy_options(sa_group_t group, char *options)
615 char *dup;
616 char *base;
617 char *token;
618 sa_optionset_t optionset;
619 struct securities *security_list = NULL;
620 sa_property_t prop;
621 int ret = SA_OK;
622 int iszfs = 0;
623 sa_group_t parent;
624 int persist = 0;
625 char *lasts;
627 /* do we have an existing optionset? */
628 optionset = sa_get_optionset(group, "nfs");
629 if (optionset == NULL) {
630 /* didn't find existing optionset so create one */
631 optionset = sa_create_optionset(group, "nfs");
632 } else {
634 * Have an existing optionset . Ideally, we would need
635 * to compare options in order to detect errors. For
636 * now, we assume that the first optionset is the
637 * correct one and the others will be the same. An
638 * empty optionset is the same as no optionset so we
639 * don't want to exit in that case. Getting an empty
640 * optionset can occur with ZFS property checking.
642 if (sa_get_property(optionset, NULL) != NULL)
643 return (ret);
646 if (strcmp(options, SHOPT_RW) == 0) {
648 * there is a special case of only the option "rw"
649 * being the default option. We don't have to do
650 * anything.
652 return (ret);
656 * check if security types are present and validate them. If
657 * any are not legal, fail.
660 if (invalid_security(options)) {
661 return (SA_INVALID_SECURITY);
665 * in order to not attempt to change ZFS properties unless
666 * absolutely necessary, we never do it in the legacy parsing.
668 if (sa_is_share(group)) {
669 char *zfs;
670 parent = sa_get_parent_group(group);
671 if (parent != NULL) {
672 zfs = sa_get_group_attr(parent, "zfs");
673 if (zfs != NULL) {
674 sa_free_attr_string(zfs);
675 iszfs++;
678 } else {
679 iszfs = sa_group_is_zfs(group);
682 /* We need a copy of options for the next part. */
683 dup = strdup(options);
684 if (dup == NULL)
685 return (SA_NO_MEMORY);
688 * we need to step through each option in the string and then
689 * add either the option or the security option as needed. If
690 * this is not a persistent share, don't commit to the
691 * repository. If there is an error, we also want to abort the
692 * processing and report it.
694 persist = is_persistent(group);
695 base = dup;
696 token = dup;
697 lasts = NULL;
698 while (token != NULL && ret == SA_OK) {
699 token = strtok_r(base, ",", &lasts);
700 base = NULL;
701 if (token != NULL) {
702 char *value;
704 * if the option has a value, it will have an '=' to
705 * separate the name from the value. The following
706 * code will result in value != NULL and token
707 * pointing to just the name if there is a value.
709 value = strchr(token, '=');
710 if (value != NULL) {
711 *value++ = '\0';
713 if (strcmp(token, SHOPT_SEC) == 0 ||
714 strcmp(token, SHOPT_SECURE) == 0) {
716 * Once in security parsing, we only
717 * do security. We do need to move
718 * between the security node and the
719 * toplevel. The security tag goes on
720 * the root while the following ones
721 * go on the security.
723 if (security_list != NULL) {
725 * have an old list so close it and
726 * start the new
728 free_security_list(security_list);
730 if (strcmp(token, SHOPT_SECURE) == 0) {
731 value = "dh";
732 } else {
733 if (value == NULL) {
734 ret = SA_SYNTAX_ERR;
735 break;
738 security_list = make_security_list(group,
739 value, "nfs");
740 } else {
742 * Note that the "old" syntax allowed a
743 * default security model. This must be
744 * accounted for and internally converted to
745 * "standard" security structure.
747 if (nfs_is_security_opt(token)) {
748 if (security_list == NULL) {
750 * need to have a
751 * security
752 * option. This will
753 * be "closed" when a
754 * defined "sec="
755 * option is
756 * seen. This is
757 * technically an
758 * error but will be
759 * allowed with
760 * warning.
762 security_list =
763 make_security_list(group,
764 "default",
765 "nfs");
767 if (security_list != NULL) {
768 ret = add_security_prop(
769 security_list, token,
770 value, persist, iszfs);
771 } else {
772 ret = SA_NO_MEMORY;
774 } else {
775 /* regular options */
776 if (value == NULL) {
777 if (strcmp(token, SHOPT_RW) ==
778 0 || strcmp(token,
779 SHOPT_RO) == 0) {
780 value = "*";
781 } else {
782 value = "global";
783 if (strcmp(token,
784 SHOPT_LOG) != 0) {
785 value = "true";
790 * In all cases, create the
791 * property specified. If the
792 * value was NULL, the default
793 * value will have been
794 * substituted.
796 prop = sa_create_property(token, value);
797 ret = sa_add_property(optionset, prop);
798 if (ret != SA_OK)
799 break;
801 if (!iszfs) {
802 ret = sa_commit_properties(
803 optionset, !persist);
809 if (security_list != NULL)
810 free_security_list(security_list);
812 free(dup);
813 return (ret);
817 * is_a_number(number)
819 * is the string a number in one of the forms we want to use?
822 static int
823 is_a_number(char *number)
825 int ret = 1;
826 int hex = 0;
828 if (strncmp(number, "0x", 2) == 0) {
829 number += 2;
830 hex = 1;
831 } else if (*number == '-') {
832 number++; /* skip the minus */
834 while (ret == 1 && *number != '\0') {
835 if (hex) {
836 ret = isxdigit(*number++);
837 } else {
838 ret = isdigit(*number++);
841 return (ret);
845 * Look for the specified tag in the configuration file. If it is found,
846 * enable logging and set the logging configuration information for exp.
848 static void
849 configlog(struct exportdata *exp, char *tag)
851 nfsl_config_t *configlist = NULL, *configp;
852 int error = 0;
853 char globaltag[] = DEFAULTTAG;
856 * Sends config errors to stderr
858 nfsl_errs_to_syslog = B_FALSE;
861 * get the list of configuration settings
863 error = nfsl_getconfig_list(&configlist);
864 if (error) {
865 (void) fprintf(stderr,
866 dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
867 strerror(error));
870 if (tag == NULL)
871 tag = globaltag;
872 if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
873 nfsl_freeconfig_list(&configlist);
874 (void) fprintf(stderr,
875 dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
876 /* bad configuration */
877 error = ENOENT;
878 goto err;
881 if ((exp->ex_tag = strdup(tag)) == NULL) {
882 error = ENOMEM;
883 goto out;
885 if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
886 error = ENOMEM;
887 goto out;
889 exp->ex_flags |= EX_LOG;
890 if (configp->nc_rpclogpath != NULL)
891 exp->ex_flags |= EX_LOG_ALLOPS;
892 out:
893 if (configlist != NULL)
894 nfsl_freeconfig_list(&configlist);
896 err:
897 if (error != 0) {
898 free(exp->ex_tag);
899 free(exp->ex_log_buffer);
900 (void) fprintf(stderr,
901 dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
902 strerror(error));
907 * fill_export_from_optionset(export, optionset)
909 * In order to share, we need to set all the possible general options
910 * into the export structure. Share info will be filled in by the
911 * caller. Various property values get turned into structure specific
912 * values.
915 static int
916 fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
918 sa_property_t option;
919 int ret = SA_OK;
921 for (option = sa_get_property(optionset, NULL);
922 option != NULL; option = sa_get_next_property(option)) {
923 char *name;
924 char *value;
925 uint32_t val;
928 * since options may be set/reset multiple times, always do an
929 * explicit set or clear of the option. This allows defaults
930 * to be set and then the protocol specific to override.
933 name = sa_get_property_attr(option, "type");
934 value = sa_get_property_attr(option, "value");
935 switch (findopt(name)) {
936 case OPT_ANON:
937 if (value != NULL && is_a_number(value)) {
938 val = strtoul(value, NULL, 0);
939 } else {
940 struct passwd *pw;
941 pw = getpwnam(value != NULL ? value : "nobody");
942 if (pw != NULL) {
943 val = pw->pw_uid;
944 } else {
945 val = UID_NOBODY;
947 endpwent();
949 export->ex_anon = val;
950 break;
951 case OPT_NOSUID:
952 if (value != NULL && (strcasecmp(value, "true") == 0 ||
953 strcmp(value, "1") == 0))
954 export->ex_flags |= EX_NOSUID;
955 else
956 export->ex_flags &= ~EX_NOSUID;
957 break;
958 case OPT_ACLOK:
959 if (value != NULL && (strcasecmp(value, "true") == 0 ||
960 strcmp(value, "1") == 0))
961 export->ex_flags |= EX_ACLOK;
962 else
963 export->ex_flags &= ~EX_ACLOK;
964 break;
965 case OPT_NOSUB:
966 if (value != NULL && (strcasecmp(value, "true") == 0 ||
967 strcmp(value, "1") == 0))
968 export->ex_flags |= EX_NOSUB;
969 else
970 export->ex_flags &= ~EX_NOSUB;
971 break;
972 case OPT_PUBLIC:
973 if (value != NULL && (strcasecmp(value, "true") == 0 ||
974 strcmp(value, "1") == 0))
975 export->ex_flags |= EX_PUBLIC;
976 else
977 export->ex_flags &= ~EX_PUBLIC;
978 break;
979 case OPT_INDEX:
980 if (value != NULL && (strcmp(value, "..") == 0 ||
981 strchr(value, '/') != NULL)) {
982 /* this is an error */
983 (void) printf(dgettext(TEXT_DOMAIN,
984 "NFS: index=\"%s\" not valid;"
985 "must be a filename.\n"),
986 value);
987 break;
989 if (value != NULL && *value != '\0' &&
990 strcmp(value, ".") != 0) {
991 /* valid index file string */
992 if (export->ex_index != NULL) {
993 /* left over from "default" */
994 free(export->ex_index);
996 /* remember to free */
997 export->ex_index = strdup(value);
998 if (export->ex_index == NULL) {
999 (void) printf(dgettext(TEXT_DOMAIN,
1000 "NFS: out of memory setting "
1001 "index property\n"));
1002 break;
1004 export->ex_flags |= EX_INDEX;
1006 break;
1007 case OPT_LOG:
1008 if (value == NULL)
1009 value = strdup("global");
1010 if (value != NULL)
1011 configlog(export,
1012 strlen(value) ? value : "global");
1013 break;
1014 case OPT_CHARSET_MAP:
1016 * Set EX_CHARMAP when there is at least one
1017 * charmap conversion property. This will get
1018 * checked by the nfs server when it needs to.
1020 export->ex_flags |= EX_CHARMAP;
1021 break;
1022 case OPT_NOACLFAB:
1023 if (value != NULL && (strcasecmp(value, "true") == 0 ||
1024 strcmp(value, "1") == 0))
1025 export->ex_flags |= EX_NOACLFAB;
1026 else
1027 export->ex_flags &= ~EX_NOACLFAB;
1028 break;
1029 default:
1030 /* have a syntactic error */
1031 (void) printf(dgettext(TEXT_DOMAIN,
1032 "NFS: unrecognized option %s=%s\n"),
1033 name != NULL ? name : "",
1034 value != NULL ? value : "");
1035 break;
1037 if (name != NULL)
1038 sa_free_attr_string(name);
1039 if (value != NULL)
1040 sa_free_attr_string(value);
1042 return (ret);
1046 * cleanup_export(export)
1048 * Cleanup the allocated areas so we don't leak memory
1051 static void
1052 cleanup_export(struct exportdata *export)
1054 int i;
1056 free(export->ex_index);
1058 for (i = 0; i < export->ex_seccnt; i++) {
1059 struct secinfo *s = &export->ex_secinfo[i];
1061 while (s->s_rootcnt > 0)
1062 free(s->s_rootnames[--s->s_rootcnt]);
1064 free(s->s_rootnames);
1066 free(export->ex_secinfo);
1070 * Given a seconfig entry and a colon-separated
1071 * list of names, allocate an array big enough
1072 * to hold the root list, then convert each name to
1073 * a principal name according to the security
1074 * info and assign it to an array element.
1075 * Return the array and its size.
1077 static caddr_t *
1078 get_rootnames(seconfig_t *sec, char *list, int *count)
1080 caddr_t *a;
1081 int c, i;
1082 char *host, *p;
1085 * Count the number of strings in the list.
1086 * This is the number of colon separators + 1.
1088 c = 1;
1089 for (p = list; *p; p++)
1090 if (*p == ':')
1091 c++;
1092 *count = c;
1094 a = (caddr_t *)malloc(c * sizeof (char *));
1095 if (a == NULL) {
1096 (void) printf(dgettext(TEXT_DOMAIN,
1097 "get_rootnames: no memory\n"));
1098 *count = 0;
1099 } else {
1100 for (i = 0; i < c; i++) {
1101 host = strtok(list, ":");
1102 if (!nfs_get_root_principal(sec, host, &a[i])) {
1103 while (i > 0)
1104 free(a[--i]);
1105 free(a);
1106 a = NULL;
1107 *count = 0;
1108 break;
1110 list = NULL;
1114 return (a);
1118 * fill_security_from_secopts(sp, secopts)
1120 * Fill the secinfo structure from the secopts optionset.
1123 static int
1124 fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
1126 sa_property_t prop;
1127 char *type;
1128 int longform;
1129 int err = SC_NOERROR;
1130 uint32_t val;
1132 type = sa_get_security_attr(secopts, "sectype");
1133 if (type != NULL) {
1134 /* named security type needs secinfo to be filled in */
1135 err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1136 sa_free_attr_string(type);
1137 if (err != SC_NOERROR)
1138 return (err);
1139 } else {
1140 /* default case */
1141 err = nfs_getseconfig_default(&sp->s_secinfo);
1142 if (err != SC_NOERROR)
1143 return (err);
1146 err = SA_OK;
1147 for (prop = sa_get_property(secopts, NULL);
1148 prop != NULL && err == SA_OK;
1149 prop = sa_get_next_property(prop)) {
1150 char *name;
1151 char *value;
1153 name = sa_get_property_attr(prop, "type");
1154 value = sa_get_property_attr(prop, "value");
1156 longform = value != NULL && strcmp(value, "*") != 0;
1158 switch (findopt(name)) {
1159 case OPT_RO:
1160 sp->s_flags |= longform ? M_ROL : M_RO;
1161 break;
1162 case OPT_RW:
1163 sp->s_flags |= longform ? M_RWL : M_RW;
1164 break;
1165 case OPT_ROOT:
1166 sp->s_flags |= M_ROOT;
1168 * if we are using AUTH_UNIX, handle like other things
1169 * such as RO/RW
1171 if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1172 break;
1173 /* not AUTH_UNIX */
1174 if (value != NULL) {
1175 sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1176 value, &sp->s_rootcnt);
1177 if (sp->s_rootnames == NULL) {
1178 err = SA_BAD_VALUE;
1179 (void) fprintf(stderr,
1180 dgettext(TEXT_DOMAIN,
1181 "Bad root list\n"));
1184 break;
1185 case OPT_NONE:
1186 sp->s_flags |= M_NONE;
1187 break;
1188 case OPT_WINDOW:
1189 if (value != NULL) {
1190 sp->s_window = atoi(value);
1191 /* just in case */
1192 if (sp->s_window < 0)
1193 sp->s_window = DEF_WIN;
1195 break;
1196 case OPT_ROOT_MAPPING:
1197 if (value != NULL && is_a_number(value)) {
1198 val = strtoul(value, NULL, 0);
1199 } else {
1200 struct passwd *pw;
1201 pw = getpwnam(value != NULL ? value : "nobody");
1202 if (pw != NULL) {
1203 val = pw->pw_uid;
1204 } else {
1205 val = UID_NOBODY;
1207 endpwent();
1209 sp->s_rootid = val;
1210 break;
1211 case OPT_UIDMAP:
1212 case OPT_GIDMAP:
1213 sp->s_flags |= M_MAP;
1214 break;
1215 default:
1216 break;
1218 if (name != NULL)
1219 sa_free_attr_string(name);
1220 if (value != NULL)
1221 sa_free_attr_string(value);
1223 /* if rw/ro options not set, use default of RW */
1224 if ((sp->s_flags & NFS_RWMODES) == 0)
1225 sp->s_flags |= M_RW;
1226 return (err);
1230 * This is for testing only
1231 * It displays the export structure that
1232 * goes into the kernel.
1234 static void
1235 printarg(char *path, struct exportdata *ep)
1237 int i, j;
1238 struct secinfo *sp;
1240 if (debug == 0)
1241 return;
1243 (void) printf("%s:\n", path);
1244 (void) printf("\tex_version = %d\n", ep->ex_version);
1245 (void) printf("\tex_path = %s\n", ep->ex_path);
1246 (void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
1247 (void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
1248 if (ep->ex_flags & EX_NOSUID)
1249 (void) printf("NOSUID ");
1250 if (ep->ex_flags & EX_ACLOK)
1251 (void) printf("ACLOK ");
1252 if (ep->ex_flags & EX_PUBLIC)
1253 (void) printf("PUBLIC ");
1254 if (ep->ex_flags & EX_NOSUB)
1255 (void) printf("NOSUB ");
1256 if (ep->ex_flags & EX_LOG)
1257 (void) printf("LOG ");
1258 if (ep->ex_flags & EX_CHARMAP)
1259 (void) printf("CHARMAP ");
1260 if (ep->ex_flags & EX_LOG_ALLOPS)
1261 (void) printf("LOG_ALLOPS ");
1262 if (ep->ex_flags == 0)
1263 (void) printf("(none)");
1264 (void) printf("\n");
1265 if (ep->ex_flags & EX_LOG) {
1266 (void) printf("\tex_log_buffer = %s\n",
1267 (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
1268 (void) printf("\tex_tag = %s\n",
1269 (ep->ex_tag ? ep->ex_tag : "(NULL)"));
1271 (void) printf("\tex_anon = %d\n", ep->ex_anon);
1272 (void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
1273 (void) printf("\n");
1274 for (i = 0; i < ep->ex_seccnt; i++) {
1275 sp = &ep->ex_secinfo[i];
1276 (void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
1277 (void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
1278 if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
1279 if (sp->s_flags & M_RO) (void) printf("M_RO ");
1280 if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
1281 if (sp->s_flags & M_RW) (void) printf("M_RW ");
1282 if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1283 if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
1284 if (sp->s_flags & M_MAP) (void) printf("M_MAP ");
1285 if (sp->s_flags == 0) (void) printf("(none)");
1286 (void) printf("\n");
1287 (void) printf("\t\ts_window = %d\n", sp->s_window);
1288 (void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
1289 (void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
1290 (void) fflush(stdout);
1291 for (j = 0; j < sp->s_rootcnt; j++)
1292 (void) printf("%s ", sp->s_rootnames[j] ?
1293 sp->s_rootnames[j] : "<null>");
1294 (void) printf("\n\n");
1299 * count_security(opts)
1301 * Count the number of security types (flavors). The optionset has
1302 * been populated with the security flavors as a holding mechanism.
1303 * We later use this number to allocate data structures.
1306 static int
1307 count_security(sa_optionset_t opts)
1309 int count = 0;
1310 sa_property_t prop;
1311 if (opts != NULL) {
1312 for (prop = sa_get_property(opts, NULL); prop != NULL;
1313 prop = sa_get_next_property(prop)) {
1314 count++;
1317 return (count);
1321 * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1323 * provides a mechanism to format NFS properties into legacy output
1324 * format. If the buffer would overflow, it is reallocated and grown
1325 * as appropriate. Special cases of converting internal form of values
1326 * to those used by "share" are done. this function does one property
1327 * at a time.
1330 static int
1331 nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1332 sa_property_t prop, int sep)
1334 char *name;
1335 char *value;
1336 int curlen;
1337 char *buff = *rbuff;
1338 size_t buffsize = *rbuffsize;
1339 int printed = B_FALSE;
1341 name = sa_get_property_attr(prop, "type");
1342 value = sa_get_property_attr(prop, "value");
1343 if (buff != NULL)
1344 curlen = strlen(buff);
1345 else
1346 curlen = 0;
1347 if (name != NULL) {
1348 int len;
1349 len = strlen(name) + sep;
1352 * A future RFE would be to replace this with more
1353 * generic code and to possibly handle more types.
1355 switch (gettype(name)) {
1356 case OPT_TYPE_BOOLEAN:
1358 * For NFS, boolean value of FALSE means it
1359 * doesn't show up in the option list at all.
1361 if (value != NULL && strcasecmp(value, "false") == 0)
1362 goto skip;
1363 if (value != NULL) {
1364 sa_free_attr_string(value);
1365 value = NULL;
1367 break;
1368 case OPT_TYPE_ACCLIST:
1369 if (value != NULL && strcmp(value, "*") == 0) {
1370 sa_free_attr_string(value);
1371 value = NULL;
1372 } else {
1373 if (value != NULL)
1374 len += 1 + strlen(value);
1376 break;
1377 case OPT_TYPE_LOGTAG:
1378 if (value != NULL && strlen(value) == 0) {
1379 sa_free_attr_string(value);
1380 value = NULL;
1381 } else {
1382 if (value != NULL)
1383 len += 1 + strlen(value);
1385 break;
1386 default:
1387 if (value != NULL)
1388 len += 1 + strlen(value);
1389 break;
1391 while (buffsize <= (curlen + len)) {
1392 /* need more room */
1393 buffsize += incr;
1394 buff = realloc(buff, buffsize);
1395 if (buff == NULL) {
1396 /* realloc failed so free everything */
1397 free(*rbuff);
1399 *rbuff = buff;
1400 *rbuffsize = buffsize;
1401 if (buff == NULL)
1402 goto skip;
1406 if (buff == NULL)
1407 goto skip;
1409 if (value == NULL) {
1410 (void) snprintf(buff + curlen, buffsize - curlen,
1411 "%s%s", sep ? "," : "", name);
1412 } else {
1413 (void) snprintf(buff + curlen, buffsize - curlen,
1414 "%s%s=%s", sep ? "," : "",
1415 name, value != NULL ? value : "");
1417 printed = B_TRUE;
1419 skip:
1420 if (name != NULL)
1421 sa_free_attr_string(name);
1422 if (value != NULL)
1423 sa_free_attr_string(value);
1424 return (printed);
1428 * nfs_format_options(group, hier)
1430 * format all the options on the group into an old-style option
1431 * string. If hier is non-zero, walk up the tree to get inherited
1432 * options.
1435 static char *
1436 nfs_format_options(sa_group_t group, int hier)
1438 sa_optionset_t options = NULL;
1439 sa_optionset_t secoptions = NULL;
1440 sa_property_t prop, secprop;
1441 sa_security_t security = NULL;
1442 char *buff;
1443 size_t buffsize;
1444 char *sectype = NULL;
1445 int sep = 0;
1448 buff = malloc(OPT_CHUNK);
1449 if (buff == NULL) {
1450 return (NULL);
1453 buff[0] = '\0';
1454 buffsize = OPT_CHUNK;
1457 * We may have a an optionset relative to this item. format
1458 * these if we find them and then add any security definitions.
1461 options = sa_get_derived_optionset(group, "nfs", hier);
1464 * do the default set first but skip any option that is also
1465 * in the protocol specific optionset.
1467 if (options != NULL) {
1468 for (prop = sa_get_property(options, NULL);
1469 prop != NULL; prop = sa_get_next_property(prop)) {
1471 * use this one since we skipped any
1472 * of these that were also in
1473 * optdefault
1475 if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1476 prop, sep))
1477 sep = 1;
1478 if (buff == NULL) {
1480 * buff could become NULL if there
1481 * isn't enough memory for
1482 * nfs_sprint_option to realloc()
1483 * as necessary. We can't really
1484 * do anything about it at this
1485 * point so we return NULL. The
1486 * caller should handle the
1487 * failure.
1489 if (options != NULL)
1490 sa_free_derived_optionset(
1491 options);
1492 return (buff);
1496 secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1497 "nfs", hier);
1498 if (secoptions != NULL) {
1499 for (secprop = sa_get_property(secoptions, NULL);
1500 secprop != NULL;
1501 secprop = sa_get_next_property(secprop)) {
1502 sectype = sa_get_property_attr(secprop, "type");
1503 security =
1504 (sa_security_t)sa_get_derived_security(
1505 group, sectype, "nfs", hier);
1506 if (security != NULL) {
1507 if (sectype != NULL) {
1508 prop = sa_create_property(
1509 "sec", sectype);
1510 if (prop == NULL)
1511 goto err;
1512 if (nfs_sprint_option(&buff,
1513 &buffsize, OPT_CHUNK, prop, sep))
1514 sep = 1;
1515 (void) sa_remove_property(prop);
1516 if (buff == NULL)
1517 goto err;
1519 for (prop = sa_get_property(security,
1520 NULL); prop != NULL;
1521 prop = sa_get_next_property(prop)) {
1522 if (nfs_sprint_option(&buff,
1523 &buffsize, OPT_CHUNK, prop, sep))
1524 sep = 1;
1525 if (buff == NULL)
1526 goto err;
1528 sa_free_derived_optionset(security);
1530 if (sectype != NULL)
1531 sa_free_attr_string(sectype);
1533 sa_free_derived_optionset(secoptions);
1536 if (options != NULL)
1537 sa_free_derived_optionset(options);
1538 return (buff);
1540 err:
1542 * If we couldn't allocate memory for option printing, we need
1543 * to break out of the nested loops, cleanup and return NULL.
1545 if (secoptions != NULL)
1546 sa_free_derived_optionset(secoptions);
1547 if (security != NULL)
1548 sa_free_derived_optionset(security);
1549 if (sectype != NULL)
1550 sa_free_attr_string(sectype);
1551 if (options != NULL)
1552 sa_free_derived_optionset(options);
1553 return (buff);
1557 * Append an entry to the nfslogtab file
1559 static int
1560 nfslogtab_add(char *dir, char *buffer, char *tag)
1562 FILE *f;
1563 struct logtab_ent lep;
1564 int error = 0;
1567 * Open the file for update and create it if necessary.
1568 * This may leave the I/O offset at the end of the file,
1569 * so rewind back to the beginning of the file.
1571 f = fopen(NFSLOGTAB, "a+");
1572 if (f == NULL) {
1573 error = errno;
1574 goto out;
1576 rewind(f);
1578 if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1579 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1580 "share complete, however failed to lock %s "
1581 "for update: %s\n"), NFSLOGTAB, strerror(errno));
1582 error = -1;
1583 goto out;
1586 if (logtab_deactivate_after_boot(f) == -1) {
1587 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1588 "share complete, however could not deactivate "
1589 "entries in %s\n"), NFSLOGTAB);
1590 error = -1;
1591 goto out;
1595 * Remove entries matching buffer and sharepoint since we're
1596 * going to replace it with perhaps an entry with a new tag.
1598 if (logtab_rement(f, buffer, dir, NULL, -1)) {
1599 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1600 "share complete, however could not remove matching "
1601 "entries in %s\n"), NFSLOGTAB);
1602 error = -1;
1603 goto out;
1607 * Deactivate all active entries matching this sharepoint
1609 if (logtab_deactivate(f, NULL, dir, NULL)) {
1610 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1611 "share complete, however could not deactivate matching "
1612 "entries in %s\n"), NFSLOGTAB);
1613 error = -1;
1614 goto out;
1617 lep.le_buffer = buffer;
1618 lep.le_path = dir;
1619 lep.le_tag = tag;
1620 lep.le_state = LES_ACTIVE;
1623 * Add new sharepoint / buffer location to nfslogtab
1625 if (logtab_putent(f, &lep) < 0) {
1626 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1627 "share complete, however could not add %s to %s\n"),
1628 dir, NFSLOGTAB);
1629 error = -1;
1632 out:
1633 if (f != NULL)
1634 (void) fclose(f);
1635 return (error);
1639 * Deactivate an entry from the nfslogtab file
1641 static int
1642 nfslogtab_deactivate(char *path)
1644 FILE *f;
1645 int error = 0;
1647 f = fopen(NFSLOGTAB, "r+");
1648 if (f == NULL) {
1649 error = errno;
1650 goto out;
1652 if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1653 error = errno;
1654 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1655 "share complete, however could not lock %s for "
1656 "update: %s\n"), NFSLOGTAB, strerror(error));
1657 goto out;
1659 if (logtab_deactivate(f, NULL, path, NULL) == -1) {
1660 error = -1;
1661 (void) fprintf(stderr,
1662 dgettext(TEXT_DOMAIN,
1663 "share complete, however could not "
1664 "deactivate %s in %s\n"), path, NFSLOGTAB);
1665 goto out;
1668 out: if (f != NULL)
1669 (void) fclose(f);
1671 return (error);
1675 * check_public(group, skipshare)
1677 * Check the group for any shares that have the public property
1678 * enabled. We skip "skipshare" since that is the one we are
1679 * working with. This is a separate function to make handling
1680 * subgroups simpler. Returns true if there is a share with public.
1682 static int
1683 check_public(sa_group_t group, sa_share_t skipshare)
1685 int exists = B_FALSE;
1686 sa_share_t share;
1687 sa_optionset_t opt;
1688 sa_property_t prop;
1689 char *shared;
1691 for (share = sa_get_share(group, NULL); share != NULL;
1692 share = sa_get_next_share(share)) {
1693 if (share == skipshare)
1694 continue;
1696 opt = sa_get_optionset(share, "nfs");
1697 if (opt == NULL)
1698 continue;
1699 prop = sa_get_property(opt, "public");
1700 if (prop == NULL)
1701 continue;
1702 shared = sa_get_share_attr(share, "shared");
1703 if (shared != NULL) {
1704 exists = strcmp(shared, "true") == 0;
1705 sa_free_attr_string(shared);
1706 if (exists == B_TRUE)
1707 break;
1711 return (exists);
1715 * public_exists(handle, skipshare)
1717 * check to see if public option is set on any other share than the
1718 * one specified. Need to check zfs sub-groups as well as the top
1719 * level groups.
1721 static int
1722 public_exists(sa_handle_t handle, sa_share_t skipshare)
1724 sa_group_t group = NULL;
1727 * If we don't have a handle, we can only do syntax check. We
1728 * can't check against other shares so we assume OK and will
1729 * catch the problem only when we actually try to apply it.
1731 if (handle == NULL)
1732 return (SA_OK);
1734 if (skipshare != NULL) {
1735 group = sa_get_parent_group(skipshare);
1736 if (group == NULL)
1737 return (SA_NO_SUCH_GROUP);
1740 for (group = sa_get_group(handle, NULL); group != NULL;
1741 group = sa_get_next_group(group)) {
1742 /* Walk any ZFS subgroups as well as all standard groups */
1743 if (sa_group_is_zfs(group)) {
1744 sa_group_t subgroup;
1745 for (subgroup = sa_get_sub_group(group);
1746 subgroup != NULL;
1747 subgroup = sa_get_next_group(subgroup)) {
1748 if (check_public(subgroup, skipshare))
1749 return (B_TRUE);
1751 } else {
1752 if (check_public(group, skipshare))
1753 return (B_TRUE);
1756 return (B_FALSE);
1760 * sa_enable_share at the protocol level, enable_share must tell the
1761 * implementation that it is to enable the share. This entails
1762 * converting the path and options into the appropriate ioctl
1763 * calls. It is assumed that all error checking of paths, etc. were
1764 * done earlier.
1766 static int
1767 nfs_enable_share(sa_share_t share)
1769 struct exportdata export;
1770 sa_optionset_t secoptlist;
1771 struct secinfo *sp;
1772 int num_secinfo;
1773 sa_optionset_t opt;
1774 sa_security_t sec;
1775 sa_property_t prop;
1776 char *path;
1777 int err = SA_OK;
1778 int i;
1779 int iszfs;
1780 sa_handle_t handle;
1782 /* Don't drop core if the NFS module isn't loaded. */
1783 (void) signal(SIGSYS, SIG_IGN);
1785 /* get the path since it is important in several places */
1786 path = sa_get_share_attr(share, "path");
1787 if (path == NULL)
1788 return (SA_NO_SUCH_PATH);
1790 iszfs = sa_path_is_zfs(path);
1792 * find the optionsets and security sets. There may not be
1793 * any or there could be one or two for each of optionset and
1794 * security may have multiple, one per security type per
1795 * protocol type.
1797 opt = sa_get_derived_optionset(share, "nfs", 1);
1798 secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
1799 if (secoptlist != NULL)
1800 num_secinfo = MAX(1, count_security(secoptlist));
1801 else
1802 num_secinfo = 1;
1805 * walk through the options and fill in the structure
1806 * appropriately.
1809 (void) memset(&export, '\0', sizeof (export));
1812 * do non-security options first since there is only one after
1813 * the derived group is constructed.
1815 export.ex_version = EX_CURRENT_VERSION;
1816 export.ex_anon = UID_NOBODY; /* this is our default value */
1817 export.ex_index = NULL;
1818 export.ex_path = path;
1819 export.ex_pathlen = strlen(path) + 1;
1821 if (opt != NULL)
1822 err = fill_export_from_optionset(&export, opt);
1825 * check to see if "public" is set. If it is, then make sure
1826 * no other share has it set. If it is already used, fail.
1829 handle = sa_find_group_handle((sa_group_t)share);
1830 if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
1831 (void) printf(dgettext(TEXT_DOMAIN,
1832 "NFS: Cannot share more than one file "
1833 "system with 'public' property\n"));
1834 err = SA_NOT_ALLOWED;
1835 goto out;
1838 sp = calloc(num_secinfo, sizeof (struct secinfo));
1839 if (sp == NULL) {
1840 err = SA_NO_MEMORY;
1841 (void) printf(dgettext(TEXT_DOMAIN,
1842 "NFS: NFS: no memory for security\n"));
1843 goto out;
1845 export.ex_secinfo = sp;
1846 /* get default secinfo */
1847 export.ex_seccnt = num_secinfo;
1849 * since we must have one security option defined, we
1850 * init to the default and then override as we find
1851 * defined security options. This handles the case
1852 * where we have no defined options but we need to set
1853 * up one.
1855 sp[0].s_window = DEF_WIN;
1856 sp[0].s_rootnames = NULL;
1857 /* setup a default in case no properties defined */
1858 if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1859 (void) printf(dgettext(TEXT_DOMAIN,
1860 "NFS: nfs_getseconfig_default: failed to "
1861 "get default security mode\n"));
1862 err = SA_CONFIG_ERR;
1864 if (secoptlist != NULL) {
1865 for (i = 0, prop = sa_get_property(secoptlist, NULL);
1866 prop != NULL && i < num_secinfo;
1867 prop = sa_get_next_property(prop), i++) {
1868 char *sectype;
1869 sectype = sa_get_property_attr(prop, "type");
1871 * if sectype is NULL, we probably
1872 * have a memory problem and can't get
1873 * the correct values. Rather than
1874 * exporting with incorrect security,
1875 * don't share it.
1877 if (sectype == NULL) {
1878 err = SA_NO_MEMORY;
1879 (void) printf(dgettext(TEXT_DOMAIN,
1880 "NFS: Cannot share %s: "
1881 "no memory\n"), path);
1882 goto out;
1884 sec = (sa_security_t)sa_get_derived_security(
1885 share, sectype, "nfs", 1);
1886 sp[i].s_window = DEF_WIN;
1887 sp[i].s_rootcnt = 0;
1888 sp[i].s_rootnames = NULL;
1889 (void) fill_security_from_secopts(&sp[i], sec);
1890 if (sec != NULL)
1891 sa_free_derived_security(sec);
1892 if (sectype != NULL)
1893 sa_free_attr_string(sectype);
1897 * when we get here, we can do the exportfs system call and
1898 * initiate things. We probably want to enable the
1899 * svc:/network/nfs/server service first if it isn't running.
1901 /* check svc:/network/nfs/server status and start if needed */
1902 /* now add the share to the internal tables */
1903 printarg(path, &export);
1905 * call the exportfs system call which is implemented
1906 * via the nfssys() call as the EXPORTFS subfunction.
1908 if (iszfs) {
1909 struct exportfs_args ea;
1910 share_t sh;
1911 char *str;
1912 priv_set_t *priv_effective;
1913 int privileged;
1916 * If we aren't a privileged user
1917 * and NFS server service isn't running
1918 * then print out an error message
1919 * and return EPERM
1922 priv_effective = priv_allocset();
1923 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
1925 privileged = (priv_isfullset(priv_effective) == B_TRUE);
1926 priv_freeset(priv_effective);
1928 if (!privileged &&
1929 (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1930 err = 0;
1931 if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1932 (void) printf(dgettext(TEXT_DOMAIN,
1933 "NFS: Cannot share remote "
1934 "filesystem: %s\n"), path);
1935 (void) printf(dgettext(TEXT_DOMAIN,
1936 "NFS: Service needs to be enabled "
1937 "by a privileged user\n"));
1938 err = SA_SYSTEM_ERR;
1939 errno = EPERM;
1941 free(str);
1944 if (err == 0) {
1945 ea.dname = path;
1946 ea.uex = &export;
1948 (void) sa_sharetab_fill_zfs(share, &sh, "nfs");
1949 err = sa_share_zfs(share, NULL, path, &sh,
1950 &ea, ZFS_SHARE_NFS);
1951 if (err != SA_OK) {
1952 errno = err;
1953 err = -1;
1955 sa_emptyshare(&sh);
1957 } else {
1958 err = exportfs(path, &export);
1961 if (err < 0) {
1962 err = SA_SYSTEM_ERR;
1963 switch (errno) {
1964 case EREMOTE:
1965 (void) printf(dgettext(TEXT_DOMAIN,
1966 "NFS: Cannot share filesystems "
1967 "in non-global zones: %s\n"), path);
1968 err = SA_NOT_SUPPORTED;
1969 break;
1970 case EPERM:
1971 if (getzoneid() != GLOBAL_ZONEID) {
1972 (void) printf(dgettext(TEXT_DOMAIN,
1973 "NFS: Cannot share file systems "
1974 "in non-global zones: %s\n"), path);
1975 err = SA_NOT_SUPPORTED;
1976 break;
1978 err = SA_NO_PERMISSION;
1979 break;
1980 case EEXIST:
1981 err = SA_SHARE_EXISTS;
1982 break;
1983 default:
1984 break;
1986 } else {
1987 /* update sharetab with an add/modify */
1988 if (!iszfs) {
1989 (void) sa_update_sharetab(share, "nfs");
1993 if (err == SA_OK) {
1995 * enable services as needed. This should probably be
1996 * done elsewhere in order to minimize the calls to
1997 * check services.
2000 * check to see if logging and other services need to
2001 * be triggered, but only if there wasn't an
2002 * error. This is probably where sharetab should be
2003 * updated with the NFS specific entry.
2005 if (export.ex_flags & EX_LOG) {
2006 /* enable logging */
2007 if (nfslogtab_add(path, export.ex_log_buffer,
2008 export.ex_tag) != 0) {
2009 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2010 "Could not enable logging for %s\n"),
2011 path);
2013 _check_services(service_list_logging);
2014 } else {
2016 * don't have logging so remove it from file. It might
2017 * not be thre, but that doesn't matter.
2019 (void) nfslogtab_deactivate(path);
2020 _check_services(service_list_default);
2024 out:
2025 free(path);
2027 cleanup_export(&export);
2028 if (opt != NULL)
2029 sa_free_derived_optionset(opt);
2030 if (secoptlist != NULL)
2031 (void) sa_destroy_optionset(secoptlist);
2032 return (err);
2036 * nfs_disable_share(share, path)
2038 * Unshare the specified share. Note that "path" is the same path as
2039 * what is in the "share" object. It is passed in to avoid an
2040 * additional lookup. A missing "path" value makes this a no-op
2041 * function.
2043 static int
2044 nfs_disable_share(sa_share_t share, char *path)
2046 int err;
2047 int ret = SA_OK;
2048 int iszfs;
2049 sa_group_t parent;
2050 sa_handle_t handle;
2052 if (path == NULL)
2053 return (ret);
2056 * If the share is in a ZFS group we need to handle it
2057 * differently. Just being on a ZFS file system isn't
2058 * enough since we may be in a legacy share case.
2060 parent = sa_get_parent_group(share);
2061 iszfs = sa_group_is_zfs(parent);
2062 if (iszfs) {
2063 struct exportfs_args ea;
2064 share_t sh = { 0 };
2065 ea.dname = path;
2066 ea.uex = NULL;
2067 sh.sh_path = path;
2068 sh.sh_fstype = "nfs";
2070 err = sa_share_zfs(share, NULL, path, &sh,
2071 &ea, ZFS_UNSHARE_NFS);
2072 if (err != SA_OK) {
2073 errno = err;
2074 err = -1;
2076 } else {
2077 err = exportfs(path, NULL);
2079 if (err < 0) {
2081 * TBD: only an error in some
2082 * cases - need better analysis
2084 switch (errno) {
2085 case EPERM:
2086 case EACCES:
2087 ret = SA_NO_PERMISSION;
2088 if (getzoneid() != GLOBAL_ZONEID) {
2089 ret = SA_NOT_SUPPORTED;
2091 break;
2092 case EINVAL:
2093 case ENOENT:
2094 ret = SA_NO_SUCH_PATH;
2095 break;
2096 default:
2097 ret = SA_SYSTEM_ERR;
2098 break;
2101 if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
2102 handle = sa_find_group_handle((sa_group_t)share);
2103 if (!iszfs)
2104 (void) sa_delete_sharetab(handle, path, "nfs");
2105 /* just in case it was logged */
2106 (void) nfslogtab_deactivate(path);
2108 return (ret);
2111 static int
2112 check_user(char *value)
2114 int ret = SA_OK;
2116 if (!is_a_number(value)) {
2117 struct passwd *pw;
2119 * in this case it would have to be a
2120 * user name
2122 pw = getpwnam(value);
2123 if (pw == NULL)
2124 ret = SA_BAD_VALUE;
2125 endpwent();
2126 } else {
2127 uint64_t intval;
2128 intval = strtoull(value, NULL, 0);
2129 if (intval > UID_MAX && intval != -1)
2130 ret = SA_BAD_VALUE;
2133 return (ret);
2136 static int
2137 check_group(char *value)
2139 int ret = SA_OK;
2141 if (!is_a_number(value)) {
2142 struct group *gr;
2144 * in this case it would have to be a
2145 * group name
2147 gr = getgrnam(value);
2148 if (gr == NULL)
2149 ret = SA_BAD_VALUE;
2150 endgrent();
2151 } else {
2152 uint64_t intval;
2153 intval = strtoull(value, NULL, 0);
2154 if (intval > UID_MAX && intval != -1)
2155 ret = SA_BAD_VALUE;
2158 return (ret);
2162 * check_rorwnone(v1, v2, v3)
2164 * check ro vs rw vs none values. Over time this may get beefed up.
2165 * for now it just does simple checks. v1 is never NULL but v2 or v3
2166 * could be.
2169 static int
2170 check_rorwnone(char *v1, char *v2, char *v3)
2172 int ret = SA_OK;
2173 if (v2 != NULL && strcmp(v1, v2) == 0)
2174 ret = SA_VALUE_CONFLICT;
2175 else if (v3 != NULL && strcmp(v1, v3) == 0)
2176 ret = SA_VALUE_CONFLICT;
2178 return (ret);
2182 * nfs_validate_property(handle, property, parent)
2184 * Check that the property has a legitimate value for its type.
2187 static int
2188 nfs_validate_property(sa_handle_t handle, sa_property_t property,
2189 sa_optionset_t parent)
2191 int ret = SA_OK;
2192 char *propname;
2193 char *other1;
2194 char *other2;
2195 int optindex;
2196 nfsl_config_t *configlist;
2197 sa_group_t parent_group;
2198 char *value;
2200 propname = sa_get_property_attr(property, "type");
2202 if ((optindex = findopt(propname)) < 0)
2203 ret = SA_NO_SUCH_PROP;
2205 /* need to validate value range here as well */
2207 if (ret == SA_OK) {
2208 parent_group = sa_get_parent_group((sa_share_t)parent);
2209 if (optdefs[optindex].share && parent_group != NULL &&
2210 !sa_is_share(parent_group))
2211 ret = SA_PROP_SHARE_ONLY;
2213 if (ret == SA_OK) {
2214 if (optdefs[optindex].index == OPT_PUBLIC) {
2216 * Public is special in that only one instance can
2217 * be in the repository at the same time.
2219 if (public_exists(handle, parent_group)) {
2220 sa_free_attr_string(propname);
2221 return (SA_VALUE_CONFLICT);
2224 value = sa_get_property_attr(property, "value");
2225 if (value != NULL) {
2226 /* first basic type checking */
2227 switch (optdefs[optindex].type) {
2229 case OPT_TYPE_NUMBER:
2230 /* check that the value is all digits */
2231 if (!is_a_number(value))
2232 ret = SA_BAD_VALUE;
2233 break;
2235 case OPT_TYPE_BOOLEAN:
2236 if (strlen(value) == 0 ||
2237 strcasecmp(value, "true") == 0 ||
2238 strcmp(value, "1") == 0 ||
2239 strcasecmp(value, "false") == 0 ||
2240 strcmp(value, "0") == 0) {
2241 ret = SA_OK;
2242 } else {
2243 ret = SA_BAD_VALUE;
2245 break;
2247 case OPT_TYPE_USER:
2248 ret = check_user(value);
2249 break;
2251 case OPT_TYPE_FILE:
2252 if (strcmp(value, "..") == 0 ||
2253 strchr(value, '/') != NULL) {
2254 ret = SA_BAD_VALUE;
2256 break;
2258 case OPT_TYPE_ACCLIST: {
2259 sa_property_t oprop1;
2260 sa_property_t oprop2;
2261 char *ovalue1 = NULL;
2262 char *ovalue2 = NULL;
2264 if (parent == NULL)
2265 break;
2267 * access list handling. Should eventually
2268 * validate that all the values make sense.
2269 * Also, ro and rw may have cross value
2270 * conflicts.
2272 if (strcmp(propname, SHOPT_RO) == 0) {
2273 other1 = SHOPT_RW;
2274 other2 = SHOPT_NONE;
2275 } else if (strcmp(propname, SHOPT_RW) == 0) {
2276 other1 = SHOPT_RO;
2277 other2 = SHOPT_NONE;
2278 } else if (strcmp(propname, SHOPT_NONE) == 0) {
2279 other1 = SHOPT_RO;
2280 other2 = SHOPT_RW;
2281 } else {
2282 other1 = NULL;
2283 other2 = NULL;
2285 if (other1 == NULL && other2 == NULL)
2286 break;
2288 /* compare rw(ro) with ro(rw) */
2290 oprop1 = sa_get_property(parent, other1);
2291 oprop2 = sa_get_property(parent, other2);
2292 if (oprop1 == NULL && oprop2 == NULL)
2293 break;
2295 * Only potential confusion if other1
2296 * or other2 exists. Check the values
2297 * and run the check if there is a
2298 * value other than the one we are
2299 * explicitly looking at.
2301 ovalue1 = sa_get_property_attr(oprop1, "value");
2302 ovalue2 = sa_get_property_attr(oprop2, "value");
2303 if (ovalue1 != NULL || ovalue2 != NULL)
2304 ret = check_rorwnone(value, ovalue1,
2305 ovalue2);
2307 if (ovalue1 != NULL)
2308 sa_free_attr_string(ovalue1);
2309 if (ovalue2 != NULL)
2310 sa_free_attr_string(ovalue2);
2311 break;
2314 case OPT_TYPE_LOGTAG:
2315 if (nfsl_getconfig_list(&configlist) == 0) {
2316 int error;
2317 if (value == NULL ||
2318 strlen(value) == 0) {
2319 if (value != NULL)
2320 sa_free_attr_string(
2321 value);
2322 value = strdup("global");
2324 if (value != NULL &&
2325 nfsl_findconfig(configlist, value,
2326 &error) == NULL) {
2327 ret = SA_BAD_VALUE;
2329 /* Must always free when done */
2330 nfsl_freeconfig_list(&configlist);
2331 } else {
2332 ret = SA_CONFIG_ERR;
2334 break;
2336 case OPT_TYPE_STRING:
2337 /* whatever is here should be ok */
2338 break;
2340 case OPT_TYPE_SECURITY:
2342 * The "sec" property isn't used in the
2343 * non-legacy parts of sharemgr. We need to
2344 * reject it here. For legacy, it is pulled
2345 * out well before we get here.
2347 ret = SA_NO_SUCH_PROP;
2348 break;
2350 case OPT_TYPE_MAPPING: {
2351 char *p;
2352 char *n;
2353 char *c;
2354 int (*f)(char *);
2356 sa_security_t security;
2359 * mapping is only supported for sec=sys
2361 ret = SA_CONFIG_ERR;
2362 if (parent_group == NULL)
2363 break;
2365 for (security = sa_get_security(parent_group,
2366 NULL, NULL); security != NULL;
2367 security = sa_get_next_security(security)) {
2368 char *type;
2369 char *sectype;
2371 type = sa_get_security_attr(security,
2372 "type");
2373 if (type == NULL)
2374 continue;
2376 if (strcmp(type, "nfs") != 0) {
2377 sa_free_attr_string(type);
2378 continue;
2380 sa_free_attr_string(type);
2382 sectype = sa_get_security_attr(security,
2383 "sectype");
2384 if (sectype == NULL)
2385 continue;
2387 if (strcmp(sectype, "sys") != 0) {
2388 sa_free_attr_string(sectype);
2389 ret = SA_CONFIG_ERR;
2390 break;
2392 sa_free_attr_string(sectype);
2393 ret = SA_OK;
2396 if (ret != SA_OK)
2397 break;
2399 assert(optindex == OPT_UIDMAP ||
2400 optindex == OPT_GIDMAP);
2401 f = optindex == OPT_UIDMAP ? check_user :
2402 check_group;
2405 p = strdup(value);
2406 if (p == NULL)
2407 ret = SA_BAD_VALUE;
2409 for (c = p; ret == SA_OK && c != NULL; c = n) {
2410 char *s;
2411 char *t;
2413 n = strchr(c, '~');
2414 if (n != NULL)
2415 *n++ = '\0';
2417 s = strchr(c, ':');
2418 if (s != NULL) {
2419 *s++ = '\0';
2420 t = strchr(s, ':');
2421 if (t != NULL)
2422 *t = '\0';
2425 if (s == NULL || t == NULL)
2426 ret = SA_BAD_VALUE;
2428 if (ret == SA_OK && *c != '\0' &&
2429 strcmp(c, "*") != 0)
2430 ret = f(c);
2432 if (ret == SA_OK && *s != '\0' &&
2433 strcmp(s, "-1") != 0)
2434 ret = f(s);
2437 free(p);
2439 break;
2442 default:
2443 break;
2446 if (value != NULL)
2447 sa_free_attr_string(value);
2449 if (ret == SA_OK && optdefs[optindex].check != NULL) {
2450 /* do the property specific check */
2451 ret = optdefs[optindex].check(handle, property);
2456 if (propname != NULL)
2457 sa_free_attr_string(propname);
2458 return (ret);
2462 * Protocol management functions
2464 * Properties defined in the default files are defined in
2465 * proto_option_defs for parsing and validation. If "other" and
2466 * "compare" are set, then the value for this property should be
2467 * compared against the property specified in "other" using the
2468 * "compare" check (either <= or >=) in order to ensure that the
2469 * values are in the correct range. E.g. setting server_versmin
2470 * higher than server_versmax should not be allowed.
2473 struct proto_option_defs {
2474 char *tag;
2475 char *name; /* display name -- remove protocol identifier */
2476 int index;
2477 int type;
2478 union {
2479 int intval;
2480 char *string;
2481 } defvalue;
2482 uint32_t svcs;
2483 int32_t minval;
2484 int32_t maxval;
2485 char *other;
2486 int compare;
2487 #define OPT_CMP_GE 0
2488 #define OPT_CMP_LE 1
2489 int (*check)(char *);
2490 } proto_options[] = {
2491 #define PROTO_OPT_NFSD_SERVERS 0
2492 {"nfsd_servers",
2493 "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 1024, SVC_NFSD,
2494 1, INT32_MAX},
2495 #define PROTO_OPT_LOCKD_LISTEN_BACKLOG 1
2496 {"lockd_listen_backlog",
2497 "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
2498 OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX},
2499 #define PROTO_OPT_LOCKD_SERVERS 2
2500 {"lockd_servers",
2501 "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 256,
2502 SVC_LOCKD, 1, INT32_MAX},
2503 #define PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT 3
2504 {"lockd_retransmit_timeout",
2505 "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
2506 OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX},
2507 #define PROTO_OPT_GRACE_PERIOD 4
2508 {"grace_period",
2509 "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
2510 SVC_LOCKD, 0, INT32_MAX},
2511 #define PROTO_OPT_NFS_SERVER_VERSMIN 5
2512 {"nfs_server_versmin",
2513 "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
2514 (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2515 NFS_VERSMAX, "server_versmax", OPT_CMP_LE},
2516 #define PROTO_OPT_NFS_SERVER_VERSMAX 6
2517 {"nfs_server_versmax",
2518 "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
2519 (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2520 NFS_VERSMAX, "server_versmin", OPT_CMP_GE},
2521 #define PROTO_OPT_NFS_CLIENT_VERSMIN 7
2522 {"nfs_client_versmin",
2523 "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
2524 (int)NFS_VERSMIN_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2525 "client_versmax", OPT_CMP_LE},
2526 #define PROTO_OPT_NFS_CLIENT_VERSMAX 8
2527 {"nfs_client_versmax",
2528 "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
2529 (int)NFS_VERSMAX_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2530 "client_versmin", OPT_CMP_GE},
2531 #define PROTO_OPT_NFS_SERVER_DELEGATION 9
2532 {"nfs_server_delegation",
2533 "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
2534 OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0},
2535 #define PROTO_OPT_NFSMAPID_DOMAIN 10
2536 {"nfsmapid_domain",
2537 "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2538 0, SVC_NFSMAPID, 0, 0},
2539 #define PROTO_OPT_NFSD_MAX_CONNECTIONS 11
2540 {"nfsd_max_connections",
2541 "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2542 OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX},
2543 #define PROTO_OPT_NFSD_PROTOCOL 12
2544 {"nfsd_protocol",
2545 "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2546 SVC_NFSD, 0, 0},
2547 #define PROTO_OPT_NFSD_LISTEN_BACKLOG 13
2548 {"nfsd_listen_backlog",
2549 "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2550 OPT_TYPE_NUMBER, 0, SVC_NFSD, 0, INT32_MAX},
2551 #define PROTO_OPT_NFSD_DEVICE 14
2552 {"nfsd_device",
2553 "device", PROTO_OPT_NFSD_DEVICE,
2554 OPT_TYPE_STRING, 0, SVC_NFSD, 0, 0},
2555 #define PROTO_OPT_MOUNTD_LISTEN_BACKLOG 15
2556 {"mountd_listen_backlog",
2557 "mountd_listen_backlog", PROTO_OPT_MOUNTD_LISTEN_BACKLOG,
2558 OPT_TYPE_NUMBER, 64, SVC_NFSD|SVC_MOUNTD, 1, INT32_MAX},
2559 #define PROTO_OPT_MOUNTD_MAX_THREADS 16
2560 {"mountd_max_threads",
2561 "mountd_max_threads", PROTO_OPT_MOUNTD_MAX_THREADS,
2562 OPT_TYPE_NUMBER, 16, SVC_NFSD|SVC_MOUNTD, 1, INT32_MAX},
2563 {NULL}
2567 * the protoset holds the defined options so we don't have to read
2568 * them multiple times
2570 static sa_protocol_properties_t protoset;
2572 static int
2573 findprotoopt(char *name, int whichname)
2575 int i;
2576 for (i = 0; proto_options[i].tag != NULL; i++) {
2577 if (whichname == 1) {
2578 if (strcasecmp(proto_options[i].name, name) == 0)
2579 return (i);
2580 } else {
2581 if (strcasecmp(proto_options[i].tag, name) == 0)
2582 return (i);
2585 return (-1);
2589 * fixcaselower(str)
2591 * convert a string to lower case (inplace).
2594 static void
2595 fixcaselower(char *str)
2597 while (*str) {
2598 *str = tolower(*str);
2599 str++;
2604 * skipwhitespace(str)
2606 * Skip leading white space. It is assumed that it is called with a
2607 * valid pointer.
2610 static char *
2611 skipwhitespace(char *str)
2613 while (*str && isspace(*str))
2614 str++;
2616 return (str);
2620 * extractprop()
2622 * Extract the property and value out of the line and create the
2623 * property in the optionset.
2625 static int
2626 extractprop(char *name, char *value)
2628 sa_property_t prop;
2629 int index;
2630 int ret = SA_OK;
2632 * Remove any leading
2633 * white space.
2635 name = skipwhitespace(name);
2637 index = findprotoopt(name, 1);
2638 if (index >= 0) {
2639 fixcaselower(name);
2640 prop = sa_create_property(proto_options[index].name, value);
2641 if (prop != NULL)
2642 ret = sa_add_protocol_property(protoset, prop);
2643 else
2644 ret = SA_NO_MEMORY;
2646 return (ret);
2649 scf_type_t
2650 getscftype(int type)
2652 scf_type_t ret;
2654 switch (type) {
2655 case OPT_TYPE_NUMBER:
2656 ret = SCF_TYPE_INTEGER;
2657 break;
2658 case OPT_TYPE_BOOLEAN:
2659 ret = SCF_TYPE_BOOLEAN;
2660 break;
2661 default:
2662 ret = SCF_TYPE_ASTRING;
2664 return (ret);
2667 char *
2668 getsvcname(uint32_t svcs)
2670 char *service;
2671 switch (svcs) {
2672 case SVC_LOCKD:
2673 service = LOCKD;
2674 break;
2675 case SVC_STATD:
2676 service = STATD;
2677 break;
2678 case SVC_NFSD:
2679 service = NFSD;
2680 break;
2681 case SVC_CLIENT:
2682 service = NFS_CLIENT_SVC;
2683 break;
2684 case SVC_NFS4CBD:
2685 service = NFS4CBD;
2686 break;
2687 case SVC_NFSMAPID:
2688 service = NFSMAPID;
2689 break;
2690 case SVC_RQUOTAD:
2691 service = RQUOTAD;
2692 break;
2693 case SVC_NFSLOGD:
2694 service = NFSLOGD;
2695 break;
2696 case SVC_REPARSED:
2697 service = REPARSED;
2698 break;
2699 default:
2700 service = NFSD;
2702 return (service);
2706 * initprotofromsmf()
2708 * Read NFS SMF properties and add the defined values to the
2709 * protoset. Note that default values are known from the built in
2710 * table in case SMF doesn't have a definition. Not having
2711 * SMF properties is OK since we have builtin default
2712 * values.
2714 static int
2715 initprotofromsmf()
2717 char name[PATH_MAX];
2718 char value[PATH_MAX];
2719 int ret = SA_OK, bufsz = 0, i;
2721 protoset = sa_create_protocol_properties("nfs");
2722 if (protoset != NULL) {
2723 for (i = 0; proto_options[i].tag != NULL; i++) {
2724 scf_type_t ptype;
2725 char *svc_name;
2727 bzero(value, PATH_MAX);
2728 (void) strncpy(name, proto_options[i].name, PATH_MAX);
2729 /* Replace NULL with the correct instance */
2730 ptype = getscftype(proto_options[i].type);
2731 svc_name = getsvcname(proto_options[i].svcs);
2732 bufsz = PATH_MAX;
2733 ret = nfs_smf_get_prop(name, value,
2734 (char *)DEFAULT_INSTANCE, ptype,
2735 svc_name, &bufsz);
2736 if (ret == SA_OK) {
2737 ret = extractprop(name, value);
2740 } else {
2741 ret = SA_NO_MEMORY;
2744 return (ret);
2748 * add_defaults()
2750 * Add the default values for any property not defined
2751 * in NFS SMF repository.
2752 * Values are set according to their defined types.
2755 static void
2756 add_defaults()
2758 int i;
2759 char number[MAXDIGITS];
2761 for (i = 0; proto_options[i].tag != NULL; i++) {
2762 sa_property_t prop;
2763 prop = sa_get_protocol_property(protoset,
2764 proto_options[i].name);
2765 if (prop == NULL) {
2766 /* add the default value */
2767 switch (proto_options[i].type) {
2768 case OPT_TYPE_NUMBER:
2769 (void) snprintf(number, sizeof (number), "%d",
2770 proto_options[i].defvalue.intval);
2771 prop = sa_create_property(proto_options[i].name,
2772 number);
2773 break;
2775 case OPT_TYPE_BOOLEAN:
2776 prop = sa_create_property(proto_options[i].name,
2777 proto_options[i].defvalue.intval ?
2778 "true" : "false");
2779 break;
2781 case OPT_TYPE_ONOFF:
2782 prop = sa_create_property(proto_options[i].name,
2783 proto_options[i].defvalue.intval ?
2784 "on" : "off");
2785 break;
2787 default:
2788 /* treat as strings of zero length */
2789 prop = sa_create_property(proto_options[i].name,
2790 "");
2791 break;
2793 if (prop != NULL)
2794 (void) sa_add_protocol_property(protoset, prop);
2799 static void
2800 free_protoprops()
2802 if (protoset != NULL) {
2803 xmlFreeNode(protoset);
2804 protoset = NULL;
2809 * nfs_init()
2811 * Initialize the NFS plugin.
2814 static int
2815 nfs_init()
2817 int ret = SA_OK;
2819 if (sa_plugin_ops.sa_init != nfs_init) {
2820 (void) printf(dgettext(TEXT_DOMAIN,
2821 "NFS plugin not properly initialized\n"));
2822 return (SA_CONFIG_ERR);
2825 ret = initprotofromsmf();
2826 if (ret != SA_OK) {
2827 (void) printf(dgettext(TEXT_DOMAIN,
2828 "NFS plugin problem with SMF repository: %s\n"),
2829 sa_errorstr(ret));
2830 ret = SA_OK;
2832 add_defaults();
2834 return (ret);
2838 * nfs_fini()
2840 * uninitialize the NFS plugin. Want to avoid memory leaks.
2843 static void
2844 nfs_fini()
2846 free_protoprops();
2850 * nfs_get_proto_set()
2852 * Return an optionset with all the protocol specific properties in
2853 * it.
2856 static sa_protocol_properties_t
2857 nfs_get_proto_set()
2859 return (protoset);
2863 * service_in_state(service, chkstate)
2865 * Want to know if the specified service is in the desired state
2866 * (chkstate) or not. Return true (1) if it is and false (0) if it
2867 * isn't.
2869 static int
2870 service_in_state(char *service, const char *chkstate)
2872 char *state;
2873 int ret = B_FALSE;
2875 state = smf_get_state(service);
2876 if (state != NULL) {
2877 /* got the state so get the equality for the return value */
2878 ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2879 free(state);
2881 return (ret);
2885 * restart_service(svcs)
2887 * Walk through the bit mask of services that need to be restarted in
2888 * order to use the new property values. Some properties affect
2889 * multiple daemons. Should only restart a service if it is currently
2890 * enabled (online).
2893 static void
2894 restart_service(uint32_t svcs)
2896 uint32_t mask;
2897 int ret;
2898 char *service;
2900 for (mask = 1; svcs != 0; mask <<= 1) {
2901 switch (svcs & mask) {
2902 case SVC_LOCKD:
2903 service = LOCKD;
2904 break;
2905 case SVC_STATD:
2906 service = STATD;
2907 break;
2908 case SVC_NFSD:
2909 service = NFSD;
2910 break;
2911 case SVC_MOUNTD:
2912 service = MOUNTD;
2913 break;
2914 case SVC_NFS4CBD:
2915 service = NFS4CBD;
2916 break;
2917 case SVC_NFSMAPID:
2918 service = NFSMAPID;
2919 break;
2920 case SVC_RQUOTAD:
2921 service = RQUOTAD;
2922 break;
2923 case SVC_NFSLOGD:
2924 service = NFSLOGD;
2925 break;
2926 case SVC_REPARSED:
2927 service = REPARSED;
2928 break;
2929 case SVC_CLIENT:
2930 service = NFS_CLIENT_SVC;
2931 break;
2932 default:
2933 continue;
2937 * Only attempt to restart the service if it is
2938 * currently running. In the future, it may be
2939 * desirable to use smf_refresh_instance if the NFS
2940 * services ever implement the refresh method.
2942 if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2943 ret = smf_restart_instance(service);
2945 * There are only a few SMF errors at this point, but
2946 * it is also possible that a bad value may have put
2947 * the service into maintenance if there wasn't an
2948 * SMF level error.
2950 if (ret != 0) {
2951 (void) fprintf(stderr,
2952 dgettext(TEXT_DOMAIN,
2953 "%s failed to restart: %s\n"),
2954 service, scf_strerror(scf_error()));
2955 } else {
2957 * Check whether it has gone to "maintenance"
2958 * mode or not. Maintenance implies something
2959 * went wrong.
2961 if (service_in_state(service,
2962 SCF_STATE_STRING_MAINT)) {
2963 (void) fprintf(stderr,
2964 dgettext(TEXT_DOMAIN,
2965 "%s failed to restart\n"),
2966 service);
2970 svcs &= ~mask;
2975 * nfs_minmax_check(name, value)
2977 * Verify that the value for the property specified by index is valid
2978 * relative to the opposite value in the case of a min/max variable.
2979 * Currently, server_minvers/server_maxvers and
2980 * client_minvers/client_maxvers are the only ones to check.
2983 static int
2984 nfs_minmax_check(int index, int value)
2986 int val;
2987 char *pval;
2988 sa_property_t prop;
2989 sa_optionset_t opts;
2990 int ret = B_TRUE;
2992 if (proto_options[index].other != NULL) {
2993 /* have a property to compare against */
2994 opts = nfs_get_proto_set();
2995 prop = sa_get_property(opts, proto_options[index].other);
2997 * If we don't find the property, assume default
2998 * values which will work since the max will be at the
2999 * max and the min at the min.
3001 if (prop != NULL) {
3002 pval = sa_get_property_attr(prop, "value");
3003 if (pval != NULL) {
3004 val = strtoul(pval, NULL, 0);
3005 if (proto_options[index].compare ==
3006 OPT_CMP_LE) {
3007 ret = value <= val ? B_TRUE : B_FALSE;
3008 } else if (proto_options[index].compare ==
3009 OPT_CMP_GE) {
3010 ret = value >= val ? B_TRUE : B_FALSE;
3012 sa_free_attr_string(pval);
3016 return (ret);
3020 * nfs_validate_proto_prop(index, name, value)
3022 * Verify that the property specified by name can take the new
3023 * value. This is a sanity check to prevent bad values getting into
3024 * the default files. All values need to be checked against what is
3025 * allowed by their defined type. If a type isn't explicitly defined
3026 * here, it is treated as a string.
3028 * Note that OPT_TYPE_NUMBER will additionally check that the value is
3029 * within the range specified and potentially against another property
3030 * value as well as specified in the proto_options members other and
3031 * compare.
3034 static int
3035 nfs_validate_proto_prop(int index, char *name, char *value)
3037 int ret = SA_OK;
3038 char *cp;
3039 switch (proto_options[index].type) {
3040 case OPT_TYPE_NUMBER:
3041 if (!is_a_number(value))
3042 ret = SA_BAD_VALUE;
3043 else {
3044 int val;
3045 val = strtoul(value, NULL, 0);
3046 if (val < proto_options[index].minval ||
3047 val > proto_options[index].maxval)
3048 ret = SA_BAD_VALUE;
3050 * For server_versmin/server_versmax and
3051 * client_versmin/client_versmax, the value of the
3052 * min(max) should be checked to be correct relative
3053 * to the current max(min).
3055 if (!nfs_minmax_check(index, val)) {
3056 ret = SA_BAD_VALUE;
3059 break;
3061 case OPT_TYPE_DOMAIN:
3063 * needs to be a qualified domain so will have at
3064 * least one period and other characters on either
3065 * side of it. A zero length string is also allowed
3066 * and is the way to turn off the override.
3068 if (strlen(value) == 0)
3069 break;
3070 cp = strchr(value, '.');
3071 if (cp == NULL || cp == value || strchr(value, '@') != NULL)
3072 ret = SA_BAD_VALUE;
3073 break;
3075 case OPT_TYPE_BOOLEAN:
3076 if (strlen(value) == 0 ||
3077 strcasecmp(value, "true") == 0 ||
3078 strcmp(value, "1") == 0 ||
3079 strcasecmp(value, "false") == 0 ||
3080 strcmp(value, "0") == 0) {
3081 ret = SA_OK;
3082 } else {
3083 ret = SA_BAD_VALUE;
3085 break;
3087 case OPT_TYPE_ONOFF:
3088 if (strcasecmp(value, "on") != 0 &&
3089 strcasecmp(value, "off") != 0) {
3090 ret = SA_BAD_VALUE;
3092 break;
3094 case OPT_TYPE_PROTOCOL: {
3095 struct netconfig *nconf;
3096 void *nc;
3097 boolean_t pfound = B_FALSE;
3099 if (strcasecmp(value, "all") == 0)
3100 break;
3102 if ((nc = setnetconfig()) == NULL) {
3103 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3104 "setnetconfig failed: %s\n"), strerror(errno));
3105 } else {
3106 while ((nconf = getnetconfig(nc)) != NULL) {
3107 if (strcmp(nconf->nc_proto, value) == 0) {
3108 pfound = B_TRUE;
3109 break;
3112 (void) endnetconfig(nc);
3115 if (!pfound)
3116 ret = SA_BAD_VALUE;
3117 break;
3120 default:
3121 /* treat as a string */
3122 break;
3124 return (ret);
3128 * nfs_set_proto_prop(prop)
3130 * check that prop is valid.
3133 static int
3134 nfs_set_proto_prop(sa_property_t prop)
3136 int ret = SA_OK;
3137 char *name;
3138 char *value;
3140 name = sa_get_property_attr(prop, "type");
3141 value = sa_get_property_attr(prop, "value");
3142 if (name != NULL && value != NULL) {
3143 scf_type_t sctype;
3144 char *svc_name;
3145 char *instance = NULL;
3146 int index = findprotoopt(name, 1);
3148 ret = nfs_validate_proto_prop(index, name, value);
3149 if (ret == SA_OK) {
3150 sctype = getscftype(proto_options[index].type);
3151 svc_name = getsvcname(proto_options[index].svcs);
3152 if (sctype == SCF_TYPE_BOOLEAN) {
3153 if (value != NULL)
3154 sa_free_attr_string(value);
3155 if (string_to_boolean(value) == 0)
3156 value = strdup("0");
3157 else
3158 value = strdup("1");
3160 ret = nfs_smf_set_prop(name, value, instance, sctype,
3161 svc_name);
3162 if (ret == SA_OK) {
3163 restart_service(proto_options[index].svcs);
3164 } else {
3165 (void) printf(dgettext(TEXT_DOMAIN,
3166 "Cannot restart NFS services : %s\n"),
3167 sa_errorstr(ret));
3171 if (name != NULL)
3172 sa_free_attr_string(name);
3173 if (value != NULL)
3174 sa_free_attr_string(value);
3175 return (ret);
3179 * nfs_get_status()
3181 * What is the current status of the nfsd? We use the SMF state here.
3182 * Caller must free the returned value.
3185 static char *
3186 nfs_get_status()
3188 return (smf_get_state(NFSD));
3192 * nfs_space_alias(alias)
3194 * Lookup the space (security) name. If it is default, convert to the
3195 * real name.
3198 static char *
3199 nfs_space_alias(char *space)
3201 char *name = space;
3202 seconfig_t secconf;
3205 * Only the space named "default" is special. If it is used,
3206 * the default needs to be looked up and the real name used.
3207 * This is normally "sys" but could be changed. We always
3208 * change default to the real name.
3210 if (strcmp(space, "default") == 0 &&
3211 nfs_getseconfig_default(&secconf) == 0) {
3212 if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
3213 name = secconf.sc_name;
3215 return (strdup(name));
3219 * nfs_features()
3221 * Return a mask of the features required.
3224 static uint64_t
3225 nfs_features()
3227 return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);