Commit 791a6cc4 791a6cc4039fcd808620f4c92592030b5b7c5141 by Sergey Poznyakoff

(getMpwnam): Return username along with

other data.
(getMpwuid): New function.
1 parent 32d4508e
......@@ -18,7 +18,7 @@
extern void *xmalloc (size_t);
static char *
sql_expand_query (const char *query, const char *username)
sql_expand_query (const char *query, const char *ustr)
{
char *p, *q, *res;
int len;
......@@ -33,7 +33,7 @@ sql_expand_query (const char *query, const char *username)
{
if (p[1] == 'u')
{
len += strlen (username);
len += strlen (ustr);
p += 2;
}
else if (p[1] == '%')
......@@ -65,7 +65,7 @@ sql_expand_query (const char *query, const char *username)
switch (*++p)
{
case 'u':
strcpy (q, username);
strcpy (q, ustr);
q += strlen (q);
p++;
break;
......@@ -147,23 +147,110 @@ getMpwnam (const char *username)
if (!tpw)
tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
tpw->pw_name = xmalloc (strlen (username)+1);
strcpy (tpw->pw_name, username);
tpw->pw_name = xmalloc (strlen (row[0])+1);
strcpy (tpw->pw_name, row[0]);
tpw->pw_passwd = xmalloc (strlen (row[0])+1);
tpw->pw_passwd = xmalloc (strlen (row[1])+1);
strcpy (tpw->pw_passwd, row[1]);
tpw->pw_uid = atoi (row[2]);
tpw->pw_gid = atoi (row[3]);
tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
strcpy (tpw->pw_gecos, "Mysql User");
tpw->pw_dir = xmalloc (strlen (row[4])+1);
strcpy (tpw->pw_dir, row[4]);
tpw->pw_shell = xmalloc (strlen (row[5])+1);
strcpy (tpw->pw_shell, row[5]);
mysql_free_result (res);
mysql_close (m);
return tpw;
}
struct passwd *
getMpwuid (uid_t uid)
{
char *QueryStr = NULL;
MYSQL *m;
MYSQL_RES *res;
MYSQL_ROW row;
static struct passwd *tpw;
char uidstr[64];
if (tpw)
{
free (tpw->pw_name);
free (tpw->pw_passwd);
free (tpw->pw_gecos);
free (tpw->pw_dir);
free (tpw->pw_shell);
}
m = mysql_init (0);
if (!m)
return NULL;
if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
sql_socket, MFLAGS))
{
mu_error ("MySQL: connect failed: %s", mysql_error (m));
mysql_close (m);
return NULL;
}
snprintf (uidstr, sizeof (uidstr), "%u", uid);
QueryStr = sql_expand_query (sql_getpwuid_query, uidstr);
if (!QueryStr)
{
mysql_close (m);
return NULL;
}
if (mysql_query (m, QueryStr) != 0)
{
mu_error ("MySQL: query failed: %s", mysql_error (m));
mysql_close (m);
return NULL;
}
if ((res = mysql_store_result (m)) == NULL)
{
mu_error ("MySQL: can't store result: %s", mysql_error (m));
mysql_close (m);
return NULL;
}
if ((row = mysql_fetch_row (res)) == NULL)
{
mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
mysql_close (m);
return NULL;
}
if (!tpw)
tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
tpw->pw_name = xmalloc (strlen (row[0]+1));
strcpy (tpw->pw_name, row[0]);
tpw->pw_passwd = xmalloc (strlen (row[1])+1);
strcpy (tpw->pw_passwd, row[0]);
tpw->pw_uid = atoi (row[1]);
tpw->pw_gid = atoi (row[2]);
tpw->pw_uid = atoi (row[2]);
tpw->pw_gid = atoi (row[3]);
tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
strcpy (tpw->pw_gecos, "Mysql User");
tpw->pw_dir = xmalloc (strlen (row[3])+1);
strcpy (tpw->pw_dir, row[3]);
tpw->pw_dir = xmalloc (strlen (row[4])+1);
strcpy (tpw->pw_dir, row[4]);
tpw->pw_shell = xmalloc (strlen (row[4])+1);
strcpy (tpw->pw_shell, row[4]);
tpw->pw_shell = xmalloc (strlen (row[5])+1);
strcpy (tpw->pw_shell, row[5]);
mysql_free_result (res);
mysql_close (m);
......