aboutsummaryrefslogtreecommitdiff
path: root/src/anubisadm.c
blob: 3ecca2d7e18e7816fdaf843f63e8792abba90952 (plain)
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
/*
   This file is part of GNU Anubis 
   Copyright (C) 2004 The Anubis Team.

   GNU Anubis 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 of the License, or
   (at your option) any later version.

   GNU Anubis 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 GNU Anubis; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   GNU Anubis is released under the GPL with the additional exemption that
   compiling, linking, and/or using OpenSSL is allowed.
*/

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

#include "headers.h"
#include "extern.h"
#include <getopt.h>
#include <getline.h>

#define OPT_VERSION          257
#define OPT_HELP             258

static struct option option[] = {
  {"create", no_argument, 0, 'c'},
  {"list", no_argument, 0, 'l'},
  {"add", no_argument, 0, 'a'},
  {"remove", no_argument, 0, 'r'},
  {"modify", no_argument, 0, 'm'},
  {"authid", required_argument, 0, 'i'},
  {"user", required_argument, 0, 'u'},
  {"rcfile", required_argument, 0, 'f'},
  {"password", optional_argument, 0, 'p'},

  {"version", no_argument, 0, OPT_VERSION},
  {"help", no_argument, 0, OPT_HELP},
  {NULL, 0, 0, 0}
};

typedef int (*operation_fp) (int, char **);

char *progname;
char *authid;
char *username;
char *rcfile;
char *password;

void
error (const char *fmt, ...)
{
  va_list ap;
  fprintf (stderr, "%s: ", progname);
  va_start (ap, fmt);
  vfprintf (stderr, fmt, ap);
  va_end (ap);
  fprintf (stderr, "\n");
}

static void
adm_memory_error (const char *msg)
{
  error ("%s", msg);
  exit (1);
}

int
op_usage (int argc, char **argv)
{
  error (_("operation not specified"));
  return 1;
}

int
opendb (void **dptr, int argc, char **argv, enum anubis_db_mode mode)
{
  int rc;
  char *err;

  if (argc == 0)
    {
      error (_("database URL is not specified"));
      return 1;
    }
  if (argc > 1)
    {
      error (_("too many arguments"));
      return 1;
    }

  rc = anubis_db_open (argv[0], mode, dptr, &err);

  if (rc != ANUBIS_DB_SUCCESS)
    error ("%s", err);
  return rc;
}

static const char delim[] = " \t\r\n";

int
op_create (int argc, char **argv)
{
  ANUBIS_USER rec;
  char *buf = NULL;
  size_t n = 0;
  size_t line = 0;
  void *db;
  int rc;

  if (opendb (&db, argc, argv, anubis_db_rdwr))
    return 1;

  memset (&rec, 0, sizeof (rec));
  while (getline (&buf, &n, stdin) > 0 && n > 0)
    {
      char *p;
      int len = strlen (buf);
      if (len > 0 && buf[len - 1] == '\n')
	buf[len - 1] = 0;
      line++;

      for (p = buf; *p && isspace (*p); p++)
	;

      if (*p == '#')
	continue;		/* Skip comments */
      p = strtok (p, delim);
      if (!p)
	continue;		/* Skip empty lines */
      rec.smtp_authid = strdup (p);

      p = strtok (NULL, delim);
      if (!p)
	{
	  error (_("%lu: incomplete line"), (unsigned long) line);
	  free (rec.smtp_authid);
	  continue;
	}
      rec.smtp_passwd = strdup (p);

      p = strtok (NULL, delim);
      if (p)
	{
	  rec.username = strcmp(p, "NONE") ? strdup (p) : NULL;
	  p = strtok (NULL, delim);
	  if (p)
	    rec.rcfile_name = strcmp(p, "NONE") ? strdup (p) : NULL;
	}

      rc = anubis_db_put_record (db, rec.smtp_authid, &rec);
      anubis_db_free_record (&rec);
      if (rc)
	{
	  error (_("%lu: cannot write to the database: %s"),
		 (unsigned long) line, anubis_db_strerror (db));
	  break;
	}
    }
  free (buf);
  anubis_db_close (&db);
  return rc;
}

void
print_record (ANUBIS_USER * rec)
{
  printf ("%s\t%s\t%s\t%s\n", rec->smtp_authid, rec->smtp_passwd,
	  rec->username ? rec->username : "NONE",
	  rec->rcfile_name ? rec->rcfile_name : "NONE");
}

void
print_list_header (void)
{
  printf ("# %s\n", _("AuthID\tPassword\tUserName\tRCfile"));
}

int
record_printer (void *item, void *data)
{
  print_record (item);
  return 0;
}

int
record_free (void *item, void *data)
{
  anubis_db_free_record (item);
  free (item);
  return 0;
}

int
op_list (int argc, char **argv)
{
  ANUBIS_USER rec;
  void *db;
  int rc;

  if (opendb (&db, argc, argv, anubis_db_rdonly))
    return 1;

  if (authid)
    {
      rc = anubis_db_get_record (db, authid, &rec);
      switch (rc)
	{
	case ANUBIS_DB_SUCCESS:
	  print_list_header ();
	  print_record (&rec);
	  anubis_db_free_record (&rec);
	  rc = 0;
	  break;

	case ANUBIS_DB_NOT_FOUND:
	  error (_("%s: authid not found"), authid);
	  rc = 0;
	  break;

	case ANUBIS_DB_FAIL:
	  error (_("database error: %s"), anubis_db_strerror (db));
	  rc = 1;
	  break;
	}
    }
  else
    {
      ANUBIS_LIST *reclist;
      rc = anubis_db_get_list (db, &reclist);
      switch (rc)
	{
	case ANUBIS_DB_SUCCESS:
	  rc = 0;
	  print_list_header ();
	  list_iterate (reclist, record_printer, NULL);
	  list_destroy (&reclist, record_free, NULL);
	  break;

	case ANUBIS_DB_NOT_FOUND:
	  rc = 0;
	  printf ("# %s\n", _("Database is empty"));
	  break;

	default:
	  error (_("database error: %s"), anubis_db_strerror (db));
	  rc = 1;
	}
    }
  anubis_db_close (&db);
  return rc;
}

int
op_add_or_modify (char *database, int code, char *errmsg)
{
  ANUBIS_USER rec;
  void *db;
  char *err;
  int rc;

  if (!authid)
    {
      error (_("authid not specified"));
      return 1;
    }
  if (!password)
    password = getpass (_("Password:"));
  if (!password)
    {
      error (_("password not specified"));
      return 1;
    }

  rc = anubis_db_open (database, anubis_db_rdwr, &db, &err);

  if (rc != ANUBIS_DB_SUCCESS)
    {
      error ("%s", err);
      return 1;
    }

  rc = anubis_db_get_record (db, authid, &rec);
  if (rc == ANUBIS_DB_FAIL)
    {
      error (_("database error: %s"), anubis_db_strerror (db));
      anubis_db_close (&db);
      return 1;
    }

  if (rc != code)
    {
      error ("%s", errmsg);
      anubis_db_close (&db);
      return 1;
    }

  rec.smtp_authid = authid;
  rec.smtp_passwd = password;
  rec.username = username;
  rec.rcfile_name = rcfile;

  rc = anubis_db_put_record (db, authid, &rec);
  if (rc != ANUBIS_DB_SUCCESS)
    {
      error (_("database error: %s"), anubis_db_strerror (db));
      return 1;
    }
  anubis_db_close (&db);
  return 0;
}

int
op_add (int argc, char **argv)
{
  return op_add_or_modify (argv[0], ANUBIS_DB_NOT_FOUND,
			   _
			   ("Record already exists. Use --modify to change it."));
}

int
op_remove (int argc, char **argv)
{
  void *db;
  int rc;

  if (!authid)
    {
      error (_("authid not specified"));
      return 1;
    }

  if (opendb (&db, argc, argv, anubis_db_rdwr))
    return 1;

  switch (anubis_db_delete_record (db, authid))
    {
    case ANUBIS_DB_NOT_FOUND:
      error (_("record not found"));
      rc = 1;
      break;

    case ANUBIS_DB_FAIL:
      error (_("database error: %s"), anubis_db_strerror (db));
      rc = 1;

    case ANUBIS_DB_SUCCESS:
      rc = 0;
    }
  anubis_db_close (&db);
  return rc;
}

int
op_modify (int argc, char **argv)
{
  return op_add_or_modify (argv[0], ANUBIS_DB_SUCCESS,
			   _("Record not found. Use --add to create it."));
}

void
print_help (void)
{
  puts (_("anubisadm -- Interface for GNU Anubis database administration."));
  puts (_("Usage: anubisadm [COMMAND] [OPTIONS] URL"));

  puts (_("\nCOMMAND is one of\n"));
  puts (_("  -c, --create            Creates the database."));
  puts (_
	("  -l, --list              List the contents of an existing database."));
  puts (_("  -a, --add               Add a new record."));
  puts (_("  -m, --modify            Modify existing record."));
  puts (_("  -r, --remove            Remove existing record."));
  puts (_
	("  --version               Display program version number and exit."));
  puts (_("  --help                  Display this help screen and exit."));

  puts (_("\nOPTION is one or more of\n"));
  puts (_
	("  -i, --authid=STRING     Specify the authid to operate upon. This option\n"
	 "                          is mandatory with --add, --modify and --remove.\n"
	 "                          It is optional when used with --list."));
  puts (_
	("  -p, --password=STRING   Specify the password for the authid. Mandatory\n"
	 "                          with --add, --modify and --remove."));
  puts (_
	("  -u, --user=STRING       Specify the system user name corresponding to\n"
	 "                          the given authid. Optional for --add, --modify\n"
	 "                          and --remove."));
  puts (_
	("  -f, --rcfile=STRING     Specify the rc file to be used for this authid.\n"
	 "                          Optional for --add, --modify and --remove."));

  puts (_("\nEXAMPLES\n"));
  puts (_("1. Create the GDBM database from a plaintext file:\n\n"
	  "example$ anubisadm --create gdbm:/etc/anubis.db < plaintext\n"));

  puts (_("2. Add SMTP authid \"test\" with password \"guessme\" and map it\n"
	  "to the system account \"gray\":\n\n"
	  "example$ anubisadm --add --authid test --password guessme \\\n"
	  "                   --user gray gdbm:/etc/anubis.db\n"));

  puts (_("3. List the entire database contents:\n\n"
	  "example$ anubisadm --list gdbm:/etc/anubis.db\n"));

  puts (_("4. List only the record with authid \"test\":\n\n"
	  "example$ anubisadm --list --authid test gdbm:/etc/anubis.db\n"));

  printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}

int
main (int argc, char **argv)
{
  int c;
  operation_fp operation = op_usage;

  /* save the program name */
  progname = strrchr (argv[0], '/');
  if (progname)
    progname++;
  else
    progname = argv[0];

  /* Register memory error printer */
  memory_error = adm_memory_error;

  /* Initialize various database formats */

  dbtext_init ();
# ifdef HAVE_LIBGDBM
  gdbm_db_init ();
# endif
# ifdef WITH_MYSQL
  mysql_db_init ();
# endif
# ifdef WITH_PGSQL
  pgsql_db_init ();
# endif

  while ((c = getopt_long (argc, argv, "clarmi:u:f:p:?",
			   option, NULL)) != EOF)
    {
      switch (c)
	{
	case OPT_VERSION:
	  printf ("%s\n", PACKAGE_STRING);
	  exit (0);
	  break;

	case OPT_HELP:
	  print_help ();
	  exit (0);
	  break;

	case 'c':
	  operation = op_create;
	  break;

	case 'l':
	  operation = op_list;
	  break;

	case 'a':
	  operation = op_add;
	  break;

	case 'r':
	  operation = op_remove;
	  break;

	case 'm':
	  operation = op_modify;
	  break;

	case 'i':
	  authid = optarg;
	  break;

	case 'u':
	  username = optarg;
	  break;

	case 'f':
	  rcfile = optarg;
	  break;

	case 'p':
	  password = optarg;
	  break;

	default:
	  return 1;
	}
    }

  return operation (argc - optind, argv + optind);
}

/* EOF */

Return to:

Send suggestions and report system problems to the System administrator.