Print this page
10120 smatch indenting fixes for usr/src/cmd
Reviewed by: Gergő Doma <domag02@gmail.com>
Portions contributed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/dfs.cmds/sharemgr/commands.c
+++ new/usr/src/cmd/dfs.cmds/sharemgr/commands.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2012 Milan Jurik. All rights reserved.
26 26 */
27 27
28 28 #include <sys/types.h>
29 29 #include <sys/stat.h>
30 30 #include <fcntl.h>
31 31 #include <stdlib.h>
32 32 #include <stdio.h>
33 33 #include <string.h>
34 34 #include <ctype.h>
35 35 #include <unistd.h>
36 36 #include <getopt.h>
37 37 #include <utmpx.h>
38 38 #include <pwd.h>
39 39 #include <auth_attr.h>
40 40 #include <secdb.h>
41 41 #include <sys/param.h>
42 42 #include <sys/stat.h>
43 43 #include <errno.h>
44 44
45 45 #include <libshare.h>
46 46 #include "sharemgr.h"
47 47 #include <libscf.h>
48 48 #include <libxml/tree.h>
49 49 #include <libintl.h>
50 50 #include <assert.h>
51 51 #include <iconv.h>
52 52 #include <langinfo.h>
53 53 #include <dirent.h>
54 54
55 55 static char *sa_get_usage(sa_usage_t);
56 56
57 57 /*
58 58 * Implementation of the common sub-commands supported by sharemgr.
59 59 * A number of helper functions are also included.
60 60 */
61 61
62 62 /*
63 63 * has_protocol(group, proto)
64 64 * If the group has an optionset with the specified protocol,
65 65 * return true (1) otherwise false (0).
66 66 */
67 67 static int
68 68 has_protocol(sa_group_t group, char *protocol)
69 69 {
70 70 sa_optionset_t optionset;
71 71 int result = 0;
72 72
73 73 optionset = sa_get_optionset(group, protocol);
74 74 if (optionset != NULL) {
75 75 result++;
76 76 }
77 77 return (result);
78 78 }
79 79
80 80 /*
81 81 * validresource(name)
82 82 *
83 83 * Check that name only has valid characters in it. The current valid
84 84 * set are the printable characters but not including:
85 85 * " / \ [ ] : | < > + ; , ? * = \t
86 86 * Note that space is included and there is a maximum length.
87 87 */
88 88 static int
89 89 validresource(const char *name)
90 90 {
91 91 const char *cp;
92 92 size_t len;
93 93
94 94 if (name == NULL)
95 95 return (B_FALSE);
96 96
97 97 len = strlen(name);
98 98 if (len == 0 || len > SA_MAX_RESOURCE_NAME)
99 99 return (B_FALSE);
100 100
101 101 if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
102 102 return (B_FALSE);
103 103 }
104 104
105 105 for (cp = name; *cp != '\0'; cp++)
106 106 if (iscntrl(*cp))
107 107 return (B_FALSE);
108 108
109 109 return (B_TRUE);
110 110 }
111 111
112 112 /*
113 113 * conv_to_utf8(input)
114 114 *
115 115 * Convert the input string to utf8 from the current locale. If the
116 116 * conversion fails, use the current locale, it is likely close
117 117 * enough. For example, the "C" locale is a subset of utf-8. The
118 118 * return value may be a new string or the original input string.
119 119 */
120 120
121 121 static char *
122 122 conv_to_utf8(char *input)
123 123 {
124 124 iconv_t cd;
125 125 char *inval = input;
126 126 char *output = input;
127 127 char *outleft;
128 128 char *curlocale;
129 129 size_t bytesleft;
130 130 size_t size;
131 131 size_t osize;
132 132 static int warned = 0;
133 133
134 134 curlocale = nl_langinfo(CODESET);
135 135 if (curlocale == NULL)
136 136 curlocale = "C";
137 137 cd = iconv_open("UTF-8", curlocale);
138 138 if (cd != NULL && cd != (iconv_t)-1) {
139 139 size = strlen(input);
140 140 /* Assume worst case of characters expanding to 4 bytes. */
141 141 bytesleft = size * 4;
142 142 output = calloc(bytesleft, 1);
143 143 if (output != NULL) {
144 144 outleft = output;
145 145 /* inval can be modified on return */
146 146 osize = iconv(cd, (const char **)&inval, &size,
147 147 &outleft, &bytesleft);
148 148 if (osize == (size_t)-1 || size != 0) {
149 149 free(output);
150 150 output = input;
151 151 }
152 152 } else {
153 153 /* Need to return something. */
154 154 output = input;
155 155 }
156 156 (void) iconv_close(cd);
157 157 } else {
158 158 if (!warned)
159 159 (void) fprintf(stderr,
160 160 gettext("Cannot convert to UTF-8 from %s\n"),
161 161 curlocale ? curlocale : gettext("unknown"));
162 162 warned = 1;
163 163 }
164 164 return (output);
165 165 }
166 166
167 167 /*
168 168 * conv_from(input)
169 169 *
170 170 * Convert the input string from utf8 to current locale. If the
171 171 * conversion isn't supported, just use as is. The return value may be
172 172 * a new string or the original input string.
173 173 */
174 174
175 175 static char *
176 176 conv_from_utf8(char *input)
177 177 {
178 178 iconv_t cd;
179 179 char *output = input;
180 180 char *inval = input;
181 181 char *outleft;
182 182 char *curlocale;
183 183 size_t bytesleft;
184 184 size_t size;
185 185 size_t osize;
186 186 static int warned = 0;
187 187
188 188 curlocale = nl_langinfo(CODESET);
189 189 if (curlocale == NULL)
190 190 curlocale = "C";
191 191 cd = iconv_open(curlocale, "UTF-8");
192 192 if (cd != NULL && cd != (iconv_t)-1) {
193 193 size = strlen(input);
194 194 /* Assume worst case of characters expanding to 4 bytes. */
195 195 bytesleft = size * 4;
196 196 output = calloc(bytesleft, 1);
197 197 if (output != NULL) {
198 198 outleft = output;
199 199 osize = iconv(cd, (const char **)&inval, &size,
200 200 &outleft, &bytesleft);
201 201 if (osize == (size_t)-1 || size != 0)
202 202 output = input;
203 203 } else {
204 204 /* Need to return something. */
205 205 output = input;
206 206 }
207 207 (void) iconv_close(cd);
208 208 } else {
209 209 if (!warned)
210 210 (void) fprintf(stderr,
211 211 gettext("Cannot convert to %s from UTF-8\n"),
212 212 curlocale ? curlocale : gettext("unknown"));
213 213 warned = 1;
214 214 }
215 215 return (output);
216 216 }
217 217
218 218 /*
219 219 * print_rsrc_desc(resource, sharedesc)
220 220 *
221 221 * Print the resource description string after converting from UTF8 to
222 222 * the current locale. If sharedesc is not NULL and there is no
223 223 * description on the resource, use sharedesc. sharedesc will already
224 224 * be converted to UTF8.
225 225 */
226 226
227 227 static void
228 228 print_rsrc_desc(sa_resource_t resource, char *sharedesc)
229 229 {
230 230 char *description;
231 231 char *desc;
232 232
233 233 if (resource == NULL)
234 234 return;
235 235
236 236 description = sa_get_resource_description(resource);
237 237 if (description != NULL) {
238 238 desc = conv_from_utf8(description);
239 239 if (desc != description) {
240 240 sa_free_share_description(description);
241 241 description = desc;
242 242 }
243 243 } else if (sharedesc != NULL) {
244 244 description = strdup(sharedesc);
245 245 }
246 246 if (description != NULL) {
247 247 (void) printf("\t\"%s\"", description);
248 248 sa_free_share_description(description);
249 249 }
250 250 }
251 251
252 252 /*
253 253 * set_resource_desc(share, description)
254 254 *
255 255 * Set the share description value after converting the description
256 256 * string to UTF8 from the current locale.
257 257 */
258 258
259 259 static int
260 260 set_resource_desc(sa_share_t share, char *description)
261 261 {
262 262 char *desc;
263 263 int ret;
264 264
265 265 desc = conv_to_utf8(description);
266 266 ret = sa_set_resource_description(share, desc);
267 267 if (description != desc)
268 268 sa_free_share_description(desc);
269 269 return (ret);
270 270 }
271 271
272 272 /*
273 273 * set_share_desc(share, description)
274 274 *
275 275 * Set the resource description value after converting the description
276 276 * string to UTF8 from the current locale.
277 277 */
278 278
279 279 static int
280 280 set_share_desc(sa_share_t share, char *description)
281 281 {
282 282 char *desc;
283 283 int ret;
284 284
285 285 desc = conv_to_utf8(description);
286 286 ret = sa_set_share_description(share, desc);
287 287 if (description != desc)
288 288 sa_free_share_description(desc);
289 289 return (ret);
290 290 }
291 291
292 292 /*
293 293 * add_list(list, item, data, proto)
294 294 * Adds a new list member that points holds item in the list.
295 295 * If list is NULL, it starts a new list. The function returns
296 296 * the first member of the list.
297 297 */
298 298 struct list *
299 299 add_list(struct list *listp, void *item, void *data, char *proto)
300 300 {
301 301 struct list *new, *tmp;
302 302
303 303 new = malloc(sizeof (struct list));
304 304 if (new != NULL) {
305 305 new->next = NULL;
306 306 new->item = item;
307 307 new->itemdata = data;
308 308 new->proto = proto;
309 309 } else {
310 310 return (listp);
311 311 }
312 312
313 313 if (listp == NULL)
314 314 return (new);
315 315
316 316 for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
317 317 /* get to end of list */
318 318 }
319 319 tmp->next = new;
320 320 return (listp);
321 321 }
322 322
323 323 /*
324 324 * free_list(list)
325 325 * Given a list, free all the members of the list;
326 326 */
327 327 static void
328 328 free_list(struct list *listp)
329 329 {
330 330 struct list *tmp;
331 331 while (listp != NULL) {
332 332 tmp = listp;
333 333 listp = listp->next;
334 334 free(tmp);
335 335 }
336 336 }
337 337
338 338 /*
339 339 * check_authorization(instname, which)
340 340 *
341 341 * Checks to see if the specific type of authorization in which is
342 342 * enabled for the user in this SMF service instance.
343 343 */
344 344
345 345 static int
346 346 check_authorization(char *instname, int which)
347 347 {
348 348 scf_handle_t *handle = NULL;
349 349 scf_simple_prop_t *prop = NULL;
350 350 char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
351 351 char *authstr = NULL;
352 352 ssize_t numauths;
353 353 int ret = B_TRUE;
354 354 uid_t uid;
355 355 struct passwd *pw = NULL;
356 356
357 357 uid = getuid();
358 358 pw = getpwuid(uid);
359 359 if (pw == NULL) {
360 360 ret = B_FALSE;
361 361 } else {
362 362 /*
363 363 * Since names are restricted to SA_MAX_NAME_LEN won't
364 364 * overflow.
365 365 */
366 366 (void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
367 367 SA_SVC_FMRI_BASE, instname);
368 368 handle = scf_handle_create(SCF_VERSION);
369 369 if (handle != NULL) {
370 370 if (scf_handle_bind(handle) == 0) {
371 371 switch (which) {
372 372 case SVC_SET:
373 373 prop = scf_simple_prop_get(handle,
374 374 svcstring, "general",
375 375 SVC_AUTH_VALUE);
376 376 break;
377 377 case SVC_ACTION:
378 378 prop = scf_simple_prop_get(handle,
379 379 svcstring, "general",
380 380 SVC_AUTH_ACTION);
381 381 break;
382 382 }
383 383 }
384 384 }
385 385 }
386 386 /* make sure we have an authorization string property */
387 387 if (prop != NULL) {
388 388 int i;
389 389 numauths = scf_simple_prop_numvalues(prop);
390 390 for (ret = 0, i = 0; i < numauths; i++) {
391 391 authstr = scf_simple_prop_next_astring(prop);
392 392 if (authstr != NULL) {
393 393 /* check if this user has one of the strings */
394 394 if (chkauthattr(authstr, pw->pw_name)) {
395 395 ret = 1;
396 396 break;
397 397 }
398 398 }
399 399 }
400 400 endauthattr();
401 401 scf_simple_prop_free(prop);
402 402 } else {
403 403 /* no authorization string defined */
404 404 ret = 0;
405 405 }
406 406 if (handle != NULL)
407 407 scf_handle_destroy(handle);
408 408 return (ret);
409 409 }
410 410
411 411 /*
412 412 * check_authorizations(instname, flags)
413 413 *
414 414 * check all the needed authorizations for the user in this service
415 415 * instance. Return value of 1(true) or 0(false) indicates whether
416 416 * there are authorizations for the user or not.
417 417 */
418 418
419 419 static int
420 420 check_authorizations(char *instname, int flags)
421 421 {
422 422 int ret1 = 0;
423 423 int ret2 = 0;
424 424 int ret;
425 425
426 426 if (flags & SVC_SET)
427 427 ret1 = check_authorization(instname, SVC_SET);
428 428 if (flags & SVC_ACTION)
429 429 ret2 = check_authorization(instname, SVC_ACTION);
430 430 switch (flags) {
431 431 case SVC_ACTION:
432 432 ret = ret2;
433 433 break;
434 434 case SVC_SET:
435 435 ret = ret1;
436 436 break;
437 437 case SVC_ACTION|SVC_SET:
438 438 ret = ret1 & ret2;
439 439 break;
440 440 default:
441 441 /* if not flags set, we assume we don't need authorizations */
442 442 ret = 1;
443 443 }
444 444 return (ret);
445 445 }
446 446
447 447 /*
448 448 * notify_or_enable_share(share, protocol)
449 449 *
450 450 * Since some protocols don't want an "enable" when properties change,
451 451 * this function will use the protocol specific notify function
452 452 * first. If that fails, it will then attempt to use the
453 453 * sa_enable_share(). "protocol" is the protocol that was specified
454 454 * on the command line.
455 455 */
456 456 static void
457 457 notify_or_enable_share(sa_share_t share, char *protocol)
458 458 {
459 459 sa_group_t group;
460 460 sa_optionset_t opt;
461 461 int ret = SA_OK;
462 462 char *path;
463 463 char *groupproto;
464 464 sa_share_t parent = share;
465 465
466 466 /* If really a resource, get parent share */
467 467 if (!sa_is_share(share)) {
468 468 parent = sa_get_resource_parent((sa_resource_t)share);
469 469 }
470 470
471 471 /*
472 472 * Now that we've got a share in "parent", make sure it has a path.
473 473 */
474 474 path = sa_get_share_attr(parent, "path");
475 475 if (path == NULL)
476 476 return;
477 477
478 478 group = sa_get_parent_group(parent);
479 479
480 480 if (group == NULL) {
481 481 sa_free_attr_string(path);
482 482 return;
483 483 }
484 484 for (opt = sa_get_optionset(group, NULL);
485 485 opt != NULL;
486 486 opt = sa_get_next_optionset(opt)) {
487 487 groupproto = sa_get_optionset_attr(opt, "type");
488 488 if (groupproto == NULL ||
489 489 (protocol != NULL && strcmp(groupproto, protocol) != 0)) {
490 490 if (groupproto != NULL)
491 491 sa_free_attr_string(groupproto);
492 492 continue;
493 493 }
494 494 if (sa_is_share(share)) {
495 495 if ((ret = sa_proto_change_notify(share,
496 496 groupproto)) != SA_OK) {
497 497 ret = sa_enable_share(share, groupproto);
498 498 if (ret != SA_OK) {
499 499 (void) printf(
500 500 gettext("Could not reenable"
501 501 " share %s: %s\n"),
502 502 path, sa_errorstr(ret));
503 503 }
504 504 }
505 505 } else {
506 506 /* Must be a resource */
507 507 if ((ret = sa_proto_notify_resource(share,
508 508 groupproto)) != SA_OK) {
509 509 ret = sa_enable_resource(share, groupproto);
510 510 if (ret != SA_OK) {
511 511 (void) printf(
512 512 gettext("Could not "
513 513 "reenable resource %s: "
514 514 "%s\n"), path,
515 515 sa_errorstr(ret));
516 516 }
517 517 }
518 518 }
519 519 sa_free_attr_string(groupproto);
520 520 }
521 521 sa_free_attr_string(path);
522 522 }
523 523
524 524 /*
525 525 * enable_group(group, updateproto, notify, proto)
526 526 *
527 527 * enable all the shares in the specified group. This is a helper for
528 528 * enable_all_groups in order to simplify regular and subgroup (zfs)
529 529 * enabling. Group has already been checked for non-NULL. If notify
530 530 * is non-zero, attempt to use the notify interface rather than
531 531 * enable.
532 532 */
533 533 static void
534 534 enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
535 535 {
536 536 sa_share_t share;
537 537
538 538 /* If the protocol isn't enabled for this group skip it */
539 539 if (!has_protocol(group, proto))
540 540 return;
541 541
542 542 for (share = sa_get_share(group, NULL);
543 543 share != NULL;
544 544 share = sa_get_next_share(share)) {
545 545 if (updateproto != NULL)
546 546 (void) sa_update_legacy(share, updateproto);
547 547 if (notify)
548 548 notify_or_enable_share(share, proto);
549 549 else
550 550 (void) sa_enable_share(share, proto);
551 551 }
552 552 }
553 553
554 554 /*
555 555 * isenabled(group)
556 556 *
557 557 * Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
558 558 * Moved to separate function to reduce clutter in the code.
559 559 */
560 560
561 561 static int
562 562 isenabled(sa_group_t group)
563 563 {
564 564 char *state;
565 565 int ret = B_FALSE;
566 566
567 567 if (group != NULL) {
568 568 state = sa_get_group_attr(group, "state");
569 569 if (state != NULL) {
570 570
571 571 if (strcmp(state, "enabled") == 0)
572 572 ret = B_TRUE;
573 573 sa_free_attr_string(state);
574 574 }
575 575 }
576 576 return (ret);
577 577 }
578 578
579 579 /*
580 580 * enable_all_groups(list, setstate, online, updateproto)
581 581 *
582 582 * Given a list of groups, enable each one found. If updateproto is
583 583 * not NULL, then update all the shares for the protocol that was
584 584 * passed in. If enable is non-zero, tell enable_group to try the
585 585 * notify interface since this is a property change.
586 586 */
587 587 static int
588 588 enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
589 589 int online, char *updateproto, int enable)
590 590 {
591 591 int ret;
592 592 char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
593 593 char *state;
594 594 char *name;
595 595 char *zfs = NULL;
596 596 sa_group_t group;
597 597 sa_group_t subgroup;
598 598
599 599 for (ret = SA_OK; work != NULL; work = work->next) {
600 600 group = (sa_group_t)work->item;
601 601
602 602 /*
603 603 * If setstate == TRUE, then make sure to set
604 604 * enabled. This needs to be done here in order for
605 605 * the isenabled check to succeed on a newly enabled
606 606 * group.
607 607 */
608 608 if (setstate == B_TRUE) {
609 609 ret = sa_set_group_attr(group, "state", "enabled");
610 610 if (ret != SA_OK)
611 611 break;
612 612 }
613 613
614 614 /*
615 615 * Check to see if group is enabled. If it isn't, skip
616 616 * the rest. We don't want shares starting if the
617 617 * group is disabled. The properties may have been
618 618 * updated, but there won't be a change until the
619 619 * group is enabled.
620 620 */
621 621 if (!isenabled(group))
622 622 continue;
623 623
624 624 /* if itemdata != NULL then a single share */
625 625 if (work->itemdata != NULL) {
626 626 if (enable) {
627 627 if (work->itemdata != NULL)
628 628 notify_or_enable_share(work->itemdata,
629 629 updateproto);
630 630 else
631 631 ret = SA_CONFIG_ERR;
632 632 } else {
633 633 if (sa_is_share(work->itemdata)) {
634 634 ret = sa_enable_share(
635 635 (sa_share_t)work->itemdata,
636 636 updateproto);
637 637 } else {
638 638 ret = sa_enable_resource(
639 639 (sa_resource_t)work->itemdata,
640 640 updateproto);
641 641 }
642 642 }
643 643 }
644 644 if (ret != SA_OK)
645 645 break;
646 646
647 647 /* if itemdata == NULL then the whole group */
648 648 if (work->itemdata == NULL) {
649 649 zfs = sa_get_group_attr(group, "zfs");
650 650 /*
651 651 * If the share is managed by ZFS, don't
652 652 * update any of the protocols since ZFS is
653 653 * handling this. Updateproto will contain
654 654 * the name of the protocol that we want to
655 655 * update legacy files for.
656 656 */
657 657 enable_group(group, zfs == NULL ? updateproto : NULL,
658 658 enable, work->proto);
659 659 if (zfs != NULL)
660 660 sa_free_attr_string(zfs);
661 661
662 662 for (subgroup = sa_get_sub_group(group);
663 663 subgroup != NULL;
664 664 subgroup = sa_get_next_group(subgroup)) {
665 665 /* never update legacy for ZFS subgroups */
666 666 enable_group(subgroup, NULL, enable,
667 667 work->proto);
668 668 }
669 669 }
670 670 if (online) {
671 671 zfs = sa_get_group_attr(group, "zfs");
672 672 name = sa_get_group_attr(group, "name");
673 673 if (name != NULL) {
674 674 if (zfs == NULL) {
675 675 (void) snprintf(instance,
676 676 sizeof (instance), "%s:%s",
677 677 SA_SVC_FMRI_BASE, name);
678 678 state = smf_get_state(instance);
679 679 if (state == NULL ||
680 680 strcmp(state, "online") != 0) {
681 681 (void) smf_enable_instance(
682 682 instance, 0);
683 683 free(state);
684 684 }
685 685 } else {
686 686 sa_free_attr_string(zfs);
687 687 zfs = NULL;
688 688 }
689 689 if (name != NULL)
690 690 sa_free_attr_string(name);
691 691 }
692 692 }
693 693 }
694 694 if (ret == SA_OK) {
695 695 ret = sa_update_config(handle);
696 696 }
697 697 return (ret);
698 698 }
699 699
700 700 /*
701 701 * chk_opt(optlistp, security, proto)
702 702 *
703 703 * Do a sanity check on the optlist provided for the protocol. This
704 704 * is a syntax check and verification that the property is either a
705 705 * general or specific to a names optionset.
706 706 */
707 707
708 708 static int
709 709 chk_opt(struct options *optlistp, int security, char *proto)
710 710 {
711 711 struct options *optlist;
712 712 char *sep = "";
713 713 int notfirst = 0;
714 714 int ret;
715 715
716 716 for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
717 717 char *optname;
718 718
719 719 optname = optlist->optname;
720 720 ret = OPT_ADD_OK;
721 721 /* extract property/value pair */
722 722 if (sa_is_security(optname, proto)) {
723 723 if (!security)
724 724 ret = OPT_ADD_SECURITY;
725 725 } else {
726 726 if (security)
727 727 ret = OPT_ADD_PROPERTY;
728 728 }
729 729 if (ret != OPT_ADD_OK) {
730 730 if (notfirst == 0)
731 731 (void) printf(
732 732 gettext("Property syntax error: "));
733 733 switch (ret) {
734 734 case OPT_ADD_SYNTAX:
735 735 (void) printf(gettext("%ssyntax error: %s"),
736 736 sep, optname);
737 737 sep = ", ";
738 738 break;
739 739 case OPT_ADD_SECURITY:
740 740 (void) printf(gettext("%s%s requires -S"),
741 741 optname, sep);
742 742 sep = ", ";
743 743 break;
744 744 case OPT_ADD_PROPERTY:
745 745 (void) printf(
746 746 gettext("%s%s not supported with -S"),
747 747 optname, sep);
748 748 sep = ", ";
749 749 break;
750 750 }
751 751 notfirst++;
752 752 }
753 753 }
754 754 if (notfirst) {
755 755 (void) printf("\n");
756 756 ret = SA_SYNTAX_ERR;
757 757 }
758 758 return (ret);
759 759 }
760 760
761 761 /*
762 762 * free_opt(optlist)
763 763 * Free the specified option list.
764 764 */
765 765 static void
766 766 free_opt(struct options *optlist)
767 767 {
768 768 struct options *nextopt;
769 769 while (optlist != NULL) {
770 770 nextopt = optlist->next;
771 771 free(optlist);
772 772 optlist = nextopt;
773 773 }
774 774 }
775 775
776 776 /*
777 777 * check property list for valid properties
778 778 * A null value is a remove which is always valid.
779 779 */
780 780 static int
781 781 valid_options(sa_handle_t handle, struct options *optlist, char *proto,
782 782 void *object, char *sec)
783 783 {
784 784 int ret = SA_OK;
785 785 struct options *cur;
786 786 sa_property_t prop;
787 787 sa_optionset_t parent = NULL;
788 788
789 789 if (object != NULL) {
790 790 if (sec == NULL)
791 791 parent = sa_get_optionset(object, proto);
792 792 else
793 793 parent = sa_get_security(object, sec, proto);
794 794 }
795 795
796 796 for (cur = optlist; cur != NULL; cur = cur->next) {
797 797 if (cur->optvalue == NULL)
798 798 continue;
799 799 prop = sa_create_property(cur->optname, cur->optvalue);
800 800 if (prop == NULL)
801 801 ret = SA_NO_MEMORY;
802 802 if (ret != SA_OK ||
803 803 (ret = sa_valid_property(handle, parent, proto, prop)) !=
804 804 SA_OK) {
805 805 (void) printf(
806 806 gettext("Could not add property %s: %s\n"),
807 807 cur->optname, sa_errorstr(ret));
808 808 }
809 809 (void) sa_remove_property(prop);
810 810 }
811 811 return (ret);
812 812 }
813 813
814 814 /*
815 815 * add_optionset(group, optlist, protocol, *err)
816 816 * Add the options in optlist to an optionset and then add the optionset
817 817 * to the group.
818 818 *
819 819 * The return value indicates if there was a "change" while errors are
820 820 * returned via the *err parameters.
821 821 */
822 822 static int
823 823 add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
824 824 {
825 825 sa_optionset_t optionset;
826 826 int ret = SA_OK;
827 827 int result = B_FALSE;
828 828 sa_handle_t handle;
829 829
830 830 optionset = sa_get_optionset(group, proto);
831 831 if (optionset == NULL) {
832 832 optionset = sa_create_optionset(group, proto);
833 833 if (optionset == NULL)
834 834 ret = SA_NO_MEMORY;
835 835 result = B_TRUE; /* adding a protocol is a change */
836 836 }
837 837 if (optionset == NULL) {
838 838 ret = SA_NO_MEMORY;
839 839 goto out;
840 840 }
841 841 handle = sa_find_group_handle(group);
842 842 if (handle == NULL) {
843 843 ret = SA_CONFIG_ERR;
844 844 goto out;
845 845 }
846 846 while (optlist != NULL) {
847 847 sa_property_t prop;
848 848 prop = sa_get_property(optionset, optlist->optname);
849 849 if (prop == NULL) {
850 850 /*
851 851 * add the property, but only if it is
852 852 * a non-NULL or non-zero length value
853 853 */
854 854 if (optlist->optvalue != NULL) {
855 855 prop = sa_create_property(optlist->optname,
856 856 optlist->optvalue);
857 857 if (prop != NULL) {
858 858 ret = sa_valid_property(handle,
859 859 optionset, proto, prop);
860 860 if (ret != SA_OK) {
861 861 (void) sa_remove_property(prop);
862 862 (void) printf(gettext("Could "
863 863 "not add property "
864 864 "%s: %s\n"),
865 865 optlist->optname,
866 866 sa_errorstr(ret));
867 867 }
868 868 }
869 869 if (ret == SA_OK) {
870 870 ret = sa_add_property(optionset, prop);
871 871 if (ret != SA_OK) {
872 872 (void) printf(gettext(
873 873 "Could not add property "
874 874 "%s: %s\n"),
875 875 optlist->optname,
876 876 sa_errorstr(ret));
877 877 } else {
878 878 /* there was a change */
879 879 result = B_TRUE;
880 880 }
881 881 }
882 882 }
883 883 } else {
884 884 ret = sa_update_property(prop, optlist->optvalue);
885 885 /* should check to see if value changed */
886 886 if (ret != SA_OK) {
887 887 (void) printf(gettext("Could not update "
888 888 "property %s: %s\n"), optlist->optname,
889 889 sa_errorstr(ret));
890 890 } else {
891 891 result = B_TRUE;
892 892 }
893 893 }
894 894 optlist = optlist->next;
895 895 }
896 896 ret = sa_commit_properties(optionset, 0);
897 897
898 898 out:
899 899 if (err != NULL)
900 900 *err = ret;
901 901 return (result);
902 902 }
903 903
904 904 /*
905 905 * resource_compliant(group)
906 906 *
907 907 * Go through all the shares in the group. Assume compliant, but if
908 908 * any share doesn't have at least one resource name, it isn't
909 909 * compliant.
910 910 */
911 911 static int
912 912 resource_compliant(sa_group_t group)
913 913 {
914 914 sa_share_t share;
915 915
916 916 for (share = sa_get_share(group, NULL); share != NULL;
917 917 share = sa_get_next_share(share)) {
918 918 if (sa_get_share_resource(share, NULL) == NULL) {
919 919 return (B_FALSE);
920 920 }
921 921 }
922 922 return (B_TRUE);
923 923 }
924 924
925 925 /*
926 926 * fix_path(path)
927 927 *
928 928 * change all illegal characters to something else. For now, all get
929 929 * converted to '_' and the leading '/' is stripped off. This is used
930 930 * to construct an resource name (SMB share name) that is valid.
931 931 * Caller must pass a valid path.
932 932 */
933 933 static void
934 934 fix_path(char *path)
935 935 {
936 936 char *cp;
937 937 size_t len;
938 938
939 939 assert(path != NULL);
940 940
941 941 /* make sure we are appropriate length */
942 942 cp = path + 1; /* skip leading slash */
943 943 while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
944 944 cp = strchr(cp, '/');
945 945 if (cp != NULL)
946 946 cp++;
947 947 }
948 948 /* two cases - cp == NULL and cp is substring of path */
949 949 if (cp == NULL) {
950 950 /* just take last SA_MAX_RESOURCE_NAME chars */
951 951 len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
952 952 (void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
953 953 path[SA_MAX_RESOURCE_NAME] = '\0';
954 954 } else {
955 955 len = strlen(cp) + 1;
956 956 (void) memmove(path, cp, len);
957 957 }
958 958
959 959 /*
960 960 * Don't want any of the characters that are not allowed
961 961 * in and SMB share name. Replace them with '_'.
962 962 */
963 963 while (*path) {
964 964 switch (*path) {
965 965 case '/':
966 966 case '"':
967 967 case '\\':
968 968 case '[':
969 969 case ']':
970 970 case ':':
971 971 case '|':
972 972 case '<':
973 973 case '>':
974 974 case '+':
975 975 case ';':
976 976 case ',':
977 977 case '?':
978 978 case '*':
979 979 case '=':
980 980 case '\t':
981 981 *path = '_';
982 982 break;
983 983 }
984 984 path++;
985 985 }
986 986 }
987 987
988 988 /*
989 989 * name_adjust(path, count)
990 990 *
991 991 * Add a ~<count> in place of last few characters. The total number of
992 992 * characters is dependent on count.
993 993 */
994 994 #define MAX_MANGLE_NUMBER 10000
995 995
996 996 static int
997 997 name_adjust(char *path, int count)
998 998 {
999 999 size_t len;
1000 1000
1001 1001 len = strlen(path) - 2;
1002 1002 if (count > 10)
1003 1003 len--;
1004 1004 if (count > 100)
1005 1005 len--;
1006 1006 if (count > 1000)
1007 1007 len--;
1008 1008 if (len > 0)
1009 1009 (void) sprintf(path + len, "~%d", count);
1010 1010 else
1011 1011 return (SA_BAD_VALUE);
1012 1012
1013 1013 return (SA_OK);
1014 1014 }
1015 1015
1016 1016 /*
1017 1017 * make_resources(group)
1018 1018 *
1019 1019 * Go through all the shares in the group and make them have resource
1020 1020 * names.
1021 1021 */
1022 1022 static void
1023 1023 make_resources(sa_group_t group)
1024 1024 {
1025 1025 sa_share_t share;
1026 1026 int count;
1027 1027 int err = SA_OK;
1028 1028
1029 1029 for (share = sa_get_share(group, NULL); share != NULL;
1030 1030 share = sa_get_next_share(share)) {
1031 1031 /* Skip those with resources */
1032 1032 if (sa_get_share_resource(share, NULL) == NULL) {
1033 1033 char *path;
1034 1034 path = sa_get_share_attr(share, "path");
1035 1035 if (path == NULL)
1036 1036 continue;
1037 1037 fix_path(path);
1038 1038 count = 0; /* reset for next resource */
1039 1039 while (sa_add_resource(share, path,
1040 1040 SA_SHARE_PERMANENT, &err) == NULL &&
1041 1041 err == SA_DUPLICATE_NAME) {
1042 1042 int ret;
1043 1043 ret = name_adjust(path, count);
1044 1044 count++;
1045 1045 if (ret != SA_OK ||
1046 1046 count >= MAX_MANGLE_NUMBER) {
1047 1047 (void) printf(gettext(
1048 1048 "Cannot create resource name for"
1049 1049 " path: %s\n"), path);
1050 1050 break;
1051 1051 }
1052 1052 }
1053 1053 sa_free_attr_string(path);
1054 1054 }
1055 1055 }
1056 1056 }
1057 1057
1058 1058 /*
1059 1059 * check_valid_group(group, protocol)
1060 1060 *
1061 1061 * Check to see that the group should have the protocol added (if
1062 1062 * there is one specified).
1063 1063 */
1064 1064
1065 1065 static int
1066 1066 check_valid_group(sa_group_t group, char *groupname, char *protocol)
1067 1067 {
1068 1068
1069 1069 if (protocol != NULL) {
1070 1070 if (has_protocol(group, protocol)) {
1071 1071 (void) printf(gettext(
1072 1072 "Group \"%s\" already exists"
1073 1073 " with protocol %s\n"), groupname,
1074 1074 protocol);
1075 1075 return (SA_DUPLICATE_NAME);
1076 1076 } else if (strcmp(groupname, "default") == 0 &&
1077 1077 strcmp(protocol, "nfs") != 0) {
1078 1078 (void) printf(gettext(
1079 1079 "Group \"%s\" only allows protocol "
1080 1080 "\"%s\"\n"), groupname, "nfs");
1081 1081 return (SA_INVALID_PROTOCOL);
1082 1082 }
1083 1083 } else {
1084 1084 /* must add new protocol */
1085 1085 (void) printf(gettext(
1086 1086 "Group already exists and no protocol "
1087 1087 "specified.\n"));
1088 1088 return (SA_DUPLICATE_NAME);
1089 1089 }
1090 1090 return (SA_OK);
1091 1091 }
1092 1092
1093 1093 /*
1094 1094 * enforce_featureset(group, protocol, dryrun, force)
1095 1095 *
1096 1096 * Check the protocol featureset against the group and enforce any
1097 1097 * rules that might be imposed.
1098 1098 */
1099 1099
1100 1100 static int
1101 1101 enforce_featureset(sa_group_t group, char *protocol, boolean_t dryrun,
1102 1102 boolean_t force)
1103 1103 {
1104 1104 uint64_t features;
1105 1105
1106 1106 if (protocol == NULL)
1107 1107 return (SA_OK);
1108 1108
1109 1109 /*
1110 1110 * First check to see if specified protocol is one we want to
1111 1111 * allow on a group. Only server protocols are allowed here.
1112 1112 */
1113 1113 features = sa_proto_get_featureset(protocol);
1114 1114 if (!(features & SA_FEATURE_SERVER)) {
1115 1115 (void) printf(
1116 1116 gettext("Protocol \"%s\" not supported.\n"), protocol);
1117 1117 return (SA_INVALID_PROTOCOL);
1118 1118 }
1119 1119
1120 1120 /*
1121 1121 * Check to see if the new protocol is one that requires
1122 1122 * resource names and make sure we are compliant before
1123 1123 * proceeding.
1124 1124 */
1125 1125 if ((features & SA_FEATURE_RESOURCE) &&
1126 1126 !resource_compliant(group)) {
1127 1127 if (force && !dryrun) {
1128 1128 make_resources(group);
1129 1129 } else {
1130 1130 (void) printf(
1131 1131 gettext("Protocol requires resource names to be "
1132 1132 "set: %s\n"), protocol);
1133 1133 return (SA_RESOURCE_REQUIRED);
1134 1134 }
1135 1135 }
1136 1136 return (SA_OK);
1137 1137 }
1138 1138
1139 1139 /*
1140 1140 * set_all_protocols(group)
1141 1141 *
1142 1142 * Get the list of all protocols and add all server protocols to the
1143 1143 * group.
1144 1144 */
1145 1145
1146 1146 static int
1147 1147 set_all_protocols(sa_group_t group)
1148 1148 {
1149 1149 char **protolist;
1150 1150 int numprotos, i;
1151 1151 uint64_t features;
1152 1152 sa_optionset_t optionset;
1153 1153 int ret = SA_OK;
1154 1154
1155 1155 /*
1156 1156 * Now make sure we really want to put this protocol on a
1157 1157 * group. Only server protocols can go here.
1158 1158 */
1159 1159 numprotos = sa_get_protocols(&protolist);
1160 1160 for (i = 0; i < numprotos; i++) {
1161 1161 features = sa_proto_get_featureset(protolist[i]);
1162 1162 if (features & SA_FEATURE_SERVER) {
1163 1163 optionset = sa_create_optionset(group, protolist[i]);
1164 1164 if (optionset == NULL) {
1165 1165 ret = SA_NO_MEMORY;
1166 1166 break;
1167 1167 }
1168 1168 }
1169 1169 }
1170 1170
1171 1171 if (protolist != NULL)
1172 1172 free(protolist);
1173 1173
1174 1174 return (ret);
1175 1175 }
1176 1176
1177 1177 /*
1178 1178 * sa_create(flags, argc, argv)
1179 1179 * create a new group
1180 1180 * this may or may not have a protocol associated with it.
1181 1181 * No protocol means "all" protocols in this case.
1182 1182 */
1183 1183 static int
1184 1184 sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
1185 1185 {
1186 1186 char *groupname;
1187 1187
1188 1188 sa_group_t group;
1189 1189 boolean_t force = B_FALSE;
1190 1190 boolean_t verbose = B_FALSE;
1191 1191 boolean_t dryrun = B_FALSE;
1192 1192 int c;
1193 1193 char *protocol = NULL;
1194 1194 int ret = SA_OK;
1195 1195 struct options *optlist = NULL;
1196 1196 int err = SA_OK;
1197 1197 int auth;
1198 1198 boolean_t created = B_FALSE;
1199 1199
1200 1200 while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
1201 1201 switch (c) {
1202 1202 case 'f':
1203 1203 force = B_TRUE;
1204 1204 break;
1205 1205 case 'v':
1206 1206 verbose = B_TRUE;
1207 1207 break;
1208 1208 case 'n':
1209 1209 dryrun = B_TRUE;
1210 1210 break;
1211 1211 case 'P':
1212 1212 if (protocol != NULL) {
1213 1213 (void) printf(gettext("Specifying "
1214 1214 "multiple protocols "
1215 1215 "not supported: %s\n"), protocol);
1216 1216 return (SA_SYNTAX_ERR);
1217 1217 }
1218 1218 protocol = optarg;
1219 1219 if (sa_valid_protocol(protocol))
1220 1220 break;
1221 1221 (void) printf(gettext(
1222 1222 "Invalid protocol specified: %s\n"), protocol);
1223 1223 return (SA_INVALID_PROTOCOL);
1224 1224 case 'p':
1225 1225 ret = add_opt(&optlist, optarg, 0);
1226 1226 switch (ret) {
1227 1227 case OPT_ADD_SYNTAX:
1228 1228 (void) printf(gettext(
1229 1229 "Property syntax error for property: %s\n"),
1230 1230 optarg);
1231 1231 return (SA_SYNTAX_ERR);
1232 1232 case OPT_ADD_SECURITY:
1233 1233 (void) printf(gettext(
1234 1234 "Security properties need "
1235 1235 "to be set with set-security: %s\n"),
1236 1236 optarg);
1237 1237 return (SA_SYNTAX_ERR);
1238 1238 default:
1239 1239 break;
1240 1240 }
1241 1241 break;
1242 1242 case 'h':
1243 1243 /* optopt on valid arg isn't defined */
1244 1244 optopt = c;
1245 1245 /*FALLTHROUGH*/
1246 1246 case '?':
1247 1247 default:
1248 1248 /*
1249 1249 * Since a bad option gets to here, sort it
1250 1250 * out and return a syntax error return value
1251 1251 * if necessary.
1252 1252 */
1253 1253 switch (optopt) {
1254 1254 default:
1255 1255 err = SA_SYNTAX_ERR;
1256 1256 break;
1257 1257 case 'h':
1258 1258 case '?':
1259 1259 break;
1260 1260 }
1261 1261 (void) printf(gettext("usage: %s\n"),
1262 1262 sa_get_usage(USAGE_CREATE));
1263 1263 return (err);
1264 1264 }
1265 1265 }
1266 1266
1267 1267 if (optind >= argc) {
1268 1268 (void) printf(gettext("usage: %s\n"),
1269 1269 sa_get_usage(USAGE_CREATE));
1270 1270 (void) printf(gettext("\tgroup must be specified.\n"));
1271 1271 return (SA_BAD_PATH);
1272 1272 }
1273 1273
1274 1274 if ((optind + 1) < argc) {
1275 1275 (void) printf(gettext("usage: %s\n"),
1276 1276 sa_get_usage(USAGE_CREATE));
1277 1277 (void) printf(gettext("\textraneous group(s) at end\n"));
1278 1278 return (SA_SYNTAX_ERR);
1279 1279 }
1280 1280
1281 1281 if (protocol == NULL && optlist != NULL) {
1282 1282 /* lookup default protocol */
1283 1283 (void) printf(gettext("usage: %s\n"),
1284 1284 sa_get_usage(USAGE_CREATE));
1285 1285 (void) printf(gettext("\tprotocol must be specified "
1286 1286 "with properties\n"));
1287 1287 return (SA_INVALID_PROTOCOL);
1288 1288 }
1289 1289
1290 1290 if (optlist != NULL)
1291 1291 ret = chk_opt(optlist, 0, protocol);
1292 1292 if (ret == OPT_ADD_SECURITY) {
1293 1293 (void) printf(gettext("Security properties not "
1294 1294 "supported with create\n"));
1295 1295 return (SA_SYNTAX_ERR);
1296 1296 }
1297 1297
1298 1298 /*
1299 1299 * If a group already exists, we can only add a new protocol
1300 1300 * to it and not create a new one or add the same protocol
1301 1301 * again.
1302 1302 */
1303 1303
1304 1304 groupname = argv[optind];
1305 1305
1306 1306 auth = check_authorizations(groupname, flags);
1307 1307
1308 1308 group = sa_get_group(handle, groupname);
1309 1309 if (group != NULL) {
1310 1310 /* group exists so must be a protocol add */
1311 1311 ret = check_valid_group(group, groupname, protocol);
1312 1312 } else {
1313 1313 /*
1314 1314 * is it a valid name? Must comply with SMF instance
1315 1315 * name restrictions.
1316 1316 */
1317 1317 if (!sa_valid_group_name(groupname)) {
1318 1318 ret = SA_INVALID_NAME;
1319 1319 (void) printf(gettext("Invalid group name: %s\n"),
1320 1320 groupname);
1321 1321 }
1322 1322 }
1323 1323 if (ret == SA_OK) {
1324 1324 /* check protocol vs optlist */
1325 1325 if (optlist != NULL) {
1326 1326 /* check options, if any, for validity */
1327 1327 ret = valid_options(handle, optlist, protocol,
1328 1328 group, NULL);
1329 1329 }
1330 1330 }
1331 1331 if (ret == SA_OK && !dryrun) {
1332 1332 if (group == NULL) {
1333 1333 group = sa_create_group(handle, (char *)groupname,
1334 1334 &err);
1335 1335 created = B_TRUE;
1336 1336 }
1337 1337 if (group != NULL) {
1338 1338 sa_optionset_t optionset;
1339 1339
1340 1340 /*
1341 1341 * Check group and protocol against featureset
1342 1342 * requirements.
1343 1343 */
1344 1344 ret = enforce_featureset(group, protocol,
1345 1345 dryrun, force);
1346 1346 if (ret != SA_OK)
1347 1347 goto err;
1348 1348
1349 1349 /*
1350 1350 * So far so good. Now add the required
1351 1351 * optionset(s) to the group.
1352 1352 */
1353 1353 if (optlist != NULL) {
1354 1354 (void) add_optionset(group, optlist, protocol,
1355 1355 &ret);
1356 1356 } else if (protocol != NULL) {
1357 1357 optionset = sa_create_optionset(group,
1358 1358 protocol);
1359 1359 if (optionset == NULL)
1360 1360 ret = SA_NO_MEMORY;
1361 1361 } else if (protocol == NULL) {
1362 1362 /* default group create so add all protocols */
1363 1363 ret = set_all_protocols(group);
1364 1364 }
1365 1365 /*
1366 1366 * We have a group and legal additions
1367 1367 */
1368 1368 if (ret == SA_OK) {
1369 1369 /*
1370 1370 * Commit to configuration for protocols that
1371 1371 * need to do block updates. For NFS, this
1372 1372 * doesn't do anything but it will be run for
1373 1373 * all protocols that implement the
1374 1374 * appropriate plugin.
1375 1375 */
1376 1376 ret = sa_update_config(handle);
1377 1377 } else {
1378 1378 if (group != NULL)
1379 1379 (void) sa_remove_group(group);
1380 1380 }
1381 1381 } else {
1382 1382 ret = err;
1383 1383 (void) printf(gettext("Could not create group: %s\n"),
1384 1384 sa_errorstr(ret));
1385 1385 }
1386 1386 }
1387 1387 if (dryrun && ret == SA_OK && !auth && verbose) {
1388 1388 (void) printf(gettext("Command would fail: %s\n"),
1389 1389 sa_errorstr(SA_NO_PERMISSION));
1390 1390 ret = SA_NO_PERMISSION;
1391 1391 }
1392 1392 err:
1393 1393 if (ret != SA_OK && created)
1394 1394 ret = sa_remove_group(group);
1395 1395
1396 1396 free_opt(optlist);
1397 1397 return (ret);
1398 1398 }
1399 1399
1400 1400 /*
1401 1401 * group_status(group)
1402 1402 *
1403 1403 * return the current status (enabled/disabled) of the group.
1404 1404 */
1405 1405
1406 1406 static char *
1407 1407 group_status(sa_group_t group)
1408 1408 {
1409 1409 char *state;
1410 1410 int enabled = 0;
1411 1411
1412 1412 state = sa_get_group_attr(group, "state");
1413 1413 if (state != NULL) {
1414 1414 if (strcmp(state, "enabled") == 0) {
1415 1415 enabled = 1;
1416 1416 }
1417 1417 sa_free_attr_string(state);
1418 1418 }
1419 1419 return (enabled ? "enabled" : "disabled");
1420 1420 }
1421 1421
1422 1422 /*
1423 1423 * sa_delete(flags, argc, argv)
1424 1424 *
1425 1425 * Delete a group.
1426 1426 */
1427 1427
1428 1428 static int
1429 1429 sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
1430 1430 {
1431 1431 char *groupname;
1432 1432 sa_group_t group;
1433 1433 sa_share_t share;
1434 1434 int verbose = 0;
1435 1435 int dryrun = 0;
1436 1436 int force = 0;
1437 1437 int c;
1438 1438 char *protocol = NULL;
1439 1439 char *sectype = NULL;
1440 1440 int ret = SA_OK;
1441 1441 int auth;
1442 1442
1443 1443 while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
1444 1444 switch (c) {
1445 1445 case 'v':
1446 1446 verbose++;
1447 1447 break;
1448 1448 case 'n':
1449 1449 dryrun++;
1450 1450 break;
1451 1451 case 'P':
1452 1452 if (protocol != NULL) {
1453 1453 (void) printf(gettext("Specifying "
1454 1454 "multiple protocols "
1455 1455 "not supported: %s\n"), protocol);
1456 1456 return (SA_SYNTAX_ERR);
1457 1457 }
1458 1458 protocol = optarg;
1459 1459 if (!sa_valid_protocol(protocol)) {
1460 1460 (void) printf(gettext("Invalid protocol "
1461 1461 "specified: %s\n"), protocol);
1462 1462 return (SA_INVALID_PROTOCOL);
1463 1463 }
1464 1464 break;
1465 1465 case 'S':
1466 1466 if (sectype != NULL) {
1467 1467 (void) printf(gettext("Specifying "
1468 1468 "multiple property "
1469 1469 "spaces not supported: %s\n"), sectype);
1470 1470 return (SA_SYNTAX_ERR);
1471 1471 }
1472 1472 sectype = optarg;
1473 1473 break;
1474 1474 case 'f':
1475 1475 force++;
1476 1476 break;
1477 1477 case 'h':
1478 1478 /* optopt on valid arg isn't defined */
1479 1479 optopt = c;
1480 1480 /*FALLTHROUGH*/
1481 1481 case '?':
1482 1482 default:
1483 1483 /*
1484 1484 * Since a bad option gets to here, sort it
1485 1485 * out and return a syntax error return value
1486 1486 * if necessary.
1487 1487 */
1488 1488 switch (optopt) {
1489 1489 default:
1490 1490 ret = SA_SYNTAX_ERR;
1491 1491 break;
1492 1492 case 'h':
1493 1493 case '?':
1494 1494 break;
1495 1495 }
1496 1496 (void) printf(gettext("usage: %s\n"),
1497 1497 sa_get_usage(USAGE_DELETE));
1498 1498 return (ret);
1499 1499 }
1500 1500 }
1501 1501
1502 1502 if (optind >= argc) {
1503 1503 (void) printf(gettext("usage: %s\n"),
1504 1504 sa_get_usage(USAGE_DELETE));
1505 1505 (void) printf(gettext("\tgroup must be specified.\n"));
1506 1506 return (SA_SYNTAX_ERR);
1507 1507 }
1508 1508
1509 1509 if ((optind + 1) < argc) {
1510 1510 (void) printf(gettext("usage: %s\n"),
1511 1511 sa_get_usage(USAGE_DELETE));
1512 1512 (void) printf(gettext("\textraneous group(s) at end\n"));
1513 1513 return (SA_SYNTAX_ERR);
1514 1514 }
1515 1515
1516 1516 if (sectype != NULL && protocol == NULL) {
1517 1517 (void) printf(gettext("usage: %s\n"),
1518 1518 sa_get_usage(USAGE_DELETE));
1519 1519 (void) printf(gettext("\tsecurity requires protocol to be "
1520 1520 "specified.\n"));
1521 1521 return (SA_SYNTAX_ERR);
1522 1522 }
1523 1523
1524 1524 /*
1525 1525 * Determine if the group already exists since it must in
1526 1526 * order to be removed.
1527 1527 *
1528 1528 * We can delete when:
1529 1529 *
1530 1530 * - group is empty
1531 1531 * - force flag is set
1532 1532 * - if protocol specified, only delete the protocol
1533 1533 */
1534 1534
1535 1535 groupname = argv[optind];
1536 1536 group = sa_get_group(handle, groupname);
1537 1537 if (group == NULL) {
1538 1538 ret = SA_NO_SUCH_GROUP;
1539 1539 goto done;
1540 1540 }
1541 1541 auth = check_authorizations(groupname, flags);
1542 1542 if (protocol == NULL) {
1543 1543 share = sa_get_share(group, NULL);
1544 1544 if (share != NULL)
1545 1545 ret = SA_BUSY;
1546 1546 if (share == NULL || (share != NULL && force == 1)) {
1547 1547 ret = SA_OK;
1548 1548 if (!dryrun) {
1549 1549 while (share != NULL) {
1550 1550 sa_share_t next_share;
1551 1551 next_share = sa_get_next_share(share);
1552 1552 /*
1553 1553 * need to do the disable of
1554 1554 * each share, but don't
1555 1555 * actually do anything on a
1556 1556 * dryrun.
1557 1557 */
1558 1558 ret = sa_disable_share(share, NULL);
1559 1559 ret = sa_remove_share(share);
1560 1560 share = next_share;
1561 1561 }
1562 1562 ret = sa_remove_group(group);
1563 1563 }
1564 1564 }
1565 1565 /* Commit to configuration if not a dryrun */
1566 1566 if (!dryrun && ret == SA_OK) {
1567 1567 ret = sa_update_config(handle);
1568 1568 }
1569 1569 } else {
1570 1570 /* a protocol delete */
1571 1571 sa_optionset_t optionset;
1572 1572 sa_security_t security;
1573 1573 if (sectype != NULL) {
1574 1574 /* only delete specified security */
1575 1575 security = sa_get_security(group, sectype, protocol);
1576 1576 if (security != NULL && !dryrun)
1577 1577 ret = sa_destroy_security(security);
1578 1578 else
1579 1579 ret = SA_INVALID_PROTOCOL;
1580 1580 } else {
1581 1581 optionset = sa_get_optionset(group, protocol);
1582 1582 if (optionset != NULL && !dryrun) {
1583 1583 /*
1584 1584 * have an optionset with
1585 1585 * protocol to delete
1586 1586 */
1587 1587 ret = sa_destroy_optionset(optionset);
1588 1588 /*
1589 1589 * Now find all security sets
1590 1590 * for the protocol and remove
1591 1591 * them. Don't remove other
1592 1592 * protocols.
1593 1593 */
1594 1594 for (security =
1595 1595 sa_get_security(group, NULL, NULL);
1596 1596 ret == SA_OK && security != NULL;
1597 1597 security = sa_get_next_security(security)) {
1598 1598 char *secprot;
1599 1599 secprot = sa_get_security_attr(security,
1600 1600 "type");
1601 1601 if (secprot != NULL &&
1602 1602 strcmp(secprot, protocol) == 0)
1603 1603 ret = sa_destroy_security(
1604 1604 security);
1605 1605 if (secprot != NULL)
1606 1606 sa_free_attr_string(secprot);
1607 1607 }
1608 1608 } else {
1609 1609 if (!dryrun)
1610 1610 ret = SA_INVALID_PROTOCOL;
1611 1611 }
1612 1612 }
1613 1613 /*
1614 1614 * With the protocol items removed, make sure that all
1615 1615 * the shares are updated in the legacy files, if
1616 1616 * necessary.
1617 1617 */
1618 1618 for (share = sa_get_share(group, NULL);
1619 1619 share != NULL;
1620 1620 share = sa_get_next_share(share)) {
1621 1621 (void) sa_delete_legacy(share, protocol);
1622 1622 }
1623 1623 }
1624 1624
1625 1625 done:
1626 1626 if (ret != SA_OK) {
1627 1627 (void) printf(gettext("Could not delete group: %s\n"),
1628 1628 sa_errorstr(ret));
1629 1629 } else if (dryrun && !auth && verbose) {
1630 1630 (void) printf(gettext("Command would fail: %s\n"),
1631 1631 sa_errorstr(SA_NO_PERMISSION));
1632 1632 }
1633 1633 return (ret);
1634 1634 }
1635 1635
1636 1636 /*
1637 1637 * strndupr(*buff, str, buffsize)
1638 1638 *
1639 1639 * used with small strings to duplicate and possibly increase the
1640 1640 * buffer size of a string.
1641 1641 */
1642 1642 static char *
1643 1643 strndupr(char *buff, char *str, int *buffsize)
1644 1644 {
1645 1645 int limit;
1646 1646 char *orig_buff = buff;
1647 1647
1648 1648 if (buff == NULL) {
1649 1649 buff = (char *)malloc(64);
1650 1650 if (buff == NULL)
1651 1651 return (NULL);
1652 1652 *buffsize = 64;
1653 1653 buff[0] = '\0';
1654 1654 }
1655 1655 limit = strlen(buff) + strlen(str) + 1;
1656 1656 if (limit > *buffsize) {
1657 1657 limit = *buffsize = *buffsize + ((limit / 64) + 64);
1658 1658 buff = realloc(buff, limit);
1659 1659 }
1660 1660 if (buff != NULL) {
1661 1661 (void) strcat(buff, str);
1662 1662 } else {
1663 1663 /* if it fails, fail it hard */
1664 1664 if (orig_buff != NULL)
1665 1665 free(orig_buff);
1666 1666 }
1667 1667 return (buff);
1668 1668 }
1669 1669
1670 1670 /*
1671 1671 * group_proto(group)
1672 1672 *
1673 1673 * return a string of all the protocols (space separated) associated
1674 1674 * with this group.
1675 1675 */
1676 1676
1677 1677 static char *
1678 1678 group_proto(sa_group_t group)
1679 1679 {
1680 1680 sa_optionset_t optionset;
1681 1681 char *proto;
1682 1682 char *buff = NULL;
1683 1683 int buffsize = 0;
1684 1684 int addspace = 0;
1685 1685 /*
1686 1686 * get the protocol list by finding the optionsets on this
1687 1687 * group and extracting the type value. The initial call to
1688 1688 * strndupr() initailizes buff.
1689 1689 */
1690 1690 buff = strndupr(buff, "", &buffsize);
1691 1691 if (buff != NULL) {
1692 1692 for (optionset = sa_get_optionset(group, NULL);
1693 1693 optionset != NULL && buff != NULL;
1694 1694 optionset = sa_get_next_optionset(optionset)) {
1695 1695 /*
1696 1696 * extract out the protocol type from this optionset
1697 1697 * and append it to the buffer "buff". strndupr() will
1698 1698 * reallocate space as necessay.
1699 1699 */
1700 1700 proto = sa_get_optionset_attr(optionset, "type");
1701 1701 if (proto != NULL) {
1702 1702 if (addspace++)
1703 1703 buff = strndupr(buff, " ", &buffsize);
1704 1704 buff = strndupr(buff, proto, &buffsize);
1705 1705 sa_free_attr_string(proto);
1706 1706 }
1707 1707 }
1708 1708 }
1709 1709 return (buff);
1710 1710 }
1711 1711
1712 1712 /*
1713 1713 * sa_list(flags, argc, argv)
1714 1714 *
1715 1715 * implements the "list" subcommand to list groups and optionally
1716 1716 * their state and protocols.
1717 1717 */
1718 1718
1719 1719 static int
1720 1720 sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
1721 1721 {
1722 1722 sa_group_t group;
1723 1723 int verbose = 0;
1724 1724 int c;
1725 1725 char *protocol = NULL;
1726 1726 int ret = SA_OK;
1727 1727 #ifdef lint
1728 1728 flags = flags;
1729 1729 #endif
1730 1730
1731 1731 while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
1732 1732 switch (c) {
1733 1733 case 'v':
1734 1734 verbose++;
1735 1735 break;
1736 1736 case 'P':
1737 1737 if (protocol != NULL) {
1738 1738 (void) printf(gettext(
1739 1739 "Specifying multiple protocols "
1740 1740 "not supported: %s\n"),
1741 1741 protocol);
1742 1742 return (SA_SYNTAX_ERR);
1743 1743 }
1744 1744 protocol = optarg;
1745 1745 if (!sa_valid_protocol(protocol)) {
1746 1746 (void) printf(gettext(
1747 1747 "Invalid protocol specified: %s\n"),
1748 1748 protocol);
1749 1749 return (SA_INVALID_PROTOCOL);
1750 1750 }
1751 1751 break;
1752 1752 case 'h':
1753 1753 /* optopt on valid arg isn't defined */
1754 1754 optopt = c;
1755 1755 /*FALLTHROUGH*/
1756 1756 case '?':
1757 1757 default:
1758 1758 /*
1759 1759 * Since a bad option gets to here, sort it
1760 1760 * out and return a syntax error return value
1761 1761 * if necessary.
1762 1762 */
1763 1763 switch (optopt) {
1764 1764 default:
1765 1765 ret = SA_SYNTAX_ERR;
1766 1766 break;
1767 1767 case 'h':
1768 1768 case '?':
1769 1769 break;
1770 1770 }
1771 1771 (void) printf(gettext("usage: %s\n"),
1772 1772 sa_get_usage(USAGE_LIST));
1773 1773 return (ret);
1774 1774 }
1775 1775 }
1776 1776
1777 1777 if (optind != argc) {
1778 1778 (void) printf(gettext("usage: %s\n"),
1779 1779 sa_get_usage(USAGE_LIST));
1780 1780 return (SA_SYNTAX_ERR);
1781 1781 }
1782 1782
1783 1783 for (group = sa_get_group(handle, NULL);
1784 1784 group != NULL;
1785 1785 group = sa_get_next_group(group)) {
1786 1786 char *name;
1787 1787 char *proto;
1788 1788 if (protocol == NULL || has_protocol(group, protocol)) {
1789 1789 name = sa_get_group_attr(group, "name");
1790 1790 if (name != NULL && (verbose > 1 || name[0] != '#')) {
1791 1791 (void) printf("%s", (char *)name);
1792 1792 if (verbose) {
1793 1793 /*
1794 1794 * Need the list of protocols
1795 1795 * and current status once
1796 1796 * available. We do want to
1797 1797 * translate the
1798 1798 * enabled/disabled text here.
1799 1799 */
1800 1800 (void) printf("\t%s", isenabled(group) ?
1801 1801 gettext("enabled") :
1802 1802 gettext("disabled"));
1803 1803 proto = group_proto(group);
1804 1804 if (proto != NULL) {
1805 1805 (void) printf("\t%s",
1806 1806 (char *)proto);
1807 1807 free(proto);
1808 1808 }
1809 1809 }
1810 1810 (void) printf("\n");
1811 1811 }
1812 1812 if (name != NULL)
1813 1813 sa_free_attr_string(name);
1814 1814 }
1815 1815 }
1816 1816 return (0);
1817 1817 }
1818 1818
1819 1819 /*
1820 1820 * out_properties(optionset, proto, sec)
1821 1821 *
1822 1822 * Format the properties and encode the protocol and optional named
1823 1823 * optionset into the string.
1824 1824 *
1825 1825 * format is protocol[:name]=(property-list)
1826 1826 */
1827 1827
1828 1828 static void
1829 1829 out_properties(sa_optionset_t optionset, char *proto, char *sec)
1830 1830 {
1831 1831 char *type;
1832 1832 char *value;
1833 1833 int spacer;
1834 1834 sa_property_t prop;
1835 1835
1836 1836 if (sec == NULL)
1837 1837 (void) printf(" %s=(", proto ? proto : gettext("all"));
1838 1838 else
1839 1839 (void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
1840 1840
1841 1841 for (spacer = 0, prop = sa_get_property(optionset, NULL);
1842 1842 prop != NULL;
1843 1843 prop = sa_get_next_property(prop)) {
1844 1844
1845 1845 /*
1846 1846 * extract the property name/value and output with
1847 1847 * appropriate spacing. I.e. no prefixed space the
1848 1848 * first time through but a space on subsequent
1849 1849 * properties.
1850 1850 */
1851 1851 type = sa_get_property_attr(prop, "type");
1852 1852 value = sa_get_property_attr(prop, "value");
1853 1853 if (type != NULL) {
1854 1854 (void) printf("%s%s=", spacer ? " " : "", type);
1855 1855 spacer = 1;
1856 1856 if (value != NULL)
1857 1857 (void) printf("\"%s\"", value);
1858 1858 else
1859 1859 (void) printf("\"\"");
1860 1860 }
1861 1861 if (type != NULL)
1862 1862 sa_free_attr_string(type);
1863 1863 if (value != NULL)
1864 1864 sa_free_attr_string(value);
1865 1865 }
1866 1866 (void) printf(")");
1867 1867 }
1868 1868
1869 1869 /*
1870 1870 * show_properties(group, protocol, prefix)
1871 1871 *
1872 1872 * print the properties for a group. If protocol is NULL, do all
1873 1873 * protocols otherwise only the specified protocol. All security
1874 1874 * (named groups specific to the protocol) are included.
1875 1875 *
1876 1876 * The "prefix" is always applied. The caller knows whether it wants
1877 1877 * some type of prefix string (white space) or not. Once the prefix
1878 1878 * has been output, it is reduced to the zero length string for the
1879 1879 * remainder of the property output.
1880 1880 */
1881 1881
1882 1882 static void
1883 1883 show_properties(sa_group_t group, char *protocol, char *prefix)
1884 1884 {
1885 1885 sa_optionset_t optionset;
1886 1886 sa_security_t security;
1887 1887 char *value;
1888 1888 char *secvalue;
1889 1889
1890 1890 if (protocol != NULL) {
1891 1891 optionset = sa_get_optionset(group, protocol);
1892 1892 if (optionset != NULL) {
1893 1893 (void) printf("%s", prefix);
1894 1894 prefix = "";
1895 1895 out_properties(optionset, protocol, NULL);
1896 1896 }
1897 1897 security = sa_get_security(group, protocol, NULL);
1898 1898 if (security != NULL) {
1899 1899 (void) printf("%s", prefix);
1900 1900 prefix = "";
1901 1901 out_properties(security, protocol, NULL);
1902 1902 }
1903 1903 } else {
1904 1904 for (optionset = sa_get_optionset(group, protocol);
1905 1905 optionset != NULL;
1906 1906 optionset = sa_get_next_optionset(optionset)) {
1907 1907
1908 1908 value = sa_get_optionset_attr(optionset, "type");
1909 1909 (void) printf("%s", prefix);
1910 1910 prefix = "";
1911 1911 out_properties(optionset, value, 0);
1912 1912 if (value != NULL)
1913 1913 sa_free_attr_string(value);
1914 1914 }
1915 1915 for (security = sa_get_security(group, NULL, protocol);
1916 1916 security != NULL;
1917 1917 security = sa_get_next_security(security)) {
1918 1918
1919 1919 value = sa_get_security_attr(security, "type");
1920 1920 secvalue = sa_get_security_attr(security, "sectype");
1921 1921 (void) printf("%s", prefix);
1922 1922 prefix = "";
1923 1923 out_properties(security, value, secvalue);
1924 1924 if (value != NULL)
1925 1925 sa_free_attr_string(value);
1926 1926 if (secvalue != NULL)
1927 1927 sa_free_attr_string(secvalue);
1928 1928 }
1929 1929 }
1930 1930 }
1931 1931
1932 1932 /*
1933 1933 * get_resource(share)
1934 1934 *
1935 1935 * Get the first resource name, if any, and fix string to be in
1936 1936 * current locale and have quotes if it has embedded spaces. Return
1937 1937 * an attr string that must be freed.
1938 1938 */
1939 1939
1940 1940 static char *
1941 1941 get_resource(sa_share_t share)
1942 1942 {
1943 1943 sa_resource_t resource;
1944 1944 char *resstring = NULL;
1945 1945 char *retstring;
1946 1946
1947 1947 if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
1948 1948 resstring = sa_get_resource_attr(resource, "name");
1949 1949 if (resstring != NULL) {
1950 1950 char *cp;
1951 1951 int len;
1952 1952
1953 1953 retstring = conv_from_utf8(resstring);
1954 1954 if (retstring != resstring) {
1955 1955 sa_free_attr_string(resstring);
1956 1956 resstring = retstring;
1957 1957 }
1958 1958 if (strpbrk(resstring, " ") != NULL) {
1959 1959 /* account for quotes */
1960 1960 len = strlen(resstring) + 3;
1961 1961 cp = calloc(len, sizeof (char));
1962 1962 if (cp != NULL) {
1963 1963 (void) snprintf(cp, len,
1964 1964 "\"%s\"", resstring);
1965 1965 sa_free_attr_string(resstring);
1966 1966 resstring = cp;
1967 1967 } else {
1968 1968 sa_free_attr_string(resstring);
1969 1969 resstring = NULL;
1970 1970 }
1971 1971 }
1972 1972 }
1973 1973 }
1974 1974 return (resstring);
1975 1975 }
1976 1976
1977 1977 /*
1978 1978 * has_resource_with_opt(share)
1979 1979 *
1980 1980 * Check to see if the share has any resource names with optionsets
1981 1981 * set. Also indicate if multiple resource names since the syntax
1982 1982 * would be about the same.
1983 1983 */
1984 1984 static int
1985 1985 has_resource_with_opt(sa_share_t share)
1986 1986 {
1987 1987 sa_resource_t resource;
1988 1988 int ret = B_FALSE;
1989 1989
1990 1990 for (resource = sa_get_share_resource(share, NULL);
1991 1991 resource != NULL;
1992 1992 resource = sa_get_next_resource(resource)) {
1993 1993
1994 1994 if (sa_get_optionset(resource, NULL) != NULL) {
1995 1995 ret = B_TRUE;
1996 1996 break;
1997 1997 }
1998 1998 }
1999 1999 return (ret);
2000 2000 }
2001 2001
2002 2002 /*
2003 2003 * has_multiple_resource(share)
2004 2004 *
2005 2005 * Check to see if the share has multiple resource names since
2006 2006 * the syntax would be about the same.
2007 2007 */
2008 2008 static boolean_t
2009 2009 has_multiple_resource(sa_share_t share)
2010 2010 {
2011 2011 sa_resource_t resource;
2012 2012 int num;
2013 2013
2014 2014 for (num = 0, resource = sa_get_share_resource(share, NULL);
2015 2015 resource != NULL;
2016 2016 resource = sa_get_next_resource(resource)) {
2017 2017 num++;
2018 2018 if (num > 1)
2019 2019 return (B_TRUE);
2020 2020 }
2021 2021 return (B_FALSE);
2022 2022 }
2023 2023
2024 2024 /*
2025 2025 * show_share(share, verbose, properties, proto, iszfs, sharepath)
2026 2026 *
2027 2027 * print out the share information. With the addition of resource as a
2028 2028 * full object that can have multiple instances below the share, we
2029 2029 * need to display that as well.
2030 2030 */
2031 2031
2032 2032 static void
2033 2033 show_share(sa_share_t share, int verbose, int properties, char *proto,
2034 2034 int iszfs, char *sharepath)
2035 2035 {
2036 2036 char *drive;
2037 2037 char *exclude;
2038 2038 sa_resource_t resource = NULL;
2039 2039 char *description;
2040 2040 char *rsrcname;
2041 2041 int rsrcwithopt;
2042 2042 boolean_t multiple;
2043 2043 char *type;
2044 2044
2045 2045 rsrcwithopt = has_resource_with_opt(share);
2046 2046
2047 2047 if (verbose || (properties && rsrcwithopt)) {
2048 2048 /* First, indicate if transient */
2049 2049 type = sa_get_share_attr(share, "type");
2050 2050 if (type != NULL && !iszfs && verbose &&
2051 2051 strcmp(type, "transient") == 0)
2052 2052 (void) printf("\t* ");
2053 2053 else
2054 2054 (void) printf("\t ");
2055 2055
2056 2056 if (type != NULL)
2057 2057 sa_free_attr_string(type);
2058 2058
2059 2059 /*
2060 2060 * If we came in with verbose, we want to handle the case of
2061 2061 * multiple resources as though they had properties set.
2062 2062 */
2063 2063 multiple = has_multiple_resource(share);
2064 2064
2065 2065 /*
2066 2066 * if there is a description on the share and there
2067 2067 * are resources, treat as multiple resources in order
2068 2068 * to get all descriptions displayed.
2069 2069 */
2070 2070 description = sa_get_share_description(share);
2071 2071 resource = sa_get_share_resource(share, NULL);
2072 2072
2073 2073 if (description != NULL && resource != NULL)
2074 2074 multiple = B_TRUE;
2075 2075
2076 2076 /* Next, if not multiple follow old model */
2077 2077 if (!multiple && !rsrcwithopt) {
2078 2078 rsrcname = get_resource(share);
2079 2079 if (rsrcname != NULL && strlen(rsrcname) > 0) {
2080 2080 (void) printf("%s=%s", rsrcname, sharepath);
2081 2081 } else {
2082 2082 (void) printf("%s", sharepath);
2083 2083 }
2084 2084 if (rsrcname != NULL)
2085 2085 sa_free_attr_string(rsrcname);
2086 2086 /* Print the description string if there is one. */
2087 2087 print_rsrc_desc(resource, description);
2088 2088 } else {
2089 2089 /* Treat as simple and then resources come later */
2090 2090 (void) printf("%s", sharepath);
2091 2091 }
2092 2092 drive = sa_get_share_attr(share, "drive-letter");
2093 2093 if (drive != NULL) {
2094 2094 if (strlen(drive) > 0)
2095 2095 (void) printf(gettext("\tdrive-letter=\"%s:\""),
2096 2096 drive);
2097 2097 sa_free_attr_string(drive);
2098 2098 }
2099 2099 if (properties)
2100 2100 show_properties(share, proto, "\t");
2101 2101 exclude = sa_get_share_attr(share, "exclude");
2102 2102 if (exclude != NULL) {
2103 2103 (void) printf(gettext("\tnot-shared-with=[%s]"),
2104 2104 exclude);
2105 2105 sa_free_attr_string(exclude);
2106 2106 }
2107 2107
2108 2108 if (description != NULL) {
2109 2109 print_rsrc_desc((sa_resource_t)share, description);
2110 2110 }
2111 2111 /*
2112 2112 * If there are resource names with options, show them
2113 2113 * here, with one line per resource. Resource specific
2114 2114 * options are at the end of the line followed by
2115 2115 * description, if any.
2116 2116 */
2117 2117 if (rsrcwithopt || multiple) {
2118 2118 for (resource = sa_get_share_resource(share, NULL);
2119 2119 resource != NULL;
2120 2120 resource = sa_get_next_resource(resource)) {
2121 2121 int has_space;
2122 2122 char *rsrc;
2123 2123
2124 2124 (void) printf("\n\t\t ");
2125 2125 rsrcname = sa_get_resource_attr(resource,
2126 2126 "name");
2127 2127 if (rsrcname == NULL)
2128 2128 continue;
2129 2129
2130 2130 rsrc = conv_from_utf8(rsrcname);
2131 2131 has_space = strpbrk(rsrc, " ") != NULL;
2132 2132
2133 2133 if (has_space)
2134 2134 (void) printf("\"%s\"=%s", rsrc,
2135 2135 sharepath);
2136 2136 else
2137 2137 (void) printf("%s=%s", rsrc,
2138 2138 sharepath);
2139 2139 if (rsrc != rsrcname)
2140 2140 sa_free_attr_string(rsrc);
2141 2141 sa_free_attr_string(rsrcname);
2142 2142 if (properties || rsrcwithopt)
2143 2143 show_properties(resource, proto, "\t");
2144 2144
2145 2145 /* Get description string if any */
2146 2146 print_rsrc_desc(resource, description);
2147 2147 }
2148 2148 }
2149 2149 if (description != NULL)
2150 2150 sa_free_share_description(description);
2151 2151 } else {
2152 2152 (void) printf("\t %s", sharepath);
2153 2153 if (properties)
2154 2154 show_properties(share, proto, "\t");
2155 2155 }
2156 2156 (void) printf("\n");
2157 2157 }
2158 2158
2159 2159 /*
2160 2160 * show_group(group, verbose, properties, proto, subgroup)
2161 2161 *
2162 2162 * helper function to show the contents of a group.
2163 2163 */
2164 2164
2165 2165 static void
2166 2166 show_group(sa_group_t group, int verbose, int properties, char *proto,
2167 2167 char *subgroup)
2168 2168 {
2169 2169 sa_share_t share;
2170 2170 char *groupname;
2171 2171 char *zfs = NULL;
2172 2172 int iszfs = 0;
2173 2173 char *sharepath;
2174 2174
2175 2175 groupname = sa_get_group_attr(group, "name");
2176 2176 if (groupname != NULL) {
2177 2177 if (proto != NULL && !has_protocol(group, proto)) {
2178 2178 sa_free_attr_string(groupname);
2179 2179 return;
2180 2180 }
2181 2181 /*
2182 2182 * check to see if the group is managed by ZFS. If
2183 2183 * there is an attribute, then it is. A non-NULL zfs
2184 2184 * variable will trigger the different way to display
2185 2185 * and will remove the transient property indicator
2186 2186 * from the output.
2187 2187 */
2188 2188 zfs = sa_get_group_attr(group, "zfs");
2189 2189 if (zfs != NULL) {
2190 2190 iszfs = 1;
2191 2191 sa_free_attr_string(zfs);
2192 2192 }
2193 2193 share = sa_get_share(group, NULL);
2194 2194 if (subgroup == NULL)
2195 2195 (void) printf("%s", groupname);
2196 2196 else
2197 2197 (void) printf(" %s/%s", subgroup, groupname);
2198 2198 if (properties)
2199 2199 show_properties(group, proto, "");
2200 2200 (void) printf("\n");
2201 2201 if (strcmp(groupname, "zfs") == 0) {
2202 2202 sa_group_t zgroup;
2203 2203
2204 2204 for (zgroup = sa_get_sub_group(group);
2205 2205 zgroup != NULL;
2206 2206 zgroup = sa_get_next_group(zgroup)) {
2207 2207 show_group(zgroup, verbose, properties, proto,
2208 2208 "zfs");
2209 2209 }
2210 2210 sa_free_attr_string(groupname);
2211 2211 return;
2212 2212 }
2213 2213 /*
2214 2214 * Have a group, so list the contents. Resource and
2215 2215 * description are only listed if verbose is set.
2216 2216 */
2217 2217 for (share = sa_get_share(group, NULL);
2218 2218 share != NULL;
2219 2219 share = sa_get_next_share(share)) {
2220 2220 sharepath = sa_get_share_attr(share, "path");
2221 2221 if (sharepath != NULL) {
2222 2222 show_share(share, verbose, properties, proto,
2223 2223 iszfs, sharepath);
2224 2224 sa_free_attr_string(sharepath);
2225 2225 }
2226 2226 }
2227 2227 }
2228 2228 if (groupname != NULL) {
2229 2229 sa_free_attr_string(groupname);
2230 2230 }
2231 2231 }
2232 2232
2233 2233 /*
2234 2234 * show_group_xml_init()
2235 2235 *
2236 2236 * Create an XML document that will be used to display config info via
2237 2237 * XML format.
2238 2238 */
2239 2239
2240 2240 xmlDocPtr
2241 2241 show_group_xml_init()
2242 2242 {
2243 2243 xmlDocPtr doc;
2244 2244 xmlNodePtr root;
2245 2245
2246 2246 doc = xmlNewDoc((xmlChar *)"1.0");
2247 2247 if (doc != NULL) {
2248 2248 root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
2249 2249 if (root != NULL)
2250 2250 (void) xmlDocSetRootElement(doc, root);
2251 2251 }
2252 2252 return (doc);
2253 2253 }
2254 2254
2255 2255 /*
2256 2256 * show_group_xml(doc, group)
2257 2257 *
2258 2258 * Copy the group info into the XML doc.
2259 2259 */
2260 2260
2261 2261 static void
2262 2262 show_group_xml(xmlDocPtr doc, sa_group_t group)
2263 2263 {
2264 2264 xmlNodePtr node;
2265 2265 xmlNodePtr root;
2266 2266
2267 2267 root = xmlDocGetRootElement(doc);
2268 2268 node = xmlCopyNode((xmlNodePtr)group, 1);
2269 2269 if (node != NULL && root != NULL) {
2270 2270 (void) xmlAddChild(root, node);
2271 2271 /*
2272 2272 * In the future, we may have interally used tags that
2273 2273 * should not appear in the XML output. Remove
2274 2274 * anything we don't want to show here.
2275 2275 */
2276 2276 }
2277 2277 }
2278 2278
2279 2279 /*
2280 2280 * sa_show(flags, argc, argv)
2281 2281 *
2282 2282 * Implements the show subcommand.
2283 2283 */
2284 2284
2285 2285 int
2286 2286 sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
2287 2287 {
2288 2288 sa_group_t group;
2289 2289 int verbose = 0;
2290 2290 int properties = 0;
2291 2291 int c;
2292 2292 int ret = SA_OK;
2293 2293 char *protocol = NULL;
2294 2294 int xml = 0;
2295 2295 xmlDocPtr doc;
2296 2296 #ifdef lint
2297 2297 flags = flags;
2298 2298 #endif
2299 2299
2300 2300 while ((c = getopt(argc, argv, "?hvP:px")) != EOF) {
2301 2301 switch (c) {
2302 2302 case 'v':
2303 2303 verbose++;
2304 2304 break;
2305 2305 case 'p':
2306 2306 properties++;
2307 2307 break;
2308 2308 case 'P':
2309 2309 if (protocol != NULL) {
2310 2310 (void) printf(gettext(
2311 2311 "Specifying multiple protocols "
2312 2312 "not supported: %s\n"),
2313 2313 protocol);
2314 2314 return (SA_SYNTAX_ERR);
2315 2315 }
2316 2316 protocol = optarg;
2317 2317 if (!sa_valid_protocol(protocol)) {
2318 2318 (void) printf(gettext(
2319 2319 "Invalid protocol specified: %s\n"),
2320 2320 protocol);
2321 2321 return (SA_INVALID_PROTOCOL);
2322 2322 }
2323 2323 break;
2324 2324 case 'x':
2325 2325 xml++;
2326 2326 break;
2327 2327 case 'h':
2328 2328 /* optopt on valid arg isn't defined */
2329 2329 optopt = c;
2330 2330 /*FALLTHROUGH*/
2331 2331 case '?':
2332 2332 default:
2333 2333 /*
2334 2334 * Since a bad option gets to here, sort it
2335 2335 * out and return a syntax error return value
2336 2336 * if necessary.
2337 2337 */
2338 2338 switch (optopt) {
2339 2339 default:
2340 2340 ret = SA_SYNTAX_ERR;
2341 2341 break;
2342 2342 case 'h':
2343 2343 case '?':
2344 2344 break;
2345 2345 }
2346 2346 (void) printf(gettext("usage: %s\n"),
2347 2347 sa_get_usage(USAGE_SHOW));
2348 2348 return (ret);
2349 2349 }
2350 2350 }
2351 2351
2352 2352 if (xml) {
2353 2353 doc = show_group_xml_init();
2354 2354 if (doc == NULL)
2355 2355 ret = SA_NO_MEMORY;
2356 2356 }
2357 2357
2358 2358 if (optind == argc) {
2359 2359 /* No group specified so go through them all */
2360 2360 for (group = sa_get_group(handle, NULL);
2361 2361 group != NULL;
2362 2362 group = sa_get_next_group(group)) {
2363 2363 /*
2364 2364 * Have a group so check if one we want and then list
2365 2365 * contents with appropriate options.
2366 2366 */
2367 2367 if (xml)
2368 2368 show_group_xml(doc, group);
2369 2369 else
2370 2370 show_group(group, verbose, properties, protocol,
2371 2371 NULL);
2372 2372 }
2373 2373 } else {
2374 2374 /* Have a specified list of groups */
2375 2375 for (; optind < argc; optind++) {
2376 2376 group = sa_get_group(handle, argv[optind]);
2377 2377 if (group != NULL) {
2378 2378 if (xml)
2379 2379 show_group_xml(doc, group);
2380 2380 else
2381 2381 show_group(group, verbose, properties,
2382 2382 protocol, NULL);
2383 2383 } else {
2384 2384 (void) printf(gettext("%s: not found\n"),
2385 2385 argv[optind]);
2386 2386 ret = SA_NO_SUCH_GROUP;
2387 2387 }
2388 2388 }
2389 2389 }
2390 2390 if (xml && ret == SA_OK) {
2391 2391 (void) xmlDocFormatDump(stdout, doc, 1);
2392 2392 xmlFreeDoc(doc);
2393 2393 }
2394 2394 return (ret);
2395 2395
2396 2396 }
2397 2397
2398 2398 /*
2399 2399 * enable_share(group, share, update_legacy)
2400 2400 *
2401 2401 * helper function to enable a share if the group is enabled.
2402 2402 */
2403 2403
2404 2404 static int
2405 2405 enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
2406 2406 int update_legacy)
2407 2407 {
2408 2408 char *value;
2409 2409 int enabled;
2410 2410 sa_optionset_t optionset;
2411 2411 int err;
2412 2412 int ret = SA_OK;
2413 2413 char *zfs = NULL;
2414 2414 int iszfs = 0;
2415 2415 int isshare;
2416 2416
2417 2417 /*
2418 2418 * need to enable this share if the group is enabled but not
2419 2419 * otherwise. The enable is also done on each protocol
2420 2420 * represented in the group.
2421 2421 */
2422 2422 value = sa_get_group_attr(group, "state");
2423 2423 enabled = value != NULL && strcmp(value, "enabled") == 0;
2424 2424 if (value != NULL)
2425 2425 sa_free_attr_string(value);
2426 2426 /* remove legacy config if necessary */
2427 2427 if (update_legacy)
2428 2428 ret = sa_delete_legacy(share, NULL);
2429 2429 zfs = sa_get_group_attr(group, "zfs");
2430 2430 if (zfs != NULL) {
2431 2431 iszfs++;
2432 2432 sa_free_attr_string(zfs);
2433 2433 }
2434 2434
2435 2435 /*
2436 2436 * Step through each optionset at the group level and
2437 2437 * enable the share based on the protocol type. This
2438 2438 * works because protocols must be set on the group
2439 2439 * for the protocol to be enabled.
2440 2440 */
2441 2441 isshare = sa_is_share(share);
2442 2442 for (optionset = sa_get_optionset(group, NULL);
2443 2443 optionset != NULL && ret == SA_OK;
2444 2444 optionset = sa_get_next_optionset(optionset)) {
2445 2445 value = sa_get_optionset_attr(optionset, "type");
2446 2446 if (value != NULL) {
2447 2447 if (enabled) {
2448 2448 if (isshare) {
2449 2449 err = sa_enable_share(share, value);
2450 2450 } else {
2451 2451 err = sa_enable_resource(share, value);
2452 2452 if (err == SA_NOT_SUPPORTED) {
2453 2453 sa_share_t parent;
2454 2454 parent = sa_get_resource_parent(
2455 2455 share);
2456 2456 if (parent != NULL)
2457 2457 err = sa_enable_share(
2458 2458 parent, value);
2459 2459 }
2460 2460 }
2461 2461 if (err != SA_OK) {
2462 2462 ret = err;
2463 2463 (void) printf(gettext(
2464 2464 "Failed to enable share for "
2465 2465 "\"%s\": %s\n"),
2466 2466 value, sa_errorstr(ret));
2467 2467 }
2468 2468 }
2469 2469 /*
2470 2470 * If we want to update the legacy, use a copy of
2471 2471 * share so we can avoid breaking the loop we are in
2472 2472 * since we might also need to go up the tree to the
2473 2473 * parent.
2474 2474 */
2475 2475 if (update_legacy && !iszfs) {
2476 2476 sa_share_t update = share;
2477 2477 if (!sa_is_share(share)) {
2478 2478 update = sa_get_resource_parent(share);
2479 2479 }
2480 2480 (void) sa_update_legacy(update, value);
2481 2481 }
2482 2482 sa_free_attr_string(value);
2483 2483 }
2484 2484 }
2485 2485 if (ret == SA_OK)
2486 2486 (void) sa_update_config(handle);
2487 2487 return (ret);
2488 2488 }
2489 2489
2490 2490 /*
2491 2491 * sa_require_resource(group)
2492 2492 *
2493 2493 * if any of the defined protocols on the group require resource
2494 2494 * names, then all shares must have them.
2495 2495 */
2496 2496
2497 2497 static int
2498 2498 sa_require_resource(sa_group_t group)
2499 2499 {
2500 2500 sa_optionset_t optionset;
2501 2501
2502 2502 for (optionset = sa_get_optionset(group, NULL);
2503 2503 optionset != NULL;
2504 2504 optionset = sa_get_next_optionset(optionset)) {
2505 2505 char *proto;
2506 2506
2507 2507 proto = sa_get_optionset_attr(optionset, "type");
2508 2508 if (proto != NULL) {
2509 2509 uint64_t features;
2510 2510
2511 2511 features = sa_proto_get_featureset(proto);
2512 2512 if (features & SA_FEATURE_RESOURCE) {
2513 2513 sa_free_attr_string(proto);
2514 2514 return (B_TRUE);
2515 2515 }
2516 2516 sa_free_attr_string(proto);
2517 2517 }
2518 2518 }
2519 2519 return (B_FALSE);
2520 2520 }
2521 2521
2522 2522 /*
2523 2523 * sa_addshare(flags, argc, argv)
2524 2524 *
2525 2525 * implements add-share subcommand.
2526 2526 */
2527 2527
2528 2528 static int
2529 2529 sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
2530 2530 {
2531 2531 int verbose = 0;
2532 2532 int dryrun = 0;
2533 2533 int c;
2534 2534 int ret = SA_OK;
2535 2535 sa_group_t group;
2536 2536 sa_share_t share;
2537 2537 sa_resource_t resource = NULL;
2538 2538 char *sharepath = NULL;
2539 2539 char *description = NULL;
2540 2540 char *rsrcname = NULL;
2541 2541 char *rsrc = NULL;
2542 2542 int persist = SA_SHARE_PERMANENT; /* default to persist */
2543 2543 int auth;
2544 2544 char dir[MAXPATHLEN];
2545 2545
2546 2546 while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
2547 2547 switch (c) {
2548 2548 case 'n':
2549 2549 dryrun++;
2550 2550 break;
2551 2551 case 'v':
2552 2552 verbose++;
2553 2553 break;
2554 2554 case 'd':
2555 2555 description = optarg;
2556 2556 break;
2557 2557 case 'r':
2558 2558 if (rsrcname != NULL) {
2559 2559 (void) printf(gettext("Adding multiple "
2560 2560 "resource names not"
2561 2561 " supported\n"));
2562 2562 return (SA_SYNTAX_ERR);
2563 2563 }
2564 2564 rsrcname = optarg;
2565 2565 break;
2566 2566 case 's':
2567 2567 /*
2568 2568 * Save share path into group. Currently limit
2569 2569 * to one share per command.
2570 2570 */
2571 2571 if (sharepath != NULL) {
2572 2572 (void) printf(gettext(
2573 2573 "Adding multiple shares not supported\n"));
2574 2574 return (SA_SYNTAX_ERR);
2575 2575 }
2576 2576 sharepath = optarg;
2577 2577 break;
2578 2578 case 't':
2579 2579 persist = SA_SHARE_TRANSIENT;
2580 2580 break;
2581 2581 case 'h':
2582 2582 /* optopt on valid arg isn't defined */
2583 2583 optopt = c;
2584 2584 /*FALLTHROUGH*/
2585 2585 case '?':
2586 2586 default:
2587 2587 /*
2588 2588 * Since a bad option gets to here, sort it
2589 2589 * out and return a syntax error return value
2590 2590 * if necessary.
2591 2591 */
2592 2592 switch (optopt) {
2593 2593 default:
2594 2594 ret = SA_SYNTAX_ERR;
2595 2595 break;
2596 2596 case 'h':
2597 2597 case '?':
2598 2598 break;
2599 2599 }
2600 2600 (void) printf(gettext("usage: %s\n"),
2601 2601 sa_get_usage(USAGE_ADD_SHARE));
2602 2602 return (ret);
2603 2603 }
2604 2604 }
2605 2605
2606 2606 if (optind >= argc) {
2607 2607 (void) printf(gettext("usage: %s\n"),
2608 2608 sa_get_usage(USAGE_ADD_SHARE));
2609 2609 if (dryrun || sharepath != NULL || description != NULL ||
2610 2610 rsrcname != NULL || verbose || persist) {
2611 2611 (void) printf(gettext("\tgroup must be specified\n"));
2612 2612 ret = SA_NO_SUCH_GROUP;
2613 2613 } else {
2614 2614 ret = SA_OK;
2615 2615 }
2616 2616 } else {
2617 2617 if (sharepath == NULL) {
2618 2618 (void) printf(gettext("usage: %s\n"),
2619 2619 sa_get_usage(USAGE_ADD_SHARE));
2620 2620 (void) printf(gettext(
2621 2621 "\t-s sharepath must be specified\n"));
2622 2622 ret = SA_BAD_PATH;
2623 2623 }
2624 2624 if (ret == SA_OK) {
2625 2625 if (realpath(sharepath, dir) == NULL) {
2626 2626 ret = SA_BAD_PATH;
2627 2627 (void) printf(gettext("Path "
2628 2628 "is not valid: %s\n"),
2629 2629 sharepath);
2630 2630 } else {
2631 2631 sharepath = dir;
2632 2632 }
2633 2633 }
2634 2634 if (ret == SA_OK && rsrcname != NULL) {
2635 2635 /* check for valid syntax */
2636 2636 if (validresource(rsrcname)) {
2637 2637 rsrc = conv_to_utf8(rsrcname);
2638 2638 resource = sa_find_resource(handle, rsrc);
2639 2639 if (resource != NULL) {
2640 2640 /*
2641 2641 * Resource names must be
2642 2642 * unique in the system
2643 2643 */
2644 2644 ret = SA_DUPLICATE_NAME;
2645 2645 (void) printf(gettext("usage: %s\n"),
2646 2646 sa_get_usage(USAGE_ADD_SHARE));
2647 2647 (void) printf(gettext(
2648 2648 "\tresource names must be unique "
2649 2649 "in the system\n"));
2650 2650 }
2651 2651 } else {
2652 2652 (void) printf(gettext("usage: %s\n"),
2653 2653 sa_get_usage(USAGE_ADD_SHARE));
2654 2654 (void) printf(gettext(
2655 2655 "\tresource names use restricted "
2656 2656 "character set\n"));
2657 2657 ret = SA_INVALID_NAME;
2658 2658 }
2659 2659 }
2660 2660
2661 2661 if (ret != SA_OK) {
2662 2662 if (rsrc != NULL && rsrcname != rsrc)
2663 2663 sa_free_attr_string(rsrc);
2664 2664 return (ret);
2665 2665 }
2666 2666
2667 2667 share = sa_find_share(handle, sharepath);
2668 2668 if (share != NULL) {
2669 2669 if (rsrcname == NULL) {
2670 2670 /*
2671 2671 * Can only have a duplicate share if a new
2672 2672 * resource name is being added.
2673 2673 */
2674 2674 ret = SA_DUPLICATE_NAME;
2675 2675 (void) printf(gettext("Share path already "
2676 2676 "shared: %s\n"), sharepath);
2677 2677 }
2678 2678 }
2679 2679 if (ret != SA_OK)
2680 2680 return (ret);
2681 2681
2682 2682 group = sa_get_group(handle, argv[optind]);
2683 2683 if (group != NULL) {
2684 2684 if (sa_require_resource(group) == B_TRUE &&
2685 2685 rsrcname == NULL) {
2686 2686 (void) printf(gettext(
2687 2687 "Resource name is required "
2688 2688 "by at least one enabled protocol "
2689 2689 "in group\n"));
2690 2690 return (SA_RESOURCE_REQUIRED);
2691 2691 }
2692 2692 if (share == NULL && ret == SA_OK) {
2693 2693 if (dryrun)
2694 2694 ret = sa_check_path(group, sharepath,
2695 2695 SA_CHECK_NORMAL);
2696 2696 else
2697 2697 share = sa_add_share(group, sharepath,
2698 2698 persist, &ret);
2699 2699 }
2700 2700 /*
2701 2701 * Make sure this isn't an attempt to put a resourced
2702 2702 * share into a different group than it already is in.
2703 2703 */
2704 2704 if (share != NULL) {
2705 2705 sa_group_t parent;
2706 2706 parent = sa_get_parent_group(share);
2707 2707 if (parent != group) {
2708 2708 ret = SA_DUPLICATE_NAME;
2709 2709 (void) printf(gettext(
2710 2710 "Share path already "
2711 2711 "shared: %s\n"), sharepath);
2712 2712 }
2713 2713 }
2714 2714 if (!dryrun && share == NULL) {
2715 2715 (void) printf(gettext(
2716 2716 "Could not add share: %s\n"),
2717 2717 sa_errorstr(ret));
2718 2718 } else {
2719 2719 auth = check_authorizations(argv[optind],
2720 2720 flags);
2721 2721 if (!dryrun && ret == SA_OK) {
2722 2722 if (rsrcname != NULL) {
2723 2723 resource = sa_add_resource(
2724 2724 share,
2725 2725 rsrc,
2726 2726 SA_SHARE_PERMANENT,
2727 2727 &ret);
2728 2728 }
2729 2729 if (ret == SA_OK &&
2730 2730 description != NULL) {
2731 2731 if (resource != NULL)
2732 2732 ret =
2733 2733 set_resource_desc(
2734 2734 resource,
2735 2735 description);
2736 2736 else
2737 2737 ret =
2738 2738 set_share_desc(
2739 2739 share,
2740 2740 description);
2741 2741 }
2742 2742 if (ret == SA_OK) {
2743 2743 /* now enable the share(s) */
2744 2744 if (resource != NULL) {
2745 2745 ret = enable_share(
2746 2746 handle,
2747 2747 group,
2748 2748 resource,
2749 2749 1);
2750 2750 } else {
2751 2751 ret = enable_share(
2752 2752 handle,
2753 2753 group,
2754 2754 share,
2755 2755 1);
2756 2756 }
2757 2757 ret = sa_update_config(handle);
2758 2758 }
2759 2759 switch (ret) {
2760 2760 case SA_DUPLICATE_NAME:
2761 2761 (void) printf(gettext(
2762 2762 "Resource name in"
2763 2763 "use: %s\n"),
2764 2764 rsrcname);
2765 2765 break;
2766 2766 default:
2767 2767 (void) printf(gettext(
2768 2768 "Could not set "
2769 2769 "attribute: %s\n"),
2770 2770 sa_errorstr(ret));
2771 2771 break;
2772 2772 case SA_OK:
2773 2773 break;
2774 2774 }
2775 2775 } else if (dryrun && ret == SA_OK &&
2776 2776 !auth && verbose) {
2777 2777 (void) printf(gettext(
2778 2778 "Command would fail: %s\n"),
2779 2779 sa_errorstr(SA_NO_PERMISSION));
2780 2780 ret = SA_NO_PERMISSION;
2781 2781 }
2782 2782 }
2783 2783 } else {
2784 2784 switch (ret) {
2785 2785 default:
2786 2786 (void) printf(gettext(
2787 2787 "Group \"%s\" not found\n"), argv[optind]);
2788 2788 ret = SA_NO_SUCH_GROUP;
2789 2789 break;
2790 2790 case SA_BAD_PATH:
2791 2791 case SA_DUPLICATE_NAME:
2792 2792 break;
2793 2793 }
2794 2794 }
2795 2795 }
2796 2796 return (ret);
2797 2797 }
2798 2798
2799 2799 /*
2800 2800 * sa_moveshare(flags, argc, argv)
2801 2801 *
2802 2802 * implements move-share subcommand.
2803 2803 */
2804 2804
2805 2805 int
2806 2806 sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
2807 2807 {
2808 2808 int verbose = 0;
2809 2809 int dryrun = 0;
2810 2810 int c;
2811 2811 int ret = SA_OK;
2812 2812 sa_group_t group;
2813 2813 sa_share_t share;
2814 2814 char *rsrcname = NULL;
2815 2815 char *sharepath = NULL;
2816 2816 int authsrc = 0, authdst = 0;
2817 2817 char dir[MAXPATHLEN];
2818 2818
2819 2819 while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
2820 2820 switch (c) {
2821 2821 case 'n':
2822 2822 dryrun++;
2823 2823 break;
2824 2824 case 'v':
2825 2825 verbose++;
2826 2826 break;
2827 2827 case 'r':
2828 2828 if (rsrcname != NULL) {
2829 2829 (void) printf(gettext(
2830 2830 "Moving multiple resource names not"
2831 2831 " supported\n"));
2832 2832 return (SA_SYNTAX_ERR);
2833 2833 }
2834 2834 rsrcname = optarg;
2835 2835 break;
2836 2836 case 's':
2837 2837 /*
2838 2838 * Remove share path from group. Currently limit
2839 2839 * to one share per command.
2840 2840 */
2841 2841 if (sharepath != NULL) {
2842 2842 (void) printf(gettext("Moving multiple shares"
2843 2843 " not supported\n"));
2844 2844 return (SA_SYNTAX_ERR);
2845 2845 }
2846 2846 sharepath = optarg;
2847 2847 break;
2848 2848 case 'h':
2849 2849 /* optopt on valid arg isn't defined */
2850 2850 optopt = c;
2851 2851 /*FALLTHROUGH*/
2852 2852 case '?':
2853 2853 default:
2854 2854 /*
2855 2855 * Since a bad option gets to here, sort it
2856 2856 * out and return a syntax error return value
2857 2857 * if necessary.
2858 2858 */
2859 2859 switch (optopt) {
2860 2860 default:
2861 2861 ret = SA_SYNTAX_ERR;
2862 2862 break;
2863 2863 case 'h':
2864 2864 case '?':
2865 2865 break;
2866 2866 }
2867 2867 (void) printf(gettext("usage: %s\n"),
2868 2868 sa_get_usage(USAGE_MOVE_SHARE));
2869 2869 return (ret);
2870 2870 }
2871 2871 }
2872 2872
2873 2873 if (optind >= argc || sharepath == NULL) {
2874 2874 (void) printf(gettext("usage: %s\n"),
2875 2875 sa_get_usage(USAGE_MOVE_SHARE));
2876 2876 if (dryrun || verbose || sharepath != NULL) {
2877 2877 (void) printf(gettext("\tgroup must be specified\n"));
2878 2878 ret = SA_NO_SUCH_GROUP;
2879 2879 } else {
2880 2880 if (sharepath == NULL) {
2881 2881 ret = SA_SYNTAX_ERR;
2882 2882 (void) printf(gettext(
2883 2883 "\tsharepath must be specified\n"));
2884 2884 } else {
2885 2885 ret = SA_OK;
2886 2886 }
2887 2887 }
2888 2888 } else {
2889 2889 sa_group_t parent;
2890 2890 char *zfsold;
2891 2891 char *zfsnew;
2892 2892
2893 2893 if (sharepath == NULL) {
2894 2894 (void) printf(gettext(
2895 2895 "sharepath must be specified with the -s "
2896 2896 "option\n"));
2897 2897 return (SA_BAD_PATH);
2898 2898 }
2899 2899 group = sa_get_group(handle, argv[optind]);
2900 2900 if (group == NULL) {
2901 2901 (void) printf(gettext("Group \"%s\" not found\n"),
2902 2902 argv[optind]);
2903 2903 return (SA_NO_SUCH_GROUP);
2904 2904 }
2905 2905 share = sa_find_share(handle, sharepath);
2906 2906 /*
2907 2907 * If a share wasn't found, it may have been a symlink
2908 2908 * or has a trailing '/'. Try again after resolving
2909 2909 * with realpath().
2910 2910 */
2911 2911 if (share == NULL) {
2912 2912 if (realpath(sharepath, dir) == NULL) {
2913 2913 (void) printf(gettext("Path "
2914 2914 "is not valid: %s\n"),
2915 2915 sharepath);
2916 2916 return (SA_BAD_PATH);
2917 2917 }
2918 2918 sharepath = dir;
2919 2919 share = sa_find_share(handle, sharepath);
2920 2920 }
2921 2921 if (share == NULL) {
2922 2922 (void) printf(gettext("Share not found: %s\n"),
2923 2923 sharepath);
2924 2924 return (SA_NO_SUCH_PATH);
2925 2925 }
2926 2926 authdst = check_authorizations(argv[optind], flags);
2927 2927
2928 2928 parent = sa_get_parent_group(share);
2929 2929 if (parent != NULL) {
2930 2930 char *pname;
2931 2931 pname = sa_get_group_attr(parent, "name");
2932 2932 if (pname != NULL) {
2933 2933 authsrc = check_authorizations(pname, flags);
2934 2934 sa_free_attr_string(pname);
2935 2935 }
2936 2936 zfsold = sa_get_group_attr(parent, "zfs");
2937 2937 zfsnew = sa_get_group_attr(group, "zfs");
2938 2938 if ((zfsold != NULL && zfsnew == NULL) ||
2939 2939 (zfsold == NULL && zfsnew != NULL)) {
2940 2940 ret = SA_NOT_ALLOWED;
2941 2941 }
2942 2942 if (zfsold != NULL)
2943 2943 sa_free_attr_string(zfsold);
2944 2944 if (zfsnew != NULL)
2945 2945 sa_free_attr_string(zfsnew);
2946 2946 }
2947 2947
2948 2948 if (ret == SA_OK && parent != group && !dryrun) {
2949 2949 char *oldstate;
2950 2950 /*
2951 2951 * Note that the share may need to be
2952 2952 * "unshared" if the new group is disabled and
2953 2953 * the old was enabled or it may need to be
2954 2954 * share to update if the new group is
2955 2955 * enabled. We disable before the move and
2956 2956 * will have to enable after the move in order
2957 2957 * to cleanup entries for protocols that
2958 2958 * aren't in the new group.
2959 2959 */
2960 2960 oldstate = sa_get_group_attr(parent, "state");
2961 2961 if (oldstate != NULL) {
2962 2962 /* enable_share determines what to do */
2963 2963 if (strcmp(oldstate, "enabled") == 0)
2964 2964 (void) sa_disable_share(share, NULL);
2965 2965 sa_free_attr_string(oldstate);
2966 2966 }
2967 2967 }
2968 2968
2969 2969 if (!dryrun && ret == SA_OK)
2970 2970 ret = sa_move_share(group, share);
2971 2971
2972 2972 /*
2973 2973 * Reenable and update any config information.
2974 2974 */
2975 2975 if (ret == SA_OK && parent != group && !dryrun) {
2976 2976 ret = sa_update_config(handle);
2977 2977
2978 2978 (void) enable_share(handle, group, share, 1);
2979 2979 }
2980 2980
2981 2981 if (ret != SA_OK)
2982 2982 (void) printf(gettext("Could not move share: %s\n"),
2983 2983 sa_errorstr(ret));
2984 2984
2985 2985 if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
2986 2986 verbose) {
2987 2987 (void) printf(gettext("Command would fail: %s\n"),
2988 2988 sa_errorstr(SA_NO_PERMISSION));
2989 2989 }
2990 2990 }
2991 2991 return (ret);
2992 2992 }
2993 2993
2994 2994 /*
2995 2995 * sa_removeshare(flags, argc, argv)
2996 2996 *
2997 2997 * implements remove-share subcommand.
2998 2998 */
2999 2999
3000 3000 int
3001 3001 sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
3002 3002 {
3003 3003 int verbose = 0;
3004 3004 int dryrun = 0;
3005 3005 int force = 0;
3006 3006 int c;
3007 3007 int ret = SA_OK;
3008 3008 sa_group_t group;
3009 3009 sa_resource_t resource = NULL;
3010 3010 sa_share_t share = NULL;
3011 3011 char *rsrcname = NULL;
3012 3012 char *sharepath = NULL;
3013 3013 char dir[MAXPATHLEN];
3014 3014 int auth;
3015 3015
3016 3016 while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
3017 3017 switch (c) {
3018 3018 case 'n':
3019 3019 dryrun++;
3020 3020 break;
3021 3021 case 'v':
3022 3022 verbose++;
3023 3023 break;
3024 3024 case 'f':
3025 3025 force++;
3026 3026 break;
3027 3027 case 's':
3028 3028 /*
3029 3029 * Remove share path from group. Currently limit
3030 3030 * to one share per command.
3031 3031 */
3032 3032 if (sharepath != NULL) {
3033 3033 (void) printf(gettext(
3034 3034 "Removing multiple shares not "
3035 3035 "supported\n"));
3036 3036 return (SA_SYNTAX_ERR);
3037 3037 }
3038 3038 sharepath = optarg;
3039 3039 break;
3040 3040 case 'r':
3041 3041 /*
3042 3042 * Remove share from group if last resource or remove
3043 3043 * resource from share if multiple resources.
3044 3044 */
3045 3045 if (rsrcname != NULL) {
3046 3046 (void) printf(gettext(
3047 3047 "Removing multiple resource names not "
3048 3048 "supported\n"));
3049 3049 return (SA_SYNTAX_ERR);
3050 3050 }
3051 3051 rsrcname = optarg;
3052 3052 break;
3053 3053 case 'h':
3054 3054 /* optopt on valid arg isn't defined */
3055 3055 optopt = c;
3056 3056 /*FALLTHROUGH*/
3057 3057 case '?':
3058 3058 default:
3059 3059 /*
3060 3060 * Since a bad option gets to here, sort it
3061 3061 * out and return a syntax error return value
3062 3062 * if necessary.
3063 3063 */
3064 3064 switch (optopt) {
3065 3065 default:
3066 3066 ret = SA_SYNTAX_ERR;
3067 3067 break;
3068 3068 case 'h':
3069 3069 case '?':
3070 3070 break;
3071 3071 }
3072 3072 (void) printf(gettext("usage: %s\n"),
3073 3073 sa_get_usage(USAGE_REMOVE_SHARE));
3074 3074 return (ret);
3075 3075 }
3076 3076 }
3077 3077
3078 3078 if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
3079 3079 if (sharepath == NULL && rsrcname == NULL) {
3080 3080 (void) printf(gettext("usage: %s\n"),
3081 3081 sa_get_usage(USAGE_REMOVE_SHARE));
3082 3082 (void) printf(gettext("\t-s sharepath or -r resource"
3083 3083 " must be specified\n"));
3084 3084 ret = SA_BAD_PATH;
3085 3085 } else {
3086 3086 ret = SA_OK;
3087 3087 }
3088 3088 }
3089 3089 if (ret != SA_OK) {
3090 3090 return (ret);
3091 3091 }
3092 3092
3093 3093 if (optind < argc) {
3094 3094 if ((optind + 1) < argc) {
3095 3095 (void) printf(gettext("Extraneous group(s) at end of "
3096 3096 "command\n"));
3097 3097 ret = SA_SYNTAX_ERR;
3098 3098 } else {
3099 3099 group = sa_get_group(handle, argv[optind]);
3100 3100 if (group == NULL) {
3101 3101 (void) printf(gettext(
3102 3102 "Group \"%s\" not found\n"), argv[optind]);
3103 3103 ret = SA_NO_SUCH_GROUP;
3104 3104 }
3105 3105 }
3106 3106 } else {
3107 3107 group = NULL;
3108 3108 }
3109 3109
3110 3110 if (rsrcname != NULL) {
3111 3111 resource = sa_find_resource(handle, rsrcname);
3112 3112 if (resource == NULL) {
3113 3113 ret = SA_NO_SUCH_RESOURCE;
3114 3114 (void) printf(gettext(
3115 3115 "Resource name not found for share: %s\n"),
3116 3116 rsrcname);
3117 3117 }
3118 3118 }
3119 3119
3120 3120 /*
3121 3121 * Lookup the path in the internal configuration. Care
3122 3122 * must be taken to handle the case where the
3123 3123 * underlying path has been removed since we need to
3124 3124 * be able to deal with that as well.
3125 3125 */
3126 3126 if (ret == SA_OK) {
3127 3127 if (sharepath != NULL) {
3128 3128 if (group != NULL)
3129 3129 share = sa_get_share(group, sharepath);
3130 3130 else
3131 3131 share = sa_find_share(handle, sharepath);
3132 3132 }
3133 3133
3134 3134 if (resource != NULL) {
3135 3135 sa_share_t rsrcshare;
3136 3136 rsrcshare = sa_get_resource_parent(resource);
3137 3137 if (share == NULL)
3138 3138 share = rsrcshare;
3139 3139 else if (share != rsrcshare) {
3140 3140 ret = SA_NO_SUCH_RESOURCE;
3141 3141 (void) printf(gettext(
3142 3142 "Bad resource name for share: %s\n"),
3143 3143 rsrcname);
3144 3144 share = NULL;
3145 3145 }
3146 3146 }
3147 3147
3148 3148 /*
3149 3149 * If we didn't find the share with the provided path,
3150 3150 * it may be a symlink so attempt to resolve it using
3151 3151 * realpath and try again. Realpath will resolve any
3152 3152 * symlinks and place them in "dir". Note that
3153 3153 * sharepath is only used for the lookup the first
3154 3154 * time and later for error messages. dir will be used
3155 3155 * on the second attempt. Once a share is found, all
3156 3156 * operations are based off of the share variable.
3157 3157 */
3158 3158 if (share == NULL) {
3159 3159 if (realpath(sharepath, dir) == NULL) {
3160 3160 ret = SA_BAD_PATH;
3161 3161 (void) printf(gettext(
3162 3162 "Path is not valid: %s\n"), sharepath);
3163 3163 } else {
3164 3164 if (group != NULL)
3165 3165 share = sa_get_share(group, dir);
3166 3166 else
3167 3167 share = sa_find_share(handle, dir);
3168 3168 }
3169 3169 }
3170 3170 }
3171 3171
3172 3172 /*
3173 3173 * If there hasn't been an error, there was likely a
3174 3174 * path found. If not, give the appropriate error
3175 3175 * message and set the return error. If it was found,
3176 3176 * then disable the share and then remove it from the
3177 3177 * configuration.
3178 3178 */
3179 3179 if (ret != SA_OK) {
3180 3180 return (ret);
3181 3181 }
3182 3182 if (share == NULL) {
3183 3183 if (group != NULL)
3184 3184 (void) printf(gettext("Share not found in group %s:"
3185 3185 " %s\n"), argv[optind], sharepath);
3186 3186 else
3187 3187 (void) printf(gettext("Share not found: %s\n"),
3188 3188 sharepath);
3189 3189 ret = SA_NO_SUCH_PATH;
3190 3190 } else {
3191 3191 if (group == NULL)
3192 3192 group = sa_get_parent_group(share);
3193 3193 if (!dryrun) {
3194 3194 if (ret == SA_OK) {
3195 3195 if (resource != NULL)
3196 3196 ret = sa_disable_resource(resource,
3197 3197 NULL);
3198 3198 else
3199 3199 ret = sa_disable_share(share, NULL);
3200 3200 /*
3201 3201 * We don't care if it fails since it
3202 3202 * could be disabled already. Some
3203 3203 * unexpected errors could occur that
3204 3204 * prevent removal, so also check for
3205 3205 * force being set.
3206 3206 */
3207 3207 if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
3208 3208 ret == SA_NOT_SUPPORTED ||
3209 3209 ret == SA_SYSTEM_ERR || force) &&
3210 3210 resource == NULL)
3211 3211 ret = sa_remove_share(share);
3212 3212
3213 3213 if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
3214 3214 ret == SA_NOT_SUPPORTED ||
3215 3215 ret == SA_SYSTEM_ERR || force) &&
3216 3216 resource != NULL) {
3217 3217 ret = sa_remove_resource(resource);
3218 3218 if (ret == SA_OK) {
3219 3219 /*
3220 3220 * If this was the
3221 3221 * last one, remove
3222 3222 * the share as well.
3223 3223 */
3224 3224 resource =
3225 3225 sa_get_share_resource(
3226 3226 share, NULL);
3227 3227 if (resource == NULL)
3228 3228 ret = sa_remove_share(
3229 3229 share);
3230 3230 }
3231 3231 }
3232 3232 if (ret == SA_OK)
3233 3233 ret = sa_update_config(handle);
3234 3234 }
3235 3235 if (ret != SA_OK)
3236 3236 (void) printf(gettext("Could not remove share:"
3237 3237 " %s\n"), sa_errorstr(ret));
3238 3238 } else if (ret == SA_OK) {
3239 3239 char *pname;
3240 3240 pname = sa_get_group_attr(group, "name");
3241 3241 if (pname != NULL) {
3242 3242 auth = check_authorizations(pname, flags);
3243 3243 sa_free_attr_string(pname);
3244 3244 }
3245 3245 if (!auth && verbose) {
3246 3246 (void) printf(gettext(
3247 3247 "Command would fail: %s\n"),
3248 3248 sa_errorstr(SA_NO_PERMISSION));
3249 3249 }
3250 3250 }
3251 3251 }
3252 3252 return (ret);
3253 3253 }
3254 3254
3255 3255 /*
3256 3256 * sa_set_share(flags, argc, argv)
3257 3257 *
3258 3258 * implements set-share subcommand.
3259 3259 */
3260 3260
3261 3261 int
3262 3262 sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
3263 3263 {
3264 3264 int dryrun = 0;
3265 3265 int c;
3266 3266 int ret = SA_OK;
3267 3267 sa_group_t group, sharegroup;
3268 3268 sa_share_t share = NULL;
3269 3269 sa_resource_t resource = NULL;
3270 3270 char *sharepath = NULL;
3271 3271 char *description = NULL;
3272 3272 char *rsrcname = NULL;
3273 3273 char *rsrc = NULL;
3274 3274 char *newname = NULL;
3275 3275 char *newrsrc;
3276 3276 char *groupname = NULL;
3277 3277 int auth;
3278 3278 int verbose = 0;
3279 3279
3280 3280 while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
3281 3281 switch (c) {
3282 3282 case 'n':
3283 3283 dryrun++;
3284 3284 break;
3285 3285 case 'd':
3286 3286 description = optarg;
3287 3287 break;
3288 3288 case 'v':
3289 3289 verbose++;
3290 3290 break;
3291 3291 case 'r':
3292 3292 /*
3293 3293 * Update share by resource name
3294 3294 */
3295 3295 if (rsrcname != NULL) {
3296 3296 (void) printf(gettext(
3297 3297 "Updating multiple resource names not "
3298 3298 "supported\n"));
3299 3299 return (SA_SYNTAX_ERR);
3300 3300 }
3301 3301 rsrcname = optarg;
3302 3302 break;
3303 3303 case 's':
3304 3304 /*
3305 3305 * Save share path into group. Currently limit
3306 3306 * to one share per command.
3307 3307 */
3308 3308 if (sharepath != NULL) {
3309 3309 (void) printf(gettext(
3310 3310 "Updating multiple shares not "
3311 3311 "supported\n"));
3312 3312 return (SA_SYNTAX_ERR);
3313 3313 }
3314 3314 sharepath = optarg;
3315 3315 break;
3316 3316 case 'h':
3317 3317 /* optopt on valid arg isn't defined */
3318 3318 optopt = c;
3319 3319 /*FALLTHROUGH*/
3320 3320 case '?':
3321 3321 default:
3322 3322 /*
3323 3323 * Since a bad option gets to here, sort it
3324 3324 * out and return a syntax error return value
3325 3325 * if necessary.
3326 3326 */
3327 3327 switch (optopt) {
3328 3328 default:
3329 3329 ret = SA_SYNTAX_ERR;
3330 3330 break;
3331 3331 case 'h':
3332 3332 case '?':
3333 3333 break;
3334 3334 }
3335 3335 (void) printf(gettext("usage: %s\n"),
3336 3336 sa_get_usage(USAGE_SET_SHARE));
3337 3337 return (ret);
3338 3338 }
3339 3339 }
3340 3340
3341 3341 if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
3342 3342 if (sharepath == NULL) {
3343 3343 (void) printf(gettext("usage: %s\n"),
3344 3344 sa_get_usage(USAGE_SET_SHARE));
3345 3345 (void) printf(gettext("\tgroup must be specified\n"));
3346 3346 ret = SA_BAD_PATH;
3347 3347 } else {
3348 3348 ret = SA_OK;
3349 3349 }
3350 3350 }
3351 3351 if ((optind + 1) < argc) {
3352 3352 (void) printf(gettext("usage: %s\n"),
3353 3353 sa_get_usage(USAGE_SET_SHARE));
3354 3354 (void) printf(gettext("\tExtraneous group(s) at end\n"));
3355 3355 ret = SA_SYNTAX_ERR;
3356 3356 }
3357 3357
3358 3358 /*
3359 3359 * Must have at least one of sharepath and rsrcrname.
3360 3360 * It is a syntax error to be missing both.
3361 3361 */
3362 3362 if (sharepath == NULL && rsrcname == NULL) {
3363 3363 (void) printf(gettext("usage: %s\n"),
3364 3364 sa_get_usage(USAGE_SET_SHARE));
3365 3365 ret = SA_SYNTAX_ERR;
3366 3366 }
3367 3367
3368 3368 if (ret != SA_OK)
3369 3369 return (ret);
3370 3370
3371 3371 if (optind < argc) {
3372 3372 groupname = argv[optind];
3373 3373 group = sa_get_group(handle, groupname);
3374 3374 } else {
3375 3375 group = NULL;
3376 3376 groupname = NULL;
3377 3377 }
3378 3378 if (rsrcname != NULL) {
3379 3379 /*
3380 3380 * If rsrcname exists, split rename syntax and then
3381 3381 * convert to utf 8 if no errors.
3382 3382 */
3383 3383 newname = strchr(rsrcname, '=');
3384 3384 if (newname != NULL) {
3385 3385 *newname++ = '\0';
3386 3386 }
3387 3387 if (!validresource(rsrcname)) {
3388 3388 ret = SA_INVALID_NAME;
3389 3389 (void) printf(gettext("Invalid resource name: "
3390 3390 "\"%s\"\n"), rsrcname);
3391 3391 } else {
3392 3392 rsrc = conv_to_utf8(rsrcname);
3393 3393 }
3394 3394 if (newname != NULL) {
3395 3395 if (!validresource(newname)) {
3396 3396 ret = SA_INVALID_NAME;
3397 3397 (void) printf(gettext("Invalid resource name: "
3398 3398 "%s\n"), newname);
3399 3399 newname = NULL;
3400 3400 } else {
3401 3401 newrsrc = conv_to_utf8(newname);
3402 3402 }
3403 3403 }
3404 3404 }
3405 3405
3406 3406 if (ret != SA_OK) {
3407 3407 if (rsrcname != NULL && rsrcname != rsrc)
3408 3408 sa_free_attr_string(rsrc);
3409 3409 if (newname != NULL && newname != newrsrc)
3410 3410 sa_free_attr_string(newrsrc);
3411 3411 return (ret);
3412 3412 }
3413 3413
3414 3414 if (sharepath != NULL) {
3415 3415 share = sa_find_share(handle, sharepath);
3416 3416 } else if (rsrcname != NULL) {
3417 3417 resource = sa_find_resource(handle, rsrc);
3418 3418 if (resource != NULL)
3419 3419 share = sa_get_resource_parent(resource);
3420 3420 else
3421 3421 ret = SA_NO_SUCH_RESOURCE;
3422 3422 }
3423 3423 if (share != NULL) {
3424 3424 sharegroup = sa_get_parent_group(share);
3425 3425 if (group != NULL && group != sharegroup) {
3426 3426 (void) printf(gettext("Group \"%s\" does not contain "
3427 3427 "share %s\n"),
3428 3428 argv[optind], sharepath);
3429 3429 ret = SA_BAD_PATH;
3430 3430 } else {
3431 3431 int delgroupname = 0;
3432 3432 if (groupname == NULL) {
3433 3433 groupname = sa_get_group_attr(sharegroup,
3434 3434 "name");
3435 3435 delgroupname = 1;
3436 3436 }
3437 3437 if (groupname != NULL) {
3438 3438 auth = check_authorizations(groupname, flags);
3439 3439 if (delgroupname) {
3440 3440 sa_free_attr_string(groupname);
3441 3441 groupname = NULL;
3442 3442 }
3443 3443 } else {
3444 3444 ret = SA_NO_MEMORY;
3445 3445 }
3446 3446 if (rsrcname != NULL) {
3447 3447 resource = sa_find_resource(handle, rsrc);
3448 3448 if (!dryrun) {
3449 3449 if (newname != NULL &&
3450 3450 resource != NULL)
3451 3451 ret = sa_rename_resource(
3452 3452 resource, newrsrc);
3453 3453 else if (newname != NULL)
3454 3454 ret = SA_NO_SUCH_RESOURCE;
3455 3455 if (newname != NULL &&
3456 3456 newname != newrsrc)
3457 3457 sa_free_attr_string(newrsrc);
3458 3458 }
3459 3459 if (rsrc != rsrcname)
3460 3460 sa_free_attr_string(rsrc);
3461 3461 }
3462 3462
3463 3463 /*
3464 3464 * If the user has set a description, it will be
3465 3465 * on the resource if -r was used otherwise it
3466 3466 * must be on the share.
3467 3467 */
3468 3468 if (!dryrun && ret == SA_OK && description != NULL) {
3469 3469 char *desc;
3470 3470 desc = conv_to_utf8(description);
3471 3471 if (resource != NULL)
3472 3472 ret = sa_set_resource_description(
3473 3473 resource, desc);
3474 3474 else
3475 3475 ret = sa_set_share_description(share,
3476 3476 desc);
3477 3477 if (desc != description)
3478 3478 sa_free_share_description(desc);
3479 3479 }
3480 3480 }
3481 3481 if (!dryrun && ret == SA_OK) {
3482 3482 if (resource != NULL)
3483 3483 (void) sa_enable_resource(resource, NULL);
3484 3484 ret = sa_update_config(handle);
3485 3485 }
3486 3486 switch (ret) {
3487 3487 case SA_DUPLICATE_NAME:
3488 3488 (void) printf(gettext("Resource name in use: %s\n"),
3489 3489 rsrcname);
3490 3490 break;
3491 3491 default:
3492 3492 (void) printf(gettext("Could not set: %s\n"),
3493 3493 sa_errorstr(ret));
3494 3494 break;
3495 3495 case SA_OK:
3496 3496 if (dryrun && !auth && verbose) {
3497 3497 (void) printf(gettext(
3498 3498 "Command would fail: %s\n"),
3499 3499 sa_errorstr(SA_NO_PERMISSION));
3500 3500 }
3501 3501 break;
3502 3502 }
3503 3503 } else {
3504 3504 switch (ret) {
3505 3505 case SA_NO_SUCH_RESOURCE:
3506 3506 (void) printf(gettext("Resource \"%s\" not found\n"),
3507 3507 rsrcname);
3508 3508 break;
3509 3509 default:
3510 3510 if (sharepath != NULL) {
3511 3511 (void) printf(
3512 3512 gettext("Share path \"%s\" not found\n"),
3513 3513 sharepath);
3514 3514 ret = SA_NO_SUCH_PATH;
3515 3515 } else {
3516 3516 (void) printf(gettext("Set failed: %s\n"),
3517 3517 sa_errorstr(ret));
3518 3518 }
3519 3519 }
3520 3520 }
3521 3521
3522 3522 return (ret);
3523 3523 }
3524 3524
3525 3525 /*
3526 3526 * add_security(group, sectype, optlist, proto, *err)
3527 3527 *
3528 3528 * Helper function to add a security option (named optionset) to the
3529 3529 * group.
3530 3530 */
3531 3531
3532 3532 static int
3533 3533 add_security(sa_group_t group, char *sectype,
3534 3534 struct options *optlist, char *proto, int *err)
3535 3535 {
3536 3536 sa_security_t security;
3537 3537 int ret = SA_OK;
3538 3538 int result = 0;
3539 3539 sa_handle_t handle;
3540 3540
3541 3541 sectype = sa_proto_space_alias(proto, sectype);
3542 3542 security = sa_get_security(group, sectype, proto);
3543 3543 if (security == NULL)
3544 3544 security = sa_create_security(group, sectype, proto);
3545 3545
3546 3546 if (sectype != NULL)
3547 3547 sa_free_attr_string(sectype);
3548 3548
3549 3549 if (security == NULL)
3550 3550 goto done;
3551 3551
3552 3552 handle = sa_find_group_handle(group);
3553 3553 if (handle == NULL) {
3554 3554 ret = SA_CONFIG_ERR;
3555 3555 goto done;
3556 3556 }
3557 3557 while (optlist != NULL) {
3558 3558 sa_property_t prop;
3559 3559 prop = sa_get_property(security, optlist->optname);
3560 3560 if (prop == NULL) {
3561 3561 /*
3562 3562 * Add the property, but only if it is
3563 3563 * a non-NULL or non-zero length value
3564 3564 */
3565 3565 if (optlist->optvalue != NULL) {
3566 3566 prop = sa_create_property(optlist->optname,
3567 3567 optlist->optvalue);
3568 3568 if (prop != NULL) {
3569 3569 ret = sa_valid_property(handle,
3570 3570 security, proto, prop);
3571 3571 if (ret != SA_OK) {
3572 3572 (void) sa_remove_property(prop);
3573 3573 (void) printf(gettext(
3574 3574 "Could not add "
3575 3575 "property %s: %s\n"),
3576 3576 optlist->optname,
3577 3577 sa_errorstr(ret));
3578 3578 }
3579 3579 if (ret == SA_OK) {
3580 3580 ret = sa_add_property(security,
3581 3581 prop);
3582 3582 if (ret != SA_OK) {
3583 3583 (void) printf(gettext(
3584 3584 "Could not add "
3585 3585 "property (%s=%s):"
3586 3586 " %s\n"),
3587 3587 optlist->optname,
3588 3588 optlist->optvalue,
3589 3589 sa_errorstr(ret));
3590 3590 } else {
3591 3591 result = 1;
3592 3592 }
3593 3593 }
3594 3594 }
3595 3595 }
3596 3596 } else {
3597 3597 ret = sa_update_property(prop, optlist->optvalue);
3598 3598 result = 1; /* should check if really changed */
3599 3599 }
3600 3600 optlist = optlist->next;
3601 3601 }
3602 3602 /*
3603 3603 * When done, properties may have all been removed but
3604 3604 * we need to keep the security type itself until
3605 3605 * explicitly removed.
3606 3606 */
3607 3607 if (result)
3608 3608 ret = sa_commit_properties(security, 0);
3609 3609 done:
3610 3610 *err = ret;
3611 3611 return (result);
3612 3612 }
3613 3613
3614 3614 /*
3615 3615 * zfscheck(group, share)
3616 3616 *
3617 3617 * For the special case where a share was provided, make sure it is a
3618 3618 * compatible path for a ZFS property change. The only path
3619 3619 * acceptable is the path that defines the zfs sub-group (dataset with
3620 3620 * the sharenfs property set) and not one of the paths that inherited
3621 3621 * the NFS properties. Returns SA_OK if it is usable and
3622 3622 * SA_NOT_ALLOWED if it isn't.
3623 3623 *
3624 3624 * If group is not a ZFS group/subgroup, we assume OK since the check
3625 3625 * on return will catch errors for those cases. What we are looking
3626 3626 * for here is that the group is ZFS and the share is not the defining
3627 3627 * share. All else is SA_OK.
3628 3628 */
3629 3629
3630 3630 static int
3631 3631 zfscheck(sa_group_t group, sa_share_t share)
3632 3632 {
3633 3633 int ret = SA_OK;
3634 3634 char *attr;
3635 3635
3636 3636 if (sa_group_is_zfs(group)) {
3637 3637 /*
3638 3638 * The group is a ZFS group. Does the share represent
3639 3639 * the dataset that defined the group? It is only OK
3640 3640 * if the attribute "subgroup" exists on the share and
3641 3641 * has a value of "true".
3642 3642 */
3643 3643
3644 3644 ret = SA_NOT_ALLOWED;
3645 3645 attr = sa_get_share_attr(share, "subgroup");
3646 3646 if (attr != NULL) {
3647 3647 if (strcmp(attr, "true") == 0)
3648 3648 ret = SA_OK;
3649 3649 sa_free_attr_string(attr);
3650 3650 }
3651 3651 }
3652 3652 return (ret);
3653 3653 }
3654 3654
3655 3655 /*
3656 3656 * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
3657 3657 *
3658 3658 * This function implements "set" when a name space (-S) is not
3659 3659 * specified. It is a basic set. Options and other CLI parsing has
3660 3660 * already been done.
3661 3661 *
3662 3662 * "rsrcname" is a "resource name". If it is non-NULL, it must match
3663 3663 * the sharepath if present or group if present, otherwise it is used
3664 3664 * to set options.
3665 3665 *
3666 3666 * Resource names may take options if the protocol supports it. If the
3667 3667 * protocol doesn't support resource level options, rsrcname is just
3668 3668 * an alias for the share.
3669 3669 */
3670 3670
3671 3671 static int
3672 3672 basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
3673 3673 char *protocol, char *sharepath, char *rsrcname, int dryrun)
3674 3674 {
3675 3675 sa_group_t group;
3676 3676 int ret = SA_OK;
3677 3677 int change = 0;
3678 3678 struct list *worklist = NULL;
3679 3679
3680 3680 group = sa_get_group(handle, groupname);
3681 3681 if (group != NULL) {
3682 3682 sa_share_t share = NULL;
3683 3683 sa_resource_t resource = NULL;
3684 3684
3685 3685 /*
3686 3686 * If there is a sharepath, make sure it belongs to
3687 3687 * the group.
3688 3688 */
3689 3689 if (sharepath != NULL) {
3690 3690 share = sa_get_share(group, sharepath);
3691 3691 if (share == NULL) {
3692 3692 (void) printf(gettext(
3693 3693 "Share does not exist in group %s\n"),
3694 3694 groupname, sharepath);
3695 3695 ret = SA_NO_SUCH_PATH;
3696 3696 } else {
3697 3697 /* if ZFS and OK, then only group */
3698 3698 ret = zfscheck(group, share);
3699 3699 if (ret == SA_OK &&
3700 3700 sa_group_is_zfs(group))
3701 3701 share = NULL;
3702 3702 if (ret == SA_NOT_ALLOWED)
3703 3703 (void) printf(gettext(
3704 3704 "Properties on ZFS group shares "
3705 3705 "not supported: %s\n"), sharepath);
3706 3706 }
3707 3707 }
3708 3708
3709 3709 /*
3710 3710 * If a resource name exists, make sure it belongs to
3711 3711 * the share if present else it belongs to the
3712 3712 * group. Also check the protocol to see if it
3713 3713 * supports resource level properties or not. If not,
3714 3714 * use share only.
3715 3715 */
3716 3716 if (rsrcname != NULL) {
3717 3717 if (share != NULL) {
3718 3718 resource = sa_get_share_resource(share,
3719 3719 rsrcname);
3720 3720 if (resource == NULL)
3721 3721 ret = SA_NO_SUCH_RESOURCE;
3722 3722 } else {
3723 3723 resource = sa_get_resource(group, rsrcname);
3724 3724 if (resource != NULL)
3725 3725 share = sa_get_resource_parent(
3726 3726 resource);
3727 3727 else
3728 3728 ret = SA_NO_SUCH_RESOURCE;
3729 3729 }
3730 3730 if (ret == SA_OK && resource != NULL) {
3731 3731 uint64_t features;
3732 3732 /*
3733 3733 * Check to see if the resource can take
3734 3734 * properties. If so, stick the resource into
3735 3735 * "share" so it will all just work.
3736 3736 */
3737 3737 features = sa_proto_get_featureset(protocol);
3738 3738 if (features & SA_FEATURE_RESOURCE)
3739 3739 share = (sa_share_t)resource;
3740 3740 }
3741 3741 }
3742 3742
3743 3743 if (ret == SA_OK) {
3744 3744 /* group must exist */
3745 3745 ret = valid_options(handle, optlist, protocol,
3746 3746 share == NULL ? group : share, NULL);
3747 3747 if (ret == SA_OK && !dryrun) {
3748 3748 if (share != NULL)
3749 3749 change |= add_optionset(share, optlist,
3750 3750 protocol, &ret);
3751 3751 else
3752 3752 change |= add_optionset(group, optlist,
3753 3753 protocol, &ret);
3754 3754 if (ret == SA_OK && change)
3755 3755 worklist = add_list(worklist, group,
3756 3756 share, protocol);
3757 3757 }
3758 3758 }
3759 3759 free_opt(optlist);
3760 3760 } else {
3761 3761 (void) printf(gettext("Group \"%s\" not found\n"), groupname);
3762 3762 ret = SA_NO_SUCH_GROUP;
3763 3763 }
3764 3764 /*
3765 3765 * we have a group and potentially legal additions
3766 3766 */
3767 3767
3768 3768 /*
3769 3769 * Commit to configuration if not a dryrunp and properties
3770 3770 * have changed.
3771 3771 */
3772 3772 if (!dryrun && ret == SA_OK && change && worklist != NULL)
3773 3773 /* properties changed, so update all shares */
3774 3774 (void) enable_all_groups(handle, worklist, 0, 0, protocol,
3775 3775 B_TRUE);
3776 3776
3777 3777 if (worklist != NULL)
3778 3778 free_list(worklist);
3779 3779 return (ret);
3780 3780 }
3781 3781
3782 3782 /*
3783 3783 * space_set(groupname, optlist, protocol, sharepath, dryrun)
3784 3784 *
3785 3785 * This function implements "set" when a name space (-S) is
3786 3786 * specified. It is a namespace set. Options and other CLI parsing has
3787 3787 * already been done.
3788 3788 */
3789 3789
3790 3790 static int
3791 3791 space_set(sa_handle_t handle, char *groupname, struct options *optlist,
3792 3792 char *protocol, char *sharepath, int dryrun, char *sectype)
3793 3793 {
3794 3794 sa_group_t group;
3795 3795 int ret = SA_OK;
3796 3796 int change = 0;
3797 3797 struct list *worklist = NULL;
3798 3798
3799 3799 /*
3800 3800 * make sure protcol and sectype are valid
3801 3801 */
3802 3802
3803 3803 if (sa_proto_valid_space(protocol, sectype) == 0) {
3804 3804 (void) printf(gettext("Option space \"%s\" not valid "
3805 3805 "for protocol.\n"), sectype);
3806 3806 return (SA_INVALID_SECURITY);
3807 3807 }
3808 3808
3809 3809 group = sa_get_group(handle, groupname);
3810 3810 if (group != NULL) {
3811 3811 sa_share_t share = NULL;
3812 3812 if (sharepath != NULL) {
3813 3813 share = sa_get_share(group, sharepath);
3814 3814 if (share == NULL) {
3815 3815 (void) printf(gettext(
3816 3816 "Share does not exist in group %s\n"),
3817 3817 groupname, sharepath);
3818 3818 ret = SA_NO_SUCH_PATH;
3819 3819 } else {
3820 3820 /* if ZFS and OK, then only group */
3821 3821 ret = zfscheck(group, share);
3822 3822 if (ret == SA_OK &&
3823 3823 sa_group_is_zfs(group))
3824 3824 share = NULL;
3825 3825 if (ret == SA_NOT_ALLOWED)
3826 3826 (void) printf(gettext(
3827 3827 "Properties on ZFS group shares "
3828 3828 "not supported: %s\n"), sharepath);
3829 3829 }
3830 3830 }
3831 3831 if (ret == SA_OK) {
3832 3832 /* group must exist */
3833 3833 ret = valid_options(handle, optlist, protocol,
3834 3834 share == NULL ? group : share, sectype);
3835 3835 if (ret == SA_OK && !dryrun) {
3836 3836 if (share != NULL)
3837 3837 change = add_security(share, sectype,
3838 3838 optlist, protocol, &ret);
3839 3839 else
3840 3840 change = add_security(group, sectype,
3841 3841 optlist, protocol, &ret);
3842 3842 if (ret != SA_OK)
3843 3843 (void) printf(gettext(
3844 3844 "Could not set property: %s\n"),
3845 3845 sa_errorstr(ret));
3846 3846 }
3847 3847 if (ret == SA_OK && change)
3848 3848 worklist = add_list(worklist, group, share,
3849 3849 protocol);
3850 3850 }
3851 3851 free_opt(optlist);
3852 3852 } else {
3853 3853 (void) printf(gettext("Group \"%s\" not found\n"), groupname);
3854 3854 ret = SA_NO_SUCH_GROUP;
3855 3855 }
3856 3856
3857 3857 /*
3858 3858 * We have a group and potentially legal additions.
3859 3859 */
3860 3860
3861 3861 /* Commit to configuration if not a dryrun */
3862 3862 if (!dryrun && ret == 0) {
3863 3863 if (change && worklist != NULL) {
3864 3864 /* properties changed, so update all shares */
3865 3865 (void) enable_all_groups(handle, worklist, 0, 0,
3866 3866 protocol, B_TRUE);
3867 3867 }
3868 3868 ret = sa_update_config(handle);
3869 3869 }
3870 3870 if (worklist != NULL)
3871 3871 free_list(worklist);
3872 3872 return (ret);
3873 3873 }
3874 3874
3875 3875 /*
3876 3876 * sa_set(flags, argc, argv)
3877 3877 *
3878 3878 * Implements the set subcommand. It keys off of -S to determine which
3879 3879 * set of operations to actually do.
3880 3880 */
3881 3881
3882 3882 int
3883 3883 sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
3884 3884 {
3885 3885 char *groupname;
3886 3886 int verbose = 0;
3887 3887 int dryrun = 0;
3888 3888 int c;
3889 3889 char *protocol = NULL;
3890 3890 int ret = SA_OK;
3891 3891 struct options *optlist = NULL;
3892 3892 char *rsrcname = NULL;
3893 3893 char *sharepath = NULL;
3894 3894 char *optset = NULL;
3895 3895 int auth;
3896 3896
3897 3897 while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
3898 3898 switch (c) {
3899 3899 case 'v':
3900 3900 verbose++;
3901 3901 break;
3902 3902 case 'n':
3903 3903 dryrun++;
3904 3904 break;
3905 3905 case 'P':
3906 3906 if (protocol != NULL) {
3907 3907 (void) printf(gettext(
3908 3908 "Specifying multiple protocols "
3909 3909 "not supported: %s\n"), protocol);
3910 3910 return (SA_SYNTAX_ERR);
3911 3911 }
3912 3912 protocol = optarg;
3913 3913 if (!sa_valid_protocol(protocol)) {
3914 3914 (void) printf(gettext(
3915 3915 "Invalid protocol specified: %s\n"),
3916 3916 protocol);
3917 3917 return (SA_INVALID_PROTOCOL);
3918 3918 }
3919 3919 break;
3920 3920 case 'p':
3921 3921 ret = add_opt(&optlist, optarg, 0);
3922 3922 switch (ret) {
3923 3923 case OPT_ADD_SYNTAX:
3924 3924 (void) printf(gettext("Property syntax error:"
3925 3925 " %s\n"), optarg);
3926 3926 return (SA_SYNTAX_ERR);
3927 3927 case OPT_ADD_MEMORY:
3928 3928 (void) printf(gettext("No memory to set "
3929 3929 "property: %s\n"), optarg);
3930 3930 return (SA_NO_MEMORY);
3931 3931 default:
3932 3932 break;
3933 3933 }
3934 3934 break;
3935 3935 case 'r':
3936 3936 if (rsrcname != NULL) {
3937 3937 (void) printf(gettext(
3938 3938 "Setting multiple resource names not"
3939 3939 " supported\n"));
3940 3940 return (SA_SYNTAX_ERR);
3941 3941 }
3942 3942 rsrcname = optarg;
3943 3943 break;
3944 3944 case 's':
3945 3945 if (sharepath != NULL) {
3946 3946 (void) printf(gettext(
3947 3947 "Setting multiple shares not supported\n"));
3948 3948 return (SA_SYNTAX_ERR);
3949 3949 }
3950 3950 sharepath = optarg;
3951 3951 break;
3952 3952 case 'S':
3953 3953 if (optset != NULL) {
3954 3954 (void) printf(gettext(
3955 3955 "Specifying multiple property "
3956 3956 "spaces not supported: %s\n"), optset);
3957 3957 return (SA_SYNTAX_ERR);
3958 3958 }
3959 3959 optset = optarg;
3960 3960 break;
3961 3961 case 'h':
3962 3962 /* optopt on valid arg isn't defined */
3963 3963 optopt = c;
3964 3964 /*FALLTHROUGH*/
3965 3965 case '?':
3966 3966 default:
3967 3967 /*
3968 3968 * Since a bad option gets to here, sort it
3969 3969 * out and return a syntax error return value
3970 3970 * if necessary.
3971 3971 */
3972 3972 switch (optopt) {
3973 3973 default:
3974 3974 ret = SA_SYNTAX_ERR;
3975 3975 break;
3976 3976 case 'h':
3977 3977 case '?':
3978 3978 break;
3979 3979 }
3980 3980 (void) printf(gettext("usage: %s\n"),
3981 3981 sa_get_usage(USAGE_SET));
3982 3982 return (ret);
3983 3983 }
3984 3984 }
3985 3985
3986 3986 if (optlist != NULL)
3987 3987 ret = chk_opt(optlist, optset != NULL, protocol);
3988 3988
3989 3989 if (optind >= argc || (optlist == NULL && optset == NULL) ||
3990 3990 protocol == NULL || ret != OPT_ADD_OK) {
3991 3991 char *sep = "\t";
3992 3992
3993 3993 (void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
3994 3994 if (optind >= argc) {
3995 3995 (void) printf(gettext("%sgroup must be specified"),
3996 3996 sep);
3997 3997 sep = ", ";
3998 3998 }
3999 3999 if (optlist == NULL) {
4000 4000 (void) printf(gettext("%sat least one property must be"
4001 4001 " specified"), sep);
4002 4002 sep = ", ";
4003 4003 }
4004 4004 if (protocol == NULL) {
4005 4005 (void) printf(gettext("%sprotocol must be specified"),
4006 4006 sep);
4007 4007 sep = ", ";
4008 4008 }
4009 4009 (void) printf("\n");
4010 4010 ret = SA_SYNTAX_ERR;
4011 4011 } else {
4012 4012 /*
4013 4013 * Group already exists so we can proceed after a few
4014 4014 * additional checks related to ZFS handling.
4015 4015 */
4016 4016
4017 4017 groupname = argv[optind];
4018 4018 if (strcmp(groupname, "zfs") == 0) {
4019 4019 (void) printf(gettext("Changing properties for group "
4020 4020 "\"zfs\" not allowed\n"));
4021 4021 return (SA_NOT_ALLOWED);
4022 4022 }
4023 4023
4024 4024 auth = check_authorizations(groupname, flags);
4025 4025 if (optset == NULL)
4026 4026 ret = basic_set(handle, groupname, optlist, protocol,
4027 4027 sharepath, rsrcname, dryrun);
4028 4028 else
4029 4029 ret = space_set(handle, groupname, optlist, protocol,
4030 4030 sharepath, dryrun, optset);
4031 4031 if (dryrun && ret == SA_OK && !auth && verbose) {
4032 4032 (void) printf(gettext("Command would fail: %s\n"),
4033 4033 sa_errorstr(SA_NO_PERMISSION));
4034 4034 }
4035 4035 }
4036 4036 return (ret);
4037 4037 }
4038 4038
4039 4039 /*
4040 4040 * remove_options(group, optlist, proto, *err)
4041 4041 *
4042 4042 * Helper function to actually remove options from a group after all
4043 4043 * preprocessing is done.
4044 4044 */
4045 4045
4046 4046 static int
4047 4047 remove_options(sa_group_t group, struct options *optlist,
4048 4048 char *proto, int *err)
4049 4049 {
4050 4050 struct options *cur;
4051 4051 sa_optionset_t optionset;
4052 4052 sa_property_t prop;
4053 4053 int change = 0;
4054 4054 int ret = SA_OK;
4055 4055
4056 4056 optionset = sa_get_optionset(group, proto);
4057 4057 if (optionset != NULL) {
4058 4058 for (cur = optlist; cur != NULL; cur = cur->next) {
4059 4059 prop = sa_get_property(optionset, cur->optname);
4060 4060 if (prop != NULL) {
4061 4061 ret = sa_remove_property(prop);
4062 4062 if (ret != SA_OK)
4063 4063 break;
4064 4064 change = 1;
4065 4065 }
4066 4066 }
4067 4067 }
4068 4068 if (ret == SA_OK && change)
4069 4069 ret = sa_commit_properties(optionset, 0);
4070 4070
4071 4071 if (err != NULL)
4072 4072 *err = ret;
4073 4073 return (change);
4074 4074 }
4075 4075
4076 4076 /*
4077 4077 * valid_unset(group, optlist, proto)
4078 4078 *
4079 4079 * Sanity check the optlist to make sure they can be removed. Issue an
4080 4080 * error if a property doesn't exist.
4081 4081 */
4082 4082
4083 4083 static int
4084 4084 valid_unset(sa_group_t group, struct options *optlist, char *proto)
4085 4085 {
4086 4086 struct options *cur;
4087 4087 sa_optionset_t optionset;
4088 4088 sa_property_t prop;
4089 4089 int ret = SA_OK;
4090 4090
4091 4091 optionset = sa_get_optionset(group, proto);
4092 4092 if (optionset != NULL) {
4093 4093 for (cur = optlist; cur != NULL; cur = cur->next) {
4094 4094 prop = sa_get_property(optionset, cur->optname);
4095 4095 if (prop == NULL) {
4096 4096 (void) printf(gettext(
4097 4097 "Could not unset property %s: not set\n"),
4098 4098 cur->optname);
4099 4099 ret = SA_NO_SUCH_PROP;
4100 4100 }
4101 4101 }
4102 4102 }
4103 4103 return (ret);
4104 4104 }
4105 4105
4106 4106 /*
4107 4107 * valid_unset_security(group, optlist, proto)
4108 4108 *
4109 4109 * Sanity check the optlist to make sure they can be removed. Issue an
4110 4110 * error if a property doesn't exist.
4111 4111 */
4112 4112
4113 4113 static int
4114 4114 valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
4115 4115 char *sectype)
4116 4116 {
4117 4117 struct options *cur;
4118 4118 sa_security_t security;
4119 4119 sa_property_t prop;
4120 4120 int ret = SA_OK;
4121 4121 char *sec;
4122 4122
4123 4123 sec = sa_proto_space_alias(proto, sectype);
4124 4124 security = sa_get_security(group, sec, proto);
4125 4125 if (security != NULL) {
4126 4126 for (cur = optlist; cur != NULL; cur = cur->next) {
4127 4127 prop = sa_get_property(security, cur->optname);
4128 4128 if (prop == NULL) {
4129 4129 (void) printf(gettext(
4130 4130 "Could not unset property %s: not set\n"),
4131 4131 cur->optname);
4132 4132 ret = SA_NO_SUCH_PROP;
4133 4133 }
4134 4134 }
4135 4135 } else {
4136 4136 (void) printf(gettext(
4137 4137 "Could not unset %s: space not defined\n"), sectype);
4138 4138 ret = SA_NO_SUCH_SECURITY;
4139 4139 }
4140 4140 if (sec != NULL)
4141 4141 sa_free_attr_string(sec);
4142 4142 return (ret);
4143 4143 }
4144 4144
4145 4145 /*
4146 4146 * remove_security(group, optlist, proto)
4147 4147 *
4148 4148 * Remove the properties since they were checked as valid.
4149 4149 */
4150 4150
4151 4151 static int
4152 4152 remove_security(sa_group_t group, char *sectype,
4153 4153 struct options *optlist, char *proto, int *err)
4154 4154 {
4155 4155 sa_security_t security;
4156 4156 int ret = SA_OK;
4157 4157 int change = 0;
4158 4158
4159 4159 sectype = sa_proto_space_alias(proto, sectype);
4160 4160 security = sa_get_security(group, sectype, proto);
4161 4161 if (sectype != NULL)
4162 4162 sa_free_attr_string(sectype);
4163 4163
4164 4164 if (security != NULL) {
4165 4165 while (optlist != NULL) {
4166 4166 sa_property_t prop;
4167 4167 prop = sa_get_property(security, optlist->optname);
4168 4168 if (prop != NULL) {
4169 4169 ret = sa_remove_property(prop);
4170 4170 if (ret != SA_OK)
4171 4171 break;
4172 4172 change = 1;
4173 4173 }
4174 4174 optlist = optlist->next;
4175 4175 }
4176 4176 /*
4177 4177 * when done, properties may have all been removed but
4178 4178 * we need to keep the security type itself until
4179 4179 * explicitly removed.
4180 4180 */
4181 4181 if (ret == SA_OK && change)
4182 4182 ret = sa_commit_properties(security, 0);
4183 4183 } else {
4184 4184 ret = SA_NO_SUCH_PROP;
4185 4185 }
4186 4186 if (err != NULL)
4187 4187 *err = ret;
4188 4188 return (change);
4189 4189 }
4190 4190
4191 4191 /*
4192 4192 * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
4193 4193 *
4194 4194 * Unset non-named optionset properties.
4195 4195 */
4196 4196
4197 4197 static int
4198 4198 basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
4199 4199 char *protocol, char *sharepath, char *rsrcname, int dryrun)
4200 4200 {
4201 4201 sa_group_t group;
4202 4202 int ret = SA_OK;
4203 4203 int change = 0;
4204 4204 struct list *worklist = NULL;
4205 4205 sa_share_t share = NULL;
4206 4206 sa_resource_t resource = NULL;
4207 4207
4208 4208 group = sa_get_group(handle, groupname);
4209 4209 if (group == NULL)
4210 4210 return (ret);
4211 4211
4212 4212 /*
4213 4213 * If there is a sharepath, make sure it belongs to
4214 4214 * the group.
4215 4215 */
4216 4216 if (sharepath != NULL) {
4217 4217 share = sa_get_share(group, sharepath);
4218 4218 if (share == NULL) {
4219 4219 (void) printf(gettext(
4220 4220 "Share does not exist in group %s\n"),
4221 4221 groupname, sharepath);
4222 4222 ret = SA_NO_SUCH_PATH;
4223 4223 }
4224 4224 }
4225 4225 /*
4226 4226 * If a resource name exists, make sure it belongs to
4227 4227 * the share if present else it belongs to the
4228 4228 * group. Also check the protocol to see if it
4229 4229 * supports resource level properties or not. If not,
4230 4230 * use share only.
4231 4231 */
4232 4232 if (rsrcname != NULL) {
4233 4233 if (share != NULL) {
4234 4234 resource = sa_get_share_resource(share, rsrcname);
4235 4235 if (resource == NULL)
4236 4236 ret = SA_NO_SUCH_RESOURCE;
4237 4237 } else {
4238 4238 resource = sa_get_resource(group, rsrcname);
4239 4239 if (resource != NULL) {
4240 4240 share = sa_get_resource_parent(resource);
4241 4241 } else {
4242 4242 ret = SA_NO_SUCH_RESOURCE;
4243 4243 }
4244 4244 }
4245 4245 if (ret == SA_OK && resource != NULL) {
4246 4246 uint64_t features;
4247 4247 /*
4248 4248 * Check to see if the resource can take
4249 4249 * properties. If so, stick the resource into
4250 4250 * "share" so it will all just work.
4251 4251 */
4252 4252 features = sa_proto_get_featureset(protocol);
4253 4253 if (features & SA_FEATURE_RESOURCE)
4254 4254 share = (sa_share_t)resource;
4255 4255 }
4256 4256 }
4257 4257
4258 4258 if (ret == SA_OK) {
4259 4259 /* group must exist */
4260 4260 ret = valid_unset(share != NULL ? share : group,
4261 4261 optlist, protocol);
4262 4262 if (ret == SA_OK && !dryrun) {
4263 4263 if (share != NULL) {
4264 4264 sa_optionset_t optionset;
4265 4265 sa_property_t prop;
4266 4266 change |= remove_options(share, optlist,
4267 4267 protocol, &ret);
4268 4268 /*
4269 4269 * If a share optionset is
4270 4270 * empty, remove it.
4271 4271 */
4272 4272 optionset = sa_get_optionset((sa_share_t)share,
4273 4273 protocol);
4274 4274 if (optionset != NULL) {
4275 4275 prop = sa_get_property(optionset, NULL);
4276 4276 if (prop == NULL)
4277 4277 (void) sa_destroy_optionset(
4278 4278 optionset);
4279 4279 }
4280 4280 } else {
4281 4281 change |= remove_options(group,
4282 4282 optlist, protocol, &ret);
4283 4283 }
4284 4284 if (ret == SA_OK && change)
4285 4285 worklist = add_list(worklist, group, share,
4286 4286 protocol);
4287 4287 if (ret != SA_OK)
4288 4288 (void) printf(gettext(
4289 4289 "Could not remove properties: "
4290 4290 "%s\n"), sa_errorstr(ret));
4291 4291 }
4292 4292 } else {
4293 4293 (void) printf(gettext("Group \"%s\" not found\n"), groupname);
4294 4294 ret = SA_NO_SUCH_GROUP;
4295 4295 }
4296 4296 free_opt(optlist);
4297 4297
4298 4298 /*
4299 4299 * We have a group and potentially legal additions
4300 4300 *
4301 4301 * Commit to configuration if not a dryrun
4302 4302 */
4303 4303 if (!dryrun && ret == SA_OK) {
4304 4304 if (change && worklist != NULL) {
4305 4305 /* properties changed, so update all shares */
4306 4306 (void) enable_all_groups(handle, worklist, 0, 0,
4307 4307 protocol, B_TRUE);
4308 4308 }
4309 4309 }
4310 4310 if (worklist != NULL)
4311 4311 free_list(worklist);
4312 4312 return (ret);
4313 4313 }
4314 4314
4315 4315 /*
4316 4316 * space_unset(groupname, optlist, protocol, sharepath, dryrun)
4317 4317 *
4318 4318 * Unset named optionset properties.
4319 4319 */
4320 4320 static int
4321 4321 space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
4322 4322 char *protocol, char *sharepath, int dryrun, char *sectype)
4323 4323 {
4324 4324 sa_group_t group;
4325 4325 int ret = SA_OK;
4326 4326 int change = 0;
4327 4327 struct list *worklist = NULL;
4328 4328 sa_share_t share = NULL;
4329 4329
4330 4330 group = sa_get_group(handle, groupname);
4331 4331 if (group == NULL) {
4332 4332 (void) printf(gettext("Group \"%s\" not found\n"), groupname);
4333 4333 return (SA_NO_SUCH_GROUP);
4334 4334 }
4335 4335 if (sharepath != NULL) {
4336 4336 share = sa_get_share(group, sharepath);
4337 4337 if (share == NULL) {
4338 4338 (void) printf(gettext(
4339 4339 "Share does not exist in group %s\n"),
4340 4340 groupname, sharepath);
4341 4341 return (SA_NO_SUCH_PATH);
4342 4342 }
4343 4343 }
4344 4344 ret = valid_unset_security(share != NULL ? share : group,
4345 4345 optlist, protocol, sectype);
4346 4346
4347 4347 if (ret == SA_OK && !dryrun) {
4348 4348 if (optlist != NULL) {
4349 4349 if (share != NULL) {
4350 4350 sa_security_t optionset;
4351 4351 sa_property_t prop;
4352 4352 change = remove_security(share,
4353 4353 sectype, optlist, protocol, &ret);
4354 4354
4355 4355 /* If a share security is empty, remove it */
4356 4356 optionset = sa_get_security((sa_group_t)share,
4357 4357 sectype, protocol);
4358 4358 if (optionset != NULL) {
4359 4359 prop = sa_get_property(optionset,
4360 4360 NULL);
4361 4361 if (prop == NULL)
4362 4362 ret = sa_destroy_security(
4363 4363 optionset);
4364 4364 }
4365 4365 } else {
4366 4366 change = remove_security(group, sectype,
4367 4367 optlist, protocol, &ret);
4368 4368 }
4369 4369 } else {
4370 4370 sa_security_t security;
4371 4371 char *sec;
4372 4372 sec = sa_proto_space_alias(protocol, sectype);
4373 4373 security = sa_get_security(group, sec, protocol);
4374 4374 if (sec != NULL)
4375 4375 sa_free_attr_string(sec);
4376 4376 if (security != NULL) {
4377 4377 ret = sa_destroy_security(security);
4378 4378 if (ret == SA_OK)
4379 4379 change = 1;
4380 4380 } else {
4381 4381 ret = SA_NO_SUCH_PROP;
4382 4382 }
4383 4383 }
4384 4384 if (ret != SA_OK)
4385 4385 (void) printf(gettext("Could not unset property: %s\n"),
4386 4386 sa_errorstr(ret));
4387 4387 }
4388 4388
4389 4389 if (ret == SA_OK && change)
4390 4390 worklist = add_list(worklist, group, 0, protocol);
4391 4391
4392 4392 free_opt(optlist);
4393 4393 /*
4394 4394 * We have a group and potentially legal additions
4395 4395 */
4396 4396
4397 4397 /* Commit to configuration if not a dryrun */
4398 4398 if (!dryrun && ret == 0) {
4399 4399 /* properties changed, so update all shares */
4400 4400 if (change && worklist != NULL)
4401 4401 (void) enable_all_groups(handle, worklist, 0, 0,
4402 4402 protocol, B_TRUE);
4403 4403 ret = sa_update_config(handle);
4404 4404 }
4405 4405 if (worklist != NULL)
4406 4406 free_list(worklist);
4407 4407 return (ret);
4408 4408 }
4409 4409
4410 4410 /*
4411 4411 * sa_unset(flags, argc, argv)
4412 4412 *
4413 4413 * Implements the unset subcommand. Parsing done here and then basic
4414 4414 * or space versions of the real code are called.
4415 4415 */
4416 4416
4417 4417 int
4418 4418 sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
4419 4419 {
4420 4420 char *groupname;
4421 4421 int verbose = 0;
4422 4422 int dryrun = 0;
4423 4423 int c;
4424 4424 char *protocol = NULL;
4425 4425 int ret = SA_OK;
4426 4426 struct options *optlist = NULL;
4427 4427 char *rsrcname = NULL;
4428 4428 char *sharepath = NULL;
4429 4429 char *optset = NULL;
4430 4430 int auth;
4431 4431
4432 4432 while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
4433 4433 switch (c) {
4434 4434 case 'v':
4435 4435 verbose++;
4436 4436 break;
4437 4437 case 'n':
4438 4438 dryrun++;
4439 4439 break;
4440 4440 case 'P':
4441 4441 if (protocol != NULL) {
4442 4442 (void) printf(gettext(
4443 4443 "Specifying multiple protocols "
4444 4444 "not supported: %s\n"), protocol);
4445 4445 return (SA_SYNTAX_ERR);
4446 4446 }
4447 4447 protocol = optarg;
4448 4448 if (!sa_valid_protocol(protocol)) {
4449 4449 (void) printf(gettext(
4450 4450 "Invalid protocol specified: %s\n"),
4451 4451 protocol);
4452 4452 return (SA_INVALID_PROTOCOL);
4453 4453 }
4454 4454 break;
4455 4455 case 'p':
4456 4456 ret = add_opt(&optlist, optarg, 1);
4457 4457 switch (ret) {
4458 4458 case OPT_ADD_SYNTAX:
4459 4459 (void) printf(gettext("Property syntax error "
4460 4460 "for property %s\n"), optarg);
4461 4461 return (SA_SYNTAX_ERR);
4462 4462
4463 4463 case OPT_ADD_PROPERTY:
4464 4464 (void) printf(gettext("Properties need to be "
4465 4465 "set with set command: %s\n"), optarg);
4466 4466 return (SA_SYNTAX_ERR);
4467 4467
4468 4468 default:
4469 4469 break;
4470 4470 }
4471 4471 break;
4472 4472 case 'r':
4473 4473 /*
4474 4474 * Unset properties on resource if applicable or on
4475 4475 * share if resource for this protocol doesn't use
4476 4476 * resources.
4477 4477 */
4478 4478 if (rsrcname != NULL) {
4479 4479 (void) printf(gettext(
4480 4480 "Unsetting multiple resource "
4481 4481 "names not supported\n"));
4482 4482 return (SA_SYNTAX_ERR);
4483 4483 }
4484 4484 rsrcname = optarg;
4485 4485 break;
4486 4486 case 's':
4487 4487 if (sharepath != NULL) {
4488 4488 (void) printf(gettext(
4489 4489 "Adding multiple shares not supported\n"));
4490 4490 return (SA_SYNTAX_ERR);
4491 4491 }
4492 4492 sharepath = optarg;
4493 4493 break;
4494 4494 case 'S':
4495 4495 if (optset != NULL) {
4496 4496 (void) printf(gettext(
4497 4497 "Specifying multiple property "
4498 4498 "spaces not supported: %s\n"), optset);
4499 4499 return (SA_SYNTAX_ERR);
4500 4500 }
4501 4501 optset = optarg;
4502 4502 break;
4503 4503 case 'h':
4504 4504 /* optopt on valid arg isn't defined */
4505 4505 optopt = c;
4506 4506 /*FALLTHROUGH*/
4507 4507 case '?':
4508 4508 default:
4509 4509 /*
4510 4510 * Since a bad option gets to here, sort it
4511 4511 * out and return a syntax error return value
4512 4512 * if necessary.
4513 4513 */
4514 4514 switch (optopt) {
4515 4515 default:
4516 4516 ret = SA_SYNTAX_ERR;
4517 4517 break;
4518 4518 case 'h':
4519 4519 case '?':
4520 4520 break;
4521 4521 }
4522 4522 (void) printf(gettext("usage: %s\n"),
4523 4523 sa_get_usage(USAGE_UNSET));
4524 4524 return (ret);
4525 4525 }
4526 4526 }
4527 4527
4528 4528 if (optlist != NULL)
4529 4529 ret = chk_opt(optlist, optset != NULL, protocol);
4530 4530
4531 4531 if (optind >= argc || (optlist == NULL && optset == NULL) ||
4532 4532 protocol == NULL) {
4533 4533 char *sep = "\t";
4534 4534 (void) printf(gettext("usage: %s\n"),
4535 4535 sa_get_usage(USAGE_UNSET));
4536 4536 if (optind >= argc) {
4537 4537 (void) printf(gettext("%sgroup must be specified"),
4538 4538 sep);
4539 4539 sep = ", ";
4540 4540 }
4541 4541 if (optlist == NULL) {
4542 4542 (void) printf(gettext("%sat least one property must "
4543 4543 "be specified"), sep);
4544 4544 sep = ", ";
4545 4545 }
4546 4546 if (protocol == NULL) {
4547 4547 (void) printf(gettext("%sprotocol must be specified"),
4548 4548 sep);
4549 4549 sep = ", ";
4550 4550 }
4551 4551 (void) printf("\n");
4552 4552 ret = SA_SYNTAX_ERR;
4553 4553 } else {
4554 4554
4555 4555 /*
4556 4556 * If a group already exists, we can only add a new
4557 4557 * protocol to it and not create a new one or add the
4558 4558 * same protocol again.
4559 4559 */
4560 4560
4561 4561 groupname = argv[optind];
4562 4562 auth = check_authorizations(groupname, flags);
4563 4563 if (optset == NULL)
4564 4564 ret = basic_unset(handle, groupname, optlist, protocol,
4565 4565 sharepath, rsrcname, dryrun);
4566 4566 else
4567 4567 ret = space_unset(handle, groupname, optlist, protocol,
4568 4568 sharepath, dryrun, optset);
4569 4569
4570 4570 if (dryrun && ret == SA_OK && !auth && verbose)
4571 4571 (void) printf(gettext("Command would fail: %s\n"),
4572 4572 sa_errorstr(SA_NO_PERMISSION));
4573 4573 }
4574 4574 return (ret);
4575 4575 }
4576 4576
4577 4577 /*
4578 4578 * sa_enable_group(flags, argc, argv)
4579 4579 *
4580 4580 * Implements the enable subcommand
4581 4581 */
4582 4582
4583 4583 int
4584 4584 sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
4585 4585 {
4586 4586 int verbose = 0;
4587 4587 int dryrun = 0;
4588 4588 int all = 0;
4589 4589 int c;
4590 4590 int ret = SA_OK;
4591 4591 char *protocol = NULL;
4592 4592 char *state;
4593 4593 struct list *worklist = NULL;
4594 4594 int auth = 1;
4595 4595 sa_group_t group;
4596 4596
4597 4597 while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
4598 4598 switch (c) {
4599 4599 case 'a':
4600 4600 all = 1;
4601 4601 break;
4602 4602 case 'n':
4603 4603 dryrun++;
4604 4604 break;
4605 4605 case 'P':
4606 4606 if (protocol != NULL) {
4607 4607 (void) printf(gettext(
4608 4608 "Specifying multiple protocols "
4609 4609 "not supported: %s\n"), protocol);
4610 4610 return (SA_SYNTAX_ERR);
4611 4611 }
4612 4612 protocol = optarg;
4613 4613 if (!sa_valid_protocol(protocol)) {
4614 4614 (void) printf(gettext(
4615 4615 "Invalid protocol specified: %s\n"),
4616 4616 protocol);
4617 4617 return (SA_INVALID_PROTOCOL);
4618 4618 }
4619 4619 break;
4620 4620 case 'v':
4621 4621 verbose++;
4622 4622 break;
4623 4623 case 'h':
4624 4624 /* optopt on valid arg isn't defined */
4625 4625 optopt = c;
4626 4626 /*FALLTHROUGH*/
4627 4627 case '?':
4628 4628 default:
4629 4629 /*
4630 4630 * Since a bad option gets to here, sort it
4631 4631 * out and return a syntax error return value
4632 4632 * if necessary.
4633 4633 */
4634 4634 switch (optopt) {
4635 4635 default:
4636 4636 ret = SA_SYNTAX_ERR;
4637 4637 break;
4638 4638 case 'h':
4639 4639 case '?':
4640 4640 (void) printf(gettext("usage: %s\n"),
4641 4641 sa_get_usage(USAGE_ENABLE));
4642 4642 return (ret);
4643 4643 }
4644 4644 }
4645 4645 }
4646 4646
4647 4647 if (optind == argc && !all) {
4648 4648 (void) printf(gettext("usage: %s\n"),
4649 4649 sa_get_usage(USAGE_ENABLE));
4650 4650 (void) printf(gettext("\tmust specify group\n"));
4651 4651 return (SA_NO_SUCH_PATH);
4652 4652 }
4653 4653 if (!all) {
4654 4654 while (optind < argc) {
4655 4655 group = sa_get_group(handle, argv[optind]);
4656 4656 if (group != NULL) {
4657 4657 auth &= check_authorizations(argv[optind],
4658 4658 flags);
4659 4659 state = sa_get_group_attr(group, "state");
4660 4660 if (state != NULL &&
4661 4661 strcmp(state, "enabled") == 0) {
4662 4662 /* already enabled */
4663 4663 if (verbose)
4664 4664 (void) printf(gettext(
4665 4665 "Group \"%s\" is already "
4666 4666 "enabled\n"),
4667 4667 argv[optind]);
4668 4668 ret = SA_BUSY; /* already enabled */
4669 4669 } else {
4670 4670 worklist = add_list(worklist, group,
4671 4671 0, protocol);
4672 4672 if (verbose)
4673 4673 (void) printf(gettext(
4674 4674 "Enabling group \"%s\"\n"),
4675 4675 argv[optind]);
4676 4676 }
4677 4677 if (state != NULL)
4678 4678 sa_free_attr_string(state);
4679 4679 } else {
4680 4680 ret = SA_NO_SUCH_GROUP;
4681 4681 }
4682 4682 optind++;
4683 4683 }
4684 4684 } else {
4685 4685 for (group = sa_get_group(handle, NULL);
4686 4686 group != NULL;
4687 4687 group = sa_get_next_group(group)) {
4688 4688 worklist = add_list(worklist, group, 0, protocol);
4689 4689 }
4690 4690 }
4691 4691 if (!dryrun && ret == SA_OK)
4692 4692 ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
4693 4693
4694 4694 if (ret != SA_OK && ret != SA_BUSY)
4695 4695 (void) printf(gettext("Could not enable group: %s\n"),
4696 4696 sa_errorstr(ret));
4697 4697 if (ret == SA_BUSY)
4698 4698 ret = SA_OK;
4699 4699
4700 4700 if (worklist != NULL)
4701 4701 free_list(worklist);
4702 4702 if (dryrun && ret == SA_OK && !auth && verbose) {
4703 4703 (void) printf(gettext("Command would fail: %s\n"),
4704 4704 sa_errorstr(SA_NO_PERMISSION));
4705 4705 }
4706 4706 return (ret);
4707 4707 }
4708 4708
4709 4709 /*
4710 4710 * disable_group(group, proto)
4711 4711 *
4712 4712 * Disable all the shares in the specified group.. This is a helper
4713 4713 * for disable_all_groups in order to simplify regular and subgroup
4714 4714 * (zfs) disabling. Group has already been checked for non-NULL.
4715 4715 */
4716 4716
4717 4717 static int
4718 4718 disable_group(sa_group_t group, char *proto)
4719 4719 {
4720 4720 sa_share_t share;
4721 4721 int ret = SA_OK;
4722 4722
4723 4723 /*
4724 4724 * If the protocol isn't enabled, skip it and treat as
4725 4725 * successful.
4726 4726 */
4727 4727 if (!has_protocol(group, proto))
4728 4728 return (ret);
4729 4729
4730 4730 for (share = sa_get_share(group, NULL);
4731 4731 share != NULL && ret == SA_OK;
4732 4732 share = sa_get_next_share(share)) {
4733 4733 ret = sa_disable_share(share, proto);
4734 4734 if (ret == SA_NO_SUCH_PATH) {
4735 4735 /*
4736 4736 * this is OK since the path is gone. we can't
4737 4737 * re-share it anyway so no error.
4738 4738 */
4739 4739 ret = SA_OK;
4740 4740 }
4741 4741 }
4742 4742 return (ret);
4743 4743 }
4744 4744
4745 4745 /*
4746 4746 * disable_all_groups(work, setstate)
4747 4747 *
4748 4748 * helper function that disables the shares in the list of groups
4749 4749 * provided. It optionally marks the group as disabled. Used by both
4750 4750 * enable and start subcommands.
4751 4751 */
4752 4752
4753 4753 static int
4754 4754 disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
4755 4755 {
4756 4756 int ret = SA_OK;
4757 4757 sa_group_t subgroup, group;
4758 4758
4759 4759 while (work != NULL && ret == SA_OK) {
4760 4760 group = (sa_group_t)work->item;
4761 4761 if (setstate)
4762 4762 ret = sa_set_group_attr(group, "state", "disabled");
4763 4763 if (ret == SA_OK) {
4764 4764 char *name;
4765 4765 name = sa_get_group_attr(group, "name");
4766 4766 if (name != NULL && strcmp(name, "zfs") == 0) {
4767 4767 /* need to get the sub-groups for stopping */
4768 4768 for (subgroup = sa_get_sub_group(group);
4769 4769 subgroup != NULL;
4770 4770 subgroup = sa_get_next_group(subgroup)) {
4771 4771 ret = disable_group(subgroup,
4772 4772 work->proto);
4773 4773 }
4774 4774 } else {
4775 4775 ret = disable_group(group, work->proto);
4776 4776 }
4777 4777 if (name != NULL)
4778 4778 sa_free_attr_string(name);
4779 4779 /*
4780 4780 * We don't want to "disable" since it won't come
4781 4781 * up after a reboot. The SMF framework should do
4782 4782 * the right thing. On enable we do want to do
4783 4783 * something.
4784 4784 */
4785 4785 }
4786 4786 work = work->next;
4787 4787 }
4788 4788 if (ret == SA_OK)
4789 4789 ret = sa_update_config(handle);
4790 4790 return (ret);
4791 4791 }
4792 4792
4793 4793 /*
4794 4794 * sa_disable_group(flags, argc, argv)
4795 4795 *
4796 4796 * Implements the disable subcommand
4797 4797 */
4798 4798
4799 4799 int
4800 4800 sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
4801 4801 {
4802 4802 int verbose = 0;
4803 4803 int dryrun = 0;
4804 4804 int all = 0;
4805 4805 int c;
4806 4806 int ret = SA_OK;
4807 4807 char *protocol = NULL;
4808 4808 char *state;
4809 4809 struct list *worklist = NULL;
4810 4810 sa_group_t group;
4811 4811 int auth = 1;
4812 4812
4813 4813 while ((c = getopt(argc, argv, "?havn")) != EOF) {
4814 4814 switch (c) {
4815 4815 case 'a':
4816 4816 all = 1;
4817 4817 break;
4818 4818 case 'n':
4819 4819 dryrun++;
4820 4820 break;
4821 4821 case 'P':
4822 4822 if (protocol != NULL) {
4823 4823 (void) printf(gettext(
4824 4824 "Specifying multiple protocols "
4825 4825 "not supported: %s\n"), protocol);
4826 4826 return (SA_SYNTAX_ERR);
4827 4827 }
4828 4828 protocol = optarg;
4829 4829 if (!sa_valid_protocol(protocol)) {
4830 4830 (void) printf(gettext(
4831 4831 "Invalid protocol specified: %s\n"),
4832 4832 protocol);
4833 4833 return (SA_INVALID_PROTOCOL);
4834 4834 }
4835 4835 break;
4836 4836 case 'v':
4837 4837 verbose++;
4838 4838 break;
4839 4839 case 'h':
4840 4840 /* optopt on valid arg isn't defined */
4841 4841 optopt = c;
4842 4842 /*FALLTHROUGH*/
4843 4843 case '?':
4844 4844 default:
4845 4845 /*
4846 4846 * Since a bad option gets to here, sort it
4847 4847 * out and return a syntax error return value
4848 4848 * if necessary.
4849 4849 */
4850 4850 switch (optopt) {
4851 4851 default:
4852 4852 ret = SA_SYNTAX_ERR;
4853 4853 break;
4854 4854 case 'h':
4855 4855 case '?':
4856 4856 break;
4857 4857 }
4858 4858 (void) printf(gettext("usage: %s\n"),
4859 4859 sa_get_usage(USAGE_DISABLE));
4860 4860 return (ret);
4861 4861 }
4862 4862 }
4863 4863
4864 4864 if (optind == argc && !all) {
4865 4865 (void) printf(gettext("usage: %s\n"),
4866 4866 sa_get_usage(USAGE_DISABLE));
4867 4867 (void) printf(gettext("\tmust specify group\n"));
4868 4868 return (SA_NO_SUCH_PATH);
4869 4869 }
4870 4870 if (!all) {
4871 4871 while (optind < argc) {
4872 4872 group = sa_get_group(handle, argv[optind]);
4873 4873 if (group != NULL) {
4874 4874 auth &= check_authorizations(argv[optind],
4875 4875 flags);
4876 4876 state = sa_get_group_attr(group, "state");
4877 4877 if (state == NULL ||
4878 4878 strcmp(state, "disabled") == 0) {
4879 4879 /* already disabled */
4880 4880 if (verbose)
4881 4881 (void) printf(gettext(
4882 4882 "Group \"%s\" is "
4883 4883 "already disabled\n"),
4884 4884 argv[optind]);
4885 4885 ret = SA_BUSY; /* already disabled */
4886 4886 } else {
4887 4887 worklist = add_list(worklist, group, 0,
4888 4888 protocol);
4889 4889 if (verbose)
4890 4890 (void) printf(gettext(
4891 4891 "Disabling group "
4892 4892 "\"%s\"\n"), argv[optind]);
4893 4893 }
4894 4894 if (state != NULL)
4895 4895 sa_free_attr_string(state);
4896 4896 } else {
4897 4897 ret = SA_NO_SUCH_GROUP;
4898 4898 }
4899 4899 optind++;
4900 4900 }
4901 4901 } else {
4902 4902 for (group = sa_get_group(handle, NULL);
4903 4903 group != NULL;
4904 4904 group = sa_get_next_group(group))
4905 4905 worklist = add_list(worklist, group, 0, protocol);
4906 4906 }
4907 4907
4908 4908 if (ret == SA_OK && !dryrun)
4909 4909 ret = disable_all_groups(handle, worklist, 1);
4910 4910 if (ret != SA_OK && ret != SA_BUSY)
4911 4911 (void) printf(gettext("Could not disable group: %s\n"),
4912 4912 sa_errorstr(ret));
4913 4913 if (ret == SA_BUSY)
4914 4914 ret = SA_OK;
4915 4915 if (worklist != NULL)
4916 4916 free_list(worklist);
4917 4917 if (dryrun && ret == SA_OK && !auth && verbose)
4918 4918 (void) printf(gettext("Command would fail: %s\n"),
4919 4919 sa_errorstr(SA_NO_PERMISSION));
4920 4920 return (ret);
4921 4921 }
4922 4922
4923 4923 /*
4924 4924 * sa_start_group(flags, argc, argv)
4925 4925 *
4926 4926 * Implements the start command.
4927 4927 * This is similar to enable except it doesn't change the state
4928 4928 * of the group(s) and only enables shares if the group is already
4929 4929 * enabled.
4930 4930 */
4931 4931
4932 4932 int
4933 4933 sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
4934 4934 {
4935 4935 int verbose = 0;
4936 4936 int all = 0;
4937 4937 int c;
4938 4938 int ret = SMF_EXIT_OK;
4939 4939 char *protocol = NULL;
4940 4940 char *state;
4941 4941 struct list *worklist = NULL;
4942 4942 sa_group_t group;
4943 4943 #ifdef lint
4944 4944 flags = flags;
4945 4945 #endif
4946 4946
4947 4947 while ((c = getopt(argc, argv, "?havP:")) != EOF) {
4948 4948 switch (c) {
4949 4949 case 'a':
4950 4950 all = 1;
4951 4951 break;
4952 4952 case 'P':
4953 4953 if (protocol != NULL) {
4954 4954 (void) printf(gettext(
4955 4955 "Specifying multiple protocols "
4956 4956 "not supported: %s\n"), protocol);
4957 4957 return (SA_SYNTAX_ERR);
4958 4958 }
4959 4959 protocol = optarg;
4960 4960 if (!sa_valid_protocol(protocol)) {
4961 4961 (void) printf(gettext(
4962 4962 "Invalid protocol specified: %s\n"),
4963 4963 protocol);
4964 4964 return (SA_INVALID_PROTOCOL);
4965 4965 }
4966 4966 break;
4967 4967 case 'v':
4968 4968 verbose++;
4969 4969 break;
4970 4970 case 'h':
4971 4971 /* optopt on valid arg isn't defined */
4972 4972 optopt = c;
4973 4973 /*FALLTHROUGH*/
4974 4974 case '?':
4975 4975 default:
4976 4976 /*
4977 4977 * Since a bad option gets to here, sort it
4978 4978 * out and return a syntax error return value
4979 4979 * if necessary.
4980 4980 */
4981 4981 ret = SA_OK;
4982 4982 switch (optopt) {
4983 4983 default:
4984 4984 ret = SA_SYNTAX_ERR;
4985 4985 break;
4986 4986 case 'h':
4987 4987 case '?':
4988 4988 break;
4989 4989 }
4990 4990 (void) printf(gettext("usage: %s\n"),
4991 4991 sa_get_usage(USAGE_START));
4992 4992 return (ret);
4993 4993 }
4994 4994 }
4995 4995
4996 4996 if (optind == argc && !all) {
4997 4997 (void) printf(gettext("usage: %s\n"),
4998 4998 sa_get_usage(USAGE_START));
4999 4999 return (SMF_EXIT_ERR_FATAL);
5000 5000 }
5001 5001
5002 5002 if (!all) {
5003 5003 while (optind < argc) {
5004 5004 group = sa_get_group(handle, argv[optind]);
5005 5005 if (group != NULL) {
5006 5006 state = sa_get_group_attr(group, "state");
5007 5007 if (state == NULL ||
5008 5008 strcmp(state, "enabled") == 0) {
5009 5009 worklist = add_list(worklist, group, 0,
5010 5010 protocol);
5011 5011 if (verbose)
5012 5012 (void) printf(gettext(
5013 5013 "Starting group \"%s\"\n"),
5014 5014 argv[optind]);
5015 5015 } else {
5016 5016 /*
5017 5017 * Determine if there are any
5018 5018 * protocols. If there aren't any,
5019 5019 * then there isn't anything to do in
5020 5020 * any case so no error.
5021 5021 */
5022 5022 if (sa_get_optionset(group,
5023 5023 protocol) != NULL) {
5024 5024 ret = SMF_EXIT_OK;
5025 5025 }
5026 5026 }
5027 5027 if (state != NULL)
5028 5028 sa_free_attr_string(state);
5029 5029 }
5030 5030 optind++;
5031 5031 }
5032 5032 } else {
5033 5033 for (group = sa_get_group(handle, NULL);
5034 5034 group != NULL;
5035 5035 group = sa_get_next_group(group)) {
5036 5036 state = sa_get_group_attr(group, "state");
5037 5037 if (state == NULL || strcmp(state, "enabled") == 0)
5038 5038 worklist = add_list(worklist, group, 0,
5039 5039 protocol);
5040 5040 if (state != NULL)
5041 5041 sa_free_attr_string(state);
5042 5042 }
5043 5043 }
5044 5044
5045 5045 (void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
5046 5046
5047 5047 if (worklist != NULL)
5048 5048 free_list(worklist);
5049 5049 return (ret);
5050 5050 }
5051 5051
5052 5052 /*
5053 5053 * sa_stop_group(flags, argc, argv)
5054 5054 *
5055 5055 * Implements the stop command.
5056 5056 * This is similar to disable except it doesn't change the state
5057 5057 * of the group(s) and only disables shares if the group is already
5058 5058 * enabled.
5059 5059 */
5060 5060 int
5061 5061 sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
5062 5062 {
5063 5063 int verbose = 0;
5064 5064 int all = 0;
5065 5065 int c;
5066 5066 int ret = SMF_EXIT_OK;
5067 5067 char *protocol = NULL;
5068 5068 char *state;
5069 5069 struct list *worklist = NULL;
5070 5070 sa_group_t group;
5071 5071 #ifdef lint
5072 5072 flags = flags;
5073 5073 #endif
5074 5074
5075 5075 while ((c = getopt(argc, argv, "?havP:")) != EOF) {
5076 5076 switch (c) {
5077 5077 case 'a':
5078 5078 all = 1;
5079 5079 break;
5080 5080 case 'P':
5081 5081 if (protocol != NULL) {
5082 5082 (void) printf(gettext(
5083 5083 "Specifying multiple protocols "
5084 5084 "not supported: %s\n"), protocol);
5085 5085 return (SA_SYNTAX_ERR);
5086 5086 }
5087 5087 protocol = optarg;
5088 5088 if (!sa_valid_protocol(protocol)) {
5089 5089 (void) printf(gettext(
5090 5090 "Invalid protocol specified: %s\n"),
5091 5091 protocol);
5092 5092 return (SA_INVALID_PROTOCOL);
5093 5093 }
5094 5094 break;
5095 5095 case 'v':
5096 5096 verbose++;
5097 5097 break;
5098 5098 case 'h':
5099 5099 /* optopt on valid arg isn't defined */
5100 5100 optopt = c;
5101 5101 /*FALLTHROUGH*/
5102 5102 case '?':
5103 5103 default:
5104 5104 /*
5105 5105 * Since a bad option gets to here, sort it
5106 5106 * out and return a syntax error return value
5107 5107 * if necessary.
5108 5108 */
5109 5109 ret = SA_OK;
5110 5110 switch (optopt) {
5111 5111 default:
5112 5112 ret = SA_SYNTAX_ERR;
5113 5113 break;
5114 5114 case 'h':
5115 5115 case '?':
5116 5116 break;
5117 5117 }
5118 5118 (void) printf(gettext("usage: %s\n"),
5119 5119 sa_get_usage(USAGE_STOP));
5120 5120 return (ret);
5121 5121 }
5122 5122 }
5123 5123
5124 5124 if (optind == argc && !all) {
5125 5125 (void) printf(gettext("usage: %s\n"),
5126 5126 sa_get_usage(USAGE_STOP));
5127 5127 return (SMF_EXIT_ERR_FATAL);
5128 5128 } else if (!all) {
5129 5129 while (optind < argc) {
5130 5130 group = sa_get_group(handle, argv[optind]);
5131 5131 if (group != NULL) {
5132 5132 state = sa_get_group_attr(group, "state");
5133 5133 if (state == NULL ||
5134 5134 strcmp(state, "enabled") == 0) {
5135 5135 worklist = add_list(worklist, group, 0,
5136 5136 protocol);
5137 5137 if (verbose)
5138 5138 (void) printf(gettext(
5139 5139 "Stopping group \"%s\"\n"),
5140 5140 argv[optind]);
5141 5141 } else {
5142 5142 ret = SMF_EXIT_OK;
5143 5143 }
5144 5144 if (state != NULL)
5145 5145 sa_free_attr_string(state);
5146 5146 }
5147 5147 optind++;
5148 5148 }
5149 5149 } else {
5150 5150 for (group = sa_get_group(handle, NULL);
5151 5151 group != NULL;
5152 5152 group = sa_get_next_group(group)) {
5153 5153 state = sa_get_group_attr(group, "state");
5154 5154 if (state == NULL || strcmp(state, "enabled") == 0)
5155 5155 worklist = add_list(worklist, group, 0,
5156 5156 protocol);
5157 5157 if (state != NULL)
5158 5158 sa_free_attr_string(state);
5159 5159 }
5160 5160 }
5161 5161 (void) disable_all_groups(handle, worklist, 0);
5162 5162 ret = sa_update_config(handle);
5163 5163
5164 5164 if (worklist != NULL)
5165 5165 free_list(worklist);
5166 5166 return (ret);
5167 5167 }
5168 5168
5169 5169 /*
5170 5170 * remove_all_options(share, proto)
5171 5171 *
5172 5172 * Removes all options on a share.
5173 5173 */
5174 5174
5175 5175 static void
5176 5176 remove_all_options(sa_share_t share, char *proto)
5177 5177 {
5178 5178 sa_optionset_t optionset;
5179 5179 sa_security_t security;
5180 5180 sa_security_t prevsec = NULL;
5181 5181
5182 5182 optionset = sa_get_optionset(share, proto);
5183 5183 if (optionset != NULL)
5184 5184 (void) sa_destroy_optionset(optionset);
5185 5185 for (security = sa_get_security(share, NULL, NULL);
5186 5186 security != NULL;
5187 5187 security = sa_get_next_security(security)) {
5188 5188 char *type;
5189 5189 /*
5190 5190 * We walk through the list. prevsec keeps the
5191 5191 * previous security so we can delete it without
5192 5192 * destroying the list.
5193 5193 */
5194 5194 if (prevsec != NULL) {
5195 5195 /* remove the previously seen security */
5196 5196 (void) sa_destroy_security(prevsec);
5197 5197 /* set to NULL so we don't try multiple times */
5198 5198 prevsec = NULL;
5199 5199 }
5200 5200 type = sa_get_security_attr(security, "type");
5201 5201 if (type != NULL) {
5202 5202 /*
5203 5203 * if the security matches the specified protocol, we
5204 5204 * want to remove it. prevsec holds it until either
5205 5205 * the next pass or we fall out of the loop.
5206 5206 */
5207 5207 if (strcmp(type, proto) == 0)
5208 5208 prevsec = security;
5209 5209 sa_free_attr_string(type);
5210 5210 }
5211 5211 }
5212 5212 /* in case there is one left */
5213 5213 if (prevsec != NULL)
5214 5214 (void) sa_destroy_security(prevsec);
5215 5215 }
5216 5216
5217 5217
5218 5218 /*
5219 5219 * for legacy support, we need to handle the old syntax. This is what
5220 5220 * we get if sharemgr is called with the name "share" rather than
5221 5221 * sharemgr.
5222 5222 */
5223 5223
5224 5224 static int
5225 5225 format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
5226 5226 {
5227 5227 int err;
5228 5228
5229 5229 err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
5230 5230 if (err > buffsize)
5231 5231 return (-1);
5232 5232 return (0);
5233 5233 }
5234 5234
5235 5235
5236 5236 /*
5237 5237 * check_legacy_cmd(proto, cmd)
5238 5238 *
5239 5239 * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is
5240 5240 * executable.
5241 5241 */
5242 5242
5243 5243 static int
5244 5244 check_legacy_cmd(char *path)
5245 5245 {
5246 5246 struct stat st;
5247 5247 int ret = 0;
5248 5248
5249 5249 if (stat(path, &st) == 0) {
5250 5250 if (S_ISREG(st.st_mode) &&
5251 5251 st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
5252 5252 ret = 1;
5253 5253 }
5254 5254 return (ret);
5255 5255 }
5256 5256
5257 5257 /*
5258 5258 * run_legacy_command(proto, cmd, argv)
5259 5259 *
5260 5260 * We know the command exists, so attempt to execute it with all the
5261 5261 * arguments. This implements full legacy share support for those
5262 5262 * protocols that don't have plugin providers.
5263 5263 */
5264 5264
5265 5265 static int
5266 5266 run_legacy_command(char *path, char *argv[])
5267 5267 {
5268 5268 int ret;
5269 5269
5270 5270 ret = execv(path, argv);
5271 5271 if (ret < 0) {
5272 5272 switch (errno) {
5273 5273 case EACCES:
5274 5274 ret = SA_NO_PERMISSION;
5275 5275 break;
5276 5276 default:
5277 5277 ret = SA_SYSTEM_ERR;
5278 5278 break;
5279 5279 }
5280 5280 }
5281 5281 return (ret);
5282 5282 }
5283 5283
5284 5284 /*
5285 5285 * out_share(out, group, proto)
5286 5286 *
5287 5287 * Display the share information in the format that the "share"
5288 5288 * command has traditionally used.
5289 5289 */
5290 5290
5291 5291 static void
5292 5292 out_share(FILE *out, sa_group_t group, char *proto)
5293 5293 {
5294 5294 sa_share_t share;
5295 5295 char resfmt[128];
5296 5296 char *defprop;
5297 5297
5298 5298 /*
5299 5299 * The original share command defaulted to displaying NFS
5300 5300 * shares or allowed a protocol to be specified. We want to
5301 5301 * skip those shares that are not the specified protocol.
5302 5302 */
5303 5303 if (proto != NULL && sa_get_optionset(group, proto) == NULL)
5304 5304 return;
5305 5305
5306 5306 if (proto == NULL)
5307 5307 proto = "nfs";
5308 5308
5309 5309 /*
5310 5310 * get the default property string. NFS uses "rw" but
5311 5311 * everything else will use "".
5312 5312 */
5313 5313 if (proto != NULL && strcmp(proto, "nfs") != 0)
5314 5314 defprop = "\"\"";
5315 5315 else
5316 5316 defprop = "rw";
5317 5317
5318 5318 for (share = sa_get_share(group, NULL);
5319 5319 share != NULL;
5320 5320 share = sa_get_next_share(share)) {
5321 5321 char *path;
5322 5322 char *type;
5323 5323 char *resource;
5324 5324 char *description;
5325 5325 char *groupname;
5326 5326 char *sharedstate;
5327 5327 int shared = 1;
5328 5328 char *soptions;
5329 5329 char shareopts[MAXNAMLEN];
5330 5330
5331 5331 sharedstate = sa_get_share_attr(share, "shared");
5332 5332 path = sa_get_share_attr(share, "path");
5333 5333 type = sa_get_share_attr(share, "type");
5334 5334 resource = get_resource(share);
5335 5335 groupname = sa_get_group_attr(group, "name");
5336 5336
5337 5337 if (groupname != NULL && strcmp(groupname, "default") == 0) {
5338 5338 sa_free_attr_string(groupname);
5339 5339 groupname = NULL;
5340 5340 }
5341 5341 description = sa_get_share_description(share);
5342 5342
5343 5343 /*
5344 5344 * Want the sharetab version if it exists, defaulting
5345 5345 * to NFS if no protocol specified.
5346 5346 */
5347 5347 (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
5348 5348 soptions = sa_get_share_attr(share, shareopts);
5349 5349
5350 5350 if (sharedstate == NULL)
5351 5351 shared = 0;
5352 5352
5353 5353 if (soptions == NULL)
5354 5354 soptions = sa_proto_legacy_format(proto, share, 1);
5355 5355
5356 5356 if (shared) {
5357 5357 /* only active shares go here */
5358 5358 (void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
5359 5359 resource != NULL ? resource : "-",
5360 5360 groupname != NULL ? "@" : "",
5361 5361 groupname != NULL ? groupname : "");
5362 5362 (void) fprintf(out, "%-14.14s %s %s \"%s\" \n",
5363 5363 resfmt, (path != NULL) ? path : "",
5364 5364 (soptions != NULL && strlen(soptions) > 0) ?
5365 5365 soptions : defprop,
5366 5366 (description != NULL) ? description : "");
5367 5367 }
5368 5368
5369 5369 if (path != NULL)
5370 5370 sa_free_attr_string(path);
5371 5371 if (type != NULL)
5372 5372 sa_free_attr_string(type);
5373 5373 if (resource != NULL)
5374 5374 sa_free_attr_string(resource);
5375 5375 if (groupname != NULL)
5376 5376 sa_free_attr_string(groupname);
5377 5377 if (description != NULL)
5378 5378 sa_free_share_description(description);
5379 5379 if (sharedstate != NULL)
5380 5380 sa_free_attr_string(sharedstate);
5381 5381 if (soptions != NULL)
5382 5382 sa_format_free(soptions);
5383 5383 }
5384 5384 }
5385 5385
5386 5386 /*
5387 5387 * output_legacy_file(out, proto)
5388 5388 *
5389 5389 * Walk all of the groups for the specified protocol and call
5390 5390 * out_share() to format and write in the format displayed by the
5391 5391 * "share" command with no arguments.
5392 5392 */
5393 5393
5394 5394 static void
5395 5395 output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
5396 5396 {
5397 5397 sa_group_t group;
5398 5398
5399 5399 for (group = sa_get_group(handle, NULL);
5400 5400 group != NULL;
5401 5401 group = sa_get_next_group(group)) {
5402 5402 char *zfs;
5403 5403
5404 5404 /*
5405 5405 * Go through all the groups and ZFS
5406 5406 * sub-groups. out_share() will format the shares in
5407 5407 * the group appropriately.
5408 5408 */
5409 5409
5410 5410 zfs = sa_get_group_attr(group, "zfs");
5411 5411 if (zfs != NULL) {
5412 5412 sa_group_t zgroup;
5413 5413 sa_free_attr_string(zfs);
5414 5414 for (zgroup = sa_get_sub_group(group);
5415 5415 zgroup != NULL;
5416 5416 zgroup = sa_get_next_group(zgroup)) {
5417 5417
5418 5418 /* got a group, so display it */
5419 5419 out_share(out, zgroup, proto);
5420 5420 }
5421 5421 } else {
5422 5422 out_share(out, group, proto);
5423 5423 }
5424 5424 }
5425 5425 }
5426 5426
5427 5427 int
5428 5428 sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
5429 5429 {
5430 5430 char *protocol = "nfs";
5431 5431 char *options = NULL;
5432 5432 char *description = NULL;
5433 5433 char *groupname = NULL;
5434 5434 char *sharepath = NULL;
5435 5435 char *resource = NULL;
5436 5436 char *groupstatus = NULL;
5437 5437 int persist = SA_SHARE_TRANSIENT;
5438 5438 int argsused = 0;
5439 5439 int c;
5440 5440 int ret = SA_OK;
5441 5441 int zfs = 0;
5442 5442 int true_legacy = 0;
5443 5443 int curtype = SA_SHARE_TRANSIENT;
5444 5444 char cmd[MAXPATHLEN];
5445 5445 sa_group_t group = NULL;
5446 5446 sa_resource_t rsrc = NULL;
5447 5447 sa_share_t share;
5448 5448 char dir[MAXPATHLEN];
5449 5449 uint64_t features;
5450 5450 #ifdef lint
5451 5451 flags = flags;
5452 5452 #endif
5453 5453
5454 5454 while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
5455 5455 switch (c) {
5456 5456 case 'd':
5457 5457 description = optarg;
5458 5458 argsused++;
5459 5459 break;
5460 5460 case 'F':
5461 5461 protocol = optarg;
5462 5462 if (!sa_valid_protocol(protocol)) {
5463 5463 if (format_legacy_path(cmd, MAXPATHLEN,
5464 5464 protocol, "share") == 0 &&
5465 5465 check_legacy_cmd(cmd)) {
5466 5466 true_legacy++;
5467 5467 } else {
5468 5468 (void) fprintf(stderr, gettext(
5469 5469 "Invalid protocol specified: "
5470 5470 "%s\n"), protocol);
5471 5471 return (SA_INVALID_PROTOCOL);
5472 5472 }
5473 5473 }
5474 5474 break;
5475 5475 case 'o':
5476 5476 options = optarg;
5477 5477 argsused++;
5478 5478 break;
5479 5479 case 'p':
5480 5480 persist = SA_SHARE_PERMANENT;
5481 5481 argsused++;
5482 5482 break;
5483 5483 case 'h':
5484 5484 /* optopt on valid arg isn't defined */
5485 5485 optopt = c;
5486 5486 /*FALLTHROUGH*/
5487 5487 case '?':
5488 5488 default:
5489 5489 /*
5490 5490 * Since a bad option gets to here, sort it
5491 5491 * out and return a syntax error return value
5492 5492 * if necessary.
5493 5493 */
5494 5494 switch (optopt) {
5495 5495 default:
5496 5496 ret = SA_LEGACY_ERR;
5497 5497 break;
5498 5498 case 'h':
5499 5499 case '?':
5500 5500 break;
5501 5501 }
5502 5502 (void) fprintf(stderr, gettext("usage: %s\n"),
5503 5503 sa_get_usage(USAGE_SHARE));
5504 5504 return (ret);
5505 5505 }
5506 5506 }
5507 5507
5508 5508 /* Have the info so construct what is needed */
5509 5509 if (!argsused && optind == argc) {
5510 5510 /* display current info in share format */
5511 5511 (void) output_legacy_file(stdout, protocol, handle);
5512 5512 return (ret);
5513 5513 }
5514 5514
5515 5515 /* We are modifying the configuration */
5516 5516 if (optind == argc) {
5517 5517 (void) fprintf(stderr, gettext("usage: %s\n"),
5518 5518 sa_get_usage(USAGE_SHARE));
5519 5519 return (SA_LEGACY_ERR);
5520 5520 }
5521 5521 if (true_legacy) {
5522 5522 /* If still using legacy share/unshare, exec it */
5523 5523 ret = run_legacy_command(cmd, argv);
5524 5524 return (ret);
5525 5525 }
5526 5526
5527 5527 sharepath = argv[optind++];
5528 5528 if (optind < argc) {
5529 5529 resource = argv[optind];
5530 5530 groupname = strchr(resource, '@');
5531 5531 if (groupname != NULL)
5532 5532 *groupname++ = '\0';
5533 5533 }
5534 5534 if (realpath(sharepath, dir) == NULL)
5535 5535 ret = SA_BAD_PATH;
5536 5536 else
5537 5537 sharepath = dir;
5538 5538 if (ret == SA_OK)
5539 5539 share = sa_find_share(handle, sharepath);
5540 5540 else
5541 5541 share = NULL;
5542 5542
5543 5543 features = sa_proto_get_featureset(protocol);
5544 5544
5545 5545 if (groupname != NULL) {
5546 5546 ret = SA_NOT_ALLOWED;
5547 5547 } else if (ret == SA_OK) {
5548 5548 char *legacygroup;
5549 5549 /*
5550 5550 * The legacy group is always present and zfs groups
5551 5551 * come and go. zfs shares may be in sub-groups and
5552 5552 * the zfs share will already be in that group so it
5553 5553 * isn't an error. If the protocol is "smb", the group
5554 5554 * "smb" is used when "default" would otherwise be
5555 5555 * used. "default" is NFS only and "smb" is SMB only.
5556 5556 */
5557 5557 if (strcmp(protocol, "smb") == 0)
5558 5558 legacygroup = "smb";
5559 5559 else
5560 5560 legacygroup = "default";
5561 5561
5562 5562 /*
5563 5563 * If the share exists (not NULL), then make sure it
5564 5564 * is one we want to handle by getting the parent
5565 5565 * group.
5566 5566 */
5567 5567 if (share != NULL) {
5568 5568 group = sa_get_parent_group(share);
5569 5569 } else {
5570 5570 group = sa_get_group(handle, legacygroup);
5571 5571 if (group == NULL && strcmp(legacygroup, "smb") == 0) {
5572 5572 /*
5573 5573 * This group may not exist, so create
5574 5574 * as necessary. It only contains the
5575 5575 * "smb" protocol.
5576 5576 */
5577 5577 group = sa_create_group(handle, legacygroup,
5578 5578 &ret);
5579 5579 if (group != NULL)
5580 5580 (void) sa_create_optionset(group,
5581 5581 protocol);
5582 5582 }
5583 5583 }
5584 5584
5585 5585 if (group == NULL) {
5586 5586 ret = SA_SYSTEM_ERR;
5587 5587 goto err;
5588 5588 }
5589 5589
5590 5590 groupstatus = group_status(group);
5591 5591 if (share == NULL) {
5592 5592 share = sa_add_share(group, sharepath,
5593 5593 persist, &ret);
5594 5594 if (share == NULL &&
5595 5595 ret == SA_DUPLICATE_NAME) {
5596 5596 /*
5597 5597 * Could be a ZFS path being started
5598 5598 */
5599 5599 if (sa_zfs_is_shared(handle,
5600 5600 sharepath)) {
5601 5601 ret = SA_OK;
5602 5602 group = sa_get_group(handle,
5603 5603 "zfs");
5604 5604 if (group == NULL) {
5605 5605 /*
5606 5606 * This shouldn't
5607 5607 * happen.
5608 5608 */
5609 5609 ret = SA_CONFIG_ERR;
5610 5610 } else {
5611 5611 share = sa_add_share(
5612 5612 group, sharepath,
5613 5613 persist, &ret);
5614 5614 }
5615 5615 }
5616 5616 }
5617 5617 } else {
5618 5618 char *type;
5619 5619 /*
5620 5620 * May want to change persist state, but the
5621 5621 * important thing is to change options. We
5622 5622 * need to change them regardless of the
5623 5623 * source.
5624 5624 */
5625 5625
5626 5626 if (sa_zfs_is_shared(handle, sharepath)) {
5627 5627 zfs = 1;
5628 5628 }
5629 5629 remove_all_options(share, protocol);
5630 5630 type = sa_get_share_attr(share, "type");
5631 5631 if (type != NULL &&
5632 5632 strcmp(type, "transient") != 0) {
5633 5633 curtype = SA_SHARE_PERMANENT;
5634 5634 }
5635 5635 if (type != NULL)
5636 5636 sa_free_attr_string(type);
5637 5637 if (curtype != persist) {
5638 5638 (void) sa_set_share_attr(share, "type",
5639 5639 persist == SA_SHARE_PERMANENT ?
5640 5640 "persist" : "transient");
5641 5641 }
5642 5642 }
5643 5643
5644 5644 /*
5645 5645 * If there is a resource name, we may
5646 5646 * actually care about it if this is share for
5647 5647 * a protocol that uses resource level sharing
5648 5648 * (SMB). We need to find the resource and, if
↓ open down ↓ |
5648 lines elided |
↑ open up ↑ |
5649 5649 * it exists, make sure it belongs to the
5650 5650 * current share. If it doesn't exist, attempt
5651 5651 * to create it.
5652 5652 */
5653 5653
5654 5654 if (ret == SA_OK && resource != NULL) {
5655 5655 rsrc = sa_find_resource(handle, resource);
5656 5656 if (rsrc != NULL) {
5657 5657 if (share != sa_get_resource_parent(rsrc))
5658 5658 ret = SA_DUPLICATE_NAME;
5659 - } else {
5660 - rsrc = sa_add_resource(share, resource,
5661 - persist, &ret);
5662 - }
5663 - if (features & SA_FEATURE_RESOURCE)
5664 - share = rsrc;
5659 + } else {
5660 + rsrc = sa_add_resource(share, resource,
5661 + persist, &ret);
5665 5662 }
5663 + if (features & SA_FEATURE_RESOURCE)
5664 + share = rsrc;
5665 + }
5666 5666
5667 - /* Have a group to hold this share path */
5668 - if (ret == SA_OK && options != NULL &&
5669 - strlen(options) > 0) {
5670 - ret = sa_parse_legacy_options(share,
5671 - options,
5667 + /* Have a group to hold this share path */
5668 + if (ret == SA_OK && options != NULL &&
5669 + strlen(options) > 0) {
5670 + ret = sa_parse_legacy_options(share,
5671 + options,
5672 + protocol);
5673 + }
5674 + if (!zfs) {
5675 + /*
5676 + * ZFS shares never have a description
5677 + * and we can't store the values so
5678 + * don't try.
5679 + */
5680 + if (ret == SA_OK && description != NULL)
5681 + ret = sa_set_share_description(share,
5682 + description);
5683 + }
5684 + if (ret == SA_OK &&
5685 + strcmp(groupstatus, "enabled") == 0) {
5686 + if (rsrc != share)
5687 + ret = sa_enable_share(share, protocol);
5688 + else
5689 + ret = sa_enable_resource(rsrc,
5672 5690 protocol);
5673 - }
5674 - if (!zfs) {
5675 - /*
5676 - * ZFS shares never have a description
5677 - * and we can't store the values so
5678 - * don't try.
5679 - */
5680 - if (ret == SA_OK && description != NULL)
5681 - ret = sa_set_share_description(share,
5682 - description);
5683 - }
5684 5691 if (ret == SA_OK &&
5685 - strcmp(groupstatus, "enabled") == 0) {
5686 - if (rsrc != share)
5687 - ret = sa_enable_share(share, protocol);
5688 - else
5689 - ret = sa_enable_resource(rsrc,
5690 - protocol);
5691 - if (ret == SA_OK &&
5692 - persist == SA_SHARE_PERMANENT) {
5693 - (void) sa_update_legacy(share,
5694 - protocol);
5695 - }
5696 - if (ret == SA_OK)
5697 - ret = sa_update_config(handle);
5692 + persist == SA_SHARE_PERMANENT) {
5693 + (void) sa_update_legacy(share,
5694 + protocol);
5698 5695 }
5696 + if (ret == SA_OK)
5697 + ret = sa_update_config(handle);
5698 + }
5699 5699 }
5700 5700 err:
5701 5701 if (ret != SA_OK) {
5702 5702 (void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
5703 5703 sharepath, sa_errorstr(ret));
5704 5704 ret = SA_LEGACY_ERR;
5705 5705 }
5706 5706 return (ret);
5707 5707 }
5708 5708
5709 5709 /*
5710 5710 * sa_legacy_unshare(flags, argc, argv)
5711 5711 *
5712 5712 * Implements the original unshare command.
5713 5713 */
5714 5714 int
5715 5715 sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
5716 5716 {
5717 5717 char *protocol = "nfs"; /* for now */
5718 5718 char *options = NULL;
5719 5719 char *sharepath = NULL;
5720 5720 int persist = SA_SHARE_TRANSIENT;
5721 5721 int argsused = 0;
5722 5722 int c;
5723 5723 int ret = SA_OK;
5724 5724 int true_legacy = 0;
5725 5725 uint64_t features = 0;
5726 5726 sa_resource_t resource = NULL;
5727 5727 char cmd[MAXPATHLEN];
5728 5728 #ifdef lint
5729 5729 flags = flags;
5730 5730 options = options;
5731 5731 #endif
5732 5732
5733 5733 while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
5734 5734 switch (c) {
5735 5735 case 'F':
5736 5736 protocol = optarg;
5737 5737 if (!sa_valid_protocol(protocol)) {
5738 5738 if (format_legacy_path(cmd, MAXPATHLEN,
5739 5739 protocol, "unshare") == 0 &&
5740 5740 check_legacy_cmd(cmd)) {
5741 5741 true_legacy++;
5742 5742 } else {
5743 5743 (void) printf(gettext(
5744 5744 "Invalid file system name\n"));
5745 5745 return (SA_INVALID_PROTOCOL);
5746 5746 }
5747 5747 }
5748 5748 break;
5749 5749 case 'o':
5750 5750 options = optarg;
5751 5751 argsused++;
5752 5752 break;
5753 5753 case 'p':
5754 5754 persist = SA_SHARE_PERMANENT;
5755 5755 argsused++;
5756 5756 break;
5757 5757 case 'h':
5758 5758 /* optopt on valid arg isn't defined */
5759 5759 optopt = c;
5760 5760 /*FALLTHROUGH*/
5761 5761 case '?':
5762 5762 default:
5763 5763 /*
5764 5764 * Since a bad option gets to here, sort it
5765 5765 * out and return a syntax error return value
5766 5766 * if necessary.
5767 5767 */
5768 5768 switch (optopt) {
5769 5769 default:
5770 5770 ret = SA_LEGACY_ERR;
5771 5771 break;
5772 5772 case 'h':
5773 5773 case '?':
5774 5774 break;
5775 5775 }
5776 5776 (void) printf(gettext("usage: %s\n"),
5777 5777 sa_get_usage(USAGE_UNSHARE));
5778 5778 return (ret);
5779 5779 }
5780 5780 }
5781 5781
5782 5782 /* Have the info so construct what is needed */
5783 5783 if (optind == argc || (optind + 1) < argc || options != NULL) {
5784 5784 ret = SA_SYNTAX_ERR;
5785 5785 } else {
5786 5786 sa_share_t share;
5787 5787 char dir[MAXPATHLEN];
5788 5788 if (true_legacy) {
5789 5789 /* if still using legacy share/unshare, exec it */
5790 5790 ret = run_legacy_command(cmd, argv);
5791 5791 return (ret);
5792 5792 }
5793 5793 /*
5794 5794 * Find the path in the internal configuration. If it
5795 5795 * isn't found, attempt to resolve the path via
5796 5796 * realpath() and try again.
5797 5797 */
5798 5798 sharepath = argv[optind++];
5799 5799 share = sa_find_share(handle, sharepath);
5800 5800 if (share == NULL) {
5801 5801 if (realpath(sharepath, dir) == NULL) {
5802 5802 ret = SA_NO_SUCH_PATH;
5803 5803 } else {
5804 5804 share = sa_find_share(handle, dir);
5805 5805 }
5806 5806 }
5807 5807 if (share == NULL) {
5808 5808 /* Could be a resource name so check that next */
5809 5809 features = sa_proto_get_featureset(protocol);
5810 5810 resource = sa_find_resource(handle, sharepath);
5811 5811 if (resource != NULL) {
5812 5812 share = sa_get_resource_parent(resource);
5813 5813 if (features & SA_FEATURE_RESOURCE)
5814 5814 (void) sa_disable_resource(resource,
5815 5815 protocol);
5816 5816 if (persist == SA_SHARE_PERMANENT) {
5817 5817 ret = sa_remove_resource(resource);
5818 5818 if (ret == SA_OK)
5819 5819 ret = sa_update_config(handle);
5820 5820 }
5821 5821 /*
5822 5822 * If we still have a resource on the
5823 5823 * share, we don't disable the share
5824 5824 * itself. IF there aren't anymore, we
5825 5825 * need to remove the share. The
5826 5826 * removal will be done in the next
5827 5827 * section if appropriate.
5828 5828 */
5829 5829 resource = sa_get_share_resource(share, NULL);
5830 5830 if (resource != NULL)
5831 5831 share = NULL;
5832 5832 } else if (ret == SA_OK) {
5833 5833 /* Didn't find path and no resource */
5834 5834 ret = SA_BAD_PATH;
5835 5835 }
5836 5836 }
5837 5837 if (share != NULL && resource == NULL) {
5838 5838 ret = sa_disable_share(share, protocol);
5839 5839 /*
5840 5840 * Errors are ok and removal should still occur. The
5841 5841 * legacy unshare is more forgiving of errors than the
5842 5842 * remove-share subcommand which may need the force
5843 5843 * flag set for some error conditions. That is, the
5844 5844 * "unshare" command will always unshare if it can
5845 5845 * while "remove-share" might require the force option.
5846 5846 */
5847 5847 if (persist == SA_SHARE_PERMANENT) {
5848 5848 ret = sa_remove_share(share);
5849 5849 if (ret == SA_OK)
5850 5850 ret = sa_update_config(handle);
5851 5851 }
5852 5852 } else if (ret == SA_OK && share == NULL && resource == NULL) {
5853 5853 /*
5854 5854 * If both share and resource are NULL, then
5855 5855 * share not found. If one or the other was
5856 5856 * found or there was an earlier error, we
5857 5857 * assume it was handled earlier.
5858 5858 */
5859 5859 ret = SA_NOT_SHARED;
5860 5860 }
5861 5861 }
5862 5862 switch (ret) {
5863 5863 default:
5864 5864 (void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
5865 5865 ret = SA_LEGACY_ERR;
5866 5866 break;
5867 5867 case SA_SYNTAX_ERR:
5868 5868 (void) printf(gettext("usage: %s\n"),
5869 5869 sa_get_usage(USAGE_UNSHARE));
5870 5870 break;
5871 5871 case SA_OK:
5872 5872 break;
5873 5873 }
5874 5874 return (ret);
5875 5875 }
5876 5876
5877 5877 /*
5878 5878 * Common commands that implement the sub-commands used by all
5879 5879 * protocols. The entries are found via the lookup command
5880 5880 */
5881 5881
5882 5882 static sa_command_t commands[] = {
5883 5883 {"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
5884 5884 {"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
5885 5885 {"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
5886 5886 {"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
5887 5887 {"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
5888 5888 {"list", 0, sa_list, USAGE_LIST},
5889 5889 {"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
5890 5890 {"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
5891 5891 {"set", 0, sa_set, USAGE_SET, SVC_SET},
5892 5892 {"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
5893 5893 {"show", 0, sa_show, USAGE_SHOW},
5894 5894 {"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
5895 5895 {"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
5896 5896 SVC_SET|SVC_ACTION},
5897 5897 {"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
5898 5898 {"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
5899 5899 {"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
5900 5900 {NULL, 0, NULL, NULL}
5901 5901 };
5902 5902
5903 5903 static char *
5904 5904 sa_get_usage(sa_usage_t index)
5905 5905 {
5906 5906 char *ret = NULL;
5907 5907 switch (index) {
5908 5908 case USAGE_ADD_SHARE:
5909 5909 ret = gettext("add-share [-nth] [-r resource-name] "
5910 5910 "[-d \"description text\"] -s sharepath group");
5911 5911 break;
5912 5912 case USAGE_CREATE:
5913 5913 ret = gettext(
5914 5914 "create [-nvh] [-P proto [-p property=value]] group");
5915 5915 break;
5916 5916 case USAGE_DELETE:
5917 5917 ret = gettext("delete [-nvh] [-P proto] [-f] group");
5918 5918 break;
5919 5919 case USAGE_DISABLE:
5920 5920 ret = gettext("disable [-nvh] {-a | group ...}");
5921 5921 break;
5922 5922 case USAGE_ENABLE:
5923 5923 ret = gettext("enable [-nvh] {-a | group ...}");
5924 5924 break;
5925 5925 case USAGE_LIST:
5926 5926 ret = gettext("list [-vh] [-P proto]");
5927 5927 break;
5928 5928 case USAGE_MOVE_SHARE:
5929 5929 ret = gettext(
5930 5930 "move-share [-nvh] -s sharepath destination-group");
5931 5931 break;
5932 5932 case USAGE_REMOVE_SHARE:
5933 5933 ret = gettext(
5934 5934 "remove-share [-fnvh] {-s sharepath | -r resource} "
5935 5935 "group");
5936 5936 break;
5937 5937 case USAGE_SET:
5938 5938 ret = gettext("set [-nvh] -P proto [-S optspace] "
5939 5939 "[-p property=value]* [-s sharepath] [-r resource]] "
5940 5940 "group");
5941 5941 break;
5942 5942 case USAGE_SET_SECURITY:
5943 5943 ret = gettext("set-security [-nvh] -P proto -S security-type "
5944 5944 "[-p property=value]* group");
5945 5945 break;
5946 5946 case USAGE_SET_SHARE:
5947 5947 ret = gettext("set-share [-nh] [-r resource] "
5948 5948 "[-d \"description text\"] -s sharepath group");
5949 5949 break;
5950 5950 case USAGE_SHOW:
5951 5951 ret = gettext("show [-pvxh] [-P proto] [group ...]");
5952 5952 break;
5953 5953 case USAGE_SHARE:
5954 5954 ret = gettext("share [-F fstype] [-p] [-o optionlist]"
5955 5955 "[-d description] [pathname [resourcename]]");
5956 5956 break;
5957 5957 case USAGE_START:
5958 5958 ret = gettext("start [-vh] [-P proto] {-a | group ...}");
5959 5959 break;
5960 5960 case USAGE_STOP:
5961 5961 ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
5962 5962 break;
5963 5963 case USAGE_UNSET:
5964 5964 ret = gettext("unset [-nvh] -P proto [-S optspace] "
5965 5965 "[-p property]* group");
5966 5966 break;
5967 5967 case USAGE_UNSET_SECURITY:
5968 5968 ret = gettext("unset-security [-nvh] -P proto "
5969 5969 "-S security-type [-p property]* group");
5970 5970 break;
5971 5971 case USAGE_UNSHARE:
5972 5972 ret = gettext(
5973 5973 "unshare [-F fstype] [-p] [-o optionlist] sharepath");
5974 5974 break;
5975 5975 }
5976 5976 return (ret);
5977 5977 }
5978 5978
5979 5979 /*
5980 5980 * sa_lookup(cmd, proto)
5981 5981 *
5982 5982 * Lookup the sub-command. proto isn't currently used, but it may
5983 5983 * eventually provide a way to provide protocol specific sub-commands.
5984 5984 */
5985 5985 sa_command_t *
5986 5986 sa_lookup(char *cmd, char *proto)
5987 5987 {
5988 5988 int i;
5989 5989 size_t len;
5990 5990 #ifdef lint
5991 5991 proto = proto;
5992 5992 #endif
5993 5993
5994 5994 len = strlen(cmd);
5995 5995 for (i = 0; commands[i].cmdname != NULL; i++) {
5996 5996 if (strncmp(cmd, commands[i].cmdname, len) == 0)
5997 5997 return (&commands[i]);
5998 5998 }
5999 5999 return (NULL);
6000 6000 }
6001 6001
6002 6002 void
6003 6003 sub_command_help(char *proto)
6004 6004 {
6005 6005 int i;
6006 6006 #ifdef lint
6007 6007 proto = proto;
6008 6008 #endif
6009 6009
6010 6010 (void) printf(gettext("\tsub-commands:\n"));
6011 6011 for (i = 0; commands[i].cmdname != NULL; i++) {
6012 6012 if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
6013 6013 (void) printf("\t%s\n",
6014 6014 sa_get_usage((sa_usage_t)commands[i].cmdidx));
6015 6015 }
6016 6016 }
↓ open down ↓ |
308 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX