update otp_hotp() to support 6,7,8,9 digit otp's
[freeradius.git] / src / modules / rlm_dbm / rlm_dbm_cat.c
1 /*
2  * rlm_dbm_cat.c :    List rlm_dbm DBM file
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2001 Koulik Andrei, Sandy Service
21  */
22
23 #include <freeradius-devel/autoconf.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifdef HAVE_NDBM_H
30 #include <ndbm.h>
31 #endif
32
33 #ifdef HAVE_GDBM_NDBM_H
34 #include <gdbm/ndbm.h>
35 #endif
36
37 #ifdef HAVE_GDBMNDBM_H
38 #include <gdbm-ndbm.h>
39 #endif
40
41 #include <unistd.h>
42 #include <ctype.h>
43
44 #define LOTSTUP 20
45 #define WRAPLEN 40
46
47
48 int wraplen = WRAPLEN, needwrap = 0, lotstup = LOTSTUP;
49 char const * progname;
50
51 static void dump_record(datum key,datum data)
52 {
53         int i,j;
54         char *p;
55         for(i = 0, p = key.dptr; i < key.dsize; i++, p++)
56           putchar(*p);
57         if ( i < lotstup ) while( i++ <= lotstup) putchar(' ');
58                 else putchar(' ');
59
60         for(j = 0, p = data.dptr ; j < data.dsize && *p ; i++, p++ ) {
61                 putchar(*p);
62                 if ( needwrap && *p == ',' && i > wraplen ) putchar('\n');
63                 if ( *p == '\n' || ( needwrap && *p == ',' && i > wraplen ) ) {
64                         for(i = 0; i < lotstup; i++) putchar(' ');
65                         i = 0;
66                 }
67         }
68
69         putchar('\n');
70 }
71
72 #ifdef __GNUC__
73 static void __attribute__((noreturn)) usage(void)
74 #else
75 static void usage(void)
76 #endif
77 {
78         fprintf(stderr, "Usage: %s: [-f file] [-w] [-i number] [-l number] [-v]\n\n",progname);
79
80         exit(1);
81 }
82
83 int main(int n, char **argv) {
84
85         const char      *fname = NULL;
86         DBM     *pdb;
87         datum   k,d;
88         int     ch;
89         int     i;
90
91         progname = argv[0];
92
93
94
95         while ((ch = getopt(n, argv, "i:l:wf:v")) != -1)
96                 switch (ch) {
97                         case 'i':       if (!isdigit((int) *optarg)) usage();
98                                         lotstup = atoi(optarg);
99                                         break;
100                         case 'l':       if (!isdigit((int) *optarg)) usage();
101                                         wraplen = atoi(optarg);
102                                         break;
103                         case 'w':       needwrap = 1;
104                                         break;
105                         case 'f':       fname = optarg;
106                                         break;
107                         case 'v':       printf("%s: $Id$\n",progname);
108                                         exit(0);
109                                         break;
110                         default : usage(); exit(1); break;
111
112                 }
113         n -= (optind - 1);
114         argv += (optind -1);
115
116         if ( fname == NULL) fname = "sandy_db";
117
118         if ( ( pdb = dbm_open(fname, O_RDONLY, 0777) ) == NULL ) {
119                 perror("Couldn't open database");
120                 exit(1);
121         }
122         if ( n > 1 ) {
123                 for ( i = 1 ; i < n ; i++ ) {
124                         printf(" Check: %s\n",argv[i]);
125                         k.dptr  = argv[i];
126                         k.dsize = strlen(argv[i]) + 1;
127                         if ( (d = dbm_fetch(pdb,k)).dptr == NULL ) {
128                                 printf("Not found\n");
129                         } else dump_record(k, d);
130                 }
131         } else {
132                 for ( k = dbm_firstkey(pdb) ; k.dptr != NULL ; k = dbm_nextkey(pdb) )
133                         if ( (d = dbm_fetch(pdb,k)).dptr == NULL ) {
134                                 perror("Couldn't fetch user record");
135                                 exit(1);
136                         } else dump_record(k, d);
137         }
138         dbm_close(pdb);
139         fflush(stdout);
140
141         return 0;
142
143 }