1 /* dav_reconstruct.c - (re)build DAV DB for a user
2  *
3  * Copyright (c) 1994-2012 Carnegie Mellon University.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. The name "Carnegie Mellon University" must not be used to
18  *    endorse or promote products derived from this software without
19  *    prior written permission. For permission or any legal
20  *    details, please contact
21  *      Carnegie Mellon University
22  *      Center for Technology Transfer and Enterprise Creation
23  *      4615 Forbes Avenue
24  *      Suite 302
25  *      Pittsburgh, PA  15213
26  *      (412) 268-7393, fax: (412) 268-7395
27  *      innovation@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  *
42  */
43 
44 #include <config.h>
45 
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <sysexits.h>
53 #include <syslog.h>
54 #include <time.h>
55 
56 #include <libical/ical.h>
57 
58 #include "annotate.h"
59 #include "global.h"
60 #include "http_dav.h"
61 #include "mailbox.h"
62 #include "message.h"
63 #include "message_guid.h"
64 #include "mboxname.h"
65 #include "mboxlist.h"
66 #include "util.h"
67 #include "xmalloc.h"
68 #include "xstrlcat.h"
69 #include "zoneinfo_db.h"
70 
71 /* generated headers are not necessarily in current directory */
72 #include "imap/imap_err.h"
73 
74 extern int optind;
75 extern char *optarg;
76 
77 /* current namespace */
78 static struct namespace recon_namespace;
79 
80 /* config.c stuff */
81 const int config_need_data = 0;
82 
83 /* forward declarations */
84 void usage(void);
85 void shut_down(int code);
86 
87 static int code = 0;
88 
do_user(const char * userid,void * rock)89 static int do_user(const char *userid, void *rock)
90 {
91     printf("Reconstructing DAV DB for %s...\n", userid);
92 
93     return dav_reconstruct_user(userid, (const char *)rock);
94 }
95 
main(int argc,char ** argv)96 int main(int argc, char **argv)
97 {
98     int opt, r;
99     char *alt_config = NULL;
100     int allusers = 0;
101     const char *audit_tool = NULL;
102 
103     while ((opt = getopt(argc, argv, "C:A:a")) != EOF) {
104         switch (opt) {
105         case 'C': /* alt config file */
106             alt_config = optarg;
107             break;
108 
109         case 'a':
110             allusers = 1;
111             break;
112 
113         case 'A':
114             audit_tool = optarg;
115             break;
116 
117         default:
118             usage();
119         }
120     }
121 
122     cyrus_init(alt_config, "dav_reconstruct", 0, 0);
123 
124     /* Set namespace -- force standard (internal) */
125     if ((r = mboxname_init_namespace(&recon_namespace, 1)) != 0) {
126         syslog(LOG_ERR, "%s", error_message(r));
127         fatal(error_message(r), EX_CONFIG);
128     }
129 
130     signals_set_shutdown(&shut_down);
131     signals_add_handlers(0);
132     sqldb_init();
133 
134     /* Initialize libical */
135     ical_support_init();
136 
137     if (allusers) {
138         mboxlist_alluser(do_user, (void *)audit_tool);
139     }
140     else if (optind == argc) {
141          usage();
142     }
143     else {
144         int i;
145         for (i = optind; i < argc; i++)
146             do_user(argv[i], (void *)audit_tool);
147     }
148 
149     exit(code);
150 }
151 
152 
usage(void)153 void usage(void)
154 {
155     fprintf(stderr,
156             "usage: dav_reconstruct [-C <alt_config>] userid\n");
157     exit(EX_USAGE);
158 }
159 
160 /*
161  * Cleanly shut down and exit
162  */
163 void shut_down(int code) __attribute__((noreturn));
shut_down(int code)164 void shut_down(int code)
165 {
166     in_shutdown = 1;
167 
168     mboxlist_close();
169     mboxlist_done();
170     sqldb_done();
171     exit(code);
172 }
173