1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* helper functions for using libscf with CIFS */
#include <libscf.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <libintl.h>
#include <assert.h>
#include <strings.h>
#include <uuid/uuid.h>
#include <sys/param.h>
#include <smbsrv/alloc.h>
#include <smbsrv/libsmb.h>
/*
* smb_smf_scf_log_error(msg)
* Logs error messages from scf API's
*/
static void
smb_smf_scf_log_error(char *msg)
{
if (!msg) {
syslog(LOG_ERR, " SMBD SMF problem: %s\n",
scf_strerror(scf_error()));
} else { /*LINTED E_SEC_PRINTF_E_VAR_FMT*/
syslog(LOG_ERR, msg, scf_strerror(scf_error()));
}
}
/*
* smb_smf_create_service_pgroup(handle, pgroup)
*
* create a new property group at service level.
*/
int
smb_smf_create_service_pgroup(smb_scfhandle_t *handle, char *pgroup)
{
int ret = SMBD_SMF_OK;
int err;
if (handle == NULL) {
return (SMBD_SMF_SYSTEM_ERR);
}
/*
* only create a handle if it doesn't exist. It is ok to exist
* since the pg handle will be set as a side effect.
*/
if (handle->scf_pg == NULL)
handle->scf_pg = scf_pg_create(handle->scf_handle);
/*
* if the pgroup exists, we are done. If it doesn't, then we
* need to actually add one to the service instance.
*/
if (scf_service_get_pg(handle->scf_service,
pgroup, handle->scf_pg) != 0) {
/* doesn't exist so create one */
if (scf_service_add_pg(handle->scf_service, pgroup,
SCF_GROUP_APPLICATION, 0, handle->scf_pg) != 0) {
err = scf_error();
if (err != SCF_ERROR_NONE)
smb_smf_scf_log_error(NULL);
switch (err) {
case SCF_ERROR_PERMISSION_DENIED:
ret = SMBD_SMF_NO_PERMISSION;
break;
default:
ret = SMBD_SMF_SYSTEM_ERR;
break;
}
}
}
return (ret);
}
/*
* Start transaction on current pg in handle.
* The pg could be service or instance level.
* Must be called after pg handle is obtained
* from create or get.
*/
int
smb_smf_start_transaction(smb_scfhandle_t *handle)
{
int ret = SMBD_SMF_OK;
if (!handle || (!handle->scf_pg))
return (SMBD_SMF_SYSTEM_ERR);
/*
* lookup the property group and create it if it doesn't already
* exist.
*/
if (handle->scf_state == SCH_STATE_INIT) {
if (ret == SMBD_SMF_OK) {
handle->scf_trans =
scf_transaction_create(handle->scf_handle);
if (handle->scf_trans != NULL) {
if (scf_transaction_start(handle->scf_trans,
handle->scf_pg) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
scf_transaction_destroy(
handle->scf_trans);
handle->scf_trans = NULL;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
}
}
if (ret == SMBD_SMF_SYSTEM_ERR &&
scf_error() == SCF_ERROR_PERMISSION_DENIED)
ret = SMBD_SMF_NO_PERMISSION;
return (ret);
}
/*
* smb_smf_end_transaction(handle)
*
* Commit the changes that were added to the transaction in the
* handle. Do all necessary cleanup.
*/
int
smb_smf_end_transaction(smb_scfhandle_t *handle)
{
int ret = SMBD_SMF_OK;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
if (handle->scf_trans == NULL) {
ret = SMBD_SMF_SYSTEM_ERR;
} else {
if (scf_transaction_commit(handle->scf_trans) < 0) {
ret = SMBD_SMF_SYSTEM_ERR;
smb_smf_scf_log_error("Failed to commit "
"transaction: %s");
}
scf_transaction_destroy_children(handle->scf_trans);
scf_transaction_destroy(handle->scf_trans);
handle->scf_trans = NULL;
}
return (ret);
}
/*
* Sets string property in current pg
*/
int
smb_smf_set_string_property(smb_scfhandle_t *handle,
char *propname, char *valstr)
{
int ret = SMBD_SMF_OK;
scf_value_t *value = NULL;
scf_transaction_entry_t *entry = NULL;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
/*
* properties must be set in transactions and don't take
* effect until the transaction has been ended/committed.
*/
value = scf_value_create(handle->scf_handle);
entry = scf_entry_create(handle->scf_handle);
if (value != NULL && entry != NULL) {
if (scf_transaction_property_change(handle->scf_trans, entry,
propname, SCF_TYPE_ASTRING) == 0 ||
scf_transaction_property_new(handle->scf_trans, entry,
propname, SCF_TYPE_ASTRING) == 0) {
if (scf_value_set_astring(value, valstr) == 0) {
if (scf_entry_add_value(entry, value) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
scf_value_destroy(value);
}
/* the value is in the transaction */
value = NULL;
} else {
/* value couldn't be constructed */
ret = SMBD_SMF_SYSTEM_ERR;
}
/* the entry is in the transaction */
entry = NULL;
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (ret == SMBD_SMF_SYSTEM_ERR) {
switch (scf_error()) {
case SCF_ERROR_PERMISSION_DENIED:
ret = SMBD_SMF_NO_PERMISSION;
break;
}
}
/*
* cleanup if there were any errors that didn't leave these
* values where they would be cleaned up later.
*/
if (value != NULL)
scf_value_destroy(value);
if (entry != NULL)
scf_entry_destroy(entry);
return (ret);
}
/*
* Gets string property value.upto sz size.
* Caller is responsible to have enough memory allocated.
*/
int
smb_smf_get_string_property(smb_scfhandle_t *handle, char *propname,
char *valstr, size_t sz)
{
int ret = SMBD_SMF_OK;
scf_value_t *value;
scf_property_t *prop;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
value = scf_value_create(handle->scf_handle);
prop = scf_property_create(handle->scf_handle);
if (value && prop &&
(scf_pg_get_property(handle->scf_pg, propname, prop) == 0)) {
if (scf_property_get_value(prop, value) == 0) {
if (scf_value_get_astring(value, valstr, sz) < 0) {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (value != NULL)
scf_value_destroy(value);
if (prop != NULL)
scf_property_destroy(prop);
return (ret);
}
/*
* Set integer value of property.
* The value is returned as int64_t value
* Caller ensures appropriate translation.
*/
int
smb_smf_set_integer_property(smb_scfhandle_t *handle, char *propname,
int64_t valint)
{
int ret = SMBD_SMF_OK;
scf_value_t *value = NULL;
scf_transaction_entry_t *entry = NULL;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
/*
* properties must be set in transactions and don't take
* effect until the transaction has been ended/committed.
*/
value = scf_value_create(handle->scf_handle);
entry = scf_entry_create(handle->scf_handle);
if (value != NULL && entry != NULL) {
if (scf_transaction_property_change(handle->scf_trans, entry,
propname, SCF_TYPE_INTEGER) == 0 ||
scf_transaction_property_new(handle->scf_trans, entry,
propname, SCF_TYPE_INTEGER) == 0) {
scf_value_set_integer(value, valint);
if (scf_entry_add_value(entry, value) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
scf_value_destroy(value);
}
/* the value is in the transaction */
value = NULL;
}
/* the entry is in the transaction */
entry = NULL;
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (ret == SMBD_SMF_SYSTEM_ERR) {
switch (scf_error()) {
case SCF_ERROR_PERMISSION_DENIED:
ret = SMBD_SMF_NO_PERMISSION;
break;
}
}
/*
* cleanup if there were any errors that didn't leave these
* values where they would be cleaned up later.
*/
if (value != NULL)
scf_value_destroy(value);
if (entry != NULL)
scf_entry_destroy(entry);
return (ret);
}
/*
* Gets integer property value.
* Caller is responsible to have enough memory allocated.
*/
int
smb_smf_get_integer_property(smb_scfhandle_t *handle, char *propname,
int64_t *valint)
{
int ret = SMBD_SMF_OK;
scf_value_t *value = NULL;
scf_property_t *prop = NULL;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
value = scf_value_create(handle->scf_handle);
prop = scf_property_create(handle->scf_handle);
if ((prop) && (value) &&
(scf_pg_get_property(handle->scf_pg, propname, prop) == 0)) {
if (scf_property_get_value(prop, value) == 0) {
if (scf_value_get_integer(value,
valint) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (value != NULL)
scf_value_destroy(value);
if (prop != NULL)
scf_property_destroy(prop);
return (ret);
}
/*
* Set boolean value of property.
* The value is returned as int64_t value
* Caller ensures appropriate translation.
*/
int
smb_smf_set_boolean_property(smb_scfhandle_t *handle, char *propname,
uint8_t valbool)
{
int ret = SMBD_SMF_OK;
scf_value_t *value = NULL;
scf_transaction_entry_t *entry = NULL;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
/*
* properties must be set in transactions and don't take
* effect until the transaction has been ended/committed.
*/
value = scf_value_create(handle->scf_handle);
entry = scf_entry_create(handle->scf_handle);
if (value != NULL && entry != NULL) {
if (scf_transaction_property_change(handle->scf_trans, entry,
propname, SCF_TYPE_BOOLEAN) == 0 ||
scf_transaction_property_new(handle->scf_trans, entry,
propname, SCF_TYPE_BOOLEAN) == 0) {
scf_value_set_boolean(value, valbool);
if (scf_entry_add_value(entry, value) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
scf_value_destroy(value);
}
/* the value is in the transaction */
value = NULL;
}
/* the entry is in the transaction */
entry = NULL;
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (ret == SMBD_SMF_SYSTEM_ERR) {
switch (scf_error()) {
case SCF_ERROR_PERMISSION_DENIED:
ret = SMBD_SMF_NO_PERMISSION;
break;
}
}
/*
* cleanup if there were any errors that didn't leave these
* values where they would be cleaned up later.
*/
if (value != NULL)
scf_value_destroy(value);
if (entry != NULL)
scf_entry_destroy(entry);
return (ret);
}
/*
* Gets boolean property value.
* Caller is responsible to have enough memory allocated.
*/
int
smb_smf_get_boolean_property(smb_scfhandle_t *handle, char *propname,
uint8_t *valbool)
{
int ret = SMBD_SMF_OK;
scf_value_t *value = NULL;
scf_property_t *prop = NULL;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
value = scf_value_create(handle->scf_handle);
prop = scf_property_create(handle->scf_handle);
if ((prop) && (value) &&
(scf_pg_get_property(handle->scf_pg, propname, prop) == 0)) {
if (scf_property_get_value(prop, value) == 0) {
if (scf_value_get_boolean(value,
valbool) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (value != NULL)
scf_value_destroy(value);
if (prop != NULL)
scf_property_destroy(prop);
return (ret);
}
/*
* Sets a blob property value.
*/
int
smb_smf_set_opaque_property(smb_scfhandle_t *handle, char *propname,
void *voidval, size_t sz)
{
int ret = SMBD_SMF_OK;
scf_value_t *value;
scf_transaction_entry_t *entry;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
/*
* properties must be set in transactions and don't take
* effect until the transaction has been ended/committed.
*/
value = scf_value_create(handle->scf_handle);
entry = scf_entry_create(handle->scf_handle);
if (value != NULL && entry != NULL) {
if (scf_transaction_property_change(handle->scf_trans, entry,
propname, SCF_TYPE_OPAQUE) == 0 ||
scf_transaction_property_new(handle->scf_trans, entry,
propname, SCF_TYPE_OPAQUE) == 0) {
if (scf_value_set_opaque(value, voidval, sz) == 0) {
if (scf_entry_add_value(entry, value) != 0) {
ret = SMBD_SMF_SYSTEM_ERR;
scf_value_destroy(value);
}
/* the value is in the transaction */
value = NULL;
} else {
/* value couldn't be constructed */
ret = SMBD_SMF_SYSTEM_ERR;
}
/* the entry is in the transaction */
entry = NULL;
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (ret == SMBD_SMF_SYSTEM_ERR) {
switch (scf_error()) {
case SCF_ERROR_PERMISSION_DENIED:
ret = SMBD_SMF_NO_PERMISSION;
break;
}
}
/*
* cleanup if there were any errors that didn't leave these
* values where they would be cleaned up later.
*/
if (value != NULL)
scf_value_destroy(value);
if (entry != NULL)
scf_entry_destroy(entry);
return (ret);
}
/*
* Gets a blob property value.
* Caller is responsible to have enough memory allocated.
*/
int
smb_smf_get_opaque_property(smb_scfhandle_t *handle, char *propname,
void *v, size_t sz)
{
int ret = SMBD_SMF_OK;
scf_value_t *value = NULL;
scf_property_t *prop = NULL;
if (handle == NULL)
return (SMBD_SMF_SYSTEM_ERR);
value = scf_value_create(handle->scf_handle);
prop = scf_property_create(handle->scf_handle);
if ((prop) && (value) &&
(scf_pg_get_property(handle->scf_pg, propname, prop) == 0)) {
if (scf_property_get_value(prop, value) == 0) {
if (scf_value_get_opaque(value, (char *)v, sz) != sz) {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
} else {
ret = SMBD_SMF_SYSTEM_ERR;
}
if (value != NULL)
scf_value_destroy(value);
if (prop != NULL)
scf_property_destroy(prop);
return (ret);
}
/*
* smb_smf_scf_init()
*
* must be called before using any of the SCF functions.
* Returns smb_scfhandle_t pointer if success.
*/
smb_scfhandle_t *
smb_smf_scf_init(char *svc_name)
{
smb_scfhandle_t *handle;
handle = malloc(sizeof (smb_scfhandle_t));
if (handle != NULL) {
bzero((char *)handle, sizeof (smb_scfhandle_t));
handle->scf_state = SCH_STATE_INITIALIZING;
handle->scf_handle = scf_handle_create(SCF_VERSION);
if (handle->scf_handle != NULL) {
if (scf_handle_bind(handle->scf_handle) == 0) {
handle->scf_scope =
scf_scope_create(handle->scf_handle);
if (scf_handle_get_local_scope(
handle->scf_handle, handle->scf_scope) != 0)
goto err;
handle->scf_service =
scf_service_create(handle->scf_handle);
if (scf_scope_get_service(handle->scf_scope,
svc_name, handle->scf_service)
!= SCF_SUCCESS) {
goto err;
}
handle->scf_pg =
scf_pg_create(handle->scf_handle);
handle->scf_state = SCH_STATE_INIT;
} else {
goto err;
}
} else {
free(handle);
handle = NULL;
smb_smf_scf_log_error("Could not access SMF "
"repository: %s\n");
}
}
return (handle);
/* error handling/unwinding */
err:
(void) smb_smf_scf_fini(handle);
(void) smb_smf_scf_log_error("SMF initialization problem: %s\n");
return (NULL);
}
/*
* smb_smf_scf_fini(handle)
*
* must be called when done. Called with the handle allocated in
* smb_smf_scf_init(), it cleans up the state and frees any SCF resources
* still in use.
*/
void
smb_smf_scf_fini(smb_scfhandle_t *handle)
{
if (handle != NULL) {
int unbind = 0;
scf_iter_destroy(handle->scf_pg_iter);
handle->scf_pg_iter = NULL;
scf_iter_destroy(handle->scf_inst_iter);
handle->scf_inst_iter = NULL;
unbind = 1;
scf_scope_destroy(handle->scf_scope);
handle->scf_scope = NULL;
scf_instance_destroy(handle->scf_instance);
handle->scf_instance = NULL;
scf_service_destroy(handle->scf_service);
handle->scf_service = NULL;
scf_pg_destroy(handle->scf_pg);
handle->scf_pg = NULL;
handle->scf_state = SCH_STATE_UNINIT;
if (unbind)
(void) scf_handle_unbind(handle->scf_handle);
scf_handle_destroy(handle->scf_handle);
handle->scf_handle = NULL;
free(handle);
}
}
|