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