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