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