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