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