1 /*
2  * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. The name "Carnegie Mellon University" must not be used to
17  *    endorse or promote products derived from this software without
18  *    prior written permission. For permission or any legal
19  *    details, please contact
20  *      Carnegie Mellon University
21  *      Center for Technology Transfer and Enterprise Creation
22  *      4615 Forbes Avenue
23  *      Suite 302
24  *      Pittsburgh, PA  15213
25  *      (412) 268-7393, fax: (412) 268-7395
26  *      innovation@andrew.cmu.edu
27  *
28  * 4. Redistributions of any form whatsoever must retain the following
29  *    acknowledgment:
30  *    "This product includes software developed by Computing Services
31  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
32  *
33  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
34  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
35  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
36  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
37  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
38  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
39  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40  */
41 
42 #include <config.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 
47 #include "auth_pts.h"
48 #include "cyrusdb.h"
49 #include "imap/global.h"
50 #include "libconfig.h"
51 
dump_cb(void * rockp,const char * key,size_t keylen,const char * data,size_t datalen)52 static int dump_cb(void *rockp __attribute__((unused)),
53                      const char *key, size_t keylen,
54                      const char *data,
55                      size_t datalen __attribute__((unused)))
56 {
57     struct auth_state *authstate = (struct auth_state *)data;
58     int i;
59 
60     printf("user: ");
61     fwrite(key, keylen, 1, stdout);
62     printf(" time: %d groups: %d\n",
63            (unsigned)authstate->mark, (unsigned)authstate->ngroups);
64 
65     for (i=0; i < authstate->ngroups; i++)
66         printf("  %s\n",authstate->groups[i].id);
67 
68     return 0;
69 }
70 
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73     struct db *ptdb;
74     extern char *optarg;
75     int opt;
76     int r;
77     const char *fname;
78     char *alt_config = NULL, *tofree = NULL;
79 
80     while ((opt = getopt(argc, argv, "C:")) != EOF) {
81         switch (opt) {
82         case 'C': /* alt config file */
83             alt_config = optarg;
84             break;
85         default:
86             fprintf(stderr,"usage: [-C filename]"
87                     "\n\t-C <filename>\tAlternate Config File"
88                     "\n");
89             exit(-1);
90             break;
91             /* just pass through */
92         }
93     }
94 
95     cyrus_init(alt_config, "ptdump", 0, 0);
96 
97     /* open database */
98     fname = config_getstring(IMAPOPT_PTSCACHE_DB_PATH);
99     if (!fname) {
100         tofree = strconcat(config_dir, PTS_DBFIL, NULL);
101         fname = tofree;
102     }
103 
104     r = cyrusdb_open(config_ptscache_db, fname, CYRUSDB_CREATE, &ptdb);
105     if(r != CYRUSDB_OK) {
106         fprintf(stderr,"error opening %s (%s)", fname,
107                cyrusdb_strerror(r));
108         exit(1);
109     }
110 
111     if (tofree) free(tofree);
112 
113     /* iterate through db, wiping expired entries */
114     cyrusdb_foreach(ptdb, "", 0, NULL, dump_cb, ptdb, NULL);
115 
116     cyrusdb_close(ptdb);
117 
118     cyrus_done();
119 
120     return 0;
121 }
122