summaryrefslogtreecommitdiffstats
path: root/proxy/src/gp_config.c
blob: f20a7b81c33662abfb4f7d626b18022c1bc2821f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
   GSS-PROXY

   Copyright (C) 2011 Red Hat, Inc.
   Copyright (C) 2011 Simo Sorce <simo.sorce@redhat.com>

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include "gp_proxy.h"
#include "iniparser.h"

#define GP_SOCKET_NAME "gssproxy.socket"

static void gp_credcfg_free(struct gp_credcfg *cred)
{
    free(cred->name);
    if (cred->mech == GP_CRED_KRB5) {
        free(cred->cred.krb5.keytab);
        free(cred->cred.krb5.ccache);
    }
    memset(cred, 0, sizeof(struct gp_credcfg));
}

static void gp_service_free(struct gp_service *svc)
{
    free(svc->name);
    free(svc->creds);
    memset(svc, 0, sizeof(struct gp_service));
}

static char *get_char_value(dictionary *dict,
                            const char *secname,
                            const char *key)
{
    char *skey;
    char *value;
    int ret;

    ret = asprintf(&skey, "%s:%s", secname, key);
    if (ret == -1) {
        return NULL;
    }

    value = iniparser_getstring(dict, skey, NULL);
    free(skey);
    return value;
}

static int get_int_value(dictionary *dict,
                         const char *secname,
                         const char *key)
{
    char *skey;
    int ret;

    ret = asprintf(&skey, "%s:%s", secname, key);
    if (ret == -1) {
        return -1;
    }

    ret = iniparser_getint(dict, skey, -1);
    free(skey);
    return ret;
}

static int get_krb5_mech_cfg(struct gp_credcfg *cred,
                             dictionary *dict,
                             const char *secname)
{
    const char *value;

    cred->name = strdup(&secname[11]); /* name after 'credentials/' */
    if (!cred->name) {
        return ENOMEM;
    }

    cred->mech = GP_CRED_KRB5;

    value = get_char_value(dict, secname, "krb5_keytab");
    if (value) {
        cred->cred.krb5.keytab = strdup(value);
        if (!cred->cred.krb5.keytab) {
            return ENOMEM;
        }
    }

    value = get_char_value(dict, secname, "krb5_ccache");
    if (value) {
        cred->cred.krb5.ccache = strdup(value);
        if (!cred->cred.krb5.ccache) {
            return ENOMEM;
        }
    }

    return 0;
}

static int get_creds_config(char *name, dictionary *dict,
                            struct gp_service *svc,
                            struct gp_credcfg **creds, int num_creds)
{
    char *value;
    char *token;
    char *handle;
    int i, n;

    svc->name = strdup(&name[8]); /* name after 'service/' */
    if (!svc->name) {
        return ENOMEM;
    }

    svc->creds = calloc(num_creds, sizeof(struct gp_credcfg *));
    if (!svc->creds) {
        return ENOMEM;
    }

    value = get_char_value(dict, name, "credentials");
    if (value == NULL) {
        /* malformed section, crentials is missing */
        syslog(LOG_INFO,
               "Credentials missing from [%s], ignoring.", name);
        return EINVAL;
    }

    n = 0;
    token = strtok_r(value, ", ", &handle);
    do {
        for (i = 0; i < num_creds; i++) {
            if (strcmp(token, creds[i]->name) == 0) {
                svc->creds[n] = creds[i];
                n++;
                break;
            }
        }

        token = strtok_r(NULL, ", ", &handle);
    } while (token != NULL);

    svc->num_creds = n;

    return 0;
}

static int load_services(struct gp_config *cfg, dictionary *dict)
{
    int num_sec;
    char *secname;
    char *value;
    int valnum;
    int ret;
    int i, n;

    num_sec = iniparser_getnsec(dict);

    /* allocate enough space for num_sec services and creds, will trim it
     * later when we know what is what */
    cfg->creds = calloc(num_sec, sizeof(struct gp_credcfg *));
    if (!cfg->creds) {
        ret = ENOMEM;
        goto done;
    }
    cfg->svcs = calloc(num_sec, sizeof(struct gp_service *));
    if (!cfg->svcs) {
        ret = ENOMEM;
        goto done;
    }

    for (i = 0; i < num_sec; i++) {
        secname = iniparser_getsecname(dict, i);
        ret = strncmp(secname, "credential/", 10);
        if (ret == 0) {
            n = cfg->num_creds;
            cfg->creds[n] = calloc(1, sizeof(struct gp_credcfg));
            if (!cfg->creds[n]) {
                ret = ENOMEM;
                goto done;
            }
            cfg->num_creds++;

            value = get_char_value(dict, secname, "mech");
            if (value == NULL) {
                /* malformed section, mech is missing */
                syslog(LOG_INFO,
                       "Mech missing from [%s], ignoring.", secname);
                gp_credcfg_free(cfg->creds[n]);
                cfg->num_creds--;
                continue;
            }

            ret = strcmp(value, "krb5");
            if (ret == 0) {
                ret = get_krb5_mech_cfg(cfg->creds[n], dict, secname);
                if (ret != 0) {
                    gp_credcfg_free(cfg->creds[n]);
                    cfg->num_creds--;
                    continue;
                }
            } else {
                syslog(LOG_INFO,
                       "Unknown mech: %s in [%s], ignoring.",
                       value, secname);
                gp_credcfg_free(cfg->creds[n]);
                cfg->num_creds--;
                continue;
            }
        }

        ret = strncmp(secname, "service/", 8);
        if (ret == 0) {
            n = cfg->num_svcs;
            cfg->svcs[n] = calloc(1, sizeof(struct gp_service));
            if (!cfg->svcs[n]) {
                ret = ENOMEM;
                goto done;
            }
            cfg->num_svcs++;

            valnum = get_int_value(dict, secname, "euid");
            if (valnum == -1) {
                /* malformed section, mech is missing */
                syslog(LOG_INFO,
                       "Euid missing from [%s], ignoring.", secname);
                gp_service_free(cfg->svcs[n]);
                cfg->num_svcs--;
                continue;
            }
            cfg->svcs[n]->euid = valnum;

            ret = get_creds_config(secname, dict, cfg->svcs[n],
                                   cfg->creds, cfg->num_creds);
            if (ret) {
                gp_service_free(cfg->svcs[n]);
                cfg->num_svcs--;
                continue;
            }
        }
    }

    if (cfg->num_creds == 0){
        syslog(LOG_ERR, "No credentials sections configured!");
        return ENOENT;
    }

    if (cfg->num_svcs == 0) {
        syslog(LOG_ERR, "No service sections configured!");
        return ENOENT;
    }

    ret = 0;

done:
    return ret;
}

int load_config(struct gp_config *cfg)
{
    dictionary *d;
    char *tmpstr;
    int ret;

    d = iniparser_load(cfg->config_file);
    if (!d) {
        return ENOENT;
    }

    tmpstr = iniparser_getstring(d, "gssproxy:debug", NULL);
    if (tmpstr) {
        if (strcasecmp(tmpstr, "1") == 0 ||
            strcasecmp(tmpstr, "on") == 0 ||
            strcasecmp(tmpstr, "true") == 0) {
            gp_debug_enable();
        }
    }

    tmpstr = iniparser_getstring(d, "gssproxy:socket", NULL);
    if (tmpstr) {
        cfg->socket_name = strdup(tmpstr);
        if (!cfg->socket_name) {
            ret = ENOMEM;
            goto done;
        }
    } else {
        ret = asprintf(&cfg->socket_name, "%s/%s",
                        PIPE_PATH, GP_SOCKET_NAME);
        if (ret == -1) {
            ret = ENOMEM;
            goto done;
        }
    }

    cfg->num_workers = iniparser_getint(d, "gssproxy:worker threads", 0);

    ret = load_services(cfg, d);

done:
    iniparser_freedict(d);
    return ret;
}

struct gp_config *read_config(char *config_file, int opt_daemonize)
{
    struct gp_config *cfg;
    int ret;

    cfg = calloc(1, sizeof(struct gp_config));
    if (!cfg) {
        return NULL;
    }

    if (config_file) {
        cfg->config_file = strdup(config_file);
        if (!cfg->config_file) {
            free(cfg);
            return NULL;
        }
    } else {
        ret = asprintf(&cfg->config_file, "%s/gssproxy.conf", PUBCONF_PATH);
        if (ret == -1) {
            free(cfg);
            return NULL;
        }
    }

    switch (opt_daemonize) {
    case 0:
        /* daemonize by default */
    case 1:
        cfg->daemonize = true;
        break;
    case 2:
        cfg->daemonize = false;
        break;
    }

    ret = load_config(cfg);
    if (ret) {
        syslog(LOG_INFO, "Config file not found! Proceeding with defaults.");
    }

    return cfg;
}