Print this page
11083 support NFS server in zone
Portions contributed by: Dan Kruchinin <dan.kruchinin@nexenta.com>
Portions contributed by: Stepan Zastupov <stepan.zastupov@gmail.com>
Portions contributed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Portions contributed by: Mike Zeller <mike@mikezeller.net>
Portions contributed by: Dan McDonald <danmcd@joyent.com>
Portions contributed by: Gordon Ross <gordon.w.ross@gmail.com>
Portions contributed by: Vitaliy Gusev <gusev.vitaliy@gmail.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>
Reviewed by: Rob Gittins <rob.gittins@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Jason King <jbk@joyent.com>
Reviewed by: C Fraire <cfraire@me.com>
Change-Id: I22f289d357503f9b48a0bc2482cc4328a6d43d16
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libshare/smb/libshare_smb.c
+++ new/usr/src/lib/libshare/smb/libshare_smb.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
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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 /*
28 28 * SMB specific functions
29 29 */
30 30 #include <stdio.h>
31 31 #include <string.h>
32 32 #include <ctype.h>
33 33 #include <stdlib.h>
34 34 #include <unistd.h>
35 35 #include <zone.h>
36 36 #include <errno.h>
37 37 #include <locale.h>
38 38 #include <fcntl.h>
39 39 #include <sys/types.h>
40 40 #include <sys/stat.h>
41 41 #include <syslog.h>
42 42 #include "libshare.h"
43 43 #include "libshare_impl.h"
44 44 #include <pwd.h>
45 45 #include <limits.h>
46 46 #include <libscf.h>
47 47 #include <libscf_priv.h>
48 48 #include <strings.h>
49 49 #include "libshare_smb.h"
50 50 #include <rpcsvc/daemon_utils.h>
51 51 #include <smbsrv/smb_share.h>
52 52 #include <smbsrv/smbinfo.h>
53 53 #include <smbsrv/libsmb.h>
54 54 #include <libdlpi.h>
55 55
56 56 #define SMB_CSC_BUFSZ 64
57 57
58 58 #define SMB_VALID_SUB_CHRS "UDhMLmIiSPu" /* substitution characters */
59 59
60 60 /* internal functions */
61 61 static int smb_share_init(void);
62 62 static void smb_share_fini(void);
63 63 static int smb_enable_share(sa_share_t);
64 64 static int smb_share_changed(sa_share_t);
65 65 static int smb_resource_changed(sa_resource_t);
66 66 static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
67 67 static int smb_disable_share(sa_share_t share, char *);
68 68 static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
69 69 static int smb_set_proto_prop(sa_property_t);
70 70 static sa_protocol_properties_t smb_get_proto_set(void);
71 71 static char *smb_get_status(void);
72 72 static int smb_parse_optstring(sa_group_t, char *);
73 73 static char *smb_format_options(sa_group_t, int);
74 74
75 75 static int smb_enable_service(void);
76 76
77 77 static int range_check_validator(int, char *);
78 78 static int range_check_validator_zero_ok(int, char *);
79 79 static int string_length_check_validator(int, char *);
80 80 static int print_enable_validator(int, char *);
81 81 static int true_false_validator(int, char *);
82 82 static int ipv4_validator(int, char *);
83 83 static int hostname_validator(int, char *);
84 84 static int path_validator(int, char *);
85 85 static int cmd_validator(int, char *);
86 86 static int disposition_validator(int, char *);
87 87 static int protocol_validator(int, char *);
88 88 static int require_validator(int, char *);
89 89
90 90 static int smb_enable_resource(sa_resource_t);
91 91 static int smb_disable_resource(sa_resource_t);
92 92 static uint64_t smb_share_features(void);
93 93 static int smb_list_transient(sa_handle_t);
94 94
95 95 static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *);
96 96 static void smb_csc_option(const char *, smb_share_t *);
97 97 static char *smb_csc_name(const smb_share_t *);
98 98 static sa_group_t smb_get_defaultgrp(sa_handle_t);
99 99 static int interface_validator(int, char *);
100 100 static int smb_update_optionset_props(sa_handle_t, sa_resource_t, nvlist_t *);
101 101
102 102 static boolean_t smb_saprop_getbool(sa_optionset_t, char *, boolean_t);
103 103 static boolean_t smb_saprop_getstr(sa_optionset_t, char *, char *, size_t);
104 104
105 105 static struct {
106 106 char *value;
107 107 uint32_t flag;
108 108 } cscopt[] = {
109 109 { "disabled", SMB_SHRF_CSC_DISABLED },
110 110 { "manual", SMB_SHRF_CSC_MANUAL },
111 111 { "auto", SMB_SHRF_CSC_AUTO },
112 112 { "vdo", SMB_SHRF_CSC_VDO }
113 113 };
114 114
115 115 /* size of basic format allocation */
116 116 #define OPT_CHUNK 1024
117 117
118 118 /* size of string for types - big enough to hold "dependency" */
119 119 #define SCFTYPE_LEN 32
120 120
121 121 /*
122 122 * Indexes of entries in smb_proto_options table.
123 123 * Changes to smb_proto_options table may require
124 124 * an update to these values.
125 125 */
126 126 #define PROTO_OPT_WINS1 6
127 127 #define PROTO_OPT_WINS_EXCLUDE 8
128 128
129 129 typedef struct smb_hostifs_walker {
130 130 const char *hiw_ifname;
131 131 boolean_t hiw_matchfound;
132 132 } smb_hostifs_walker_t;
133 133
134 134
135 135 /*
136 136 * ops vector that provides the protocol specific info and operations
137 137 * for share management.
138 138 */
139 139
140 140 struct sa_plugin_ops sa_plugin_ops = {
141 141 SA_PLUGIN_VERSION,
142 142 SMB_PROTOCOL_NAME,
143 143 smb_share_init,
144 144 smb_share_fini,
145 145 smb_enable_share,
146 146 smb_disable_share,
147 147 smb_validate_property,
148 148 NULL, /* valid_space */
149 149 NULL, /* security_prop */
150 150 smb_parse_optstring,
151 151 smb_format_options,
152 152 smb_set_proto_prop,
153 153 smb_get_proto_set,
154 154 smb_get_status,
155 155 NULL, /* space_alias */
156 156 NULL, /* update_legacy */
157 157 NULL, /* delete_legacy */
158 158 smb_share_changed,
159 159 smb_enable_resource,
160 160 smb_disable_resource,
161 161 smb_share_features,
162 162 smb_list_transient,
163 163 smb_resource_changed,
164 164 smb_rename_resource,
165 165 NULL, /* run_command */
166 166 NULL, /* command_help */
167 167 NULL /* delete_proto_section */
168 168 };
169 169
170 170 struct option_defs optdefs[] = {
171 171 { SHOPT_AD_CONTAINER, OPT_TYPE_STRING },
172 172 { SHOPT_ABE, OPT_TYPE_BOOLEAN },
173 173 { SHOPT_NAME, OPT_TYPE_NAME },
174 174 { SHOPT_RO, OPT_TYPE_ACCLIST },
175 175 { SHOPT_RW, OPT_TYPE_ACCLIST },
176 176 { SHOPT_NONE, OPT_TYPE_ACCLIST },
177 177 { SHOPT_CATIA, OPT_TYPE_BOOLEAN },
178 178 { SHOPT_CSC, OPT_TYPE_CSC },
179 179 { SHOPT_GUEST, OPT_TYPE_BOOLEAN },
180 180 { SHOPT_DFSROOT, OPT_TYPE_BOOLEAN },
181 181 { SHOPT_DESCRIPTION, OPT_TYPE_STRING },
182 182 { SHOPT_CA, OPT_TYPE_BOOLEAN },
183 183 { SHOPT_FSO, OPT_TYPE_BOOLEAN },
184 184 { SHOPT_QUOTAS, OPT_TYPE_BOOLEAN },
185 185 { SHOPT_ENCRYPT, OPT_TYPE_STRING },
186 186 { NULL, 0 }
187 187 };
188 188
189 189 /*
190 190 * findopt(name)
191 191 *
192 192 * Lookup option "name" in the option table and return the table
193 193 * index.
194 194 */
195 195 static int
196 196 findopt(char *name)
197 197 {
198 198 int i;
199 199 if (name != NULL) {
200 200 for (i = 0; optdefs[i].tag != NULL; i++) {
201 201 if (strcmp(optdefs[i].tag, name) == 0)
202 202 return (i);
203 203 }
204 204 }
205 205 return (-1);
206 206 }
207 207
208 208 /*
209 209 * is_a_number(number)
210 210 *
211 211 * is the string a number in one of the forms we want to use?
212 212 */
213 213 static boolean_t
214 214 is_a_number(char *number)
215 215 {
216 216 boolean_t isnum = B_TRUE;
217 217 boolean_t ishex = B_FALSE;
218 218
219 219 if (number == NULL || *number == '\0')
220 220 return (B_FALSE);
221 221
222 222 if (strncasecmp(number, "0x", 2) == 0) {
223 223 number += 2;
224 224 ishex = B_TRUE;
225 225 } else if (*number == '-') {
226 226 number++;
227 227 }
228 228
229 229 while (isnum && (*number != '\0')) {
230 230 isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
231 231 number++;
232 232 }
233 233
234 234 return (isnum);
235 235 }
236 236
237 237 /*
238 238 * check ro vs rw values. Over time this may get beefed up.
239 239 * for now it just does simple checks.
240 240 */
241 241
242 242 static int
243 243 check_rorw(char *v1, char *v2)
244 244 {
245 245 int ret = SA_OK;
246 246 if (strcmp(v1, v2) == 0)
247 247 ret = SA_VALUE_CONFLICT;
248 248 return (ret);
249 249 }
250 250
251 251 /*
252 252 * validresource(name)
253 253 *
254 254 * Check that name only has valid characters in it. The current valid
255 255 * set are the printable characters but not including:
256 256 * " / \ [ ] : | < > + ; , ? * = \t
257 257 * Note that space is included and there is a maximum length.
258 258 */
259 259 static boolean_t
260 260 validresource(const char *name)
261 261 {
262 262 const char *cp;
263 263 size_t len;
264 264
265 265 if (name == NULL)
266 266 return (B_FALSE);
267 267
268 268 len = strlen(name);
269 269 if (len == 0 || len > SA_MAX_RESOURCE_NAME)
270 270 return (B_FALSE);
271 271
272 272 if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
273 273 return (B_FALSE);
274 274 }
275 275
276 276 for (cp = name; *cp != '\0'; cp++)
277 277 if (iscntrl(*cp))
278 278 return (B_FALSE);
279 279
280 280 return (B_TRUE);
281 281 }
282 282
283 283 /*
284 284 * Check that the client-side caching (CSC) option value is valid.
285 285 */
286 286 static boolean_t
287 287 validcsc(const char *value)
288 288 {
289 289 int i;
290 290
291 291 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
292 292 if (strcasecmp(value, cscopt[i].value) == 0)
293 293 return (B_TRUE);
294 294 }
295 295
296 296 return (B_FALSE);
297 297 }
298 298
299 299 /*
300 300 * smb_isonline()
301 301 *
302 302 * Determine if the SMF service instance is in the online state or
303 303 * not. A number of operations depend on this state.
304 304 */
305 305 static boolean_t
306 306 smb_isonline(void)
307 307 {
308 308 char *str;
309 309 boolean_t ret = B_FALSE;
310 310
311 311 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
312 312 ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
313 313 free(str);
314 314 }
315 315 return (ret);
316 316 }
317 317
318 318 /*
319 319 * smb_isdisabled()
320 320 *
321 321 * Determine if the SMF service instance is in the disabled state or
322 322 * not. A number of operations depend on this state.
323 323 */
324 324 static boolean_t
325 325 smb_isdisabled(void)
326 326 {
327 327 char *str;
328 328 boolean_t ret = B_FALSE;
329 329
330 330 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
331 331 ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
332 332 free(str);
333 333 }
334 334 return (ret);
335 335 }
336 336
337 337 /*
338 338 * smb_isautoenable()
339 339 *
340 340 * Determine if the SMF service instance auto_enabled set or not. A
341 341 * number of operations depend on this state. The property not being
342 342 * set or being set to true means autoenable. Only being set to false
343 343 * is not autoenabled.
344 344 */
345 345 static boolean_t
346 346 smb_isautoenable(void)
347 347 {
348 348 boolean_t ret = B_TRUE;
349 349 scf_simple_prop_t *prop;
350 350 uint8_t *retstr;
351 351
352 352 prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
353 353 "application", "auto_enable");
354 354 if (prop != NULL) {
355 355 retstr = scf_simple_prop_next_boolean(prop);
356 356 ret = *retstr != 0;
357 357 scf_simple_prop_free(prop);
358 358 }
359 359 return (ret);
360 360 }
361 361
362 362 /*
363 363 * smb_ismaint()
364 364 *
365 365 * Determine if the SMF service instance is in the disabled state or
366 366 * not. A number of operations depend on this state.
367 367 */
368 368 static boolean_t
369 369 smb_ismaint(void)
370 370 {
371 371 char *str;
372 372 boolean_t ret = B_FALSE;
373 373
374 374 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
375 375 ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
376 376 free(str);
377 377 }
378 378 return (ret);
379 379 }
380 380
381 381 /*
382 382 * smb_enable_share tells the implementation that it is to enable the share.
383 383 * This entails converting the path and options into the appropriate ioctl
|
↓ open down ↓ |
383 lines elided |
↑ open up ↑ |
384 384 * calls. It is assumed that all error checking of paths, etc. were
385 385 * done earlier.
386 386 */
387 387 static int
388 388 smb_enable_share(sa_share_t share)
389 389 {
390 390 char *path;
391 391 smb_share_t si;
392 392 sa_resource_t resource;
393 393 boolean_t iszfs;
394 - boolean_t privileged;
395 394 int err = SA_OK;
396 - priv_set_t *priv_effective;
397 395 boolean_t online;
398 396
399 397 /*
400 398 * Don't support Trusted Extensions.
401 399 */
402 400 if (is_system_labeled()) {
403 401 (void) printf(dgettext(TEXT_DOMAIN,
404 402 "SMB: service not supported with Trusted Extensions\n"));
405 403 return (SA_NOT_SUPPORTED);
406 404 }
407 405
408 - priv_effective = priv_allocset();
409 - (void) getppriv(PRIV_EFFECTIVE, priv_effective);
410 - privileged = (priv_isfullset(priv_effective) == B_TRUE);
411 - priv_freeset(priv_effective);
412 -
413 406 /* get the path since it is important in several places */
414 407 path = sa_get_share_attr(share, "path");
415 408 if (path == NULL)
416 409 return (SA_NO_SUCH_PATH);
417 410
418 411 /*
419 412 * If administratively disabled, don't try to start anything.
420 413 */
421 414 online = smb_isonline();
422 415 if (!online && !smb_isautoenable() && smb_isdisabled())
423 416 goto done;
424 417
425 418 iszfs = sa_path_is_zfs(path);
426 419
427 - if (iszfs) {
428 -
429 - if (privileged == B_FALSE && !online) {
430 -
431 - if (!online) {
432 - (void) printf(dgettext(TEXT_DOMAIN,
433 - "SMB: Cannot share remove "
434 - "file system: %s\n"), path);
435 - (void) printf(dgettext(TEXT_DOMAIN,
436 - "SMB: Service needs to be enabled "
437 - "by a privileged user\n"));
438 - err = SA_NO_PERMISSION;
439 - errno = EPERM;
440 - }
441 - if (err) {
442 - sa_free_attr_string(path);
443 - return (err);
444 - }
445 -
446 - }
447 - }
448 -
449 - if (privileged == B_TRUE && !online) {
420 + if (!online) {
450 421 err = smb_enable_service();
451 422 if (err != SA_OK) {
452 423 (void) printf(dgettext(TEXT_DOMAIN,
453 424 "SMB: Unable to enable service\n"));
454 425 } else {
455 426 online = B_TRUE;
456 427 }
457 428 }
458 429
459 430 /*
460 431 * Don't bother trying to start shares if the service isn't
461 432 * running.
462 433 */
463 434 if (!online)
464 435 goto done;
465 436
466 437 /* Each share can have multiple resources */
467 438 for (resource = sa_get_share_resource(share, NULL);
468 439 resource != NULL;
469 440 resource = sa_get_next_resource(resource)) {
470 441 err = smb_build_shareinfo(share, resource, &si);
471 442 if (err != SA_OK) {
472 443 sa_free_attr_string(path);
473 444 return (err);
474 445 }
475 446
476 447 if (!iszfs) {
477 448 err = smb_share_create(&si);
478 449 } else {
479 450 share_t sh;
480 451
481 452 (void) sa_sharetab_fill_zfs(share, &sh, "smb");
482 453 err = sa_share_zfs(share, resource, (char *)path, &sh,
483 454 &si, ZFS_SHARE_SMB);
484 455 if (err != SA_OK) {
485 456 errno = err;
486 457 err = -1;
487 458 }
488 459 sa_emptyshare(&sh);
489 460 }
490 461 }
491 462 if (!iszfs)
492 463 (void) sa_update_sharetab(share, "smb");
493 464 done:
494 465 sa_free_attr_string(path);
495 466
496 467 return (err == NERR_DuplicateShare ? 0 : err);
497 468 }
498 469
499 470 /*
500 471 * This is the share for CIFS all shares have resource names.
501 472 * Enable tells the smb server to update its hash. If it fails
502 473 * because smb server is down, we just ignore as smb server loads
503 474 * the resources from sharemanager at startup.
504 475 */
505 476
506 477 static int
507 478 smb_enable_resource(sa_resource_t resource)
508 479 {
509 480 sa_share_t share;
510 481 smb_share_t si;
511 482 int ret = SA_OK;
512 483 int err;
513 484 boolean_t isonline;
514 485
515 486 share = sa_get_resource_parent(resource);
516 487 if (share == NULL)
517 488 return (SA_NO_SUCH_PATH);
518 489
519 490 /*
520 491 * If administratively disabled, don't try to start anything.
521 492 */
522 493 isonline = smb_isonline();
523 494 if (!isonline && !smb_isautoenable() && smb_isdisabled())
524 495 return (SA_OK);
525 496
526 497 if (!isonline) {
527 498 (void) smb_enable_service();
528 499
529 500 if (!smb_isonline())
530 501 return (SA_OK);
531 502 }
532 503
533 504 if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
534 505 return (ret);
535 506
536 507 /*
537 508 * Attempt to add the share. Any error that occurs if it was
538 509 * online is an error but don't count NERR_DuplicateName if
539 510 * smb/server had to be brought online since bringing the
540 511 * service up will enable the share that was just added prior
541 512 * to the attempt to enable.
542 513 */
543 514 err = smb_share_create(&si);
544 515 if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
545 516 (void) sa_update_sharetab(share, "smb");
546 517 else
547 518 return (SA_NOT_SHARED);
548 519
549 520 return (SA_OK);
550 521 }
551 522
552 523 /*
553 524 * Remove it from smb server hash.
554 525 */
555 526 static int
556 527 smb_disable_resource(sa_resource_t resource)
557 528 {
558 529 char *rname;
559 530 uint32_t res;
560 531 sa_share_t share;
561 532
562 533 rname = sa_get_resource_attr(resource, "name");
563 534 if (rname == NULL)
564 535 return (SA_NO_SUCH_RESOURCE);
565 536
566 537 if (smb_isonline()) {
567 538 res = smb_share_delete(rname);
568 539 if (res != NERR_Success &&
569 540 res != NERR_NetNameNotFound) {
570 541 sa_free_attr_string(rname);
571 542 return (SA_CONFIG_ERR);
572 543 }
573 544 }
574 545
575 546 sa_free_attr_string(rname);
576 547
577 548 share = sa_get_resource_parent(resource);
578 549 if (share != NULL) {
579 550 rname = sa_get_share_attr(share, "path");
580 551 if (rname != NULL) {
581 552 sa_handle_t handle;
582 553
583 554 handle = sa_find_group_handle((sa_group_t)resource);
584 555 (void) sa_delete_sharetab(handle, rname, "smb");
585 556 sa_free_attr_string(rname);
586 557 }
587 558 }
588 559 /*
589 560 * Always return OK as smb/server may be down and
590 561 * Shares will be picked up when loaded.
591 562 */
592 563 return (SA_OK);
593 564 }
594 565
595 566 /*
596 567 * smb_share_changed(sa_share_t share)
597 568 *
598 569 * The specified share has changed.
599 570 */
600 571 static int
601 572 smb_share_changed(sa_share_t share)
602 573 {
603 574 char *path;
604 575 sa_resource_t resource;
605 576
606 577 if (!smb_isonline())
607 578 return (SA_OK);
608 579
609 580 /* get the path since it is important in several places */
610 581 path = sa_get_share_attr(share, "path");
611 582 if (path == NULL)
612 583 return (SA_NO_SUCH_PATH);
613 584
614 585 for (resource = sa_get_share_resource(share, NULL);
615 586 resource != NULL;
616 587 resource = sa_get_next_resource(resource))
617 588 (void) smb_resource_changed(resource);
618 589
619 590 sa_free_attr_string(path);
620 591
621 592 return (SA_OK);
622 593 }
623 594
624 595 /*
625 596 * smb_resource_changed(sa_resource_t resource)
626 597 *
627 598 * The specified resource has changed.
628 599 */
629 600 static int
630 601 smb_resource_changed(sa_resource_t resource)
631 602 {
632 603 uint32_t res;
633 604 sa_share_t share;
634 605 smb_share_t si;
635 606
636 607 if (!smb_isonline())
637 608 return (SA_OK);
638 609
639 610 if ((share = sa_get_resource_parent(resource)) == NULL)
640 611 return (SA_CONFIG_ERR);
641 612
642 613 if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
643 614 return (res);
644 615
645 616 res = smb_share_modify(&si);
646 617
647 618 if (res != NERR_Success)
648 619 return (SA_CONFIG_ERR);
649 620
650 621 return (smb_enable_service());
651 622 }
652 623
653 624 /*
654 625 * smb_disable_share(sa_share_t share, char *path)
655 626 *
656 627 * Unshare the specified share. Note that "path" is the same
657 628 * path as what is in the "share" object. It is passed in to avoid an
658 629 * additional lookup. A missing "path" value makes this a no-op
659 630 * function.
660 631 */
661 632 static int
662 633 smb_disable_share(sa_share_t share, char *path)
663 634 {
664 635 char *rname;
665 636 sa_resource_t resource;
666 637 sa_group_t parent;
667 638 boolean_t iszfs;
668 639 int err = SA_OK;
669 640 int ret = SA_OK;
670 641 sa_handle_t handle;
671 642 boolean_t first = B_TRUE; /* work around sharetab issue */
672 643
673 644 if (path == NULL)
674 645 return (ret);
675 646
676 647 /*
677 648 * If the share is in a ZFS group we need to handle it
678 649 * differently. Just being on a ZFS file system isn't
679 650 * enough since we may be in a legacy share case.
680 651 */
681 652 parent = sa_get_parent_group(share);
682 653 iszfs = sa_group_is_zfs(parent);
683 654
684 655 if (!smb_isonline())
685 656 goto done;
686 657
687 658 for (resource = sa_get_share_resource(share, NULL);
688 659 resource != NULL;
689 660 resource = sa_get_next_resource(resource)) {
690 661 rname = sa_get_resource_attr(resource, "name");
691 662 if (rname == NULL) {
692 663 continue;
693 664 }
694 665 if (!iszfs) {
695 666 err = smb_share_delete(rname);
696 667 switch (err) {
697 668 case NERR_NetNameNotFound:
698 669 case NERR_Success:
699 670 err = SA_OK;
700 671 break;
701 672 default:
702 673 err = SA_CONFIG_ERR;
703 674 break;
704 675 }
705 676 } else {
706 677 share_t sh;
707 678
708 679 (void) sa_sharetab_fill_zfs(share, &sh, "smb");
709 680 err = sa_share_zfs(share, resource, (char *)path, &sh,
710 681 rname, ZFS_UNSHARE_SMB);
711 682 if (err != SA_OK) {
712 683 switch (err) {
713 684 case EINVAL:
714 685 case ENOENT:
715 686 err = SA_OK;
716 687 break;
717 688 default:
718 689 /*
719 690 * If we are no longer the first case,
720 691 * we don't care about the sa_share_zfs
721 692 * err if it is -1. This works around
722 693 * a problem in sharefs and should be
723 694 * removed when sharefs supports
724 695 * multiple entries per path.
725 696 */
726 697 if (!first)
727 698 err = SA_OK;
728 699 else
729 700 err = SA_SYSTEM_ERR;
730 701 break;
731 702 }
732 703 }
733 704
734 705 first = B_FALSE;
735 706
736 707 sa_emptyshare(&sh);
737 708 }
738 709
739 710 if (err != SA_OK)
740 711 ret = err;
741 712 sa_free_attr_string(rname);
742 713 }
743 714 done:
744 715 if (!iszfs) {
745 716 handle = sa_find_group_handle((sa_group_t)share);
746 717 if (handle != NULL)
747 718 (void) sa_delete_sharetab(handle, path, "smb");
748 719 else
749 720 ret = SA_SYSTEM_ERR;
750 721 }
751 722 return (ret);
752 723 }
753 724
754 725 /*
755 726 * smb_validate_property(handle, property, parent)
756 727 *
757 728 * Check that the property has a legitimate value for its type.
758 729 * Handle isn't currently used but may need to be in the future.
759 730 */
760 731
761 732 /*ARGSUSED*/
762 733 static int
763 734 smb_validate_property(sa_handle_t handle, sa_property_t property,
764 735 sa_optionset_t parent)
765 736 {
766 737 int ret = SA_OK;
767 738 char *propname;
768 739 int optindex;
769 740 sa_group_t parent_group;
770 741 char *value;
771 742 char *other;
772 743
773 744 propname = sa_get_property_attr(property, "type");
774 745
775 746 if ((optindex = findopt(propname)) < 0)
776 747 ret = SA_NO_SUCH_PROP;
777 748
778 749 /* need to validate value range here as well */
779 750 if (ret == SA_OK) {
780 751 parent_group = sa_get_parent_group((sa_share_t)parent);
781 752 if (optdefs[optindex].share && !sa_is_share(parent_group))
782 753 ret = SA_PROP_SHARE_ONLY;
783 754 }
784 755 if (ret != SA_OK) {
785 756 if (propname != NULL)
786 757 sa_free_attr_string(propname);
787 758 return (ret);
788 759 }
789 760
790 761 value = sa_get_property_attr(property, "value");
791 762 if (value != NULL) {
792 763 /* first basic type checking */
793 764 switch (optdefs[optindex].type) {
794 765 case OPT_TYPE_NUMBER:
795 766 /* check that the value is all digits */
796 767 if (!is_a_number(value))
797 768 ret = SA_BAD_VALUE;
798 769 break;
799 770 case OPT_TYPE_BOOLEAN:
800 771 ret = true_false_validator(0, value);
801 772 break;
802 773 case OPT_TYPE_NAME:
803 774 /*
804 775 * Make sure no invalid characters
805 776 */
806 777 if (!validresource(value))
807 778 ret = SA_BAD_VALUE;
808 779 break;
809 780 case OPT_TYPE_STRING:
810 781 /* whatever is here should be ok */
811 782 break;
812 783 case OPT_TYPE_CSC:
813 784 if (!validcsc(value))
814 785 ret = SA_BAD_VALUE;
815 786 break;
816 787 case OPT_TYPE_ACCLIST: {
817 788 sa_property_t oprop;
818 789 char *ovalue;
819 790 /*
820 791 * access list handling. Should eventually
821 792 * validate that all the values make sense.
822 793 * Also, ro and rw may have cross value
823 794 * conflicts.
824 795 */
825 796 if (parent == NULL)
826 797 break;
827 798 if (strcmp(propname, SHOPT_RO) == 0)
828 799 other = SHOPT_RW;
829 800 else if (strcmp(propname, SHOPT_RW) == 0)
830 801 other = SHOPT_RO;
831 802 else
832 803 other = NULL;
833 804 if (other == NULL)
834 805 break;
835 806
836 807 /* compare rw(ro) with ro(rw) */
837 808 oprop = sa_get_property(parent, other);
838 809 if (oprop == NULL)
839 810 break;
840 811 /*
841 812 * only potential
842 813 * confusion if other
843 814 * exists
844 815 */
845 816 ovalue = sa_get_property_attr(oprop, "value");
846 817 if (ovalue != NULL) {
847 818 ret = check_rorw(value, ovalue);
848 819 sa_free_attr_string(ovalue);
849 820 }
850 821 break;
851 822 }
852 823 default:
853 824 break;
854 825 }
855 826 }
856 827
857 828 if (value != NULL)
858 829 sa_free_attr_string(value);
859 830 if (ret == SA_OK && optdefs[optindex].check != NULL)
860 831 /* do the property specific check */
861 832 ret = optdefs[optindex].check(property);
862 833
863 834 if (propname != NULL)
864 835 sa_free_attr_string(propname);
865 836 return (ret);
866 837 }
867 838
868 839 /*
869 840 * Protocol management functions
870 841 *
871 842 * properties defined in the default files are defined in
872 843 * proto_option_defs for parsing and validation.
873 844 */
874 845
875 846 struct smb_proto_option_defs {
876 847 int smb_index;
877 848 int32_t minval;
878 849 int32_t maxval; /* In case of length of string this should be max */
879 850 int (*validator)(int, char *);
880 851 int32_t refresh;
881 852 } smb_proto_options[] = {
882 853 { SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
883 854 string_length_check_validator, SMB_REFRESH_REFRESH },
884 855 { SMB_CI_MAX_WORKERS, SMB_PI_MAX_WORKERS_MIN, SMB_PI_MAX_WORKERS_MAX,
885 856 range_check_validator, SMB_REFRESH_REFRESH },
886 857 { SMB_CI_NETBIOS_ENABLE, 0, 0, true_false_validator,
887 858 SMB_REFRESH_REFRESH },
888 859 { SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
889 860 string_length_check_validator, 0 },
890 861 { SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
891 862 { SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
892 863 SMB_REFRESH_REFRESH },
893 864 { SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
894 865 ipv4_validator, SMB_REFRESH_REFRESH },
895 866 { SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
896 867 ipv4_validator, SMB_REFRESH_REFRESH },
897 868 { SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
898 869 interface_validator, SMB_REFRESH_REFRESH },
899 870 { SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
900 871 SMB_REFRESH_REFRESH },
901 872 { SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
902 873 SMB_REFRESH_REFRESH },
903 874 { SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
904 875 SMB_REFRESH_REFRESH },
905 876 { SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
906 877 hostname_validator, SMB_REFRESH_REFRESH },
907 878 { SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
908 879 string_length_check_validator, SMB_REFRESH_REFRESH },
909 880 { SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
910 881 { SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
911 882 { SMB_CI_IPV6_ENABLE, 0, 0, true_false_validator,
912 883 SMB_REFRESH_REFRESH },
913 884 { SMB_CI_PRINT_ENABLE, 0, 0, print_enable_validator,
914 885 SMB_REFRESH_REFRESH },
915 886 { SMB_CI_TRAVERSE_MOUNTS, 0, 0, true_false_validator,
916 887 SMB_REFRESH_REFRESH },
917 888 { SMB_CI_MAP, 0, MAX_VALUE_BUFLEN, cmd_validator, SMB_REFRESH_REFRESH },
918 889 { SMB_CI_UNMAP, 0, MAX_VALUE_BUFLEN, cmd_validator,
919 890 SMB_REFRESH_REFRESH },
920 891 { SMB_CI_DISPOSITION, 0, MAX_VALUE_BUFLEN,
921 892 disposition_validator, SMB_REFRESH_REFRESH },
922 893 { SMB_CI_MAX_PROTOCOL, 0, MAX_VALUE_BUFLEN, protocol_validator,
923 894 SMB_REFRESH_REFRESH },
924 895 { SMB_CI_ENCRYPT, 0, MAX_VALUE_BUFLEN, require_validator,
925 896 SMB_REFRESH_REFRESH },
926 897 { SMB_CI_MIN_PROTOCOL, 0, MAX_VALUE_BUFLEN, protocol_validator,
927 898 SMB_REFRESH_REFRESH },
928 899 { SMB_CI_BYPASS_TRAVERSE_CHECKING, 0, 0, true_false_validator,
929 900 SMB_REFRESH_REFRESH },
930 901 { SMB_CI_OPLOCK_ENABLE, 0, 0, true_false_validator,
931 902 SMB_REFRESH_REFRESH },
932 903 };
933 904
934 905 #define SMB_OPT_NUM \
935 906 (sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
936 907
937 908 static int
938 909 require_validator(int index, char *value)
939 910 {
940 911 if (string_length_check_validator(index, value) != SA_OK)
941 912 return (SA_BAD_VALUE);
942 913
943 914 if (strcmp(value, "required") == 0)
944 915 return (SA_OK);
945 916
946 917 if (strcmp(value, "disabled") == 0)
947 918 return (SA_OK);
948 919
949 920 if (strcmp(value, "enabled") == 0)
950 921 return (SA_OK);
951 922
952 923 return (SA_BAD_VALUE);
953 924 }
954 925
955 926 /*
956 927 * Check the range of value as int range.
957 928 */
958 929 static int
959 930 range_check_validator(int index, char *value)
960 931 {
961 932 int ret = SA_OK;
962 933
963 934 if (!is_a_number(value)) {
964 935 ret = SA_BAD_VALUE;
965 936 } else {
966 937 int val;
967 938 val = strtoul(value, NULL, 0);
968 939 if (val < smb_proto_options[index].minval ||
969 940 val > smb_proto_options[index].maxval)
970 941 ret = SA_BAD_VALUE;
971 942 }
972 943 return (ret);
973 944 }
974 945
975 946 /*
976 947 * Check the range of value as int range.
977 948 */
978 949 static int
979 950 range_check_validator_zero_ok(int index, char *value)
980 951 {
981 952 int ret = SA_OK;
982 953
983 954 if (!is_a_number(value)) {
984 955 ret = SA_BAD_VALUE;
985 956 } else {
986 957 int val;
987 958 val = strtoul(value, NULL, 0);
988 959 if (val == 0)
989 960 ret = SA_OK;
990 961 else {
991 962 if (val < smb_proto_options[index].minval ||
992 963 val > smb_proto_options[index].maxval)
993 964 ret = SA_BAD_VALUE;
994 965 }
995 966 }
996 967 return (ret);
997 968 }
998 969
999 970 /*
1000 971 * Check the length of the string
1001 972 */
1002 973 static int
1003 974 string_length_check_validator(int index, char *value)
1004 975 {
1005 976 int ret = SA_OK;
1006 977
1007 978 if (value == NULL)
1008 979 return (SA_BAD_VALUE);
1009 980 if (strlen(value) > smb_proto_options[index].maxval)
1010 981 ret = SA_BAD_VALUE;
1011 982 return (ret);
1012 983 }
1013 984
1014 985 /*
1015 986 * Check yes/no
1016 987 */
1017 988 /*ARGSUSED*/
1018 989 static int
1019 990 true_false_validator(int index, char *value)
1020 991 {
1021 992 if (value == NULL)
1022 993 return (SA_BAD_VALUE);
1023 994 if ((strcasecmp(value, "true") == 0) ||
1024 995 (strcasecmp(value, "false") == 0))
1025 996 return (SA_OK);
1026 997 return (SA_BAD_VALUE);
1027 998 }
1028 999
1029 1000 /*
1030 1001 * If printing support is compiled in, this is the same as:
1031 1002 * true_false_validator. Otherwise, only allow false.
1032 1003 */
1033 1004 /*ARGSUSED*/
1034 1005 static int
1035 1006 print_enable_validator(int index, char *value)
1036 1007 {
1037 1008 if (value == NULL)
1038 1009 return (SA_BAD_VALUE);
1039 1010
1040 1011 #ifdef HAVE_CUPS
1041 1012 if (strcasecmp(value, "true") == 0)
1042 1013 return (SA_OK);
1043 1014 #endif
1044 1015 if (strcasecmp(value, "false") == 0)
1045 1016 return (SA_OK);
1046 1017
1047 1018 return (SA_BAD_VALUE);
1048 1019 }
1049 1020
1050 1021 /*
1051 1022 * Check IP v4 address.
1052 1023 */
1053 1024 /*ARGSUSED*/
1054 1025 static int
1055 1026 ipv4_validator(int index, char *value)
1056 1027 {
1057 1028 char sbytes[16];
1058 1029
1059 1030 if (value == NULL)
1060 1031 return (SA_OK);
1061 1032
1062 1033 if (strlen(value) == 0)
1063 1034 return (SA_OK);
1064 1035
1065 1036 if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
1066 1037 return (SA_BAD_VALUE);
1067 1038
1068 1039 return (SA_OK);
1069 1040 }
1070 1041
1071 1042 /*
1072 1043 * Check that the specified name is an IP address (v4 or v6) or a hostname.
1073 1044 * Per RFC 1035 and 1123, names may contain alphanumeric characters, hyphens
1074 1045 * and dots. The first and last character of a label must be alphanumeric.
1075 1046 * Interior characters may be alphanumeric or hypens.
1076 1047 *
1077 1048 * Domain names should not contain underscores but we allow them because
1078 1049 * Windows names are often in non-compliance with this rule.
1079 1050 */
1080 1051 /*ARGSUSED*/
1081 1052 static int
1082 1053 hostname_validator(int index, char *value)
1083 1054 {
1084 1055 char sbytes[INET6_ADDRSTRLEN];
1085 1056 boolean_t new_label = B_TRUE;
1086 1057 char *p;
1087 1058 char label_terminator;
1088 1059 int len;
1089 1060
1090 1061 if (value == NULL)
1091 1062 return (SA_OK);
1092 1063
1093 1064 if ((len = strlen(value)) == 0)
1094 1065 return (SA_OK);
1095 1066
1096 1067 if (inet_pton(AF_INET, value, (void *)sbytes) == 1)
1097 1068 return (SA_OK);
1098 1069
1099 1070 if (inet_pton(AF_INET6, value, (void *)sbytes) == 1)
1100 1071 return (SA_OK);
1101 1072
1102 1073 if (len >= MAXHOSTNAMELEN)
1103 1074 return (SA_BAD_VALUE);
1104 1075
1105 1076 if (strspn(value, "0123456789.") == len)
1106 1077 return (SA_BAD_VALUE);
1107 1078
1108 1079 label_terminator = *value;
1109 1080
1110 1081 for (p = value; *p != '\0'; ++p) {
1111 1082 if (new_label) {
1112 1083 if (!isalnum(*p))
1113 1084 return (SA_BAD_VALUE);
1114 1085 new_label = B_FALSE;
1115 1086 label_terminator = *p;
1116 1087 continue;
1117 1088 }
1118 1089
1119 1090 if (*p == '.') {
1120 1091 if (!isalnum(label_terminator))
1121 1092 return (SA_BAD_VALUE);
1122 1093 new_label = B_TRUE;
1123 1094 label_terminator = *p;
1124 1095 continue;
1125 1096 }
1126 1097
1127 1098 label_terminator = *p;
1128 1099
1129 1100 if (isalnum(*p) || *p == '-' || *p == '_')
1130 1101 continue;
1131 1102
1132 1103 return (SA_BAD_VALUE);
1133 1104 }
1134 1105
1135 1106 if (!isalnum(label_terminator))
1136 1107 return (SA_BAD_VALUE);
1137 1108
1138 1109 return (SA_OK);
1139 1110 }
1140 1111
1141 1112 /*
1142 1113 * Call back function for dlpi_walk.
1143 1114 * Returns TRUE if interface name exists on the host.
1144 1115 */
1145 1116 static boolean_t
1146 1117 smb_get_interface(const char *ifname, void *arg)
1147 1118 {
1148 1119 smb_hostifs_walker_t *iterp = arg;
1149 1120
1150 1121 iterp->hiw_matchfound = (strcmp(ifname, iterp->hiw_ifname) == 0);
1151 1122
1152 1123 return (iterp->hiw_matchfound);
1153 1124 }
1154 1125
1155 1126 /*
1156 1127 * Checks to see if the input interface exists on the host.
1157 1128 * Returns B_TRUE if the match is found, B_FALSE otherwise.
1158 1129 */
1159 1130 static boolean_t
1160 1131 smb_validate_interface(const char *ifname)
1161 1132 {
1162 1133 smb_hostifs_walker_t iter;
1163 1134
1164 1135 if ((ifname == NULL) || (*ifname == '\0'))
1165 1136 return (B_FALSE);
1166 1137
1167 1138 iter.hiw_ifname = ifname;
1168 1139 iter.hiw_matchfound = B_FALSE;
1169 1140 dlpi_walk(smb_get_interface, &iter, 0);
1170 1141
1171 1142 return (iter.hiw_matchfound);
1172 1143 }
1173 1144
1174 1145 /*
1175 1146 * Check valid interfaces. Interface names value can be NULL or empty.
1176 1147 * Returns SA_BAD_VALUE if interface cannot be found on the host.
1177 1148 */
1178 1149 /*ARGSUSED*/
1179 1150 static int
1180 1151 interface_validator(int index, char *value)
1181 1152 {
1182 1153 char buf[16];
1183 1154 int ret = SA_OK;
1184 1155 char *ifname, *tmp, *p;
1185 1156
1186 1157 if (value == NULL || *value == '\0')
1187 1158 return (ret);
1188 1159
1189 1160 if (strlen(value) > MAX_VALUE_BUFLEN)
1190 1161 return (SA_BAD_VALUE);
1191 1162
1192 1163 if ((p = strdup(value)) == NULL)
1193 1164 return (SA_NO_MEMORY);
1194 1165
1195 1166 tmp = p;
1196 1167 while ((ifname = strsep(&tmp, ",")) != NULL) {
1197 1168 if (*ifname == '\0') {
1198 1169 ret = SA_BAD_VALUE;
1199 1170 break;
1200 1171 }
1201 1172
1202 1173 if (!smb_validate_interface(ifname)) {
1203 1174 if (inet_pton(AF_INET, ifname, (void *)buf) == 0) {
1204 1175 ret = SA_BAD_VALUE;
1205 1176 break;
1206 1177 }
1207 1178 }
1208 1179 }
1209 1180
1210 1181 free(p);
1211 1182 return (ret);
1212 1183 }
1213 1184
1214 1185 /*
1215 1186 * Check path
1216 1187 */
1217 1188 /*ARGSUSED*/
1218 1189 static int
1219 1190 path_validator(int index, char *path)
1220 1191 {
1221 1192 struct stat buffer;
1222 1193 int fd, status;
1223 1194
1224 1195 if (path == NULL)
1225 1196 return (SA_BAD_VALUE);
1226 1197
1227 1198 fd = open(path, O_RDONLY);
1228 1199 if (fd < 0)
1229 1200 return (SA_BAD_VALUE);
1230 1201
1231 1202 status = fstat(fd, &buffer);
1232 1203 (void) close(fd);
1233 1204
1234 1205 if (status < 0)
1235 1206 return (SA_BAD_VALUE);
1236 1207
1237 1208 if (buffer.st_mode & S_IFDIR)
1238 1209 return (SA_OK);
1239 1210 return (SA_BAD_VALUE);
1240 1211 }
1241 1212
1242 1213 /*
1243 1214 * the protoset holds the defined options so we don't have to read
1244 1215 * them multiple times
1245 1216 */
1246 1217 static sa_protocol_properties_t protoset;
1247 1218
1248 1219 static int
1249 1220 findprotoopt(char *name)
1250 1221 {
1251 1222 int i;
1252 1223 char *sc_name;
1253 1224
1254 1225 for (i = 0; i < SMB_OPT_NUM; i++) {
1255 1226 sc_name = smb_config_getname(smb_proto_options[i].smb_index);
1256 1227 if (strcasecmp(sc_name, name) == 0)
1257 1228 return (i);
1258 1229 }
1259 1230
1260 1231 return (-1);
1261 1232 }
1262 1233
1263 1234 /*
1264 1235 * smb_load_proto_properties()
1265 1236 *
1266 1237 * read the smb config values from SMF.
1267 1238 */
1268 1239
1269 1240 static int
1270 1241 smb_load_proto_properties()
1271 1242 {
1272 1243 sa_property_t prop;
1273 1244 char value[MAX_VALUE_BUFLEN];
1274 1245 char *name;
1275 1246 int index;
1276 1247 int ret = SA_OK;
1277 1248 int rc;
1278 1249
1279 1250 protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
1280 1251 if (protoset == NULL)
1281 1252 return (SA_NO_MEMORY);
1282 1253
1283 1254 for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
1284 1255 rc = smb_config_get(smb_proto_options[index].smb_index,
1285 1256 value, sizeof (value));
1286 1257 if (rc != SMBD_SMF_OK)
1287 1258 continue;
1288 1259 name = smb_config_getname(smb_proto_options[index].smb_index);
1289 1260 prop = sa_create_property(name, value);
1290 1261 if (prop != NULL)
1291 1262 ret = sa_add_protocol_property(protoset, prop);
1292 1263 else
1293 1264 ret = SA_NO_MEMORY;
1294 1265 }
1295 1266 return (ret);
1296 1267 }
1297 1268
1298 1269 /*
1299 1270 * smb_share_init()
1300 1271 *
1301 1272 * Initialize the smb plugin.
1302 1273 */
1303 1274
1304 1275 static int
1305 1276 smb_share_init(void)
1306 1277 {
1307 1278 if (sa_plugin_ops.sa_init != smb_share_init)
1308 1279 return (SA_SYSTEM_ERR);
1309 1280
1310 1281 smb_share_door_clnt_init();
1311 1282 return (smb_load_proto_properties());
1312 1283 }
1313 1284
1314 1285 /*
1315 1286 * smb_share_fini()
1316 1287 *
1317 1288 */
1318 1289 static void
1319 1290 smb_share_fini(void)
1320 1291 {
1321 1292 xmlFreeNode(protoset);
1322 1293 protoset = NULL;
1323 1294
1324 1295 smb_share_door_clnt_fini();
1325 1296 }
1326 1297
1327 1298 /*
1328 1299 * smb_get_proto_set()
1329 1300 *
1330 1301 * Return an optionset with all the protocol specific properties in
1331 1302 * it.
1332 1303 */
1333 1304 static sa_protocol_properties_t
1334 1305 smb_get_proto_set(void)
1335 1306 {
1336 1307 return (protoset);
1337 1308 }
1338 1309
1339 1310 /*
1340 1311 * smb_enable_dependencies()
1341 1312 *
1342 1313 * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
1343 1314 * enabled. This will attempt to enable all of them.
1344 1315 */
1345 1316 static void
1346 1317 smb_enable_dependencies(const char *fmri)
1347 1318 {
1348 1319 scf_handle_t *handle;
1349 1320 scf_service_t *service;
1350 1321 scf_instance_t *inst = NULL;
1351 1322 scf_iter_t *iter;
1352 1323 scf_property_t *prop;
1353 1324 scf_value_t *value;
1354 1325 scf_propertygroup_t *pg;
1355 1326 scf_scope_t *scope;
1356 1327 char type[SCFTYPE_LEN];
1357 1328 char *dependency;
1358 1329 char *servname;
1359 1330 int maxlen;
1360 1331
1361 1332 /*
1362 1333 * Get all required handles and storage.
1363 1334 */
1364 1335 handle = scf_handle_create(SCF_VERSION);
1365 1336 if (handle == NULL)
1366 1337 return;
1367 1338
1368 1339 if (scf_handle_bind(handle) != 0) {
1369 1340 scf_handle_destroy(handle);
1370 1341 return;
1371 1342 }
1372 1343
1373 1344 maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1374 1345 if (maxlen == (ssize_t)-1)
1375 1346 maxlen = MAXPATHLEN;
1376 1347
1377 1348 dependency = malloc(maxlen);
1378 1349
1379 1350 service = scf_service_create(handle);
1380 1351
1381 1352 iter = scf_iter_create(handle);
1382 1353
1383 1354 pg = scf_pg_create(handle);
1384 1355
1385 1356 prop = scf_property_create(handle);
1386 1357
1387 1358 value = scf_value_create(handle);
1388 1359
1389 1360 scope = scf_scope_create(handle);
1390 1361
1391 1362 if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
1392 1363 value == NULL || scope == NULL || dependency == NULL)
1393 1364 goto done;
1394 1365
1395 1366 /*
1396 1367 * We passed in the FMRI for the default instance but for
1397 1368 * some things we need the simple form so construct it. Since
1398 1369 * we reuse the storage that dependency points to, we need to
1399 1370 * use the servname early.
1400 1371 */
1401 1372 (void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
1402 1373 servname = strrchr(dependency, ':');
1403 1374 if (servname == NULL)
1404 1375 goto done;
1405 1376 *servname = '\0';
1406 1377 servname = dependency;
1407 1378
1408 1379 /*
1409 1380 * Setup to iterate over the service property groups, only
1410 1381 * looking at those that are "dependency" types. The "entity"
1411 1382 * property will have the FMRI of the service we are dependent
1412 1383 * on.
1413 1384 */
1414 1385 if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
1415 1386 goto done;
1416 1387
1417 1388 if (scf_scope_get_service(scope, servname, service) != 0)
1418 1389 goto done;
1419 1390
1420 1391 if (scf_iter_service_pgs(iter, service) != 0)
1421 1392 goto done;
1422 1393
1423 1394 while (scf_iter_next_pg(iter, pg) > 0) {
1424 1395 char *services[2];
1425 1396 /*
1426 1397 * Have a property group for the service. See if it is
1427 1398 * a dependency pg and only do operations on those.
1428 1399 */
1429 1400 if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
1430 1401 continue;
1431 1402
1432 1403 if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
1433 1404 continue;
1434 1405 /*
1435 1406 * Have a dependency. Attempt to enable it.
1436 1407 */
1437 1408 if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
1438 1409 continue;
1439 1410
1440 1411 if (scf_property_get_value(prop, value) != 0)
1441 1412 continue;
1442 1413
1443 1414 services[1] = NULL;
1444 1415
1445 1416 if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
1446 1417 services[0] = dependency;
1447 1418 _check_services(services);
1448 1419 }
1449 1420 }
1450 1421
1451 1422 done:
1452 1423 if (dependency != NULL)
1453 1424 free(dependency);
1454 1425 if (value != NULL)
1455 1426 scf_value_destroy(value);
1456 1427 if (prop != NULL)
1457 1428 scf_property_destroy(prop);
1458 1429 if (pg != NULL)
1459 1430 scf_pg_destroy(pg);
1460 1431 if (iter != NULL)
1461 1432 scf_iter_destroy(iter);
1462 1433 if (scope != NULL)
1463 1434 scf_scope_destroy(scope);
1464 1435 if (inst != NULL)
1465 1436 scf_instance_destroy(inst);
1466 1437 if (service != NULL)
1467 1438 scf_service_destroy(service);
1468 1439
1469 1440 (void) scf_handle_unbind(handle);
1470 1441 scf_handle_destroy(handle);
1471 1442 }
1472 1443
1473 1444 /*
1474 1445 * How long to wait for service to come online
1475 1446 */
1476 1447 #define WAIT_FOR_SERVICE 15
1477 1448
1478 1449 /*
1479 1450 * smb_enable_service()
1480 1451 *
1481 1452 */
1482 1453 static int
1483 1454 smb_enable_service(void)
1484 1455 {
1485 1456 int i;
1486 1457 int ret = SA_OK;
1487 1458 char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
1488 1459
1489 1460 if (!smb_isonline()) {
1490 1461 /*
1491 1462 * Attempt to start the idmap, and other dependent
1492 1463 * services, first. If it fails, the SMB service will
1493 1464 * ultimately fail so we use that as the error. If we
1494 1465 * don't try to enable idmap, smb won't start the
1495 1466 * first time unless the admin has done it
1496 1467 * manually. The service could be administratively
1497 1468 * disabled so we won't always get started.
1498 1469 */
1499 1470 smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
1500 1471 _check_services(service);
1501 1472
1502 1473 /* Wait for service to come online */
1503 1474 for (i = 0; i < WAIT_FOR_SERVICE; i++) {
1504 1475 if (smb_isonline()) {
1505 1476 ret = SA_OK;
1506 1477 break;
1507 1478 } else if (smb_ismaint()) {
1508 1479 /* maintenance requires help */
1509 1480 ret = SA_SYSTEM_ERR;
1510 1481 break;
1511 1482 } else {
1512 1483 /* try another time */
1513 1484 ret = SA_BUSY;
1514 1485 (void) sleep(1);
1515 1486 }
1516 1487 }
1517 1488 }
1518 1489 return (ret);
1519 1490 }
1520 1491
1521 1492 /*
1522 1493 * smb_validate_proto_prop(index, name, value)
1523 1494 *
1524 1495 * Verify that the property specified by name can take the new
1525 1496 * value. This is a sanity check to prevent bad values getting into
1526 1497 * the default files.
1527 1498 */
1528 1499 static int
1529 1500 smb_validate_proto_prop(int index, char *name, char *value)
1530 1501 {
1531 1502 if ((name == NULL) || (index < 0))
1532 1503 return (SA_BAD_VALUE);
1533 1504
1534 1505 if (smb_proto_options[index].validator == NULL)
1535 1506 return (SA_OK);
1536 1507
1537 1508 if (smb_proto_options[index].validator(index, value) == SA_OK)
1538 1509 return (SA_OK);
1539 1510 return (SA_BAD_VALUE);
1540 1511 }
1541 1512
1542 1513 /*
1543 1514 * smb_set_proto_prop(prop)
1544 1515 *
1545 1516 * check that prop is valid.
1546 1517 */
1547 1518 /*ARGSUSED*/
1548 1519 static int
1549 1520 smb_set_proto_prop(sa_property_t prop)
1550 1521 {
1551 1522 int ret = SA_OK;
1552 1523 char *name;
1553 1524 char *value;
1554 1525 int index = -1;
1555 1526 struct smb_proto_option_defs *opt;
1556 1527
1557 1528 name = sa_get_property_attr(prop, "type");
1558 1529 value = sa_get_property_attr(prop, "value");
1559 1530 if (name != NULL && value != NULL) {
1560 1531 index = findprotoopt(name);
1561 1532 if (index >= 0) {
1562 1533 /* should test for valid value */
1563 1534 ret = smb_validate_proto_prop(index, name, value);
1564 1535 if (ret == SA_OK) {
1565 1536 opt = &smb_proto_options[index];
1566 1537
1567 1538 /* Save to SMF */
1568 1539 if (smb_config_set(opt->smb_index,
1569 1540 value) != 0) {
1570 1541 ret = SA_BAD_VALUE;
1571 1542 goto out;
1572 1543 }
1573 1544 /*
1574 1545 * Specialized refresh mechanisms can
1575 1546 * be flagged in the proto_options and
1576 1547 * processed here.
1577 1548 */
1578 1549 if (opt->refresh & SMB_REFRESH_REFRESH)
1579 1550 (void) smf_refresh_instance(
1580 1551 SMBD_DEFAULT_INSTANCE_FMRI);
1581 1552 else if (opt->refresh & SMB_REFRESH_RESTART)
1582 1553 (void) smf_restart_instance(
1583 1554 SMBD_DEFAULT_INSTANCE_FMRI);
1584 1555 }
1585 1556 }
1586 1557 }
1587 1558
1588 1559 out:
1589 1560 if (name != NULL)
1590 1561 sa_free_attr_string(name);
1591 1562 if (value != NULL)
1592 1563 sa_free_attr_string(value);
1593 1564
1594 1565 return (ret);
1595 1566 }
1596 1567
1597 1568 /*
1598 1569 * smb_get_status()
1599 1570 *
1600 1571 * What is the current status of the smbd? We use the SMF state here.
1601 1572 * Caller must free the returned value.
1602 1573 */
1603 1574
1604 1575 static char *
1605 1576 smb_get_status(void)
1606 1577 {
1607 1578 return (smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI));
1608 1579 }
1609 1580
1610 1581 /*
1611 1582 * This protocol plugin require resource names
1612 1583 */
1613 1584 static uint64_t
1614 1585 smb_share_features(void)
1615 1586 {
1616 1587 return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
1617 1588 SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
1618 1589 }
1619 1590
1620 1591 /*
1621 1592 * This should be used to convert smb_share_t to sa_resource_t
1622 1593 * Should only be needed to build transient shares/resources to be
1623 1594 * supplied to sharemgr to display.
1624 1595 */
1625 1596 static int
1626 1597 smb_add_transient(sa_handle_t handle, smb_share_t *si)
1627 1598 {
1628 1599 int err;
1629 1600 sa_share_t share;
1630 1601 sa_group_t group;
1631 1602 sa_resource_t resource;
1632 1603 nvlist_t *nvl;
1633 1604 char *opt;
1634 1605
1635 1606 if (si == NULL)
1636 1607 return (SA_INVALID_NAME);
1637 1608
1638 1609 if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
1639 1610 if ((group = smb_get_defaultgrp(handle)) == NULL)
1640 1611 return (SA_NO_SUCH_GROUP);
1641 1612
1642 1613 share = sa_get_share(group, si->shr_path);
1643 1614 if (share == NULL) {
1644 1615 share = sa_add_share(group, si->shr_path,
1645 1616 SA_SHARE_TRANSIENT, &err);
1646 1617 if (share == NULL)
1647 1618 return (SA_NO_SUCH_PATH);
1648 1619 }
1649 1620 }
1650 1621
1651 1622 /*
1652 1623 * Now handle the resource. Make sure that the resource is
1653 1624 * transient and added to the share.
1654 1625 */
1655 1626 resource = sa_get_share_resource(share, si->shr_name);
1656 1627 if (resource == NULL) {
1657 1628 resource = sa_add_resource(share,
1658 1629 si->shr_name, SA_SHARE_TRANSIENT, &err);
1659 1630 if (resource == NULL)
1660 1631 return (SA_NO_SUCH_RESOURCE);
1661 1632 }
1662 1633
1663 1634 if (si->shr_cmnt[0] != '\0')
1664 1635 (void) sa_set_resource_description(resource, si->shr_cmnt);
1665 1636
1666 1637 if (si->shr_container[0] != '\0')
1667 1638 (void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
1668 1639 si->shr_container);
1669 1640
1670 1641 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1671 1642 return (SA_NO_MEMORY);
1672 1643
1673 1644 if ((opt = smb_csc_name(si)) != NULL)
1674 1645 err |= nvlist_add_string(nvl, SHOPT_CSC, opt);
1675 1646
1676 1647 opt = (si->shr_flags & SMB_SHRF_ABE) ? "true" : "false";
1677 1648 err |= nvlist_add_string(nvl, SHOPT_ABE, opt);
1678 1649
1679 1650 if ((si->shr_flags & SMB_SHRF_AUTOHOME) == 0) {
1680 1651 opt = (si->shr_flags & SMB_SHRF_GUEST_OK) ? "true" : "false";
1681 1652 err |= nvlist_add_string(nvl, SHOPT_GUEST, opt);
1682 1653 }
1683 1654
1684 1655 if (si->shr_access_ro[0] != '\0')
1685 1656 err |= nvlist_add_string(nvl, SHOPT_RO, si->shr_access_ro);
1686 1657
1687 1658 if (si->shr_access_rw[0] != '\0')
1688 1659 err |= nvlist_add_string(nvl, SHOPT_RW, si->shr_access_rw);
1689 1660
1690 1661 if (si->shr_access_none[0] != '\0')
1691 1662 err |= nvlist_add_string(nvl, SHOPT_NONE, si->shr_access_none);
1692 1663
1693 1664 if (err) {
1694 1665 nvlist_free(nvl);
1695 1666 return (SA_CONFIG_ERR);
1696 1667 }
1697 1668
1698 1669 err = smb_update_optionset_props(handle, resource, nvl);
1699 1670
1700 1671 nvlist_free(nvl);
1701 1672 return (err);
1702 1673 }
1703 1674
1704 1675 /*
1705 1676 * Return smb transient shares.
1706 1677 */
1707 1678 static int
1708 1679 smb_list_transient(sa_handle_t handle)
1709 1680 {
1710 1681 int i, offset;
1711 1682 smb_shrlist_t list;
1712 1683 int res;
1713 1684
1714 1685 if (smb_share_count() <= 0)
1715 1686 return (SA_OK);
1716 1687
1717 1688 offset = 0;
1718 1689 while (smb_share_list(offset, &list) == NERR_Success) {
1719 1690 if (list.sl_cnt == 0)
1720 1691 break;
1721 1692
1722 1693 for (i = 0; i < list.sl_cnt; i++) {
1723 1694 res = smb_add_transient(handle, &(list.sl_shares[i]));
1724 1695 if (res != SA_OK)
1725 1696 return (res);
1726 1697 }
1727 1698 offset += list.sl_cnt;
1728 1699 }
1729 1700
1730 1701 return (SA_OK);
1731 1702 }
1732 1703
1733 1704 /*
1734 1705 * fix_resource_name(share, name, prefix)
1735 1706 *
1736 1707 * Construct a name where the ZFS dataset has the prefix replaced with "name".
1737 1708 */
1738 1709 static char *
1739 1710 fix_resource_name(sa_share_t share, char *name, char *prefix)
1740 1711 {
1741 1712 char buf[SA_MAX_RESOURCE_NAME + 1];
1742 1713 char *dataset;
1743 1714 size_t bufsz = SA_MAX_RESOURCE_NAME + 1;
1744 1715 size_t prelen;
1745 1716
1746 1717 if (prefix == NULL)
1747 1718 return (strdup(name));
1748 1719
1749 1720 dataset = sa_get_share_attr(share, "dataset");
1750 1721 if (dataset == NULL)
1751 1722 return (strdup(name));
1752 1723
1753 1724 (void) strlcpy(buf, name, bufsz);
1754 1725 prelen = strlen(prefix);
1755 1726
1756 1727 if (strncmp(dataset, prefix, prelen) == 0)
1757 1728 (void) strlcat(buf, dataset + prelen, bufsz);
1758 1729
1759 1730 sa_free_attr_string(dataset);
1760 1731 sa_fix_resource_name(buf);
1761 1732 return (strdup(buf));
1762 1733 }
1763 1734
1764 1735 /*
1765 1736 * smb_parse_optstring(group, options)
1766 1737 *
1767 1738 * parse a compact option string into individual options. This allows
1768 1739 * ZFS sharesmb and sharemgr "share" command to work. group can be a
1769 1740 * group, a share or a resource.
1770 1741 */
1771 1742 static int
1772 1743 smb_parse_optstring(sa_group_t group, char *options)
1773 1744 {
1774 1745 char *dup;
1775 1746 char *base;
1776 1747 char *lasts;
1777 1748 char *token;
1778 1749 sa_optionset_t optionset;
1779 1750 sa_group_t parent = NULL;
1780 1751 sa_resource_t resource = NULL;
1781 1752 int iszfs = 0;
1782 1753 int persist = 0;
1783 1754 int need_optionset = 0;
1784 1755 int ret = SA_OK;
1785 1756 sa_property_t prop;
1786 1757
1787 1758 /*
1788 1759 * In order to not attempt to change ZFS properties unless
1789 1760 * absolutely necessary, we never do it in the legacy parsing
1790 1761 * so we need to keep track of this.
1791 1762 */
1792 1763 if (sa_is_share(group)) {
1793 1764 char *zfs;
1794 1765
1795 1766 parent = sa_get_parent_group(group);
1796 1767 if (parent != NULL) {
1797 1768 zfs = sa_get_group_attr(parent, "zfs");
1798 1769 if (zfs != NULL) {
1799 1770 sa_free_attr_string(zfs);
1800 1771 iszfs = 1;
1801 1772 }
1802 1773 }
1803 1774 } else {
1804 1775 iszfs = sa_group_is_zfs(group);
1805 1776 /*
1806 1777 * If a ZFS group, then we need to see if a resource
1807 1778 * name is being set. If so, bail with
1808 1779 * SA_PROP_SHARE_ONLY, so we come back in with a share
1809 1780 * instead of a group.
1810 1781 */
1811 1782 if (iszfs ||
1812 1783 strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1813 1784 strstr(options, ",name=") != NULL) {
1814 1785 return (SA_PROP_SHARE_ONLY);
1815 1786 }
1816 1787 }
1817 1788
1818 1789 /* do we have an existing optionset? */
1819 1790 optionset = sa_get_optionset(group, "smb");
1820 1791 if (optionset == NULL) {
1821 1792 /* didn't find existing optionset so create one */
1822 1793 optionset = sa_create_optionset(group, "smb");
1823 1794 if (optionset == NULL)
1824 1795 return (SA_NO_MEMORY);
1825 1796 } else {
1826 1797 /*
1827 1798 * If an optionset already exists, we've come through
1828 1799 * twice so ignore the second time.
1829 1800 */
1830 1801 return (ret);
1831 1802 }
1832 1803
1833 1804 /* We need a copy of options for the next part. */
1834 1805 dup = strdup(options);
1835 1806 if (dup == NULL)
1836 1807 return (SA_NO_MEMORY);
1837 1808
1838 1809 /*
1839 1810 * SMB properties are straightforward and are strings,
1840 1811 * integers or booleans. Properties are separated by
1841 1812 * commas. It will be necessary to parse quotes due to some
1842 1813 * strings not having a restricted characters set.
1843 1814 *
1844 1815 * Note that names will create a resource. For now, if there
1845 1816 * is a set of properties "before" the first name="", those
1846 1817 * properties will be placed on the group.
1847 1818 */
1848 1819 persist = sa_is_persistent(group);
1849 1820 base = dup;
1850 1821 token = dup;
1851 1822 lasts = NULL;
1852 1823 while (token != NULL && ret == SA_OK) {
1853 1824 ret = SA_OK;
1854 1825 token = strtok_r(base, ",", &lasts);
1855 1826 base = NULL;
1856 1827 if (token != NULL) {
1857 1828 char *value;
1858 1829 /*
1859 1830 * All SMB properties have values so there
1860 1831 * MUST be an '=' character. If it doesn't,
1861 1832 * it is a syntax error.
1862 1833 */
1863 1834 value = strchr(token, '=');
1864 1835 if (value != NULL) {
1865 1836 *value++ = '\0';
1866 1837 } else {
1867 1838 ret = SA_SYNTAX_ERR;
1868 1839 break;
1869 1840 }
1870 1841 /*
1871 1842 * We may need to handle a "name" property
1872 1843 * that is a ZFS imposed resource name. Each
1873 1844 * name would trigger getting a new "resource"
1874 1845 * to put properties on. For now, assume no
1875 1846 * "name" property for special handling.
1876 1847 */
1877 1848
1878 1849 if (strcmp(token, SHOPT_NAME) == 0) {
1879 1850 char *prefix;
1880 1851 char *name = NULL;
1881 1852 /*
1882 1853 * We have a name, so now work on the
1883 1854 * resource level. We have a "share"
1884 1855 * in "group" due to the caller having
1885 1856 * added it. If we are called with a
1886 1857 * group, the check for group/share
1887 1858 * at the beginning of this function
1888 1859 * will bail out the parse if there is a
1889 1860 * "name" but no share.
1890 1861 */
1891 1862 if (!iszfs) {
1892 1863 ret = SA_SYNTAX_ERR;
1893 1864 break;
1894 1865 }
1895 1866 /*
1896 1867 * Make sure the parent group has the
1897 1868 * "prefix" property since we will
1898 1869 * need to use this for constructing
1899 1870 * inherited name= values.
1900 1871 */
1901 1872 prefix = sa_get_group_attr(parent, "prefix");
1902 1873 if (prefix == NULL) {
1903 1874 prefix = sa_get_group_attr(parent,
1904 1875 "name");
1905 1876 if (prefix != NULL) {
1906 1877 (void) sa_set_group_attr(parent,
1907 1878 "prefix", prefix);
1908 1879 }
1909 1880 }
1910 1881 name = fix_resource_name((sa_share_t)group,
1911 1882 value, prefix);
1912 1883 if (name != NULL) {
1913 1884 resource = sa_add_resource(
1914 1885 (sa_share_t)group, name,
1915 1886 SA_SHARE_TRANSIENT, &ret);
1916 1887 sa_free_attr_string(name);
1917 1888 } else {
1918 1889 ret = SA_NO_MEMORY;
1919 1890 }
1920 1891 if (prefix != NULL)
1921 1892 sa_free_attr_string(prefix);
1922 1893
1923 1894 /* A resource level optionset is needed */
1924 1895
1925 1896 need_optionset = 1;
1926 1897 if (resource == NULL) {
1927 1898 ret = SA_NO_MEMORY;
1928 1899 break;
1929 1900 }
1930 1901 continue;
1931 1902 }
1932 1903
1933 1904 if (iszfs && strcmp(token, SHOPT_DESCRIPTION) == 0) {
1934 1905 if (resource == NULL)
1935 1906 (void) sa_set_share_description(
1936 1907 (sa_share_t)group, value);
1937 1908 else
1938 1909 (void) sa_set_resource_description(
1939 1910 resource, value);
1940 1911 continue;
1941 1912 }
1942 1913
1943 1914 if (need_optionset) {
1944 1915 optionset = sa_create_optionset(resource,
1945 1916 "smb");
1946 1917 need_optionset = 0;
1947 1918 }
1948 1919
1949 1920 prop = sa_create_property(token, value);
1950 1921 if (prop == NULL)
1951 1922 ret = SA_NO_MEMORY;
1952 1923 else
1953 1924 ret = sa_add_property(optionset, prop);
1954 1925 if (ret != SA_OK)
1955 1926 break;
1956 1927 if (!iszfs)
1957 1928 ret = sa_commit_properties(optionset, !persist);
1958 1929 }
1959 1930 }
1960 1931 free(dup);
1961 1932 return (ret);
1962 1933 }
1963 1934
1964 1935 /*
1965 1936 * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1966 1937 *
1967 1938 * provides a mechanism to format SMB properties into legacy output
1968 1939 * format. If the buffer would overflow, it is reallocated and grown
1969 1940 * as appropriate. Special cases of converting internal form of values
1970 1941 * to those used by "share" are done. this function does one property
1971 1942 * at a time.
1972 1943 */
1973 1944
1974 1945 static void
1975 1946 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1976 1947 sa_property_t prop, int sep)
1977 1948 {
1978 1949 char *name;
1979 1950 char *value;
1980 1951 int curlen;
1981 1952 char *buff = *rbuff;
1982 1953 size_t buffsize = *rbuffsize;
1983 1954
1984 1955 name = sa_get_property_attr(prop, "type");
1985 1956 value = sa_get_property_attr(prop, "value");
1986 1957 if (buff != NULL)
1987 1958 curlen = strlen(buff);
1988 1959 else
1989 1960 curlen = 0;
1990 1961 if (name != NULL) {
1991 1962 int len;
1992 1963 len = strlen(name) + sep;
1993 1964
1994 1965 /*
1995 1966 * A future RFE would be to replace this with more
1996 1967 * generic code and to possibly handle more types.
1997 1968 *
1998 1969 * For now, everything else is treated as a string. If
1999 1970 * we get any properties that aren't exactly
2000 1971 * name/value pairs, we may need to
2001 1972 * interpret/transform.
2002 1973 */
2003 1974 if (value != NULL)
2004 1975 len += 1 + strlen(value);
2005 1976
2006 1977 while (buffsize <= (curlen + len)) {
2007 1978 /* need more room */
2008 1979 buffsize += incr;
2009 1980 buff = realloc(buff, buffsize);
2010 1981 *rbuff = buff;
2011 1982 *rbuffsize = buffsize;
2012 1983 if (buff == NULL) {
2013 1984 /* realloc failed so free everything */
2014 1985 if (*rbuff != NULL)
2015 1986 free(*rbuff);
2016 1987 goto err;
2017 1988 }
2018 1989 }
2019 1990 if (buff == NULL)
2020 1991 goto err;
2021 1992 (void) snprintf(buff + curlen, buffsize - curlen,
2022 1993 "%s%s=%s", sep ? "," : "",
2023 1994 name, value != NULL ? value : "\"\"");
2024 1995
2025 1996 }
2026 1997 err:
2027 1998 if (name != NULL)
2028 1999 sa_free_attr_string(name);
2029 2000 if (value != NULL)
2030 2001 sa_free_attr_string(value);
2031 2002 }
2032 2003
2033 2004 /*
2034 2005 * smb_format_resource_options(resource, hier)
2035 2006 *
2036 2007 * format all the options on the group into a flattened option
2037 2008 * string. If hier is non-zero, walk up the tree to get inherited
2038 2009 * options.
2039 2010 */
2040 2011
2041 2012 static char *
2042 2013 smb_format_options(sa_group_t group, int hier)
2043 2014 {
2044 2015 sa_optionset_t options = NULL;
2045 2016 sa_property_t prop;
2046 2017 int sep = 0;
2047 2018 char *buff;
2048 2019 size_t buffsize;
2049 2020
2050 2021
2051 2022 buff = malloc(OPT_CHUNK);
2052 2023 if (buff == NULL)
2053 2024 return (NULL);
2054 2025
2055 2026 buff[0] = '\0';
2056 2027 buffsize = OPT_CHUNK;
2057 2028
2058 2029 /*
2059 2030 * We may have a an optionset relative to this item. format
2060 2031 * these if we find them and then add any security definitions.
2061 2032 */
2062 2033
2063 2034 options = sa_get_derived_optionset(group, "smb", hier);
2064 2035
2065 2036 /*
2066 2037 * do the default set first but skip any option that is also
2067 2038 * in the protocol specific optionset.
2068 2039 */
2069 2040 if (options != NULL) {
2070 2041 for (prop = sa_get_property(options, NULL);
2071 2042 prop != NULL; prop = sa_get_next_property(prop)) {
2072 2043 /*
2073 2044 * use this one since we skipped any
2074 2045 * of these that were also in
2075 2046 * optdefault
2076 2047 */
2077 2048 smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
2078 2049 prop, sep);
2079 2050 if (buff == NULL) {
2080 2051 /*
2081 2052 * buff could become NULL if there
2082 2053 * isn't enough memory for
2083 2054 * smb_sprint_option to realloc()
2084 2055 * as necessary. We can't really
2085 2056 * do anything about it at this
2086 2057 * point so we return NULL. The
2087 2058 * caller should handle the
2088 2059 * failure.
2089 2060 */
2090 2061 if (options != NULL)
2091 2062 sa_free_derived_optionset(
2092 2063 options);
2093 2064 return (buff);
2094 2065 }
2095 2066 sep = 1;
2096 2067 }
2097 2068 }
2098 2069
2099 2070 if (options != NULL)
2100 2071 sa_free_derived_optionset(options);
2101 2072 return (buff);
2102 2073 }
2103 2074
2104 2075 /*
2105 2076 * smb_rename_resource(resource, newname)
2106 2077 *
2107 2078 * Change the current exported name of the resource to newname.
2108 2079 */
2109 2080 /*ARGSUSED*/
2110 2081 int
2111 2082 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
2112 2083 {
2113 2084 int ret = SA_OK;
2114 2085 int err;
2115 2086 char *oldname;
2116 2087
2117 2088 if (!smb_isonline())
2118 2089 return (SA_OK);
2119 2090
2120 2091 oldname = sa_get_resource_attr(resource, "name");
2121 2092 if (oldname == NULL)
2122 2093 return (SA_NO_SUCH_RESOURCE);
2123 2094
2124 2095 err = smb_share_rename(oldname, newname);
2125 2096
2126 2097 sa_free_attr_string(oldname);
2127 2098
2128 2099 /* improve error values somewhat */
2129 2100 switch (err) {
2130 2101 case NERR_Success:
2131 2102 break;
2132 2103 case NERR_InternalError:
2133 2104 ret = SA_SYSTEM_ERR;
2134 2105 break;
2135 2106 case NERR_DuplicateShare:
2136 2107 ret = SA_DUPLICATE_NAME;
2137 2108 break;
2138 2109 default:
2139 2110 ret = SA_CONFIG_ERR;
2140 2111 break;
2141 2112 }
2142 2113
2143 2114 return (ret);
2144 2115 }
2145 2116
2146 2117 static int
2147 2118 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2148 2119 {
2149 2120 sa_optionset_t opts;
2150 2121 char *path;
2151 2122 char *rname;
2152 2123 char *val = NULL;
2153 2124 char csc_value[SMB_CSC_BUFSZ];
2154 2125 char strbuf[sizeof ("required")];
2155 2126
2156 2127 bzero(si, sizeof (smb_share_t));
2157 2128
2158 2129 if ((path = sa_get_share_attr(share, "path")) == NULL)
2159 2130 return (SA_NO_SUCH_PATH);
2160 2131
2161 2132 if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2162 2133 sa_free_attr_string(path);
2163 2134 return (SA_NO_SUCH_RESOURCE);
2164 2135 }
2165 2136
2166 2137 (void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2167 2138 (void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2168 2139 sa_free_attr_string(path);
2169 2140 sa_free_attr_string(rname);
2170 2141
2171 2142 val = sa_get_resource_description(resource);
2172 2143 if (val == NULL)
2173 2144 val = sa_get_share_description(share);
2174 2145
2175 2146 if (val != NULL) {
2176 2147 (void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2177 2148 sa_free_share_description(val);
2178 2149 }
2179 2150
2180 2151 si->shr_flags = (sa_is_persistent(share))
2181 2152 ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2182 2153
2183 2154 opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2184 2155 if (opts == NULL)
2185 2156 return (SA_OK);
2186 2157
2187 2158 if (smb_saprop_getbool(opts, SHOPT_CATIA, B_FALSE))
2188 2159 si->shr_flags |= SMB_SHRF_CATIA;
2189 2160
2190 2161 if (smb_saprop_getbool(opts, SHOPT_ABE, B_FALSE))
2191 2162 si->shr_flags |= SMB_SHRF_ABE;
2192 2163
2193 2164 if (smb_saprop_getbool(opts, SHOPT_GUEST, B_FALSE))
2194 2165 si->shr_flags |= SMB_SHRF_GUEST_OK;
2195 2166
2196 2167 if (smb_saprop_getbool(opts, SHOPT_DFSROOT, B_FALSE))
2197 2168 si->shr_flags |= SMB_SHRF_DFSROOT;
2198 2169
2199 2170 if (smb_saprop_getbool(opts, SHOPT_CA, B_FALSE))
2200 2171 si->shr_flags |= SMB_SHRF_CA;
2201 2172
2202 2173 if (smb_saprop_getbool(opts, SHOPT_FSO, B_FALSE))
2203 2174 si->shr_flags |= SMB_SHRF_FSO;
2204 2175
2205 2176 /* Quotas are enabled by default. */
2206 2177 if (smb_saprop_getbool(opts, SHOPT_QUOTAS, B_TRUE))
2207 2178 si->shr_flags |= SMB_SHRF_QUOTAS;
2208 2179
2209 2180 if (smb_saprop_getstr(opts, SHOPT_ENCRYPT, strbuf, sizeof (strbuf)))
2210 2181 smb_cfg_set_require(strbuf, &si->shr_encrypt);
2211 2182
2212 2183 (void) smb_saprop_getstr(opts, SHOPT_AD_CONTAINER, si->shr_container,
2213 2184 sizeof (si->shr_container));
2214 2185
2215 2186 if (smb_saprop_getstr(opts, SHOPT_CSC, csc_value, sizeof (csc_value)))
2216 2187 smb_csc_option(csc_value, si);
2217 2188
2218 2189 if (smb_saprop_getstr(opts, SHOPT_RO, si->shr_access_ro,
2219 2190 sizeof (si->shr_access_ro)))
2220 2191 si->shr_flags |= SMB_SHRF_ACC_RO;
2221 2192
2222 2193 if (smb_saprop_getstr(opts, SHOPT_RW, si->shr_access_rw,
2223 2194 sizeof (si->shr_access_rw)))
2224 2195 si->shr_flags |= SMB_SHRF_ACC_RW;
2225 2196
2226 2197 if (smb_saprop_getstr(opts, SHOPT_NONE, si->shr_access_none,
2227 2198 sizeof (si->shr_access_none)))
2228 2199 si->shr_flags |= SMB_SHRF_ACC_NONE;
2229 2200
2230 2201 sa_free_derived_optionset(opts);
2231 2202 return (SA_OK);
2232 2203 }
2233 2204
2234 2205 /*
2235 2206 * Map a client-side caching (CSC) option to the appropriate share
2236 2207 * flag. Only one option is allowed; an error will be logged if
2237 2208 * multiple options have been specified. We don't need to do anything
2238 2209 * about multiple values here because the SRVSVC will not recognize
2239 2210 * a value containing multiple flags and will return the default value.
2240 2211 *
2241 2212 * If the option value is not recognized, it will be ignored: invalid
2242 2213 * values will typically be caught and rejected by sharemgr.
2243 2214 */
2244 2215 static void
2245 2216 smb_csc_option(const char *value, smb_share_t *si)
2246 2217 {
2247 2218 char buf[SMB_CSC_BUFSZ];
2248 2219 int i;
2249 2220
2250 2221 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2251 2222 if (strcasecmp(value, cscopt[i].value) == 0) {
2252 2223 si->shr_flags |= cscopt[i].flag;
2253 2224 break;
2254 2225 }
2255 2226 }
2256 2227
2257 2228 switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2258 2229 case 0:
2259 2230 case SMB_SHRF_CSC_DISABLED:
2260 2231 case SMB_SHRF_CSC_MANUAL:
2261 2232 case SMB_SHRF_CSC_AUTO:
2262 2233 case SMB_SHRF_CSC_VDO:
2263 2234 break;
2264 2235
2265 2236 default:
2266 2237 buf[0] = '\0';
2267 2238
2268 2239 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2269 2240 if (si->shr_flags & cscopt[i].flag) {
2270 2241 (void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2271 2242 (void) strlcat(buf, cscopt[i].value,
2272 2243 SMB_CSC_BUFSZ);
2273 2244 }
2274 2245 }
2275 2246
2276 2247 syslog(LOG_ERR, "csc option conflict:%s", buf);
2277 2248 break;
2278 2249 }
2279 2250 }
2280 2251
2281 2252 /*
2282 2253 * Return the option name for the first CSC flag (there should be only
2283 2254 * one) encountered in the share flags.
2284 2255 */
2285 2256 static char *
2286 2257 smb_csc_name(const smb_share_t *si)
2287 2258 {
2288 2259 int i;
2289 2260
2290 2261 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2291 2262 if (si->shr_flags & cscopt[i].flag)
2292 2263 return (cscopt[i].value);
2293 2264 }
2294 2265
2295 2266 return (NULL);
2296 2267 }
2297 2268
2298 2269 /*
2299 2270 * smb_get_defaultgrp
2300 2271 *
2301 2272 * If default group for CIFS shares (i.e. "smb") exists
2302 2273 * then it will return the group handle, otherwise it will
2303 2274 * create the group and return the handle.
2304 2275 *
2305 2276 * All the shares created by CIFS clients (this is only possible
2306 2277 * via RPC) will be added to "smb" groups.
2307 2278 */
2308 2279 static sa_group_t
2309 2280 smb_get_defaultgrp(sa_handle_t handle)
2310 2281 {
2311 2282 sa_group_t group = NULL;
2312 2283 int err;
2313 2284
2314 2285 group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2315 2286 if (group != NULL)
2316 2287 return (group);
2317 2288
2318 2289 group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2319 2290 if (group == NULL)
2320 2291 return (NULL);
2321 2292
2322 2293 if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2323 2294 (void) sa_remove_group(group);
2324 2295 group = NULL;
2325 2296 }
2326 2297
2327 2298 return (group);
2328 2299 }
2329 2300
2330 2301 /*
2331 2302 * Checks to see if the command args are the supported substitution specifier.
2332 2303 * i.e. <cmd> %U %S
2333 2304 */
2334 2305 static int
2335 2306 cmd_validator(int index, char *value)
2336 2307 {
2337 2308 char cmd[MAXPATHLEN];
2338 2309 char *ptr, *v;
2339 2310 boolean_t skip_cmdname;
2340 2311
2341 2312 if (string_length_check_validator(index, value) != SA_OK)
2342 2313 return (SA_BAD_VALUE);
2343 2314
2344 2315 if (*value == '\0')
2345 2316 return (SA_OK);
2346 2317
2347 2318 (void) strlcpy(cmd, value, sizeof (cmd));
2348 2319
2349 2320 ptr = cmd;
2350 2321 skip_cmdname = B_TRUE;
2351 2322 do {
2352 2323 if ((v = strsep(&ptr, " ")) == NULL)
2353 2324 break;
2354 2325
2355 2326 if (*v != '\0') {
2356 2327
2357 2328 if (skip_cmdname) {
2358 2329 skip_cmdname = B_FALSE;
2359 2330 continue;
2360 2331 }
2361 2332
2362 2333 if ((strlen(v) != 2) || *v != '%')
2363 2334 return (SA_BAD_VALUE);
2364 2335
2365 2336 if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2366 2337 return (SA_BAD_VALUE);
2367 2338 }
2368 2339
2369 2340 } while (v != NULL);
2370 2341
2371 2342 /*
2372 2343 * If skip_cmdname is still true then the string contains
2373 2344 * only spaces. Don't allow such a string.
2374 2345 */
2375 2346 if (skip_cmdname)
2376 2347 return (SA_BAD_VALUE);
2377 2348
2378 2349 return (SA_OK);
2379 2350 }
2380 2351
2381 2352 /*ARGSUSED*/
2382 2353 static int
2383 2354 disposition_validator(int index, char *value)
2384 2355 {
2385 2356 if (value == NULL)
2386 2357 return (SA_BAD_VALUE);
2387 2358
2388 2359 if (*value == '\0')
2389 2360 return (SA_OK);
2390 2361
2391 2362 if ((strcasecmp(value, SMB_EXEC_DISP_CONTINUE) == 0) ||
2392 2363 (strcasecmp(value, SMB_EXEC_DISP_TERMINATE) == 0))
2393 2364 return (SA_OK);
2394 2365
2395 2366 return (SA_BAD_VALUE);
2396 2367 }
2397 2368
2398 2369 /*ARGSUSED*/
2399 2370 static int
2400 2371 protocol_validator(int index, char *value)
2401 2372 {
2402 2373 if (value == NULL)
2403 2374 return (SA_BAD_VALUE);
2404 2375
2405 2376 if (*value == '\0')
2406 2377 return (SA_OK);
2407 2378
2408 2379 if (smb_config_check_protocol(value) == 0)
2409 2380 return (SA_OK);
2410 2381
2411 2382 return (SA_BAD_VALUE);
2412 2383
2413 2384 }
2414 2385
2415 2386 /*
2416 2387 * Updates the optionset properties of the share resource.
2417 2388 * The properties are given as a list of name-value pair.
2418 2389 * The name argument should be the optionset property name and the value
2419 2390 * should be a valid value for the specified property.
2420 2391 *
2421 2392 * When calling this function for permanent shares, the caller must also
2422 2393 * call sa_commit_properties() to commit the changes to SMF.
2423 2394 */
2424 2395 static int
2425 2396 smb_update_optionset_props(sa_handle_t handle, sa_resource_t resource,
2426 2397 nvlist_t *nvl)
2427 2398 {
2428 2399 sa_property_t prop;
2429 2400 sa_optionset_t opts;
2430 2401 int err = SA_OK;
2431 2402 nvpair_t *cur;
2432 2403 char *name, *val;
2433 2404
2434 2405 if ((opts = sa_get_optionset(resource, SMB_PROTOCOL_NAME)) == NULL) {
2435 2406 opts = sa_create_optionset(resource, SMB_PROTOCOL_NAME);
2436 2407 if (opts == NULL)
2437 2408 return (SA_CONFIG_ERR);
2438 2409 }
2439 2410
2440 2411 cur = nvlist_next_nvpair(nvl, NULL);
2441 2412 while (cur != NULL) {
2442 2413 name = nvpair_name(cur);
2443 2414 err = nvpair_value_string(cur, &val);
2444 2415 if ((err != 0) || (name == NULL) || (val == NULL)) {
2445 2416 err = SA_CONFIG_ERR;
2446 2417 break;
2447 2418 }
2448 2419
2449 2420 prop = NULL;
2450 2421 if ((prop = sa_get_property(opts, name)) == NULL) {
2451 2422 prop = sa_create_property(name, val);
2452 2423 if (prop != NULL) {
2453 2424 err = sa_valid_property(handle, opts,
2454 2425 SMB_PROTOCOL_NAME, prop);
2455 2426 if (err != SA_OK) {
2456 2427 (void) sa_remove_property(prop);
2457 2428 break;
2458 2429 }
2459 2430 }
2460 2431 err = sa_add_property(opts, prop);
2461 2432 if (err != SA_OK)
2462 2433 break;
2463 2434 } else {
2464 2435 err = sa_update_property(prop, val);
2465 2436 if (err != SA_OK)
2466 2437 break;
2467 2438 }
2468 2439
2469 2440 cur = nvlist_next_nvpair(nvl, cur);
2470 2441 }
2471 2442
2472 2443 return (err);
2473 2444 }
2474 2445
2475 2446 static boolean_t
2476 2447 smb_saprop_getbool(sa_optionset_t opts, char *propname, boolean_t def)
2477 2448 {
2478 2449 sa_property_t prop;
2479 2450 char *val;
2480 2451 boolean_t ret = def;
2481 2452
2482 2453 prop = sa_get_property(opts, propname);
2483 2454 if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2484 2455 if (def) {
2485 2456 /* Default is true, ret false if... */
2486 2457 if ((strcasecmp(val, "false") == 0) ||
2487 2458 (strcmp(val, "0") == 0))
2488 2459 ret = B_FALSE;
2489 2460 } else {
2490 2461 /* Default is false, ret true if... */
2491 2462 if ((strcasecmp(val, "true") == 0) ||
2492 2463 (strcmp(val, "1") == 0))
2493 2464 ret = B_TRUE;
2494 2465 }
2495 2466 free(val);
2496 2467 }
2497 2468
2498 2469 return (ret);
2499 2470 }
2500 2471
2501 2472 static boolean_t
2502 2473 smb_saprop_getstr(sa_optionset_t opts, char *propname, char *buf, size_t bufsz)
2503 2474 {
2504 2475 sa_property_t prop;
2505 2476 char *val;
2506 2477
2507 2478 prop = sa_get_property(opts, propname);
2508 2479 if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2509 2480 (void) strlcpy(buf, val, bufsz);
2510 2481 free(val);
2511 2482 return (B_TRUE);
2512 2483 }
2513 2484
2514 2485 return (B_FALSE);
2515 2486 }
|
↓ open down ↓ |
2056 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX