sql.c 10.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 2002 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */


#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#ifdef HAVE_SHADOW_H
# include <shadow.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_CRYPT_H
# include <crypt.h>
#endif

#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/mailbox.h>
#include <mailutils/argp.h>
#include <mailutils/mu_auth.h>
#include <mailutils/error.h>
#include <mailutils/nls.h>

#ifdef HAVE_MYSQL
#include <mysql/mysql.h>

#define MFLAGS 0                /* Special user flags. It is safe to leave
				   this untouched */

char *sql_getpwnam_query;
char *sql_getpass_query;
char *sql_getpwuid_query;

char *sql_host = NULL;          /* Hostname to connect to. NULL for UNIX
                                   socket connection */
char *sql_user = "accounts";    /* Username for mysql access */    
char *sql_passwd = "yurpass";   /* Password for mysql access */
char *sql_db = "accounts";      /* Database Name */
char *sql_socket = NULL;        /* Socket name to use. Valid only if
				   connecting via UNIX sockets */
int  sql_port = 0;              /* Port number to connect to. 0 means default
	                           MySQL port (3300) */

static char *
sql_expand_query (const char *query, const char *ustr)
{
  char *p, *q, *res;
  int len;

  if (!query)
    return NULL;
  
  /* Compute resulting query length */
  for (len = 0, p = (char *) query; *p; )
    {
      if (*p == '%')
	{
	  if (p[1] == 'u')
	    {
	      len += strlen (ustr);
	      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 = (char *) query, q = res; *p; )
    {
      if (*p == '%')
	{
	  switch (*++p)
	    {
	    case 'u':
	      strcpy (q, ustr);
	      q += strlen (q);
	      p++;
	      break;
	      
	    case '%':
	      *q++ = *p++;
	      break;
	      
	    default:
	      *q++ = *p++;
	    }
	}
      else
	*q++ = *p++;
    }
  *q = 0;
  return res;
}

static int
mu_auth_sql_by_name (void *return_data, void *key,
		     void *unused_func_data, void *unused_call_data)
{
  char *query_str = NULL;
  MYSQL *m;
  MYSQL_RES *res;
  MYSQL_ROW row;
  char *mailbox_name;
  int rc;
  
  if (!key)
    {
      errno = EINVAL;
      return 1;
    }

  m = mysql_init (0);

  if (!m)
    return 1;

  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 1;
    }

  query_str = sql_expand_query (sql_getpwnam_query, key);

  if (!query_str)
    {
      mysql_close (m);
      return 1;
    }
  
  if (mysql_query (m, query_str) != 0)
    {
      mu_error ("MySQL: query failed: %s", mysql_error (m));
      free (query_str);
      mysql_close (m);
      return 1;
    }

  free (query_str);

  if ((res = mysql_store_result (m)) == NULL)
    {
      mu_error ("MySQL: can't store result: %s", mysql_error (m));
      mysql_close (m);
      return 1;
    }
  
  if ((row = mysql_fetch_row (res)) == NULL)
    {
      mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
      mysql_close (m);
      return 1;
    }

  if (mysql_num_fields (res) == 7 && row[6])
    mailbox_name = strdup (row[6]);
  else
    {
      mailbox_name = malloc (strlen (mu_path_maildir) +
			     strlen (row[0]) + 1);
      if (mailbox_name)
	sprintf (mailbox_name, "%s%s", mu_path_maildir, row[0]);
    }

  if (mailbox_name)
    rc = mu_auth_data_alloc ((struct mu_auth_data **) return_data,
			     row[0],
			     row[1],
			     atoi (row[2]),
			     atoi (row[3]),
			     "Mysql User",
			     row[4],
			     row[5],
			     mailbox_name,
			     1);
  else
    rc = 1;
  
  free (mailbox_name);
  mysql_free_result (res);
  mysql_close (m);
  
  return rc;
}

static int
mu_auth_sql_by_uid (void *return_data, void *key,
		    void *unused_func_data, void *unused_call_data)
{
  char *query_str = NULL;
  MYSQL *m;
  MYSQL_RES *res;
  MYSQL_ROW row;
  char uidstr[64];
  char *mailbox_name;
  int rc;
  
  if (!key)
    {
      errno = EINVAL;
      return 1;
    }

  m = mysql_init (0);

  if (!m)
    return 1;

  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 1;
    }

  snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key);
  query_str = sql_expand_query (sql_getpwuid_query, uidstr);

  if (!query_str)
    {
      mysql_close (m);
      return 1;
    }
  
  if (mysql_query (m, query_str) != 0)
    {
      mu_error ("MySQL: query failed: %s", mysql_error (m));
      free (query_str);
      mysql_close (m);
      return 1;
    }

  free (query_str);
  
  if ((res = mysql_store_result (m)) == NULL)
    {
      mu_error ("MySQL: can't store result: %s", mysql_error (m));
      mysql_close (m);
      return 1;
    }
  
  if ((row = mysql_fetch_row (res)) == NULL)
    {
      mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
      mysql_close (m);
      return 1;
    }

  if (mysql_num_fields (res) == 7 && row[6])
    mailbox_name = strdup (row[6]);
  else
    {
      mailbox_name = malloc (strlen (mu_path_maildir) +
			     strlen (row[0]) + 1);
      if (mailbox_name)
	sprintf (mailbox_name, "%s%s", mu_path_maildir, row[0]);
    }

  if (mailbox_name)
    rc = mu_auth_data_alloc ((struct mu_auth_data **) return_data,
			     row[0],
			     row[1],
			     atoi (row[2]),
			     atoi (row[3]),
			     "Mysql User",
			     row[4],
			     row[5],
			     mailbox_name,
			     1);
  else
    rc = 1;
  
  free (mailbox_name);
  mysql_free_result (res);
  mysql_close (m);
  
  return rc;
}

static int
mu_sql_authenticate (void *ignored_return_data, void *key,
		     void *ignored_func_data, void *call_data)
{
  struct mu_auth_data *auth_data = key;
  char *query_str = NULL;
  MYSQL *m;
  MYSQL_RES *res;
  MYSQL_ROW row;
  int rc;
  
  if (!auth_data)
    return 1;

  m = mysql_init (0);

  if (!m)
    return 1;
  
  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 1;
    }
  
  query_str = sql_expand_query (sql_getpass_query, auth_data->name);

  if (!query_str)
    {
      mysql_close (m);
      return 1;
    }

  if (mysql_query (m, query_str) != 0)
    {
      mu_error ("MySQL: query failed: %s", mysql_error (m));
      free (query_str);
      mysql_close (m);
      return 1;
    }

  free (query_str);
  
  if ((res = mysql_store_result (m)) == NULL)
    {
      mu_error ("MySQL: can't store result: %s", mysql_error (m));
      mysql_close (m);
      return 1;
    }
  
  if ((row = mysql_fetch_row (res)) == NULL)
    {
      mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
      mysql_close (m);
      return 1;
    }

  rc = strcmp (row[0], crypt (auth_data->passwd, row[0]));
  mysql_free_result (res);
  mysql_close (m);
  return rc;
}


# define ARG_SQL_GETPWNAM 1
# define ARG_SQL_GETPWUID 2
# define ARG_SQL_GETPASS 3
# define ARG_SQL_HOST 4
# define ARG_SQL_USER 5
# define ARG_SQL_PASSWD 6
# define ARG_SQL_DB 7
# define ARG_SQL_PORT 8

static struct argp_option mu_sql_argp_option[] = {
  {"sql-getpwnam", ARG_SQL_GETPWNAM, N_("QUERY"), 0,
   N_("SQL query to retrieve a passwd entry based on username"), 0},
  {"sql-getpwuid", ARG_SQL_GETPWUID, N_("QUERY"), 0,
   N_("SQL query to retrieve a passwd entry based on UID"), 0},
  {"sql-getpass", ARG_SQL_GETPASS, N_("QUERY"), 0,
   N_("SQL query to retrieve a password from the database"), 0},
  {"sql-host", ARG_SQL_HOST, N_("HOSTNAME"), 0,
   N_("Name or IP of MySQL server to connect to"), 0},
  {"sql-user", ARG_SQL_USER, N_("NAME"), 0,
   N_("SQL user name"), 0},
  {"sql-passwd", ARG_SQL_PASSWD, N_("STRING"), 0,
   N_("SQL connection password"), 0},
  {"sql-db", ARG_SQL_DB, N_("STRING"), 0,
   N_("Name of the database to connect to"), 0},
  {"sql-port", ARG_SQL_PORT, N_("NUMBER"), 0,
   N_("Port to use"), 0},
  { NULL,      0, NULL, 0, NULL, 0 }
};

static error_t
mu_sql_argp_parser (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case ARG_SQL_GETPWNAM:
      sql_getpwnam_query = arg;
      break;

    case ARG_SQL_GETPWUID:
      sql_getpwuid_query = arg;
      break;

    case ARG_SQL_GETPASS:
      sql_getpass_query = arg;
      break;

    case ARG_SQL_HOST:
      sql_host = arg;
      break;

    case ARG_SQL_USER:
      sql_user = arg;
      break;

    case ARG_SQL_PASSWD:
      sql_passwd = arg;
      break;

    case ARG_SQL_DB:
      sql_db = arg;
      break;

    case ARG_SQL_PORT:
      sql_port = strtoul (arg, NULL, 0);
      if (sql_port == 0)
	{
	  sql_host = NULL;
	  sql_socket = arg;
	}
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

struct argp mu_sql_argp = {
  mu_sql_argp_option,
  mu_sql_argp_parser,
};

#else

#define mu_sql_authenticate mu_auth_nosupport
#define mu_auth_sql_by_name mu_auth_nosupport
#define mu_auth_sql_by_uid  mu_auth_nosupport

#endif

struct mu_auth_module mu_auth_sql_module = {
  "sql",
#ifdef HAVE_MYSQL
  &mu_sql_argp,
#else
  NULL,
#endif
  mu_sql_authenticate,
  NULL,
  mu_auth_sql_by_name,
  NULL,
  mu_auth_sql_by_uid,
  NULL
};