cacb3c3f2acd0699f020583a19d1860714d8055c
[m6w6/ext-courierauth] / courierauth.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: courierauth |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include "php.h"
20 #include "ext/standard/info.h"
21 #include "php_courierauth.h"
22
23 #include <courierauth.h>
24
25 typedef struct {
26 zval *rv;
27 unsigned success:1;
28 unsigned _res:31;
29 } php_courierauth_data;
30
31 #define php_courierauth_data_init {return_value, 0, 0}
32
33 static int php_courierauth_callback(struct authinfo *ai, void *arg)
34 {
35 php_courierauth_data *pa = (php_courierauth_data *) arg;
36
37 convert_to_object(pa->rv);
38
39 if (ai->sysusername) {
40 add_property_string(pa->rv, "sysusername", ai->sysusername);
41 add_property_null(pa->rv, "sysuserid");
42 add_property_null(pa->rv, "sysgroupid");
43 } else if (ai->sysuserid) {
44 add_property_null(pa->rv, "sysusername");
45 add_property_long(pa->rv, "sysuserid", *ai->sysuserid);
46 add_property_long(pa->rv, "sysgroupid", ai->sysgroupid);
47 } else {
48 add_property_null(pa->rv, "sysusername");
49 add_property_null(pa->rv, "sysuserid");
50 add_property_null(pa->rv, "sysgroupid");
51 }
52 #define ADD_STRING(s) \
53 if (ai->s) { \
54 add_property_string(pa->rv, #s, ai->s); \
55 } else { \
56 add_property_null(pa->rv, #s); \
57 }
58 ADD_STRING(homedir);
59 ADD_STRING(address);
60 ADD_STRING(fullname);
61 ADD_STRING(maildir);
62 ADD_STRING(quota);
63 ADD_STRING(passwd);
64 #if PHP_COURIERAUTH_SECURITY_RISK
65 ADD_STRING(clearpasswd);
66 #else
67 add_property_null(pa->rv, "clearpasswd");
68 #endif
69 ADD_STRING(options);
70
71 return 0;
72 }
73
74 static void php_courierauth_enumeration_callback(const char *sysusername, uid_t sysuserid, gid_t sysgroupid, const char *homedir, const char *maildir, const char *options, void *arg)
75 {
76 struct authinfo ai = {NULL};
77 php_courierauth_data *pa = (php_courierauth_data *) arg;
78
79 if (!sysusername && !sysuserid && !homedir && !maildir && !options) {
80 pa->success = 1;
81 } else {
82 zval *array, tmp;
83
84 array = pa->rv;
85 pa->rv = &tmp;
86 ZVAL_NULL(&tmp);
87
88 ai.sysusername = sysusername;
89 ai.sysuserid = &sysuserid;
90 ai.homedir = homedir;
91 ai.maildir = maildir;
92 ai.options = options;
93
94 php_courierauth_callback(&ai, (void *) pa);
95
96 add_next_index_zval(array, pa->rv);
97 pa->rv = array;
98 }
99 }
100
101 /* {{{ proto object courierauth_login(string service, string user, string pass)
102 Login and return account info on success */
103 static PHP_FUNCTION(courierauth_login)
104 {
105 char *svc_str, *user_str, *pass_str;
106 size_t svc_len, user_len, pass_len;
107 php_courierauth_data ca = php_courierauth_data_init;
108
109 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "sss", &svc_str, &svc_len, &user_str, &user_len, &pass_str, &pass_len)) {
110 RETURN_FALSE;
111 }
112
113 if (0 != auth_login(svc_str, user_str, pass_str, php_courierauth_callback, &ca)) {
114 RETURN_FALSE;
115 }
116 }
117 /* }}} */
118
119 /* {{{ proto object courierauth_getuserinfo(string service, string user)
120 Get account info for a user */
121 static PHP_FUNCTION(courierauth_getuserinfo)
122 {
123 char *svc_str, *user_str;
124 size_t svc_len, user_len;
125 php_courierauth_data ca = php_courierauth_data_init;
126
127 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &svc_str, &svc_len, &user_str, &user_len)) {
128 RETURN_FALSE;
129 }
130
131 if (0 != auth_getuserinfo(svc_str, user_str, php_courierauth_callback, &ca)) {
132 RETURN_FALSE;
133 }
134 }
135 /* }}} */
136
137 /* {{{ proto array courierauth_enumerate(void)
138 List all users */
139 static PHP_FUNCTION(courierauth_enumerate)
140 {
141 php_courierauth_data ca = php_courierauth_data_init;
142
143 zend_parse_parameters_none();
144
145 array_init(return_value);
146 auth_enumerate(php_courierauth_enumeration_callback, &ca);
147 if (!ca.success) {
148 php_error_docref(NULL, E_NOTICE, "auth_enumerate() aborted or unavailable");
149 }
150 }
151 /* }}} */
152
153 /* {{{ proto bool courierauth_passwd(string service, string user, string old_pass, string new_pass)
154 Change the password of a user */
155 static PHP_FUNCTION(courierauth_passwd)
156 {
157 char *svc_str, *user_str, *oldpw_str, *newpw_str;
158 size_t svc_len, user_len, oldpw_len, newpw_len;
159
160 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "ssss", &svc_str, &svc_len, &user_str, &user_len, &oldpw_str, &oldpw_len, &newpw_str, &newpw_len)) {
161 RETURN_FALSE;
162 }
163
164 RETURN_BOOL(0 == auth_passwd(svc_str, user_str, oldpw_str, newpw_str));
165 }
166
167 /* {{{ proto string courierauth_getoption(string options, string key)
168 Get the value of a key from options string */
169 static PHP_FUNCTION(courierauth_getoption)
170 {
171 char *opt_str, *key_str, *val_str;
172 size_t opt_len, key_len;
173
174 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &opt_str, &opt_len, &key_str, &key_len)) {
175 RETURN_FALSE;
176 }
177
178 if ((val_str = auth_getoption(opt_str, key_str))) {
179 RETVAL_STRING(val_str);
180 free(val_str);
181 } else {
182 RETURN_FALSE;
183 }
184 }
185 /* }}} */
186
187 /* {{{ courierauth_functions[] */
188 static zend_function_entry courierauth_functions[] = {
189 PHP_FE(courierauth_login, NULL)
190 PHP_FE(courierauth_enumerate, NULL)
191 PHP_FE(courierauth_getuserinfo, NULL)
192 PHP_FE(courierauth_passwd, NULL)
193 PHP_FE(courierauth_getoption, NULL)
194 {0}
195 };
196 /* }}} */
197
198 /* {{{ PHP_MINFO_FUNCTION */
199 static PHP_MINFO_FUNCTION(courierauth)
200 {
201 php_info_print_table_start();
202 php_info_print_table_header(2, "courierauth support", "enabled");
203 php_info_print_table_row(2, "extension version", PHP_COURIERAUTH_VERSION);
204 php_info_print_table_row(2, "courierauth version", "unknown");
205 php_info_print_table_end();
206 }
207 /* }}} */
208
209 /* {{{ courierauth_module_entry
210 */
211 zend_module_entry courierauth_module_entry = {
212 STANDARD_MODULE_HEADER,
213 "courierauth",
214 courierauth_functions,
215 NULL,
216 NULL,
217 NULL,
218 NULL,
219 PHP_MINFO(courierauth),
220 PHP_COURIERAUTH_VERSION,
221 STANDARD_MODULE_PROPERTIES
222 };
223 /* }}} */
224
225 #ifdef COMPILE_DL_COURIERAUTH
226 ZEND_GET_MODULE(courierauth)
227 #endif
228
229 /*
230 * Local variables:
231 * tab-width: 4
232 * c-basic-offset: 4
233 * End:
234 * vim600: noet sw=4 ts=4 fdm=marker
235 * vim<600: noet sw=4 ts=4
236 */