Commit 791a6cc4 791a6cc4039fcd808620f4c92592030b5b7c5141 by Sergey Poznyakoff

(getMpwnam): Return username along with

other data.
(getMpwuid): New function.
1 parent 32d4508e
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 extern void *xmalloc (size_t); 18 extern void *xmalloc (size_t);
19 19
20 static char * 20 static char *
21 sql_expand_query (const char *query, const char *username) 21 sql_expand_query (const char *query, const char *ustr)
22 { 22 {
23 char *p, *q, *res; 23 char *p, *q, *res;
24 int len; 24 int len;
...@@ -33,7 +33,7 @@ sql_expand_query (const char *query, const char *username) ...@@ -33,7 +33,7 @@ sql_expand_query (const char *query, const char *username)
33 { 33 {
34 if (p[1] == 'u') 34 if (p[1] == 'u')
35 { 35 {
36 len += strlen (username); 36 len += strlen (ustr);
37 p += 2; 37 p += 2;
38 } 38 }
39 else if (p[1] == '%') 39 else if (p[1] == '%')
...@@ -65,7 +65,7 @@ sql_expand_query (const char *query, const char *username) ...@@ -65,7 +65,7 @@ sql_expand_query (const char *query, const char *username)
65 switch (*++p) 65 switch (*++p)
66 { 66 {
67 case 'u': 67 case 'u':
68 strcpy (q, username); 68 strcpy (q, ustr);
69 q += strlen (q); 69 q += strlen (q);
70 p++; 70 p++;
71 break; 71 break;
...@@ -147,23 +147,110 @@ getMpwnam (const char *username) ...@@ -147,23 +147,110 @@ getMpwnam (const char *username)
147 147
148 if (!tpw) 148 if (!tpw)
149 tpw = (struct passwd *)xmalloc (sizeof (struct passwd)); 149 tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
150 tpw->pw_name = xmalloc (strlen (username)+1); 150 tpw->pw_name = xmalloc (strlen (row[0])+1);
151 strcpy (tpw->pw_name, username); 151 strcpy (tpw->pw_name, row[0]);
152 152
153 tpw->pw_passwd = xmalloc (strlen (row[0])+1); 153 tpw->pw_passwd = xmalloc (strlen (row[1])+1);
154 strcpy (tpw->pw_passwd, row[1]);
155
156 tpw->pw_uid = atoi (row[2]);
157 tpw->pw_gid = atoi (row[3]);
158
159 tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
160 strcpy (tpw->pw_gecos, "Mysql User");
161
162 tpw->pw_dir = xmalloc (strlen (row[4])+1);
163 strcpy (tpw->pw_dir, row[4]);
164
165 tpw->pw_shell = xmalloc (strlen (row[5])+1);
166 strcpy (tpw->pw_shell, row[5]);
167
168 mysql_free_result (res);
169 mysql_close (m);
170 return tpw;
171 }
172
173 struct passwd *
174 getMpwuid (uid_t uid)
175 {
176 char *QueryStr = NULL;
177 MYSQL *m;
178 MYSQL_RES *res;
179 MYSQL_ROW row;
180 static struct passwd *tpw;
181 char uidstr[64];
182
183 if (tpw)
184 {
185 free (tpw->pw_name);
186 free (tpw->pw_passwd);
187 free (tpw->pw_gecos);
188 free (tpw->pw_dir);
189 free (tpw->pw_shell);
190 }
191
192 m = mysql_init (0);
193
194 if (!m)
195 return NULL;
196
197 if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
198 sql_socket, MFLAGS))
199 {
200 mu_error ("MySQL: connect failed: %s", mysql_error (m));
201 mysql_close (m);
202 return NULL;
203 }
204
205 snprintf (uidstr, sizeof (uidstr), "%u", uid);
206 QueryStr = sql_expand_query (sql_getpwuid_query, uidstr);
207
208 if (!QueryStr)
209 {
210 mysql_close (m);
211 return NULL;
212 }
213
214 if (mysql_query (m, QueryStr) != 0)
215 {
216 mu_error ("MySQL: query failed: %s", mysql_error (m));
217 mysql_close (m);
218 return NULL;
219 }
220
221 if ((res = mysql_store_result (m)) == NULL)
222 {
223 mu_error ("MySQL: can't store result: %s", mysql_error (m));
224 mysql_close (m);
225 return NULL;
226 }
227
228 if ((row = mysql_fetch_row (res)) == NULL)
229 {
230 mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
231 mysql_close (m);
232 return NULL;
233 }
234
235 if (!tpw)
236 tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
237 tpw->pw_name = xmalloc (strlen (row[0]+1));
238 strcpy (tpw->pw_name, row[0]);
239
240 tpw->pw_passwd = xmalloc (strlen (row[1])+1);
154 strcpy (tpw->pw_passwd, row[0]); 241 strcpy (tpw->pw_passwd, row[0]);
155 242
156 tpw->pw_uid = atoi (row[1]); 243 tpw->pw_uid = atoi (row[2]);
157 tpw->pw_gid = atoi (row[2]); 244 tpw->pw_gid = atoi (row[3]);
158 245
159 tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1); 246 tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
160 strcpy (tpw->pw_gecos, "Mysql User"); 247 strcpy (tpw->pw_gecos, "Mysql User");
161 248
162 tpw->pw_dir = xmalloc (strlen (row[3])+1); 249 tpw->pw_dir = xmalloc (strlen (row[4])+1);
163 strcpy (tpw->pw_dir, row[3]); 250 strcpy (tpw->pw_dir, row[4]);
164 251
165 tpw->pw_shell = xmalloc (strlen (row[4])+1); 252 tpw->pw_shell = xmalloc (strlen (row[5])+1);
166 strcpy (tpw->pw_shell, row[4]); 253 strcpy (tpw->pw_shell, row[5]);
167 254
168 mysql_free_result (res); 255 mysql_free_result (res);
169 mysql_close (m); 256 mysql_close (m);
......