1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * core library for common functions across all config store types 31 * and file systems to be exported. This includes legacy dfstab/sharetab 32 * parsing. Need to eliminate XML where possible. 33 */ 34 35 #include <stdio.h> 36 #include <string.h> 37 #include <ctype.h> 38 #include <unistd.h> 39 #include <limits.h> 40 #include <errno.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <libxml/parser.h> 44 #include <libxml/tree.h> 45 #include "libshare.h" 46 #include "libshare_impl.h" 47 #include <fcntl.h> 48 #include <sys/stat.h> 49 #include <grp.h> 50 #include <limits.h> 51 #include <sys/param.h> 52 #include <signal.h> 53 #include <libintl.h> 54 55 #include "sharetab.h" 56 57 #define DFSTAB_NOTICE_LINES 5 58 static char *notice[DFSTAB_NOTICE_LINES] = { 59 "# Do not modify this file directly.\n", 60 "# Use the sharemgr(1m) command for all share management\n", 61 "# This file is reconstructed and only maintained for backward\n", 62 "# compatibility. Configuration lines could be lost.\n", 63 "#\n" 64 }; 65 66 #define STRNCAT(x, y, z) (xmlChar *)strncat((char *)x, (char *)y, z) 67 68 /* will be much smaller, but this handles bad syntax in the file */ 69 #define MAXARGSFORSHARE 256 70 71 /* used internally only */ 72 typedef 73 struct sharelist { 74 struct sharelist *next; 75 int persist; 76 char *path; 77 char *resource; 78 char *fstype; 79 char *options; 80 char *description; 81 char *group; 82 char *origline; 83 int lineno; 84 } xfs_sharelist_t; 85 static void parse_dfstab(sa_handle_t, char *, xmlNodePtr); 86 extern char *get_token(char *); 87 static void dfs_free_list(xfs_sharelist_t *); 88 /* prototypes */ 89 void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *); 90 extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *); 91 extern sa_group_t _sa_create_group(sa_handle_impl_t, char *); 92 static void outdfstab(FILE *, xfs_sharelist_t *); 93 extern int _sa_remove_optionset(sa_optionset_t); 94 extern int set_node_share(void *, char *, char *); 95 extern void set_node_attr(void *, char *, char *); 96 97 /* 98 * sablocksigs(*sigs) 99 * 100 * block important signals for a critical region. Arg is a pointer to 101 * a sigset_t that is used later for the unblock. 102 */ 103 void 104 sablocksigs(sigset_t *sigs) 105 { 106 sigset_t new; 107 108 if (sigs != NULL) { 109 (void) sigprocmask(SIG_BLOCK, NULL, &new); 110 (void) sigaddset(&new, SIGHUP); 111 (void) sigaddset(&new, SIGINT); 112 (void) sigaddset(&new, SIGQUIT); 113 (void) sigaddset(&new, SIGTSTP); 114 (void) sigprocmask(SIG_SETMASK, &new, sigs); 115 } 116 } 117 118 /* 119 * saunblocksigs(*sigs) 120 * 121 * unblock previously blocked signals from the sigs arg. 122 */ 123 void 124 saunblocksigs(sigset_t *sigs) 125 { 126 if (sigs != NULL) 127 (void) sigprocmask(SIG_SETMASK, sigs, NULL); 128 } 129 130 /* 131 * alloc_sharelist() 132 * 133 * allocator function to return an zfs_sharelist_t 134 */ 135 136 static xfs_sharelist_t * 137 alloc_sharelist() 138 { 139 xfs_sharelist_t *item; 140 141 item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t)); 142 if (item != NULL) 143 (void) memset(item, '\0', sizeof (xfs_sharelist_t)); 144 return (item); 145 } 146 147 /* 148 * fix_notice(list) 149 * 150 * Look at the beginning of the current /etc/dfs/dfstab file and add 151 * the do not modify notice if it doesn't exist. 152 */ 153 154 static xfs_sharelist_t * 155 fix_notice(xfs_sharelist_t *list) 156 { 157 xfs_sharelist_t *item, *prev; 158 int i; 159 160 if (list == NULL) { 161 /* zero length dfstab */ 162 list = alloc_sharelist(); 163 if (list == NULL) 164 return (NULL); 165 list->description = strdup("#\n"); 166 } 167 if (list->path == NULL && list->description != NULL && 168 strcmp(list->description, notice[0]) != 0) { 169 for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) { 170 item = alloc_sharelist(); 171 if (item != NULL) { 172 item->description = strdup(notice[i]); 173 if (prev == NULL) { 174 item->next = list; 175 prev = item; 176 list = item; 177 } else { 178 item->next = prev->next; 179 prev->next = item; 180 prev = item; 181 } 182 } 183 } 184 } 185 return (list); 186 } 187 188 /* 189 * getdfstab(dfs) 190 * 191 * Returns an zfs_sharelist_t list of lines from the dfstab file 192 * pointed to by the FILE pointer dfs. Each entry is parsed and the 193 * original line is also preserved. Used in parsing and updating the 194 * dfstab file. 195 */ 196 197 static xfs_sharelist_t * 198 getdfstab(FILE *dfs) 199 { 200 char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */ 201 char *bp; 202 char *token; 203 char *args[MAXARGSFORSHARE]; 204 int argc; 205 int c; 206 static int line = 0; 207 xfs_sharelist_t *item = NULL, *first = NULL, *last; 208 209 if (dfs != NULL) { 210 first = NULL; 211 line = 0; 212 while (fgets(buff, sizeof (buff), dfs) != NULL) { 213 line++; 214 bp = buff; 215 if (buff[0] == '#') { 216 item = alloc_sharelist(); 217 if (item != NULL) { 218 /* if no path, then comment */ 219 item->lineno = line; 220 item->description = strdup(buff); 221 if (first == NULL) { 222 first = item; 223 last = item; 224 } else { 225 last->next = item; 226 last = item; 227 } 228 } else { 229 break; 230 } 231 continue; 232 } else if (buff[0] == '\n') { 233 continue; 234 } 235 optind = 1; 236 item = alloc_sharelist(); 237 if (item == NULL) { 238 break; 239 } else if (first == NULL) { 240 first = item; 241 last = item; 242 } else { 243 last->next = item; 244 last = item; 245 } 246 item->lineno = line; 247 item->origline = strdup(buff); 248 (void) get_token(NULL); /* reset to new pointers */ 249 argc = 0; 250 while ((token = get_token(bp)) != NULL) { 251 if (argc < MAXARGSFORSHARE) { 252 args[argc++] = token; 253 } 254 } 255 while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) { 256 switch (c) { 257 case 'p': 258 item->persist = 1; 259 break; 260 case 'F': 261 item->fstype = strdup(optarg); 262 break; 263 case 'o': 264 item->options = strdup(optarg); 265 break; 266 case 'd': 267 item->description = strdup(optarg); 268 break; 269 case 'g': 270 item->group = strdup(optarg); 271 break; 272 default: 273 break; 274 } 275 } 276 if (optind < argc) { 277 item->path = strdup(args[optind]); 278 optind++; 279 if (optind < argc) { 280 char *resource; 281 char *optgroup; 282 /* resource and/or groupname */ 283 resource = args[optind]; 284 optgroup = strchr(resource, '@'); 285 if (optgroup != NULL) { 286 *optgroup++ = '\0'; 287 } 288 if (optgroup != NULL) 289 item->group = strdup(optgroup); 290 if (resource != NULL && strlen(resource) > 0) 291 item->resource = strdup(resource); 292 } 293 } 294 if (item != NULL && item->fstype == NULL) { 295 item->fstype = strdup("nfs"); /* this is the default */ 296 } 297 } 298 } 299 first = fix_notice(first); 300 return (first); 301 } 302 303 /* 304 * finddfsentry(list, path) 305 * 306 * Look for path in the zfs_sharelist_t list and return the entry if it 307 * exists. 308 */ 309 310 static xfs_sharelist_t * 311 finddfsentry(xfs_sharelist_t *list, char *path) 312 { 313 xfs_sharelist_t *item; 314 315 for (item = list; item != NULL; item = item->next) { 316 if (item->path != NULL && strcmp(item->path, path) == 0) 317 return (item); 318 } 319 return (NULL); 320 } 321 322 /* 323 * remdfsentry(list, path, proto) 324 * 325 * Remove the specified path (with protocol) from the list. This will 326 * remove it from dfstab when the file is rewritten. 327 */ 328 329 static xfs_sharelist_t * 330 remdfsentry(xfs_sharelist_t *list, char *path, char *proto) 331 { 332 xfs_sharelist_t *item, *prev = NULL; 333 334 335 for (item = prev = list; item != NULL; item = item->next) { 336 /* skip comment entry but don't lose it */ 337 if (item->path == NULL) { 338 prev = item; 339 continue; 340 } 341 /* if proto is NULL, remove all protocols */ 342 if (proto == NULL || (strcmp(item->path, path) == 0 && 343 (item->fstype != NULL && strcmp(item->fstype, proto) == 0))) 344 break; 345 if (item->fstype == NULL && 346 (proto == NULL || strcmp(proto, "nfs") == 0)) 347 break; 348 prev = item; 349 } 350 if (item != NULL) { 351 if (item == prev) { 352 list = item->next; /* this must be the first one */ 353 } else { 354 prev->next = item->next; 355 } 356 item->next = NULL; 357 dfs_free_list(item); 358 } 359 return (list); 360 } 361 362 /* 363 * remdfsline(list, line) 364 * 365 * Remove the line specified from the list. 366 */ 367 368 static xfs_sharelist_t * 369 remdfsline(xfs_sharelist_t *list, char *line) 370 { 371 xfs_sharelist_t *item, *prev = NULL; 372 373 for (item = prev = list; item != NULL; item = item->next) { 374 /* skip comment entry but don't lose it */ 375 if (item->path == NULL) { 376 prev = item; 377 continue; 378 } 379 if (strcmp(item->origline, line) == 0) { 380 break; 381 } 382 prev = item; 383 } 384 if (item != NULL) { 385 if (item == prev) { 386 list = item->next; /* this must be the first one */ 387 } else { 388 prev->next = item->next; 389 } 390 item->next = NULL; 391 dfs_free_list(item); 392 } 393 return (list); 394 } 395 396 /* 397 * adddfsentry(list, share, proto) 398 * 399 * Add an entry to the dfstab list for share (relative to proto). This 400 * is used to update dfstab for legacy purposes. 401 */ 402 403 static xfs_sharelist_t * 404 adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto) 405 { 406 xfs_sharelist_t *item, *tmp; 407 sa_group_t parent; 408 char *groupname; 409 410 item = alloc_sharelist(); 411 if (item != NULL) { 412 parent = sa_get_parent_group(share); 413 groupname = sa_get_group_attr(parent, "name"); 414 if (strcmp(groupname, "default") == 0) { 415 sa_free_attr_string(groupname); 416 groupname = NULL; 417 } 418 item->path = sa_get_share_attr(share, "path"); 419 item->resource = sa_get_share_attr(share, "resource"); 420 item->group = groupname; 421 item->fstype = strdup(proto); 422 item->options = sa_proto_legacy_format(proto, share, 1); 423 if (item->options != NULL && strlen(item->options) == 0) { 424 free(item->options); 425 item->options = NULL; 426 } 427 item->description = sa_get_share_description(share); 428 if (item->description != NULL && strlen(item->description) == 0) { 429 sa_free_share_description(item->description); 430 item->description = NULL; 431 } 432 if (list == NULL) { 433 list = item; 434 } else { 435 for (tmp = list; tmp->next != NULL; tmp = tmp->next) 436 /* do nothing */; 437 tmp->next = item; 438 } 439 } 440 return (list); 441 } 442 443 /* 444 * outdfstab(dfstab, list) 445 * 446 * Output the list to dfstab making sure the file is truncated. 447 * Comments and errors are preserved. 448 */ 449 450 static void 451 outdfstab(FILE *dfstab, xfs_sharelist_t *list) 452 { 453 xfs_sharelist_t *item; 454 455 (void) ftruncate(fileno(dfstab), 0); 456 457 for (item = list; item != NULL; item = item->next) { 458 if (item->path != NULL) { 459 if (*item->path == '/') 460 (void) fprintf(dfstab, "share %s%s%s%s%s%s%s %s%s%s%s%s\n", 461 (item->fstype != NULL) ? "-F " : "", 462 (item->fstype != NULL) ? item->fstype : "", 463 (item->options != NULL) ? " -o " : "", 464 (item->options != NULL) ? item->options : "", 465 (item->description != NULL) ? " -d \"" : "", 466 (item->description != NULL) ? 467 item->description : "", 468 (item->description != NULL) ? "\"" : "", 469 item->path, 470 ((item->resource != NULL) || 471 (item->group != NULL)) ? " " : "", 472 (item->resource != NULL) ? item->resource : "", 473 item->group != NULL ? "@" : "", 474 item->group != NULL ? item->group : ""); 475 else 476 (void) fprintf(dfstab, "%s", item->origline); 477 } else { 478 if (item->description != NULL) { 479 (void) fprintf(dfstab, "%s", item->description); 480 } else { 481 (void) fprintf(dfstab, "%s", item->origline); 482 } 483 } 484 } 485 } 486 487 /* 488 * open_dfstab(file) 489 * 490 * Open the specified dfstab file. If the owner/group/perms are wrong, 491 * fix them. 492 */ 493 494 static FILE * 495 open_dfstab(char *file) 496 { 497 struct group *grp; 498 struct group group; 499 char *buff; 500 int grsize; 501 FILE *dfstab; 502 503 dfstab = fopen(file, "r+"); 504 if (dfstab == NULL) { 505 dfstab = fopen(file, "w+"); 506 } 507 if (dfstab != NULL) { 508 grsize = sysconf(_SC_GETGR_R_SIZE_MAX); 509 buff = malloc(grsize); 510 if (buff != NULL) 511 grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff, grsize); 512 else 513 grp = getgrnam(SA_DEFAULT_FILE_GRP); /* take the risk */ 514 (void) fchmod(fileno(dfstab), 0644); 515 (void) fchown(fileno(dfstab), 0, 516 grp != NULL ? grp->gr_gid : 3); 517 if (buff != NULL) 518 free(buff); 519 rewind(dfstab); 520 } 521 return (dfstab); 522 } 523 524 /* 525 * sa_comment_line(line, err) 526 * 527 * Add a comment to the dfstab file with err as a prefix to the 528 * original line. 529 */ 530 531 static void 532 sa_comment_line(char *line, char *err) 533 { 534 FILE *dfstab; 535 xfs_sharelist_t *list; 536 sigset_t old; 537 538 dfstab = open_dfstab(SA_LEGACY_DFSTAB); 539 if (dfstab != NULL) { 540 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 541 sablocksigs(&old); 542 (void) lockf(fileno(dfstab), F_LOCK, 0); 543 list = getdfstab(dfstab); 544 rewind(dfstab); 545 /* 546 * don't ignore the return since the list could have 547 * gone to NULL if the file only had one line in it. 548 */ 549 list = remdfsline(list, line); 550 outdfstab(dfstab, list); 551 (void) fprintf(dfstab, "# Error: %s: %s", err, line); 552 (void) fsync(fileno(dfstab)); 553 (void) lockf(fileno(dfstab), F_ULOCK, 0); 554 (void) fclose(dfstab); 555 saunblocksigs(&old); 556 if (list != NULL) 557 dfs_free_list(list); 558 } 559 } 560 561 /* 562 * sa_delete_legacy(share) 563 * 564 * Delete the specified share from the legacy config file. 565 */ 566 567 int 568 sa_delete_legacy(sa_share_t share) 569 { 570 FILE *dfstab; 571 int err; 572 int ret = SA_OK; 573 xfs_sharelist_t *list; 574 char *path; 575 sa_optionset_t optionset; 576 sa_group_t parent; 577 sigset_t old; 578 579 dfstab = open_dfstab(SA_LEGACY_DFSTAB); 580 if (dfstab != NULL) { 581 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 582 sablocksigs(&old); 583 path = sa_get_share_attr(share, "path"); 584 parent = sa_get_parent_group(share); 585 if (parent != NULL) { 586 (void) lockf(fileno(dfstab), F_LOCK, 0); 587 list = getdfstab(dfstab); 588 rewind(dfstab); 589 for (optionset = sa_get_optionset(parent, NULL); 590 optionset != NULL; 591 optionset = sa_get_next_optionset(optionset)) { 592 char *proto = sa_get_optionset_attr(optionset, "type"); 593 if (list != NULL && proto != NULL) 594 list = remdfsentry(list, path, proto); 595 if (proto == NULL) 596 ret = SA_NO_MEMORY; 597 /* 598 * may want to only do the dfstab if this call 599 * returns NOT IMPLEMENTED but it shouldn't 600 * hurt. 601 */ 602 if (ret == SA_OK) { 603 err = sa_proto_delete_legacy(proto, share); 604 if (err != SA_NOT_IMPLEMENTED) 605 ret = err; 606 } 607 if (proto != NULL) 608 sa_free_attr_string(proto); 609 } 610 outdfstab(dfstab, list); 611 if (list != NULL) 612 dfs_free_list(list); 613 (void) fflush(dfstab); 614 (void) lockf(fileno(dfstab), F_ULOCK, 0); 615 } 616 (void) fsync(fileno(dfstab)); 617 saunblocksigs(&old); 618 (void) fclose(dfstab); 619 sa_free_attr_string(path); 620 } else { 621 if (errno == EACCES || errno == EPERM) { 622 ret = SA_NO_PERMISSION; 623 } else { 624 ret = SA_CONFIG_ERR; 625 } 626 } 627 return (ret); 628 } 629 630 /* 631 * sa_update_legacy(share, proto) 632 * 633 * There is an assumption that dfstab will be the most common form of 634 * legacy configuration file for shares, but not the only one. Because 635 * of that, dfstab handling is done in the main code with calls to 636 * this function and protocol specific calls to deal with formating 637 * options into dfstab/share compatible syntax. Since not everything 638 * will be dfstab, there is a provision for calling a protocol 639 * specific plugin interface that allows the protocol plugin to do its 640 * own legacy files and skip the dfstab update. 641 */ 642 643 int 644 sa_update_legacy(sa_share_t share, char *proto) 645 { 646 FILE *dfstab; 647 int ret = SA_OK; 648 xfs_sharelist_t *list; 649 char *path; 650 sigset_t old; 651 char *persist; 652 653 ret = sa_proto_update_legacy(proto, share); 654 if (ret != SA_NOT_IMPLEMENTED) 655 return (ret); 656 /* do the dfstab format */ 657 persist = sa_get_share_attr(share, "type"); 658 /* 659 * only update if the share is not transient -- no share type 660 * set or the type is not "transient". 661 */ 662 if (persist == NULL || strcmp(persist, "transient") != 0) { 663 dfstab = open_dfstab(SA_LEGACY_DFSTAB); 664 if (dfstab != NULL) { 665 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 666 sablocksigs(&old); 667 path = sa_get_share_attr(share, "path"); 668 (void) lockf(fileno(dfstab), F_LOCK, 0); 669 list = getdfstab(dfstab); 670 rewind(dfstab); 671 if (list != NULL) 672 list = remdfsentry(list, path, proto); 673 list = adddfsentry(list, share, proto); 674 outdfstab(dfstab, list); 675 (void) fflush(dfstab); 676 (void) lockf(fileno(dfstab), F_ULOCK, 0); 677 (void) fsync(fileno(dfstab)); 678 saunblocksigs(&old); 679 (void) fclose(dfstab); 680 sa_free_attr_string(path); 681 if (list != NULL) 682 dfs_free_list(list); 683 } else { 684 if (errno == EACCES || errno == EPERM) { 685 ret = SA_NO_PERMISSION; 686 } else { 687 ret = SA_CONFIG_ERR; 688 } 689 } 690 } 691 if (persist != NULL) 692 sa_free_attr_string(persist); 693 return (ret); 694 } 695 696 /* 697 * sa_is_security(optname, proto) 698 * 699 * Check to see if optname is a security (named optionset) specific 700 * property for the specified protocol. 701 */ 702 703 int 704 sa_is_security(char *optname, char *proto) 705 { 706 int ret = 0; 707 if (proto != NULL) 708 ret = sa_proto_security_prop(proto, optname); 709 return (ret); 710 } 711 712 /* 713 * add_syntax_comment(root, line, err, todfstab) 714 * 715 * add a comment to the document indicating a syntax error. If 716 * todfstab is set, write it back to the dfstab file as well. 717 */ 718 719 static void 720 add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab) 721 { 722 xmlNodePtr node; 723 724 node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line); 725 if (node != NULL) { 726 xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err); 727 } 728 if (todfstab) 729 sa_comment_line(line, err); 730 } 731 732 /* 733 * sa_is_share(object) 734 * 735 * returns true of the object is of type "share". 736 */ 737 738 int 739 sa_is_share(void *object) 740 { 741 if (object != NULL) { 742 if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0) 743 return (1); 744 } 745 return (0); 746 } 747 748 /* 749 * _sa_remove_property(property) 750 * 751 * remove a property only from the document. 752 */ 753 754 static void 755 _sa_remove_property(sa_property_t property) 756 { 757 xmlUnlinkNode((xmlNodePtr)property); 758 xmlFreeNode((xmlNodePtr)property); 759 } 760 761 /* 762 * sa_parse_legacy_options(group, options, proto) 763 * 764 * In order to support legacy configurations, we allow the protocol 765 * specific plugin to parse legacy syntax options (like those in 766 * /etc/dfs/dfstab). This adds a new optionset to the group (or 767 * share). 768 * 769 * Once the optionset has been created, we then get the derived 770 * optionset of the parent (options from the optionset of the parent 771 * and any parent it might have) and remove those from the created 772 * optionset. This avoids duplication of options. 773 */ 774 775 int 776 sa_parse_legacy_options(sa_group_t group, char *options, char *proto) 777 { 778 int ret = SA_INVALID_PROTOCOL; 779 sa_group_t parent; 780 parent = sa_get_parent_group(group); 781 782 if (proto != NULL) 783 ret = sa_proto_legacy_opts(proto, group, options); 784 /* 785 * if in a group, remove the inherited options and security 786 */ 787 if (ret == SA_OK) { 788 if (parent != NULL) { 789 sa_optionset_t optionset; 790 sa_property_t popt, prop; 791 sa_optionset_t localoptions; 792 /* find parent options to remove from child */ 793 optionset = sa_get_derived_optionset(parent, proto, 1); 794 localoptions = sa_get_optionset(group, proto); 795 if (optionset != NULL) { 796 for (popt = sa_get_property(optionset, NULL); 797 popt != NULL; 798 popt = sa_get_next_property(popt)) { 799 char *tag; 800 char *value1; 801 char *value2; 802 803 tag = sa_get_property_attr(popt, "type"); 804 if (tag != NULL) { 805 prop = sa_get_property(localoptions, tag); 806 if (prop != NULL) { 807 value1 = sa_get_property_attr(popt, "value"); 808 value2 = sa_get_property_attr(prop, "value"); 809 if (value1 != NULL && value2 != NULL && 810 strcmp(value1, value2) == 0) { 811 /* remove the property from the child */ 812 (void) _sa_remove_property(prop); 813 } 814 if (value1 != NULL) 815 sa_free_attr_string(value1); 816 if (value2 != NULL) 817 sa_free_attr_string(value2); 818 } 819 sa_free_attr_string(tag); 820 } 821 } 822 prop = sa_get_property(localoptions, NULL); 823 if (prop == NULL && sa_is_share(group)) { 824 /* 825 * all properties removed so remove the 826 * optionset if it is on a share 827 */ 828 (void) _sa_remove_optionset(localoptions); 829 } 830 sa_free_derived_optionset(optionset); 831 } 832 /* 833 * need to remove security here. If there are no 834 * security options on the local group/share, don't 835 * bother since those are the only ones that would be 836 * affected. 837 */ 838 localoptions = sa_get_all_security_types(group, proto, 0); 839 if (localoptions != NULL) { 840 for (prop = sa_get_property(localoptions, NULL); 841 prop != NULL; prop = sa_get_next_property(prop)) { 842 char *tag; 843 sa_security_t security; 844 tag = sa_get_property_attr(prop, "type"); 845 if (tag != NULL) { 846 security = sa_get_security(group, tag, proto); 847 sa_free_attr_string(tag); 848 for (popt = sa_get_property(security, NULL); 849 popt != NULL; 850 popt = sa_get_next_property(popt)) { 851 char *value1; 852 char *value2; 853 854 /* remove duplicates from this level */ 855 value1 = sa_get_property_attr(popt, "value"); 856 value2 = sa_get_property_attr(prop, "value"); 857 if (value1 != NULL && value2 != NULL && 858 strcmp(value1, value2) == 0) { 859 /* remove the property from the child */ 860 (void) _sa_remove_property(prop); 861 } 862 if (value1 != NULL) 863 sa_free_attr_string(value1); 864 if (value2 != NULL) 865 sa_free_attr_string(value2); 866 } 867 } 868 } 869 (void) sa_destroy_optionset(localoptions); 870 } 871 } 872 } 873 return (ret); 874 } 875 876 /* 877 * dfs_free_list(list) 878 * 879 * Free the data in each list entry of the list as well as freeing the 880 * entries themselves. We need to avoid memory leaks and don't want to 881 * dereference any NULL members. 882 */ 883 884 static void 885 dfs_free_list(xfs_sharelist_t *list) 886 { 887 xfs_sharelist_t *entry; 888 for (entry = list; entry != NULL; entry = list) { 889 if (entry->path != NULL) 890 free(entry->path); 891 if (entry->resource != NULL) 892 free(entry->resource); 893 if (entry->fstype != NULL) 894 free(entry->fstype); 895 if (entry->options != NULL) 896 free(entry->options); 897 if (entry->description != NULL) 898 free(entry->description); 899 if (entry->origline != NULL) 900 free(entry->origline); 901 if (entry->group != NULL) 902 free(entry->group); 903 list = list->next; 904 free(entry); 905 } 906 } 907 908 /* 909 * parse_dfstab(dfstab, root) 910 * 911 * Open and read the existing dfstab, parsing each line and adding it 912 * to the internal configuration. Make sure syntax errors, etc are 913 * preserved as comments. 914 */ 915 916 static void 917 parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root) 918 { 919 sa_share_t share; 920 sa_group_t group; 921 sa_group_t sgroup = NULL; 922 sa_group_t defgroup; 923 xfs_sharelist_t *head, *list; 924 int err; 925 int defined_group; 926 FILE *dfs; 927 char *oldprops; 928 929 /* read the dfstab format file and fill in the doc tree */ 930 931 dfs = fopen(dfstab, "r"); 932 if (dfs == NULL) { 933 return; 934 } 935 936 defgroup = sa_get_group(handle, "default"); 937 938 for (head = list = getdfstab(dfs); 939 list != NULL; 940 list = list->next) { 941 share = NULL; 942 group = NULL; 943 defined_group = 0; 944 err = 0; 945 946 if (list->origline == NULL) { 947 /* 948 * Comment line that we will likely skip. 949 * If the line has the syntax: 950 * # error: string: string 951 * It should be preserved until manually deleted. 952 */ 953 if (list->description != NULL && 954 strncmp(list->description, "# Error: ", 9) == 0) { 955 char *line; 956 char *error; 957 char *cmd; 958 line = strdup(list->description); 959 if (line != NULL) { 960 error = line + 9; 961 cmd = strchr(error, ':'); 962 if (cmd != NULL) { 963 int len; 964 *cmd = '\0'; 965 cmd += 2; 966 len = strlen(cmd); 967 cmd[len - 1] = '\0'; 968 add_syntax_comment(root, cmd, error, 0); 969 } 970 free(line); 971 } 972 } 973 continue; 974 } 975 if (list->path != NULL && strlen(list->path) > 0 && 976 *list->path == '/') { 977 share = sa_find_share(handle, list->path); 978 if (share != NULL) 979 sgroup = sa_get_parent_group(share); 980 else 981 sgroup = NULL; 982 } else { 983 (void) printf(dgettext(TEXT_DOMAIN, 984 "No share specified in dfstab: " 985 "line %d: %s\n"), 986 list->lineno, list->origline); 987 add_syntax_comment(root, list->origline, 988 dgettext(TEXT_DOMAIN, "No share specified"), 989 1); 990 continue; 991 } 992 if (list->group != NULL && strlen(list->group) > 0) { 993 group = sa_get_group(handle, list->group); 994 defined_group = 1; 995 } else { 996 group = defgroup; 997 } 998 if (defined_group && group == NULL) { 999 (void) printf(dgettext(TEXT_DOMAIN, 1000 "Unknown group used in dfstab: " 1001 "line %d: %s\n"), 1002 list->lineno, list->origline); 1003 add_syntax_comment(root, list->origline, 1004 dgettext(TEXT_DOMAIN, 1005 "Unknown group specified"), 1); 1006 continue; 1007 } 1008 if (group != NULL) { 1009 if (share == NULL) { 1010 if (!defined_group && group == defgroup) { 1011 /* this is an OK add for legacy */ 1012 share = sa_add_share(defgroup, list->path, 1013 SA_SHARE_PERMANENT | SA_SHARE_PARSER, 1014 &err); 1015 if (share != NULL) { 1016 if (list->description != NULL && 1017 strlen(list->description) > 0) 1018 (void) sa_set_share_description(share, 1019 list->description); 1020 if (list->options != NULL && 1021 strlen(list->options) > 0) { 1022 (void) sa_parse_legacy_options(share, 1023 list->options, 1024 list->fstype); 1025 } 1026 if (list->resource != NULL) 1027 (void) sa_set_share_attr(share, "resource", 1028 list->resource); 1029 } else { 1030 (void) printf(dgettext(TEXT_DOMAIN, 1031 "Error in dfstab: " 1032 "line %d: %s\n"), 1033 list->lineno, list->origline); 1034 if (err != SA_BAD_PATH) 1035 add_syntax_comment(root, list->origline, 1036 dgettext(TEXT_DOMAIN, 1037 "Syntax"), 1); 1038 else 1039 add_syntax_comment(root, list->origline, 1040 dgettext(TEXT_DOMAIN, 1041 "Path"), 1); 1042 continue; 1043 } 1044 } 1045 } else { 1046 if (group != sgroup) { 1047 (void) printf(dgettext(TEXT_DOMAIN, "Attempt to change" 1048 "configuration in" 1049 "dfstab: line %d: %s\n"), 1050 list->lineno, list->origline); 1051 add_syntax_comment(root, list->origline, 1052 dgettext(TEXT_DOMAIN, 1053 "Attempt to change configuration"), 1054 1); 1055 continue; 1056 } 1057 /* its the same group but could have changed options */ 1058 oldprops = sa_proto_legacy_format(list->fstype, share, 0); 1059 if (oldprops != NULL) { 1060 if (list->options != NULL && 1061 strcmp(oldprops, list->options) != 0) { 1062 sa_optionset_t opts; 1063 sa_security_t secs; 1064 /* possibly different values */ 1065 opts = sa_get_optionset((sa_group_t)share, 1066 list->fstype); 1067 (void) sa_destroy_optionset(opts); 1068 for (secs = sa_get_security((sa_group_t)share, 1069 NULL, list->fstype); 1070 secs != NULL; 1071 secs = sa_get_security((sa_group_t)share, 1072 NULL, list->fstype)) { 1073 (void) sa_destroy_security(secs); 1074 } 1075 (void) sa_parse_legacy_options(share, 1076 list->options, 1077 list->fstype); 1078 } 1079 sa_format_free(oldprops); 1080 } 1081 } 1082 } else { 1083 /* shouldn't happen */ 1084 err = SA_CONFIG_ERR; 1085 } 1086 1087 } 1088 dfs_free_list(head); 1089 } 1090 1091 /* 1092 * legacy_removes(group, file) 1093 * 1094 * Find any shares that are "missing" from the legacy file. These 1095 * should be removed from the configuration since they are likely from 1096 * a legacy app or the admin modified the dfstab file directly. We 1097 * have to support this even if it is not the recommended way to do 1098 * things. 1099 */ 1100 1101 static void 1102 legacy_removes(sa_group_t group, char *file) 1103 { 1104 sa_share_t share; 1105 char *path; 1106 xfs_sharelist_t *list, *item; 1107 FILE *dfstab; 1108 1109 dfstab = fopen(file, "r"); 1110 if (dfstab != NULL) { 1111 list = getdfstab(dfstab); 1112 (void) fclose(dfstab); 1113 retry: 1114 for (share = sa_get_share(group, NULL); share != NULL; 1115 share = sa_get_next_share(share)) { 1116 /* now see if the share is in the dfstab file */ 1117 path = sa_get_share_attr(share, "path"); 1118 if (path != NULL) { 1119 item = finddfsentry(list, path); 1120 sa_free_attr_string(path); 1121 if (item == NULL) { 1122 /* the share was removed this way */ 1123 (void) sa_remove_share(share); 1124 1125 /* start over since the list was broken */ 1126 goto retry; 1127 } 1128 } 1129 } 1130 if (list != NULL) 1131 dfs_free_list(list); 1132 } 1133 } 1134 1135 /* 1136 * getlegacyconfig(path, root) 1137 * 1138 * Parse dfstab and build the legacy configuration. This only gets 1139 * called when a change was detected. 1140 */ 1141 1142 void 1143 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root) 1144 { 1145 sa_group_t defgroup; 1146 1147 if (root != NULL) { 1148 if (*root == NULL) 1149 *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1150 if (*root != NULL) { 1151 if (strcmp(path, SA_LEGACY_DFSTAB) == 0) { 1152 /* 1153 * walk the default shares and find anything 1154 * missing. we do this first to make sure it 1155 * is cleaned up since there may be legacy 1156 * code add/del via dfstab and we need to 1157 * cleanup SMF. 1158 */ 1159 defgroup = sa_get_group(handle, "default"); 1160 if (defgroup != NULL) { 1161 legacy_removes(defgroup, path); 1162 } 1163 /* parse the dfstab and add anything new */ 1164 parse_dfstab(handle, path, *root); 1165 } 1166 } 1167 } 1168 } 1169 1170 /* 1171 * get_share_list(&err) 1172 * 1173 * Get a linked list of all the shares on the system from 1174 * /etc/dfs/sharetab. This is partially copied from libfsmgt which we 1175 * can't use due to package dependencies. 1176 */ 1177 static xfs_sharelist_t * 1178 get_share_list(int *errp) 1179 { 1180 xfs_sharelist_t *newp; 1181 xfs_sharelist_t *headp; 1182 xfs_sharelist_t *tailp; 1183 FILE *fp; 1184 1185 headp = NULL; 1186 tailp = NULL; 1187 1188 if ((fp = fopen(SHARETAB, "r")) != NULL) { 1189 struct share *sharetab_entry; 1190 (void) lockf(fileno(fp), F_LOCK, 0); 1191 1192 while (getshare(fp, &sharetab_entry) > 0) { 1193 newp = alloc_sharelist(); 1194 if (newp == NULL) { 1195 goto err; 1196 } 1197 1198 /* 1199 * link into the list here so we don't leak 1200 * memory on a failure from strdup(). 1201 */ 1202 if (headp == NULL) { 1203 headp = newp; 1204 tailp = newp; 1205 } else { 1206 tailp->next = newp; 1207 tailp = newp; 1208 } 1209 1210 newp->path = strdup(sharetab_entry->sh_path); 1211 if (newp->path == NULL) 1212 goto err; 1213 newp->resource = strdup(sharetab_entry->sh_res); 1214 if (newp->resource == NULL) 1215 goto err; 1216 newp->fstype = strdup(sharetab_entry->sh_fstype); 1217 if (newp->fstype == NULL) 1218 goto err; 1219 newp->options = strdup(sharetab_entry->sh_opts); 1220 if (newp->options == NULL) 1221 goto err; 1222 newp->description = strdup(sharetab_entry->sh_descr); 1223 if (newp->description == NULL) 1224 goto err; 1225 } 1226 (void) lockf(fileno(fp), F_ULOCK, 0); 1227 (void) fclose(fp); 1228 } else { 1229 *errp = errno; 1230 } 1231 1232 /* 1233 * Caller must free the mount list 1234 */ 1235 return (headp); 1236 err: 1237 /* 1238 * Out of memory so cleanup and leave. 1239 */ 1240 dfs_free_list(headp); 1241 (void) fclose(fp); 1242 return (NULL); 1243 } 1244 1245 /* 1246 * parse_sharetab(handle) 1247 * 1248 * Read the /etc/dfs/sharetab file and see which entries don't exist 1249 * in the repository. These shares are marked transient. We also need 1250 * to see if they are ZFS shares since ZFS bypasses the SMF 1251 * repository. 1252 */ 1253 1254 int 1255 parse_sharetab(sa_handle_t handle) 1256 { 1257 xfs_sharelist_t *list, *tmplist; 1258 int err = 0; 1259 sa_share_t share; 1260 sa_group_t group; 1261 sa_group_t lgroup; 1262 char *groupname; 1263 int legacy = 0; 1264 1265 list = get_share_list(&err); 1266 if (list == NULL) 1267 return (legacy); 1268 1269 lgroup = sa_get_group(handle, "default"); 1270 1271 for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) { 1272 group = NULL; 1273 share = sa_find_share(handle, tmplist->path); 1274 if (share == NULL) { 1275 /* 1276 * this share is transient so needs to be 1277 * added. Initially, this will be under 1278 * default(legacy) unless it is a ZFS 1279 * share. If zfs, we need a zfs group. 1280 */ 1281 if (tmplist->resource != NULL && 1282 (groupname = strchr(tmplist->resource, '@')) != NULL) { 1283 /* there is a defined group */ 1284 *groupname++ = '\0'; 1285 group = sa_get_group(handle, groupname); 1286 if (group != NULL) { 1287 share = _sa_add_share(group, tmplist->path, 1288 SA_SHARE_TRANSIENT, &err); 1289 } else { 1290 /* 1291 * While this case shouldn't occur very often, 1292 * it does occur out of a "zfs set 1293 * sharenfs=off" when the dataset is also set 1294 * to canmount=off. A warning will then cause 1295 * the zfs command to abort. Since we add it 1296 * to the default list, everything works 1297 * properly anyway and the library doesn't 1298 * need to give a warning. 1299 */ 1300 share = _sa_add_share(lgroup, tmplist->path, 1301 SA_SHARE_TRANSIENT, &err); 1302 } 1303 } else { 1304 if (sa_zfs_is_shared(handle, tmplist->path)) { 1305 group = sa_get_group(handle, "zfs"); 1306 if (group == NULL) { 1307 group = sa_create_group(handle, "zfs", &err); 1308 if (group == NULL && err == SA_NO_PERMISSION) { 1309 group = _sa_create_group( 1310 (sa_handle_impl_t)handle, 1311 "zfs"); 1312 } 1313 if (group != NULL) { 1314 (void) sa_create_optionset(group, 1315 tmplist->fstype); 1316 (void) sa_set_group_attr(group, "zfs", "true"); 1317 } 1318 } 1319 if (group != NULL) { 1320 share = _sa_add_share(group, tmplist->path, 1321 SA_SHARE_TRANSIENT, &err); 1322 } 1323 } else { 1324 share = _sa_add_share(lgroup, tmplist->path, 1325 SA_SHARE_TRANSIENT, &err); 1326 } 1327 } 1328 if (share == NULL) 1329 (void) printf(dgettext(TEXT_DOMAIN, 1330 "Problem with transient: %s\n"), 1331 sa_errorstr(err)); 1332 if (share != NULL) 1333 set_node_attr(share, "shared", "true"); 1334 1335 if (err == SA_OK) { 1336 if (tmplist->options != NULL && 1337 strlen(tmplist->options) > 0) { 1338 (void) sa_parse_legacy_options(share, 1339 tmplist->options, 1340 tmplist->fstype); 1341 } 1342 if (tmplist->resource != NULL && 1343 strcmp(tmplist->resource, "-") != 0) 1344 set_node_attr(share, "resource", tmplist->resource); 1345 if (tmplist->description != NULL) { 1346 xmlNodePtr node; 1347 node = xmlNewChild((xmlNodePtr)share, NULL, 1348 (xmlChar *)"description", NULL); 1349 xmlNodeSetContent(node, 1350 (xmlChar *)tmplist->description); 1351 } 1352 legacy = 1; 1353 } 1354 } else { 1355 /* 1356 * if this is a legacy share, mark as shared so we 1357 * only update sharetab appropriately. We also keep 1358 * the sharetab options in order to display for legacy 1359 * share with no arguments. 1360 */ 1361 set_node_attr(share, "shared", "true"); 1362 set_node_attr(share, "shareopts", tmplist->options); 1363 } 1364 } 1365 dfs_free_list(list); 1366 return (legacy); 1367 } 1368 1369 /* 1370 * get the transient shares from the sharetab (or other) file. since 1371 * these are transient, they only appear in the working file and not 1372 * in a repository. 1373 */ 1374 int 1375 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root) 1376 { 1377 int legacy = 0; 1378 1379 if (root != NULL) { 1380 if (*root == NULL) 1381 *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1382 if (*root != NULL) { 1383 legacy = parse_sharetab(ihandle); 1384 } 1385 } 1386 return (legacy); 1387 } 1388 1389 /* 1390 * sa_has_prop(optionset, prop) 1391 * 1392 * Is the specified property a member of the optionset? 1393 */ 1394 1395 int 1396 sa_has_prop(sa_optionset_t optionset, sa_property_t prop) 1397 { 1398 char *name; 1399 sa_property_t otherprop; 1400 int result = 0; 1401 1402 if (optionset != NULL) { 1403 name = sa_get_property_attr(prop, "type"); 1404 if (name != NULL) { 1405 otherprop = sa_get_property(optionset, name); 1406 if (otherprop != NULL) 1407 result = 1; 1408 sa_free_attr_string(name); 1409 } 1410 } 1411 return (result); 1412 } 1413 1414 /* 1415 * Update legacy files 1416 * 1417 * Provides functions to add/remove/modify individual entries 1418 * in dfstab and sharetab 1419 */ 1420 1421 void 1422 update_legacy_config(sa_handle_t handle) 1423 { 1424 /* 1425 * no longer used -- this is a placeholder in case we need to 1426 * add it back later. 1427 */ 1428 #ifdef lint 1429 handle = handle; 1430 #endif 1431 } 1432 1433 /* 1434 * sa_valid_property(object, proto, property) 1435 * 1436 * check to see if the specified property is valid relative to the 1437 * specified protocol. The protocol plugin is called to do the work. 1438 */ 1439 1440 int 1441 sa_valid_property(void *object, char *proto, sa_property_t property) 1442 { 1443 int ret = SA_OK; 1444 1445 if (proto != NULL && property != NULL) { 1446 ret = sa_proto_valid_prop(proto, property, object); 1447 } 1448 1449 return (ret); 1450 } 1451 1452 /* 1453 * sa_fstype(path) 1454 * 1455 * Given path, return the string representing the path's file system 1456 * type. This is used to discover ZFS shares. 1457 */ 1458 1459 char * 1460 sa_fstype(char *path) 1461 { 1462 int err; 1463 struct stat st; 1464 1465 err = stat(path, &st); 1466 if (err < 0) { 1467 err = SA_NO_SUCH_PATH; 1468 } else { 1469 err = SA_OK; 1470 } 1471 if (err == SA_OK) { 1472 /* have a valid path at this point */ 1473 return (strdup(st.st_fstype)); 1474 } 1475 return (NULL); 1476 } 1477 1478 void 1479 sa_free_fstype(char *type) 1480 { 1481 free(type); 1482 } 1483 1484 /* 1485 * sa_get_derived_optionset(object, proto, hier) 1486 * 1487 * Work backward to the top of the share object tree and start 1488 * copying protocol specific optionsets into a newly created 1489 * optionset that doesn't have a parent (it will be freed 1490 * later). This provides for the property inheritence model. That 1491 * is, properties closer to the share take precedence over group 1492 * level. This also provides for groups of groups in the future. 1493 */ 1494 1495 sa_optionset_t 1496 sa_get_derived_optionset(void *object, char *proto, int hier) 1497 { 1498 sa_optionset_t newoptionset; 1499 sa_optionset_t optionset; 1500 sa_group_t group; 1501 1502 if (hier && 1503 (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 1504 newoptionset = sa_get_derived_optionset((void *)group, proto, hier); 1505 } else { 1506 newoptionset = (sa_optionset_t)xmlNewNode(NULL, 1507 (xmlChar *)"optionset"); 1508 if (newoptionset != NULL) { 1509 sa_set_optionset_attr(newoptionset, "type", proto); 1510 } 1511 } 1512 /* dont' do anything if memory wasn't allocated */ 1513 if (newoptionset == NULL) 1514 return (NULL); 1515 1516 /* found the top so working back down the stack */ 1517 optionset = sa_get_optionset((sa_optionset_t)object, proto); 1518 if (optionset != NULL) { 1519 sa_property_t prop; 1520 /* add optionset to the newoptionset */ 1521 for (prop = sa_get_property(optionset, NULL); 1522 prop != NULL; prop = sa_get_next_property(prop)) { 1523 sa_property_t newprop; 1524 char *name; 1525 char *value; 1526 name = sa_get_property_attr(prop, "type"); 1527 value = sa_get_property_attr(prop, "value"); 1528 if (name != NULL) { 1529 newprop = sa_get_property(newoptionset, name); 1530 /* replace the value with the new value */ 1531 if (newprop != NULL) { 1532 /* 1533 * only set if value is non NULL, old value ok 1534 * if it is NULL. 1535 */ 1536 if (value != NULL) 1537 set_node_attr(newprop, "value", value); 1538 } else { 1539 /* an entirely new property */ 1540 if (value != NULL) { 1541 newprop = sa_create_property(name, value); 1542 if (newprop != NULL) { 1543 newprop = (sa_property_t) 1544 xmlAddChild((xmlNodePtr)newoptionset, 1545 (xmlNodePtr)newprop); 1546 } 1547 } 1548 } 1549 sa_free_attr_string(name); 1550 } 1551 if (value != NULL) 1552 sa_free_attr_string(value); 1553 } 1554 } 1555 return (newoptionset); 1556 } 1557 1558 void 1559 sa_free_derived_optionset(sa_optionset_t optionset) 1560 { 1561 /* while it shouldn't be linked, it doesn't hurt */ 1562 if (optionset != NULL) { 1563 xmlUnlinkNode((xmlNodePtr) optionset); 1564 xmlFreeNode((xmlNodePtr) optionset); 1565 } 1566 } 1567 1568 /* 1569 * sa_get_all_security_types(object, proto, hier) 1570 * 1571 * find all the security types set for this object. This is 1572 * preliminary to getting a derived security set. The return value is an 1573 * optionset containg properties which are the sectype values found by 1574 * walking up the XML document struture. The returned optionset 1575 * is a derived optionset. 1576 * 1577 * If hier is 0, only look at object. If non-zero, walk up the tree. 1578 */ 1579 sa_optionset_t 1580 sa_get_all_security_types(void *object, char *proto, int hier) 1581 { 1582 sa_optionset_t options; 1583 sa_security_t security; 1584 sa_group_t group; 1585 sa_property_t prop; 1586 1587 options = NULL; 1588 1589 if (hier && 1590 (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 1591 options = sa_get_all_security_types((void *)group, proto, hier); 1592 } else { 1593 options = (sa_optionset_t)xmlNewNode(NULL, 1594 (xmlChar *)"optionset"); 1595 } 1596 /* hit the top so collect the security types working back */ 1597 if (options != NULL) { 1598 for (security = sa_get_security((sa_group_t)object, NULL, NULL); 1599 security != NULL; security = sa_get_next_security(security)) { 1600 char *type; 1601 char *sectype; 1602 1603 type = sa_get_security_attr(security, "type"); 1604 if (type != NULL) { 1605 if (strcmp(type, proto) != 0) { 1606 sa_free_attr_string(type); 1607 continue; 1608 } 1609 sectype = sa_get_security_attr(security, "sectype"); 1610 if (sectype != NULL) { 1611 /* 1612 * have a security type, check to see if 1613 * already present in optionset and add if it 1614 * isn't. 1615 */ 1616 if (sa_get_property(options, sectype) == NULL) { 1617 prop = sa_create_property(sectype, "true"); 1618 if (prop != NULL) 1619 prop = (sa_property_t) 1620 xmlAddChild((xmlNodePtr)options, 1621 (xmlNodePtr)prop); 1622 } 1623 sa_free_attr_string(sectype); 1624 } 1625 sa_free_attr_string(type); 1626 } 1627 } 1628 } 1629 return (options); 1630 } 1631 1632 /* 1633 * sa_get_derived_security(object, sectype, proto, hier) 1634 * 1635 * Get the derived security(named optionset) for the object given the 1636 * sectype and proto. If hier is non-zero, walk up the tree to get all 1637 * properties defined for this object, otherwise just those on the 1638 * object. 1639 */ 1640 1641 sa_security_t 1642 sa_get_derived_security(void *object, char *sectype, char *proto, int hier) 1643 { 1644 sa_security_t newsecurity; 1645 sa_security_t security; 1646 sa_group_t group; 1647 1648 if (hier && 1649 (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 1650 newsecurity = sa_get_derived_security((void *)group, 1651 sectype, proto, hier); 1652 } else { 1653 newsecurity = (sa_security_t)xmlNewNode(NULL, 1654 (xmlChar *)"security"); 1655 if (newsecurity != NULL) { 1656 sa_set_security_attr(newsecurity, "type", proto); 1657 sa_set_security_attr(newsecurity, "sectype", sectype); 1658 } 1659 } 1660 /* dont' do anything if memory wasn't allocated */ 1661 if (newsecurity == NULL) 1662 return (NULL); 1663 1664 /* found the top so working back down the stack */ 1665 security = sa_get_security((sa_security_t)object, sectype, proto); 1666 if (security != NULL) { 1667 sa_property_t prop; 1668 /* add security to the newsecurity */ 1669 for (prop = sa_get_property(security, NULL); 1670 prop != NULL; prop = sa_get_next_property(prop)) { 1671 sa_property_t newprop; 1672 char *name; 1673 char *value; 1674 name = sa_get_property_attr(prop, "type"); 1675 value = sa_get_property_attr(prop, "value"); 1676 if (name != NULL) { 1677 newprop = sa_get_property(newsecurity, name); 1678 /* replace the value with the new value */ 1679 if (newprop != NULL) { 1680 /* 1681 * only set if value is non NULL, old value ok 1682 * if it is NULL. 1683 */ 1684 if (value != NULL) 1685 set_node_attr(newprop, name, value); 1686 } else { 1687 /* an entirely new property */ 1688 if (value != NULL) { 1689 newprop = sa_create_property(name, value); 1690 newprop = (sa_property_t) 1691 xmlAddChild((xmlNodePtr)newsecurity, 1692 (xmlNodePtr)newprop); 1693 } 1694 } 1695 sa_free_attr_string(name); 1696 } 1697 if (value != NULL) 1698 sa_free_attr_string(value); 1699 } 1700 } 1701 return (newsecurity); 1702 } 1703 1704 void 1705 sa_free_derived_security(sa_security_t security) 1706 { 1707 /* while it shouldn't be linked, it doesn't hurt */ 1708 if (security != NULL) { 1709 xmlUnlinkNode((xmlNodePtr)security); 1710 xmlFreeNode((xmlNodePtr)security); 1711 } 1712 } 1713 1714 /* 1715 * sharetab utility functions 1716 * 1717 * makes use of the original sharetab.c from fs.d/nfs/lib 1718 */ 1719 1720 /* 1721 * fillshare(share, proto, sh) 1722 * 1723 * Fill the struct share with values obtained from the share object. 1724 */ 1725 static void 1726 fillshare(sa_share_t share, char *proto, struct share *sh) 1727 { 1728 char *groupname = NULL; 1729 char *value; 1730 sa_group_t group; 1731 char *buff; 1732 char *zfs; 1733 1734 group = sa_get_parent_group(share); 1735 if (group != NULL) { 1736 zfs = sa_get_group_attr(group, "zfs"); 1737 groupname = sa_get_group_attr(group, "name"); 1738 1739 if (groupname != NULL && 1740 (strcmp(groupname, "default") == 0 || zfs != NULL)) { 1741 /* 1742 * since the groupname is either "default" or the 1743 * group is a ZFS group, we don't want to keep 1744 * groupname. We do want it if it is any other type of 1745 * group. 1746 */ 1747 sa_free_attr_string(groupname); 1748 groupname = NULL; 1749 } 1750 if (zfs != NULL) 1751 sa_free_attr_string(zfs); 1752 } 1753 1754 value = sa_get_share_attr(share, "path"); 1755 if (value != NULL) { 1756 sh->sh_path = strdup(value); 1757 sa_free_attr_string(value); 1758 } 1759 1760 value = sa_get_share_attr(share, "resource"); 1761 if (value != NULL || groupname != NULL) { 1762 int len = 0; 1763 1764 if (value != NULL) 1765 len += strlen(value); 1766 if (groupname != NULL) 1767 len += strlen(groupname); 1768 len += 3; /* worst case */ 1769 buff = malloc(len); 1770 (void) snprintf(buff, len, "%s%s%s", 1771 (value != NULL && strlen(value) > 0) ? value : "-", 1772 groupname != NULL ? "@" : "", 1773 groupname != NULL ? groupname : ""); 1774 sh->sh_res = buff; 1775 if (value != NULL) 1776 sa_free_attr_string(value); 1777 if (groupname != NULL) { 1778 sa_free_attr_string(groupname); 1779 groupname = NULL; 1780 } 1781 } else { 1782 sh->sh_res = strdup("-"); 1783 } 1784 1785 sh->sh_fstype = strdup(proto); 1786 value = sa_proto_legacy_format(proto, share, 1); 1787 if (value != NULL) { 1788 if (strlen(value) > 0) 1789 sh->sh_opts = strdup(value); 1790 else 1791 sh->sh_opts = strdup("rw"); 1792 free(value); 1793 } else 1794 sh->sh_opts = strdup("rw"); 1795 1796 value = sa_get_share_description(share); 1797 if (value != NULL) { 1798 sh->sh_descr = strdup(value); 1799 sa_free_share_description(value); 1800 } else 1801 sh->sh_descr = strdup(""); 1802 } 1803 1804 /* 1805 * emptyshare(sh) 1806 * 1807 * Free the strings in the non-NULL members of sh. 1808 */ 1809 1810 static void 1811 emptyshare(struct share *sh) 1812 { 1813 if (sh->sh_path != NULL) 1814 free(sh->sh_path); 1815 sh->sh_path = NULL; 1816 if (sh->sh_res != NULL) 1817 free(sh->sh_res); 1818 sh->sh_res = NULL; 1819 if (sh->sh_fstype != NULL) 1820 free(sh->sh_fstype); 1821 sh->sh_fstype = NULL; 1822 if (sh->sh_opts != NULL) 1823 free(sh->sh_opts); 1824 sh->sh_opts = NULL; 1825 if (sh->sh_descr != NULL) 1826 free(sh->sh_descr); 1827 sh->sh_descr = NULL; 1828 } 1829 1830 /* 1831 * checkshare(struct share *) 1832 * 1833 * If the share to write to sharetab is not present, need to add. If 1834 * the share is present, replace if options are different else we want 1835 * to keep it. 1836 * Return values: 1837 * 1 - keep 1838 * 2 - replace 1839 * The CHK_NEW value isn't currently returned. 1840 */ 1841 #define CHK_NEW 0 1842 #define CHK_KEEP 1 1843 #define CHK_REPLACE 2 1844 static int 1845 checkshare(struct share *sh) 1846 { 1847 xfs_sharelist_t *list, *head; 1848 int err; 1849 int ret = CHK_NEW; 1850 1851 head = list = get_share_list(&err); 1852 while (list != NULL && ret == CHK_NEW) { 1853 if (strcmp(sh->sh_path, list->path) == 0) { 1854 /* Have the same path so check if replace or keep */ 1855 if (strcmp(sh->sh_opts, list->options) == 0) 1856 ret = CHK_KEEP; 1857 else 1858 ret = CHK_REPLACE; 1859 } 1860 list = list->next; 1861 } 1862 if (head != NULL) { 1863 dfs_free_list(head); 1864 } 1865 /* 1866 * Just in case it was added by another process after our 1867 * scan, we always replace even if we think it is new. 1868 */ 1869 if (ret == CHK_NEW) 1870 ret = CHK_REPLACE; 1871 return (ret); 1872 } 1873 1874 /* 1875 * sa_update_sharetab(share, proto) 1876 * 1877 * Update the sharetab file with info from the specified share. 1878 * This could be an update or add. 1879 */ 1880 1881 int 1882 sa_update_sharetab(sa_share_t share, char *proto) 1883 { 1884 int ret = SA_OK; 1885 struct share shtab; 1886 char *path; 1887 int logging = 0; 1888 FILE *sharetab; 1889 sigset_t old; 1890 int action; 1891 1892 path = sa_get_share_attr(share, "path"); 1893 if (path != NULL) { 1894 (void) memset(&shtab, '\0', sizeof (shtab)); 1895 sharetab = fopen(SA_LEGACY_SHARETAB, "r+"); 1896 if (sharetab == NULL) { 1897 sharetab = fopen(SA_LEGACY_SHARETAB, "w+"); 1898 } 1899 if (sharetab != NULL) { 1900 (void) setvbuf(sharetab, NULL, _IOLBF, BUFSIZ * 8); 1901 sablocksigs(&old); 1902 /* 1903 * Fill in share structure and write it out if the 1904 * share isn't already shared with the same options. 1905 */ 1906 (void) fillshare(share, proto, &shtab); 1907 /* 1908 * If share is new or changed, remove the old, 1909 * otherwise keep it in place since it hasn't changed. 1910 */ 1911 action = checkshare(&shtab); 1912 (void) lockf(fileno(sharetab), F_LOCK, 0); 1913 switch (action) { 1914 case CHK_REPLACE: 1915 (void) remshare(sharetab, path, &logging); 1916 (void) putshare(sharetab, &shtab); 1917 break; 1918 case CHK_KEEP: 1919 /* Don't do anything */ 1920 break; 1921 } 1922 emptyshare(&shtab); 1923 (void) fflush(sharetab); 1924 (void) lockf(fileno(sharetab), F_ULOCK, 0); 1925 (void) fsync(fileno(sharetab)); 1926 saunblocksigs(&old); 1927 (void) fclose(sharetab); 1928 } else { 1929 if (errno == EACCES || errno == EPERM) { 1930 ret = SA_NO_PERMISSION; 1931 } else { 1932 ret = SA_CONFIG_ERR; 1933 } 1934 } 1935 sa_free_attr_string(path); 1936 } 1937 return (ret); 1938 } 1939 1940 /* 1941 * sa_delete_sharetab(path, proto) 1942 * 1943 * remove the specified share from sharetab. 1944 */ 1945 1946 int 1947 sa_delete_sharetab(char *path, char *proto) 1948 { 1949 int ret = SA_OK; 1950 int logging = 0; 1951 FILE *sharetab; 1952 sigset_t old; 1953 #ifdef lint 1954 proto = proto; 1955 #endif 1956 1957 if (path != NULL) { 1958 sharetab = fopen(SA_LEGACY_SHARETAB, "r+"); 1959 if (sharetab == NULL) { 1960 sharetab = fopen(SA_LEGACY_SHARETAB, "w+"); 1961 } 1962 if (sharetab != NULL) { 1963 /* should block keyboard level signals around the lock */ 1964 sablocksigs(&old); 1965 (void) lockf(fileno(sharetab), F_LOCK, 0); 1966 ret = remshare(sharetab, path, &logging); 1967 (void) fflush(sharetab); 1968 (void) lockf(fileno(sharetab), F_ULOCK, 0); 1969 (void) fsync(fileno(sharetab)); 1970 saunblocksigs(&old); 1971 (void) fclose(sharetab); 1972 } else { 1973 if (errno == EACCES || errno == EPERM) { 1974 ret = SA_NO_PERMISSION; 1975 } else { 1976 ret = SA_CONFIG_ERR; 1977 } 1978 } 1979 } 1980 return (ret); 1981 } 1982