summaryrefslogtreecommitdiffstats
path: root/libpam/pam_env.c
blob: 1c8403d6f6f19d8627402b1b4e0b6040fa0dcdbf (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
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
/*
 * pam_env.c
 *
 * Copyright (c) Andrew G. Morgan <morgan@parc.power.net> 1996,1997
 * All rights reserved.
 *
 * This file was written from a "hint" provided by the people at SUN.
 * and the X/Open XSSO draft of March 1997.
 *
 * $Id$
 */

#include "pam_private.h"

#include <string.h>
#include <stdlib.h>

#ifdef sunos
#define memmove(x,y,z) bcopy(y,x,z)
#endif

/* helper functions */

#ifdef PAM_DEBUG
static void _pam_dump_env(pam_handle_t *pamh)
{
    int i;

    D(("Listing environment of pamh=%p", pamh));
    D(("pamh->env = %p", pamh->env));
    D(("environment entries used = %d [of %d allocated]"
       , pamh->env->requested, pamh->env->entries));

    for (i=0; i<pamh->env->requested; ++i) {
	_pam_output_debug(">%-3d [%9p]:[%s]"
			  , i, pamh->env->list[i], pamh->env->list[i]);
    }
    _pam_output_debug("*NOTE* the last item should be (nil)");
}
#else
#define _pam_dump_env(x)
#endif

/*
 * Create the environment
 */

int _pam_make_env(pam_handle_t *pamh)
{
    D(("called."));

    IF_NO_PAMH("_pam_make_env", pamh, PAM_ABORT);

    /*
     * get structure memory
     */

    pamh->env = (struct pam_environ *) malloc(sizeof(struct pam_environ));
    if (pamh->env == NULL) {
	pam_syslog(pamh, LOG_CRIT, "_pam_make_env: out of memory");
	return PAM_BUF_ERR;
    }

    /*
     * get list memory
     */

    pamh->env->list = (char **)calloc( PAM_ENV_CHUNK, sizeof(char *) );
    if (pamh->env->list == NULL) {
	pam_syslog(pamh, LOG_CRIT, "_pam_make_env: no memory for list");
	_pam_drop(pamh->env);
	return PAM_BUF_ERR;
    }

    /*
     * fill entries in pamh->env
     */

    pamh->env->entries = PAM_ENV_CHUNK;
    pamh->env->requested = 1;
    pamh->env->list[0] = NULL;

    _pam_dump_env(pamh);                    /* only active when debugging */

    return PAM_SUCCESS;
}

/*
 * purge the environment
 */

void _pam_drop_env(pam_handle_t *pamh)
{
    D(("called."));
    IF_NO_PAMH("_pam_make_env", pamh, /* nothing to return */);

    if (pamh->env != NULL) {
	int i;
	/* we will only purge the pamh->env->requested number of elements */

	for (i=pamh->env->requested-1; i-- > 0; ) {
	    D(("dropping #%3d>%s<", i, pamh->env->list[i]));
	    _pam_overwrite(pamh->env->list[i]);          /* clean */
	    _pam_drop(pamh->env->list[i]);              /* forget */
	}
	pamh->env->requested = 0;
	pamh->env->entries = 0;
	_pam_drop(pamh->env->list);                     /* forget */
	_pam_drop(pamh->env);                           /* forget */
    } else {
	D(("no environment present in pamh?"));
    }
}

/*
 * Return the item number of the given variable = first 'length' chars
 * of 'name_value'. Since this is a static function, it is safe to
 * assume its supplied arguments are well defined.
 */

static int _pam_search_env(const struct pam_environ *env
			   , const char *name_value, int length)
{
    int i;

    for (i=env->requested-1; i-- > 0; ) {
	if (strncmp(name_value,env->list[i],length) == 0
	    && env->list[i][length] == '=') {

	    return i;                                   /* Got it! */

	}
    }

    return -1;                                          /* no luck */
}

/*
 * externally visible functions
 */

/*
 *  pam_putenv(): Add/replace/delete a PAM-environment variable.
 *
 *  Add/replace:
 *      name_value = "NAME=VALUE" or "NAME=" (for empty value="\0")
 *
 *  delete:
 *      name_value = "NAME"
 */

int pam_putenv(pam_handle_t *pamh, const char *name_value)
{
    int l2eq, item, retval;

    D(("called."));
    IF_NO_PAMH("pam_putenv", pamh, PAM_ABORT);

    if (name_value == NULL) {
	pam_syslog(pamh, LOG_ERR, "pam_putenv: no variable indicated");
	return PAM_PERM_DENIED;
    }

    /*
     * establish if we are setting or deleting; scan for '='
     */

    for (l2eq=0; name_value[l2eq] && name_value[l2eq] != '='; ++l2eq);
    if (l2eq <= 0) {
	pam_syslog(pamh, LOG_ERR, "pam_putenv: bad variable");
	return PAM_BAD_ITEM;
    }

    /*
     *  Look first for environment.
     */

    if (pamh->env == NULL || pamh->env->list == NULL) {
	pam_syslog(pamh, LOG_ERR, "pam_putenv: no env%s found",
		       pamh->env == NULL ? "":"-list");
	return PAM_ABORT;
    }

    /* find the item to replace */

    item = _pam_search_env(pamh->env, name_value, l2eq);

    if (name_value[l2eq]) {                     /* (re)setting */

	if (item == -1) {                      /* new variable */
	    D(("adding item: %s", name_value));
	    /* enough space? */
	    if (pamh->env->entries <= pamh->env->requested) {
		register int i;
		register char **tmp;

		/* get some new space */
		tmp = calloc( pamh->env->entries + PAM_ENV_CHUNK
				     , sizeof(char *) );
		if (tmp == NULL) {
		    /* nothing has changed - old env intact */
		    pam_syslog(pamh, LOG_CRIT,
			       "pam_putenv: cannot grow environment");
		    return PAM_BUF_ERR;
		}

		/* copy old env-item pointers/forget old */
		for (i=0; i<pamh->env->requested; ++i) {
		    tmp[i] = pamh->env->list[i];
		    pamh->env->list[i] = NULL;
		}

		/* drop old list and replace with new */
		_pam_drop(pamh->env->list);
		pamh->env->list = tmp;
		pamh->env->entries += PAM_ENV_CHUNK;

		D(("resized env list"));
		_pam_dump_env(pamh);              /* only when debugging */
	    }

	    item = pamh->env->requested-1;        /* old last item (NULL) */

	    /* add a new NULL entry at end; increase counter */
	    pamh->env->list[pamh->env->requested++] = NULL;

	} else {                                /* replace old */
	    D(("replacing item: %s\n          with: %s"
	       , pamh->env->list[item], name_value));
	    _pam_overwrite(pamh->env->list[item]);
	    _pam_drop(pamh->env->list[item]);
	}

	/*
	 * now we have a place to put the new env-item, insert at 'item'
	 */

	pamh->env->list[item] = _pam_strdup(name_value);
	if (pamh->env->list[item] != NULL) {
	    _pam_dump_env(pamh);                   /* only when debugging */
	    return PAM_SUCCESS;
	}

	/* something went wrong; we should delete the item - fall through */

	retval = PAM_BUF_ERR;                        /* an error occurred */
    } else {
	retval = PAM_SUCCESS;                      /* we requested delete */
    }

    /* getting to here implies we are deleting an item */

    if (item < 0) {
	pam_syslog(pamh, LOG_ERR,
		   "pam_putenv: delete non-existent entry; %s", name_value);
	return PAM_BAD_ITEM;
    }

    /*
     * remove item: purge memory; reset counter; resize [; display-env]
     */

    D(("deleting: env#%3d:[%s]", item, pamh->env->list[item]));
    _pam_overwrite(pamh->env->list[item]);
    _pam_drop(pamh->env->list[item]);
    --(pamh->env->requested);
    D(("mmove: item[%d]+%d -> item[%d]"
       , item+1, ( pamh->env->requested - item ), item));
    (void) memmove(&pamh->env->list[item], &pamh->env->list[item+1]
		   , ( pamh->env->requested - item )*sizeof(char *) );

    _pam_dump_env(pamh);                   /* only when debugging */

    /*
     * deleted.
     */

    return retval;
}

/*
 *  Return the value of the requested environment variable
 */

const char *pam_getenv(pam_handle_t *pamh, const char *name)
{
    int item;

    D(("called."));
    IF_NO_PAMH("pam_getenv", pamh, NULL);

    if (name == NULL) {
	pam_syslog(pamh, LOG_ERR, "pam_getenv: no variable indicated");
	return NULL;
    }

    if (pamh->env == NULL || pamh->env->list == NULL) {
	pam_syslog(pamh, LOG_ERR, "pam_getenv: no env%s found",
		   pamh->env == NULL ? "":"-list" );
	return NULL;
    }

    /* find the requested item */

    item = _pam_search_env(pamh->env, name, strlen(name));
    if (item != -1) {

	D(("env-item: %s, found!", name));
	return (pamh->env->list[item] + 1 + strlen(name));

    } else {

	D(("env-item: %s, not found", name));
	return NULL;

    }
}

static char **_copy_env(pam_handle_t *pamh)
{
    char **dump;
    int i = pamh->env->requested;          /* reckon size of environment */
    char *const *env = pamh->env->list;

    D(("now get some memory for dump"));

    /* allocate some memory for this (plus the null tail-pointer) */
    dump = (char **) calloc(i, sizeof(char *));
    D(("dump = %p", dump));
    if (dump == NULL) {
	return NULL;
    }

    /* now run through entries and copy the variables over */
    dump[--i] = NULL;
    while (i-- > 0) {
	D(("env[%d]=`%s'", i,env[i]));
	dump[i] = _pam_strdup(env[i]);
	D(("->dump[%d]=`%s'", i,dump[i]));
	if (dump[i] == NULL) {
	    /* out of memory */

	    while (dump[++i]) {
		_pam_overwrite(dump[i]);
		_pam_drop(dump[i]);
	    }
	    _pam_drop(dump);
	    return NULL;
	}
    }

    env = NULL;                             /* forget now */

    /* return transcribed environment */
    return dump;
}

char **pam_getenvlist(pam_handle_t *pamh)
{
    int i;

    D(("called."));
    IF_NO_PAMH("pam_getenvlist", pamh, NULL);

    if (pamh->env == NULL || pamh->env->list == NULL) {
	pam_syslog(pamh, LOG_ERR, "pam_getenvlist: no env%s found",
		   pamh->env == NULL ? "":"-list" );
	return NULL;
    }

    /* some quick checks */

    if (pamh->env->requested > pamh->env->entries) {
	pam_syslog(pamh, LOG_ERR, "pam_getenvlist: environment corruption");
	_pam_dump_env(pamh);                 /* only active when debugging */
	return NULL;
    }

    for (i=pamh->env->requested-1; i-- > 0; ) {
	if (pamh->env->list[i] == NULL) {
	    pam_syslog(pamh, LOG_ERR, "pam_getenvlist: environment broken");
	    _pam_dump_env(pamh);              /* only active when debugging */
	    return NULL;          /* somehow we've broken the environment!? */
	}
    }

    /* Seems fine; copy environment */

    _pam_dump_env(pamh);                    /* only active when debugging */

    return _copy_env(pamh);
}