/***************************************************************************** POPular -- A POP3 server and proxy for large mail systems $Id: ptestpdm.c,v 1.15 2001/04/08 18:23:57 sqrt Exp $ http://www.remote.org/jochen/mail/popular/ ****************************************************************************** Copyright (C) 1999-2001 Jochen Topf 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA *****************************************************************************/ #if HAVE_CONFIG_H # include #endif #include "popular.h" /* not static because it is needed by pdm.c */ struct pdm_module *first_pdm_module; /* not static because it is needed by pdm.c */ int debug; /***************************************************************************** check_auth_data() *****************************************************************************/ void check_auth_data(FILE *file) { char buf[MAXBUF]; int n=1; while (fgets(buf, sizeof(buf), file)) { char *targv[MAXARGS]; int targc; struct pdm_request pr; struct pdm_data pd; pdm_result_t result; n++; if (buf[0] == '\0' || buf[0] == '#' || buf[1] == '\0') continue; buf[strlen(buf)-1] = '\0'; targc = sep_args(buf, targv, MAXARGS); if (targc != 5) { DEBUG1(DG_AUTH, "check_auth_data", "wrong number of arguments in line %d (must be 5)", n); continue; } if (debug) { int i; DEBUG1(DG_AUTH, "check_auth_data", "line %d has the following arguments:", n); for (i=0; itype; } /* XLOG-DOC:INF:0069:checking * Checking user in POPular database modules. */ xlog_printf(xlog_inf, 0x0069, "checking user='%s' pass='%s' ns='%s' peer='%s'", pr.user, pr.pass, pr.namespace, pr.peer); result = pdm_auth_user(&pr, &pd); switch (result) { case pdmAccept: /* XLOG-DOC:INF:006a:result_user_accepted * The user was accepted by a pdm module. */ xlog_printf(xlog_inf, 0x006a, "result_user_accepted backend='%s' user='%s' pass='%s'", pd.backend, pd.user, pd.pass); break; case pdmFail: /* XLOG-DOC:INF:006b:result_user_exists * The user was found by a pdm module but authentication failed. */ xlog_printf(xlog_err, 0x006b, "result_user_exists (but auth failed) backend='%s' user='%s' pass='%s'", pd.backend, pd.user, pd.pass); break; case pdmError: /* XLOG-DOC:INF:006c:result_pdm_internal_error * A pdm module returned an internal error. */ xlog_printf(xlog_adm, 0x006c, "result_pdm_internal_error"); break; case pdmUnknown: /* XLOG-DOC:INF:006d:result_unknown_user * The user was not found by any pdm module. */ xlog_printf(xlog_err, 0x006d, "result_unknown_user"); break; default: /* XLOG-DOC:INF:006e:result_unknown_result_code * An unknown result code was returned by pdm_auth_user(). */ xlog_printf(xlog_bug, 0x006e, "result_unknown_result_code code=%d", result); break; } } } /***************************************************************************** print_usage() *****************************************************************************/ void print_usage(char *prg) { printf("Usage: %s [OPTION] ... COMMAND_FILE [TEST_FILE]\n", prg); printf("Test program for pdm database modules.\n"); printf("Options:\n"); printf(" -d, --dir=DIR set directory where database modules are found\n"); printf(" (default: `" PPROXY_PDM_DIR "')\n"); printf(" --debug enable debugging messages\n"); printf(" --help print this help message\n"); printf(" --version print version information\n"); printf("\n"); printf("The COMMAND_FILE contains the list of database modules to load\n"); printf("with their parameters. The TEST_FILE contains the authorization\n"); printf("data to check. If no TEST_FILE is given, stdin is read.\n"); } /***************************************************************************** main() *****************************************************************************/ int main(int argc, char *argv[]) { int c; char *dir = PPROXY_PDM_DIR; char *prgname = argv[0]; static struct option long_options[] = { { "help", no_argument, 0, 0 }, { "version", no_argument, 0, 0 }, { "debug", no_argument, 0, 0 }, { "dir", required_argument, 0, 'd' }, { NULL, 0, 0, 0 } }; debug = 0; xlog_init(PTESTPDM_PRG_NAME); /*************************************************************************** Read command line arguments. ***************************************************************************/ while (1) { int option_index = 0; c = getopt_long(argc, argv, "d:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: if (! strcmp(long_options[option_index].name, "version")) { printf("ptestpdm - POPular POP server suite %s\n", VERSION); exit(RCODE_OK); } else if (! strcmp(long_options[option_index].name, "help")) { print_usage(prgname); exit(RCODE_OK); } else if (! strcmp(long_options[option_index].name, "debug")) { debug = DG_ALL; } else { fprintf(stderr, "%s: unknown option: %s\n", prgname, long_options[option_index].name); exit(RCODE_INTERNAL); } break; case 'd': dir = optarg; break; default: fprintf(stderr, "Try `%s --help' for more information.\n", prgname); exit(RCODE_CMDLINE); } } if (optind >= argc) { fprintf(stderr, "%s: missing command file argument on command line\n", prgname); exit(RCODE_CMDLINE); } if (optind+2 != argc) { fprintf(stderr, "%s: invalid extra arguments.\nTry `%s --help' for more information.\n", prgname, prgname); exit(RCODE_CMDLINE); } fprintf(stderr, "%s: module directory is `%s'\n", prgname, dir); /*************************************************************************** Read command file ***************************************************************************/ { char buf[MAXBUF]; int n=0; struct pdm_module *pmodule; struct pdm_module **pep = &first_pdm_module; FILE *cf = fopen(argv[optind], "r"); if (! cf) { fprintf(stderr, "%s: can't open command file `%s': %s\n", prgname, argv[optind], strerror(errno)); exit(RCODE_CMDLINE); } while (fgets(buf, sizeof(buf), cf)) { char *targv[MAXARGS]; int targc; n++; if (buf[0] == '\0' || buf[0] == '#' || buf[1] == '\0') continue; buf[strlen(buf)-1] = '\0'; targc = sep_args(buf, targv, MAXARGS); if (targc == -1) { fprintf(stderr, "%s: parse error in command file line `%d'\n", prgname, n); exit(RCODE_ERR); } if (targc == -2) { fprintf(stderr, "%s: too many arguments in command file line `%d'\n", prgname, n); exit(RCODE_ERR); } if (targc < 0) { fprintf(stderr, "%s: internal error (sep_args) reading command file line `%d'\n", prgname, n); exit(RCODE_ERR); } if (targc == 0 || targc == 1) { fprintf(stderr, "%s: not enough arguments in command file line `%d'\n", prgname, n); exit(RCODE_ERR); } if (debug) { int i; DEBUG2(DG_AUTH, "main", "loading module `libpdm_%s.so' with id `%s'", targv[1], targv[0]); for (i=2; inext; } fclose(cf); } DEBUG0(DG_AUTH, "main", "all modules loaded sucessfully"); /*************************************************************************** Check auth data ***************************************************************************/ if (optind+1 < argc) { FILE *af; DEBUG1(DG_AUTH, "main", "checking auth data in file `%s'", argv[optind+1]); af = fopen(argv[optind+1], "r"); if (af) { check_auth_data(af); } else { fprintf(stderr, "%s: can't open auth data file `%s': %s\n", prgname, argv[optind+1], strerror(errno)); } } else { DEBUG0(DG_AUTH, "main", "checking auth data from stdin"); check_auth_data(stdin); } /*************************************************************************** Close everything down ***************************************************************************/ { struct pdm_module *p; for (p = first_pdm_module; p; p = p->next) { DEBUG1(DG_AUTH, "main", "unloading module `%s'", p->id); p->close(p->ctx); dlclose(p->lib); } } DEBUG0(DG_AUTH, "main", "all modules unloaded sucessfully"); return 0; } /** THE END *****************************************************************/