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 2008 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 #include <dirent.h> 55 56 #include <sharefs/share.h> 57 #include "sharetab.h" 58 59 #define DFSTAB_NOTICE_LINES 5 60 static char *notice[DFSTAB_NOTICE_LINES] = { 61 "# Do not modify this file directly.\n", 62 "# Use the sharemgr(1m) command for all share management\n", 63 "# This file is reconstructed and only maintained for backward\n", 64 "# compatibility. Configuration lines could be lost.\n", 65 "#\n" 66 }; 67 68 #define STRNCAT(x, y, z) (xmlChar *)strncat((char *)x, (char *)y, z) 69 70 /* will be much smaller, but this handles bad syntax in the file */ 71 #define MAXARGSFORSHARE 256 72 73 /* used internally only */ 74 typedef 75 struct sharelist { 76 struct sharelist *next; 77 int persist; 78 char *path; 79 char *resource; 80 char *fstype; 81 char *options; 82 char *description; 83 char *group; 84 char *origline; 85 int lineno; 86 } xfs_sharelist_t; 87 static void parse_dfstab(sa_handle_t, char *, xmlNodePtr); 88 extern char *get_token(char *); 89 static void dfs_free_list(xfs_sharelist_t *); 90 /* prototypes */ 91 void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *); 92 extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t); 93 extern sa_group_t _sa_create_group(sa_handle_impl_t, char *); 94 static void outdfstab(FILE *, xfs_sharelist_t *); 95 extern int _sa_remove_optionset(sa_optionset_t); 96 extern int set_node_share(void *, char *, char *); 97 extern void set_node_attr(void *, char *, char *); 98 99 /* 100 * sablocksigs(*sigs) 101 * 102 * block important signals for a critical region. Arg is a pointer to 103 * a sigset_t that is used later for the unblock. 104 */ 105 void 106 sablocksigs(sigset_t *sigs) 107 { 108 sigset_t new; 109 110 if (sigs != NULL) { 111 (void) sigprocmask(SIG_BLOCK, NULL, &new); 112 (void) sigaddset(&new, SIGHUP); 113 (void) sigaddset(&new, SIGINT); 114 (void) sigaddset(&new, SIGQUIT); 115 (void) sigaddset(&new, SIGTSTP); 116 (void) sigprocmask(SIG_SETMASK, &new, sigs); 117 } 118 } 119 120 /* 121 * saunblocksigs(*sigs) 122 * 123 * unblock previously blocked signals from the sigs arg. 124 */ 125 void 126 saunblocksigs(sigset_t *sigs) 127 { 128 if (sigs != NULL) 129 (void) sigprocmask(SIG_SETMASK, sigs, NULL); 130 } 131 132 /* 133 * alloc_sharelist() 134 * 135 * allocator function to return an zfs_sharelist_t 136 */ 137 138 static xfs_sharelist_t * 139 alloc_sharelist() 140 { 141 xfs_sharelist_t *item; 142 143 item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t)); 144 if (item != NULL) 145 (void) memset(item, '\0', sizeof (xfs_sharelist_t)); 146 return (item); 147 } 148 149 /* 150 * fix_notice(list) 151 * 152 * Look at the beginning of the current /etc/dfs/dfstab file and add 153 * the do not modify notice if it doesn't exist. 154 */ 155 156 static xfs_sharelist_t * 157 fix_notice(xfs_sharelist_t *list) 158 { 159 xfs_sharelist_t *item, *prev; 160 int i; 161 162 if (list == NULL) { 163 /* zero length dfstab */ 164 list = alloc_sharelist(); 165 if (list == NULL) 166 return (NULL); 167 list->description = strdup("#\n"); 168 } 169 if (list->path == NULL && list->description != NULL && 170 strcmp(list->description, notice[0]) != 0) { 171 for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) { 172 item = alloc_sharelist(); 173 if (item != NULL) { 174 item->description = strdup(notice[i]); 175 if (prev == NULL) { 176 item->next = list; 177 prev = item; 178 list = item; 179 } else { 180 item->next = prev->next; 181 prev->next = item; 182 prev = item; 183 } 184 } 185 } 186 } 187 return (list); 188 } 189 190 /* 191 * getdfstab(dfs) 192 * 193 * Returns an zfs_sharelist_t list of lines from the dfstab file 194 * pointed to by the FILE pointer dfs. Each entry is parsed and the 195 * original line is also preserved. Used in parsing and updating the 196 * dfstab file. 197 */ 198 199 static xfs_sharelist_t * 200 getdfstab(FILE *dfs) 201 { 202 char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */ 203 char *bp; 204 char *token; 205 char *args[MAXARGSFORSHARE]; 206 int argc; 207 int c; 208 static int line = 0; 209 xfs_sharelist_t *item = NULL, *first = NULL, *last; 210 211 if (dfs != NULL) { 212 first = NULL; 213 line = 0; 214 while (fgets(buff, sizeof (buff), dfs) != NULL) { 215 line++; 216 bp = buff; 217 if (buff[0] == '#') { 218 item = alloc_sharelist(); 219 if (item != NULL) { 220 /* if no path, then comment */ 221 item->lineno = line; 222 item->description = strdup(buff); 223 if (first == NULL) { 224 first = item; 225 last = item; 226 } else { 227 last->next = item; 228 last = item; 229 } 230 } else { 231 break; 232 } 233 continue; 234 } else if (buff[0] == '\n') { 235 continue; 236 } 237 optind = 1; 238 item = alloc_sharelist(); 239 if (item == NULL) { 240 break; 241 } else if (first == NULL) { 242 first = item; 243 last = item; 244 } else { 245 last->next = item; 246 last = item; 247 } 248 item->lineno = line; 249 item->origline = strdup(buff); 250 (void) get_token(NULL); /* reset to new pointers */ 251 argc = 0; 252 while ((token = get_token(bp)) != NULL) { 253 if (argc < MAXARGSFORSHARE) 254 args[argc++] = token; 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 if (optgroup != NULL) 289 item->group = strdup(optgroup); 290 if (resource != NULL && 291 strlen(resource) > 0) 292 item->resource = 293 strdup(resource); 294 } 295 } 296 /* NFS is the default if none defined */ 297 if (item != NULL && item->fstype == NULL) 298 item->fstype = strdup("nfs"); 299 } 300 } 301 first = fix_notice(first); 302 return (first); 303 } 304 305 /* 306 * finddfsentry(list, path) 307 * 308 * Look for path in the zfs_sharelist_t list and return the entry if it 309 * exists. 310 */ 311 312 static xfs_sharelist_t * 313 finddfsentry(xfs_sharelist_t *list, char *path) 314 { 315 xfs_sharelist_t *item; 316 317 for (item = list; item != NULL; item = item->next) { 318 if (item->path != NULL && strcmp(item->path, path) == 0) 319 return (item); 320 } 321 return (NULL); 322 } 323 324 /* 325 * remdfsentry(list, path, proto) 326 * 327 * Remove the specified path (with protocol) from the list. This will 328 * remove it from dfstab when the file is rewritten. 329 */ 330 331 static xfs_sharelist_t * 332 remdfsentry(xfs_sharelist_t *list, char *path, char *proto) 333 { 334 xfs_sharelist_t *item, *prev = NULL; 335 336 337 for (item = prev = list; item != NULL; item = item->next) { 338 /* skip comment entry but don't lose it */ 339 if (item->path == NULL) { 340 prev = item; 341 continue; 342 } 343 /* if proto is NULL, remove all protocols */ 344 if (proto == NULL || (strcmp(item->path, path) == 0 && 345 (item->fstype != NULL && strcmp(item->fstype, proto) == 0))) 346 break; 347 if (item->fstype == NULL && 348 (proto == NULL || strcmp(proto, "nfs") == 0)) 349 break; 350 prev = item; 351 } 352 if (item != NULL) { 353 if (item == prev) 354 list = item->next; /* this must be the first one */ 355 else 356 prev->next = item->next; 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 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 item->next = NULL; 390 dfs_free_list(item); 391 } 392 return (list); 393 } 394 395 /* 396 * adddfsentry(list, share, proto) 397 * 398 * Add an entry to the dfstab list for share (relative to proto). This 399 * is used to update dfstab for legacy purposes. 400 */ 401 402 static xfs_sharelist_t * 403 adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto) 404 { 405 xfs_sharelist_t *item, *tmp; 406 sa_group_t parent; 407 char *groupname; 408 409 item = alloc_sharelist(); 410 if (item != NULL) { 411 parent = sa_get_parent_group(share); 412 groupname = sa_get_group_attr(parent, "name"); 413 if (strcmp(groupname, "default") == 0) { 414 sa_free_attr_string(groupname); 415 groupname = NULL; 416 } 417 item->path = sa_get_share_attr(share, "path"); 418 item->resource = sa_get_share_attr(share, "resource"); 419 item->group = groupname; 420 item->fstype = strdup(proto); 421 item->options = sa_proto_legacy_format(proto, share, 1); 422 if (item->options != NULL && strlen(item->options) == 0) { 423 free(item->options); 424 item->options = NULL; 425 } 426 item->description = sa_get_share_description(share); 427 if (item->description != NULL && 428 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, 461 "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) ? 466 item->options : "", 467 (item->description != NULL) ? 468 " -d \"" : "", 469 (item->description != NULL) ? 470 item->description : "", 471 (item->description != NULL) ? "\"" : "", 472 item->path, 473 ((item->resource != NULL) || 474 (item->group != NULL)) ? " " : "", 475 (item->resource != NULL) ? 476 item->resource : "", 477 item->group != NULL ? "@" : "", 478 item->group != NULL ? item->group : ""); 479 } else { 480 (void) fprintf(dfstab, "%s", item->origline); 481 } 482 } else { 483 if (item->description != NULL) 484 (void) fprintf(dfstab, "%s", item->description); 485 else 486 (void) fprintf(dfstab, "%s", item->origline); 487 } 488 } 489 } 490 491 /* 492 * open_dfstab(file) 493 * 494 * Open the specified dfstab file. If the owner/group/perms are wrong, 495 * fix them. 496 */ 497 498 static FILE * 499 open_dfstab(char *file) 500 { 501 struct group *grp; 502 struct group group; 503 char *buff; 504 int grsize; 505 FILE *dfstab; 506 507 dfstab = fopen(file, "r+"); 508 if (dfstab == NULL) { 509 dfstab = fopen(file, "w+"); 510 } 511 if (dfstab != NULL) { 512 grsize = sysconf(_SC_GETGR_R_SIZE_MAX); 513 buff = malloc(grsize); 514 if (buff != NULL) 515 grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff, 516 grsize); 517 else 518 grp = getgrnam(SA_DEFAULT_FILE_GRP); 519 (void) fchmod(fileno(dfstab), 0644); 520 (void) fchown(fileno(dfstab), 0, 521 grp != NULL ? grp->gr_gid : 3); 522 if (buff != NULL) 523 free(buff); 524 rewind(dfstab); 525 } 526 return (dfstab); 527 } 528 529 /* 530 * sa_comment_line(line, err) 531 * 532 * Add a comment to the dfstab file with err as a prefix to the 533 * original line. 534 */ 535 536 static void 537 sa_comment_line(char *line, char *err) 538 { 539 FILE *dfstab; 540 xfs_sharelist_t *list; 541 sigset_t old; 542 543 dfstab = open_dfstab(SA_LEGACY_DFSTAB); 544 if (dfstab != NULL) { 545 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 546 sablocksigs(&old); 547 (void) lockf(fileno(dfstab), F_LOCK, 0); 548 list = getdfstab(dfstab); 549 rewind(dfstab); 550 /* 551 * don't ignore the return since the list could have 552 * gone to NULL if the file only had one line in it. 553 */ 554 list = remdfsline(list, line); 555 outdfstab(dfstab, list); 556 (void) fprintf(dfstab, "# Error: %s: %s", err, line); 557 (void) fsync(fileno(dfstab)); 558 (void) lockf(fileno(dfstab), F_ULOCK, 0); 559 (void) fclose(dfstab); 560 saunblocksigs(&old); 561 if (list != NULL) 562 dfs_free_list(list); 563 } 564 } 565 566 /* 567 * sa_delete_legacy(share, protocol) 568 * 569 * Delete the specified share from the legacy config file. 570 */ 571 572 int 573 sa_delete_legacy(sa_share_t share, char *protocol) 574 { 575 FILE *dfstab; 576 int err; 577 int ret = SA_OK; 578 xfs_sharelist_t *list; 579 char *path; 580 sa_optionset_t optionset; 581 sa_group_t parent; 582 sigset_t old; 583 584 /* 585 * Protect against shares that don't have paths. This is not 586 * really an error at this point. 587 */ 588 path = sa_get_share_attr(share, "path"); 589 if (path == NULL) 590 return (ret); 591 592 dfstab = open_dfstab(SA_LEGACY_DFSTAB); 593 if (dfstab != NULL) { 594 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 595 sablocksigs(&old); 596 parent = sa_get_parent_group(share); 597 if (parent != NULL) { 598 (void) lockf(fileno(dfstab), F_LOCK, 0); 599 list = getdfstab(dfstab); 600 rewind(dfstab); 601 if (protocol != NULL) { 602 if (list != NULL) 603 list = remdfsentry(list, path, 604 protocol); 605 } else { 606 for (optionset = sa_get_optionset(parent, NULL); 607 optionset != NULL; 608 optionset = 609 sa_get_next_optionset(optionset)) { 610 char *proto = sa_get_optionset_attr( 611 optionset, "type"); 612 613 if (list != NULL && proto != NULL) 614 list = remdfsentry(list, path, 615 proto); 616 if (proto == NULL) 617 ret = SA_NO_MEMORY; 618 /* 619 * may want to only do the dfstab if 620 * this call returns NOT IMPLEMENTED 621 * but it shouldn't hurt. 622 */ 623 if (ret == SA_OK) { 624 err = sa_proto_delete_legacy( 625 proto, share); 626 if (err != SA_NOT_IMPLEMENTED) 627 ret = err; 628 } 629 if (proto != NULL) 630 sa_free_attr_string(proto); 631 } 632 } 633 outdfstab(dfstab, list); 634 if (list != NULL) 635 dfs_free_list(list); 636 (void) fflush(dfstab); 637 (void) lockf(fileno(dfstab), F_ULOCK, 0); 638 } 639 (void) fsync(fileno(dfstab)); 640 saunblocksigs(&old); 641 (void) fclose(dfstab); 642 } else { 643 if (errno == EACCES || errno == EPERM) 644 ret = SA_NO_PERMISSION; 645 else 646 ret = SA_CONFIG_ERR; 647 } 648 649 if (path != NULL) 650 sa_free_attr_string(path); 651 652 return (ret); 653 } 654 655 /* 656 * sa_update_legacy(share, proto) 657 * 658 * There is an assumption that dfstab will be the most common form of 659 * legacy configuration file for shares, but not the only one. Because 660 * of that, dfstab handling is done in the main code with calls to 661 * this function and protocol specific calls to deal with formatting 662 * options into dfstab/share compatible syntax. Since not everything 663 * will be dfstab, there is a provision for calling a protocol 664 * specific plugin interface that allows the protocol plugin to do its 665 * own legacy files and skip the dfstab update. 666 */ 667 668 int 669 sa_update_legacy(sa_share_t share, char *proto) 670 { 671 FILE *dfstab; 672 int ret = SA_OK; 673 xfs_sharelist_t *list; 674 char *path; 675 sigset_t old; 676 char *persist; 677 uint64_t features; 678 679 ret = sa_proto_update_legacy(proto, share); 680 if (ret != SA_NOT_IMPLEMENTED) 681 return (ret); 682 683 features = sa_proto_get_featureset(proto); 684 if (!(features & SA_FEATURE_DFSTAB)) 685 return (ret); 686 687 /* do the dfstab format */ 688 persist = sa_get_share_attr(share, "type"); 689 /* 690 * only update if the share is not transient -- no share type 691 * set or the type is not "transient". 692 */ 693 if (persist == NULL || strcmp(persist, "transient") != 0) { 694 dfstab = open_dfstab(SA_LEGACY_DFSTAB); 695 if (dfstab != NULL) { 696 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 697 sablocksigs(&old); 698 path = sa_get_share_attr(share, "path"); 699 (void) lockf(fileno(dfstab), F_LOCK, 0); 700 list = getdfstab(dfstab); 701 rewind(dfstab); 702 if (list != NULL) 703 list = remdfsentry(list, path, proto); 704 list = adddfsentry(list, share, proto); 705 outdfstab(dfstab, list); 706 (void) fflush(dfstab); 707 (void) lockf(fileno(dfstab), F_ULOCK, 0); 708 (void) fsync(fileno(dfstab)); 709 saunblocksigs(&old); 710 (void) fclose(dfstab); 711 sa_free_attr_string(path); 712 if (list != NULL) 713 dfs_free_list(list); 714 } else { 715 if (errno == EACCES || errno == EPERM) 716 ret = SA_NO_PERMISSION; 717 else 718 ret = SA_CONFIG_ERR; 719 } 720 } 721 if (persist != NULL) 722 sa_free_attr_string(persist); 723 return (ret); 724 } 725 726 /* 727 * sa_is_security(optname, proto) 728 * 729 * Check to see if optname is a security (named optionset) specific 730 * property for the specified protocol. 731 */ 732 733 int 734 sa_is_security(char *optname, char *proto) 735 { 736 int ret = 0; 737 if (proto != NULL) 738 ret = sa_proto_security_prop(proto, optname); 739 return (ret); 740 } 741 742 /* 743 * add_syntax_comment(root, line, err, todfstab) 744 * 745 * Add a comment to the document indicating a syntax error. If 746 * todfstab is set, write it back to the dfstab file as well. 747 */ 748 749 static void 750 add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab) 751 { 752 xmlNodePtr node; 753 754 node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line); 755 if (node != NULL) 756 (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err); 757 if (todfstab) 758 sa_comment_line(line, err); 759 } 760 761 /* 762 * sa_is_share(object) 763 * 764 * returns true of the object is of type "share". 765 */ 766 767 int 768 sa_is_share(void *object) 769 { 770 if (object != NULL) { 771 if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0) 772 return (1); 773 } 774 return (0); 775 } 776 /* 777 * sa_is_resource(object) 778 * 779 * returns true of the object is of type "share". 780 */ 781 782 int 783 sa_is_resource(void *object) 784 { 785 if (object != NULL) { 786 if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0) 787 return (1); 788 } 789 return (0); 790 } 791 792 /* 793 * _sa_remove_property(property) 794 * 795 * remove a property only from the document. 796 */ 797 798 static void 799 _sa_remove_property(sa_property_t property) 800 { 801 xmlUnlinkNode((xmlNodePtr)property); 802 xmlFreeNode((xmlNodePtr)property); 803 } 804 805 /* 806 * _sa_create_dummy_share() 807 * 808 * Create a share entry suitable for parsing but not tied to any real 809 * config tree. Need to have a parent as well as the node to parse 810 * on. Free using _sa_free_dummy_share(share); 811 */ 812 813 static sa_group_t 814 _sa_create_dummy_share() 815 { 816 xmlNodePtr parent_node = NULL; 817 xmlNodePtr child_node = NULL; 818 819 parent_node = xmlNewNode(NULL, (xmlChar *)"group"); 820 if (parent_node != NULL) { 821 child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share", 822 NULL); 823 if (child_node != NULL) { 824 /* 825 * Use a "zfs" tag since that will make sure nothing 826 * really attempts to put values into the 827 * repository. Also ZFS is currently the only user of 828 * this interface. 829 */ 830 set_node_attr(parent_node, "type", "transient"); 831 set_node_attr(parent_node, "zfs", "true"); 832 set_node_attr(child_node, "type", "transient"); 833 set_node_attr(child_node, "zfs", "true"); 834 } else { 835 xmlFreeNode(parent_node); 836 } 837 } 838 return (child_node); 839 } 840 841 /* 842 * _sa_free_dummy_share(share) 843 * 844 * Free the dummy share and its parent. It is an error to try and 845 * free something that isn't a dummy. 846 */ 847 848 static int 849 _sa_free_dummy_share(sa_share_t share) 850 { 851 xmlNodePtr node = (xmlNodePtr)share; 852 xmlNodePtr parent; 853 int ret = SA_OK; 854 char *name; 855 856 if (node != NULL) { 857 parent = node->parent; 858 name = (char *)xmlGetProp(node, (xmlChar *)"path"); 859 if (name != NULL) { 860 /* Real shares always have a path but a dummy doesn't */ 861 ret = SA_NOT_ALLOWED; 862 sa_free_attr_string(name); 863 } else { 864 /* 865 * If there is a parent, do the free on that since 866 * xmlFreeNode is a recursive function and free's an 867 * child nodes. 868 */ 869 if (parent != NULL) { 870 node = parent; 871 } 872 xmlUnlinkNode(node); 873 xmlFreeNode(node); 874 } 875 } 876 return (ret); 877 } 878 879 880 /* 881 * sa_parse_legacy_options(group, options, proto) 882 * 883 * In order to support legacy configurations, we allow the protocol 884 * specific plugin to parse legacy syntax options (like those in 885 * /etc/dfs/dfstab). This adds a new optionset to the group (or 886 * share). 887 * 888 * Once the optionset has been created, we then get the derived 889 * optionset of the parent (options from the optionset of the parent 890 * and any parent it might have) and remove those from the created 891 * optionset. This avoids duplication of options. 892 */ 893 894 int 895 sa_parse_legacy_options(sa_group_t group, char *options, char *proto) 896 { 897 int ret = SA_INVALID_PROTOCOL; 898 sa_group_t parent; 899 int using_dummy = B_FALSE; 900 char *pvalue; 901 sa_optionset_t optionset; 902 sa_property_t popt, prop; 903 sa_optionset_t localoptions; 904 905 /* 906 * If "group" is NULL, this is just a parse without saving 907 * anything in either SMF or ZFS. Create a dummy group to 908 * handle this case. 909 */ 910 if (group == NULL) { 911 group = (sa_group_t)_sa_create_dummy_share(); 912 using_dummy = B_TRUE; 913 } 914 915 parent = sa_get_parent_group(group); 916 917 if (proto != NULL) 918 ret = sa_proto_legacy_opts(proto, group, options); 919 920 if (using_dummy) { 921 /* Since this is a dummy parse, cleanup and quit here */ 922 (void) _sa_free_dummy_share(parent); 923 return (ret); 924 } 925 926 if (ret != SA_OK) 927 return (ret); 928 929 /* 930 * If in a group, remove the inherited options and security 931 */ 932 933 if (parent == NULL) 934 return (ret); 935 936 /* Find parent options to remove from child */ 937 optionset = sa_get_derived_optionset(parent, proto, 1); 938 localoptions = sa_get_optionset(group, proto); 939 if (optionset != NULL) { 940 for (popt = sa_get_property(optionset, NULL); 941 popt != NULL; 942 popt = sa_get_next_property(popt)) { 943 char *tag; 944 char *value; 945 tag = sa_get_property_attr(popt, "type"); 946 if (tag == NULL) 947 continue; 948 prop = sa_get_property(localoptions, tag); 949 if (prop != NULL) { 950 value = sa_get_property_attr(popt, 951 "value"); 952 pvalue = sa_get_property_attr(prop, 953 "value"); 954 if (value != NULL && pvalue != NULL && 955 strcmp(value, pvalue) == 0) { 956 /* 957 * Remove the property 958 * from the 959 * child. While we 960 * removed it, we 961 * don't need to reset 962 * as we do below 963 * since we always 964 * search from the 965 * beginning. 966 */ 967 (void) _sa_remove_property( 968 prop); 969 } 970 if (value != NULL) 971 sa_free_attr_string(value); 972 if (pvalue != NULL) 973 sa_free_attr_string(pvalue); 974 } 975 sa_free_attr_string(tag); 976 } 977 prop = sa_get_property(localoptions, NULL); 978 if (prop == NULL && sa_is_share(group)) { 979 /* 980 * All properties removed so remove the 981 * optionset if it is on a share 982 */ 983 (void) _sa_remove_optionset(localoptions); 984 } 985 sa_free_derived_optionset(optionset); 986 } 987 /* 988 * Need to remove security here. If there are no 989 * security options on the local group/share, don't 990 * bother since those are the only ones that would be 991 * affected. 992 */ 993 localoptions = sa_get_all_security_types(group, proto, 0); 994 if (localoptions != NULL) { 995 for (prop = sa_get_property(localoptions, NULL); 996 prop != NULL; 997 prop = sa_get_next_property(prop)) { 998 char *tag; 999 sa_security_t security; 1000 tag = sa_get_property_attr(prop, "type"); 1001 if (tag != NULL) { 1002 sa_property_t nextpopt = NULL; 1003 security = sa_get_security(group, tag, proto); 1004 sa_free_attr_string(tag); 1005 /* 1006 * prop's value only changes outside this loop 1007 */ 1008 pvalue = sa_get_property_attr(prop, "value"); 1009 for (popt = sa_get_property(security, NULL); 1010 popt != NULL; 1011 popt = nextpopt) { 1012 char *value; 1013 /* 1014 * Need to get the next prop 1015 * now since we could break 1016 * the list during removal. 1017 */ 1018 nextpopt = sa_get_next_property(popt); 1019 /* remove Duplicates from this level */ 1020 value = sa_get_property_attr(popt, 1021 "value"); 1022 if (value != NULL && pvalue != NULL && 1023 strcmp(value, pvalue) == 0) { 1024 /* 1025 * remove the property 1026 * from the child 1027 */ 1028 (void) _sa_remove_property 1029 (popt); 1030 } 1031 if (value != NULL) 1032 sa_free_attr_string(value); 1033 } 1034 if (pvalue != NULL) 1035 sa_free_attr_string(pvalue); 1036 } 1037 } 1038 (void) sa_destroy_optionset(localoptions); 1039 } 1040 return (ret); 1041 } 1042 1043 /* 1044 * dfs_free_list(list) 1045 * 1046 * Free the data in each list entry of the list as well as freeing the 1047 * entries themselves. We need to avoid memory leaks and don't want to 1048 * dereference any NULL members. 1049 */ 1050 1051 static void 1052 dfs_free_list(xfs_sharelist_t *list) 1053 { 1054 xfs_sharelist_t *entry; 1055 for (entry = list; entry != NULL; entry = list) { 1056 if (entry->path != NULL) 1057 free(entry->path); 1058 if (entry->resource != NULL) 1059 free(entry->resource); 1060 if (entry->fstype != NULL) 1061 free(entry->fstype); 1062 if (entry->options != NULL) 1063 free(entry->options); 1064 if (entry->description != NULL) 1065 free(entry->description); 1066 if (entry->origline != NULL) 1067 free(entry->origline); 1068 if (entry->group != NULL) 1069 free(entry->group); 1070 list = list->next; 1071 free(entry); 1072 } 1073 } 1074 1075 /* 1076 * parse_dfstab(dfstab, root) 1077 * 1078 * Open and read the existing dfstab, parsing each line and adding it 1079 * to the internal configuration. Make sure syntax errors, etc are 1080 * preserved as comments. 1081 */ 1082 1083 static void 1084 parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root) 1085 { 1086 sa_share_t share; 1087 sa_group_t group; 1088 sa_group_t sgroup = NULL; 1089 sa_group_t defgroup; 1090 xfs_sharelist_t *head, *list; 1091 int err; 1092 int defined_group; 1093 FILE *dfs; 1094 char *oldprops; 1095 1096 /* read the dfstab format file and fill in the doc tree */ 1097 1098 dfs = fopen(dfstab, "r"); 1099 if (dfs == NULL) 1100 return; 1101 1102 defgroup = sa_get_group(handle, "default"); 1103 1104 for (head = list = getdfstab(dfs); 1105 list != NULL; 1106 list = list->next) { 1107 share = NULL; 1108 group = NULL; 1109 defined_group = 0; 1110 err = 0; 1111 1112 if (list->origline == NULL) { 1113 /* 1114 * Comment line that we will likely skip. 1115 * If the line has the syntax: 1116 * # error: string: string 1117 * It should be preserved until manually deleted. 1118 */ 1119 if (list->description != NULL && 1120 strncmp(list->description, "# Error: ", 9) == 0) { 1121 char *line; 1122 char *error; 1123 char *cmd; 1124 line = strdup(list->description); 1125 if (line != NULL) { 1126 error = line + 9; 1127 cmd = strchr(error, ':'); 1128 if (cmd != NULL) { 1129 int len; 1130 *cmd = '\0'; 1131 cmd += 2; 1132 len = strlen(cmd); 1133 cmd[len - 1] = '\0'; 1134 add_syntax_comment(root, cmd, 1135 error, 0); 1136 } 1137 free(line); 1138 } 1139 } 1140 continue; 1141 } 1142 if (list->path != NULL && strlen(list->path) > 0 && 1143 *list->path == '/') { 1144 share = sa_find_share(handle, list->path); 1145 if (share != NULL) 1146 sgroup = sa_get_parent_group(share); 1147 else 1148 sgroup = NULL; 1149 } else { 1150 (void) printf(dgettext(TEXT_DOMAIN, 1151 "No share specified in dfstab: " 1152 "line %d: %s\n"), 1153 list->lineno, list->origline); 1154 add_syntax_comment(root, list->origline, 1155 dgettext(TEXT_DOMAIN, "No share specified"), 1); 1156 continue; 1157 } 1158 if (list->group != NULL && strlen(list->group) > 0) { 1159 group = sa_get_group(handle, list->group); 1160 defined_group = 1; 1161 } else { 1162 group = defgroup; 1163 } 1164 if (defined_group && group == NULL) { 1165 (void) printf(dgettext(TEXT_DOMAIN, 1166 "Unknown group used in dfstab: line %d: %s\n"), 1167 list->lineno, list->origline); 1168 add_syntax_comment(root, list->origline, 1169 dgettext(TEXT_DOMAIN, "Unknown group specified"), 1170 1); 1171 continue; 1172 } 1173 if (group == NULL) { 1174 /* Shouldn't happen unless an SMF error */ 1175 err = SA_CONFIG_ERR; 1176 continue; 1177 } 1178 if (share == NULL) { 1179 if (defined_group || group != defgroup) 1180 continue; 1181 /* This is an OK add for legacy */ 1182 share = sa_add_share(defgroup, list->path, 1183 SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err); 1184 if (share != NULL) { 1185 if (list->description != NULL && 1186 strlen(list->description) > 0) 1187 (void) sa_set_share_description(share, 1188 list->description); 1189 if (list->options != NULL && 1190 strlen(list->options) > 0) { 1191 (void) sa_parse_legacy_options(share, 1192 list->options, list->fstype); 1193 } 1194 if (list->resource != NULL) 1195 (void) sa_set_share_attr(share, 1196 "resource", list->resource); 1197 } else { 1198 (void) printf(dgettext(TEXT_DOMAIN, 1199 "Error in dfstab: line %d: %s\n"), 1200 list->lineno, list->origline); 1201 if (err != SA_BAD_PATH) 1202 add_syntax_comment(root, list->origline, 1203 dgettext(TEXT_DOMAIN, "Syntax"), 1); 1204 else 1205 add_syntax_comment(root, list->origline, 1206 dgettext(TEXT_DOMAIN, 1207 "Path"), 1); 1208 continue; 1209 } 1210 } else { 1211 if (group != sgroup) { 1212 (void) printf(dgettext(TEXT_DOMAIN, 1213 "Attempt to change configuration in " 1214 "dfstab: line %d: %s\n"), 1215 list->lineno, list->origline); 1216 add_syntax_comment(root, list->origline, 1217 dgettext(TEXT_DOMAIN, 1218 "Attempt to change configuration"), 1); 1219 continue; 1220 } 1221 /* 1222 * It is the same group but could have changed 1223 * options. Make sure we include the group's 1224 * properties so we don't end up moving them to 1225 * the share inadvertantly. The last arg being 1226 * true says to get the inherited properties as well 1227 * as the local properties. 1228 */ 1229 oldprops = sa_proto_legacy_format(list->fstype, share, 1230 B_TRUE); 1231 1232 if (oldprops == NULL) 1233 continue; 1234 1235 if (list->options != NULL && 1236 strcmp(oldprops, list->options) != 0) { 1237 sa_optionset_t opts; 1238 sa_security_t secs; 1239 1240 /* possibly different values */ 1241 opts = sa_get_optionset((sa_group_t) 1242 share, list->fstype); 1243 (void) sa_destroy_optionset(opts); 1244 1245 for (secs = sa_get_security( 1246 (sa_group_t)share, NULL, list->fstype); 1247 secs != NULL; 1248 secs = sa_get_security((sa_group_t)share, 1249 NULL, list->fstype)) { 1250 (void) sa_destroy_security( 1251 secs); 1252 } 1253 (void) sa_parse_legacy_options(share, 1254 list->options, list->fstype); 1255 } 1256 sa_format_free(oldprops); 1257 } 1258 } 1259 dfs_free_list(head); 1260 } 1261 1262 /* 1263 * legacy_removes(group, file) 1264 * 1265 * Find any shares that are "missing" from the legacy file. These 1266 * should be removed from the configuration since they are likely from 1267 * a legacy app or the admin modified the dfstab file directly. We 1268 * have to support this even if it is not the recommended way to do 1269 * things. 1270 */ 1271 1272 static void 1273 legacy_removes(sa_group_t group, char *file) 1274 { 1275 sa_share_t share; 1276 char *path; 1277 xfs_sharelist_t *list, *item; 1278 FILE *dfstab; 1279 1280 dfstab = fopen(file, "r"); 1281 if (dfstab != NULL) { 1282 list = getdfstab(dfstab); 1283 (void) fclose(dfstab); 1284 retry: 1285 for (share = sa_get_share(group, NULL); 1286 share != NULL; 1287 share = sa_get_next_share(share)) { 1288 /* now see if the share is in the dfstab file */ 1289 path = sa_get_share_attr(share, "path"); 1290 if (path != NULL) { 1291 item = finddfsentry(list, path); 1292 sa_free_attr_string(path); 1293 if (item == NULL) { 1294 /* The share was removed this way */ 1295 (void) sa_remove_share(share); 1296 1297 /* 1298 * Start over since the list was broken 1299 */ 1300 goto retry; 1301 } 1302 } 1303 } 1304 if (list != NULL) 1305 dfs_free_list(list); 1306 } 1307 } 1308 1309 /* 1310 * getlegacyconfig(path, root) 1311 * 1312 * Parse dfstab and build the legacy configuration. This only gets 1313 * called when a change was detected. 1314 */ 1315 1316 void 1317 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root) 1318 { 1319 sa_group_t defgroup; 1320 1321 if (root != NULL) { 1322 if (*root == NULL) 1323 *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1324 if (*root != NULL) { 1325 if (strcmp(path, SA_LEGACY_DFSTAB) == 0) { 1326 /* 1327 * Walk the default shares and find anything 1328 * missing. we do this first to make sure it 1329 * is cleaned up since there may be legacy 1330 * code add/del via dfstab and we need to 1331 * cleanup SMF. 1332 */ 1333 defgroup = sa_get_group(handle, "default"); 1334 if (defgroup != NULL) 1335 legacy_removes(defgroup, path); 1336 /* Parse the dfstab and add anything new */ 1337 parse_dfstab(handle, path, *root); 1338 } 1339 } 1340 } 1341 } 1342 1343 /* 1344 * get_share_list(&err) 1345 * 1346 * Get a linked list of all the shares on the system from 1347 * /etc/dfs/sharetab. This is partially copied from libfsmgt which we 1348 * can't use due to package dependencies. 1349 */ 1350 static xfs_sharelist_t * 1351 get_share_list(int *errp) 1352 { 1353 xfs_sharelist_t *newp; 1354 xfs_sharelist_t *headp; 1355 xfs_sharelist_t *tailp; 1356 FILE *fp; 1357 1358 headp = NULL; 1359 tailp = NULL; 1360 1361 if ((fp = fopen(SHARETAB, "r")) != NULL) { 1362 struct share *sharetab_entry; 1363 1364 while (getshare(fp, &sharetab_entry) > 0) { 1365 newp = alloc_sharelist(); 1366 if (newp == NULL) 1367 goto err; 1368 1369 /* 1370 * Link into the list here so we don't leak 1371 * memory on a failure from strdup(). 1372 */ 1373 if (headp == NULL) { 1374 headp = newp; 1375 tailp = newp; 1376 } else { 1377 tailp->next = newp; 1378 tailp = newp; 1379 } 1380 1381 newp->path = strdup(sharetab_entry->sh_path); 1382 if (newp->path == NULL) 1383 goto err; 1384 newp->resource = strdup(sharetab_entry->sh_res); 1385 if (newp->resource == NULL) 1386 goto err; 1387 newp->fstype = strdup(sharetab_entry->sh_fstype); 1388 if (newp->fstype == NULL) 1389 goto err; 1390 newp->options = strdup(sharetab_entry->sh_opts); 1391 if (newp->options == NULL) 1392 goto err; 1393 newp->description = strdup(sharetab_entry->sh_descr); 1394 if (newp->description == NULL) 1395 goto err; 1396 } 1397 (void) lockf(fileno(fp), F_ULOCK, 0); 1398 (void) fclose(fp); 1399 } else { 1400 *errp = errno; 1401 } 1402 1403 /* 1404 * Caller must free the mount list 1405 */ 1406 return (headp); 1407 err: 1408 /* 1409 * Out of memory so cleanup and leave. 1410 */ 1411 dfs_free_list(headp); 1412 (void) fclose(fp); 1413 return (NULL); 1414 } 1415 1416 /* 1417 * parse_sharetab(handle) 1418 * 1419 * Read the /etc/dfs/sharetab file and see which entries don't exist 1420 * in the repository. These shares are marked transient. We also need 1421 * to see if they are ZFS shares since ZFS bypasses the SMF 1422 * repository. 1423 */ 1424 1425 int 1426 parse_sharetab(sa_handle_t handle) 1427 { 1428 xfs_sharelist_t *list, *tmplist; 1429 int err = 0; 1430 sa_share_t share; 1431 sa_group_t group; 1432 sa_group_t lgroup; 1433 char *groupname; 1434 int legacy = 0; 1435 char shareopts[MAXNAMLEN]; 1436 1437 list = get_share_list(&err); 1438 if (list == NULL) 1439 return (legacy); 1440 1441 lgroup = sa_get_group(handle, "default"); 1442 1443 for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) { 1444 group = NULL; 1445 share = sa_find_share(handle, tmplist->path); 1446 if (share != NULL) { 1447 /* 1448 * If this is a legacy share, mark as shared so we 1449 * only update sharetab appropriately. We also keep 1450 * the sharetab options in order to display for legacy 1451 * share with no arguments. 1452 */ 1453 set_node_attr(share, "shared", "true"); 1454 (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", 1455 tmplist->fstype); 1456 set_node_attr(share, shareopts, tmplist->options); 1457 continue; 1458 } 1459 1460 /* 1461 * This share is transient so needs to be 1462 * added. Initially, this will be under 1463 * default(legacy) unless it is a ZFS 1464 * share. If zfs, we need a zfs group. 1465 */ 1466 if (tmplist->resource != NULL && 1467 (groupname = strchr(tmplist->resource, '@')) != NULL) { 1468 /* There is a defined group */ 1469 *groupname++ = '\0'; 1470 group = sa_get_group(handle, groupname); 1471 if (group != NULL) { 1472 share = _sa_add_share(group, tmplist->path, 1473 SA_SHARE_TRANSIENT, &err, 1474 (uint64_t)SA_FEATURE_NONE); 1475 } else { 1476 /* 1477 * While this case shouldn't 1478 * occur very often, it does 1479 * occur out of a "zfs set 1480 * sharenfs=off" when the 1481 * dataset is also set to 1482 * canmount=off. A warning 1483 * will then cause the zfs 1484 * command to abort. Since we 1485 * add it to the default list, 1486 * everything works properly 1487 * anyway and the library 1488 * doesn't need to give a 1489 * warning. 1490 */ 1491 share = _sa_add_share(lgroup, 1492 tmplist->path, SA_SHARE_TRANSIENT, 1493 &err, (uint64_t)SA_FEATURE_NONE); 1494 } 1495 } else { 1496 if (sa_zfs_is_shared(handle, tmplist->path)) { 1497 group = sa_get_group(handle, "zfs"); 1498 if (group == NULL) { 1499 group = sa_create_group(handle, 1500 "zfs", &err); 1501 if (group == NULL && 1502 err == SA_NO_PERMISSION) { 1503 group = _sa_create_group( 1504 (sa_handle_impl_t) 1505 handle, 1506 "zfs"); 1507 } 1508 if (group != NULL) { 1509 (void) sa_create_optionset( 1510 group, tmplist->fstype); 1511 (void) sa_set_group_attr(group, 1512 "zfs", "true"); 1513 } 1514 } 1515 if (group != NULL) { 1516 share = _sa_add_share(group, 1517 tmplist->path, SA_SHARE_TRANSIENT, 1518 &err, (uint64_t)SA_FEATURE_NONE); 1519 } 1520 } else { 1521 share = _sa_add_share(lgroup, tmplist->path, 1522 SA_SHARE_TRANSIENT, &err, 1523 (uint64_t)SA_FEATURE_NONE); 1524 } 1525 } 1526 if (share == NULL) 1527 (void) printf(dgettext(TEXT_DOMAIN, 1528 "Problem with transient: %s\n"), sa_errorstr(err)); 1529 if (share != NULL) 1530 set_node_attr(share, "shared", "true"); 1531 if (err == SA_OK) { 1532 if (tmplist->options != NULL && 1533 strlen(tmplist->options) > 0) { 1534 (void) sa_parse_legacy_options(share, 1535 tmplist->options, tmplist->fstype); 1536 } 1537 if (tmplist->resource != NULL && 1538 strcmp(tmplist->resource, "-") != 0) 1539 set_node_attr(share, "resource", 1540 tmplist->resource); 1541 if (tmplist->description != NULL) { 1542 xmlNodePtr node; 1543 node = xmlNewChild((xmlNodePtr)share, NULL, 1544 (xmlChar *)"description", NULL); 1545 xmlNodeSetContent(node, 1546 (xmlChar *)tmplist->description); 1547 } 1548 legacy = 1; 1549 } 1550 } 1551 dfs_free_list(list); 1552 return (legacy); 1553 } 1554 1555 /* 1556 * Get the transient shares from the sharetab (or other) file. since 1557 * these are transient, they only appear in the working file and not 1558 * in a repository. 1559 */ 1560 int 1561 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root) 1562 { 1563 int legacy = 0; 1564 int numproto; 1565 char **protocols = NULL; 1566 int i; 1567 1568 if (root != NULL) { 1569 if (*root == NULL) 1570 *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1571 if (*root != NULL) { 1572 legacy = parse_sharetab(ihandle); 1573 numproto = sa_get_protocols(&protocols); 1574 for (i = 0; i < numproto; i++) 1575 legacy |= sa_proto_get_transients( 1576 (sa_handle_t)ihandle, protocols[i]); 1577 if (protocols != NULL) 1578 free(protocols); 1579 } 1580 } 1581 return (legacy); 1582 } 1583 1584 /* 1585 * sa_has_prop(optionset, prop) 1586 * 1587 * Is the specified property a member of the optionset? 1588 */ 1589 1590 int 1591 sa_has_prop(sa_optionset_t optionset, sa_property_t prop) 1592 { 1593 char *name; 1594 sa_property_t otherprop; 1595 int result = 0; 1596 1597 if (optionset != NULL) { 1598 name = sa_get_property_attr(prop, "type"); 1599 if (name != NULL) { 1600 otherprop = sa_get_property(optionset, name); 1601 if (otherprop != NULL) 1602 result = 1; 1603 sa_free_attr_string(name); 1604 } 1605 } 1606 return (result); 1607 } 1608 1609 /* 1610 * Update legacy files 1611 * 1612 * Provides functions to add/remove/modify individual entries 1613 * in dfstab and sharetab 1614 */ 1615 1616 void 1617 update_legacy_config(sa_handle_t handle) 1618 { 1619 /* 1620 * no longer used -- this is a placeholder in case we need to 1621 * add it back later. 1622 */ 1623 #ifdef lint 1624 handle = handle; 1625 #endif 1626 } 1627 1628 /* 1629 * sa_valid_property(handle, object, proto, property) 1630 * 1631 * check to see if the specified property is valid relative to the 1632 * specified protocol. The protocol plugin is called to do the work. 1633 */ 1634 1635 int 1636 sa_valid_property(sa_handle_t handle, void *object, char *proto, 1637 sa_property_t property) 1638 { 1639 int ret = SA_OK; 1640 1641 if (proto != NULL && property != NULL) { 1642 ret = sa_proto_valid_prop(handle, proto, property, object); 1643 } 1644 1645 return (ret); 1646 } 1647 1648 /* 1649 * sa_fstype(path) 1650 * 1651 * Given path, return the string representing the path's file system 1652 * type. This is used to discover ZFS shares. 1653 */ 1654 1655 char * 1656 sa_fstype(char *path) 1657 { 1658 int err; 1659 struct stat st; 1660 1661 err = stat(path, &st); 1662 if (err < 0) 1663 err = SA_NO_SUCH_PATH; 1664 else 1665 err = SA_OK; 1666 1667 /* 1668 * If we have a valid path at this point ret, return the fstype. 1669 */ 1670 if (err == SA_OK) 1671 return (strdup(st.st_fstype)); 1672 1673 return (NULL); 1674 } 1675 1676 void 1677 sa_free_fstype(char *type) 1678 { 1679 free(type); 1680 } 1681 1682 /* 1683 * sa_get_derived_optionset(object, proto, hier) 1684 * 1685 * Work backward to the top of the share object tree and start 1686 * copying protocol specific optionsets into a newly created 1687 * optionset that doesn't have a parent (it will be freed 1688 * later). This provides for the property inheritance model. That 1689 * is, properties closer to the share take precedence over group 1690 * level. This also provides for groups of groups in the future. 1691 */ 1692 1693 sa_optionset_t 1694 sa_get_derived_optionset(void *object, char *proto, int hier) 1695 { 1696 sa_optionset_t newoptionset; 1697 sa_optionset_t optionset; 1698 sa_group_t group; 1699 1700 if (hier && 1701 (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 1702 newoptionset = sa_get_derived_optionset((void *)group, proto, 1703 hier); 1704 } else { 1705 newoptionset = (sa_optionset_t)xmlNewNode(NULL, 1706 (xmlChar *)"optionset"); 1707 if (newoptionset != NULL) { 1708 sa_set_optionset_attr(newoptionset, "type", proto); 1709 } 1710 } 1711 /* Dont' do anything if memory wasn't allocated */ 1712 if (newoptionset == NULL) 1713 return (NULL); 1714 1715 /* Found the top so working back down the stack */ 1716 optionset = sa_get_optionset((sa_optionset_t)object, proto); 1717 if (optionset != NULL) { 1718 sa_property_t prop; 1719 /* add optionset to the newoptionset */ 1720 for (prop = sa_get_property(optionset, NULL); 1721 prop != NULL; 1722 prop = sa_get_next_property(prop)) { 1723 sa_property_t newprop; 1724 char *name; 1725 char *value; 1726 name = sa_get_property_attr(prop, "type"); 1727 value = sa_get_property_attr(prop, "value"); 1728 if (name == NULL) 1729 continue; 1730 newprop = sa_get_property(newoptionset, name); 1731 /* Replace the value with the new value */ 1732 if (newprop != NULL) { 1733 /* 1734 * Only set if value is non NULL, old value ok 1735 * if it is NULL. 1736 */ 1737 if (value != NULL) 1738 set_node_attr(newprop, "value", value); 1739 } else { 1740 /* an entirely new property */ 1741 if (value != NULL) { 1742 newprop = sa_create_property(name, 1743 value); 1744 if (newprop != NULL) { 1745 newprop = (sa_property_t) 1746 xmlAddChild( 1747 (xmlNodePtr)newoptionset, 1748 (xmlNodePtr)newprop); 1749 } 1750 } 1751 } 1752 sa_free_attr_string(name); 1753 1754 if (value != NULL) 1755 sa_free_attr_string(value); 1756 } 1757 } 1758 return (newoptionset); 1759 } 1760 1761 void 1762 sa_free_derived_optionset(sa_optionset_t optionset) 1763 { 1764 /* While it shouldn't be linked, it doesn't hurt */ 1765 if (optionset != NULL) { 1766 xmlUnlinkNode((xmlNodePtr) optionset); 1767 xmlFreeNode((xmlNodePtr) optionset); 1768 } 1769 } 1770 1771 /* 1772 * sa_get_all_security_types(object, proto, hier) 1773 * 1774 * Find all the security types set for this object. This is 1775 * preliminary to getting a derived security set. The return value is an 1776 * optionset containg properties which are the sectype values found by 1777 * walking up the XML document structure. The returned optionset 1778 * is a derived optionset. 1779 * 1780 * If hier is 0, only look at object. If non-zero, walk up the tree. 1781 */ 1782 sa_optionset_t 1783 sa_get_all_security_types(void *object, char *proto, int hier) 1784 { 1785 sa_optionset_t options; 1786 sa_security_t security; 1787 sa_group_t group; 1788 sa_property_t prop; 1789 1790 options = NULL; 1791 1792 if (hier && 1793 (group = sa_get_parent_group((sa_share_t)object)) != NULL) 1794 options = sa_get_all_security_types((void *)group, proto, hier); 1795 else 1796 options = (sa_optionset_t)xmlNewNode(NULL, 1797 (xmlChar *)"optionset"); 1798 1799 if (options == NULL) 1800 return (options); 1801 1802 /* Hit the top so collect the security types working back. */ 1803 for (security = sa_get_security((sa_group_t)object, NULL, NULL); 1804 security != NULL; 1805 security = sa_get_next_security(security)) { 1806 char *type; 1807 char *sectype; 1808 1809 type = sa_get_security_attr(security, "type"); 1810 if (type != NULL) { 1811 if (strcmp(type, proto) != 0) { 1812 sa_free_attr_string(type); 1813 continue; 1814 } 1815 sectype = sa_get_security_attr(security, "sectype"); 1816 if (sectype != NULL) { 1817 /* 1818 * Have a security type, check to see if 1819 * already present in optionset and add if it 1820 * isn't. 1821 */ 1822 if (sa_get_property(options, sectype) == NULL) { 1823 prop = sa_create_property(sectype, 1824 "true"); 1825 if (prop != NULL) 1826 prop = (sa_property_t) 1827 xmlAddChild( 1828 (xmlNodePtr)options, 1829 (xmlNodePtr)prop); 1830 } 1831 sa_free_attr_string(sectype); 1832 } 1833 sa_free_attr_string(type); 1834 } 1835 } 1836 1837 return (options); 1838 } 1839 1840 /* 1841 * sa_get_derived_security(object, sectype, proto, hier) 1842 * 1843 * Get the derived security(named optionset) for the object given the 1844 * sectype and proto. If hier is non-zero, walk up the tree to get all 1845 * properties defined for this object, otherwise just those on the 1846 * object. 1847 */ 1848 1849 sa_security_t 1850 sa_get_derived_security(void *object, char *sectype, char *proto, int hier) 1851 { 1852 sa_security_t newsecurity; 1853 sa_security_t security; 1854 sa_group_t group; 1855 sa_property_t prop; 1856 1857 if (hier && 1858 (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 1859 newsecurity = sa_get_derived_security((void *)group, 1860 sectype, proto, hier); 1861 } else { 1862 newsecurity = (sa_security_t)xmlNewNode(NULL, 1863 (xmlChar *)"security"); 1864 if (newsecurity != NULL) { 1865 sa_set_security_attr(newsecurity, "type", proto); 1866 sa_set_security_attr(newsecurity, "sectype", sectype); 1867 } 1868 } 1869 /* Don't do anything if memory wasn't allocated */ 1870 if (newsecurity == NULL) 1871 return (newsecurity); 1872 1873 /* Found the top so working back down the stack. */ 1874 security = sa_get_security((sa_security_t)object, sectype, proto); 1875 if (security == NULL) 1876 return (newsecurity); 1877 1878 /* add security to the newsecurity */ 1879 for (prop = sa_get_property(security, NULL); 1880 prop != NULL; prop = sa_get_next_property(prop)) { 1881 sa_property_t newprop; 1882 char *name; 1883 char *value; 1884 name = sa_get_property_attr(prop, "type"); 1885 value = sa_get_property_attr(prop, "value"); 1886 if (name != NULL) { 1887 newprop = sa_get_property(newsecurity, name); 1888 /* Replace the value with the new value */ 1889 if (newprop != NULL) { 1890 /* 1891 * Only set if value is non NULL, old 1892 * value ok if it is NULL. The value 1893 * must be associated with the "value" 1894 * tag within XML. 1895 */ 1896 if (value != NULL) 1897 set_node_attr(newprop, "value", value); 1898 } else { 1899 /* An entirely new property */ 1900 if (value != NULL) { 1901 newprop = sa_create_property(name, 1902 value); 1903 newprop = (sa_property_t) 1904 xmlAddChild((xmlNodePtr)newsecurity, 1905 (xmlNodePtr)newprop); 1906 } 1907 } 1908 sa_free_attr_string(name); 1909 } 1910 if (value != NULL) 1911 sa_free_attr_string(value); 1912 } 1913 return (newsecurity); 1914 } 1915 1916 void 1917 sa_free_derived_security(sa_security_t security) 1918 { 1919 /* while it shouldn't be linked, it doesn't hurt */ 1920 if (security != NULL) { 1921 xmlUnlinkNode((xmlNodePtr)security); 1922 xmlFreeNode((xmlNodePtr)security); 1923 } 1924 } 1925 1926 /* 1927 * sharetab utility functions 1928 * 1929 * Makes use of the original sharetab.c from fs.d/nfs/lib 1930 */ 1931 1932 /* 1933 * sa_fillshare(share, proto, sh) 1934 * 1935 * Fill the struct share with values obtained from the share object. 1936 */ 1937 void 1938 sa_fillshare(sa_share_t share, char *proto, struct share *sh) 1939 { 1940 char *groupname = NULL; 1941 char *value; 1942 sa_group_t group; 1943 char *buff; 1944 char *zfs; 1945 sa_resource_t resource; 1946 char *rsrcname = NULL; 1947 char *defprop; 1948 1949 /* 1950 * We only want to deal with the path level shares for the 1951 * sharetab file. If a resource, get the parent. 1952 */ 1953 if (sa_is_resource(share)) { 1954 resource = (sa_resource_t)share; 1955 share = sa_get_resource_parent(resource); 1956 rsrcname = sa_get_resource_attr(resource, "name"); 1957 } 1958 1959 group = sa_get_parent_group(share); 1960 if (group != NULL) { 1961 zfs = sa_get_group_attr(group, "zfs"); 1962 groupname = sa_get_group_attr(group, "name"); 1963 1964 if (groupname != NULL && 1965 (strcmp(groupname, "default") == 0 || zfs != NULL)) { 1966 /* 1967 * since the groupname is either "default" or the 1968 * group is a ZFS group, we don't want to keep 1969 * groupname. We do want it if it is any other type of 1970 * group. 1971 */ 1972 sa_free_attr_string(groupname); 1973 groupname = NULL; 1974 } 1975 if (zfs != NULL) 1976 sa_free_attr_string(zfs); 1977 } 1978 1979 value = sa_get_share_attr(share, "path"); 1980 if (value != NULL) { 1981 sh->sh_path = strdup(value); 1982 sa_free_attr_string(value); 1983 } 1984 1985 if (rsrcname != NULL || groupname != NULL) { 1986 int len = 0; 1987 1988 if (rsrcname != NULL) 1989 len += strlen(rsrcname); 1990 if (groupname != NULL) 1991 len += strlen(groupname); 1992 len += 3; /* worst case */ 1993 buff = malloc(len); 1994 (void) snprintf(buff, len, "%s%s%s", 1995 (rsrcname != NULL && 1996 strlen(rsrcname) > 0) ? rsrcname : "-", 1997 groupname != NULL ? "@" : "", 1998 groupname != NULL ? groupname : ""); 1999 sh->sh_res = buff; 2000 if (rsrcname != NULL) 2001 sa_free_attr_string(rsrcname); 2002 if (groupname != NULL) 2003 sa_free_attr_string(groupname); 2004 } else { 2005 sh->sh_res = strdup("-"); 2006 } 2007 2008 /* 2009 * Get correct default prop string. NFS uses "rw", others use 2010 * "". 2011 */ 2012 if (strcmp(proto, "nfs") != 0) 2013 defprop = "\"\""; 2014 else 2015 defprop = "rw"; 2016 2017 sh->sh_fstype = strdup(proto); 2018 value = sa_proto_legacy_format(proto, share, 1); 2019 if (value != NULL) { 2020 if (strlen(value) > 0) 2021 sh->sh_opts = strdup(value); 2022 else 2023 sh->sh_opts = strdup(defprop); 2024 free(value); 2025 } else { 2026 sh->sh_opts = strdup(defprop); 2027 } 2028 2029 value = sa_get_share_description(share); 2030 if (value != NULL) { 2031 sh->sh_descr = strdup(value); 2032 sa_free_share_description(value); 2033 } else { 2034 sh->sh_descr = strdup(""); 2035 } 2036 } 2037 2038 /* 2039 * sa_emptyshare(sh) 2040 * 2041 * Free the strings in the non-NULL members of sh. 2042 */ 2043 2044 void 2045 sa_emptyshare(struct share *sh) 2046 { 2047 if (sh->sh_path != NULL) 2048 free(sh->sh_path); 2049 sh->sh_path = NULL; 2050 if (sh->sh_res != NULL) 2051 free(sh->sh_res); 2052 sh->sh_res = NULL; 2053 if (sh->sh_fstype != NULL) 2054 free(sh->sh_fstype); 2055 sh->sh_fstype = NULL; 2056 if (sh->sh_opts != NULL) 2057 free(sh->sh_opts); 2058 sh->sh_opts = NULL; 2059 if (sh->sh_descr != NULL) 2060 free(sh->sh_descr); 2061 sh->sh_descr = NULL; 2062 } 2063 2064 /* 2065 * sa_update_sharetab_ts(handle) 2066 * 2067 * Update the internal timestamp of when sharetab was last 2068 * changed. This needs to be public for ZFS to get at it. 2069 */ 2070 2071 void 2072 sa_update_sharetab_ts(sa_handle_t handle) 2073 { 2074 struct stat st; 2075 sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 2076 2077 if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 2078 implhandle->tssharetab = TSTAMP(st.st_mtim); 2079 } 2080 2081 /* 2082 * sa_update_sharetab(share, proto) 2083 * 2084 * Update the sharetab file with info from the specified share. 2085 * This could be an update or add. 2086 */ 2087 2088 int 2089 sa_update_sharetab(sa_share_t share, char *proto) 2090 { 2091 int ret = SA_OK; 2092 share_t sh; 2093 char *path; 2094 sa_handle_t handle; 2095 2096 path = sa_get_share_attr(share, "path"); 2097 if (path != NULL) { 2098 (void) memset(&sh, '\0', sizeof (sh)); 2099 2100 handle = sa_find_group_handle((sa_group_t)share); 2101 if (handle != NULL) { 2102 /* 2103 * Fill in share structure and send it to the kernel. 2104 */ 2105 (void) sa_fillshare(share, proto, &sh); 2106 (void) _sharefs(SHAREFS_ADD, &sh); 2107 /* 2108 * We need the timestamp of the sharetab file right 2109 * after the update was done. This lets us detect a 2110 * change that made by a different process. 2111 */ 2112 sa_update_sharetab_ts(handle); 2113 sa_emptyshare(&sh); 2114 } else { 2115 ret = SA_CONFIG_ERR; 2116 } 2117 sa_free_attr_string(path); 2118 } 2119 2120 return (ret); 2121 } 2122 2123 /* 2124 * sa_delete_sharetab(handle, path, proto) 2125 * 2126 * remove the specified share from sharetab. 2127 */ 2128 2129 int 2130 sa_delete_sharetab(sa_handle_t handle, char *path, char *proto) 2131 { 2132 int ret = SA_OK; 2133 struct stat st; 2134 2135 share_t sh; 2136 /* 2137 * Both the path and the proto are 2138 * keys into the sharetab. 2139 */ 2140 if (path != NULL && proto != NULL) { 2141 (void) memset(&sh, '\0', sizeof (sh)); 2142 sh.sh_path = path; 2143 sh.sh_fstype = proto; 2144 2145 ret = _sharefs(SHAREFS_REMOVE, &sh); 2146 if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 2147 sa_update_sharetab_ts(handle); 2148 } 2149 return (ret); 2150 } 2151 2152 /* 2153 * sa_needs_refresh(handle) 2154 * 2155 * Returns B_TRUE if the internal cache needs to be refreshed do to a 2156 * change by another process. B_FALSE returned otherwise. 2157 */ 2158 boolean_t 2159 sa_needs_refresh(sa_handle_t handle) 2160 { 2161 sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 2162 struct stat st; 2163 char *str; 2164 uint64_t tstamp; 2165 scf_simple_prop_t *prop; 2166 2167 if (handle == NULL) 2168 return (B_TRUE); 2169 2170 /* 2171 * If sharetab has changed, then there was an external 2172 * change. Check sharetab first since it is updated by ZFS as 2173 * well as sharemgr. This is where external ZFS changes are 2174 * caught. 2175 */ 2176 if (stat(SA_LEGACY_SHARETAB, &st) == 0 && 2177 TSTAMP(st.st_mtim) != implhandle->tssharetab) 2178 return (B_TRUE); 2179 2180 /* 2181 * If sharetab wasn't changed, check whether there were any 2182 * SMF transactions that modified the config but didn't 2183 * initiate a share. This is less common but does happen. 2184 */ 2185 prop = scf_simple_prop_get(implhandle->scfhandle->handle, 2186 (const char *)SA_SVC_FMRI_BASE ":default", "state", 2187 "lastupdate"); 2188 if (prop != NULL) { 2189 str = scf_simple_prop_next_astring(prop); 2190 if (str != NULL) 2191 tstamp = strtoull(str, NULL, 0); 2192 else 2193 tstamp = 0; 2194 scf_simple_prop_free(prop); 2195 if (tstamp != implhandle->tstrans) 2196 return (B_TRUE); 2197 } 2198 2199 return (B_FALSE); 2200 } 2201 2202 /* 2203 * sa_fix_resource_name(path) 2204 * 2205 * change all illegal characters to something else. For now, all get 2206 * converted to '_' and the leading '/' is stripped off. This is used 2207 * to construct an resource name (SMB share name) that is valid. 2208 * Caller must pass a valid path. 2209 */ 2210 void 2211 sa_fix_resource_name(char *path) 2212 { 2213 char *cp; 2214 size_t len; 2215 2216 assert(path != NULL); 2217 2218 /* make sure we are appropriate length */ 2219 cp = path; 2220 if (*cp == '/') 2221 cp++; /* skip leading slash */ 2222 while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) { 2223 cp = strchr(cp, '/'); 2224 if (cp != NULL) 2225 cp++; 2226 } 2227 /* two cases - cp == NULL and cp is substring of path */ 2228 if (cp == NULL) { 2229 /* just take last SA_MAX_RESOURCE_NAME chars */ 2230 len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME; 2231 (void) memmove(path, path + len, SA_MAX_RESOURCE_NAME); 2232 path[SA_MAX_RESOURCE_NAME] = '\0'; 2233 } else { 2234 len = strlen(cp) + 1; 2235 (void) memmove(path, cp, len); 2236 } 2237 2238 /* 2239 * Don't want any of the characters that are not allowed 2240 * in an SMB share name. Replace them with '_'. 2241 */ 2242 while (*path) { 2243 switch (*path) { 2244 case '/': 2245 case '"': 2246 case '\\': 2247 case '[': 2248 case ']': 2249 case ':': 2250 case '|': 2251 case '<': 2252 case '>': 2253 case '+': 2254 case ';': 2255 case ',': 2256 case '?': 2257 case '*': 2258 case '=': 2259 case '\t': 2260 *path = '_'; 2261 break; 2262 } 2263 path++; 2264 } 2265 } 2266