2314c33f1f6acdb8b7bb5f640da968fa024b62bb
[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 /* {{{ courierauth_functions[] */
26 zend_function_entry courierauth_functions[] = {
27 PHP_FE(courierauth_login, NULL)
28 PHP_FE(courierauth_enumerate, NULL)
29 PHP_FE(courierauth_getuserinfo, NULL)
30 PHP_FE(courierauth_passwd, NULL)
31 PHP_FE(courierauth_getoption, NULL)
32 {NULL, NULL, NULL}
33 };
34 /* }}} */
35
36 /* {{{ courierauth_module_entry
37 */
38 zend_module_entry courierauth_module_entry = {
39 #if ZEND_MODULE_API_NO >= 20010901
40 STANDARD_MODULE_HEADER,
41 #endif
42 "courierauth",
43 courierauth_functions,
44 NULL,
45 NULL,
46 NULL,
47 NULL,
48 PHP_MINFO(courierauth),
49 #if ZEND_MODULE_API_NO >= 20010901
50 PHP_COURIERAUTH_VERSION,
51 #endif
52 STANDARD_MODULE_PROPERTIES
53 };
54 /* }}} */
55
56 #ifdef COMPILE_DL_COURIERAUTH
57 ZEND_GET_MODULE(courierauth)
58 #endif
59
60 /* {{{ PHP_MINFO_FUNCTION */
61 PHP_MINFO_FUNCTION(courierauth)
62 {
63 php_info_print_table_start();
64 php_info_print_table_header(2, "courierauth support", "enabled");
65 php_info_print_table_row(2, "courierauth version", courierauth_h_rcsid);
66 php_info_print_table_end();
67 }
68 /* }}} */
69
70 typedef struct {
71 zval *rv;
72 #ifdef ZTS
73 void ***tsrm_ls;
74 #endif
75 uint success:1;
76 uint _res:31;
77 } php_courierauth_data;
78
79 #ifdef ZTS
80 #define php_courierauth_data_init {return_value, tsrm_ls, 0, 0}
81 #else
82 #define TSRMLS_SET_CTX(c)
83 #define TSRMLS_FETCH_FROM_CTX(c)
84 #define php_courierauth_data_init {return_value, 0, 0}
85 #endif
86
87 static int php_courierauth_callback(struct authinfo *ai, void *arg)
88 {
89 php_courierauth_data *pa = (php_courierauth_data *) arg;
90 TSRMLS_FETCH_FROM_CTX(pa->tsrm_ls);
91
92 convert_to_object(pa->rv);
93
94 if (ai->sysusername) {
95 add_property_string(pa->rv, "sysusername", (char *) ai->sysusername, 1);
96 add_property_null(pa->rv, "sysuserid");
97 add_property_null(pa->rv, "sysgroupid");
98 } else if (ai->sysuserid) {
99 add_property_null(pa->rv, "sysusername");
100 add_property_long(pa->rv, "sysuserid", *ai->sysuserid);
101 add_property_long(pa->rv, "sysgroupid", ai->sysgroupid);
102 } else {
103 add_property_null(pa->rv, "sysusername");
104 add_property_null(pa->rv, "sysuserid");
105 add_property_null(pa->rv, "sysgroupid");
106 }
107 #define ADD_STRING(s) \
108 if (ai->s) { \
109 add_property_string(pa->rv, #s, (char *) ai->s, 1); \
110 } else { \
111 add_property_null(pa->rv, #s); \
112 }
113 ADD_STRING(homedir);
114 ADD_STRING(address);
115 ADD_STRING(fullname);
116 ADD_STRING(maildir);
117 ADD_STRING(quota);
118 ADD_STRING(passwd);
119 #if PHP_COURIERAUTH_SECURITY_RISK
120 ADD_STRING(clearpasswd);
121 #else
122 add_property_null(pa->rv, "clearpasswd");
123 #endif
124 ADD_STRING(options);
125
126 return 0;
127 }
128
129 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)
130 {
131 zval *entry, *array;
132 struct authinfo ai = {NULL};
133 php_courierauth_data *pa = (php_courierauth_data *) arg;
134 TSRMLS_FETCH_FROM_CTX(pa->tsrm_ls);
135
136 if (!sysusername && !sysuserid && !homedir && !maildir && !options) {
137 pa->success = 1;
138 } else {
139 array = pa->rv;
140 convert_to_array(array);
141 MAKE_STD_ZVAL(entry);
142 ZVAL_NULL(entry);
143 pa->rv = entry;
144
145 ai.sysusername = sysusername;
146 ai.sysuserid = &sysuserid;
147 ai.homedir = homedir;
148 ai.maildir = maildir;
149 ai.options = options;
150
151 php_courierauth_callback(&ai, (void *) pa);
152
153 add_next_index_zval(array, entry);
154 pa->rv = array;
155 }
156 }
157
158 /* {{{ proto object courierauth_login(string service, string user, string pass)
159 Login and return account info on success */
160 PHP_FUNCTION(courierauth_login)
161 {
162 char *svc_str, *user_str, *pass_str;
163 int svc_len, user_len, pass_len;
164 php_courierauth_data ca = php_courierauth_data_init;
165
166 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &svc_str, &svc_len, &user_str, &user_len, &pass_str, &pass_len)) {
167 RETURN_FALSE;
168 }
169
170 if (0 != auth_login(svc_str, user_str, pass_str, php_courierauth_callback, &ca)) {
171 RETURN_FALSE;
172 }
173 }
174 /* }}} */
175
176 /* {{{ proto object courierauth_getuserinfo(string service, string user)
177 Get account info for a user */
178 PHP_FUNCTION(courierauth_getuserinfo)
179 {
180 char *svc_str, *user_str;
181 int svc_len, user_len;
182 php_courierauth_data ca = php_courierauth_data_init;
183
184 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &svc_str, &svc_len, &user_str, &user_len)) {
185 RETURN_FALSE;
186 }
187
188 if (0 != auth_getuserinfo(svc_str, user_str, php_courierauth_callback, &ca)) {
189 RETURN_FALSE;
190 }
191 }
192 /* }}} */
193
194 /* {{{ proto array courierauth_enumerate(void)
195 List all users */
196 PHP_FUNCTION(courierauth_enumerate)
197 {
198 php_courierauth_data ca = php_courierauth_data_init;
199
200 zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "");
201
202 auth_enumerate(php_courierauth_enumeration_callback, &ca);
203 if (!ca.success) {
204 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "auth_enumerate() aborted or unavailable");
205 }
206 }
207 /* }}} */
208
209 /* {{{ proto bool courierauth_passwd(string service, string user, string old_pass, string new_pass)
210 Change the password of a user */
211 PHP_FUNCTION(courierauth_passwd)
212 {
213 char *svc_str, *user_str, *oldpw_str, *newpw_str;
214 int svc_len, user_len, oldpw_len, newpw_len;
215
216 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &svc_str, &svc_len, &user_str, &user_len, &oldpw_str, &oldpw_len, &newpw_str, &newpw_len)) {
217 RETURN_FALSE;
218 }
219
220 RETURN_BOOL(0 == auth_passwd(svc_str, user_str, oldpw_str, newpw_str));
221 }
222
223 /* {{{ proto string courierauth_getoption(string options, string key)
224 Get the value of a key from options string */
225 PHP_FUNCTION(courierauth_getoption)
226 {
227 char *opt_str, *key_str, *val_str;
228 int opt_len, key_len;
229
230 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &opt_str, &opt_len, &key_str, &key_len)) {
231 RETURN_FALSE;
232 }
233
234 if ((val_str = auth_getoption(opt_str, key_str))) {
235 RETVAL_STRING(val_str, 1);
236 free(val_str);
237 } else {
238 RETURN_FALSE;
239 }
240 }
241 /* }}} */
242
243 /*
244 * Local variables:
245 * tab-width: 4
246 * c-basic-offset: 4
247 * End:
248 * vim600: noet sw=4 ts=4 fdm=marker
249 * vim<600: noet sw=4 ts=4
250 */