/* * read initiator name from supplied filename */ int isns_read_initiatorname(const char *filename) { FILE *fp; char *name, *pos; if ((fp = fopen(filename, "r")) == NULL) { perror(filename); return -1; } while ((pos = parser_get_next_line(fp)) != NULL) { pos[strcspn(pos, "#")] = '\0'; if (!(name = parser_get_next_word(&pos))) continue; if (strcmp(name, "InitiatorName")) continue; if (pos[0] == '=') pos++; if (!strncmp(pos, "iqn.", 4)) isns_assign_string(&isns_config.ic_source_name, pos); } fclose(fp); return 0; }
/* * Read the registry file, match each entry against the given owner=<foo> tag, * and invoke the callback function. * This is used for both reading the registry, and rewriting it. */ static int __isns_local_registry_read(const char *match_owner, __isns_local_registry_cb_fn_t handle_matching, __isns_local_registry_cb_fn_t handle_nonmatching, void *user_data) { const char *filename = isns_config.ic_local_registry_file; char *line, *copy = NULL; FILE *fp; int rv = 0, owner_len; if (!(fp = fopen(filename, "r"))) { if (errno == ENOENT) { isns_debug_state("Unable to open %s: %m\n", filename); return 1; } isns_error("Unable to open %s: %m\n", filename); return 0; } owner_len = match_owner? strlen(match_owner) : 0; while ((line = parser_get_next_line(fp)) != NULL) { __isns_local_registry_cb_fn_t *cb; char *argv[256], *owner; int argc = 0; isns_assign_string(©, line); argc = isns_attr_list_split(line, argv, 255); if (argc <= 0) continue; /* Last attr should be owner */ if (strncasecmp(argv[argc-1], "owner=", 6)) { isns_error("%s: syntax error (missing owner field)\n", filename); goto out; } owner = argv[argc-1] + 6; if (!strncasecmp(owner, match_owner, owner_len) && (owner[owner_len] == '\0' || owner[owner_len] == ':')) cb = handle_matching; else cb = handle_nonmatching; if (cb && !cb(copy, argc, argv, user_data)) goto out; } rv = 1; out: free(copy); fclose(fp); return rv; }
/* * Read the iSNS configuration file */ int isns_read_config(const char *filename) { FILE *fp; char *name, *pos; __isns_config_defaults(); if ((fp = fopen(filename, "r")) == NULL) { perror(filename); return -1; } while ((pos = parser_get_next_line(fp)) != NULL) { pos[strcspn(pos, "#")] = '\0'; if (!(name = parser_get_next_word(&pos))) continue; isns_config_set(name, pos); } fclose(fp); /* Massage the config file */ if (isns_config.ic_security < 0) { /* By default, we will enable authentication * whenever we find our private key, and * the server's public key. */ if (access(isns_config.ic_auth_key_file, R_OK) == 0 && access(isns_config.ic_server_key_file, R_OK) == 0) isns_config.ic_security = 1; else isns_config.ic_security = 0; } return 0; }
static int parser_read_file_compact( ParserState *st, PData *section ) { /* section is the parent pdata that needs some entries */ PData *pd = 0; char *line, *cur; while ( ( line = parser_get_next_line(st) ) ) { switch ( line[0] ) { case '>': /* this section is finished */ return 1; case '<': /* add a whole subsection */ pd = parser_create_pdata(strdup( line + 1 ), 0, st->lineno, st->ctd); pd->entries = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK ); parser_read_file_compact( st, pd ); /* add to section */ list_add( section->entries, pd ); break; default: /* check name */ if ( ( cur = strchr( line, '»' ) ) == 0 ) { sprintf( parser_sub_error, tr("parse error: use '%c' for assignment or '<' for section"), '»' ); return 0; } cur[0] = 0; cur++; /* read values as subsection */ pd = parser_create_pdata(strdup( line ), parser_explode_string( cur, '°' ), st->lineno, st->ctd); /* add to section */ list_add( section->entries, pd ); break; } } return 1; }