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
|
/*
* 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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2018 Nexenta Systems, Inc. All rights reserved.
*/
/*
* This is a helper file to get/set Windows SD. This is used by
* SRVSVC service.
*/
#include <strings.h>
#include <libzfs.h>
#include <smbsrv/libsmb.h>
#include <smbsrv/libmlsvc.h>
#include <smbsrv/ndl/srvsvc.ndl>
/* Size of offset members in mslm_security_descriptor structure */
#define SRVSVC_SD_OFFSET_SZ 16
#define SRVSVC_ACE_OFFSET 8
#define SRVSVC_SID_OFFSET 8
uint32_t srvsvc_sd_set_relative(smb_sd_t *, uint8_t *);
static uint32_t srvsvc_sd_get_autohome(const smb_share_t *, smb_sd_t *);
static uint32_t srvsvc_sd_status_to_error(uint32_t);
static uint32_t srvsvc_sd_set_absolute(uint8_t *, smb_sd_t *);
/*
* This method computes ACL on share path from a share name.
* Return 0 upon success, -1 upon failure.
*/
static int
srvsvc_shareacl_getpath(smb_share_t *si, char *shr_acl_path)
{
char dataset[MAXPATHLEN];
char mp[ZFS_MAXPROPLEN];
libzfs_handle_t *libhd;
zfs_handle_t *zfshd;
int ret = 0;
if ((libhd = libzfs_init()) == NULL)
return (-1);
ret = smb_getdataset(libhd, si->shr_path, dataset, MAXPATHLEN);
if (ret != 0) {
libzfs_fini(libhd);
return (ret);
}
if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_DATASET)) == NULL) {
libzfs_fini(libhd);
return (-1);
}
if (zfs_prop_get(zfshd, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL,
NULL, 0, B_FALSE) != 0) {
zfs_close(zfshd);
libzfs_fini(libhd);
return (-1);
}
zfs_close(zfshd);
libzfs_fini(libhd);
(void) snprintf(shr_acl_path, MAXPATHLEN, "%s/.zfs/shares/%s",
mp, si->shr_name);
return (ret);
}
/*
* This method sets Security Descriptor on a share path.
*
* Returns:
* ERROR_SUCCESS
* ERROR_NOT_ENOUGH_MEMORY
* ERROR_INVALID_ACL
* ERROR_INVALID_SID
* ERROR_INVALID_SECURITY_DESCR
* ERROR_NONE_MAPPED
* ERROR_INTERNAL_ERROR
* ERROR_PATH_NOT_FOUND
*/
uint32_t
srvsvc_sd_set(smb_share_t *si, uint8_t *sdbuf)
{
smb_sd_t sd;
uint32_t status = ERROR_SUCCESS;
char path[MAXPATHLEN];
int ret = 0;
ret = srvsvc_shareacl_getpath(si, path);
if (ret != 0)
return (ERROR_PATH_NOT_FOUND);
smb_sd_init(&sd, 0);
status = srvsvc_sd_set_absolute(sdbuf, &sd);
if (status != ERROR_SUCCESS) {
smb_sd_term(&sd);
return (status);
}
status = smb_sd_write(path, &sd, SMB_DACL_SECINFO);
status = srvsvc_sd_status_to_error(status);
smb_sd_term(&sd);
return (status);
}
/*
* This method returns a Security Descriptor of a share path in self relative
* format. Call to this function with NULL buffer, returns the size of the
* security descriptor, which can be used to allocate buffer.
*
* Returns:
* ERROR_SUCCESS
* ERROR_NOT_ENOUGH_MEMORY
* ERROR_INVALID_ACL
* ERROR_INVALID_SID
* ERROR_INVALID_SECURITY_DESCR
* ERROR_INVALID_PARAMETER
* ERROR_NONE_MAPPED
* ERROR_INTERNAL_ERROR
* ERROR_PATH_NOT_FOUND
*/
uint32_t
srvsvc_sd_get(smb_share_t *si, uint8_t *sdbuf, uint32_t *size)
{
smb_sd_t sd;
uint32_t status = ERROR_SUCCESS;
char path[MAXPATHLEN];
int ret = 0;
if (sdbuf == NULL && size == NULL)
return (ERROR_INVALID_PARAMETER);
bzero(&sd, sizeof (smb_sd_t));
if (si->shr_flags & SMB_SHRF_AUTOHOME) {
status = srvsvc_sd_get_autohome(si, &sd);
} else {
ret = srvsvc_shareacl_getpath(si, path);
if (ret != 0)
return (ERROR_PATH_NOT_FOUND);
status = smb_sd_read(path, &sd, SMB_ALL_SECINFO);
status = srvsvc_sd_status_to_error(status);
}
if (status != ERROR_SUCCESS) {
smb_sd_term(&sd);
return (status);
}
if (sdbuf == NULL) {
*size = smb_sd_len(&sd, SMB_ALL_SECINFO);
smb_sd_term(&sd);
return (status);
}
status = srvsvc_sd_set_relative(&sd, sdbuf);
smb_sd_term(&sd);
return (status);
}
static uint32_t
srvsvc_sd_get_autohome(const smb_share_t *si, smb_sd_t *sd)
{
smb_fssd_t fs_sd;
acl_t *acl;
uint32_t status;
if (acl_fromtext("owner@:rwxpdDaARWcCos::allow", &acl) != 0)
return (ERROR_NOT_ENOUGH_MEMORY);
smb_fssd_init(&fs_sd, SMB_ALL_SECINFO, SMB_FSSD_FLAGS_DIR);
fs_sd.sd_uid = si->shr_uid;
fs_sd.sd_gid = si->shr_gid;
fs_sd.sd_zdacl = acl;
fs_sd.sd_zsacl = NULL;
status = smb_sd_fromfs(&fs_sd, sd);
status = srvsvc_sd_status_to_error(status);
smb_fssd_term(&fs_sd);
return (status);
}
/*
* This method converts an ACE from absolute (pointer) to
* self relative (flat buffer) format.
*
* Returns Win32 error codes.
*/
static uint32_t
srvsvc_ace_set_relative(mslm_ace_t *m_ace, struct mslm_sid *m_sid,
smb_ace_t *ace)
{
if ((m_ace == NULL) || (ace == NULL))
return (ERROR_INVALID_PARAMETER);
bcopy(&ace->se_hdr, &m_ace->header, sizeof (mslm_ace_hdr_t));
m_ace->mask = ace->se_mask;
if ((ace->se_sid == NULL) || (m_sid == NULL))
return (ERROR_INVALID_PARAMETER);
bcopy(ace->se_sid, m_sid, smb_sid_len(ace->se_sid));
return (ERROR_SUCCESS);
}
/*
* This method converts an ACL from absolute (pointer) to
* self relative (flat buffer) format.
*
* Returns an initialized mslm_acl structure on success.
* Returns NULL on failure.
*/
static struct mslm_acl *
srvsvc_acl_set_relative(uint8_t *sdbuf, smb_acl_t *acl)
{
struct mslm_acl *m_acl;
if (sdbuf == NULL)
return (NULL);
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_acl = (struct mslm_acl *)sdbuf;
m_acl->revision = acl->sl_revision;
m_acl->sbz1 = 0;
m_acl->size = acl->sl_bsize;
m_acl->sbz2 = 0;
m_acl->ace_count = acl->sl_acecnt;
return (m_acl);
}
/*
* This method converts Security Descriptor from absolute (pointer) to
* self relative (flat buffer) format.
*
* Returns Win32 error codes.
*/
uint32_t
srvsvc_sd_set_relative(smb_sd_t *sd, uint8_t *sdbuf)
{
mslm_security_descriptor_t *msd;
int offset, len, i;
smb_ace_t *ace;
mslm_ace_t *m_ace;
struct mslm_sid *m_sid;
uint16_t ace_cnt;
uint32_t status = ERROR_SUCCESS;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
msd = (mslm_security_descriptor_t *)sdbuf;
if (msd == NULL)
return (ERROR_INVALID_SECURITY_DESCR);
msd->revision = sd->sd_revision;
msd->sbz1 = 0;
msd->control = sd->sd_control | SE_SELF_RELATIVE;
offset = sizeof (mslm_security_descriptor_t) - SRVSVC_SD_OFFSET_SZ;
msd->offset_owner = msd->offset_group = 0;
msd->offset_sacl = msd->offset_dacl = 0;
if (sd->sd_owner != NULL) {
msd->offset_owner = offset;
if (sd->sd_owner == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
len = smb_sid_len(sd->sd_owner);
bcopy(sd->sd_owner, &sdbuf[offset], len);
offset += len;
}
if (sd->sd_group != NULL) {
msd->offset_group = offset;
if (sd->sd_group == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
len = smb_sid_len(sd->sd_group);
bcopy(sd->sd_group, &sdbuf[offset], len);
offset += len;
}
if (sd->sd_sacl != NULL) {
msd->offset_sacl = offset;
msd->sacl = srvsvc_acl_set_relative(&sdbuf[offset],
sd->sd_sacl);
if (msd->sacl == NULL)
return (ERROR_INVALID_PARAMETER);
ace = sd->sd_sacl->sl_aces;
ace_cnt = msd->sacl->ace_count;
offset += SRVSVC_ACE_OFFSET;
for (i = 0; i < ace_cnt; i++, ace++) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_ace = (mslm_ace_t *)&sdbuf[offset];
offset += SRVSVC_SID_OFFSET;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_sid = (struct mslm_sid *)&sdbuf[offset];
status = srvsvc_ace_set_relative(m_ace, m_sid, ace);
if (status != ERROR_SUCCESS)
return (status);
offset += smb_sid_len(ace->se_sid);
}
}
if (sd->sd_dacl != NULL) {
msd->offset_dacl = offset;
msd->dacl = srvsvc_acl_set_relative(&sdbuf[offset],
sd->sd_dacl);
if (msd->dacl == NULL)
return (ERROR_INVALID_PARAMETER);
ace = sd->sd_dacl->sl_aces;
ace_cnt = msd->dacl->ace_count;
offset += SRVSVC_ACE_OFFSET;
for (i = 0; i < ace_cnt; i++, ace++) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_ace = (mslm_ace_t *)&sdbuf[offset];
offset += SRVSVC_SID_OFFSET;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_sid = (struct mslm_sid *)&sdbuf[offset];
status = srvsvc_ace_set_relative(m_ace, m_sid, ace);
if (status != ERROR_SUCCESS)
return (status);
offset += smb_sid_len(ace->se_sid);
}
}
return (status);
}
/*
* This method converts an ACE from self relative (flat buffer) to
* absolute (pointer) format.
*
* Returns Win32 error codes.
*/
static uint32_t
srvsvc_ace_set_absolute(mslm_ace_t *m_ace, struct mslm_sid *m_sid,
smb_ace_t *ace)
{
int sid_size = 0;
if ((m_ace == NULL) || (ace == NULL) || (m_sid == NULL))
return (ERROR_INVALID_PARAMETER);
bzero(ace, sizeof (smb_ace_t));
bcopy(&m_ace->header, &ace->se_hdr, sizeof (mslm_ace_hdr_t));
ace->se_mask = m_ace->mask;
sid_size = smb_sid_len((smb_sid_t *)m_sid);
if ((ace->se_sid = malloc(sid_size)) == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
bcopy(m_sid, ace->se_sid, sid_size);
return (ERROR_SUCCESS);
}
/*
* This method converts an ACL from self relative (flat buffer) to
* absolute (pointer) format.
*
* Returns an initialized smb_acl_t structure on success.
* Returns NULL on failure.
*/
static smb_acl_t *
srvsvc_acl_set_absolute(uint8_t *sdbuf, int *offset)
{
uint8_t rev;
uint16_t sz, ace_cnt;
smb_acl_t *acl;
bcopy(&sdbuf[*offset], &rev, sizeof (uint8_t));
*offset += 2; /* Pad for Sbz1 */
bcopy(&sdbuf[*offset], &sz, sizeof (uint16_t));
*offset += 2;
bcopy(&sdbuf[*offset], &ace_cnt, sizeof (uint16_t));
*offset += 4; /* Pad for Sbz2 */
acl = smb_acl_alloc(rev, sz, ace_cnt);
return (acl);
}
/*
* This method converts Security Descriptor from self relative (flat buffer) to
* absolute (pointer) format.
*
* Returns Win32 error codes.
*/
static uint32_t
srvsvc_sd_set_absolute(uint8_t *sdbuf, smb_sd_t *sd)
{
mslm_security_descriptor_t *msd;
mslm_ace_t *m_ace;
struct mslm_sid *m_sid;
smb_ace_t *ace;
uint16_t ace_cnt;
int offset, i, sid_size;
uint32_t status = ERROR_SUCCESS;
if (sdbuf == NULL)
return (ERROR_INVALID_SECURITY_DESCR);
/*LINTED E_BAD_PTR_CAST_ALIGN*/
msd = (mslm_security_descriptor_t *)sdbuf;
sd->sd_revision = msd->revision;
sd->sd_control = msd->control & (~SE_SELF_RELATIVE);
if (msd->offset_owner != 0) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_sid = (struct mslm_sid *)&sdbuf[msd->offset_owner];
sid_size = smb_sid_len((smb_sid_t *)m_sid);
if ((sd->sd_owner = malloc(sid_size)) == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
bcopy(m_sid, sd->sd_owner, sid_size);
}
if (msd->offset_group != 0) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_sid = (struct mslm_sid *)&sdbuf[msd->offset_group];
sid_size = smb_sid_len((smb_sid_t *)m_sid);
if ((sd->sd_group = malloc(sid_size)) == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
bcopy(m_sid, sd->sd_group, sid_size);
}
if (msd->offset_sacl != 0) {
offset = msd->offset_sacl;
sd->sd_sacl = srvsvc_acl_set_absolute(sdbuf, &offset);
if (sd->sd_sacl == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
ace = sd->sd_sacl->sl_aces;
ace_cnt = sd->sd_sacl->sl_acecnt;
for (i = 0; i < ace_cnt; i++, ace++) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_ace = (mslm_ace_t *)&sdbuf[offset];
offset += SRVSVC_SID_OFFSET;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_sid = (struct mslm_sid *)&sdbuf[offset];
status = srvsvc_ace_set_absolute(m_ace, m_sid, ace);
if (status != ERROR_SUCCESS)
return (status);
offset += smb_sid_len(ace->se_sid);
}
}
if (msd->offset_dacl != 0) {
offset = msd->offset_dacl;
sd->sd_dacl = srvsvc_acl_set_absolute(sdbuf, &offset);
if (sd->sd_dacl == NULL)
return (ERROR_NOT_ENOUGH_MEMORY);
ace = sd->sd_dacl->sl_aces;
ace_cnt = sd->sd_dacl->sl_acecnt;
for (i = 0; i < ace_cnt; i++, ace++) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_ace = (mslm_ace_t *)&sdbuf[offset];
offset += SRVSVC_SID_OFFSET;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
m_sid = (struct mslm_sid *)&sdbuf[offset];
status = srvsvc_ace_set_absolute(m_ace, m_sid, ace);
if (status != ERROR_SUCCESS)
return (status);
offset += smb_sid_len(ace->se_sid);
}
}
return (status);
}
/*
* This method maps NT status codes into Win 32 error codes.
* This method operates on status codes that are related
* to processing of Security Descriptor.
*/
static uint32_t
srvsvc_sd_status_to_error(uint32_t status)
{
int i;
static struct {
uint32_t nt_status;
uint32_t err_code;
} errmap[] = {
{ NT_STATUS_SUCCESS, ERROR_SUCCESS },
{ NT_STATUS_INVALID_ACL, ERROR_INVALID_ACL },
{ NT_STATUS_INVALID_SID, ERROR_INVALID_SID },
{ NT_STATUS_NONE_MAPPED, ERROR_NONE_MAPPED }
};
for (i = 0; i < (sizeof (errmap) / sizeof (errmap[0])); ++i) {
if (status == errmap[i].nt_status)
return (errmap[i].err_code);
}
return (ERROR_INTERNAL_ERROR);
}
|