]> Pileus Git - ~andy/fetchmail/blob - conf.c
Feature: bad-header {reject|pass}
[~andy/fetchmail] / conf.c
1 /*
2  * conf.c -- dump fetchmail configuration as Python dictionary initializer
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8 #include "tunable.h"
9
10 #include <stdio.h>
11 #include <ctype.h>
12 #if defined(STDC_HEADERS)
13 #include <stdlib.h>
14 #endif
15 #if defined(HAVE_UNISTD_H)
16 #include <unistd.h>
17 #endif
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21
22 #include "fetchmail.h"
23
24 /* Python prettyprinting functions */
25
26 static int indent_level;
27
28 static void indent(char ic)
29 /* indent current line */
30 {
31     int i;
32
33     if (ic == ')' || ic == ']' || ic == '}')
34         indent_level--;
35
36     /*
37      * The guard here is a kluge.  It depends on the fact that in the
38      * particular structure we're dumping, opening [s are always
39      * initializers for dictionary members and thus will be preceded
40      * by a member name.
41      */
42     if (ic != '[')
43     {
44         for (i = 0; i < indent_level / 2; i++)
45             putc('\t', stdout);
46         if (indent_level % 2)
47             fputs("    ", stdout);
48     }
49
50     if (ic)
51     {
52         putc(ic, stdout);
53         putc('\n', stdout);
54     }
55
56     if (ic == '(' || ic == '[' || ic == '{')
57         indent_level++;
58 }
59
60
61 static void stringdump(const char *name, const char *member)
62 /* dump a string member with current indent */
63 {
64     indent('\0');
65     fprintf(stdout, "\"%s\":", name);
66     if (member)
67         fprintf(stdout, "\"%s\"", visbuf(member));
68     else
69         fputs("None", stdout);
70     fputs(",\n", stdout);
71 }
72
73 static void numdump(const char *name, const int num)
74 /* dump a numeric quantity at current indent */
75 {
76     indent('\0');
77     fprintf(stdout, "'%s':%d,\n", name, NUM_VALUE_OUT(num));
78 }
79
80 static void booldump(const char *name, const int onoff)
81 /* dump a boolean quantity at current indent */
82 {
83     indent('\0');
84     if (onoff)
85         fprintf(stdout, "'%s':TRUE,\n", name);
86     else
87         fprintf(stdout, "'%s':FALSE,\n", name);
88 }
89
90 static void listdump(const char *name, struct idlist *list)
91 /* dump a string list member with current indent */
92 {
93     indent('\0');
94     fprintf(stdout, "\"%s\":", name);
95
96     if (!list)
97         fputs("[],\n", stdout);
98     else
99     {
100         struct idlist *idp;
101
102         fputs("[", stdout);
103         for (idp = list; idp; idp = idp->next)
104             if (idp->id)
105             {
106                 fprintf(stdout, "\"%s\"", visbuf(idp->id));
107                 if (idp->next)
108                     fputs(", ", stdout);
109             }
110         fputs("],\n", stdout);
111     }
112 }
113
114 /*
115  * Note: this function dumps the entire configuration,
116  * after merging of the defaults record (if any).  It
117  * is intended to produce output parseable by a configuration
118  * front end, not anything especially comfortable for humans.
119  */
120
121 void dump_config(struct runctl *runp, struct query *querylist)
122 /* dump the in-core configuration in recompilable form */
123 {
124     struct query *ctl;
125     struct idlist *idp;
126     const char *features;
127
128     indent_level = 0;
129
130     /*
131      * These had better match the values fetchmailconf is expecting!
132      * (We don't want to import them from Tkinter because the user
133      * might not have it installed.)
134      */
135     fputs("TRUE=1; FALSE=0\n\n", stdout);
136
137     /*
138      * We need this in order to know whether `interface' and `monitor'
139      * are valid options or not.
140      */
141 #if defined(linux)
142     fputs("os_type = 'linux'\n", stdout);
143 #elif defined(__FreeBSD__)
144     fputs("os_type = 'freebsd'\n", stdout);
145 #else
146     fputs("os_type = 'generic'\n", stdout);
147 #endif
148
149     /* 
150      * This should be approximately in sync with the -V option dumping 
151      * in fetchmail.c.
152      */
153     features = "feature_options = ("
154 #ifdef POP2_ENABLE
155     "'pop2',"
156 #endif /* POP2_ENABLE */
157 #ifdef POP3_ENABLE
158     "'pop3',"
159 #endif /* POP3_ENABLE */
160 #ifdef IMAP_ENABLE
161     "'imap',"
162 #endif /* IMAP_ENABLE */
163 #ifdef GSSAPI
164     "'gssapi',"
165 #endif /* GSSAPI */
166 #if defined(KERBEROS_V4)
167     "'kerberos',"
168 #endif /* defined(IMAP4) */
169 #ifdef RPA_ENABLE
170     "'rpa',"
171 #endif /* RPA_ENABLE */
172 #ifdef SDPS_ENABLE
173     "'sdps',"
174 #endif /* SDPS_ENABLE */
175 #ifdef ETRN_ENABLE
176     "'etrn',"
177 #endif /* ETRN_ENABLE */
178 #ifdef ODMR_ENABLE
179     "'odmr',"
180 #endif /* ODMR_ENABLE */
181 #ifdef SSL_ENABLE
182     "'ssl',"
183 #endif /* SSL_ENABLE */
184 #ifdef OPIE_ENABLE
185     "'opie',"
186 #endif /* OPIE_ENABLE */
187 #ifdef HAVE_SOCKS
188     "'socks',"
189 #endif /* HAVE_SOCKS */
190     ")\n";
191     fputs(features, stdout);
192
193     fputs("# Start of configuration initializer\n", stdout);
194     fputs("fetchmailrc = ", stdout);
195     indent('{');
196
197     numdump("poll_interval", runp->poll_interval);
198     stringdump("logfile", runp->logfile);
199     stringdump("idfile", runp->idfile);
200     stringdump("postmaster", runp->postmaster);
201     booldump("bouncemail", runp->bouncemail);
202     booldump("spambounce", runp->spambounce);
203     booldump("softbounce", runp->softbounce);
204     stringdump("properties", runp->properties);
205     booldump("invisible", runp->invisible);
206     booldump("showdots", runp->showdots);
207     booldump("syslog", runp->use_syslog);
208
209     if (!querylist)
210     {
211         fputs("    'servers': []\n", stdout);
212         goto alldone;
213     }
214
215     indent(0);
216     fputs("# List of server entries begins here\n", stdout);
217     indent(0);
218     fputs("'servers': ", stdout);
219     indent('[');
220
221     for (ctl = querylist; ctl; ctl = ctl->next)
222     {
223         /*
224          * First, the server stuff.
225          */
226         if (!ctl->server.lead_server)
227         {
228             flag        using_kpop;
229
230             /*
231              * Every time we see a leading server entry after the first one,
232              * it implicitly ends the both (a) the list of user structures
233              * associated with the previous entry, and (b) that previous entry.
234              */
235             if (ctl > querylist)
236             {
237                 indent(']');
238                 indent('}');
239                 indent('\0'); 
240                 putc(',', stdout);
241                 putc('\n', stdout);
242             }
243
244             indent(0);
245             fprintf(stdout,"# Entry for site `%s' begins:\n",ctl->server.pollname);
246             indent('{');
247
248             using_kpop =
249                 (ctl->server.protocol == P_POP3 &&
250                  ctl->server.service && !strcmp(ctl->server.service, KPOP_PORT ) &&
251                  ctl->server.authenticate == A_KERBEROS_V4);
252
253             stringdump("pollname", ctl->server.pollname); 
254             booldump("active", !ctl->server.skip); 
255             stringdump("via", ctl->server.via); 
256             stringdump("protocol", 
257                        using_kpop ? "KPOP" : showproto(ctl->server.protocol));
258             stringdump("service",  ctl->server.service);
259             numdump("timeout",  ctl->server.timeout);
260             numdump("interval", ctl->server.interval);
261
262             if (ctl->server.envelope == STRING_DISABLED)
263                 stringdump("envelope", NULL); 
264             else if (ctl->server.envelope == NULL)
265                 stringdump("envelope", "Received");             
266             else
267                 stringdump("envelope", ctl->server.envelope); 
268             numdump("envskip", ctl->server.envskip);
269             stringdump("qvirtual", ctl->server.qvirtual);
270  
271             if (ctl->server.authenticate == A_ANY)
272                 stringdump("auth", "any");
273             else if (ctl->server.authenticate == A_PASSWORD)
274                 stringdump("auth", "password");
275             else if (ctl->server.authenticate == A_OTP)
276                 stringdump("auth", "otp");
277             else if (ctl->server.authenticate == A_NTLM)
278                 stringdump("auth", "ntlm");
279             else if (ctl->server.authenticate == A_CRAM_MD5)
280                 stringdump("auth", "cram-md5");
281             else if (ctl->server.authenticate == A_GSSAPI)
282                 stringdump("auth", "gssapi");
283             else if (ctl->server.authenticate == A_KERBEROS_V4)
284                 stringdump("auth", "kerberos_v4");
285             else if (ctl->server.authenticate == A_KERBEROS_V5)
286                 stringdump("auth", "kerberos_v5");
287             else if (ctl->server.authenticate == A_SSH)
288                 stringdump("auth", "ssh");
289             else if (ctl->server.authenticate == A_OTP)
290                 stringdump("auth", "otp");
291             else if (ctl->server.authenticate == A_MSN)
292                 stringdump("auth", "msn");
293
294 #ifdef HAVE_RES_SEARCH
295             booldump("dns", ctl->server.dns);
296 #endif /* HAVE_RES_SEARCH */
297             booldump("uidl", ctl->server.uidl);
298
299             listdump("aka", ctl->server.akalist);
300             listdump("localdomains", ctl->server.localdomains);
301
302 #ifdef CAN_MONITOR
303             stringdump("interface", ctl->server.interface);
304             stringdump("monitor", ctl->server.monitor);
305 #endif
306
307             stringdump("plugin", ctl->server.plugin);
308             stringdump("plugout", ctl->server.plugout);
309             stringdump("principal", ctl->server.principal);
310             if (ctl->server.esmtp_name)
311                 stringdump("esmtpname",ctl->server.esmtp_name);
312             if (ctl->server.esmtp_password)
313                 stringdump("esmtppassword",ctl->server.esmtp_password);
314             booldump("tracepolls", ctl->server.tracepolls);
315             indent(0);
316             switch(ctl->server.badheader) {
317                 /* this is a hack - we map this to a boolean option for
318                  * fetchmailconf purposes */
319                 case BHREJECT: puts("'badheader': FALSE,"); break;
320                 case BHPASS:   puts("'badheader': TRUE,"); break;
321             }
322
323             indent(0);
324             fputs("'users': ", stdout);
325             indent('[');
326         }
327
328         indent('{');
329
330         stringdump("remote", ctl->remotename);
331         stringdump("password", ctl->password);
332
333         indent('\0');
334         fprintf(stdout, "'localnames':[");
335         for (idp = ctl->localnames; idp; idp = idp->next)
336         {
337             char namebuf[USERNAMELEN + 1];
338
339             strlcpy(namebuf, visbuf(idp->id), sizeof(namebuf));
340             if (idp->val.id2)
341                 fprintf(stdout, "(\"%s\", %s)", namebuf, visbuf(idp->val.id2));
342             else
343                 fprintf(stdout, "\"%s\"", namebuf);
344             if (idp->next)
345                 fputs(", ", stdout);
346         }
347         if (ctl->wildcard)
348             fputs(", '*'", stdout);
349         fputs("],\n", stdout);
350
351         booldump("fetchall", ctl->fetchall);
352         booldump("keep", ctl->keep);
353         booldump("flush", ctl->flush);
354         booldump("limitflush", ctl->limitflush);
355         booldump("rewrite", ctl->rewrite);
356         booldump("stripcr", ctl->stripcr); 
357         booldump("forcecr", ctl->forcecr);
358         booldump("pass8bits", ctl->pass8bits);
359         booldump("dropstatus", ctl->dropstatus);
360         booldump("dropdelivered", ctl->dropdelivered);
361         booldump("mimedecode", ctl->mimedecode);
362         booldump("idle", ctl->idle);
363
364         stringdump("mda", ctl->mda);
365         stringdump("bsmtp", ctl->bsmtp);
366         indent('\0');
367         if (ctl->listener == LMTP_MODE)
368             fputs("'lmtp':TRUE,\n", stdout);
369         else
370             fputs("'lmtp':FALSE,\n", stdout);
371             
372         stringdump("preconnect", ctl->preconnect);
373         stringdump("postconnect", ctl->postconnect);
374         numdump("limit", ctl->limit);
375         numdump("warnings", ctl->warnings);
376         numdump("fetchlimit", ctl->fetchlimit);
377         numdump("fetchsizelimit", ctl->fetchsizelimit);
378         numdump("fastuidl", ctl->fastuidl);
379         numdump("batchlimit", ctl->batchlimit);
380 #ifdef SSL_ENABLE
381         booldump("ssl", ctl->use_ssl);
382         stringdump("sslkey", ctl->sslkey);
383         stringdump("sslcert", ctl->sslcert);
384         stringdump("sslproto", ctl->sslproto);
385         booldump("sslcertck", ctl->sslcertck);
386         stringdump("sslcertpath", ctl->sslcertpath);
387         stringdump("sslcommonname", ctl->sslcommonname);
388         stringdump("sslfingerprint", ctl->sslfingerprint);
389 #endif /* SSL_ENABLE */
390         numdump("expunge", ctl->expunge);
391         stringdump("properties", ctl->properties);
392         listdump("smtphunt", ctl->smtphunt);
393         listdump("fetchdomains", ctl->domainlist);
394         stringdump("smtpaddress", ctl->smtpaddress);
395         stringdump("smtpname", ctl->smtpname);
396
397         indent('\0');
398         fprintf(stdout, "'antispam':'");
399         for (idp = ctl->antispam; idp; idp = idp->next)
400         {
401             fprintf(stdout, "%d", idp->val.status.num);
402             if (idp->next)
403                 fputs(" ", stdout);
404         }
405         fputs("',\n", stdout);
406         listdump("mailboxes", ctl->mailboxes);
407
408         indent('}');
409         indent('\0'); 
410         fputc(',', stdout);
411     }
412
413     /* end last span of user entries and last server entry */
414     indent(']');
415     indent('}');
416
417     /* end array of servers */
418     indent(']');
419
420  alldone:
421     /* end top-level dictionary */
422     indent('}');
423     fputs("# End of initializer\n", stdout);
424 }
425
426 /* conf.c ends here */