/
pam_http.c
315 lines (253 loc) · 8.51 KB
/
pam_http.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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <curl/curl.h>
#include <libconfig.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#ifndef PAM_EXTERN
#define PAM_EXTERN
#endif
#define BUFSIZE 1024
#define DEFAULT_USER "nobody"
#define DEFAULT_METHOD "GET"
#define DEFAULT_USERNAME_FIELD "username"
#define DEFAULT_PASSWORD_FIELD "password"
#define DEFAULT_HTTP_SUCCESS_CODE 200
#define DEFAULT_CURL_TIMEOUT 10
static const char* HTTP_METHOD_POST = "POST";
static const char* HTTP_METHOD_GET = "GET";
static char password_prompt[] = "Password:";
struct config_auth {
const char* c_auth_url;
const char* c_method;
const char* c_username_field;
const char* c_password_field;
long long timeout;
long long success_code;
};
static void pam_http_syslog(int priority, const char* format, ...)
{
va_list args;
va_start(args, format);
openlog("pam_http", LOG_CONS | LOG_PID, LOG_AUTH);
vsyslog(priority, format, args);
closelog();
va_end(args);
}
static char* config_get_string(config_t* cfg, const char* name, const char* def_value)
{
const char* value;
if (!config_lookup_string(cfg, name, &value))
value = def_value;
return strndup(value, BUFSIZE);
}
static int read_config_auth(char config_file[BUFSIZE], struct config_auth* s_auth)
{
config_t cfg;
const char* buf;
config_init(&cfg);
if (!config_read_file(&cfg, config_file)) {
pam_http_syslog(LOG_ALERT, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return 0;
}
if (!config_lookup_string(&cfg, "auth_url", &buf)) {
pam_http_syslog(LOG_ALERT, "No 'auth_url' setting in configuration file.");
config_destroy(&cfg);
return 0;
}
s_auth->c_auth_url = strndup(buf, BUFSIZE);
s_auth->c_method = config_get_string(&cfg, "auth_method", DEFAULT_METHOD);
if (strncmp(s_auth->c_method, HTTP_METHOD_GET, 3) != 0 && strncmp(s_auth->c_method, HTTP_METHOD_POST, 4) != 0) {
pam_http_syslog(LOG_ALERT, "Wrong 'auth_method' setting in configuration file (%s).", s_auth->c_method);
config_destroy(&cfg);
return 0;
}
s_auth->c_username_field = config_get_string(&cfg, "auth_username_field", DEFAULT_USERNAME_FIELD);
s_auth->c_password_field = config_get_string(&cfg, "auth_password_field", DEFAULT_PASSWORD_FIELD);
if (!config_lookup_int64(&cfg, "auth_timeout", &s_auth->timeout)) {
s_auth->timeout = DEFAULT_CURL_TIMEOUT;
}
if (!config_lookup_int64(&cfg, "auth_success_code", &s_auth->success_code)) {
s_auth->success_code = DEFAULT_HTTP_SUCCESS_CODE;
}
config_destroy(&cfg);
return 1;
}
static int pam_http_get_user(pam_handle_t* pamh, const char** user)
{
int retval;
retval = pam_get_user(pamh, user, NULL);
if (retval != PAM_SUCCESS) {
pam_http_syslog(LOG_ALERT, "pam_get_user returns an error: %s", pam_strerror(pamh, retval));
return retval;
}
if (*user == NULL || **user == '\0') {
pam_http_syslog(LOG_ALERT, "undefined user");
retval = pam_set_item(pamh, PAM_USER, (const void*)DEFAULT_USER);
if (retval != PAM_SUCCESS)
return PAM_USER_UNKNOWN;
}
return retval;
}
static int pam_http_get_password(pam_handle_t* pamh, char** password)
{
int retval, retry;
struct pam_conv* conv;
struct pam_message msg;
const struct pam_message* msgp;
struct pam_response* resp;
retval = pam_get_item(pamh, PAM_CONV, (const void**)&conv);
if (retval != PAM_SUCCESS)
return PAM_SYSTEM_ERR;
msg.msg_style = PAM_PROMPT_ECHO_OFF;
msg.msg = password_prompt;
msgp = &msg;
for (retry = 0; retry < 3; ++retry) {
resp = NULL;
retval = (*conv->conv)(1, &msgp, &resp, conv->appdata_ptr);
if (resp != NULL) {
if (retval == PAM_SUCCESS)
*password = resp->resp;
else
free(resp->resp);
free(resp);
}
if (retval == PAM_SUCCESS)
break;
}
if (retval == PAM_CONV_ERR)
return retval;
if (retval != PAM_SUCCESS)
return PAM_AUTH_ERR;
return PAM_SUCCESS;
}
static int pam_http_request(struct config_auth* s_auth, const char* user, const char* password)
{
int retval;
CURL* curl;
CURLcode res;
long http_code;
char auth_url[BUFSIZE];
char post_fields[BUFSIZE];
const char *has_parm, *sep;
char *esc_user, *esc_password;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
esc_user = curl_easy_escape(curl, user, 0);
esc_password = curl_easy_escape(curl, password, 0);
if (!esc_user || !esc_password) {
pam_http_syslog(LOG_ALERT, "could not escape credentials");
retval = PAM_CRED_ERR;
} else {
if (!strncmp(s_auth->c_method, HTTP_METHOD_GET, 3)) {
has_parm = strstr(s_auth->c_auth_url, "?");
sep = (has_parm == NULL) ? "?" : "&";
snprintf(auth_url, BUFSIZE, "%s%s%s=%s&%s=%s", s_auth->c_auth_url, sep,
s_auth->c_username_field, esc_user, s_auth->c_password_field, esc_password);
} else {
strncpy(auth_url, s_auth->c_auth_url, BUFSIZE);
snprintf(post_fields, BUFSIZE, "%s=%s&%s=%s", s_auth->c_username_field,
esc_user, s_auth->c_password_field, esc_password);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_fields);
}
curl_easy_setopt(curl, CURLOPT_URL, auth_url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, s_auth->timeout);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
pam_http_syslog(LOG_ALERT, "could not perform http request: %s %s: %s",
s_auth->c_method, auth_url, curl_easy_strerror(res));
retval = PAM_SERVICE_ERR;
} else {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
retval = http_code == s_auth->success_code ? PAM_SUCCESS : PAM_AUTH_ERR;
}
curl_free(esc_user);
curl_free(esc_password);
}
curl_easy_cleanup(curl);
} else {
pam_http_syslog(LOG_ALERT, "could not init curl");
retval = PAM_SYSTEM_ERR;
}
curl_global_cleanup();
return retval;
}
static int pam_http_authenticate(pam_handle_t* pamh, int argc, const char** argv)
{
int retval, i;
char config_file[BUFSIZE];
struct config_auth s_auth;
const char* user = NULL;
char* password;
for (i = 0; i < argc; i++) {
if (!strncmp(argv[i], "conf=", 5)) {
if (argv[i] + 5)
strncpy(config_file, argv[i] + 5, BUFSIZE - 2);
}
}
if (strlen(config_file) <= 1) {
pam_http_syslog(LOG_ALERT, "conf directive is not set");
return PAM_SYSTEM_ERR;
}
retval = read_config_auth(config_file, &s_auth);
if (!retval)
return PAM_SYSTEM_ERR;
if ((retval = pam_http_get_user(pamh, &user)) != PAM_SUCCESS)
return retval;
if ((retval = pam_http_get_password(pamh, &password)) != PAM_SUCCESS)
return retval;
retval = pam_http_request(&s_auth, user, password);
return retval;
}
PAM_EXTERN
int pam_sm_authenticate(pam_handle_t* pamh, int flags, int argc, const char** argv)
{
return pam_http_authenticate(pamh, argc, argv);
}
PAM_EXTERN
int pam_sm_setcred(pam_handle_t* pamh, int flags, int argc, const char** argv)
{
return PAM_SUCCESS;
}
PAM_EXTERN
int pam_sm_acct_mgmt(pam_handle_t* pamh, int flags, int argc, const char** argv)
{
return PAM_SUCCESS;
}
PAM_EXTERN
int pam_sm_chauthtok(pam_handle_t* pamh, int flags, int argc, const char** argv)
{
return PAM_SUCCESS;
}
PAM_EXTERN
int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc,
const char** argv)
{
return PAM_SUCCESS;
}
PAM_EXTERN
int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc,
const char** argv)
{
return PAM_SUCCESS;
}
#ifdef PAM_STATIC
struct pam_module _pam_http_modstruct = {
"pam_http",
pam_sm_authenticate,
pam_sm_setcred,
pam_sm_acct_mgmt,
pam_sm_open_session,
pam_sm_close_session,
pam_sm_chauthtok
};
#endif