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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/*
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 <errno.h>
#include "gp_proxy.h"
#include "gp_config.h"
static void gp_service_free(struct gp_service *svc)
{
free(svc->name);
if (svc->mechs & GP_CRED_KRB5) {
free(svc->krb5.principal);
free(svc->krb5.keytab);
free(svc->krb5.ccache);
}
gp_free_creds_handle(&svc->creds_handle);
memset(svc, 0, sizeof(struct gp_service));
}
static bool option_is_set(const char *s)
{
if (strcasecmp(s, "1") == 0 ||
strcasecmp(s, "on") == 0 ||
strcasecmp(s, "true") == 0 ||
strcasecmp(s, "yes") == 0) {
return true;
}
return false;
}
static int get_krb5_mech_cfg(struct gp_service *svc,
struct gp_ini_context *ctx,
const char *secname)
{
const char *value;
value = gp_config_get_string(ctx, secname, "krb5_principal");
if (value) {
svc->krb5.principal = strdup(value);
if (!svc->krb5.principal) {
return ENOMEM;
}
}
value = gp_config_get_string(ctx, secname, "krb5_keytab");
if (value) {
svc->krb5.keytab = strdup(value);
if (!svc->krb5.keytab) {
return ENOMEM;
}
}
value = gp_config_get_string(ctx, secname, "krb5_ccache");
if (value) {
svc->krb5.ccache = strdup(value);
if (!svc->krb5.ccache) {
return ENOMEM;
}
}
return 0;
}
static int setup_service_creds_handle(struct gp_service *svc)
{
uint32_t ret_maj, ret_min;
ret_maj = gp_init_creds_handle(&ret_min, &svc->creds_handle);
if (ret_maj) {
return ret_min;
}
return 0;
}
static int load_services(struct gp_config *cfg, struct gp_ini_context *ctx)
{
int num_sec;
char *secname = NULL;
char *value;
char *token;
char *handle;
int valnum;
int ret;
int i, n;
num_sec = gp_config_get_nsec(ctx);
/* allocate enough space for num_sec services,
* we won't waste too much space by overallocating */
cfg->svcs = calloc(num_sec, sizeof(struct gp_service *));
if (!cfg->svcs) {
ret = ENOMEM;
goto done;
}
for (i = 0; i < num_sec; i++) {
secname = gp_config_get_secname(ctx, i);
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++;
cfg->svcs[n]->name = strdup(secname + 8);
if (!cfg->svcs[n]->name) {
ret = ENOMEM;
goto done;
}
valnum = gp_config_get_int(ctx, secname, "euid");
if (valnum == -1) {
/* malformed section, mech is missing */
GPDEBUG("Euid missing from [%s], ignoring.\n", secname);
gp_service_free(cfg->svcs[n]);
cfg->num_svcs--;
free(secname);
continue;
}
cfg->svcs[n]->euid = valnum;
value = gp_config_get_string(ctx, secname, "trusted");
if (value != NULL) {
if (option_is_set(value)) {
cfg->svcs[n]->trusted = true;
}
}
value = gp_config_get_string(ctx, secname, "kernel_nfsd");
if (value != NULL) {
if (option_is_set(value)) {
cfg->svcs[n]->kernel_nfsd = true;
}
}
ret = setup_service_creds_handle(cfg->svcs[n]);
if (ret) {
goto done;
}
value = gp_config_get_string(ctx, secname, "mechs");
if (value == NULL) {
/* malformed section, mech is missing */
GPDEBUG("Mechs missing from [%s], ignoring.\n", secname);
gp_service_free(cfg->svcs[n]);
cfg->num_svcs--;
free(secname);
continue;
}
token = strtok_r(value, ", ", &handle);
do {
ret = strcmp(value, "krb5");
if (ret == 0) {
ret = get_krb5_mech_cfg(cfg->svcs[n], ctx, secname);
if (ret == 0) {
cfg->svcs[n]->mechs |= GP_CRED_KRB5;
} else {
GPDEBUG("Failed to read krb5 config for %s, ignoring.\n",
secname);
}
} else {
GPDEBUG("Unknown mech: %s in [%s], ignoring.\n",
token, secname);
}
token = strtok_r(NULL, ", ", &handle);
} while (token != NULL);
if (cfg->svcs[n]->mechs == 0) {
GPDEBUG("No mechs found for [%s], ignoring.\n", secname);
gp_service_free(cfg->svcs[n]);
cfg->num_svcs--;
free(secname);
continue;
}
free(secname);
secname = NULL;
}
}
if (cfg->num_svcs == 0) {
GPERROR("No service sections configured!\n");
return ENOENT;
}
ret = 0;
done:
free(secname);
return ret;
}
static int gp_init_ini_context(const char *config_file,
struct gp_ini_context **ctxp)
{
struct gp_ini_context *ctx;
int ret;
if (!ctxp) {
return EINVAL;
}
ctx = calloc(1, sizeof(struct gp_ini_context));
if (!ctx) {
return ENOENT;
}
ret = gp_config_init(config_file, ctx);
if (ret) {
return ret;
}
*ctxp = ctx;
return 0;
}
int load_config(struct gp_config *cfg)
{
struct gp_ini_context *ctx;
char *tmpstr;
int ret;
ret = gp_init_ini_context(cfg->config_file, &ctx);
if (ret) {
return ret;
}
tmpstr = gp_config_get_string(ctx, "gssproxy", "debug");
if (tmpstr) {
if (option_is_set(tmpstr)) {
gp_debug_enable();
}
}
cfg->num_workers = gp_config_get_int(ctx, "gssproxy", "worker threads");
ret = load_services(cfg, ctx);
gp_config_close(ctx);
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;
}
}
cfg->socket_name = strdup(GP_SOCKET_NAME);
if (cfg->socket_name == NULL) {
ret = ENOMEM;
goto done;
}
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) {
GPDEBUG("Config file not found! Proceeding with defaults.\n");
}
done:
if (ret) {
free_config(&cfg);
}
return cfg;
}
struct gp_creds_handle *gp_service_get_creds_handle(struct gp_service *svc)
{
return svc->creds_handle;
}
void free_config(struct gp_config **cfg)
{
struct gp_config *config = *cfg;
uint32_t i;
if (!config) {
return;
}
free(config->config_file);
free(config->socket_name);
for (i=0; i < config->num_svcs; i++) {
gp_service_free(config->svcs[i]);
}
free(config->svcs);
free(config);
*cfg = NULL;
}
#ifdef HAVE_INIPARSER
#define WITH_INIPARSER 1
#endif
#ifdef WITH_INIPARSER
#include "gp_config_iniparser.h"
int gp_config_init(const char *config_file,
struct gp_ini_context *ctx)
{
return gp_iniparser_init(config_file, ctx);
}
char *gp_config_get_string(struct gp_ini_context *ctx,
const char *secname,
const char *keyname)
{
return gp_iniparser_get_string(ctx, secname, keyname);
}
int gp_config_get_int(struct gp_ini_context *ctx,
const char *secname,
const char *keyname)
{
return gp_iniparser_get_int(ctx, secname, keyname);
}
int gp_config_get_nsec(struct gp_ini_context *ctx)
{
return gp_iniparser_get_nsec(ctx);
}
char *gp_config_get_secname(struct gp_ini_context *ctx,
int i)
{
return gp_iniparser_get_secname(ctx, i);
}
int gp_config_close(struct gp_ini_context *ctx)
{
return gp_iniparser_close(ctx);
}
#endif /* WITH_INIPARSER */
|