Commit 5130f6bc 5130f6bc60773a7adb1c0b04a8c29f8aa5ad173a by Sergey Poznyakoff

Functionality merged into auth/sql.c

1 parent d4627cb0
1 Makefile.in
2 Makefile
3 .deps
4 .libs
1 noinst_LIBRARIES = @BUILD_LIBMYSQL@
2 EXTRA_LIBRARIES = libmailMysql.a
3
4 libmailMysql_a_SOURCES = MySql.c
5
6 noinst_HEADERS = MySql.h
7 INCLUDES = -I$(top_srcdir)/include
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <pwd.h>
7
8 #include <mailutils/error.h>
9
10 #ifdef HAVE_MYSQL
11
12 #ifdef HAVE_SHADOW_H
13 #include <shadow.h>
14 #endif /* HAVE_SHADOW_H */
15
16 #include <mysql/mysql.h>
17 #include "MySql.h"
18
19 extern void *xmalloc (size_t);
20
21 static char *
22 sql_expand_query (const char *query, const char *ustr)
23 {
24 char *p, *q, *res;
25 int len;
26
27 if (!query)
28 return NULL;
29
30 /* Compute resulting query length */
31 for (len = 0, p = (char *) query; *p; )
32 {
33 if (*p == '%')
34 {
35 if (p[1] == 'u')
36 {
37 len += strlen (ustr);
38 p += 2;
39 }
40 else if (p[1] == '%')
41 {
42 len++;
43 p += 2;
44 }
45 else
46 {
47 len++;
48 p++;
49 }
50 }
51 else
52 {
53 len++;
54 p++;
55 }
56 }
57
58 res = malloc (len + 1);
59 if (!res)
60 return res;
61
62 for (p = (char *) query, q = res; *p; )
63 {
64 if (*p == '%')
65 {
66 switch (*++p)
67 {
68 case 'u':
69 strcpy (q, ustr);
70 q += strlen (q);
71 p++;
72 break;
73
74 case '%':
75 *q++ = *p++;
76 break;
77
78 default:
79 *q++ = *p++;
80 }
81 }
82 else
83 *q++ = *p++;
84 }
85 *q = 0;
86 return res;
87 }
88
89 struct passwd *
90 getMpwnam (const char *username)
91 {
92 char *QueryStr = NULL;
93 MYSQL *m;
94 MYSQL_RES *res;
95 MYSQL_ROW row;
96 static struct passwd *tpw;
97
98 if (tpw)
99 {
100 free (tpw->pw_name);
101 free (tpw->pw_passwd);
102 free (tpw->pw_gecos);
103 free (tpw->pw_dir);
104 free (tpw->pw_shell);
105 }
106
107 m = mysql_init (0);
108
109 if (!m)
110 return NULL;
111
112 if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
113 sql_socket, MFLAGS))
114 {
115 mu_error ("MySQL: connect failed: %s", mysql_error (m));
116 mysql_close (m);
117 return NULL;
118 }
119
120 QueryStr = sql_expand_query (sql_getpwnam_query, username);
121
122 if (!QueryStr)
123 {
124 mysql_close (m);
125 return NULL;
126 }
127
128 if (mysql_query (m, QueryStr) != 0)
129 {
130 mu_error ("MySQL: query failed: %s", mysql_error (m));
131 mysql_close (m);
132 return NULL;
133 }
134
135 if ((res = mysql_store_result (m)) == NULL)
136 {
137 mu_error ("MySQL: can't store result: %s", mysql_error (m));
138 mysql_close (m);
139 return NULL;
140 }
141
142 if ((row = mysql_fetch_row (res)) == NULL)
143 {
144 mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
145 mysql_close (m);
146 return NULL;
147 }
148
149 if (!tpw)
150 tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
151 tpw->pw_name = xmalloc (strlen (row[0])+1);
152 strcpy (tpw->pw_name, row[0]);
153
154 tpw->pw_passwd = xmalloc (strlen (row[1])+1);
155 strcpy (tpw->pw_passwd, row[1]);
156
157 tpw->pw_uid = atoi (row[2]);
158 tpw->pw_gid = atoi (row[3]);
159
160 tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
161 strcpy (tpw->pw_gecos, "Mysql User");
162
163 tpw->pw_dir = xmalloc (strlen (row[4])+1);
164 strcpy (tpw->pw_dir, row[4]);
165
166 tpw->pw_shell = xmalloc (strlen (row[5])+1);
167 strcpy (tpw->pw_shell, row[5]);
168
169 mysql_free_result (res);
170 mysql_close (m);
171 return tpw;
172 }
173
174 struct passwd *
175 getMpwuid (uid_t *puid)
176 {
177 char *QueryStr = NULL;
178 MYSQL *m;
179 MYSQL_RES *res;
180 MYSQL_ROW row;
181 static struct passwd *tpw;
182 char uidstr[64];
183
184 if (tpw)
185 {
186 free (tpw->pw_name);
187 free (tpw->pw_passwd);
188 free (tpw->pw_gecos);
189 free (tpw->pw_dir);
190 free (tpw->pw_shell);
191 }
192
193 m = mysql_init (0);
194
195 if (!m)
196 return NULL;
197
198 if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
199 sql_socket, MFLAGS))
200 {
201 mu_error ("MySQL: connect failed: %s", mysql_error (m));
202 mysql_close (m);
203 return NULL;
204 }
205
206 snprintf (uidstr, sizeof (uidstr), "%u", *puid);
207 QueryStr = sql_expand_query (sql_getpwuid_query, uidstr);
208
209 if (!QueryStr)
210 {
211 mysql_close (m);
212 return NULL;
213 }
214
215 if (mysql_query (m, QueryStr) != 0)
216 {
217 mu_error ("MySQL: query failed: %s", mysql_error (m));
218 mysql_close (m);
219 return NULL;
220 }
221
222 if ((res = mysql_store_result (m)) == NULL)
223 {
224 mu_error ("MySQL: can't store result: %s", mysql_error (m));
225 mysql_close (m);
226 return NULL;
227 }
228
229 if ((row = mysql_fetch_row (res)) == NULL)
230 {
231 mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
232 mysql_close (m);
233 return NULL;
234 }
235
236 if (!tpw)
237 tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
238 tpw->pw_name = xmalloc (strlen (row[0]+1));
239 strcpy (tpw->pw_name, row[0]);
240
241 tpw->pw_passwd = xmalloc (strlen (row[1])+1);
242 strcpy (tpw->pw_passwd, row[0]);
243
244 tpw->pw_uid = atoi (row[2]);
245 tpw->pw_gid = atoi (row[3]);
246
247 tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
248 strcpy (tpw->pw_gecos, "Mysql User");
249
250 tpw->pw_dir = xmalloc (strlen (row[4])+1);
251 strcpy (tpw->pw_dir, row[4]);
252
253 tpw->pw_shell = xmalloc (strlen (row[5])+1);
254 strcpy (tpw->pw_shell, row[5]);
255
256 mysql_free_result (res);
257 mysql_close (m);
258 return tpw;
259 }
260
261 #ifdef HAVE_SHADOW_H
262
263 struct spwd *
264 getMspnam (const char *username)
265 {
266 char *QueryStr = NULL;
267 MYSQL *m;
268 MYSQL_RES *res;
269 MYSQL_ROW row;
270 static struct spwd *tpw;
271
272 if (tpw)
273 {
274 free (tpw->sp_namp);
275 free (tpw->sp_pwdp);
276 }
277
278 m = mysql_init (0);
279
280 if (!m)
281 return NULL;
282
283 if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
284 sql_socket, MFLAGS))
285 {
286 mu_error ("MySQL: connect failed: %s", mysql_error (m));
287 mysql_close (m);
288 return NULL;
289 }
290
291 QueryStr = sql_expand_query (sql_getpass_query, username);
292
293 if (!QueryStr)
294 {
295 mysql_close (m);
296 return NULL;
297 }
298
299 if (mysql_query (m, QueryStr) != 0)
300 {
301 mu_error ("MySQL: query failed: %s", mysql_error (m));
302 mysql_close (m);
303 return NULL;
304 }
305
306 if ((res = mysql_store_result (m)) == NULL)
307 {
308 mu_error ("MySQL: can't store result: %s", mysql_error (m));
309 mysql_close (m);
310 return NULL;
311 }
312
313 if ((row = mysql_fetch_row (res)) == NULL)
314 {
315 mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
316 mysql_close (m);
317 return NULL;
318 }
319
320 if (!tpw)
321 tpw = (struct spwd *)xmalloc (sizeof (struct spwd));
322
323 tpw->sp_namp = xmalloc (strlen (username)+1);
324 strcpy (tpw->sp_namp, username);
325
326 tpw->sp_pwdp = xmalloc (strlen (row[0])+1);
327 strcpy (tpw->sp_pwdp, row[0]);
328
329 tpw->sp_lstchg = 11428;
330 tpw->sp_min = 0;
331 tpw->sp_max = 99999;
332 tpw->sp_warn = 7;
333
334 mysql_free_result (res);
335 mysql_close (m);
336 return tpw;
337 }
338
339 #endif /* HAVE_SHADOW_H */
340
341 #endif /* HAVE_MYSQL */
1 #include <config.h>
2
3 #ifdef HAVE_MYSQL
4 extern char *sql_getpwnam_query;
5 extern char *sql_getpass_query;
6 extern char *sql_getpwuid_query;
7 extern char *sql_host;
8 extern char *sql_user;
9 extern char *sql_passwd;
10 extern char *sql_db;
11 extern char *sql_socket;
12 extern int sql_port;
13
14 struct passwd *getMpwnam (const char *username);
15 struct passwd *getMpwuid (uid_t *puid);
16 struct spwd *getMspnam (const char *username);
17
18 #define MHOST NULL /* Hostname to connect to. NULL for UNIX
19 socket connection */
20 #define MPORT 0 /* Port number to connect to. 0 means default
21 MySQL port (3300) */
22 #define MSOCKET NULL /* Socket name to use. Valid only if
23 connecting via UNIX sockets */
24 #define MFLAGS 0 /* Special user flags. It is safe to leave
25 this untouched */
26 #define MUSER "accounts" /* Username for mysql access */
27 #define MPASS "yurpass" /* Password for mysql access */
28 #define MDB "accounts" /* Database Name */
29
30
31 #endif /* HAVE_MYSQL */