Changeset 3472

Show
Ignore:
Timestamp:
04/12/08 23:54:02 (6 months ago)
Author:
alonbl
Message:

Add --get-conf-entry, --set-conf-entry to opensc-tool

Although not perfect, will enable installer/users
to perform some simple tasks against configuration file.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/tools/opensc-tool.c

    r3405 r3472  
    5252        { "serial",             0, NULL,        OPT_SERIAL  }, 
    5353        { "name",               0, NULL,                'n' }, 
     54        { "get-conf-entry",     1, NULL,                'G' }, 
     55        { "set-conf-entry",     1, NULL,                'S' }, 
    5456        { "list-readers",       0, NULL,                'l' }, 
    5557        { "list-drivers",       0, NULL,                'D' }, 
     
    6971        "Prints the card serial number", 
    7072        "Identify the card and print its name", 
     73        "Get configuration key, format: section:name:key", 
     74        "Set configuration key, format: section:name:key:value", 
    7175        "Lists all configured readers", 
    7276        "Lists all installed card drivers", 
     
    108112        printf ("Enabled features:%s\n", OPENSC_FEATURES); 
    109113        return 0; 
     114} 
     115 
     116static int opensc_get_conf_entry(const char *config) 
     117{ 
     118        scconf_block *conf_block = NULL, **blocks; 
     119        char *buffer = NULL; 
     120        char *section = NULL; 
     121        char *name = NULL; 
     122        char *key = NULL; 
     123        int r = 0; 
     124 
     125        if (ctx->conf == NULL) { 
     126                r = ENOENT; 
     127                goto cleanup; 
     128        } 
     129 
     130        if ((buffer = strdup(config)) == NULL) { 
     131                r = ENOMEM; 
     132                goto cleanup; 
     133        } 
     134 
     135        section = buffer; 
     136        name = section == NULL ? NULL : strchr(section+1, ':'); 
     137        key = name == NULL ? NULL : strchr(name+1, ':'); 
     138        if (key == NULL) { 
     139                r = EINVAL; 
     140                goto cleanup; 
     141        } 
     142        *name = '\0'; 
     143        name++; 
     144        *key = '\0'; 
     145        key++; 
     146 
     147        blocks = scconf_find_blocks(ctx->conf, NULL, section, name); 
     148        if (blocks[0]) 
     149                conf_block = blocks[0]; 
     150        free(blocks); 
     151        if (conf_block != NULL) { 
     152                const char *value = scconf_get_str(conf_block, key, NULL); 
     153 
     154                if (value != NULL) { 
     155                        printf ("%s\n", value); 
     156                } 
     157        } 
     158 
     159        r = 0; 
     160 
     161cleanup: 
     162 
     163        if (buffer != NULL) 
     164                free(buffer); 
     165 
     166        return r; 
     167} 
     168 
     169static int opensc_set_conf_entry(const char *config) 
     170{ 
     171        scconf_block *conf_block = NULL, **blocks; 
     172        char *buffer = NULL; 
     173        char *section = NULL; 
     174        char *name = NULL; 
     175        char *key = NULL; 
     176        char *value = NULL; 
     177        int r = 0; 
     178 
     179        if (ctx->conf == NULL) { 
     180                r = ENOENT; 
     181                goto cleanup; 
     182        } 
     183 
     184        if ((buffer = strdup(config)) == NULL) { 
     185                r = ENOMEM; 
     186                goto cleanup; 
     187        } 
     188 
     189        section = buffer; 
     190        name = section == NULL ? NULL : strchr(section+1, ':'); 
     191        key = name == NULL ? NULL : strchr(name+1, ':'); 
     192        value = key == NULL ? NULL : strchr(key+1, ':'); 
     193        if (value == NULL) { 
     194                r = EINVAL; 
     195                goto cleanup; 
     196        } 
     197        *name = '\0'; 
     198        name++; 
     199        *key = '\0'; 
     200        key++; 
     201        *value = '\0'; 
     202        value++; 
     203 
     204        blocks = scconf_find_blocks(ctx->conf, NULL, section, name); 
     205        if (blocks[0]) 
     206                conf_block = blocks[0]; 
     207        free(blocks); 
     208        if (conf_block != NULL) { 
     209                scconf_item *item; 
     210 
     211                for (item = conf_block->items; item != NULL; item = item->next) { 
     212                        scconf_list *list; 
     213 
     214                        if ((item->type != SCCONF_ITEM_TYPE_VALUE) 
     215                            || (strcmp(item->key, key) != 0)) 
     216                                continue; 
     217                        list = item->value.list; 
     218                        scconf_list_destroy(list); 
     219                        list = NULL; 
     220                        scconf_list_add(&list, value); 
     221                        item->value.list = list; 
     222                        break; 
     223                } 
     224                if (item == NULL) 
     225                        scconf_put_str(conf_block, key, value); 
     226        } 
     227 
     228        /* Write */ 
     229        if ((r = scconf_write(ctx->conf, ctx->conf->filename)) != 0) { 
     230                fprintf(stderr, "scconf_write(): %s\n", strerror(r)); 
     231                goto cleanup; 
     232        } 
     233 
     234        r = 0; 
     235 
     236cleanup: 
     237 
     238        if (buffer != NULL) 
     239                free(buffer); 
     240 
     241        return r; 
    110242} 
    111243 
     
    403535        int err = 0, r, c, long_optind = 0; 
    404536        int do_info = 0; 
     537        int do_get_conf_entry = 0; 
     538        int do_set_conf_entry = 0; 
    405539        int do_list_readers = 0; 
    406540        int do_list_drivers = 0; 
     
    413547        int action_count = 0; 
    414548        const char *opt_driver = NULL; 
     549        const char *opt_conf_entry = NULL; 
    415550        sc_context_param_t ctx_param; 
    416551                 
     
    419554 
    420555        while (1) { 
    421                 c = getopt_long(argc, argv, "inlfr:vs:DRc:aw", options, &long_optind); 
     556                c = getopt_long(argc, argv, "inlG:S:fr:vs:DRc:aw", options, &long_optind); 
    422557                if (c == -1) 
    423558                        break; 
     
    427562                case 'i': 
    428563                        do_info = 1; 
     564                        action_count++; 
     565                        break; 
     566                case 'G': 
     567                        do_get_conf_entry = 1; 
     568                        opt_conf_entry = optarg; 
     569                        action_count++; 
     570                        break; 
     571                case 'S': 
     572                        do_set_conf_entry = 1; 
     573                        opt_conf_entry = optarg; 
    429574                        action_count++; 
    430575                        break; 
     
    499644        if (verbose > 1) 
    500645                ctx->debug = verbose-1; 
     646        if (do_get_conf_entry) { 
     647                if ((err = opensc_get_conf_entry (opt_conf_entry))) 
     648                        goto end; 
     649                action_count--; 
     650        } 
     651        if (do_set_conf_entry) { 
     652                if ((err = opensc_set_conf_entry (opt_conf_entry))) 
     653                        goto end; 
     654                action_count--; 
     655        } 
    501656        if (do_list_rdrivers) { 
    502657                if ((err = list_reader_drivers()))