summaryrefslogtreecommitdiff
path: root/ext/vpopmail/php_vpopmail.c
blob: 5e9e029f5ea4f16d67c722bdf19773288b0ec142 (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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999, 2000, 2001 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          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: David Croft <david@infotrek.co.uk>                          |
   |          Boian Bonev <boian@bonev.com>                               |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

// TODO: move to config.m4 when support for old versions is ready or just don't support rather old vpopmail
// current version must bail out if incompat option is specified and work for minimal params
//#undef VPOPMAIL_IS_REALLY_OLD

#include "php.h"
#include "php_ini.h"
#include "php_vpopmail.h"

#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#if HAVE_VPOPMAIL

#include "vpopmail.h"

#include "ext/standard/exec.h"
#include "ext/standard/php_string.h"

// keep this as the last include in order to destroy VERSION/PACKAGE only for the rest of the code
#undef VERSION
#undef PACKAGE
#include "vpopmail_config.h"
#undef PACKAGE
#include "vpopmail.h"

// vpopmail does not export this, argh!
#define MAX_BUFF 500

#define VPOPSTR(x) #x

/* removed by intent - will add when need to
ZEND_DECLARE_MODULE_GLOBALS(vpop)
*/

/* Function table */

function_entry vpopmail_functions[] = {
	// domain management
	PHP_FE(vpopmail_adddomain, NULL)
	PHP_FE(vpopmail_deldomain, NULL)
	PHP_FE(vpopmail_addaliasdomain,	NULL)
	// user management
	PHP_FE(vpopmail_adduser, NULL)
	PHP_FE(vpopmail_deluser, NULL)
	PHP_FE(vpopmail_passwd, NULL)
	PHP_FE(vpopmail_setuserquota, NULL)
	PHP_FE(vpopmail_auth_user, NULL)
	{NULL, NULL, NULL}
};

zend_module_entry vpopmail_module_entry = {
	"vpopmail",
	vpopmail_functions,
	NULL, //PHP_MINIT(vpopmail),		// do nothing upon
	NULL, //PHP_MSHUTDOWN(vpopmail),	// 	these
	NULL, //PHP_RINIT(vpopmail),		// 	events
	PHP_RSHUTDOWN(vpopmail),		// close vpopmail lib upon request shutdown
	PHP_MINFO(vpopmail),
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_VPOPMAIL
ZEND_GET_MODULE(vpopmail)
#endif

// removed by intent - will add when needed
//PHP_INI_BEGIN()
/*	STD_PHP_INI_ENTRY("pfpro.proxypassword",		"",				PHP_INI_ALL, OnUpdateString,	proxypassword,			php_pfpro_globals,	pfpro_globals) */
//PHP_INI_END()


// removed by intent - will add when needed
/*PHP_MINIT_FUNCTION(vpopmail) {
	REGISTER_INI_ENTRIES();
	return SUCCESS;
}*/

// removed by intent - will add when needed
/*PHP_MSHUTDOWN_FUNCTION(vpopmail)
{
	UNREGISTER_INI_ENTRIES();
	return SUCCESS;
}*/

/* at request end we close the connection to any databases open by the lib */
PHP_RSHUTDOWN_FUNCTION(vpopmail) {
	vclose();
	return SUCCESS;
}

PHP_MINFO_FUNCTION(vpopmail) {
	char g[64],u[64];

	sprintf(g,"%d",VPOPMAILGID);
	sprintf(u,"%d",VPOPMAILUID);
	php_info_print_table_start();
	php_info_print_table_row(2, "vpopmail support", "enabled");
	php_info_print_table_row(2, "vpopmail version", VERSION);
	php_info_print_table_row(2, "vpopmail uid", u);
	php_info_print_table_row(2, "vpopmail gid", g);
	php_info_print_table_row(2, "vpopmail dir", VPOPMAILDIR);
	php_info_print_table_row(2, "vpopmail vadddomain", VPOPMAIL_BIN_DIR VPOPMAIL_ADDD);
	php_info_print_table_row(2, "vpopmail vdeldomain", VPOPMAIL_BIN_DIR VPOPMAIL_DELD);
	php_info_print_table_row(2, "vpopmail vaddaliasdomain", VPOPMAIL_BIN_DIR VPOPMAIL_ADAD);
	php_info_print_table_end();

	// will add when needed
	//DISPLAY_INI_ENTRIES();
}

/*
 * shall not copy/paste this stuff but rather make _Exec from ext/standard/exec.c public as php_exec or something
 */
static int vpopmail_exec(char *cmd) {
	FILE *fp;
	int t, l, ret, output=1;
	PLS_FETCH();

#ifdef PHP_WIN32
	fp = V_POPEN(cmd, "rb");
#else
	fp = V_POPEN(cmd, "r");
#endif
	if (!fp) {
		php_error(E_WARNING, "Unable to fork [%s]", cmd);
		return -1;
	}
	ret = pclose(fp);
#if HAVE_SYS_WAIT_H
	if (WIFEXITED(ret)) {
		ret = WEXITSTATUS(ret);
	}
#endif
	return ret;
}

//////////////////////////////////////////////////////////////////////////
// domain management functions (these are slow - implementation executes external binary

/* {{{ proto int vpopmail_adddomain(string domain, string passwd [, string quota [, string bounce [, bool apop]]])
   Add a new virtual domain */
PHP_FUNCTION(vpopmail_adddomain) {	// this code cannot work if not suid/sgid
	zval **domain, **passwd, **quota, **bounce, **apop;
	int retval,len=0,argc=ZEND_NUM_ARGS(),Uid=VPOPMAILUID,Gid=VPOPMAILGID,is_bounce_email;
	int fr_bounce=0,fr_quota=0;
	char *q,*bo,*cmd,*escdomain="",*escpasswd="",*escquota="",*escbounce="",*escapop="";

	if (argc < 2 || argc > 5 || zend_get_parameters_ex(argc, &domain, &passwd, &quota, &bounce, &apop) == FAILURE){
		WRONG_PARAM_COUNT;
	}

	switch (argc) {
		case 5:
			convert_to_long_ex(apop);
			escapop=Z_BVAL_PP(apop)?"1":"0";
			/* Fall-through. */
		case 4:
			fr_bounce=1;
			convert_to_string_ex(bounce);
			escbounce=php_escape_shell_cmd(Z_STRVAL_PP(bounce));
			if (!escbounce) {
				php_error(E_WARNING,"vpopmail_adddomain error: cannot alloc");
				RETURN_LONG(-1);
			}
			/* Fall-through. */
		case 3:
			fr_quota=1;
			convert_to_string_ex(quota);
			escquota=php_escape_shell_cmd(Z_STRVAL_PP(quota));
			if (!escquota) {
				php_error(E_WARNING,"vpopmail_adddomain error: cannot alloc");
				RETURN_LONG(-1);
			}
			/* Fall-through. */
		case 2:
			convert_to_string_ex(passwd);
			convert_to_string_ex(domain);
			break;
	}

	escdomain=php_escape_shell_cmd(Z_STRVAL_PP(domain));
	escpasswd=php_escape_shell_cmd(Z_STRVAL_PP(passwd));
	if (!escdomain||!escpasswd) {
		if (fr_quota)
			efree(escquota);
		if (fr_bounce)
			efree(escbounce);
		php_error(E_WARNING,"vpopmail_adddomain error: cannot alloc");
		RETURN_LONG(-1);
	}
	len+=strlen(VPOPMAIL_BIN_DIR);
	len+=strlen(VPOPMAIL_ADDD);
	if (*escquota)
		len+=strlen("-q ")+strlen(escquota)+strlen(" ");
	if (*escbounce) {
		if (strchr(Z_STRVAL_PP(bounce),'@')) {
			is_bounce_email=1;
			len+=strlen("-e ")+strlen(escbounce)+strlen(" ");
		} else {
			is_bounce_email=0;
			len+=strlen("-b ");
		}
	}
	if (*escapop)
		len+=strlen("-a ");
	len+=strlen(escdomain)+strlen(" ");
	len+=strlen(escpasswd)+strlen(" ");
	len++;
	cmd=emalloc(len);
	if (!cmd) {
		if (fr_quota)
			efree(escquota);
		if (fr_bounce)
			efree(escbounce);
		efree(escdomain);
		efree(escpasswd);
		php_error(E_WARNING,"vpopmail_adddomain error: cannot alloc");
		RETURN_LONG(-1);
	}
	strcpy(cmd,VPOPMAIL_BIN_DIR VPOPMAIL_ADDD);
	if (*escquota) {
		strcat(cmd,"-q ");
		strcat(cmd,escquota);
		strcat(cmd," ");
	}
	if (*escbounce) {
		if (is_bounce_email) {
			strcat(cmd,"-e ");
			strcat(cmd,escbounce);
			strcat(cmd," ");
		} else {
			strcat(cmd,"-b ");
		}
	}
	if (*escapop)
		strcat(cmd,"-a ");
	strcat(cmd,escdomain);
	strcat(cmd," ");
	strcat(cmd,escpasswd);
	retval=vpopmail_exec(cmd);
	efree(cmd);
	efree(escdomain);
	efree(escpasswd);
	if (fr_quota)
		efree(escquota);
	if (fr_bounce)
		efree(escbounce);

	if (retval!=VA_SUCCESS)
		php_error(E_WARNING,"vpopmail_adddomain error: %d", retval);

	RETURN_LONG(retval);
}
/* }}} */

/* {{{ proto int vpopmail_deldomain(string domain)
   Delete a virtual domain */
PHP_FUNCTION(vpopmail_deldomain) {
	zval **domain;
	int retval=-1;
	char *cmd,*escdomain;

	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &domain) == FAILURE){
		WRONG_PARAM_COUNT;
	}

	convert_to_string_ex(domain);

	escdomain=php_escape_shell_cmd(Z_STRVAL_PP(domain));
	if (!escdomain) {
		php_error(E_WARNING,"vpopmail_deldomain error: cannot alloc");
		RETURN_LONG(-1);
	}
	cmd=emalloc(strlen(VPOPMAIL_BIN_DIR)+strlen(VPOPMAIL_DELD)+strlen(escdomain)+1);
	if (!cmd) {
		efree(escdomain);
		php_error(E_WARNING,"vpopmail_deldomain error: cannot alloc");
		RETURN_LONG(-1);
	}
	sprintf(cmd,VPOPMAIL_BIN_DIR VPOPMAIL_DELD"%s",escdomain);
	retval=vpopmail_exec(cmd);
	efree(escdomain);
	efree(cmd);

	if (retval!=VA_SUCCESS)
		php_error(E_WARNING,"vpopmail_deldomain error: %d", retval);

	RETURN_LONG(retval);
}
/* }}} */

/* {{{ proto int vpopmail_addaliasdomain(string olddomain, string newdomain)
   Add alias to an existing virtual domain */
PHP_FUNCTION(vpopmail_addaliasdomain) {
	zval **olddomain, **newdomain;
	int retval;
	char *cmd,*escolddomain,*escnewdomain;

	if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &olddomain, &newdomain) == FAILURE){
		WRONG_PARAM_COUNT;
	}

	convert_to_string_ex(olddomain);
	convert_to_string_ex(newdomain);
	escnewdomain=php_escape_shell_cmd(Z_STRVAL_PP(newdomain));
	if (!escnewdomain) {
		php_error(E_WARNING,"vpopmail_addaliasdomain error: cannot alloc");
		RETURN_LONG(-1);
	}
	escolddomain=php_escape_shell_cmd(Z_STRVAL_PP(olddomain));
	if (!escolddomain) {
		efree(escnewdomain);
		php_error(E_WARNING,"vpopmail_addaliasdomain error: cannot alloc");
		RETURN_LONG(-1);
	}

	cmd=emalloc(strlen(VPOPMAIL_BIN_DIR VPOPMAIL_ADAD)+strlen(escolddomain)+strlen(" ")+strlen(escnewdomain)+1);
	if (!cmd) {
		efree(escnewdomain);
		efree(escolddomain);
		php_error(E_WARNING,"vpopmail_addaliasdomain error: cannot alloc");
		RETURN_LONG(-1);
	}
	sprintf(cmd,"%s%s %s",VPOPMAIL_BIN_DIR VPOPMAIL_ADAD,escolddomain,escnewdomain);
	retval=vpopmail_exec(cmd);
	efree(cmd);
	efree(escnewdomain);
	efree(escolddomain);

	if (retval!=VA_SUCCESS)
		php_error(E_WARNING,"vpopmail_addaliasdomain error: %d", retval);

	RETURN_LONG(retval);
}
/* }}} */

//////////////////////////////////////////////////////////////////////////
// user management functions - native implementation - fast

/* {{{ proto int vpopmail_adduser(string username, string domain, string password [, string quota [, string gecos [, int apop]]])
   Add a new user to the specified virtual domain */
PHP_FUNCTION(vpopmail_adduser) {
	zval **username, **domain, **password, **quota, **gecos, **apop;
	int argc;
	int retval,hv_apop=0,hv_gecos=0,hv_quota=0;

	argc = ZEND_NUM_ARGS();
	if (argc < 3 || argc > 6 || zend_get_parameters_ex(argc, &username, &domain, &password, &quota, &gecos, &apop) == FAILURE){
		WRONG_PARAM_COUNT;
	}

	switch (argc) {
		case 6:
			hv_apop=1;
			convert_to_long_ex(apop);
			/* Fall-through. */
		case 5:
			hv_gecos=1;
			convert_to_string_ex(gecos);
			/* Fall-through. */
		case 4:
			hv_quota=1;
			convert_to_string_ex(quota);
			/* Fall-through. */
		case 3:
			convert_to_string_ex(password);
			convert_to_string_ex(domain);
			convert_to_string_ex(username);
			break;
	}

	retval=vadduser(Z_STRVAL_PP(username),Z_STRVAL_PP(domain),Z_STRVAL_PP(password),hv_gecos?Z_STRVAL_PP(gecos):Z_STRVAL_PP(username),hv_apop?Z_LVAL_PP(apop):0);
	if (retval!=VA_SUCCESS) {
        	php_error(E_WARNING, "vpopmail_adduser error: %s", verror(retval));
		RETURN_LONG(retval);
	}
	if (hv_quota)
		retval=vsetuserquota(Z_STRVAL_PP(username),Z_STRVAL_PP(domain),Z_STRVAL_PP(quota));
	if (retval != VA_SUCCESS)
        	php_error(E_WARNING, "vpopmail_adduser error: %s", verror(retval));
	RETURN_LONG(VA_SUCCESS);
}
/* }}} */

/* {{{ proto int vpopmail_deluser(string username, string domain)
   Delete a user from a virtual domain */
PHP_FUNCTION(vpopmail_deluser) {
	zval **user;
	zval **domain;
	int retval;

	if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &user, &domain) == FAILURE)
		WRONG_PARAM_COUNT;

	convert_to_string_ex(user);
	convert_to_string_ex(domain);

	retval = vdeluser(Z_STRVAL_PP(user), Z_STRVAL_PP(domain));

	if (retval != VA_SUCCESS)
        	php_error(E_WARNING, "vpopmail_deluser error: %s", verror(retval));
	RETURN_LONG(retval);
}
/* }}} */

/* {{{ proto int vpopmail_passwd(string username, string domain, string password[, int apop])
   Change a virtual user's password */
PHP_FUNCTION(vpopmail_passwd) {
	zval **username, **domain, **password, **apop;
	int argc,retval,is_apop=0;

	argc=ZEND_NUM_ARGS();
	if (argc < 3 || argc > 4 || zend_get_parameters_ex(argc, &username, &domain, &password, &apop) == FAILURE)
		WRONG_PARAM_COUNT;
	if (argc==4) {
		convert_to_long_ex(apop);
		is_apop=(Z_BVAL_PP(apop) ? 1 : 0);
	}

	convert_to_string_ex(username);
	convert_to_string_ex(domain);
	convert_to_string_ex(password);

	retval=vpasswd(Z_STRVAL_PP(username),Z_STRVAL_PP(domain),Z_STRVAL_PP(password),is_apop);
	if (retval != VA_SUCCESS)
	        php_error(E_WARNING, "vpopmail_passwd error: %s", verror(retval));
	RETURN_LONG(retval);
}
/* }}} */

/* {{{ proto int vpopmail_setuserquota(string username, string domain, string quota)
   Sets a virtual user's quota */
PHP_FUNCTION(vpopmail_setuserquota) {
	zval **user;
	zval **domain;
	zval **quota;
	int retval;

	if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &user, &domain, &quota) == FAILURE)
		WRONG_PARAM_COUNT;

	convert_to_string_ex(user);
	convert_to_string_ex(domain);
	convert_to_string_ex(quota);

	retval = vsetuserquota(Z_STRVAL_PP(user), Z_STRVAL_PP(domain), Z_STRVAL_PP(quota));
	if (retval != VA_SUCCESS)
	        php_error(E_WARNING, "vpopmail_setuserquota error: %s", verror(retval));
	RETURN_LONG(retval);
}
/* }}} */

/* {{{ proto bool vpopmail_auth_user(string user, string domain, string password [, int apop])
   Attempt to validate a username/domain/password. Returns true/false */
PHP_FUNCTION(vpopmail_auth_user) {
	zval **user;
	zval **domain;
	zval **password;
	zval **apop;
	struct passwd *retval;
	int argc=ZEND_NUM_ARGS();
	int hv_apop=0;

	if (argc < 3 || argc > 4 || zend_get_parameters_ex(argc, &user, &domain, &password, &apop) == FAILURE)
		WRONG_PARAM_COUNT;

	convert_to_string_ex(user);
	convert_to_string_ex(domain);
	convert_to_string_ex(password);

	if (argc==4) {
		convert_to_string_ex(apop);
		hv_apop=1;
	}

	// warning: retval is a ref to a static member; this is safe - will not leak
	retval = vauth_user(Z_STRVAL_PP(user), Z_STRVAL_PP(domain), Z_STRVAL_PP(password), hv_apop?Z_STRVAL_PP(apop):"");

	if (retval == NULL) {
		RETURN_FALSE;
	} else {
		RETURN_TRUE;
	}
}
/* }}} */


#endif	/* HAVE_VPOPMAIL */


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */