Commit 89da3111 89da3111b019f5994af9abcbeb5362f8bf141526 by Sergey Poznyakoff

Added to the repository

1 parent 82f8b6cd
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 (void *return_data, void *key,
33 void *func_data ARG_UNUSED,
34 void *call_data ARG_UNUSED)
35 {
36 char *query_str = NULL;
37 MYSQL *m;
38 MYSQL_RES *res;
39 MYSQL_ROW row;
40 char *mailbox_name;
41 int rc;
42
43 if (!key)
44 {
45 errno = EINVAL;
46 return 1;
47 }
48
49 m = mysql_init (0);
50
51 if (!m)
52 return 1;
53
54 if (!mysql_real_connect (m,
55 mu_sql_host, mu_sql_user, mu_sql_passwd,
56 mu_sql_db, mu_sql_port,
57 mu_sql_socket, MFLAGS))
58 {
59 mu_error (_("MySQL: connect failed: %s"), mysql_error (m));
60 mysql_close (m);
61 return 1;
62 }
63
64 query_str = mu_sql_expand_query (mu_sql_getpwnam_query, key);
65
66 if (!query_str)
67 {
68 mysql_close (m);
69 return 1;
70 }
71
72 if (mysql_query (m, query_str) != 0)
73 {
74 mu_error (_("MySQL: query failed: %s"), mysql_error (m));
75 free (query_str);
76 mysql_close (m);
77 return 1;
78 }
79
80 free (query_str);
81
82 if ((res = mysql_store_result (m)) == NULL)
83 {
84 mu_error (_("MySQL: can't store result: %s"), mysql_error (m));
85 mysql_close (m);
86 return 1;
87 }
88
89 if ((row = mysql_fetch_row (res)) == NULL)
90 {
91 mu_error (_("MySQL: can't fetch row: %s"), mysql_error (m));
92 mysql_close (m);
93 return 1;
94 }
95
96 if (mysql_num_fields (res) == 7 && row[6])
97 mailbox_name = strdup (row[6]);
98 else
99 {
100 mailbox_name = malloc (strlen (mu_path_maildir) +
101 strlen (row[0]) + 1);
102 if (mailbox_name)
103 sprintf (mailbox_name, "%s%s", mu_path_maildir, row[0]);
104 }
105
106 if (mailbox_name)
107 rc = mu_auth_data_alloc ((struct mu_auth_data **) return_data,
108 row[0],
109 row[1],
110 atoi (row[2]),
111 atoi (row[3]),
112 "Mysql User",
113 row[4],
114 row[5],
115 mailbox_name,
116 1);
117 else
118 rc = 1;
119
120 free (mailbox_name);
121 mysql_free_result (res);
122 mysql_close (m);
123
124 return rc;
125 }
126
127 int
128 mysql_auth_sql_by_uid (void *return_data, void *key,
129 void *func_data ARG_UNUSED,
130 void *call_data ARG_UNUSED)
131 {
132 char *query_str = NULL;
133 MYSQL *m;
134 MYSQL_RES *res;
135 MYSQL_ROW row;
136 char uidstr[64];
137 char *mailbox_name;
138 int rc;
139
140 if (!key)
141 {
142 errno = EINVAL;
143 return 1;
144 }
145
146 m = mysql_init (0);
147
148 if (!m)
149 return 1;
150
151 if (!mysql_real_connect (m,
152 mu_sql_host, mu_sql_user,
153 mu_sql_passwd, mu_sql_db,
154 mu_sql_port, mu_sql_socket, MFLAGS))
155 {
156 mu_error (_("MySQL: connect failed: %s"), mysql_error (m));
157 mysql_close (m);
158 return 1;
159 }
160
161 snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key);
162 query_str = mu_sql_expand_query (mu_sql_getpwuid_query, uidstr);
163
164 if (!query_str)
165 {
166 mysql_close (m);
167 return 1;
168 }
169
170 if (mysql_query (m, query_str) != 0)
171 {
172 mu_error (_("MySQL: query failed: %s"), mysql_error (m));
173 free (query_str);
174 mysql_close (m);
175 return 1;
176 }
177
178 free (query_str);
179
180 if ((res = mysql_store_result (m)) == NULL)
181 {
182 mu_error (_("MySQL: can't store result: %s"), mysql_error (m));
183 mysql_close (m);
184 return 1;
185 }
186
187 if ((row = mysql_fetch_row (res)) == NULL)
188 {
189 mu_error (_("MySQL: can't fetch row: %s"), mysql_error (m));
190 mysql_close (m);
191 return 1;
192 }
193
194 if (mysql_num_fields (res) == 7 && row[6])
195 mailbox_name = strdup (row[6]);
196 else
197 {
198 mailbox_name = malloc (strlen (mu_path_maildir) +
199 strlen (row[0]) + 1);
200 if (mailbox_name)
201 sprintf (mailbox_name, "%s%s", mu_path_maildir, row[0]);
202 }
203
204 if (mailbox_name)
205 rc = mu_auth_data_alloc ((struct mu_auth_data **) return_data,
206 row[0],
207 row[1],
208 atoi (row[2]),
209 atoi (row[3]),
210 "Mysql User",
211 row[4],
212 row[5],
213 mailbox_name,
214 1);
215 else
216 rc = 1;
217
218 free (mailbox_name);
219 mysql_free_result (res);
220 mysql_close (m);
221
222 return rc;
223 }
224
225 int
226 mysql_sql_authenticate (void *return_data ARG_UNUSED, void *key,
227 void *func_data ARG_UNUSED, void *call_data)
228 {
229 struct mu_auth_data *auth_data = key;
230 char *pass = call_data;
231 char *query_str = NULL;
232 MYSQL *m;
233 MYSQL_RES *res;
234 MYSQL_ROW row;
235 int rc;
236
237 if (!auth_data)
238 return 1;
239
240 m = mysql_init (0);
241
242 if (!m)
243 return 1;
244
245 if (!mysql_real_connect (m,
246 mu_sql_host, mu_sql_user,
247 mu_sql_passwd, mu_sql_db,
248 mu_sql_port,
249 mu_sql_socket, MFLAGS))
250 {
251 mu_error (_("MySQL: connect failed: %s"), mysql_error (m));
252 mysql_close (m);
253 return 1;
254 }
255
256 query_str = mu_sql_expand_query (mu_sql_getpass_query, auth_data->name);
257
258 if (!query_str)
259 {
260 mysql_close (m);
261 return 1;
262 }
263
264 if (mysql_query (m, query_str) != 0)
265 {
266 mu_error (_("MySQL: query failed: %s"), mysql_error (m));
267 free (query_str);
268 mysql_close (m);
269 return 1;
270 }
271
272 free (query_str);
273
274 if ((res = mysql_store_result (m)) == NULL)
275 {
276 mu_error (_("MySQL: can't store result: %s"), mysql_error (m));
277 mysql_close (m);
278 return 1;
279 }
280
281 if ((row = mysql_fetch_row (res)) == NULL)
282 {
283 mu_error (_("MySQL: can't fetch row: %s"), mysql_error (m));
284 mysql_close (m);
285 return 1;
286 }
287
288 rc = strcmp (row[0], crypt (pass, row[0]));
289 mysql_free_result (res);
290 mysql_close (m);
291 return rc;
292 }
293
294 #endif
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
26 static PGconn *
27 pg_connect ()
28 {
29 PGconn *pgconn;
30 char portbuf[16];
31 char *portstr;
32
33 if (mu_sql_port == 0)
34 portstr = NULL;
35 else
36 {
37 portstr = portbuf;
38 snprintf (portbuf, sizeof (portbuf), "%d", mu_sql_port);
39 }
40
41 pgconn = PQsetdbLogin (mu_sql_host, portstr, NULL, NULL,
42 mu_sql_db, mu_sql_user, mu_sql_passwd);
43
44 if (PQstatus (pgconn) == CONNECTION_BAD)
45 {
46 mu_error("PQconnectStart: %s", PQerrorMessage (pgconn));
47 PQfinish (pgconn);
48 return NULL;
49 }
50 return pgconn;
51 }
52
53 static char *
54 chop (char *str)
55 {
56 int len;
57
58 for (len = strlen(str); len > 0 && isspace(str[len-1]); len--)
59 ;
60 str[len] = 0;
61 return str;
62 }
63
64 static int
65 pg_auth_common (PGresult *res, char *query_str, struct mu_auth_data **auth)
66 {
67 char *user_name, *passwd, *homedir, *shell, *mailbox_name;
68 uid_t uid;
69 gid_t gid;
70 ExecStatusType stat;
71 int ntuples, nfields;
72 int rc = 1;
73
74 stat = PQresultStatus (res);
75
76 if (stat != PGRES_TUPLES_OK)
77 {
78 mu_error (_("PQexec status: %s"), PQresStatus(stat));
79 return 1;
80 }
81
82 ntuples = PQntuples (res);
83 nfields = PQnfields (res);
84 if (ntuples > 1 && nfields)
85 {
86 mu_error (ngettext("query returned %d tuple: %s",
87 "query returned %d tuples: %s",
88 ntuples),
89 ntuples, query_str);
90 }
91
92 if (nfields < 6)
93 {
94 mu_error (ngettext("query returned %d field: %s",
95 "query returned %d fields: %s",
96 nfields),
97 nfields, query_str);
98 return 1;
99 }
100
101 user_name = chop (PQgetvalue (res, 0, 0));
102 passwd = chop (PQgetvalue (res, 0, 1));
103 uid = strtoul (PQgetvalue (res, 0, 2), NULL, 0);
104 gid = strtoul (PQgetvalue (res, 0, 3), NULL, 0);
105 homedir = chop (PQgetvalue (res, 0, 4));
106 shell = chop (PQgetvalue (res, 0, 5));
107
108 if (ntuples == 7)
109 {
110 mailbox_name = strdup (chop (PQgetvalue (res, 0, 6)));
111 }
112 else
113 {
114 mailbox_name = malloc (strlen (mu_path_maildir) +
115 strlen (user_name) + 1);
116 if (mailbox_name)
117 sprintf (mailbox_name, "%s%s", mu_path_maildir, user_name);
118 }
119
120 if (mailbox_name)
121 rc = mu_auth_data_alloc (auth,
122 user_name,
123 passwd,
124 uid,
125 gid,
126 "PGsql User",
127 homedir,
128 shell,
129 mailbox_name,
130 1);
131 free (mailbox_name);
132 return rc;
133 }
134
135 int
136 pg_auth_sql_by_name (void *return_data, void *key,
137 void *func_data ARG_UNUSED,
138 void *call_data ARG_UNUSED)
139 {
140 char *query_str = NULL;
141 int rc;
142 PGconn *conn;
143 PGresult *res = NULL;
144
145 if (!key)
146 {
147 errno = EINVAL;
148 return 1;
149 }
150
151 conn = pg_connect ();
152 if (!conn)
153 {
154 /* Error message already issued */
155 return 1;
156 }
157
158 query_str = mu_sql_expand_query (mu_sql_getpwnam_query, key);
159
160 if (!query_str)
161 {
162 PQfinish (conn);
163 return 1;
164 }
165
166 res = PQexec (conn, query_str);
167 if (res == NULL)
168 {
169 mu_error ("PQexec: %s", PQerrorMessage (conn));
170 rc = 1;
171 }
172 else
173 {
174 rc = pg_auth_common (res, query_str,
175 (struct mu_auth_data **)return_data);
176 PQclear(res);
177 }
178
179 free (query_str);
180 PQfinish (conn);
181
182 return rc;
183 }
184
185 int
186 pg_auth_sql_by_uid (void *return_data, void *key,
187 void *func_data ARG_UNUSED,
188 void *call_data ARG_UNUSED)
189 {
190 char uidstr[64];
191 char *query_str = NULL;
192 int rc;
193 PGconn *conn;
194 PGresult *res = NULL;
195
196 if (!key)
197 {
198 errno = EINVAL;
199 return 1;
200 }
201
202 conn = pg_connect ();
203 if (!conn)
204 {
205 /* Error message already issued */
206 return 1;
207 }
208
209 snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key);
210 query_str = mu_sql_expand_query (mu_sql_getpwuid_query, uidstr);
211 if (!query_str)
212 {
213 PQfinish (conn);
214 return 1;
215 }
216
217 res = PQexec (conn, query_str);
218 if (res == NULL)
219 {
220 mu_error ("PQexec: %s", PQerrorMessage (conn));
221 rc = 1;
222 }
223 else
224 {
225 rc = pg_auth_common (res, query_str,
226 (struct mu_auth_data **)return_data);
227 PQclear(res);
228 }
229
230 free (query_str);
231 PQfinish (conn);
232
233 return rc;
234 }
235
236 int
237 pg_sql_authenticate (void *return_data ARG_UNUSED, void *key,
238 void *func_data ARG_UNUSED, void *call_data)
239 {
240 PGconn *conn;
241 PGresult *res = NULL;
242
243 struct mu_auth_data *auth_data = key;
244 char *pass = call_data;
245 char *query_str = NULL;
246 int rc = 1;
247
248 if (!auth_data)
249 return 1;
250
251 conn = pg_connect ();
252 if (!conn)
253 {
254 /* Error message already issued */
255 return 1;
256 }
257
258 query_str = mu_sql_expand_query (mu_sql_getpass_query, auth_data->name);
259 if (!query_str)
260 {
261 PQfinish (conn);
262 return 1;
263 }
264
265 res = PQexec (conn, query_str);
266 if (res == NULL)
267 {
268 mu_error ("PQexec: %s", PQerrorMessage (conn));
269 }
270 else
271 {
272 ExecStatusType stat = PQresultStatus (res);
273
274 if (stat != PGRES_TUPLES_OK)
275 mu_error (_("PQexec status: %s"), PQresStatus(stat));
276 else
277 {
278 char *p;
279 int ntuples = PQntuples (res);
280 int nfields = PQnfields (res);
281 if (ntuples > 1 && nfields)
282 {
283 mu_error (ngettext("query returned %d tuple: %s",
284 "query returned %d tuples: %s",
285 ntuples),
286 ntuples, query_str);
287 }
288
289 if (nfields > 1)
290 {
291 mu_error (ngettext("query returned %d field: %s",
292 "query returned %d fields: %s",
293 nfields),
294 nfields, query_str);
295 }
296
297 p = chop (PQgetvalue (res, 0, 0));
298 rc = strcmp (p, crypt (pass, p));
299 }
300 PQclear (res);
301 }
302
303 free (query_str);
304 PQfinish (conn);
305
306 return rc;
307 }
308 #endif
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 #include <sys/types.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #define _XOPEN_SOURCE
22 #include <unistd.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #ifdef HAVE_CRYPT_H
26 # include <crypt.h>
27 #endif
28
29 #ifdef HAVE_STRINGS_H
30 # include <strings.h>
31 #endif
32 #include <string.h>
33
34 #include <mailutils/mu_auth.h>
35 #include <mailutils/error.h>
36 #include <mailutils/errno.h>
37 #include <mailutils/nls.h>
38 #include <mailutils/mailbox.h>
39
40 extern char *mu_sql_getpwnam_query;
41 extern char *mu_sql_getpass_query;
42 extern char *mu_sql_getpwuid_query;
43 extern char *mu_sql_host;
44 extern char *mu_sql_user;
45 extern char *mu_sql_passwd;
46 extern char *mu_sql_db;
47 extern char *mu_sql_socket;
48 extern int mu_sql_port;
49
50 extern char *mu_sql_expand_query __P((const char *query, const char *ustr));
51
1 dnl Arguments:
2 dnl $1 -- Library to look for
3 dnl $2 -- Function to check in the library
4 dnl $3 -- Any additional libraries that might be needed
5 dnl $4 -- Action to be taken when test succeeds
6 dnl $5 -- Action to be taken when test fails
7 dnl $6 -- Directories where the library may reside
8 AC_DEFUN(MU_CHECK_LIB,
9 [
10 save_LIBS=$LIBS
11 AC_CACHE_CHECK([for -l$1], mu_cv_lib_$1,
12 [
13 for path in $6
14 do
15 LIBS="$save_LIBS -L$path"
16 AC_CHECK_LIB($1, $2,
17 [mu_cv_lib_$1="$3 -L$path -l$1"
18 break],
19 [mu_cv_lib_$1=no],$3)
20 done
21 ])
22 MU_RESULT_ACTIONS([mu_cv_lib_$1],[LIB$1],[$4],[$5])
23 LIBS=$save_LIBS
24 ])
25
26