source: creddy/creddy.c @ 219f8ad

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 219f8ad was 219f8ad, checked in by Mike Ryan <mikeryan@…>, 14 years ago

silence debugging

  • Property mode set to 100644
File size: 4.3 KB
Line 
1#include <assert.h>
2#include <getopt.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#include <library.h>
8
9#include "creddy.h"
10
11#define OPT_CN              1
12#define OPT_VALIDITY        2
13
14int main(int argc, char **argv) {
15    int ret;
16    options_t options = { 0, };
17
18    options.validity = 1080;
19
20    struct option getopts[] = {
21        { "help",       0, &options.help, 1 },
22        { "generate",   0, &options.mode, MODE_GENERATE },
23        { "verify",     0, &options.mode, MODE_VERIFY },
24        { "keyid",      0, &options.mode, MODE_KEYID },
25        { "attribute",  0, &options.mode, MODE_ATTRIBUTE },
26        { "roles",      0, &options.mode, MODE_ROLES },
27
28        // generate options
29        { "cn",         1, 0, OPT_CN },
30        { "validity",   1, 0, OPT_VALIDITY },
31
32        { NULL },
33    };
34
35    for ( ; ; ) {
36        int c = getopt_long(argc, argv, "", getopts, NULL);
37        if (c < 0)
38            break;
39
40        switch (c) {
41            // set the option from the value in the getopts struct
42            case 0:
43                continue;
44
45            // generate options
46            case OPT_CN:
47                options.cn = xstrdup(optarg);
48                break;
49            case OPT_VALIDITY:
50                options.validity = atoi(optarg);
51                break;
52
53            case '?':
54                break;
55
56            default:
57                printf("wat\n");
58                return 45;
59        }
60    }
61
62    if (options.help || optind < argc) {
63        if (optind > 0 && optind < argc)
64            printf("I don't understand %s\n", argv[optind]);
65        usage(&options);
66    }
67
68    // init libstrongswan
69    atexit(library_deinit);
70    dbg_default_set_level(-1);
71    ret = library_init(NULL);
72    if (!ret)
73        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
74    ret = lib->plugins->load(
75        lib->plugins,
76        NULL,
77        lib->settings->get_str(lib->settings, "", PLUGINS)
78    );
79    if (!ret)
80        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
81
82    switch (options.mode) {
83        case MODE_GENERATE:
84            generate_main(&options);
85            break;
86
87        default:
88            usage(&options);
89    }
90
91    return 0;
92}
93
94void usage(options_t *opts) {
95    if (opts->mode == MODE_GENERATE)
96        printf(
97            "Usage: creddy --generate --cn <name> [ --validity <day> ]\n"
98            "    cert will be in ${name}_ID.der, private key in ${name}_private.der\n"
99            "    default validity: 1080 days\n"
100        );
101
102    else if (opts->mode == MODE_VERIFY)
103        printf(
104            "Usage: identity --verify --cert <cert> [ --attrcert <cert> ]\n"
105            "    if attrcert is provided, verify that it was issued by cert\n"
106        );
107
108    else if (opts->mode == MODE_KEYID)
109        printf(
110            "Usage: identity --keyid --cert <cert>\n"
111        );
112
113    else if (opts->mode == MODE_ATTRIBUTE)
114        printf(
115            "Usage: identity --attribute \\\n"
116            "                --issuer <cert> --key <key> --role <role> \\\n"
117            "                --subject <cert> [ --subject-role <role> ] \\\n"
118            "                [ --validity <days> ] --out <file>\n"
119            "    default validity: 365 days\n"
120        );
121
122    else if (opts->mode == MODE_ROLES)
123        printf(
124            "Usage: identity --roles --cert <cert>\n"
125        );
126
127    else
128        printf(
129            "Usage: creddy [--<mode>] [--help]\n"
130            "    --generate:  generate X.509 identity cert and private key\n"
131            "    --verify:    check validity of X.509 ID or attribute cert\n"
132            "    --keyid:     get fingerprint from X.509 ID cert\n"
133            "    --attribute: generate an X.509 attribute cert\n"
134            "    --roles:     list roles from an X.509 attribute cert\n"
135        );
136
137
138    exit(1);
139}
140
141void *xmalloc(size_t len) {
142    void *ret = malloc(len);
143    if (ret == NULL)
144        err(1, "couldn't malloc %d bytes\n", len);
145    return ret;
146}
147
148char *xstrdup(char *string) {
149    char *dup = strdup(string);
150    if (dup == NULL)
151        err(1, "Can't dup %s", string);
152    return dup;
153}
154
155int clean_name(char *string) {
156    int i;
157
158    assert(string != NULL);
159
160    // must start with a letter
161    if (!isalpha(string[0]))
162        return 0;
163
164    // rest must be alphanumeric
165    for (i = 1; string[i] != '\0'; ++i)
166        if (!isalnum(string[i]))
167            return 0;
168
169    return 1;
170}
Note: See TracBrowser for help on using the repository browser.