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