-
Notifications
You must be signed in to change notification settings - Fork 3
/
pam.c
executable file
·389 lines (346 loc) · 10.6 KB
/
pam.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
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Mikael Johansson <mikael AT synd DOT info> |
| Chad Cunningham |
+----------------------------------------------------------------------+
*/
/* $Id: pam.c 291416 2009-11-29 10:47:35Z mikl $ */
#include "php_pam.h"
static PHP_MINIT_FUNCTION(pam);
static PHP_MSHUTDOWN_FUNCTION(pam);
static PHP_MINFO_FUNCTION(pam);
static PHP_GINIT_FUNCTION(pam);
static PHP_FUNCTION(pam_auth);
static PHP_FUNCTION(pam_chpass);
/* {{{ globals */
ZEND_DECLARE_MODULE_GLOBALS(pam);
/* }}} */
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("pam.servicename", "php", PHP_INI_ALL, OnUpdateString, servicename, zend_pam_globals, pam_globals)
PHP_INI_END()
/* }}} */
#ifdef ZEND_BEGIN_ARG_INFO
ZEND_BEGIN_ARG_INFO_EX(arginfo_pam_auth, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 2)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, status)
ZEND_ARG_INFO(0, checkacctmgmt)
ZEND_END_ARG_INFO()
#else
#define arginfo_pam_parse third_arg_force_ref
#define arginfo_pam_parse_file third_arg_force_ref
#define arginfo_parm_parse_url third_arg_force_ref
#endif
/* {{{ pam_functions[]
*/
zend_function_entry pam_functions[] = {
PHP_FE(pam_auth, arginfo_pam_auth)
PHP_FE(pam_chpass, NULL)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ pam_module_entry
*/
zend_module_entry pam_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
NULL,
PHP_PAM_EXTENSION_NAME,
pam_functions,
PHP_MINIT(pam),
PHP_MSHUTDOWN(pam),
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
PHP_MINFO(pam),
PHP_PAM_EXTENSION_VERSION,
PHP_MODULE_GLOBALS(pam),
PHP_GINIT(pam),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
ZEND_GET_MODULE(pam)
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(pam)
{
REGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(pam)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(pam)
{
php_info_print_table_start();
php_info_print_table_header(2, "PAM support", "enabled");
php_info_print_table_row(2, "Extension version", PHP_PAM_EXTENSION_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* {{{ PHP_GINIT_FUNCTION() */
static PHP_GINIT_FUNCTION(pam)
{
pam_globals->servicename = NULL;
}
/* }}} */
/* {{{ php_pam_init_globals
*/
static void php_pam_init_globals(zend_pam_globals *pam_globals)
{
pam_globals->servicename = NULL;
}
/* }}} */
/*
* auth_pam_talker: supply authentication information to PAM when asked
*
* Assumptions:
* A password is asked for by requesting input without echoing
* A username is asked for by requesting input _with_ echoing
*
*/
static
int auth_pam_talker(int num_msg,
const struct pam_message ** msg,
struct pam_response ** resp,
void *appdata_ptr)
{
unsigned short i = 0;
pam_auth_t *userinfo = (pam_auth_t *) appdata_ptr;
struct pam_response *response = 0;
/* parameter sanity checking */
if (!resp || !msg || !userinfo)
return PAM_CONV_ERR;
/* allocate memory to store response */
response = malloc(num_msg * sizeof(struct pam_response));
if (!response)
return PAM_CONV_ERR;
/* copy values */
for (i = 0; i < num_msg; i++) {
/* initialize to safe values */
response[i].resp_retcode = 0;
response[i].resp = 0;
/* select response based on requested output style */
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_ON:
/* on memory allocation failure, auth fails */
response[i].resp = strdup(userinfo->name);
break;
case PAM_PROMPT_ECHO_OFF:
response[i].resp = strdup(userinfo->pw);
break;
default:
if (response)
free(response);
return PAM_CONV_ERR;
}
}
/* everything okay, set PAM response values */
*resp = response;
return PAM_SUCCESS;
}
/*
* chpass_pam_talker: supply authentication information to PAM when asked
*
* Assumptions:
* A password is asked for by requesting input without echoing
* A username is asked for by requesting input _with_ echoing
*
*/
static
int chpass_pam_talker(int num_msg,
const struct pam_message ** msg,
struct pam_response ** resp,
void *appdata_ptr)
{
unsigned short i = 0;
pam_chpass_t *userinfo = (pam_chpass_t *) appdata_ptr;
struct pam_response *response = 0;
/* parameter sanity checking */
if (!resp || !msg || !userinfo)
return PAM_CONV_ERR;
/* allocate memory to store response */
response = malloc(num_msg * sizeof(struct pam_response));
if (!response)
return PAM_CONV_ERR;
/* copy values */
for (i = 0; i < num_msg; i++) {
/* initialize to safe values */
response[i].resp_retcode = 0;
response[i].resp = 0;
/* select response based on requested output style */
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_ON:
/* on memory allocation failure, auth fails */
response[i].resp = strdup(userinfo->name);
break;
case PAM_PROMPT_ECHO_OFF:
if (userinfo->count++) {
response[i].resp = strdup(userinfo->newpw);
}
else {
response[i].resp = strdup(userinfo->oldpw);
}
break;
default:
if (response)
free(response);
return PAM_CONV_ERR;
}
}
/* everything okay, set PAM response values */
*resp = response;
return PAM_SUCCESS;
}
/* {{{ proto bool pam_auth( string host, string password [, string &status [ bool checkacctmgmt = true ] ])
Authenticates a user and returns TRUE on success, FALSE on failure */
PHP_FUNCTION(pam_auth)
{
zval *status = NULL;
zval *server;
zval *remote_addr;
zend_bool checkacctmgmt = 1;
int result;
int username_len = 0;
char *username = {0};
int password_len = 0;
char *password = {0};
pam_auth_t userinfo = {NULL, NULL};
struct pam_conv conv_info = {&auth_pam_talker, (void *) &userinfo};
pam_handle_t *pamh = NULL;
char *error_msg;
zend_string *hash_key;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|zb", &username, &username_len, &password, &password_len, &status, &checkacctmgmt) == FAILURE) {
return;
}
userinfo.name = username;
userinfo.pw = password;
if ((result = pam_start(PAM_G(servicename), userinfo.name, &conv_info, &pamh)) != PAM_SUCCESS) {
if (status) {
spprintf(&error_msg, 0, "%s (in %s)", (char *) pam_strerror(pamh, result), "pam_start");
zval_dtor(status);
ZVAL_STRING(status, error_msg);
efree(error_msg);
}
RETURN_FALSE;
}
hash_key = zend_string_init("_SERVER", sizeof ( "_SERVER"), 0);
if (zend_hash_exists(&EG(symbol_table), hash_key)) {
if ((server = zend_hash_str_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER")-1)) != NULL && Z_TYPE_P(server) == IS_ARRAY) {
if ((remote_addr = zend_hash_str_find(Z_ARRVAL_P(server), "REMOTE_ADDR", sizeof("REMOTE_ADDR")-1)) != NULL && Z_TYPE_P(remote_addr) == IS_STRING) {
pam_set_item(pamh, PAM_RHOST, Z_STRVAL_P(remote_addr));
}
}
}
if ((result = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) {
if (status) {
spprintf(&error_msg, 0, "%s (in %s)", (char *) pam_strerror(pamh, result), "pam_authenticate");
zval_dtor(status);
ZVAL_STRING(status, error_msg);
efree(error_msg);
}
pam_end(pamh, PAM_SUCCESS);
RETURN_FALSE;
}
if (checkacctmgmt) {
if ((result = pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) {
if (status) {
spprintf(&error_msg, 0, "%s (in %s)", (char *) pam_strerror(pamh, result), "pam_acct_mgmt");
zval_dtor(status);
ZVAL_STRING(status, error_msg);
efree(error_msg);
}
pam_end(pamh, PAM_SUCCESS);
RETURN_FALSE;
}
}
pam_end(pamh, PAM_SUCCESS);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool pam_auth( string host, string password [, string &status ])
Changes a users password and returns TRUE on success, FALSE on failure */
PHP_FUNCTION(pam_chpass)
{
zval *status = NULL;
int username_len = 0;
int oldpass_len = 0;
int newpass_len = 0;
char *username = {0};
char *oldpass = {0};
char *newpass = {0};
pam_chpass_t userinfo = {NULL, NULL, NULL, 0};
struct pam_conv conv_info = {&chpass_pam_talker, (void *) &userinfo};
pam_handle_t *pamh = NULL;
int result;
char *error_msg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|z", &username, &username_len, &oldpass, &oldpass_len, &newpass, &newpass_len, &status) == FAILURE) {
return;
}
userinfo.name = username;
userinfo.oldpw = oldpass;
userinfo.newpw = newpass;
if ((result = pam_start(PAM_G(servicename), userinfo.name, &conv_info, &pamh)) != PAM_SUCCESS) {
if (status) {
spprintf(&error_msg, 0, "%s (in %s)", (char *) pam_strerror(pamh, result), "pam_start");
zval_dtor(status);
ZVAL_STRING(status, error_msg);
efree(error_msg);
}
RETURN_FALSE;
}
if ((result = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) {
if (status) {
spprintf(&error_msg, 0, "%s (in %s)", (char *) pam_strerror(pamh, result), "pam_authenticate");
zval_dtor(status);
ZVAL_STRING(status, error_msg);
efree(error_msg);
}
pam_end(pamh, PAM_SUCCESS);
RETURN_FALSE;
}
if ((result = pam_chauthtok(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) {
if (status) {
spprintf(&error_msg, 0, "%s (in %s)", (char *) pam_strerror(pamh, result), "pam_chauthtok");
zval_dtor(status);
ZVAL_STRING(status, error_msg);
efree(error_msg);
}
pam_end(pamh, PAM_SUCCESS);
RETURN_FALSE;
}
pam_end(pamh, PAM_SUCCESS);
RETURN_TRUE;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/