Blame view

MySql/MySql.c 6.33 KB
1 2 3
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
Alain Magloire authored
4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>


#ifdef HAVE_MYSQL

#ifdef HAVE_SHADOW_H
#include <shadow.h>
#endif /* HAVE_SHADOW_H */

#include <mysql/mysql.h>
#include "MySql.h"

18
extern void *xmalloc (size_t);
Alain Magloire authored
19

20
static char *
21
sql_expand_query (const char *query, const char *ustr)
22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
  char *p, *q, *res;
  int len;

  if (!query)
    return NULL;
  
  /* Compute resulting query length */
  for (len = 0, p = query; *p; )
    {
      if (*p == '%')
	{
	  if (p[1] == 'u')
	    {
36
	      len += strlen (ustr);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	      p += 2;
	    }
	  else if (p[1] == '%')
	    {
	      len++;
	      p += 2;
	    }
	  else
	    {
	      len++;
	      p++;
	    }
	}
      else
	{
	  len++;
	  p++;
	}
    }

  res = malloc (len + 1);
  if (!res)
    return res;

  for (p = query, q = res; *p; )
    {
      if (*p == '%')
	{
	  switch (*++p)
	    {
	    case 'u':
68
	      strcpy (q, ustr);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	      q += strlen (q);
	      p++;
	      break;
	      
	    case '%':
	      *q++ = *p++;
	      break;
	      
	    default:
	      *q++ = *p++;
	    }
	}
      else
	*q++ = *p++;
    }
  *q = 0;
  return res;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
struct passwd *
getMpwnam (const char *username)
{
  char *QueryStr = NULL;
  MYSQL *m;
  MYSQL_RES *res;
  MYSQL_ROW row;
  static struct passwd *tpw;

  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;

111 112
  if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
			   sql_socket, MFLAGS))
113 114 115 116 117
    {
      mu_error ("MySQL: connect failed: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
118 119

  QueryStr = sql_expand_query (sql_getpwnam_query, username);
120 121

  if (!QueryStr)
122 123 124 125
    {
      mysql_close (m);
      return NULL;
    }
126 127
  
  if (mysql_query (m, QueryStr) != 0)
128 129 130 131 132
    {
      mu_error ("MySQL: query failed: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
133 134

  if ((res = mysql_store_result (m)) == NULL)
135 136 137 138 139 140
    {
      mu_error ("MySQL: can't store result: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
  
141
  if ((row = mysql_fetch_row (res)) == NULL)
142 143 144 145 146 147
    {
      mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
  
148 149
  if (!tpw)
    tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
150 151
  tpw->pw_name = xmalloc (strlen (row[0])+1);
  strcpy (tpw->pw_name, row[0]);
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  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);
241 242
  strcpy (tpw->pw_passwd, row[0]);

243 244
  tpw->pw_uid = atoi (row[2]);
  tpw->pw_gid = atoi (row[3]);
Alain Magloire authored
245
	
246 247
  tpw->pw_gecos = xmalloc (strlen ("Mysql User")+1);
  strcpy (tpw->pw_gecos, "Mysql User");
Alain Magloire authored
248

249 250
  tpw->pw_dir = xmalloc (strlen (row[4])+1);
  strcpy (tpw->pw_dir, row[4]);
Alain Magloire authored
251

252 253
  tpw->pw_shell = xmalloc (strlen (row[5])+1);
  strcpy (tpw->pw_shell, row[5]);
Alain Magloire authored
254
	
255 256
  mysql_free_result (res);
  mysql_close (m);
257
  return tpw;
Alain Magloire authored
258 259 260 261
}

#ifdef HAVE_SHADOW_H

262 263
struct spwd *
getMspnam (const char *username)
Alain Magloire authored
264
{
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  char *QueryStr = NULL;
  MYSQL *m;
  MYSQL_RES *res;
  MYSQL_ROW row;
  static struct spwd *tpw;

  if (tpw)
    {
      free (tpw->sp_namp);
      free (tpw->sp_pwdp);
    }
      
  m = mysql_init (0);

  if (!m)
    return NULL;
  
282 283
  if (!mysql_real_connect (m, sql_host, sql_user, sql_passwd, sql_db, sql_port,
			   sql_socket, MFLAGS))
284 285 286 287 288
    {
      mu_error ("MySQL: connect failed: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
289

290 291 292 293 294 295 296
  QueryStr = sql_expand_query (sql_getpass_query, username);

  if (!QueryStr)
    {
      mysql_close (m);
      return NULL;
    }
297 298

  if (mysql_query (m, QueryStr) != 0)
299 300 301 302 303
    {
      mu_error ("MySQL: query failed: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
Alain Magloire authored
304

305 306 307 308 309 310 311
  if ((res = mysql_store_result (m)) == NULL)
    {
      mu_error ("MySQL: can't store result: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
  
312
  if ((row = mysql_fetch_row (res)) == NULL)
313 314 315 316 317
    {
      mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
      mysql_close (m);
      return NULL;
    }
Alain Magloire authored
318

319 320
  if (!tpw)
    tpw = (struct spwd *)xmalloc (sizeof (struct spwd));
Alain Magloire authored
321
	
322 323
  tpw->sp_namp = xmalloc (strlen (username)+1);
  strcpy (tpw->sp_namp, username);
Alain Magloire authored
324

325 326
  tpw->sp_pwdp = xmalloc (strlen (row[0])+1);
  strcpy (tpw->sp_pwdp, row[0]);
Alain Magloire authored
327

328 329 330 331
  tpw->sp_lstchg = 11428;
  tpw->sp_min = 0;
  tpw->sp_max = 99999;
  tpw->sp_warn = 7;
Alain Magloire authored
332

333 334
  mysql_free_result (res);
  mysql_close (m);
335
  return tpw;
Alain Magloire authored
336 337 338 339 340
}

#endif /* HAVE_SHADOW_H */

#endif /* HAVE_MYSQL */