Boolean_t mgmt_param_save2scf(tgt_node_t *node, char *target_name, int lun) { targ_scf_t *h = NULL; char *pgname = NULL; ssize_t max_name_len; tgt_node_t *n = NULL; Boolean_t status = False; h = mgmt_handle_init(); if (h == NULL) return (status); if (alloc_scf_name(&max_name_len, (void *)&pgname) == False) { goto error; } (void) snprintf(pgname, max_name_len, "param_%s_%d", target_name, lun); (void) pthread_mutex_lock(&scf_param_mutex); if (mgmt_transaction_start(h, pgname, "parameter") == True) { (void) scf_pg_delete(h->t_pg); (void) mgmt_transaction_end(h); } if (mgmt_transaction_start(h, pgname, "parameter") == True) { for (n = node->x_child; n; n = n->x_sibling) { if (n->x_child == NULL) { /* now n is node of basic property */ new_property(h, n); } } new_property(h, node->x_attr); n = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, n); tgt_node_free(n); n = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, n); tgt_node_free(n); (void) mgmt_transaction_end(h); } status = True; error: (void) pthread_mutex_unlock(&scf_param_mutex); free(pgname); mgmt_handle_fini(h); return (status); }
Boolean_t mgmt_param_save2scf(tgt_node_t *node, char *target_name, int lun) { targ_scf_t *h = NULL; scf_property_t *prop = NULL; scf_value_t *value = NULL; scf_iter_t *iter = NULL; char pgname[64]; tgt_node_t *n = NULL; h = mgmt_handle_init(); if (h == NULL) return (False); snprintf(pgname, sizeof (pgname), "param_%s_%d", target_name, lun); (void) pthread_mutex_lock(&scf_param_mutex); if (mgmt_transaction_start(h, pgname, "parameter") == True) { scf_pg_delete(h->t_pg); mgmt_transaction_end(h); } if (mgmt_transaction_start(h, pgname, "parameter") == True) { for (n = node->x_child; n; n = n->x_sibling) { if (n->x_child == NULL) { /* now n is node of basic property */ new_property(h, n); } } new_property(h, node->x_attr); n = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, n); tgt_node_free(n); n = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, n); tgt_node_free(n); mgmt_transaction_end(h); } (void) pthread_mutex_unlock(&scf_param_mutex); return (True); error: (void) pthread_mutex_unlock(&scf_param_mutex); scf_iter_destroy(iter); scf_value_destroy(value); scf_property_destroy(prop); mgmt_handle_fini(h); return (False); }
Boolean_t tgt_node_remove(tgt_node_t *parent, tgt_node_t *child, match_type_t m) { tgt_node_t *s; tgt_node_t *c = NULL; if ((parent == NULL) || (child == NULL)) return (False); for (s = parent->x_child; s; c = s, s = s->x_sibling) { /* * See if the new child node matches one of the children * in the parent. */ if ((strcmp(s->x_name, child->x_name) == 0) && ((m == MatchName) || (strcmp(s->x_value, child->x_value) == 0))) { if (parent->x_child == s) { parent->x_child = s->x_sibling; } else { c->x_sibling = s->x_sibling; } tgt_node_free(s); break; } } if (s == NULL) return (False); else return (True); }
/* * []---- * | delete_element -- helper function to remove a node from a parent * | * | A False return value indicates a failure to allocate enough memory. * []---- */ static Boolean_t delete_element(char *name, tgt_node_t *p, match_type_t m) { tgt_node_t *c; if ((c = tgt_node_alloc(name, String, NULL)) == NULL) { return (False); } tgt_node_remove(p, c, m); tgt_node_free(c); return (True); }
/* * []---- * | modify_element -- helper function to create node and add it to parent * | * | A False return value indicates a failure to allocate enough memory. * []---- */ static Boolean_t modify_element(char *name, char *value, tgt_node_t *p, match_type_t m) { tgt_node_t *c; if ((c = tgt_node_alloc(name, String, value)) == NULL) { return (False); } else { tgt_node_replace(p, c, m); tgt_node_free(c); return (True); } }
void tgt_node_replace(tgt_node_t *parent, tgt_node_t *child, match_type_t m) { tgt_node_t *s; tgt_node_t *c; if ((parent == NULL) || (child == NULL)) return; for (s = parent->x_child; s; s = s->x_sibling) { /* * See if the new child node matches one of the children * in the parent. */ if ((strcmp(s->x_name, child->x_name) == 0) && ((m == MatchName) || (strcmp(s->x_value, child->x_value) == 0))) { /* * We have a match. Now save the values of the new * child in this current node. */ free(s->x_name); free(s->x_value); s->x_name = strdup(child->x_name); s->x_value = strdup(child->x_value); if (s->x_child) { tgt_node_free(s->x_child); s->x_child = NULL; } for (c = child->x_child; c; c = c->x_sibling) (void) tgt_node_add(s, tgt_node_dup(c)); break; } } if (s == NULL) { /* * Never found the child so add it */ (void) tgt_node_add(parent, tgt_node_dup(child)); } }
void tgt_node_free(tgt_node_t *n) { tgt_node_t *c; tgt_node_t *c1; if (n == NULL) return; for (c = n->x_child; c; ) { c1 = c->x_sibling; tgt_node_free(c); c = c1; } for (c = n->x_attr; c; ) { c1 = c->x_sibling; node_free(c); c = c1; } node_free(n); }
/* * this function tries to convert configuration in files into scf * it loads xml conf into node tree then dump them to scf with * mgmt_config_save2scf() * this function has 3 return values: * CONVERT_OK: successfully converted * CONVERT_INIT_NEW: configuration files dont exist, created a new scf entry * CONVERT_FAIL: some error occurred in conversion and no scf entry created. * In this case, user have to check files manually and try * conversion again. */ convert_ret_t mgmt_convert_conf() { targ_scf_t *h = NULL; xmlTextReaderPtr r; convert_ret_t ret = CONVERT_FAIL; int xml_fd = -1; int n; tgt_node_t *node = NULL; tgt_node_t *next = NULL; char path[MAXPATHLEN]; char *target = NULL; h = mgmt_handle_init(); if (h == NULL) return (CONVERT_FAIL); /* check main config in pgroup iscsitgt */ if (scf_service_get_pg(h->t_service, "iscsitgt", h->t_pg) == 0) { ret = CONVERT_OK; goto done; } /* check the conf files */ if (access(config_file, R_OK) != 0) { /* * if there is no configuration file, initialize * an empty scf entry */ if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) { ret = CONVERT_INIT_NEW; node = tgt_node_alloc(XML_ELEMENT_VERS, String, "1.0"); new_property(h, node); tgt_node_free(node); /* "daemonize" is set to true by default */ node = tgt_node_alloc(XML_ELEMENT_DBGDAEMON, String, "true"); new_property(h, node); tgt_node_free(node); node = NULL; node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, node); tgt_node_free(node); node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, node); tgt_node_free(node); mgmt_transaction_end(h); } else { syslog(LOG_ERR, "Creating empty entry failed"); ret = CONVERT_FAIL; goto done; } if (mgmt_transaction_start(h, "passwords", "application") == True) { node = tgt_node_alloc(ISCSI_READ_AUTHNAME, String, ISCSI_AUTH_READ); new_property(h, node); tgt_node_free(node); node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, node); tgt_node_free(node); node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, node); tgt_node_free(node); mgmt_transaction_end(h); } else { syslog(LOG_ERR, "Creating empty entry failed"); ret = CONVERT_FAIL; } goto done; } if ((xml_fd = open(config_file, O_RDONLY)) >= 0) r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd, NULL, NULL, 0); if (r != NULL) { n = xmlTextReaderRead(r); while (n == 1) { if (tgt_node_process(r, &node) == False) { break; } n = xmlTextReaderRead(r); } if (n < 0) { syslog(LOG_ERR, "Parsing main config failed"); ret = CONVERT_FAIL; goto done; } main_config = node; (void) tgt_find_value_str(node, XML_ELEMENT_BASEDIR, &target_basedir); if (target_basedir == NULL) target_basedir = strdup(DEFAULT_TARGET_BASEDIR); /* Now convert targets' config if possible */ if (xml_fd != -1) (void) close(xml_fd); xmlTextReaderClose(r); xmlFreeTextReader(r); xmlCleanupParser(); r = NULL; xml_fd = -1; node = NULL; (void) snprintf(path, MAXPATHLEN, "%s/%s", target_basedir, "config.xml"); if ((xml_fd = open(path, O_RDONLY)) >= 0) r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd, NULL, NULL, 0); if (r != NULL) { n = xmlTextReaderRead(r); while (n == 1) { if (tgt_node_process(r, &node) == False) { break; } n = xmlTextReaderRead(r); } if (n < 0) { syslog(LOG_ERR, "Parsing target conf failed"); ret = CONVERT_FAIL; goto done; } /* now combine main_config and node */ if (node) { next = NULL; while ((next = tgt_node_next(node, XML_ELEMENT_TARG, next)) != NULL) { tgt_node_add(main_config, tgt_node_dup(next)); } tgt_node_free(node); } if (mgmt_config_save2scf() != True) { syslog(LOG_ERR, "Converting config failed"); if (xml_fd != -1) (void) close(xml_fd); xmlTextReaderClose(r); xmlFreeTextReader(r); xmlCleanupParser(); ret = CONVERT_FAIL; goto done; } /* Copy files into backup dir */ (void) snprintf(path, sizeof (path), "%s/backup", target_basedir); if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) { syslog(LOG_ERR, "Creating backup dir failed"); ret = CONVERT_FAIL; goto done; } backup(config_file, NULL); (void) snprintf(path, MAXPATHLEN, "%s/%s", target_basedir, "config.xml"); backup(path, NULL); while ((next = tgt_node_next(main_config, XML_ELEMENT_TARG, next)) != NULL) { if (tgt_find_value_str(next, XML_ELEMENT_INAME, &target) == False) { continue; } (void) snprintf(path, MAXPATHLEN, "%s/%s", target_basedir, target); if (mgmt_convert_param(path, next) != True) { ret = CONVERT_FAIL; goto done; } free(target); } ret = CONVERT_OK; syslog(LOG_NOTICE, "Conversion succeeded"); xmlTextReaderClose(r); xmlFreeTextReader(r); xmlCleanupParser(); } else { syslog(LOG_ERR, "Reading targets config failed"); ret = CONVERT_FAIL; goto done; } } else { syslog(LOG_ERR, "Reading main config failed"); ret = CONVERT_FAIL; goto done; } done: if (xml_fd != -1) (void) close(xml_fd); mgmt_handle_fini(h); return (ret); }
/* * mgmt_config_save2scf() saves main configuration to scf * See also : mgmt_get_main_config() */ Boolean_t mgmt_config_save2scf() { targ_scf_t *h = NULL; scf_property_t *prop = NULL; scf_value_t *value = NULL; scf_iter_t *iter = NULL; char pgname[64]; char passcode[32]; unsigned int outlen; tgt_node_t *n = NULL; tgt_node_t *pn = NULL; tgt_node_t *tn = NULL; scf_transaction_t *tx = NULL; secret_list_t *sl_head; secret_list_t *sl_tail; h = mgmt_handle_init(); if (h == NULL) return (False); prop = scf_property_create(h->t_handle); value = scf_value_create(h->t_handle); iter = scf_iter_create(h->t_handle); (void) pthread_mutex_lock(&scf_conf_mutex); if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) { scf_pg_delete(h->t_pg); mgmt_transaction_end(h); } if (mgmt_transaction_start(h, "passwords", "application") == True) { scf_pg_delete(h->t_pg); mgmt_transaction_end(h); } if (scf_iter_service_pgs_typed(iter, h->t_service, "configuration") == -1) { goto error; } tx = scf_transaction_create(h->t_handle); while (scf_iter_next_pg(iter, h->t_pg) > 0) { scf_transaction_start(tx, h->t_pg); scf_pg_delete(h->t_pg); scf_transaction_commit(tx); } scf_transaction_reset(tx); scf_transaction_destroy(tx); sl_head = (secret_list_t *)calloc(1, sizeof (secret_list_t)); sl_tail = sl_head; if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) { for (n = main_config->x_child; n; n = n->x_sibling) { if (strcmp(n->x_name, XML_ELEMENT_CHAPSECRET) == 0) { sl_tail->next = (secret_list_t *) calloc(1, sizeof (secret_list_t)); sl_tail = sl_tail->next; sl_tail->name = strdup("main"); sl_tail->secret = strdup(n->x_value); continue; } /* so does the radius server secret */ if (strcmp(n->x_name, XML_ELEMENT_RAD_SECRET) == 0) { sl_tail->next = (secret_list_t *) calloc(1, sizeof (secret_list_t)); sl_tail = sl_tail->next; sl_tail->name = strdup("radius"); sl_tail->secret = strdup(n->x_value); continue; } if (n->x_child == NULL) { new_property(h, n); } } new_property(h, main_config->x_attr); n = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, n); tgt_node_free(n); n = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, n); tgt_node_free(n); mgmt_transaction_end(h); } /* now update target/initiator/tpgt information */ for (n = main_config->x_child; n; n = n->x_sibling) { if (n->x_child == NULL) continue; snprintf(pgname, sizeof (pgname), "%s_%s", n->x_name, n->x_value); if (mgmt_transaction_start(h, pgname, "configuration") == True) { for (pn = n->x_child; pn; pn = pn->x_sibling) { if (strcmp(pn->x_name, XML_ELEMENT_CHAPSECRET) == 0) { sl_tail->next = (secret_list_t *) calloc(1, sizeof (secret_list_t)); sl_tail = sl_tail->next; sl_tail->name = (char *) calloc(1, strlen(n->x_value) + 3); snprintf(sl_tail->name, strlen(n->x_value) + 3, "I_%s", n->x_value); sl_tail->secret = strdup(pn->x_value); continue; } if (pn->x_child == NULL) { /* normal property */ new_property(h, pn); } else { /* pn -> xxx-list */ new_value_list(h, pn); } tn = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, tn); tgt_node_free(tn); tn = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, tn); tgt_node_free(tn); } mgmt_transaction_end(h); } } if (mgmt_transaction_start(h, "passwords", "application") == True) { while (sl_head != NULL) { /* Here we use sl_tail as a temporari var */ sl_tail = sl_head->next; if (sl_head->name) { /* max length of encoded passwd is 24B */ sasl_encode64(sl_head->secret, strlen(sl_head->secret), passcode, sizeof (passcode), &outlen); n = tgt_node_alloc(sl_head->name, String, passcode); new_property(h, n); tgt_node_free(n); } if (sl_head->name) free(sl_head->name); if (sl_head->secret) free(sl_head->secret); free(sl_head); sl_head = sl_tail; } n = tgt_node_alloc(ISCSI_READ_AUTHNAME, String, ISCSI_AUTH_READ); new_property(h, n); tgt_node_free(n); n = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, n); tgt_node_free(n); n = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, n); tgt_node_free(n); mgmt_transaction_end(h); } if (smf_refresh_instance(SA_TARGET_SVC_INSTANCE_FMRI) != 0) goto error; (void) pthread_mutex_unlock(&scf_conf_mutex); scf_iter_destroy(iter); scf_value_destroy(value); scf_property_destroy(prop); return (True); error: (void) pthread_mutex_unlock(&scf_conf_mutex); scf_iter_destroy(iter); scf_value_destroy(value); scf_property_destroy(prop); mgmt_handle_fini(h); return (False); }
/* * check_auth_modify() checks if a given cred has * the authorization to add/change/remove configuration values. * cred is from the door call. */ Boolean_t check_auth_modify(ucred_t *cred) { targ_scf_t *h = NULL; Boolean_t ret = False; int exit_code = -1; uid_t uid; gid_t gid; pid_t pid; tgt_node_t *n = NULL; scf_transaction_entry_t *ent = NULL; const priv_set_t *eset; pid = fork(); switch (pid) { case 0: /* Child process to check authorization */ uid = ucred_geteuid(cred); if (seteuid(uid) != 0) { syslog(LOG_ERR, "not priviliged\n"); exit(-1); } gid = ucred_getegid(cred); if (setegid(gid) != 0) { syslog(LOG_ERR, "not priviliged\n"); exit(-1); } eset = ucred_getprivset(cred, PRIV_EFFECTIVE); setppriv(PRIV_ON, PRIV_EFFECTIVE, eset); h = mgmt_handle_init(); if (h == NULL) { exit(-1); } if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) { n = tgt_node_alloc("dummy", String, "dummy"); new_property(h, n); tgt_node_free(n); if (mgmt_transaction_end(h) == True) { exit_code = 0; } else { exit_code = -1; } } else { exit_code = -1; } if (exit_code != 0) { mgmt_handle_fini(h); exit(exit_code); } if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) { ent = scf_entry_create(h->t_handle); if (ent) { scf_transaction_property_delete(h->t_trans, ent, "dummy"); } } mgmt_transaction_end(h); mgmt_handle_fini(h); exit(exit_code); break; case -1: /* Fail to fork */ exit(SMF_EXIT_ERR_CONFIG); default: wait(&exit_code); exit_code = exit_code >> 8; if (exit_code == 0) ret = True; else ret = False; break; } return (ret); }
static int showStats(int operandLen, char *operand[], cmdOptions_t *options) { char *first_str = NULL; char scale_buf[16]; tgt_node_t *node, *n1; int interval = -1; int count = -1; int header; stat_delta_t cur_data, *pd; tgt_buf_add_tag(&first_str, "list", Tag_Start); tgt_buf_add_tag(&first_str, XML_ELEMENT_TARG, Tag_Start); tgt_buf_add(&first_str, XML_ELEMENT_IOSTAT, OPT_TRUE); if (operandLen) tgt_buf_add(&first_str, XML_ELEMENT_NAME, operand[0]); for (; options->optval; options++) { switch (options->optval) { case 0: break; case 'I': /* optarg = refresh interval */ interval = atoi(options->optarg); if (interval == 0) { (void) fprintf(stderr, "%s: %s\n", cmdName, gettext("interval must be non-zero")); free(first_str); return (1); } break; case 'N': count = atoi(options->optarg); if (count == 0) { (void) fprintf(stderr, "%s: %s\n", cmdName, gettext("count must be non-zero")); free(first_str); return (1); } break; default: (void) fprintf(stderr, "%s: %c: %s\n", cmdName, options->optval, gettext("unknown option")); free(first_str); return (1); } } tgt_buf_add_tag(&first_str, XML_ELEMENT_TARG, Tag_End); tgt_buf_add_tag(&first_str, "list", Tag_End); header = 1; /*CONSTANTCONDITION*/ while (1) { if (--header == 0) { (void) printf("%20s %12s %12s\n", " ", gettext("operations"), gettext("bandwidth ")); (void) printf("%-20s %5s %5s %5s %5s\n", gettext("device"), gettext("read"), gettext("write"), gettext("read"), gettext("write")); (void) printf("%-20s %5s %5s %5s %5s\n", "--------------------", "-----", "-----", "-----", "-----"); header = 20; } if ((node = tgt_door_call(first_str, 0)) == NULL) { (void) fprintf(stderr, "%s: %s\n", cmdName, gettext("No reponse from daemon")); return (1); } if (strcmp(node->x_name, XML_ELEMENT_RESULT)) { (void) fprintf(stderr, "%s: %s\n", cmdName, gettext("Bad XML response")); free(first_str); tgt_node_free(node); stats_free(); return (1); } n1 = NULL; while (n1 = tgt_node_next_child(node, XML_ELEMENT_TARG, n1)) { stats_load_counts(n1, &cur_data); if ((pd = stats_prev_counts(&cur_data)) == NULL) { free(first_str); tgt_node_free(node); return (1); } (void) printf("%-20s ", pd->device); (void) printf("%5s ", number_to_scaled_string(scale_buf, cur_data.read_cmds - pd->read_cmds, 1, 1024)); (void) printf("%5s ", number_to_scaled_string(scale_buf, cur_data.write_cmds - pd->write_cmds, 1, 1024)); (void) printf("%5s ", number_to_scaled_string(scale_buf, cur_data.read_blks - pd->read_blks, 512, 1024)); (void) printf("%5s\n", number_to_scaled_string(scale_buf, cur_data.write_blks - pd->write_blks, 512, 1024)); stats_update_counts(pd, &cur_data); } tgt_node_free(node); if (count == -1) { if (interval == -1) /* No count or internal, do it just once */ break; else (void) sleep(interval); } else if (--count) { if (interval == -1) break; else (void) sleep(interval); } else break; } stats_free(); free(first_str); return (0); }
/* * modify_zfs -- test for the existence of a certain dataset being shared * * Called when someone uses the iscsitgt_is_shared() function from libiscsitgt. * All that */ static char * modify_zfs(tgt_node_t *x, ucred_t *cred) { char *msg = NULL; char *dataset = NULL; char *prop; char *m; tgt_node_t *n = NULL; tgt_node_t *t = NULL; tgt_node_t *list = NULL; tgt_node_t *c1, *c2; Boolean_t change_made = False; uint64_t size; int status; int val; char *tru = "true"; (void) pthread_rwlock_wrlock(&targ_config_mutex); if (tgt_find_value_str(x, XML_ELEMENT_NAME, &dataset) == False) { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_NAME); goto error; } /* * Validate request */ if (tgt_find_value_str(x, XML_ELEMENT_VALIDATE, &tru)) { (void) pthread_rwlock_unlock(&targ_config_mutex); if (tru) free(tru); free(dataset); return (validate_zfs_iscsitgt(x)); } /* * Check for existance of ZFS shareiscsi properties */ status = get_zfs_shareiscsi(dataset, &n, &size, cred); if ((status != ERR_SUCCESS) && (status != ERR_NULL_XML_MESSAGE)) { xml_rtn_msg(&msg, ERR_TARG_NOT_FOUND); goto error; } while ((t = tgt_node_next_child(targets_config, XML_ELEMENT_TARG, t)) != NULL) { if (strcmp(t->x_value, dataset) == 0) break; } if (t == NULL) { xml_rtn_msg(&msg, ERR_TARG_NOT_FOUND); goto error; } if (tgt_find_value_str(x, XML_ELEMENT_TPGT, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_TPGT); goto error; } /* * Validate that the Target Portal Group Tag is reasonable. */ val = strtoll(prop, &m, 0); if ((val < TPGT_MIN) || (val > TPGT_MAX) || ((m != NULL) && (*m != '\0'))) { xml_rtn_msg(&msg, ERR_INVALID_TPGT); goto error; } if ((c1 = tgt_node_alloc(XML_ELEMENT_TPGT, String, prop)) == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } /* * Due to the fact that the targets_config differs from the * ZVOL properties stored in zfs_shareiscsi, two lists need to * be updated */ c2 = tgt_node_dup(c1); if ((list = tgt_node_next(t, XML_ELEMENT_TPGTLIST, NULL)) != NULL) { /* * tgt_node_replace will duplicate the child node * tgt_node_add which is used below just links it * into the tree. */ tgt_node_replace(list, c1, MatchBoth); tgt_node_free(c1); } else { list = tgt_node_alloc(XML_ELEMENT_TPGTLIST, String, ""); if (list == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } tgt_node_add(list, c1); tgt_node_add(t, list); } if ((list = tgt_node_next(n, XML_ELEMENT_TPGTLIST, NULL)) != NULL) { /* * tgt_node_replace will duplicate the child node * tgt_node_add which is used below just links it * into the tree. */ tgt_node_replace(list, c2, MatchBoth); tgt_node_free(c2); } else { list = tgt_node_alloc(XML_ELEMENT_TPGTLIST, String, ""); if (list == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } tgt_node_add(list, c2); tgt_node_add(n, list); } change_made = True; } if (tgt_find_value_str(x, XML_ELEMENT_ACL, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_ACL); goto error; } c1 = tgt_node_alloc(XML_ELEMENT_INIT, String, prop); if (c1 == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } /* * Due to the fact that the targets_config differs from the * ZVOL properties stored in zfs_shareiscsi, two lists need to * be updated */ c2 = tgt_node_dup(c1); if ((list = tgt_node_next(t, XML_ELEMENT_ACLLIST, NULL)) != NULL) { /* * tgt_node_replace will duplicate the child node * tgt_node_add which is used below just links it * into the tree. */ tgt_node_replace(list, c1, MatchBoth); tgt_node_free(c1); } else { list = tgt_node_alloc(XML_ELEMENT_ACLLIST, String, ""); if (list == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } tgt_node_add(list, c1); tgt_node_add(t, list); } if ((list = tgt_node_next(n, XML_ELEMENT_ACLLIST, NULL)) != NULL) { /* * tgt_node_replace will duplicate the child node * tgt_node_add which is used below just links it * into the tree. */ tgt_node_replace(list, c2, MatchBoth); tgt_node_free(c2); } else { list = tgt_node_alloc(XML_ELEMENT_ACLLIST, String, ""); if (list == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } tgt_node_add(list, c2); tgt_node_add(n, list); } change_made = True; } if (change_made == True) { status = put_zfs_shareiscsi(dataset, n); if (status != ERR_SUCCESS) { xml_rtn_msg(&msg, status); goto error; } else { xml_rtn_msg(&msg, ERR_SUCCESS); } } else { xml_rtn_msg(&msg, ERR_SUCCESS); } error: if (n) tgt_node_free(n); if (dataset) free(dataset); (void) pthread_rwlock_unlock(&targ_config_mutex); return (msg); }
/* * []---- * | modify_target -- updates one or more properties for a target * []---- */ static char * modify_target(tgt_node_t *x, ucred_t *cred) { char *msg = NULL; char *name = NULL; char iscsi_path[MAXPATHLEN]; char targ_name[64]; char *iscsi = NULL; char *prop = NULL; char path[MAXPATHLEN]; char *m; char buf[512]; /* one sector size block */ tgt_node_t *t = NULL; tgt_node_t *list = NULL; tgt_node_t *c = NULL; tgt_node_t *node = NULL; tgt_node_t *tpgt = NULL; Boolean_t change_made = False; int lun = 0; int fd; uint64_t val, new_lu_size, cur_lu_size; struct stat st; uint32_t isns_mods = 0; (void) pthread_rwlock_wrlock(&targ_config_mutex); if (tgt_find_value_str(x, XML_ELEMENT_NAME, &name) == False) { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_NAME); goto error; } while ((t = tgt_node_next_child(targets_config, XML_ELEMENT_TARG, t)) != NULL) { if (strcmp(t->x_value, name) == 0) { break; } } if (t == NULL) { free(name); xml_rtn_msg(&msg, ERR_TARG_NOT_FOUND); goto error; } if (tgt_find_attr_str(t, XML_ELEMENT_INCORE, &m) == True) { if (strcmp(m, "true") == 0) { free(m); free(name); (void) pthread_rwlock_unlock(&targ_config_mutex); return (modify_zfs(x, cred)); } free(m); } /* * Under base dir, file 'target name' is a symbolic link * to the real directory 'IQN name' which stores params and back * storage. Therefore we can easily get IQN name from target * name by read the symbolic link content. */ (void) snprintf(path, sizeof (path), "%s/%s", target_basedir, name); bzero(iscsi_path, sizeof (iscsi_path)); (void) readlink(path, iscsi_path, sizeof (iscsi_path)); iscsi = basename(iscsi_path); /* ---- Finished with these so go ahead and release the memory ---- */ (void) strncpy(targ_name, name, sizeof (targ_name)); free(name); /* * Grow the LU. We currently do not support shrinking the LU and * that is only because it's unknown if any applications could support * that type of data loss. To support shrinking all that would be * needed is to remove the new/old size check and perform a truncation. * The actually truncation request should be shipped off to the T10 * layer so that the LU thread can remap the smaller size without * anyone accessing the data. */ if (tgt_find_value_str(x, XML_ELEMENT_SIZE, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_TPGT); goto error; } if (strtoll_multiplier(prop, &new_lu_size) == False) { free(prop); xml_rtn_msg(&msg, ERR_INVALID_SIZE); goto error; } free(prop); if ((new_lu_size % 512LL) != 0) { xml_rtn_msg(&msg, ERR_SIZE_MOD_BLOCK); goto error; } new_lu_size /= 512LL; /* ---- default to LUN 0 ---- */ (void) tgt_find_value_int(x, XML_ELEMENT_LUN, &lun); /* ---- read in current parameters ---- */ if (mgmt_get_param(&node, targ_name, lun) == False) { xml_rtn_msg(&msg, ERR_OPEN_PARAM_FILE_FAILED); goto error; } /* ---- validate that we're indeed growing the LU ---- */ if (tgt_find_value_str(node, XML_ELEMENT_SIZE, &prop) == False) { xml_rtn_msg(&msg, ERR_INIT_XML_READER_FAILED); goto error; } if (strtoll_multiplier(prop, &cur_lu_size) == False) { free(prop); xml_rtn_msg(&msg, ERR_INVALID_SIZE); goto error; } free(prop); if (new_lu_size < cur_lu_size) { xml_rtn_msg(&msg, ERR_CANT_SHRINK_LU); goto error; } /* ---- check that this LU is of type 'disk' or 'tape' ---- */ if (tgt_find_value_str(node, XML_ELEMENT_DTYPE, &prop) == False) { xml_rtn_msg(&msg, ERR_INIT_XML_READER_FAILED); goto error; } if ((strcmp(prop, TGT_TYPE_DISK) != 0) && (strcmp(prop, TGT_TYPE_TAPE) != 0)) { xml_rtn_msg(&msg, ERR_RESIZE_WRONG_DTYPE); free(prop); goto error; } free(prop); /* ---- validate the backing store is a regular file ---- */ (void) snprintf(path, sizeof (path), "%s/%s/%s%d", target_basedir, iscsi, LUNBASE, lun); if (stat(path, &st) == -1) { xml_rtn_msg(&msg, ERR_STAT_BACKING_FAILED); goto error; } if ((st.st_mode & S_IFMT) != S_IFREG) { xml_rtn_msg(&msg, ERR_DISK_BACKING_MUST_BE_REGULAR_FILE); goto error; } /* ---- update the parameter node with new size ---- */ if ((c = tgt_node_alloc(XML_ELEMENT_SIZE, Uint64, &new_lu_size)) == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } tgt_node_replace(node, c, MatchName); tgt_node_free(c); /* ---- now update params file ---- */ (void) mgmt_param_save2scf(node, targ_name, lun); /* ---- grow lu backing store ---- */ (void) snprintf(path, sizeof (path), "%s/%s/%s%d", target_basedir, iscsi, LUNBASE, lun); if ((fd = open(path, O_RDWR|O_CREAT|O_LARGEFILE, 0600)) < 0) { xml_rtn_msg(&msg, ERR_LUN_NOT_FOUND); goto error; } (void) lseek(fd, (new_lu_size * 512LL) - 512LL, 0); bzero(buf, sizeof (buf)); if (write(fd, buf, sizeof (buf)) != sizeof (buf)) { xml_rtn_msg(&msg, ERR_LUN_NOT_GROWN); (void) close(fd); goto error; } (void) close(fd); /* ---- send updates to current initiators via ASC/ASCQ ---- */ iscsi_capacity_change(iscsi, lun); prop = NULL; tgt_node_free(node); node = NULL; change_made = True; } if (tgt_find_value_str(x, XML_ELEMENT_TPGT, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_TPGT); goto error; } /* * Validate that the Target Portal Group Tag is reasonable. */ val = strtoll(prop, &m, 0); if ((val < TPGT_MIN) || (val > TPGT_MAX) || ((m != NULL) && (*m != '\0'))) { xml_rtn_msg(&msg, ERR_INVALID_TPGT); free(prop); goto error; } /* update isns only if TPGT contains ip_addr */ tpgt = NULL; while ((tpgt = tgt_node_next_child(main_config, XML_ELEMENT_TPGT, tpgt)) != NULL) { if (strcmp(prop, tpgt->x_value) != 0) continue; if (tgt_node_next(tpgt, XML_ELEMENT_IPADDR, NULL) != NULL) { isns_mods |= ISNS_MOD_TPGT; break; } else { xml_rtn_msg(&msg, ERR_TPGT_NO_IPADDR); free(prop); goto error; } } if ((c = tgt_node_alloc(XML_ELEMENT_TPGT, String, prop)) == NULL) { free(prop); xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } if ((list = tgt_node_next(t, XML_ELEMENT_TPGTLIST, NULL)) != NULL) { tgt_node_replace(list, c, MatchBoth); /* * tgt_node_replace will duplicate the child node * tgt_node_add which is used below just links it * into the tree. */ tgt_node_free(c); } else { list = tgt_node_alloc(XML_ELEMENT_TPGTLIST, String, ""); if (list == NULL) { free(prop); xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } tgt_node_add(list, c); tgt_node_add(t, list); } free(prop); prop = NULL; change_made = True; } if (tgt_find_value_str(x, XML_ELEMENT_ACL, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_ACL); goto error; } c = tgt_node_alloc(XML_ELEMENT_INIT, String, prop); if (c == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); free(prop); goto error; } if ((list = tgt_node_next(t, XML_ELEMENT_ACLLIST, NULL)) != NULL) { tgt_node_replace(list, c, MatchBoth); /* ---- See above usage ---- */ tgt_node_free(c); } else { list = tgt_node_alloc(XML_ELEMENT_ACLLIST, String, ""); if (list == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); free(prop); goto error; } tgt_node_add(list, c); tgt_node_add(t, list); } free(prop); prop = NULL; change_made = True; } if (tgt_find_value_str(x, XML_ELEMENT_ALIAS, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_ALIAS); goto error; } if (modify_element(XML_ELEMENT_ALIAS, prop, t, MatchName) == False) { xml_rtn_msg(&msg, ERR_NO_MEM); free(prop); goto error; } free(prop); prop = NULL; isns_mods |= ISNS_MOD_ALIAS; change_made = True; } if (tgt_find_value_str(x, XML_ELEMENT_MAXRECV, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_MAXRECV); goto error; } if ((strtoll_multiplier(prop, &val) == False) || (val < MAXRCVDATA_MIN) || (val > MAXRCVDATA_MAX)) { free(prop); xml_rtn_msg(&msg, ERR_INVALID_MAXRECV); goto error; } free(prop); if ((prop = malloc(32)) == NULL) { xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } (void) snprintf(prop, 32, "%d", val); if (modify_element(XML_ELEMENT_MAXRECV, prop, t, MatchName) == False) { free(prop); xml_rtn_msg(&msg, ERR_NO_MEM); goto error; } free(prop); prop = NULL; change_made = True; } if (change_made == True) { if (mgmt_config_save2scf() == False) { xml_rtn_msg(&msg, ERR_UPDATE_TARGCFG_FAILED); goto error; } if (isns_enabled() == True) { if (isns_dev_update(t->x_value, isns_mods) != 0) { xml_rtn_msg(&msg, ERR_ISNS_ERROR); goto error; } } xml_rtn_msg(&msg, ERR_SUCCESS); } else { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_OPERAND); } error: (void) pthread_rwlock_unlock(&targ_config_mutex); if (node) tgt_node_free(node); return (msg); }
static char * remove_target(tgt_node_t *x) { char *msg = NULL; char *prop = NULL; tgt_node_t *targ = NULL; tgt_node_t *list; tgt_node_t *c = NULL; Boolean_t change_made = False; int lun_num; if (tgt_find_value_str(x, XML_ELEMENT_NAME, &prop) == False) { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_NAME); return (msg); } while ((targ = tgt_node_next(targets_config, XML_ELEMENT_TARG, targ)) != NULL) { if (strcmp(targ->x_value, prop) == 0) break; } free(prop); if (targ == NULL) { xml_rtn_msg(&msg, ERR_TARG_NOT_FOUND); return (msg); } if (tgt_find_value_str(x, XML_ELEMENT_ACL, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_ACL); return (msg); } if ((list = tgt_node_next(targ, XML_ELEMENT_ACLLIST, NULL)) == NULL) { free(prop); xml_rtn_msg(&msg, ERR_ACL_NOT_FOUND); return (msg); } c = tgt_node_alloc(XML_ELEMENT_ACLINIT, String, prop); if (tgt_node_remove(list, c, MatchBoth) == False) { xml_rtn_msg(&msg, ERR_INIT_NOT_FOUND); goto error; } tgt_node_free(c); if (list->x_child == NULL) (void) tgt_node_remove(targ, list, MatchName); free(prop); change_made = True; } if (tgt_find_value_str(x, XML_ELEMENT_TPGT, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_TPGT); return (msg); } if ((list = tgt_node_next(targ, XML_ELEMENT_TPGTLIST, NULL)) == NULL) { free(prop); xml_rtn_msg(&msg, ERR_ACL_NOT_FOUND); return (msg); } c = tgt_node_alloc(XML_ELEMENT_TPGT, String, prop); if (tgt_node_remove(list, c, MatchBoth) == False) { xml_rtn_msg(&msg, ERR_TPGT_NOT_FOUND); goto error; } tgt_node_free(c); if (list->x_child == NULL) (void) tgt_node_remove(targ, list, MatchName); free(prop); /* update isns */ if (isns_enabled()) { if (isns_dev_update(targ->x_value, ISNS_MOD_TPGT) != 0) syslog(LOG_ALERT, "ISNS register failed\n"); } change_made = True; } if (tgt_find_value_int(x, XML_ELEMENT_LUN, &lun_num) == True) { if (tgt_find_value_intchk(x, XML_ELEMENT_LUN, &lun_num) == False) { xml_rtn_msg(&msg, ERR_LUN_INVALID_RANGE); return (msg); } /* * Save the iscsi-name which we'll need to remove LUNs. */ if (tgt_find_value_str(targ, XML_ELEMENT_INAME, &prop) == False) { xml_rtn_msg(&msg, ERR_TARGCFG_MISSING_INAME); return (msg); } logout_targ(prop); thick_provo_stop(prop, lun_num); remove_target_common(targ->x_value, lun_num, &msg); if (msg != NULL) goto error; /* ISNS de-register target if it's the last lun */ if (lun_num == 0 && isns_enabled() == True) { if (isns_dereg(prop) != 0) syslog(LOG_INFO, "ISNS dereg failed\n"); } iscsi_inventory_change(prop); free(prop); change_made = True; } if (change_made == True) { if (mgmt_config_save2scf() == True) xml_rtn_msg(&msg, ERR_SUCCESS); } else { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_OPERAND); } return (msg); error: if (c != NULL) tgt_node_free(c); if (prop != NULL) free(prop); return (msg); }
/* * mgmt_get_main_config() loads main configuration * from scf into a node tree. * Main configuration includes: admin/target/tpgt/initiator info. * admin info is stored in "iscsitgt" property group * target info is stored in "target_<name>" property group * initiator info is stored in "initiator_<name>" property group * tpgt info is stored in "tpgt_<number>" property group */ Boolean_t mgmt_get_main_config(tgt_node_t **node) { targ_scf_t *h = NULL; scf_property_t *prop = NULL; scf_value_t *value = NULL; scf_iter_t *iter = NULL; scf_iter_t *iter_v = NULL; scf_iter_t *iter_pv = NULL; char pname[32]; char valuebuf[MAXPATHLEN]; char passcode[32]; unsigned int outlen; tgt_node_t *n; tgt_node_t *pn; tgt_node_t *vn; Boolean_t status = False; h = mgmt_handle_init(); if (h == NULL) return (status); prop = scf_property_create(h->t_handle); value = scf_value_create(h->t_handle); iter = scf_iter_create(h->t_handle); (void) pthread_mutex_lock(&scf_conf_mutex); /* Basic Information is stored in iscsitgt pg */ if (scf_service_get_pg(h->t_service, "iscsitgt", h->t_pg) == -1) { goto error; } *node = NULL; *node = tgt_node_alloc("main_config", String, NULL); if (*node == NULL) goto error; if (scf_iter_pg_properties(iter, h->t_pg) == -1) { goto error; } while (scf_iter_next_property(iter, prop) > 0) { scf_property_get_value(prop, value); scf_value_get_as_string(value, valuebuf, MAXPATHLEN); scf_property_get_name(prop, pname, sizeof (pname)); /* avoid load auth to incore data */ if (strcmp(pname, ISCSI_READ_AUTHNAME) == 0 || strcmp(pname, ISCSI_MODIFY_AUTHNAME) == 0 || strcmp(pname, ISCSI_VALUE_AUTHNAME) == 0) continue; n = tgt_node_alloc(pname, String, valuebuf); if (n == NULL) goto error; /* put version info into root node's attr */ if (strcmp(pname, XML_ELEMENT_VERS) == 0) { tgt_node_add_attr(*node, n); } else { /* add other basic info into root node */ tgt_node_add(*node, n); } } /* * targets/initiators/tpgt information is * stored as type "configuration" in scf * each target's param is stored as type "parameter" */ if (scf_iter_service_pgs_typed(iter, h->t_service, "configuration") == -1) { goto error; } while (scf_iter_next_pg(iter, h->t_pg) > 0) { char *iname; scf_pg_get_name(h->t_pg, pname, sizeof (pname)); iname = strchr(pname, '_'); if (iname == NULL) { /* the pg found here is not a tgt/initiator/tpgt */ continue; } *iname = '\0'; iname++; /* * now pname is "target" or "initiator" or "tpgt" * meanwhile iname is the actual name of the item */ n = tgt_node_alloc(pname, String, iname); if (n == NULL) goto error; iter_v = scf_iter_create(h->t_handle); if (scf_iter_pg_properties(iter_v, h->t_pg) == -1) { goto error; } while (scf_iter_next_property(iter_v, prop) > 0) { /* there may be many values in one property */ char *vname; scf_property_get_name(prop, pname, sizeof (pname)); /* avoid load auth to incore data */ if (strcmp(pname, ISCSI_READ_AUTHNAME) == 0 || strcmp(pname, ISCSI_MODIFY_AUTHNAME) == 0 || strcmp(pname, ISCSI_VALUE_AUTHNAME) == 0) continue; vname = strstr(pname, "-list"); if (vname == NULL) { scf_property_get_value(prop, value); scf_value_get_as_string(value, valuebuf, MAXPATHLEN); pn = tgt_node_alloc(pname, String, valuebuf); if (pn == NULL) goto error; tgt_node_add(n, pn); } else { pn = tgt_node_alloc(pname, String, NULL); if (pn == NULL) goto error; tgt_node_add(n, pn); *vname = '\0'; iter_pv = scf_iter_create(h->t_handle); scf_iter_property_values(iter_pv, prop); while (scf_iter_next_value(iter_pv, value) > 0) { scf_value_get_as_string(value, valuebuf, MAXPATHLEN); vn = tgt_node_alloc(pname, String, valuebuf); if (vn == NULL) goto error; tgt_node_add(pn, vn); } scf_iter_destroy(iter_pv); iter_pv = NULL; } } tgt_node_add(*node, n); scf_iter_destroy(iter_v); iter_v = NULL; } /* chap-secrets are stored in "passwords" pgroup as "application" */ if (scf_service_get_pg(h->t_service, "passwords", h->t_pg) == 0) { if (scf_iter_pg_properties(iter, h->t_pg) == -1) { goto error; } while (scf_iter_next_property(iter, prop) > 0) { scf_property_get_value(prop, value); scf_value_get_as_string(value, valuebuf, MAXPATHLEN); scf_property_get_name(prop, pname, sizeof (pname)); /* avoid load auth to incore data */ if (strcmp(pname, ISCSI_READ_AUTHNAME) == 0 || strcmp(pname, ISCSI_MODIFY_AUTHNAME) == 0 || strcmp(pname, ISCSI_VALUE_AUTHNAME) == 0) continue; /* max length of decoded passwd is 16B */ sasl_decode64(valuebuf, strlen(valuebuf), passcode, sizeof (passcode), &outlen); if (strcmp(pname, "radius") == 0) { pn = tgt_node_alloc(XML_ELEMENT_RAD_SECRET, String, passcode); tgt_node_add(*node, pn); } else if (strcmp(pname, "main") == 0) { pn = tgt_node_alloc(XML_ELEMENT_CHAPSECRET, String, passcode); tgt_node_add(*node, pn); } else { /* find corresponding initiator */ n = NULL; while (n = tgt_node_next_child(*node, XML_ELEMENT_INIT, n)) { if (strcmp(pname + 2, n->x_value) != 0) continue; pn = tgt_node_alloc( XML_ELEMENT_CHAPSECRET, String, passcode); tgt_node_add(n, pn); } } } } status = True; error: if ((status != True) && (*node != NULL)) tgt_node_free(*node); (void) pthread_mutex_unlock(&scf_conf_mutex); if (iter_pv != NULL) scf_iter_destroy(iter_pv); if (iter_v != NULL) scf_iter_destroy(iter_v); scf_iter_destroy(iter); scf_value_destroy(value); scf_property_destroy(prop); mgmt_handle_fini(h); return (status); }
static char * remove_tpgt(tgt_node_t *x) { char *msg = NULL; char *prop = NULL; tgt_node_t *node = NULL; tgt_node_t *c = NULL; Boolean_t change_made = False; if (tgt_find_value_str(x, XML_ELEMENT_NAME, &prop) == False) { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_NAME); return (msg); } while ((node = tgt_node_next(main_config, XML_ELEMENT_TPGT, node)) != NULL) { if (strcmp(node->x_value, prop) == 0) break; } free(prop); if (node == NULL) { xml_rtn_msg(&msg, ERR_TPGT_NOT_FOUND); return (msg); } if (tgt_find_value_str(x, XML_ELEMENT_IPADDR, &prop) == True) { if (prop == NULL) { xml_rtn_msg(&msg, ERR_SYNTAX_EMPTY_IPADDR); return (msg); } c = tgt_node_alloc(XML_ELEMENT_IPADDR, String, prop); if (tgt_node_remove(node, c, MatchBoth) == False) { xml_rtn_msg(&msg, ERR_INVALID_IP); goto error; } tgt_node_free(c); free(prop); change_made = True; } if ((change_made != True) && (tgt_find_value_str(x, XML_ELEMENT_ALL, &prop) == True)) { tgt_node_remove(main_config, node, MatchBoth); change_made = True; } if (change_made == True) { /* Isns re-register all target */ if (isns_enabled() == True) isns_reg_all(); if (mgmt_config_save2scf() == True) xml_rtn_msg(&msg, ERR_SUCCESS); } else { xml_rtn_msg(&msg, ERR_SYNTAX_MISSING_OPERAND); } return (msg); error: if (c != NULL) tgt_node_free(c); if (prop != NULL) free(prop); return (msg); }
/* * Convert legacy (XML) configuration files into an equivalent SCF * representation. * * Read the XML from disk, translate the XML into a tree of nodes of * type tgt_node_t, and write the in-memory tree to SCF's persistent * data-store using mgmt_config_save2scf(). * * Return Values: * CONVERT_OK: successfully converted * CONVERT_INIT_NEW: configuration files don't exist; created an SCF entry * CONVERT_FAIL: some conversion error occurred; no SCF entry created. * In this case, user has to manually check files and try * conversion again. */ convert_ret_t mgmt_convert_conf() { targ_scf_t *h = NULL; xmlTextReaderPtr r; convert_ret_t ret = CONVERT_FAIL; int xml_fd = -1; int n; tgt_node_t *node = NULL; tgt_node_t *next = NULL; char path[MAXPATHLEN]; char *target = NULL; h = mgmt_handle_init(); if (h == NULL) return (CONVERT_FAIL); /* * Check if the "iscsitgt" PropertyGroup has already been added * to the "iscsitgt" SMF Service. If so, then we have already * converted the legacy configuration files (and there is no work * to do). */ if (scf_service_get_pg(h->t_service, "iscsitgt", h->t_pg) == 0) { ret = CONVERT_OK; goto done; } if (access(config_file, R_OK) != 0) { /* * then the Main Config file is not present; initialize * SCF Properties to default values. */ if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) { ret = CONVERT_INIT_NEW; node = tgt_node_alloc(XML_ELEMENT_VERS, String, "1.0"); new_property(h, node); tgt_node_free(node); /* "daemonize" is set to true by default */ node = tgt_node_alloc(XML_ELEMENT_DBGDAEMON, String, "true"); new_property(h, node); tgt_node_free(node); node = NULL; node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, node); tgt_node_free(node); node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, node); tgt_node_free(node); (void) mgmt_transaction_end(h); } else { syslog(LOG_ERR, "Creating empty entry failed"); ret = CONVERT_FAIL; goto done; } if (mgmt_transaction_start(h, "passwords", "application") == True) { node = tgt_node_alloc(ISCSI_READ_AUTHNAME, String, ISCSI_AUTH_READ); new_property(h, node); tgt_node_free(node); node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String, ISCSI_AUTH_MODIFY); new_property(h, node); tgt_node_free(node); node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String, ISCSI_AUTH_VALUE); new_property(h, node); tgt_node_free(node); (void) mgmt_transaction_end(h); } else { syslog(LOG_ERR, "Creating empty entry failed"); ret = CONVERT_FAIL; } goto done; } if ((xml_fd = open(config_file, O_RDONLY)) >= 0) r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd, NULL, NULL, 0); if (r != NULL) { int is_target_config; n = xmlTextReaderRead(r); while (n == 1) { if (tgt_node_process(r, &node) == False) { break; } n = xmlTextReaderRead(r); } if (n < 0) { syslog(LOG_ERR, "Parsing main config failed"); ret = CONVERT_FAIL; goto done; } main_config = node; /* * Initialize the Base Directory (global) variable by * using the value specified in the XML_ELEMENT_BASEDIR * XML tag. If a tag is not specified, use a default. */ (void) tgt_find_value_str(node, XML_ELEMENT_BASEDIR, &target_basedir); if (target_basedir == NULL) target_basedir = strdup(DEFAULT_TARGET_BASEDIR); if (xml_fd != -1) { (void) close(xml_fd); xml_fd = -1; } (void) xmlTextReaderClose(r); xmlFreeTextReader(r); xmlCleanupParser(); /* * If a Target Config file is present, read and translate * its XML representation into a tree of tgt_node_t. * Merge that tree with the tree of tgt_node_t rooted at * 'main_config'. The merged tree will then be archived * using an SCF representation. */ (void) snprintf(path, MAXPATHLEN, "%s/%s", target_basedir, "config.xml"); if ((xml_fd = open(path, O_RDONLY)) >= 0) { is_target_config = 1; r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd, NULL, NULL, 0); } else { is_target_config = 0; r = NULL; } if (r != NULL) { /* then the Target Config file is available. */ node = NULL; /* * Create a tree of tgt_node_t rooted at 'node' by * processing each XML Tag in the file. */ n = xmlTextReaderRead(r); while (n == 1) { if (tgt_node_process(r, &node) == False) { break; } n = xmlTextReaderRead(r); } if (n < 0) { syslog(LOG_ERR, "Parsing target conf failed"); ret = CONVERT_FAIL; goto done; } /* * Merge the tree at 'node' into the tree rooted at * 'main_config'. */ if (node != NULL) { next = NULL; while ((next = tgt_node_next(node, XML_ELEMENT_TARG, next)) != NULL) { tgt_node_add(main_config, tgt_node_dup(next)); } tgt_node_free(node); } } /* * Iterate over the in-memory tree rooted at 'main_config' * and write a representation of the appropriate nodes to * SCF's persistent data-store. */ if (mgmt_config_save2scf() != True) { syslog(LOG_ERR, "Converting config failed"); if (xml_fd != -1) { (void) close(xml_fd); xml_fd = -1; } (void) xmlTextReaderClose(r); xmlFreeTextReader(r); xmlCleanupParser(); ret = CONVERT_FAIL; goto done; } /* * Move the configuration files into a well-known backup * directory. This allows a user to restore their * configuration, if they choose. */ (void) snprintf(path, sizeof (path), "%s/backup", target_basedir); if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) { syslog(LOG_ERR, "Creating backup dir failed"); ret = CONVERT_FAIL; goto done; } /* Save the Main Config file. */ backup(config_file, NULL); /* Save the Target Config file, if it was present. */ if (is_target_config != 0) { (void) snprintf(path, MAXPATHLEN, "%s/%s", target_basedir, "config.xml"); backup(path, NULL); } /* * For each tgt_node_t node in 'main_config' whose value is * an iSCSI Name as defined in the RFC (3720) standard (eg, * "iqn.1986..."), read its XML-encoded attributes from a * flat-file and write an equivalent representation to SCF's * data-store. */ while ((next = tgt_node_next(main_config, XML_ELEMENT_TARG, next)) != NULL) { if (tgt_find_value_str(next, XML_ELEMENT_INAME, &target) == False) { continue; } (void) snprintf(path, MAXPATHLEN, "%s/%s", target_basedir, target); if (mgmt_convert_param(path, next) != True) { ret = CONVERT_FAIL; goto done; } free(target); } ret = CONVERT_OK; syslog(LOG_NOTICE, "Conversion succeeded"); (void) xmlTextReaderClose(r); xmlFreeTextReader(r); xmlCleanupParser(); } else { syslog(LOG_ERR, "Reading main config failed"); ret = CONVERT_FAIL; goto done; } done: if (xml_fd != -1) (void) close(xml_fd); mgmt_handle_fini(h); return (ret); }