Removed in favor of the general SQL interface
Showing
2 changed files
with
0 additions
and
610 deletions
auth/mysql.c
deleted
100644 → 0
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2002, 2003 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public | ||
15 | License along with this library; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #ifdef HAVE_MYSQL | ||
23 | |||
24 | #include <sql.h> | ||
25 | #include <mysql/mysql.h> | ||
26 | |||
27 | #define MFLAGS 0 /* Special user flags. It is safe to leave | ||
28 | this untouched */ | ||
29 | |||
30 | |||
31 | int | ||
32 | mysql_auth_sql_by_name (struct mu_auth_data **return_data, | ||
33 | const void *key, | ||
34 | void *func_data ARG_UNUSED, | ||
35 | void *call_data ARG_UNUSED) | ||
36 | { | ||
37 | char *query_str = NULL; | ||
38 | MYSQL *m; | ||
39 | MYSQL_RES *res; | ||
40 | MYSQL_ROW row; | ||
41 | char *mailbox_name; | ||
42 | int rc; | ||
43 | |||
44 | if (!key) | ||
45 | { | ||
46 | errno = EINVAL; | ||
47 | return 1; | ||
48 | } | ||
49 | |||
50 | m = mysql_init (0); | ||
51 | |||
52 | if (!m) | ||
53 | return 1; | ||
54 | |||
55 | if (!mysql_real_connect (m, | ||
56 | mu_sql_host, mu_sql_user, mu_sql_passwd, | ||
57 | mu_sql_db, mu_sql_port, | ||
58 | mu_sql_socket, MFLAGS)) | ||
59 | { | ||
60 | mu_error (_("MySQL: connect failed: %s"), mysql_error (m)); | ||
61 | mysql_close (m); | ||
62 | return 1; | ||
63 | } | ||
64 | |||
65 | query_str = mu_sql_expand_query (mu_sql_getpwnam_query, key); | ||
66 | |||
67 | if (!query_str) | ||
68 | { | ||
69 | mysql_close (m); | ||
70 | return 1; | ||
71 | } | ||
72 | |||
73 | if (mysql_query (m, query_str) != 0) | ||
74 | { | ||
75 | mu_error (_("MySQL: query failed: %s"), mysql_error (m)); | ||
76 | free (query_str); | ||
77 | mysql_close (m); | ||
78 | return 1; | ||
79 | } | ||
80 | |||
81 | free (query_str); | ||
82 | |||
83 | if ((res = mysql_store_result (m)) == NULL) | ||
84 | { | ||
85 | mu_error (_("MySQL: can't store result: %s"), mysql_error (m)); | ||
86 | mysql_close (m); | ||
87 | return 1; | ||
88 | } | ||
89 | |||
90 | if ((row = mysql_fetch_row (res)) == NULL) | ||
91 | { | ||
92 | mu_error (_("MySQL: can't fetch row: %s"), mysql_error (m)); | ||
93 | mysql_close (m); | ||
94 | return 1; | ||
95 | } | ||
96 | |||
97 | if (mysql_num_fields (res) == 7 && row[6]) | ||
98 | mailbox_name = strdup (row[6]); | ||
99 | else | ||
100 | { | ||
101 | mailbox_name = malloc (strlen (mu_path_maildir) + | ||
102 | strlen (row[0]) + 1); | ||
103 | if (mailbox_name) | ||
104 | sprintf (mailbox_name, "%s%s", mu_path_maildir, row[0]); | ||
105 | } | ||
106 | |||
107 | if (mailbox_name) | ||
108 | rc = mu_auth_data_alloc (return_data, | ||
109 | row[0], | ||
110 | row[1], | ||
111 | atoi (row[2]), | ||
112 | atoi (row[3]), | ||
113 | "Mysql User", | ||
114 | row[4], | ||
115 | row[5], | ||
116 | mailbox_name, | ||
117 | 1); | ||
118 | else | ||
119 | rc = 1; | ||
120 | |||
121 | free (mailbox_name); | ||
122 | mysql_free_result (res); | ||
123 | mysql_close (m); | ||
124 | |||
125 | return rc; | ||
126 | } | ||
127 | |||
128 | int | ||
129 | mysql_auth_sql_by_uid (struct mu_auth_data **return_data, void *key, | ||
130 | void *func_data ARG_UNUSED, | ||
131 | void *call_data ARG_UNUSED) | ||
132 | { | ||
133 | char *query_str = NULL; | ||
134 | MYSQL *m; | ||
135 | MYSQL_RES *res; | ||
136 | MYSQL_ROW row; | ||
137 | char uidstr[64]; | ||
138 | char *mailbox_name; | ||
139 | int rc; | ||
140 | |||
141 | if (!key) | ||
142 | { | ||
143 | errno = EINVAL; | ||
144 | return 1; | ||
145 | } | ||
146 | |||
147 | m = mysql_init (0); | ||
148 | |||
149 | if (!m) | ||
150 | return 1; | ||
151 | |||
152 | if (!mysql_real_connect (m, | ||
153 | mu_sql_host, mu_sql_user, | ||
154 | mu_sql_passwd, mu_sql_db, | ||
155 | mu_sql_port, mu_sql_socket, MFLAGS)) | ||
156 | { | ||
157 | mu_error (_("MySQL: connect failed: %s"), mysql_error (m)); | ||
158 | mysql_close (m); | ||
159 | return 1; | ||
160 | } | ||
161 | |||
162 | snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key); | ||
163 | query_str = mu_sql_expand_query (mu_sql_getpwuid_query, uidstr); | ||
164 | |||
165 | if (!query_str) | ||
166 | { | ||
167 | mysql_close (m); | ||
168 | return 1; | ||
169 | } | ||
170 | |||
171 | if (mysql_query (m, query_str) != 0) | ||
172 | { | ||
173 | mu_error (_("MySQL: query failed: %s"), mysql_error (m)); | ||
174 | free (query_str); | ||
175 | mysql_close (m); | ||
176 | return 1; | ||
177 | } | ||
178 | |||
179 | free (query_str); | ||
180 | |||
181 | if ((res = mysql_store_result (m)) == NULL) | ||
182 | { | ||
183 | mu_error (_("MySQL: can't store result: %s"), mysql_error (m)); | ||
184 | mysql_close (m); | ||
185 | return 1; | ||
186 | } | ||
187 | |||
188 | if ((row = mysql_fetch_row (res)) == NULL) | ||
189 | { | ||
190 | mu_error (_("MySQL: can't fetch row: %s"), mysql_error (m)); | ||
191 | mysql_close (m); | ||
192 | return 1; | ||
193 | } | ||
194 | |||
195 | if (mysql_num_fields (res) == 7 && row[6]) | ||
196 | mailbox_name = strdup (row[6]); | ||
197 | else | ||
198 | { | ||
199 | mailbox_name = malloc (strlen (mu_path_maildir) + | ||
200 | strlen (row[0]) + 1); | ||
201 | if (mailbox_name) | ||
202 | sprintf (mailbox_name, "%s%s", mu_path_maildir, row[0]); | ||
203 | } | ||
204 | |||
205 | if (mailbox_name) | ||
206 | rc = mu_auth_data_alloc (return_data, | ||
207 | row[0], | ||
208 | row[1], | ||
209 | atoi (row[2]), | ||
210 | atoi (row[3]), | ||
211 | "Mysql User", | ||
212 | row[4], | ||
213 | row[5], | ||
214 | mailbox_name, | ||
215 | 1); | ||
216 | else | ||
217 | rc = 1; | ||
218 | |||
219 | free (mailbox_name); | ||
220 | mysql_free_result (res); | ||
221 | mysql_close (m); | ||
222 | |||
223 | return rc; | ||
224 | } | ||
225 | |||
226 | int | ||
227 | mysql_sql_authenticate (struct mu_auth_data **return_data ARG_UNUSED, | ||
228 | const void *key, | ||
229 | void *func_data ARG_UNUSED, void *call_data) | ||
230 | { | ||
231 | struct mu_auth_data *auth_data = key; | ||
232 | char *pass = call_data; | ||
233 | char *query_str = NULL; | ||
234 | MYSQL *m; | ||
235 | MYSQL_RES *res; | ||
236 | MYSQL_ROW row; | ||
237 | int rc; | ||
238 | |||
239 | if (!auth_data) | ||
240 | return 1; | ||
241 | |||
242 | m = mysql_init (0); | ||
243 | |||
244 | if (!m) | ||
245 | return 1; | ||
246 | |||
247 | if (!mysql_real_connect (m, | ||
248 | mu_sql_host, mu_sql_user, | ||
249 | mu_sql_passwd, mu_sql_db, | ||
250 | mu_sql_port, | ||
251 | mu_sql_socket, MFLAGS)) | ||
252 | { | ||
253 | mu_error (_("MySQL: connect failed: %s"), mysql_error (m)); | ||
254 | mysql_close (m); | ||
255 | return 1; | ||
256 | } | ||
257 | |||
258 | query_str = mu_sql_expand_query (mu_sql_getpass_query, auth_data->name); | ||
259 | |||
260 | if (!query_str) | ||
261 | { | ||
262 | mysql_close (m); | ||
263 | return 1; | ||
264 | } | ||
265 | |||
266 | if (mysql_query (m, query_str) != 0) | ||
267 | { | ||
268 | mu_error (_("MySQL: query failed: %s"), mysql_error (m)); | ||
269 | free (query_str); | ||
270 | mysql_close (m); | ||
271 | return 1; | ||
272 | } | ||
273 | |||
274 | free (query_str); | ||
275 | |||
276 | if ((res = mysql_store_result (m)) == NULL) | ||
277 | { | ||
278 | mu_error (_("MySQL: can't store result: %s"), mysql_error (m)); | ||
279 | mysql_close (m); | ||
280 | return 1; | ||
281 | } | ||
282 | |||
283 | if ((row = mysql_fetch_row (res)) == NULL) | ||
284 | { | ||
285 | mu_error (_("MySQL: can't fetch row: %s"), mysql_error (m)); | ||
286 | mysql_close (m); | ||
287 | return 1; | ||
288 | } | ||
289 | |||
290 | rc = strcmp (row[0], crypt (pass, row[0])); | ||
291 | mysql_free_result (res); | ||
292 | mysql_close (m); | ||
293 | return rc; | ||
294 | } | ||
295 | |||
296 | #endif |
auth/pgsql.c
deleted
100644 → 0
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public | ||
15 | License along with this library; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #ifdef HAVE_PGSQL | ||
23 | #include <sql.h> | ||
24 | #include <libpq-fe.h> | ||
25 | #include <ctype.h> | ||
26 | |||
27 | static PGconn * | ||
28 | pg_connect () | ||
29 | { | ||
30 | PGconn *pgconn; | ||
31 | char portbuf[16]; | ||
32 | char *portstr; | ||
33 | |||
34 | if (mu_sql_port == 0) | ||
35 | portstr = NULL; | ||
36 | else | ||
37 | { | ||
38 | portstr = portbuf; | ||
39 | snprintf (portbuf, sizeof (portbuf), "%d", mu_sql_port); | ||
40 | } | ||
41 | |||
42 | pgconn = PQsetdbLogin (mu_sql_host, portstr, NULL, NULL, | ||
43 | mu_sql_db, mu_sql_user, mu_sql_passwd); | ||
44 | |||
45 | if (PQstatus (pgconn) == CONNECTION_BAD) | ||
46 | { | ||
47 | mu_error("PQconnectStart: %s", PQerrorMessage (pgconn)); | ||
48 | PQfinish (pgconn); | ||
49 | return NULL; | ||
50 | } | ||
51 | return pgconn; | ||
52 | } | ||
53 | |||
54 | static char * | ||
55 | chop (char *str) | ||
56 | { | ||
57 | int len; | ||
58 | |||
59 | for (len = strlen(str); len > 0 && isspace(str[len-1]); len--) | ||
60 | ; | ||
61 | str[len] = 0; | ||
62 | return str; | ||
63 | } | ||
64 | |||
65 | static int | ||
66 | pg_auth_common (PGresult *res, char *query_str, struct mu_auth_data **auth) | ||
67 | { | ||
68 | char *user_name, *passwd, *homedir, *shell, *mailbox_name; | ||
69 | uid_t uid; | ||
70 | gid_t gid; | ||
71 | ExecStatusType stat; | ||
72 | int ntuples, nfields; | ||
73 | int rc = 1; | ||
74 | |||
75 | stat = PQresultStatus (res); | ||
76 | |||
77 | if (stat != PGRES_TUPLES_OK) | ||
78 | { | ||
79 | mu_error (_("PQexec status: %s"), PQresStatus(stat)); | ||
80 | return 1; | ||
81 | } | ||
82 | |||
83 | ntuples = PQntuples (res); | ||
84 | nfields = PQnfields (res); | ||
85 | if ((ntuples > 1 && nfields) || ntuples == 0) | ||
86 | { | ||
87 | mu_error (ngettext("query returned %d tuple: %s", | ||
88 | "query returned %d tuples: %s", | ||
89 | ntuples), | ||
90 | ntuples, query_str); | ||
91 | if (ntuples == 0) | ||
92 | return 1; | ||
93 | } | ||
94 | |||
95 | if (nfields < 6) | ||
96 | { | ||
97 | mu_error (ngettext("query returned %d field: %s", | ||
98 | "query returned %d fields: %s", | ||
99 | nfields), | ||
100 | nfields, query_str); | ||
101 | return 1; | ||
102 | } | ||
103 | |||
104 | user_name = chop (PQgetvalue (res, 0, 0)); | ||
105 | passwd = chop (PQgetvalue (res, 0, 1)); | ||
106 | uid = strtoul (PQgetvalue (res, 0, 2), NULL, 0); | ||
107 | gid = strtoul (PQgetvalue (res, 0, 3), NULL, 0); | ||
108 | homedir = chop (PQgetvalue (res, 0, 4)); | ||
109 | shell = chop (PQgetvalue (res, 0, 5)); | ||
110 | |||
111 | if (nfields == 7) | ||
112 | { | ||
113 | mailbox_name = strdup (chop (PQgetvalue (res, 0, 6))); | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | mailbox_name = malloc (strlen (mu_path_maildir) + | ||
118 | strlen (user_name) + 1); | ||
119 | if (mailbox_name) | ||
120 | sprintf (mailbox_name, "%s%s", mu_path_maildir, user_name); | ||
121 | } | ||
122 | |||
123 | if (mailbox_name) | ||
124 | rc = mu_auth_data_alloc (auth, | ||
125 | user_name, | ||
126 | passwd, | ||
127 | uid, | ||
128 | gid, | ||
129 | "PGsql User", | ||
130 | homedir, | ||
131 | shell, | ||
132 | mailbox_name, | ||
133 | 1); | ||
134 | free (mailbox_name); | ||
135 | return rc; | ||
136 | } | ||
137 | |||
138 | int | ||
139 | pg_auth_sql_by_name (struct mu_auth_data **return_data, | ||
140 | const void *key, | ||
141 | void *func_data ARG_UNUSED, | ||
142 | void *call_data ARG_UNUSED) | ||
143 | { | ||
144 | char *query_str = NULL; | ||
145 | int rc; | ||
146 | PGconn *conn; | ||
147 | PGresult *res = NULL; | ||
148 | |||
149 | if (!key) | ||
150 | { | ||
151 | errno = EINVAL; | ||
152 | return 1; | ||
153 | } | ||
154 | |||
155 | conn = pg_connect (); | ||
156 | if (!conn) | ||
157 | { | ||
158 | /* Error message already issued */ | ||
159 | return 1; | ||
160 | } | ||
161 | |||
162 | query_str = mu_sql_expand_query (mu_sql_getpwnam_query, key); | ||
163 | |||
164 | if (!query_str) | ||
165 | { | ||
166 | PQfinish (conn); | ||
167 | return 1; | ||
168 | } | ||
169 | |||
170 | res = PQexec (conn, query_str); | ||
171 | if (res == NULL) | ||
172 | { | ||
173 | mu_error ("PQexec: %s", PQerrorMessage (conn)); | ||
174 | rc = 1; | ||
175 | } | ||
176 | else | ||
177 | { | ||
178 | rc = pg_auth_common (res, query_str, return_data); | ||
179 | PQclear(res); | ||
180 | } | ||
181 | |||
182 | free (query_str); | ||
183 | PQfinish (conn); | ||
184 | |||
185 | return rc; | ||
186 | } | ||
187 | |||
188 | int | ||
189 | pg_auth_sql_by_uid (struct mu_auth_data **return_data, | ||
190 | const void *key, | ||
191 | void *func_data ARG_UNUSED, | ||
192 | void *call_data ARG_UNUSED) | ||
193 | { | ||
194 | char uidstr[64]; | ||
195 | char *query_str = NULL; | ||
196 | int rc; | ||
197 | PGconn *conn; | ||
198 | PGresult *res = NULL; | ||
199 | |||
200 | if (!key) | ||
201 | { | ||
202 | errno = EINVAL; | ||
203 | return 1; | ||
204 | } | ||
205 | |||
206 | conn = pg_connect (); | ||
207 | if (!conn) | ||
208 | { | ||
209 | /* Error message already issued */ | ||
210 | return 1; | ||
211 | } | ||
212 | |||
213 | snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key); | ||
214 | query_str = mu_sql_expand_query (mu_sql_getpwuid_query, uidstr); | ||
215 | if (!query_str) | ||
216 | { | ||
217 | PQfinish (conn); | ||
218 | return 1; | ||
219 | } | ||
220 | |||
221 | res = PQexec (conn, query_str); | ||
222 | if (res == NULL) | ||
223 | { | ||
224 | mu_error ("PQexec: %s", PQerrorMessage (conn)); | ||
225 | rc = 1; | ||
226 | } | ||
227 | else | ||
228 | { | ||
229 | rc = pg_auth_common (res, query_str, return_data); | ||
230 | PQclear(res); | ||
231 | } | ||
232 | |||
233 | free (query_str); | ||
234 | PQfinish (conn); | ||
235 | |||
236 | return rc; | ||
237 | } | ||
238 | |||
239 | int | ||
240 | pg_sql_authenticate (struct mu_auth_data **return_data ARG_UNUSED, | ||
241 | const void *key, | ||
242 | void *func_data ARG_UNUSED, void *call_data) | ||
243 | { | ||
244 | PGconn *conn; | ||
245 | PGresult *res = NULL; | ||
246 | |||
247 | const struct mu_auth_data *auth_data = key; | ||
248 | char *pass = call_data; | ||
249 | char *query_str = NULL; | ||
250 | int rc = 1; | ||
251 | |||
252 | if (!auth_data) | ||
253 | return 1; | ||
254 | |||
255 | conn = pg_connect (); | ||
256 | if (!conn) | ||
257 | { | ||
258 | /* Error message already issued */ | ||
259 | return 1; | ||
260 | } | ||
261 | |||
262 | query_str = mu_sql_expand_query (mu_sql_getpass_query, auth_data->name); | ||
263 | if (!query_str) | ||
264 | { | ||
265 | PQfinish (conn); | ||
266 | return 1; | ||
267 | } | ||
268 | |||
269 | res = PQexec (conn, query_str); | ||
270 | if (res == NULL) | ||
271 | { | ||
272 | mu_error ("PQexec: %s", PQerrorMessage (conn)); | ||
273 | } | ||
274 | else | ||
275 | { | ||
276 | ExecStatusType stat = PQresultStatus (res); | ||
277 | |||
278 | if (stat != PGRES_TUPLES_OK) | ||
279 | mu_error (_("PQexec status: %s"), PQresStatus(stat)); | ||
280 | else | ||
281 | { | ||
282 | char *p; | ||
283 | int ntuples = PQntuples (res); | ||
284 | int nfields = PQnfields (res); | ||
285 | if ((ntuples > 1 && nfields) || ntuples == 0) | ||
286 | { | ||
287 | mu_error (ngettext("query returned %d tuple: %s", | ||
288 | "query returned %d tuples: %s", | ||
289 | ntuples), | ||
290 | ntuples, query_str); | ||
291 | if (ntuples == 0) | ||
292 | return 1; | ||
293 | } | ||
294 | |||
295 | if (nfields > 1) | ||
296 | { | ||
297 | mu_error (ngettext("query returned %d field: %s", | ||
298 | "query returned %d fields: %s", | ||
299 | nfields), | ||
300 | nfields, query_str); | ||
301 | } | ||
302 | |||
303 | p = chop (PQgetvalue (res, 0, 0)); | ||
304 | rc = strcmp (p, crypt (pass, p)); | ||
305 | } | ||
306 | PQclear (res); | ||
307 | } | ||
308 | |||
309 | free (query_str); | ||
310 | PQfinish (conn); | ||
311 | |||
312 | return rc; | ||
313 | } | ||
314 | #endif |
-
Please register or sign in to post a comment