File: lcmaps_return_account_from_pem.c

package info (click to toggle)
lcmaps 1.6.6-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,016 kB
  • ctags: 1,428
  • sloc: sh: 11,191; ansic: 9,519; makefile: 530; lex: 184; perl: 92; yacc: 60
file content (446 lines) | stat: -rw-r--r-- 15,971 bytes parent folder | download | duplicates (4)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
 * Copyright (c) Members of the EGEE Collaboration. 2004-2010.
 * See http://www.eu-egee.org/partners/ for details on the copyright
 * holders.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 *  Authors:
 *  2009-
 *     Oscar Koeroo <okoeroo@nikhef.nl>
 *     Mischa Sall\'e <msalle@nikhef.nl>
 *     David Groep <davidg@nikhef.nl>
 *     NIKHEF Amsterdam, the Netherlands
 *     <grid-mw-security@nikhef.nl>
 *
 *  2007-2009
 *     Oscar Koeroo <okoeroo@nikhef.nl>
 *     David Groep <davidg@nikhef.nl>
 *     NIKHEF Amsterdam, the Netherlands
 *
 *  2003-2007
 *     Martijn Steenbakkers <martijn@nikhef.nl>
 *     Gerben Venekamp <venekamp@nikhef.nl>
 *     Oscar Koeroo <okoeroo@nikhef.nl>
 *     David Groep <davidg@nikhef.nl>
 *     NIKHEF Amsterdam, the Netherlands
 *
 */


/*!
    \file   lcmaps_return_account_from_pem.c
    \brief  This interface returns the uid, gids and poolindex, i.e. leaseid,
            using LCMAPS and takes the user credential in PEM format as input
    \author Martijn Steenbakkers for the EU DataGrid.

    This interface returns the uid, gids and poolindex, i.e. leaseid, using LCMAPS.
    As input it requires the user credential in PEM format
    -# lcmaps_return_account_from_pem: interface function
*/

#define _XOPEN_SOURCE	600

/******************************************************************************
                             Include header files
******************************************************************************/
#include "lcmaps_config.h"

/* Needed for NULL */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>

/* LCMAPS includes */
#include "lcmaps.h"
#include "lcmaps_log.h"
#include "lcmaps_account.h"
#include "lcmaps_return_account_from_pem.h"

#include "pluginmanager/_lcmaps_utils.h"
#include "pluginmanager/_lcmaps_log.h"

/******************************************************************************
                             Module specific defines
******************************************************************************/
/* Default settings */
#define LCMAPS_MAX_POLICIES 10
#define LCMAPS_DEFAULT_DB_FILE "lcmaps.db"
#if defined(DONT_ALLOW_ABSOLUTE_LCMAPS_DB_FILE) ||\
    defined(CHECK_LCMAPS_DB_PERMISSIONS)
# define LCMAPS_REQUIRED_DB_PATH LCMAPS_ETC_HOME
#endif

/*
 * If the following is set, LCMAPS will not allow the environment variable
 * LCMAPS_DB_FILE to contain an absolute path.
 * For now I don't see cases were this is necessary, so unset it
 */
#define DONT_ALLOW_ABSOLUTE_LCMAPS_DB_FILE 0
/*
 * Also this should ideally happen in the calling application (glexec)
 * so unset it.
 */
#define CHECK_LCMAPS_DB_PERMISSIONS 0

/******************************************************************************
Function:   lcmaps_return_account_from_pem_va
Description:
    If LCMAPS is invoked via this interface it will return the user account info
    and poolindex, alternatively named leaseid, to the calling application.
    The arguments of this function are passed as a variable argument list.

Parameters:
    narg:  the number of arguments that follow
    ap:    vararg list, that consists of
        - input:  The PEM-encoded string containing the user proxy
        - output: a structure that contains account information:
                uid, gids (primary and secondary) and the poolindex
        - input, optional: the mapcounter
Returns:
    0: success
    1: failure
******************************************************************************/
static int lcmaps_return_account_from_pem_va(
    int        narg,
    ...
)
{
    va_list                             ap;
    char *                              pem_string = NULL;        /* input variable */
    int                                 mapcounter = -1;         /* input variable */
    lcmaps_account_info_t *             plcmaps_account = NULL;  /* output variable */
    char *                              lcmaps_policy_string = NULL;
    char *                              lcmaps_policies[LCMAPS_MAX_POLICIES];
    int                                 lcmaps_npols = LCMAPS_MAX_POLICIES;
    const char *                        lcmaps_db_file_env = NULL;
    char *                              lcmaps_db_file = NULL;
    int                                 rc = -1;
    uid_t                               uid = (uid_t)-1;
    gid_t *                             pgid_list = NULL;
    int                                 npgid = 0;
    gid_t *                             sgid_list = NULL;
    int                                 nsgid = 0;
    char *                              poolindex = NULL;
    int                                 jj = 0;

#if CHECK_LCMAPS_DB_PERMISSIONS
    struct stat                         prg_info;   /* program info holder   */
    struct stat                         dir_info;   /* directory info holder */
#endif

    /* First initialize LCMAPS, initialize without file pointer or name which
     * will result in getting env var LCMAPS_LOG_FILE. Specify DO_USRLOG to try
     * that first, if that fails we will go automatically to syslog */
    if (lcmaps_init_and_logfile(NULL, NULL, (unsigned short)(DO_USRLOG)))
    {
        lcmaps_log(LOG_ERR, "%s: LCMAPS initialization failure\n", __func__);
        goto return_account_from_pem_error;
    }

    /*
     * Set the lcmaps_db_file. If we're very paranoid we set
     * DONT_ALLOW_ABSOLUTE_LCMAPS_DB_FILE, but normally this is not needed,
     * because the calling application (glexec) should take care that the
     * environment variables are set correctly
     */
#if DONT_ALLOW_ABSOLUTE_LCMAPS_DB_FILE
    /*
     * "Another exception is made for the lcmaps.db file: This interface is used
     * by VO services (i.e. not site-controlled services), so the VO should not
     * be able to determine which lcmaps.db file (or in the future a general
     * lcmaps config file) they take."
     */
    if ( (lcmaps_db_file_env = getenv("LCMAPS_DB_FILE")) != NULL)
    {
        /*
         * OK, one tries to set the location of the lcmaps.db file.
	 * We don't allow paths to be included in the filename. LCMAPS will add
	 * the site-controlled path to it.
         * If a path is found: error
	 * If no path is found: add the default path to it and performs various
	 * checks
         */
        if (strchr(lcmaps_db_file_env, '/') != NULL)
        {
            lcmaps_log(LOG_ERR, "%s: (LCMAPS_DB_FILE = %s)\n", __func__, lcmaps_db_file_env);
            lcmaps_log(LOG_ERR,
                "%s: Attempt to include path to lcmaps.db file by environment variable\n", __func__);
            lcmaps_log(LOG_ERR, "%s: This is not allowed for this lcmaps interface\n", __func__);
            return 1;
        }
    }
    else
    {
        lcmaps_db_file_env = LCMAPS_DEFAULT_DB_FILE;
    }
    /* prepend required path */
    lcmaps_db_file = (char *) malloc(strlen(LCMAPS_REQUIRED_DB_PATH) + 2 + strlen(lcmaps_db_file_env));
    if (lcmaps_db_file==NULL)	{
	lcmaps_log(LOG_ERR, "%s: Out of memory\n", __func__);
	return 1;
    }
    sprintf(lcmaps_db_file, "%s/%s", LCMAPS_REQUIRED_DB_PATH, lcmaps_db_file_env);
#else /* DONT_ALLOW_ABSOLUTE_LCMAPS_DB_FILE */
    lcmaps_db_file_env = getenv("LCMAPS_DB_FILE");
    lcmaps_db_file_env = (lcmaps_db_file_env ? lcmaps_db_file_env : LCMAPS_DEFAULT_DB_FILE );
    lcmaps_db_file = strdup(lcmaps_db_file_env);
    if (lcmaps_db_file==NULL)	{
	lcmaps_log(LOG_ERR, "%s: Out of memory\n", __func__);
	return 1;
    }
#endif /* DONT_ALLOW_ABSOLUTE_LCMAPS_DB_FILE */

    /*
     * Check the lcmaps_db_file.
     * Again, if we're very paranoid we set CHECK_LCMAPS_DB_PERMISSIONS, but
     * normally this is not needed, because the calling application (glexec)
     * should do that.
     */
#if CHECK_LCMAPS_DB_PERMISSIONS
    /*
     * Check lcmaps.db file:
     * - Should be in a directory owned by root, which non-writable for others.
     * - Should be owned by root and non-writable for others.
     * If checks are ok reset environment variable
     */
    /*
     * Stat the directory of lcmaps.db and verify it is a directory, or error
     * out.
     */
    if (((lstat(LCMAPS_REQUIRED_DB_PATH, &dir_info)) != 0) || !(S_ISDIR(dir_info.st_mode)))
    {
        lcmaps_log(LOG_ERR, "%s: cannot stat directory: (%s)\n", __func__, LCMAPS_REQUIRED_DB_PATH);
        if (lcmaps_db_file) {free(lcmaps_db_file); lcmaps_db_file = NULL;}
        return 1;
    }
    /*
     * Error out if the directory is not owned by root
     */
    if ((dir_info.st_uid != 0) || (dir_info.st_gid != 0))
    {
        lcmaps_log(LOG_ERR, "%s: directory is not owned by root: (%s)\n", __func__, LCMAPS_REQUIRED_DB_PATH);
        if (lcmaps_db_file) {free(lcmaps_db_file); lcmaps_db_file = NULL;}
        return 1;
    }
    /*
     * Error out if the directory is writable by others.
     */
    if (dir_info.st_mode & S_IWOTH)
    {
        lcmaps_log(LOG_ERR, "%s: directory is writable by others: (%s)\n", __func__, LCMAPS_REQUIRED_DB_PATH);
        if (lcmaps_db_file) {free(lcmaps_db_file); lcmaps_db_file = NULL;}
        return 1;
    }
    /*
     * Error out if we cannot stat the lcmaps.db file.
     */
    if (((lstat(lcmaps_db_file, &prg_info)) != 0) || (S_ISLNK(prg_info.st_mode)))
    {
        lcmaps_log(LOG_ERR, "%s: cannot stat program: (%s)\n", __func__, lcmaps_db_file);
        if (lcmaps_db_file) {free(lcmaps_db_file); lcmaps_db_file = NULL;}
        return 1;
    }
    /*
     * Error out if the lcmaps.db file is not owned by root
     */
    if ((prg_info.st_uid != 0) || (prg_info.st_gid != 0))
    {
        lcmaps_log(LOG_ERR, "%s: file is not owned by root: (%s)\n", __func__, lcmaps_db_file);
        if (lcmaps_db_file) {free(lcmaps_db_file); lcmaps_db_file = NULL;}
        return 1;
    }
    /*
     * Error out if the lcmaps.db file is writable by others.
     */
    if (prg_info.st_mode & S_IWOTH)
    {
        lcmaps_log(LOG_ERR, "%s: file is writable by others: (%s)\n", __func__, lcmaps_db_file);
        if (lcmaps_db_file) {free(lcmaps_db_file); lcmaps_db_file = NULL;}
        return 1;
    }
#endif /* CHECK_LCMAPS_DB_PERMISSIONS*/

    /* Everything's fine, so set (overwrite!) the environment variable. */
    setenv("LCMAPS_DB_FILE", lcmaps_db_file, 1);
    if (lcmaps_db_file)
    {
        free(lcmaps_db_file);
        lcmaps_db_file = NULL;
    }

    /* Parse arguments from va_list */
    va_start(ap, narg);
    if (narg == 3)
    {
        pem_string = va_arg(ap, char *);
        mapcounter = va_arg(ap, int);
        plcmaps_account = va_arg(ap, lcmaps_account_info_t *);
    }
    else
    {
        lcmaps_log(LOG_ERR, "%s: The number of arguments (%d) should be 3\n", __func__, narg);
        return 1;
    }
    va_end(ap);

    /* Parse policy environment variable */
    for (jj=0; jj < LCMAPS_MAX_POLICIES; jj++) lcmaps_policies[jj] = 0;
    lcmaps_policy_string = getenv("LCMAPS_POLICY_NAME");
    if ((rc = lcmaps_tokenize(lcmaps_policy_string, lcmaps_policies, &lcmaps_npols, ":")) != 0)
    {
        lcmaps_log(LOG_ERR, "%s: Cannot parse LCMAPS_POLICY_NAME environment variable, because\n", __func__);
        switch (rc)
        {
            case -1:
                lcmaps_log(LOG_ERR, "%s: of a malloc error\n", __func__);
                break;
            case -2:
                lcmaps_log(LOG_ERR, "%s: the policy list is too large (max = %d)\n",
                    __func__, LCMAPS_MAX_POLICIES);
                break;
            case -3:
                lcmaps_log(LOG_ERR, "%s: of a non-matching quote\n", __func__);
                break;
            case -4:
                lcmaps_log(LOG_ERR, "%s: invalid input\n", __func__);
                break;
            default:
                lcmaps_log(LOG_ERR, "%s: of an unknown error\n", __func__);
                break;
        }
        goto return_account_from_pem_error;
    }

    /*
     * Now that we have the credential let's run (and terminate) LCMAPS !
     */
    rc = lcmaps_run_with_pem_and_return_account(
        NULL,
        pem_string,
        mapcounter,
        NULL,
        lcmaps_npols,
        lcmaps_policies,
        &uid,
        &pgid_list,
        &npgid,
        &sgid_list,
        &nsgid,
        &poolindex
    );
    if (rc != 0)
    {
        lcmaps_log (LOG_ERR, "LCMAPS failed to do mapping and return account information\n");
        if (lcmaps_term())
        {
            lcmaps_log (LOG_ERR, "LCMAPS termination failure\n");
            goto return_account_from_pem_error;
        }
        goto return_account_from_pem_error;
    }
    /*
     * Fill the lcmaps account information
     */
    rc = lcmaps_account_info_fill(
        &uid,
        &pgid_list,
        &npgid,
        &sgid_list,
        &nsgid,
        &poolindex,
        plcmaps_account
    );
    if (rc != 0)
    {
        lcmaps_log (LOG_ERR, "LCMAPS failed to do mapping and return account information\n");
        if (lcmaps_term())
        {
            lcmaps_log (LOG_ERR, "LCMAPS termination failure\n");
            goto return_account_from_pem_error;
        }
        goto return_account_from_pem_error;
    }
    /* Now we're done with the poolindex (has been dupped into plcmaps_account),
     * so free it */
    free(poolindex);

    rc = lcmaps_term();
    if (rc)
    {
        lcmaps_log (LOG_ERR, "LCMAPS termination failure\n");
        goto return_account_from_pem_error;
    }

    /* Clean policies */
    for (jj=0; jj < LCMAPS_MAX_POLICIES; jj++)
    {
        if ((lcmaps_policies[jj]) != NULL)
        {
            free(lcmaps_policies[jj]);
            lcmaps_policies[jj] = NULL;
        }
    }

    return 0;

 return_account_from_pem_error:
    /* Clean policies */
    for (jj=0; jj < LCMAPS_MAX_POLICIES; jj++)
    {
        if ((lcmaps_policies[jj]) != NULL)
        {
            free(lcmaps_policies[jj]);
            lcmaps_policies[jj] = NULL;
        }
    }
    return 1;
}

/******************************************************************************
Function:   lcmaps_return_account_from_pem
Description:
    If LCMAPS is invoked via this interface it will return the user account info
    and poolindex, alternatively named leaseid, to the calling application.

Parameters:
    mapcounter:    :  The counter which will be added to the poolindex, effectively enabling
                      multiple account mappings
    pem_string:    :  The PEM-encoded string containing the user proxy
    plcmaps_account:  A structure that contains account information: (output)
                      uid, gids (primary and secondary) and the poolindex
                      Please use lcmaps_account_info_clean() to clean this
                      structure after use.
Returns:
    0: success
    1: failure
******************************************************************************/
int lcmaps_return_account_from_pem(
        char * pem_string,
        int mapcounter,
        lcmaps_account_info_t * plcmaps_account
)
{
    return (lcmaps_return_account_from_pem_va(3, pem_string, mapcounter, plcmaps_account));
}

/******************************************************************************
CVS Information:
    $Source: /srv/home/dennisvd/svn/mw-security/lcmaps/src/lcmaps_return_account_from_pem.c,v $
    $Date: 2014-03-04 16:17:33 +0100 (Tue, 04 Mar 2014) $
    $Revision: 17597 $
    $Author: msalle $
******************************************************************************/