summaryrefslogtreecommitdiffstats
path: root/tests/wrap-pam.c
blob: dc922870ad60ccb1e47b0d0ae820acdcbb471a6b (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
/*
 * Copyright 2013 Red Hat, Inc.
 *
 * This Program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This Program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this Program; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307 USA
 *
 */
#define _GNU_SOURCE

#include <sys/types.h>
#include <dlfcn.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <security/pam_appl.h>

static const struct {
	int value;
	const char *name;
} pam_errors[] = {
	{PAM_SUCCESS, "SUCCESS"},
	{PAM_SUCCESS, "0"},
	{PAM_OPEN_ERR, "OPEN_ERR"},
	{PAM_SYMBOL_ERR, "SYMBOL_ERR"},
	{PAM_SERVICE_ERR, "SERVICE_ERR"},
	{PAM_SYSTEM_ERR, "SYSTEM_ERR"},
	{PAM_BUF_ERR, "BUF_ERR"},
	{PAM_PERM_DENIED, "PERM_DENIED"},
	{PAM_AUTH_ERR, "AUTH_ERR"},
	{PAM_CRED_INSUFFICIENT, "CRED_INSUFFICIENT"},
	{PAM_AUTHINFO_UNAVAIL, "AUTHINFO_UNAVAIL"},
	{PAM_USER_UNKNOWN, "USER_UNKNOWN"},
	{PAM_MAXTRIES, "MAXTRIES"},
	{PAM_NEW_AUTHTOK_REQD, "NEW_AUTHTOK_REQD"},
	{PAM_ACCT_EXPIRED, "ACCT_EXPIRED"},
	{PAM_SESSION_ERR, "SESSION_ERR"},
	{PAM_CRED_UNAVAIL, "CRED_UNAVAIL"},
	{PAM_CRED_EXPIRED, "CRED_EXPIRED"},
	{PAM_CRED_ERR, "CRED_ERR"},
	{PAM_NO_MODULE_DATA, "NO_MODULE_DATA"},
	{PAM_CONV_ERR, "CONV_ERR"},
	{PAM_AUTHTOK_ERR, "AUTHTOK_ERR"},
	{PAM_AUTHTOK_RECOVERY_ERR, "AUTHTOK_RECOVERY_ERR"},
	{PAM_AUTHTOK_LOCK_BUSY, "AUTHTOK_LOCK_BUSY"},
	{PAM_AUTHTOK_DISABLE_AGING, "AUTHTOK_DISABLE_AGING"},
	{PAM_TRY_AGAIN, "TRY_AGAIN"},
	{PAM_IGNORE, "IGNORE"},
	{PAM_ABORT, "ABORT"},
	{PAM_AUTHTOK_EXPIRED, "AUTHTOK_EXPIRED"},
	{PAM_MODULE_UNKNOWN, "UNKNOWN"},
	{PAM_BAD_ITEM, "BAD_ITEM"},
	{PAM_CONV_AGAIN, "CONV_AGAIN"},
	{PAM_INCOMPLETE, "INCOMPLETE"},
};

typedef struct pam_handle {
	char *authtok, errbuf[LINE_MAX];
	struct pam_conv conv;
	int auth, acct;
} pam_handle_t;

static int
pam_numerror(const char *name)
{
	unsigned int i, l;

	for (i = 0; i < sizeof(pam_errors) / sizeof(pam_errors[0]); i++) {
		l = strlen(pam_errors[i].name);
		if (strncasecmp(pam_errors[i].name, name, l) == 0) {
			return pam_errors[i].value;
		}
	}
	return -1;
}

const char *
pam_strerror(pam_handle_t *pamh, int errnum)
{
	unsigned int i;

	for (i = 0; i < sizeof(pam_errors) / sizeof(pam_errors[0]); i++) {
		if (pam_errors[i].value == errnum) {
			return pam_errors[i].name;
		}
	}
	snprintf(pamh->errbuf, sizeof(pamh->errbuf), "PAM error %d", errnum);
	return pamh->errbuf;
}

int
pam_start(const char *service_name, const char *user,
	  const struct pam_conv *pam_conversation, pam_handle_t **pamh)
{
	FILE *fp;
	char buf[LINE_MAX], *p, *q;
	pam_handle_t *ret;

	ret = calloc(1, sizeof(*ret));
	if (ret == NULL) {
		return PAM_BUF_ERR;
	}
	ret->conv = *pam_conversation;
	if (getenv("WRAPPERS_PAM_CREDS") == NULL) {
		return PAM_ABORT;
	}
	fp = fopen(getenv("WRAPPERS_PAM_CREDS"), "r");
	if (fp == NULL) {
		free(ret);
		return PAM_ABORT;
	}
	while (fgets(buf, sizeof(buf), fp) != NULL) {
		buf[strcspn(buf, "\r\n")] = '\0';
		if ((strlen(buf) > strlen(user)) &&
		    (strncmp(user, buf, strlen(user)) == 0) &&
		    (buf[strlen(user)] == ':')) {
			p = buf + strcspn(buf, ":");
			if (*p != '\0') {
				p++;
				q = p + strcspn(p, ":");
				ret->authtok = strndup(p, q - p);
				p = q;
			}
			if (*p != '\0') {
				p++;
				q = p + strcspn(p, ":");
				ret->auth = pam_numerror(p);
				p = q;
			}
			if (*p != '\0') {
				p++;
				q = p + strcspn(p, ":");
				ret->acct = pam_numerror(p);
				p = q;
			}
			break;
		}
	}
	fclose(fp);
	*pamh = ret;
	return PAM_SUCCESS;
}

int
pam_end(pam_handle_t *pamh, int pam_status)
{
	if (pamh == NULL) {
		return PAM_SYSTEM_ERR;
	}
	free(pamh->authtok);
	free(pamh);
	return PAM_SUCCESS;
}

int
pam_authenticate(pam_handle_t *pamh, int flags)
{
	struct pam_response *resp;
	struct pam_message messages[] = {
		{.msg_style = PAM_PROMPT_ECHO_OFF, .msg = "Password: "},
	};
	const struct pam_message *msgs = &messages[0];
	int ret;

	resp = NULL;
	if (pamh == NULL) {
		return PAM_SYSTEM_ERR;
	}
	if (pamh->authtok == NULL) {
		return pamh->auth ? pamh->auth : PAM_USER_UNKNOWN;
	}
	if (pamh->conv.conv == NULL) {
		return PAM_CONV_ERR;
	}
	ret = pamh->conv.conv(1, &msgs, &resp, pamh->conv.appdata_ptr);
	if (ret != PAM_SUCCESS) {
		return ret;
	}
	if (strcmp(pamh->authtok, resp->resp) == 0) {
		ret = pamh->auth;
	} else {
		ret = PAM_AUTH_ERR;
	}
	free(resp->resp);
	free(resp);
	return ret;
}

int
pam_acct_mgmt(pam_handle_t *pamh, int flags)
{
	if (pamh == NULL) {
		return PAM_SYSTEM_ERR;
	}
	return pamh->acct;
}