forked from illumos/illumos-gate
/
commands.c
6021 lines (5633 loc) · 139 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2012 Milan Jurik. All rights reserved.
* Copyright 2018 Nexenta Systems, Inc.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <utmpx.h>
#include <pwd.h>
#include <auth_attr.h>
#include <secdb.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <libshare.h>
#include "sharemgr.h"
#include <libscf.h>
#include <libxml/tree.h>
#include <libintl.h>
#include <assert.h>
#include <iconv.h>
#include <langinfo.h>
#include <dirent.h>
static char *sa_get_usage(sa_usage_t);
/*
* Implementation of the common sub-commands supported by sharemgr.
* A number of helper functions are also included.
*/
/*
* has_protocol(group, proto)
* If the group has an optionset with the specified protocol,
* return true (1) otherwise false (0).
*/
static int
has_protocol(sa_group_t group, char *protocol)
{
sa_optionset_t optionset;
int result = 0;
optionset = sa_get_optionset(group, protocol);
if (optionset != NULL) {
result++;
}
return (result);
}
/*
* validresource(name)
*
* Check that name only has valid characters in it. The current valid
* set are the printable characters but not including:
* " / \ [ ] : | < > + ; , ? * = \t
* Note that space is included and there is a maximum length.
*/
static int
validresource(const char *name)
{
const char *cp;
size_t len;
if (name == NULL)
return (B_FALSE);
len = strlen(name);
if (len == 0 || len > SA_MAX_RESOURCE_NAME)
return (B_FALSE);
if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
return (B_FALSE);
}
for (cp = name; *cp != '\0'; cp++)
if (iscntrl(*cp))
return (B_FALSE);
return (B_TRUE);
}
/*
* conv_to_utf8(input)
*
* Convert the input string to utf8 from the current locale. If the
* conversion fails, use the current locale, it is likely close
* enough. For example, the "C" locale is a subset of utf-8. The
* return value may be a new string or the original input string.
*/
static char *
conv_to_utf8(char *input)
{
iconv_t cd;
char *inval = input;
char *output = input;
char *outleft;
char *curlocale;
size_t bytesleft;
size_t size;
size_t osize;
static int warned = 0;
curlocale = nl_langinfo(CODESET);
if (curlocale == NULL)
curlocale = "C";
cd = iconv_open("UTF-8", curlocale);
if (cd != NULL && cd != (iconv_t)-1) {
size = strlen(input);
/* Assume worst case of characters expanding to 4 bytes. */
bytesleft = size * 4;
output = calloc(bytesleft, 1);
if (output != NULL) {
outleft = output;
/* inval can be modified on return */
osize = iconv(cd, (const char **)&inval, &size,
&outleft, &bytesleft);
if (osize == (size_t)-1 || size != 0) {
free(output);
output = input;
}
} else {
/* Need to return something. */
output = input;
}
(void) iconv_close(cd);
} else {
if (!warned)
(void) fprintf(stderr,
gettext("Cannot convert to UTF-8 from %s\n"),
curlocale ? curlocale : gettext("unknown"));
warned = 1;
}
return (output);
}
/*
* conv_from(input)
*
* Convert the input string from utf8 to current locale. If the
* conversion isn't supported, just use as is. The return value may be
* a new string or the original input string.
*/
static char *
conv_from_utf8(char *input)
{
iconv_t cd;
char *output = input;
char *inval = input;
char *outleft;
char *curlocale;
size_t bytesleft;
size_t size;
size_t osize;
static int warned = 0;
curlocale = nl_langinfo(CODESET);
if (curlocale == NULL)
curlocale = "C";
cd = iconv_open(curlocale, "UTF-8");
if (cd != NULL && cd != (iconv_t)-1) {
size = strlen(input);
/* Assume worst case of characters expanding to 4 bytes. */
bytesleft = size * 4;
output = calloc(bytesleft, 1);
if (output != NULL) {
outleft = output;
osize = iconv(cd, (const char **)&inval, &size,
&outleft, &bytesleft);
if (osize == (size_t)-1 || size != 0)
output = input;
} else {
/* Need to return something. */
output = input;
}
(void) iconv_close(cd);
} else {
if (!warned)
(void) fprintf(stderr,
gettext("Cannot convert to %s from UTF-8\n"),
curlocale ? curlocale : gettext("unknown"));
warned = 1;
}
return (output);
}
/*
* print_rsrc_desc(resource, sharedesc)
*
* Print the resource description string after converting from UTF8 to
* the current locale. If sharedesc is not NULL and there is no
* description on the resource, use sharedesc. sharedesc will already
* be converted to UTF8.
*/
static void
print_rsrc_desc(sa_resource_t resource, char *sharedesc)
{
char *description;
char *desc;
if (resource == NULL)
return;
description = sa_get_resource_description(resource);
if (description != NULL) {
desc = conv_from_utf8(description);
if (desc != description) {
sa_free_share_description(description);
description = desc;
}
} else if (sharedesc != NULL) {
description = strdup(sharedesc);
}
if (description != NULL) {
(void) printf("\t\"%s\"", description);
sa_free_share_description(description);
}
}
/*
* set_resource_desc(share, description)
*
* Set the share description value after converting the description
* string to UTF8 from the current locale.
*/
static int
set_resource_desc(sa_share_t share, char *description)
{
char *desc;
int ret;
desc = conv_to_utf8(description);
ret = sa_set_resource_description(share, desc);
if (description != desc)
sa_free_share_description(desc);
return (ret);
}
/*
* set_share_desc(share, description)
*
* Set the resource description value after converting the description
* string to UTF8 from the current locale.
*/
static int
set_share_desc(sa_share_t share, char *description)
{
char *desc;
int ret;
desc = conv_to_utf8(description);
ret = sa_set_share_description(share, desc);
if (description != desc)
sa_free_share_description(desc);
return (ret);
}
/*
* add_list(list, item, data, proto)
* Adds a new list member that points holds item in the list.
* If list is NULL, it starts a new list. The function returns
* the first member of the list.
*/
struct list *
add_list(struct list *listp, void *item, void *data, char *proto)
{
struct list *new, *tmp;
new = malloc(sizeof (struct list));
if (new != NULL) {
new->next = NULL;
new->item = item;
new->itemdata = data;
new->proto = proto;
} else {
return (listp);
}
if (listp == NULL)
return (new);
for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
/* get to end of list */
}
tmp->next = new;
return (listp);
}
/*
* free_list(list)
* Given a list, free all the members of the list;
*/
static void
free_list(struct list *listp)
{
struct list *tmp;
while (listp != NULL) {
tmp = listp;
listp = listp->next;
free(tmp);
}
}
/*
* check_authorization(instname, which)
*
* Checks to see if the specific type of authorization in which is
* enabled for the user in this SMF service instance.
*/
static int
check_authorization(char *instname, int which)
{
scf_handle_t *handle = NULL;
scf_simple_prop_t *prop = NULL;
char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
char *authstr = NULL;
ssize_t numauths;
int ret = B_TRUE;
uid_t uid;
struct passwd *pw = NULL;
uid = getuid();
pw = getpwuid(uid);
if (pw == NULL) {
ret = B_FALSE;
} else {
/*
* Since names are restricted to SA_MAX_NAME_LEN won't
* overflow.
*/
(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
SA_SVC_FMRI_BASE, instname);
handle = scf_handle_create(SCF_VERSION);
if (handle != NULL) {
if (scf_handle_bind(handle) == 0) {
switch (which) {
case SVC_SET:
prop = scf_simple_prop_get(handle,
svcstring, "general",
SVC_AUTH_VALUE);
break;
case SVC_ACTION:
prop = scf_simple_prop_get(handle,
svcstring, "general",
SVC_AUTH_ACTION);
break;
}
}
}
}
/* make sure we have an authorization string property */
if (prop != NULL) {
int i;
numauths = scf_simple_prop_numvalues(prop);
for (ret = 0, i = 0; i < numauths; i++) {
authstr = scf_simple_prop_next_astring(prop);
if (authstr != NULL) {
/* check if this user has one of the strings */
if (chkauthattr(authstr, pw->pw_name)) {
ret = 1;
break;
}
}
}
endauthattr();
scf_simple_prop_free(prop);
} else {
/* no authorization string defined */
ret = 0;
}
if (handle != NULL)
scf_handle_destroy(handle);
return (ret);
}
/*
* check_authorizations(instname, flags)
*
* check all the needed authorizations for the user in this service
* instance. Return value of 1(true) or 0(false) indicates whether
* there are authorizations for the user or not.
*/
static int
check_authorizations(char *instname, int flags)
{
int ret1 = 0;
int ret2 = 0;
int ret;
if (flags & SVC_SET)
ret1 = check_authorization(instname, SVC_SET);
if (flags & SVC_ACTION)
ret2 = check_authorization(instname, SVC_ACTION);
switch (flags) {
case SVC_ACTION:
ret = ret2;
break;
case SVC_SET:
ret = ret1;
break;
case SVC_ACTION|SVC_SET:
ret = ret1 & ret2;
break;
default:
/* if not flags set, we assume we don't need authorizations */
ret = 1;
}
return (ret);
}
/*
* notify_or_enable_share(share, protocol)
*
* Since some protocols don't want an "enable" when properties change,
* this function will use the protocol specific notify function
* first. If that fails, it will then attempt to use the
* sa_enable_share(). "protocol" is the protocol that was specified
* on the command line.
*/
static void
notify_or_enable_share(sa_share_t share, char *protocol)
{
sa_group_t group;
sa_optionset_t opt;
int ret = SA_OK;
char *path;
char *groupproto;
sa_share_t parent = share;
/* If really a resource, get parent share */
if (!sa_is_share(share)) {
parent = sa_get_resource_parent((sa_resource_t)share);
}
/*
* Now that we've got a share in "parent", make sure it has a path.
*/
path = sa_get_share_attr(parent, "path");
if (path == NULL)
return;
group = sa_get_parent_group(parent);
if (group == NULL) {
sa_free_attr_string(path);
return;
}
for (opt = sa_get_optionset(group, NULL);
opt != NULL;
opt = sa_get_next_optionset(opt)) {
groupproto = sa_get_optionset_attr(opt, "type");
if (groupproto == NULL ||
(protocol != NULL && strcmp(groupproto, protocol) != 0)) {
if (groupproto != NULL)
sa_free_attr_string(groupproto);
continue;
}
if (sa_is_share(share)) {
if ((ret = sa_proto_change_notify(share,
groupproto)) != SA_OK) {
ret = sa_enable_share(share, groupproto);
if (ret != SA_OK) {
(void) printf(
gettext("Could not reenable"
" share %s: %s\n"),
path, sa_errorstr(ret));
}
}
} else {
/* Must be a resource */
if ((ret = sa_proto_notify_resource(share,
groupproto)) != SA_OK) {
ret = sa_enable_resource(share, groupproto);
if (ret != SA_OK) {
(void) printf(
gettext("Could not "
"reenable resource %s: "
"%s\n"), path,
sa_errorstr(ret));
}
}
}
sa_free_attr_string(groupproto);
}
sa_free_attr_string(path);
}
/*
* enable_group(group, updateproto, notify, proto)
*
* enable all the shares in the specified group. This is a helper for
* enable_all_groups in order to simplify regular and subgroup (zfs)
* enabling. Group has already been checked for non-NULL. If notify
* is non-zero, attempt to use the notify interface rather than
* enable.
*/
static void
enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
{
sa_share_t share;
/* If the protocol isn't enabled for this group skip it */
if (!has_protocol(group, proto))
return;
for (share = sa_get_share(group, NULL);
share != NULL;
share = sa_get_next_share(share)) {
if (updateproto != NULL)
(void) sa_update_legacy(share, updateproto);
if (notify)
notify_or_enable_share(share, proto);
else
(void) sa_enable_share(share, proto);
}
}
/*
* isenabled(group)
*
* Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
* Moved to separate function to reduce clutter in the code.
*/
static int
isenabled(sa_group_t group)
{
char *state;
int ret = B_FALSE;
if (group != NULL) {
state = sa_get_group_attr(group, "state");
if (state != NULL) {
if (strcmp(state, "enabled") == 0)
ret = B_TRUE;
sa_free_attr_string(state);
}
}
return (ret);
}
/*
* enable_all_groups(list, setstate, online, updateproto)
*
* Given a list of groups, enable each one found. If updateproto is
* not NULL, then update all the shares for the protocol that was
* passed in. If enable is non-zero, tell enable_group to try the
* notify interface since this is a property change.
*/
static int
enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
int online, char *updateproto, int enable)
{
int ret;
char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
char *state;
char *name;
char *zfs = NULL;
sa_group_t group;
sa_group_t subgroup;
for (ret = SA_OK; work != NULL; work = work->next) {
group = (sa_group_t)work->item;
/*
* If setstate == TRUE, then make sure to set
* enabled. This needs to be done here in order for
* the isenabled check to succeed on a newly enabled
* group.
*/
if (setstate == B_TRUE) {
ret = sa_set_group_attr(group, "state", "enabled");
if (ret != SA_OK)
break;
}
/*
* Check to see if group is enabled. If it isn't, skip
* the rest. We don't want shares starting if the
* group is disabled. The properties may have been
* updated, but there won't be a change until the
* group is enabled.
*/
if (!isenabled(group))
continue;
/* if itemdata != NULL then a single share */
if (work->itemdata != NULL) {
if (enable) {
if (work->itemdata != NULL)
notify_or_enable_share(work->itemdata,
updateproto);
else
ret = SA_CONFIG_ERR;
} else {
if (sa_is_share(work->itemdata)) {
ret = sa_enable_share(
(sa_share_t)work->itemdata,
updateproto);
} else {
ret = sa_enable_resource(
(sa_resource_t)work->itemdata,
updateproto);
}
}
}
if (ret != SA_OK)
break;
/* if itemdata == NULL then the whole group */
if (work->itemdata == NULL) {
zfs = sa_get_group_attr(group, "zfs");
/*
* If the share is managed by ZFS, don't
* update any of the protocols since ZFS is
* handling this. Updateproto will contain
* the name of the protocol that we want to
* update legacy files for.
*/
enable_group(group, zfs == NULL ? updateproto : NULL,
enable, work->proto);
if (zfs != NULL)
sa_free_attr_string(zfs);
for (subgroup = sa_get_sub_group(group);
subgroup != NULL;
subgroup = sa_get_next_group(subgroup)) {
/* never update legacy for ZFS subgroups */
enable_group(subgroup, NULL, enable,
work->proto);
}
}
if (online) {
zfs = sa_get_group_attr(group, "zfs");
name = sa_get_group_attr(group, "name");
if (name != NULL) {
if (zfs == NULL) {
(void) snprintf(instance,
sizeof (instance), "%s:%s",
SA_SVC_FMRI_BASE, name);
state = smf_get_state(instance);
if (state == NULL ||
strcmp(state, "online") != 0) {
(void) smf_enable_instance(
instance, 0);
free(state);
}
} else {
sa_free_attr_string(zfs);
zfs = NULL;
}
if (name != NULL)
sa_free_attr_string(name);
}
}
}
if (ret == SA_OK) {
ret = sa_update_config(handle);
}
return (ret);
}
/*
* chk_opt(optlistp, security, proto)
*
* Do a sanity check on the optlist provided for the protocol. This
* is a syntax check and verification that the property is either a
* general or specific to a names optionset.
*/
static int
chk_opt(struct options *optlistp, int security, char *proto)
{
struct options *optlist;
char *sep = "";
int notfirst = 0;
int ret;
for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
char *optname;
optname = optlist->optname;
ret = OPT_ADD_OK;
/* extract property/value pair */
if (sa_is_security(optname, proto)) {
if (!security)
ret = OPT_ADD_SECURITY;
} else {
if (security)
ret = OPT_ADD_PROPERTY;
}
if (ret != OPT_ADD_OK) {
if (notfirst == 0)
(void) printf(
gettext("Property syntax error: "));
switch (ret) {
case OPT_ADD_SYNTAX:
(void) printf(gettext("%ssyntax error: %s"),
sep, optname);
sep = ", ";
break;
case OPT_ADD_SECURITY:
(void) printf(gettext("%s%s requires -S"),
optname, sep);
sep = ", ";
break;
case OPT_ADD_PROPERTY:
(void) printf(
gettext("%s%s not supported with -S"),
optname, sep);
sep = ", ";
break;
}
notfirst++;
}
}
if (notfirst) {
(void) printf("\n");
ret = SA_SYNTAX_ERR;
}
return (ret);
}
/*
* free_opt(optlist)
* Free the specified option list.
*/
static void
free_opt(struct options *optlist)
{
struct options *nextopt;
while (optlist != NULL) {
nextopt = optlist->next;
free(optlist);
optlist = nextopt;
}
}
/*
* check property list for valid properties
* A null value is a remove which is always valid.
*/
static int
valid_options(sa_handle_t handle, struct options *optlist, char *proto,
void *object, char *sec)
{
int ret = SA_OK;
struct options *cur;
sa_property_t prop;
sa_optionset_t parent = NULL;
if (object != NULL) {
if (sec == NULL)
parent = sa_get_optionset(object, proto);
else
parent = sa_get_security(object, sec, proto);
}
for (cur = optlist; cur != NULL; cur = cur->next) {
if (cur->optvalue == NULL)
continue;
prop = sa_create_property(cur->optname, cur->optvalue);
if (prop == NULL)
ret = SA_NO_MEMORY;
if (ret != SA_OK ||
(ret = sa_valid_property(handle, parent, proto, prop)) !=
SA_OK) {
(void) printf(
gettext("Could not add property %s: %s\n"),
cur->optname, sa_errorstr(ret));
}
(void) sa_remove_property(prop);
}
return (ret);
}
/*
* add_optionset(group, optlist, protocol, *err)
* Add the options in optlist to an optionset and then add the optionset
* to the group.
*
* The return value indicates if there was a "change" while errors are
* returned via the *err parameters.
*/
static int
add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
{
sa_optionset_t optionset;
int ret = SA_OK;
int result = B_FALSE;
sa_handle_t handle;
optionset = sa_get_optionset(group, proto);
if (optionset == NULL) {
optionset = sa_create_optionset(group, proto);
if (optionset == NULL)
ret = SA_NO_MEMORY;
result = B_TRUE; /* adding a protocol is a change */
}
if (optionset == NULL) {
ret = SA_NO_MEMORY;
goto out;
}
handle = sa_find_group_handle(group);
if (handle == NULL) {
ret = SA_CONFIG_ERR;
goto out;
}
while (optlist != NULL) {
sa_property_t prop;
prop = sa_get_property(optionset, optlist->optname);
if (prop == NULL) {
/*
* add the property, but only if it is
* a non-NULL or non-zero length value
*/
if (optlist->optvalue != NULL) {
prop = sa_create_property(optlist->optname,
optlist->optvalue);
if (prop != NULL) {
ret = sa_valid_property(handle,
optionset, proto, prop);
if (ret != SA_OK) {
(void) sa_remove_property(prop);
(void) printf(gettext("Could "
"not add property "
"%s: %s\n"),
optlist->optname,
sa_errorstr(ret));
}
}
if (ret == SA_OK) {
ret = sa_add_property(optionset, prop);
if (ret != SA_OK) {
(void) printf(gettext(
"Could not add property "
"%s: %s\n"),
optlist->optname,
sa_errorstr(ret));
} else {
/* there was a change */
result = B_TRUE;
}
}
}
} else {
ret = sa_update_property(prop, optlist->optvalue);
/* should check to see if value changed */
if (ret != SA_OK) {
(void) printf(gettext("Could not update "
"property %s: %s\n"), optlist->optname,
sa_errorstr(ret));
} else {
result = B_TRUE;
}
}
optlist = optlist->next;
}
ret = sa_commit_properties(optionset, 0);
out:
if (err != NULL)
*err = ret;
return (result);
}
/*
* resource_compliant(group)
*
* Go through all the shares in the group. Assume compliant, but if
* any share doesn't have at least one resource name, it isn't
* compliant.
*/
static int
resource_compliant(sa_group_t group)
{
sa_share_t share;
for (share = sa_get_share(group, NULL); share != NULL;
share = sa_get_next_share(share)) {
if (sa_get_share_resource(share, NULL) == NULL) {
return (B_FALSE);
}
}
return (B_TRUE);
}
/*
* fix_path(path)
*
* change all illegal characters to something else. For now, all get
* converted to '_' and the leading '/' is stripped off. This is used
* to construct an resource name (SMB share name) that is valid.
* Caller must pass a valid path.
*/
static void
fix_path(char *path)
{
char *cp;
size_t len;
assert(path != NULL);
/* make sure we are appropriate length */
cp = path + 1; /* skip leading slash */
while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
cp = strchr(cp, '/');
if (cp != NULL)
cp++;
}
/* two cases - cp == NULL and cp is substring of path */
if (cp == NULL) {
/* just take last SA_MAX_RESOURCE_NAME chars */
len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
path[SA_MAX_RESOURCE_NAME] = '\0';
} else {
len = strlen(cp) + 1;
(void) memmove(path, cp, len);
}
/*
* Don't want any of the characters that are not allowed
* in and SMB share name. Replace them with '_'.
*/
while (*path) {
switch (*path) {
case '/':
case '"':
case '\\':
case '[':
case ']':
case ':':
case '|':
case '<':
case '>':
case '+':
case ';':
case ',':
case '?':
case '*':
case '=':
case '\t':
*path = '_';
break;
}
path++;
}
}
/*
* name_adjust(path, count)
*
* Add a ~<count> in place of last few characters. The total number of
* characters is dependent on count.
*/
#define MAX_MANGLE_NUMBER 10000
static int