popauth.c 11.5 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU 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 General Public License for more details.

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

#include "pop3d.h"

static char short_options[] = "acdf:hlmo:p:u:v";

static struct option long_options[] =
{
  { "add", no_argument, 0, 'a' },
  { "create", no_argument, 0, 'c' },
  { "modify", no_argument, 0, 'm' },
  { "delete", no_argument, 0, 'd' },
  { "file", required_argument, 0, 'f' },
  { "help", no_argument, 0, 'h' },
  { "list", no_argument, 0, 'l' },
  { "output", required_argument, 0, 'o' },
  { "password", required_argument, 0, 'p' },
  { "user", required_argument, 0, 'u' },
  { "version", no_argument, 0, 'v', },
  { 0, 0, 0, 0 }
};

int db_list (char *input_name, char *output_name);
int db_make (char *input_name, char *output_name);
static void help (void);
static void version (void);

#define ACT_CREATE  0
#define ACT_ADD     1
#define ACT_DELETE  2
#define ACT_LIST    3
#define ACT_CHPASS  4

struct action_data {
  char *input_name;
  char *output_name;
  char *username;
  char *passwd;
};

void check_action(int action);
int action_create __P((struct action_data *ap));
int action_add __P((struct action_data *ap));
int action_delete __P((struct action_data *ap));
int action_list __P((struct action_data *ap));
int action_chpass __P((struct action_data *ap));

int (*ftab[]) __P((struct action_data *)) = {
  action_create,
  action_add,
  action_delete,
  action_list,
  action_chpass
};

int
main(int argc, char **argv)
{
  int c;
  int action = -1;
  struct action_data adata;

  memset (&adata, 0, sizeof adata);

  while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
	 != -1)
    {
      switch (c)
	{
	case 'a':
	  check_action (action);
	  action = ACT_ADD;
	  break;

	case 'c':
	  check_action (action);
	  action = ACT_CREATE;
	  break;
	  
	case 'l':
	  check_action (action);
	  action = ACT_LIST;
	  break;
	  
	case 'd':
	  check_action (action);
	  action = ACT_DELETE;
	  break;
	  
	case 'p':
	  adata.passwd = optarg;
	  break;

	case 'm':
	  check_action (action);
	  action = ACT_CHPASS;
	  break;
	  
	case 'f':
	  adata.input_name = optarg;
	  break;
	  
	case 'h':
	  help ();
	  break;
	  
	case 'o':
	  adata.output_name = optarg;
	  break;

	case 'u':
	  adata.username = optarg;
	  break;
	  
	case 'v':
	  version ();
	  break;
	  
	default:
	  break;
	}
    }

  if (action == -1)
    {
      /* Deduce the default action */
      if (getuid () == 0)
	action = ACT_LIST;
      else
	action = ACT_CHPASS;
    }

  return (*ftab[action]) (&adata);
}

void
check_action (int action)
{
  if (action != -1)
    {
      mu_error ("You may not specify more than one `-aldp' option");
      exit (1);
    }
}

int
check_user_perm (int action, struct action_data *ap)
{
  struct stat sb;
  struct passwd *pw;
  uid_t uid;
  
  if (!ap->input_name)
    ap->input_name = APOP_PASSFILE;

  if (mu_dbm_stat (ap->input_name, &sb))
    {
      mu_error ("can't stat %s: %s", ap->input_name, strerror (errno));
      exit (1);
    }

  uid = getuid ();
  if (uid == 0 || sb.st_uid == uid)
    return 0;

  if (ap->username)
    {
      mu_error ("Only the file owner can use --username");
      exit (1);
    }

  if (action != ACT_CHPASS)
    {
      mu_error ("Operation not allowed");
      exit (1);
    }
  pw = getpwuid (uid);
  if (!pw)
    exit (1);
  ap->username = pw->pw_name;
  return 1;
}

int
action_list (struct action_data *ap)
{
  FILE *fp;
  DBM_FILE db;
  DBM_DATUM key;
  DBM_DATUM contents;
  
  check_user_perm (ACT_LIST, ap);
  if (mu_dbm_open (ap->input_name, &db, MU_STREAM_READ, 0600))
    {
      mu_error("can't open %s: %s", ap->input_name, strerror (errno));
      return 1;
    }
  
  if (ap->output_name)
    {
      fp = fopen (ap->output_name, "w");
      if (!fp)
	{
	  mu_error("can't create %s: %s", ap->output_name, strerror (errno));
	  return 1;
	}
    }
  else
    fp = stdout;

  if (ap->username)
    {
      memset (&key, 0, sizeof key);
      memset (&contents, 0, sizeof contents);
      MU_DATUM_PTR (key) = ap->username;
      MU_DATUM_SIZE (key) = strlen (ap->username);
      if (mu_dbm_fetch (db, key, &contents))
	{
	  mu_error ("no such user: %s", ap->username);
	}
      else
	fprintf (fp, "%.*s: %.*s\n",
		 (int) MU_DATUM_SIZE (key),
		 (char*) MU_DATUM_PTR (key),
		 (int) MU_DATUM_SIZE (contents),
		 (char*) MU_DATUM_PTR (contents));
    }
  else
    {
      for (key = mu_dbm_firstkey (db); MU_DATUM_PTR(key);
	   key = mu_dbm_nextkey (db, key))
	{
	  memset (&contents, 0, sizeof contents);
	  mu_dbm_fetch (db, key, &contents);
	  fprintf (fp, "%.*s: %.*s\n",
		   (int) MU_DATUM_SIZE (key),
		   (char*) MU_DATUM_PTR (key),
		   (int) MU_DATUM_SIZE (contents),
		   (char*) MU_DATUM_PTR (contents));
	}
    }
  
  mu_dbm_close (db);
  fclose (fp);
  return 0;
}

int
action_create (struct action_data *ap)
{
  FILE *fp;
  DBM_FILE db;
  DBM_DATUM key;
  DBM_DATUM contents;
  char buf[256];
  int line = 0;

  /* Make sure we have proper privileges if popauth is setuid */
  setuid (getuid ());
  
  if (ap->input_name)
    {
      fp = fopen (ap->input_name, "r");
      if (!fp)
	{
	  mu_error("can't open %s: %s", ap->input_name, strerror (errno));
	  return 1;
	}
    }
  else
    {
      ap->input_name = "";
      fp = stdin;
    }
  
  if (!ap->output_name)
    ap->output_name = APOP_PASSFILE;
  if (mu_dbm_open (ap->output_name, &db, MU_STREAM_CREAT, 0600))
    {
      mu_error("can't create %s: %s", ap->output_name, strerror (errno));
      return 1;
    }

  line = 0;
  while (fgets (buf, sizeof buf - 1, fp))
    {
      int len;
      int argc;
      char **argv;

      len = strlen (buf);
      if (buf[len-1] == '\n')
	buf[--len] = 0;
      
      line++;
      if (argcv_get (buf, ":", NULL, &argc, &argv))
	{
	  argcv_free (argc, argv);
	  continue;
	}

      if (argc == 0 || argv[0][0] == '#')
	{
	  argcv_free (argc, argv);
	  continue;
	}
      
      if (argc != 3 || argv[1][0] != ':' || argv[1][1] != 0)
	{
	  mu_error ("%s:%d: malformed line", ap->input_name, line);
	  argcv_free (argc, argv);
	  continue;
	}

      memset (&key, 0, sizeof key);
      memset (&contents, 0, sizeof contents);
      MU_DATUM_PTR (key) = argv[0];
      MU_DATUM_SIZE (key) = strlen (argv[0]);
      MU_DATUM_PTR (contents) = argv[2];
      MU_DATUM_SIZE (contents) = strlen (argv[2]);

      if (mu_dbm_insert (db, key, contents, 1))
	mu_error ("%s:%d: can't store datum", ap->input_name, line);

      argcv_free (argc, argv);
    }
  mu_dbm_close (db);
  fclose (fp);
  return 0;
}

int
open_io (int action, struct action_data *ap, DBM_FILE *db, int *not_owner)
{
  int rc = check_user_perm (action, ap);
  if (not_owner)
    *not_owner = rc;
  if (mu_dbm_open (ap->input_name, db, MU_STREAM_RDWR, 0600))
    {
      mu_error("can't open %s: %s", ap->input_name, strerror (errno));
      return 1;
    }
  return 0;
}

void
fill_pass (struct action_data *ap)
{
  if (!ap->passwd)
    {
      char *p;

      while (1) {
	if (ap->passwd)
	  free (ap->passwd);
	p = getpass ("Password:");
	if (!p)
	  exit (1);
	ap->passwd = strdup (p);
	p = getpass ("Confirm :");
	if (strcmp (ap->passwd, p) == 0)
	  break;
	mu_error ("Passwords differ. Please retry.");
      } 
    }
}

int
action_add (struct action_data *ap)
{
  DBM_FILE db;
  DBM_DATUM key;
  DBM_DATUM contents;
  int rc;
  
  if (!ap->username)
    {
      mu_error ("missing username to add");
      return 1;
    }

  if (open_io (ACT_ADD, ap, &db, NULL))
    return 1;

  fill_pass (ap);
  
  memset (&key, 0, sizeof key);
  memset (&contents, 0, sizeof contents);
  MU_DATUM_PTR (key) = ap->username;
  MU_DATUM_SIZE (key) = strlen (ap->username);
  MU_DATUM_PTR (contents) = ap->passwd;
  MU_DATUM_SIZE (contents) = strlen (ap->passwd);

  rc = mu_dbm_insert (db, key, contents, 1);
  if (rc)
    mu_error ("can't store datum");

  mu_dbm_close (db);
  return rc;
}

int
action_delete (struct action_data *ap)
{
  DBM_FILE db;
  DBM_DATUM key;
  int rc;
  
  if (!ap->username)
    {
      mu_error ("missing username to delete");
      return 1;
    }

  if (open_io (ACT_DELETE, ap, &db, NULL))
    return 1;
  
  MU_DATUM_PTR (key) = ap->username;
  MU_DATUM_SIZE (key) = strlen (ap->username);

  rc = mu_dbm_delete (db, key);
  if (rc)
    mu_error ("can't remove record for %s", ap->username);

  mu_dbm_close (db);
  return rc;
}

int
action_chpass (struct action_data *ap)
{
  DBM_FILE db;
  DBM_DATUM key;
  DBM_DATUM contents;
  int rc;
  int not_owner;
  
  if (open_io (ACT_CHPASS, ap, &db, &not_owner))
    return 1;

  if (!ap->username)
    {
      mu_error ("missing username");
      return 1;
    }

  memset (&key, 0, sizeof key);
  memset (&contents, 0, sizeof contents);

  MU_DATUM_PTR (key) = ap->username;
  MU_DATUM_SIZE (key) = strlen (ap->username);
  if (mu_dbm_fetch (db, key, &contents))
    {
      mu_error ("no such user: %s", ap->username);
      return 1;
    }

  if (not_owner)
    {
      char *oldpass, *p;
      
      oldpass = xmalloc (MU_DATUM_SIZE (contents) + 1);
      memcpy (oldpass, MU_DATUM_PTR (contents), MU_DATUM_SIZE (contents));
      oldpass[MU_DATUM_SIZE (contents)] = 0;
      p = getpass ("Old Password:");
      if (!p)
	return 1;
      if (strcmp (oldpass, p))
	{
	  mu_error ("Sorry");
	  return 1;
	}
    }

  fill_pass (ap);
  
  MU_DATUM_PTR (contents) = ap->passwd;
  MU_DATUM_SIZE (contents) = strlen (ap->passwd);
  rc = mu_dbm_insert (db, key, contents, 1);
  if (rc)
    mu_error ("can't replace datum");

  mu_dbm_close (db);
  return rc;
}

static void
help ()
{
  printf ("Usage: popauth [OPTIONS] [ACTION]\n");
  printf ("Manipulates pop3d authentication database.\n\n");
  printf ("Options are:\n");
  printf ("  -f, --file=FILE          Read input from FILE (default stdin)\n");
  printf ("  -o, --output=FILE        Direct output to FILE\n");
  printf ("  -h, --help               Display this help and exit\n");
  printf ("  -u, --user=USERNAME      Specify the user name\n");
  printf ("  -p, --password=PASS      Specify user's password\n");
  printf ("  -v, --version            Display program version\n");
  printf ("Actions are:\n");
  printf ("  -a, --add                Add user\n");
  printf ("  -l, --list               List the contents of DBM file\n");
  printf ("  -d, --delete             Delete the given user\n");
  printf ("  -m, --modify             Modify user's record (change password)\n");
  printf ("\nDefault action is:\n");
  printf ("  For the file owner: --list\n");
  printf ("  For a user: --modify --username <username>\n");
  printf ("\nReport bugs to bug-mailutils@gnu.org\n");
  exit (EXIT_SUCCESS);
}

static void
version ()
{
#if defined(WITH_GDBM)
# define FORMAT "GDBM"
#elif defined(WITH_BDB2)
# define FORMAT "Berkeley DB"  
#elif defined(WITH_NDBM)
# define FORMAT "NDBM"  
#elif defined(WITH_OLD_DBM)
# define FORMAT "Old DBM"  
#endif
  printf ("popauth: %s (%s)\n", PACKAGE, VERSION);
  printf ("Database format: %s\n", FORMAT);
  printf ("Database location: %s\n", APOP_PASSFILE);
  exit (EXIT_SUCCESS);
}