summaryrefslogtreecommitdiff
path: root/ext/vpopmail/php_vpopmail.c
blob: 30d54281686b9f4631f06cb665c41cbf03f4f501 (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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
   +----------------------------------------------------------------------+
   | 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
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#undef VPOPMAIL_IS_REALLY_OLD

#include <errno.h>
#include <signal.h>
#include "php.h"
#include "php_ini.h"
#include "php_vpopmail.h"

#if HAVE_VPOPMAIL

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

ZEND_DECLARE_MODULE_GLOBALS(vpopmail)

/* 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"
#include "vpopmail.h"
#if HAVE_VPOPMAIL_VAUTH
#include "vauth.h"
#endif


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

/* Function table */

function_entry vpopmail_functions[] = {
	/* domain management - lib call */
	PHP_FE(vpopmail_add_domain, NULL)
	PHP_FE(vpopmail_del_domain, NULL)
	PHP_FE(vpopmail_add_alias_domain, NULL)
	/* domain management - exec */
	PHP_FE(vpopmail_add_domain_ex, NULL)
	PHP_FE(vpopmail_del_domain_ex, NULL)
	PHP_FE(vpopmail_add_alias_domain_ex, NULL)
	/* user management */
	PHP_FE(vpopmail_add_user, NULL)
	PHP_FE(vpopmail_del_user, NULL)
	PHP_FE(vpopmail_passwd, NULL)
	PHP_FE(vpopmail_set_user_quota, NULL)
	PHP_FE(vpopmail_auth_user, NULL)
	/* alias management */
#if HAVE_VPOPMAIL_VALIAS
	PHP_FE(vpopmail_alias_add, NULL)
	PHP_FE(vpopmail_alias_del, NULL)
#ifdef VALIAS
	PHP_FE(vpopmail_alias_del_domain, NULL)
#endif
	PHP_FE(vpopmail_alias_get, NULL)
	PHP_FE(vpopmail_alias_get_all, NULL)
#endif
	/* error handling */
	PHP_FE(vpopmail_error, NULL)
	{NULL, NULL, NULL}
};

zend_module_entry vpopmail_module_entry = {
	"vpopmail",
	vpopmail_functions,
	PHP_MINIT(vpopmail),
	PHP_MSHUTDOWN(vpopmail),
	PHP_RINIT(vpopmail),
	PHP_RSHUTDOWN(vpopmail),
	PHP_MINFO(vpopmail),
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_VPOPMAIL
ZEND_GET_MODULE(vpopmail)
#endif


PHP_INI_BEGIN()
	/*STD_PHP_INI_ENTRY("vpopmail.directory",		"",				PHP_INI_ALL, OnUpdateString,	directory,			php_vpopmail_globals,	vpopmail_globals)*/
PHP_INI_END()


PHP_MINIT_FUNCTION(vpopmail)
{
	REGISTER_INI_ENTRIES();
	return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(vpopmail)
{
	UNREGISTER_INI_ENTRIES();
	return SUCCESS;
}

PHP_RINIT_FUNCTION(vpopmail)
{
	VPOPMAILLS_FETCH();

	VPOPMAILG(vpopmail_open) = 0;
	VPOPMAILG(vpopmail_errno) = 0;

	return SUCCESS;
}

/* nasty but... */
void vclose();
/* ...we need this */

PHP_RSHUTDOWN_FUNCTION(vpopmail)
{
	VPOPMAILLS_FETCH();

	if (VPOPMAILG(vpopmail_open) != 0) {
		vclose();
	}

	return SUCCESS;
}

PHP_MINFO_FUNCTION(vpopmail)
{
	char ids[64];

	sprintf(ids, "%d/%d %d/%d/%d/%d", VPOPMAILUID, VPOPMAILGID, getuid(), getgid(), geteuid(), getegid());

	php_info_print_table_start();
	php_info_print_table_header(2, "vpopmail support", "enabled");
	php_info_print_table_row(2, "vpopmail version", VERSION);
	php_info_print_table_row(2, "vpopmail uid/gid php uid/gid/euid/egid", ids);
	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);
#if HAVE_VPOPMAIL_VALIAS
	php_info_print_table_row(2, "vpopmail valias support", "Enabled");
#else
	php_info_print_table_row(2, "vpopmail valias support", "Not supported by vpopmail");
#endif
	php_info_print_table_end();

	DISPLAY_INI_ENTRIES();
}

/*
 * Domain management functions - library call
 */

/* {{{ proto bool vpopmail_add_domain(string domain, string dir, int uid, int gid)
   Add a new virtual domain */
PHP_FUNCTION(vpopmail_add_domain)
{
	zval **domain;
	zval **dir;
	zval **uid;
	zval **gid;
	int retval;

	if (ZEND_NUM_ARGS() != 4
			|| zend_get_parameters_ex(4, &domain, &dir, &uid, &gid) == FAILURE)
		WRONG_PARAM_COUNT;

	convert_to_string_ex(domain);
	convert_to_string_ex(dir);
	convert_to_long_ex(uid);
	convert_to_long_ex(gid);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = vadddomain(Z_STRVAL_PP(domain),
#ifdef VPOPMAIL_IS_REALLY_OLD
						0
#else
						Z_STRVAL_PP(dir),
						Z_LVAL_PP(uid),
						Z_LVAL_PP(gid)
#endif
						);
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_del_domain(string domain)
   Delete a virtual domain */
PHP_FUNCTION(vpopmail_del_domain)
{
	zval **domain;
	int retval;

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

	convert_to_string_ex(domain);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = vdeldomain(Z_STRVAL_PP(domain));
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_add_alias_domain(string domain, string aliasdomain)
   Add an alias for a virtual domain */
PHP_FUNCTION(vpopmail_add_alias_domain)
{
	zval **domain;
	zval **aliasdomain;
	char *tmpstr;
	char Dir[156];
	char TmpBuf1[300];
	char TmpBuf2[300];
	int uid, gid;

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

	convert_to_string_ex(domain);
	convert_to_string_ex(aliasdomain);

	php_strtolower(Z_STRVAL_PP(domain), Z_STRLEN_PP(domain));
	php_strtolower(Z_STRVAL_PP(aliasdomain), Z_STRLEN_PP(aliasdomain));

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	tmpstr = vget_assign(Z_STRVAL_PP(domain), Dir, 156, &uid, &gid);

	if (tmpstr == NULL) {
		php_error(E_WARNING, "vpopmail_add_alias_domain error: existing domain %s was not found", Z_STRVAL_PP(domain));
		VPOPMAILG(vpopmail_errno) = 1;
		RETURN_FALSE;
	}

	tmpstr = strstr(Dir, "/domains");
	*tmpstr = 0;

	sprintf(TmpBuf1, "%s/domains/%s", Dir, Z_STRVAL_PP(aliasdomain));
	sprintf(TmpBuf2, "%s/domains/%s", Dir, Z_STRVAL_PP(domain));

	if (symlink(TmpBuf2, TmpBuf1) != 0) {
	        php_error(E_WARNING, "vpopmail_add_alias_domain error: could not symlink domains: %s", strerror(errno));
		VPOPMAILG(vpopmail_errno) = 1;
		RETURN_FALSE;
	}

	if (add_domain_assign(Z_STRVAL_PP(aliasdomain), Dir, uid, gid) != 0) {
		php_error(E_WARNING, "vpopmail_addaliasdomain could not add domain to control files");
		VPOPMAILG(vpopmail_errno) = 1;
		RETURN_FALSE;
	}

	signal_process("qmail-send", SIGHUP);

	VPOPMAILG(vpopmail_errno) = 0;
	RETURN_TRUE;
}
/* }}} */

/*
 * Domain management functions - exec
 */

/* {{{ proto bool vpopmail_add_domain_ex(string domain, string passwd [, string quota [, string bounce [, bool apop]]])
   Add a new virtual domain */
PHP_FUNCTION(vpopmail_add_domain_ex) {
	zval **domain, **passwd, **quota, **bounce, **apop;
	int retval,len=0,argc=ZEND_NUM_ARGS(),is_bounce_email;
	int fr_bounce=0,fr_quota=0;
	char *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;
	}
	
	VPOPMAILLS_FETCH();

	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");
				VPOPMAILG(vpopmail_errno)=1;
				RETURN_FALSE;
			}
			/* 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");
				VPOPMAILG(vpopmail_errno)=1;
				RETURN_FALSE;
			}
			/* 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");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}
	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");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}
	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=php_Exec(0,cmd,NULL,return_value);
	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_add_domain_ex error: %d", retval);
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	} else {
		VPOPMAILG(vpopmail_errno)=0;
		RETURN_TRUE;
	}
}
/* }}} */

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

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

	VPOPMAILLS_FETCH();

	convert_to_string_ex(domain);

	escdomain=php_escape_shell_cmd(Z_STRVAL_PP(domain));
	if (!escdomain) {
		php_error(E_WARNING,"vpopmail_del_domain_ex error: cannot alloc");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}
	cmd=emalloc(strlen(VPOPMAIL_BIN_DIR)+strlen(VPOPMAIL_DELD)+strlen(escdomain)+1);
	if (!cmd) {
		efree(escdomain);
		php_error(E_WARNING,"vpopmail_del_domain_ex error: cannot alloc");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}
	sprintf(cmd,VPOPMAIL_BIN_DIR VPOPMAIL_DELD"%s",escdomain);
	retval=php_Exec(0,cmd,NULL,return_value);
	efree(escdomain);
	efree(cmd);

	if (retval!=VA_SUCCESS) {
		php_error(E_WARNING,"vpopmail_del_domain_ex error: %d", retval);
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	} else {
		VPOPMAILG(vpopmail_errno)=0;
		RETURN_TRUE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_add_alias_domain_ex(string olddomain, string newdomain)
   Add alias to an existing virtual domain */
PHP_FUNCTION(vpopmail_add_alias_domain_ex) {
	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;
	}

	VPOPMAILLS_FETCH();

	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");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}
	escolddomain=php_escape_shell_cmd(Z_STRVAL_PP(olddomain));
	if (!escolddomain) {
		efree(escnewdomain);
		php_error(E_WARNING,"vpopmail_addaliasdomain error: cannot alloc");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}

	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");
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	}
	sprintf(cmd,"%s%s %s",VPOPMAIL_BIN_DIR VPOPMAIL_ADAD,escolddomain,escnewdomain);
	retval=php_Exec(0,cmd,NULL,return_value);
	efree(cmd);
	efree(escnewdomain);
	efree(escolddomain);

	if (retval!=VA_SUCCESS) {
		php_error(E_WARNING,"vpopmail_addaliasdomain error: %d", retval);
		VPOPMAILG(vpopmail_errno)=1;
		RETURN_FALSE;
	} else {
		VPOPMAILG(vpopmail_errno)=0;
		RETURN_TRUE;
	}
}
/* }}} */

/*
 * User management functions
 */

/* {{{ proto bool vpopmail_add_user(string user, string domain, string password[, string gecos[, bool apop]])
   Add a new user to the specified virtual domain */
PHP_FUNCTION(vpopmail_add_user)
{
	zval **user;
	zval **domain;
	zval **password;
	zval **gecos;
	zval **apop;
	int is_apop = 0;
	char *the_gecos = "";
	int retval;

	if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 5
			|| zend_get_parameters_ex(ZEND_NUM_ARGS(), &user, &domain, &password, &gecos, &apop) == FAILURE)
		WRONG_PARAM_COUNT;

	switch (ZEND_NUM_ARGS()) {
	case 5:
		convert_to_boolean_ex(apop);
		is_apop = (Z_BVAL_PP(apop) ? 1 : 0);
		/* fall through */

	case 4:
		convert_to_string_ex(gecos);
		the_gecos = Z_STRVAL_PP(gecos);
		/* fall through */

	default:
		convert_to_string_ex(user);
		convert_to_string_ex(domain);
		convert_to_string_ex(password);
	}

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = vadduser(Z_STRVAL_PP(user),
					  Z_STRVAL_PP(domain),
					  Z_STRVAL_PP(password),
					  the_gecos,
					  is_apop);
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
        	php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_del_user(string user, string domain)
   Delete a user from a virtual domain */
PHP_FUNCTION(vpopmail_del_user)
{
	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);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

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

	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

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

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

	if (ZEND_NUM_ARGS() > 3) {
		convert_to_boolean_ex(apop);
		is_apop = (Z_BVAL_PP(apop) ? 1 : 0);
	}

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

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = vpasswd(Z_STRVAL_PP(user),
					 Z_STRVAL_PP(domain),
					 Z_STRVAL_PP(password),
					 is_apop);
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_set_user_quota(string user, string domain, string quota)
   Sets a virtual user's quota */
PHP_FUNCTION(vpopmail_set_user_quota)
{
	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);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = vsetuserquota(Z_STRVAL_PP(user),
						   Z_STRVAL_PP(domain),
						   Z_STRVAL_PP(quota));
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_auth_user(string user, string domain, string password[, string apop])
   Attempt to validate a username/domain/password. Returns true/false */
PHP_FUNCTION(vpopmail_auth_user)
{
	zval **user;
	zval **domain;
	zval **password;
	zval **apop;
	/* indeed we do not care of contents; newer vpopmail versions use struct vpasswd instead passwd */
	void *retval;
	int argc=ZEND_NUM_ARGS();

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

	if (argc > 3)
		convert_to_string_ex(apop);

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

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;
	VPOPMAILG(vpopmail_errno) = 0;

	retval = vauth_user(Z_STRVAL_PP(user),
				Z_STRVAL_PP(domain),
				Z_STRVAL_PP(password),
				(argc>3)?Z_STRVAL_PP(apop):"");

	/* 
	 * we do not set vpopmail_errno here - it is considered auth_user cannot fail; insted it does not auth
	 * this is a vpopmail's api limitation - there is no error return form vauth_user
	 */

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


#if HAVE_VPOPMAIL_VALIAS

/*
 * Alias management functions
 */


/* {{{ proto bool vpopmail_alias_add(string user, string domain, string alias)
   insert a virtual alias */
PHP_FUNCTION(vpopmail_alias_add)
{
	zval **user;
	zval **domain;
	zval **alias;
	int retval;

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

	convert_to_string_ex(user);
	convert_to_string_ex(domain);
	convert_to_string_ex(alias);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = valias_insert(Z_STRVAL_PP(user),
						   Z_STRVAL_PP(domain),
						   Z_STRVAL_PP(alias));
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto bool vpopmail_alias_del(string user, string domain)
   deletes all virtual aliases of a user */
PHP_FUNCTION(vpopmail_alias_del)
{
	zval **user;
	zval **domain;
	int retval;

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

	convert_to_string_ex(user);
	convert_to_string_ex(domain);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = valias_delete(Z_STRVAL_PP(user), Z_STRVAL_PP(domain));
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */

#ifdef VALIAS
/* {{{ proto bool vpopmail_alias_del_domain(string domain)
   deletes all virtual aliases of a domain */
PHP_FUNCTION(vpopmail_alias_del_domain)
{
	zval **domain;
	int retval;

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

	convert_to_string_ex(domain);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	retval = valias_delete_domain(Z_STRVAL_PP(domain));
	
	VPOPMAILG(vpopmail_errno)=retval;

	if (retval == VA_SUCCESS) {
		RETURN_TRUE;
	} else {
	        php_error(E_WARNING, "vpopmail error: %s", verror(retval));
		RETURN_FALSE;
	}
}
/* }}} */
#endif

/* {{{ proto array vpopmail_alias_get(string alias, string domain)
   get all lines of an alias for a domain */
PHP_FUNCTION(vpopmail_alias_get)
{
	zval **alias;
	zval **domain;
	int retval;
	char *talias;
	uint index=0;

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

	convert_to_string_ex(alias);
	convert_to_string_ex(domain);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	if (array_init(return_value)!=SUCCESS) {
		zend_error(E_ERROR,"unable to create array");
		RETURN_FALSE;
	}

	talias=valias_select(Z_STRVAL_PP(alias), Z_STRVAL_PP(domain));
	while (talias) {
		add_index_string(return_value,index++,talias,1);
		talias=valias_select_next();
	}
}
/* }}} */

/* {{{ proto array vpopmail_alias_get_all(string domain)
   get all lines of an alias for a domain */
PHP_FUNCTION(vpopmail_alias_get_all)
{
	zval **domain;
	int retval;
	char *talias;
	char tpath[1024];
	uint index=0;

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

	convert_to_string_ex(domain);

	VPOPMAILLS_FETCH();
	VPOPMAILG(vpopmail_open) = 1;

	if (array_init(return_value)!=SUCCESS) {
		zend_error(E_ERROR,"unable to create array");
		RETURN_FALSE;
	}

	talias=valias_select_all(tpath, Z_STRVAL_PP(domain));
	while (talias) {
		strcat(tpath,":");
		strcat(tpath,talias);
		add_index_string(return_value,index++,tpath,1);
		talias=valias_select_all_next(tpath);
	}
}
/* }}} */

#endif

/*
 * Error handling helper
 */

/* {{{ proto string vpopmail_error(void)
   Get text message for last vpopmail error. Returns string */
PHP_FUNCTION(vpopmail_error)
{
	if (ZEND_NUM_ARGS() != 0)
		WRONG_PARAM_COUNT;
	
	VPOPMAILLS_FETCH();

	RETURN_STRING(verror(VPOPMAILG(vpopmail_errno)),1);
}
/* }}} */

#endif HAVE_VPOPMAIL

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: sw=4 ts=4 tw=78 fdm=marker
 * vim<600: sw=4 ts=4 tw=78
 */