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
|
/*
This file is part of GNU Anubis
Copyright (C) 2004, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "anubisadm.h"
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");
}
void
xalloc_die ()
{
error ("%s", _("Not enough memory"));
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 = 0;
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 = 0;
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;
break;
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."));
}
int
main (int argc, char **argv)
{
int index;
operation_fp operation = op_usage;
/* save the program name */
progname = strrchr (argv[0], '/');
if (progname)
progname++;
else
progname = argv[0];
/* 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
adm_get_options (argc, argv, &operation, &index);
return operation (argc - index, argv + index);
}
/* EOF */
|