-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpam_af.c
449 lines (395 loc) · 11.3 KB
/
pam_af.c
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
447
448
449
/*-
* Copyright (c) 2004-2005 Stanislav Sedov <[email protected]>
* Copyright (c) 2005 MBSD labs
* Copyright (c) 2005 by 310.ru [Tridesyatoe], Moscow, Russian Federation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: pam_af.c,v 1.24 2006/11/07 00:05:53 stas Exp $
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#if !defined(__FreeBSD__) || (__FreeBSD_version >= 500001)
# include <stdint.h>
#endif
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <syslog.h>
#include <assert.h>
#include <ndbm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/file.h>
#define PAM_SM_AUTH
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#if defined(OPENPAM) || defined(_OPENPAM)
# include <security/openpam.h>
# include <security/pam_mod_misc.h>
#endif
#include "pam_af.h"
#include "subr.h"
/* For err()-like routines */
const char *progname = "pam_af";
/* Local prototypes */
static char ** pam_af_build_env __P((pam_handle_t *pamh));
static void pam_af_free_env __P((char **env));
static const char * pam_af_get_option __P((int optc, const char *optv[], \
const char *opt0));
/* Local defines */
#define ENV_ITEM(item) {(item), #item} /* Enviropment vars to set */
static struct {
int item;
const char *name;
} env_items[] = {
ENV_ITEM(PAM_SERVICE),
ENV_ITEM(PAM_TTY),
ENV_ITEM(PAM_USER),
ENV_ITEM(PAM_RUSER),
ENV_ITEM(PAM_RHOST)
};
#define NITEMS (sizeof(env_items) / sizeof(*env_items))
static const char *
pam_af_get_option(optc, optv, opt0)
int optc;
const char *optv[];
const char *opt0;
{
const char *opt;
int len;
ASSERT(opt0);
len = strlen(opt0);
while (optc--) {
opt = optv[optc];
if (strncmp(opt, opt0, len) == 0) {
if (opt[len] == '=')
len++;
return &opt[len];
}
}
return NULL;
}
static void
pam_af_free_env(env)
char **env;
{
uint i;
for(i = 0; env[i] != NULL; i++)
free(env[i]);
free(env);
}
/*
* The purpose of this routine is to set-up enviropment for external
* program's execution. This enviropment consists of PAM enviropment
* and items, defined in env_items.
*/
static char **
pam_af_build_env(pamh)
pam_handle_t *pamh;
{
int ret;
int items;
unsigned int i;
char **env, **tmp;
char *envstr;
void *item;
ASSERT(pamh)
env = pam_getenvlist(pamh);
ASSERT(env)
for (items = 0; env[items] != NULL; items++);
tmp = realloc(env, (items + NITEMS + 1) * sizeof(*env));
if (tmp == NULL) {
PAM_AF_LOGERR("malloc(%ld): %s\n",
(long)(items * sizeof(*env)),
strerror(errno));
pam_af_free_env(env);
return NULL;
}
env = tmp;
for (i = 0; i < NITEMS; i++) {
#ifndef _SUN_PAM_
ret = pam_get_item(pamh, env_items[i].item,
(const void **)&item);
#else /* _SUN_PAM_ */
ret = pam_get_item(pamh, env_items[i].item,
(void **)&item);
#endif /* _SUN_PAM_ */
if (ret != PAM_SUCCESS || item == NULL) {
PAM_AF_LOG("can't get %s item\n", env_items[i].name);
continue;
}
// asprintf(&envstr, "%s=%s", env_items[i].name, (char *)item);
envstr = (char *)malloc(strlen(env_items[i].name) + strlen((char *)item) + 2);
if (envstr == NULL) {
/* Maybe we'll be more lucky on next loop */
PAM_AF_LOGERR("can't allocate memory: %s\n", \
strerror(errno));
continue;
}
sprintf(envstr, "%s=%s", env_items[i].name, (char *)item);
env[items++] = envstr;
env[items] = NULL;
}
return env;
}
PAM_EXTERN int
pam_sm_authenticate(pamh, flags, argc, argv)
pam_handle_t *pamh;
int flags __unused;
int argc;
const char *argv[];
{
void *host;
DBM *stdbp;
const char *cfgdb = CFGDB, *stdb = STATDB;
datum key, data;
hostrec_t hstr;
hostrule_t *hostent;
time_t curtime;
int ret, pam_ret = PAM_SUCCESS;
int pam_err_ret = PAM_AUTH_ERR;/* Result in case of err. */
const char *tmp;
char **env;
int update_when_locked = 0; /* Update host stats when it's locked */
#ifdef _USE_SYSLOG_
openlog("pam_af", 0, LOG_AUTHPRIV);
#endif
/* Get runtime configuration */
if (pam_af_get_option(argc, argv, "allow_on_error") != NULL)
pam_err_ret = PAM_SUCCESS;
if (pam_af_get_option(argc, argv, "update_locked") != NULL)
update_when_locked = 1;
if ((tmp = pam_af_get_option(argc, argv, "statdb")) != NULL)
stdb = tmp;
if ((tmp = pam_af_get_option(argc, argv, "cfgdb")) != NULL)
cfgdb = tmp;
/* Known hostname is mandatory */
#ifndef _SUN_PAM_
ret = pam_get_item(pamh, PAM_RHOST, (const void **)&host);
#else /* _SUN_PAM_ */
ret = pam_get_item(pamh, PAM_RHOST, (void **)&host);
#endif /* _SUN_PAM_ */
if (host == NULL)
host = (void *)strdup("localhost"); /* Map local logins to
"localhost"
*/
if (ret != PAM_SUCCESS) {
PAM_AF_LOGERR("can't get '%s' item\n", "PAM_RHOST");
PAM_RETURN(pam_err_ret);
}
PAM_AF_LOG("processing host '%s'\n", (char *)host);
/* Fetch rule for host */
hostent = find_host_rule(cfgdb, (char *)host);
ASSERT(hostent)
/* Open statistics database and obtain exclusive lock */
#ifdef O_EXLOCK
stdbp = dbm_open(stdb, O_RDWR | O_CREAT | O_EXLOCK, STATDB_PERM);
#else
stdbp = dbm_open(stdb, O_RDWR | O_CREAT, STATDB_PERM);
#endif
if (stdbp == NULL) {
/*
* We need this because of PAM subsystem executes
* this routines under user's credentials and we don't want
* flood in system log.
*/
if (getuid() == 0) {
PAM_AF_LOGERR("can't open '%s' database: %s\n", \
stdb, strerror(errno));
PAM_RETURN(pam_err_ret);
}
else
PAM_RETURN(PAM_SUCCESS);
}
#ifndef O_EXLOCK
/* If we can't obtain lock through open(2) */
if (flock(dbm_pagfno(stdbp), LOCK_EX) != 0) {
PAM_AF_LOGERR("can't obtain exclusive lock on %s: %s\n",
stdb, strerror(errno));
dbm_close(stdbp);
PAM_RETURN(pam_err_ret);
}
#endif
key.dptr = (char *)host;
key.dsize = strlen((char *)host) + 1;
curtime = time(NULL);
data = dbm_fetch(stdbp, key);
if (data.dptr == NULL) {
/* Not found */
PAM_AF_LOG("host record not found in statistics database\n");
hstr.num = 0;
hstr.locked_for = 0;
}
else {
PAM_AF_LOG("found host record in statistics database\n");
if (data.dsize != sizeof(hstr)) {
PAM_AF_LOGERR("database '%s' seriously broken\n", stdb);
dbm_close(stdbp);
PAM_RETURN(pam_err_ret);
}
bcopy(data.dptr, &hstr, sizeof(hstr));
}
/* Reject host, if locktime interval wasn't passed */
if (hstr.locked_for != 0 && \
(unsigned)(curtime - hstr.last_attempt) <= hstr.locked_for) {
PAM_AF_LOG("rejecting host '%s', its blocked for %ld since" \
" %ld\n", (char *)host, hstr.locked_for, \
(long)hstr.last_attempt);
pam_ret = PAM_AUTH_ERR;
if (update_when_locked == 0) {
/* Fast rejection */
dbm_close(stdbp);
PAM_RETURN(pam_ret);
}
}
/*
* Build enviropment, includind PAM_RHOST, PAM_RUSER, PAM_USER,
* PAM_TTY, PAM_SERVICE and other values. Then they could be used
* by external commands to do host or user-specific work.
*/
if ((env = pam_af_build_env(pamh)) == NULL) {
PAM_AF_LOGERR("can't build env list\n");
}
/* Unlock host, if it was not rejected yet */
if (hstr.locked_for != 0 && pam_ret != PAM_AUTH_ERR) {
PAM_AF_LOG("unlocking host '%s' due the locktime has been " \
"passed\n", (char *)host);
hstr.num = 0;
hstr.locked_for = 0;
pam_ret = PAM_SUCCESS;
/* Execute unlocking cmd, if needed */
if (strlen(hostent->unlock_cmd) > 0) {
(void)exec_cmd(hostent->unlock_cmd, env);
}
}
/* Account current attempt too */
hstr.last_attempt = curtime;
hstr.num++;
/* Lock host, if needed */
if (hstr.num > hostent->attempts) {
PAM_AF_LOG("blocking host '%s'\n", (char *)host);
hstr.locked_for = hostent->locktime;
pam_ret = PAM_AUTH_ERR;
if (strlen(hostent->lock_cmd) > 0) {
(void)exec_cmd(hostent->lock_cmd, env);
}
}
/* Save recent statistics */
data.dptr = (char *)&hstr;
data.dsize = sizeof(hstr);
ret = dbm_store(stdbp, key, data, DBM_REPLACE);
if (ret != 0)
PAM_AF_LOGERR("can't update record: %s\n", strerror(ret));
dbm_close(stdbp);
pam_af_free_env(env);
PAM_RETURN(pam_ret);
}
PAM_EXTERN int
pam_sm_setcred(pamh, flags, argc, argv)
pam_handle_t *pamh;
int flags __unused;
int argc;
const char *argv[];
{
void *host;
const char *stdb = STATDB;
DBM *stdbp;
datum key, data;
hostrec_t hstr;
const char *tmp;
int ret;
int pam_err_ret = PAM_SERVICE_ERR;/* Default ret value */
#ifdef _USE_SYSLOG_
openlog("pam_af", 0, LOG_AUTHPRIV);
#endif
if (pam_af_get_option(argc, argv, "allow_on_error") != NULL)
pam_err_ret = PAM_SUCCESS;
if ((tmp = pam_af_get_option(argc, argv, "statdb")) != NULL)
stdb = tmp;
/* Get peer host */
#ifndef _SUN_PAM_
ret = pam_get_item(pamh, PAM_RHOST, (const void **)&host);
#else /* _SUN_PAM_ */
ret = pam_get_item(pamh, PAM_RHOST, (void **)&host);
#endif /* _SUN_PAM_ */
if (ret != PAM_SUCCESS) {
PAM_AF_LOGERR("can't get '%s' item\n", "PAM_RHOST");
PAM_RETURN(pam_err_ret);
}
if (host == NULL)
host = (void *)strdup("localhost"); /* Map local logins to
"localhost"
*/
/* Open statistics database */
#ifdef O_EXLOCK
stdbp = dbm_open(stdb, O_RDWR | O_CREAT | O_EXLOCK, STATDB_PERM);
#else
stdbp = dbm_open(stdb, O_RDWR | O_CREAT, STATDB_PERM);
#endif
if (stdbp == NULL) {
/*
* We need this because of PAM subsystem executes
* this routines under user's credentials and we don't want
* flood in system log.
*/
if (getuid() == 0) {
PAM_AF_LOGERR("can't open '%s' database: %s\n", \
stdb, strerror(errno));
PAM_RETURN(pam_err_ret);
}
else
PAM_RETURN(PAM_SUCCESS);
}
#ifndef O_EXLOCK
/* If we can't obtain lock through open(2) */
if (flock(dbm_pagfno(stdbp), LOCK_EX) != 0) {
PAM_AF_LOGERR("can't obtain exclusive lock on %s: %s\n",
stdb, strerror(errno));
dbm_close(stdbp);
PAM_RETURN(pam_err_ret);
}
#endif
/* Update records */
hstr.num = 0;
hstr.locked_for = 0;
hstr.last_attempt = time(NULL);
data.dptr = (char *)&hstr;
data.dsize = sizeof(hstr);
key.dptr = host;
key.dsize = strlen(host) + 1;
ret = dbm_store(stdbp, key, data, DBM_REPLACE);
if (ret != 0)
PAM_AF_LOGERR("can't update record: %s\n", \
strerror(ret));
dbm_close(stdbp);
PAM_RETURN(PAM_SUCCESS);
}
#ifdef _USE_MODULE_ENTRY_
PAM_MODULE_ENTRY("pam_af");
#endif