]> Pileus Git - ~andy/fetchmail/blob - rcfile_l.l
Fix segfault when run control file ends with a backslash inside an
[~andy/fetchmail] / rcfile_l.l
1 %{
2
3 /*
4  * rcfile_l.l -- lexer for the run control file
5  *
6  * For license terms, see the file COPYING in this directory.
7  */
8 #include <string.h>
9
10 #include "config.h"
11 #include "fetchmail.h"
12 #include "rcfile_y.h"
13
14 int prc_lineno = 1;
15
16 #ifdef LEXDEBUG
17 #define SETSTATE(n)     do {BEGIN(n); if (yydebug) fprintf(stderr, "Entering lexer state %d\n", n);} while (0)
18 #else
19 #define SETSTATE(n)     BEGIN(n)
20 #endif /* LEXDEBUG */
21 %}
22
23 /* this doesn't work with Linux lex, see the INSTALL file */
24 %o 7000
25 %a 4000
26 %p 3000
27 %option nounput
28
29 %s NAME AUTH
30
31 %%
32
33 \"[^\"]*\"      {
34                         yytext[yyleng-1] = '\0';
35                         escapes(yytext+1, yytext);
36                         yyleng = strlen(yytext);
37                         yylval.sval = yytext;
38                         SETSTATE(0);
39                         return STRING;
40                 }
41 \'[^\']*\'      {
42                         yytext[yyleng-1] = '\0';
43                         escapes(yytext+1, yytext);
44                         yyleng = strlen(yytext);
45                         yylval.sval = yytext;
46                         SETSTATE(0);
47                         return STRING;
48                 }
49
50 "*"             { BEGIN(0); return WILDCARD; }
51
52 <NAME>[^=;:, \t\r\n]+   {
53                         static char *in;
54                         static size_t ins;
55
56                         if (yyleng + 1 > ins) {
57                             ins = yyleng + 1;
58                             in = xrealloc(in, ins);
59                         }
60                         memcpy(in, yytext, yyleng);
61                         in[yyleng] = '\0';
62                         escapes(in, in);
63                         yyleng = strlen(in);
64                         yylval.sval = in;
65                         SETSTATE(0);
66                         return STRING;
67                 }
68
69 set             { return SET; }
70 logfile         { return LOGFILE; }
71 idfile          { return IDFILE; }
72 daemon          { return DAEMON; }
73 syslog          { return SYSLOG; }
74 invisible       { return INVISIBLE; }
75 showdots        { return SHOWDOTS; }
76 postmaster      { return POSTMASTER; }
77 bouncemail      { return BOUNCEMAIL; }
78 spambounce      { return SPAMBOUNCE; }
79 warnings        { return WARNINGS; }
80 tracepolls      { return TRACEPOLLS; }
81
82 defaults        { return DEFAULTS; }
83 server          { return POLL; }
84 poll            { return POLL; }
85 skip            { return SKIP; }
86 via             { return VIA; }
87 aka             { return AKA; }
88 local(domains)  { return LOCALDOMAINS; }
89 proto(col)?     { return PROTOCOL; }
90 service         { return SERVICE; }
91 port            { return PORT; }
92 interval        { return INTERVAL; }
93 preauth(enticate)?      { SETSTATE(AUTH); return AUTHENTICATE; }
94 auth(enticate)? { SETSTATE(AUTH); return AUTHENTICATE; }
95 any             { SETSTATE(0); yylval.proto = A_ANY; return AUTHTYPE;}
96 gssapi          { SETSTATE(0); yylval.proto = A_GSSAPI; return AUTHTYPE;}
97 kerberos(_v)?4  { SETSTATE(0); yylval.proto = A_KERBEROS_V4; return AUTHTYPE;}
98 kerberos(_v)?5  { SETSTATE(0); yylval.proto = A_KERBEROS_V5; return AUTHTYPE;}
99 kerberos        { SETSTATE(0); yylval.proto = A_KERBEROS_V4; return AUTHTYPE;}
100 ssh             { SETSTATE(0); yylval.proto = A_SSH; return AUTHTYPE;}
101 (otp|opie)      { SETSTATE(0); yylval.proto = A_OTP; return AUTHTYPE;}
102 cram(-md5)?     { SETSTATE(0); yylval.proto = A_CRAM_MD5; return AUTHTYPE;}
103 msn             { SETSTATE(0); yylval.proto = A_MSN; return AUTHTYPE;}
104 ntlm            { SETSTATE(0); yylval.proto = A_NTLM; return AUTHTYPE;}
105 <AUTH>password  { SETSTATE(0); yylval.proto = A_PASSWORD; return AUTHTYPE;}
106 timeout         { return TIMEOUT;}
107 envelope        { return ENVELOPE; }
108 qvirtual        { return QVIRTUAL; }
109 principal       { return PRINCIPAL; }
110 esmtpname       { return ESMTPNAME; }
111 esmtppassword   { return ESMTPPASSWORD; }
112
113
114 user(name)?     {SETSTATE(NAME); return USERNAME; }
115 <INITIAL,NAME>pass(word)?       {SETSTATE(NAME); return PASSWORD; }
116 folder(s)?      { return FOLDER; }
117 smtp(host)?     { return SMTPHOST; }
118 fetchdomains    { return FETCHDOMAINS; }
119 smtpaddress     { return SMTPADDRESS; }
120 smtpname        { return SMTPNAME; }
121 antispam        { return SPAMRESPONSE; }
122 mda             { return MDA; }
123 bsmtp           { return BSMTP; }
124 lmtp            { return LMTP; }
125 pre(connect)?   { return PRECONNECT; }
126 post(connect)?  { return POSTCONNECT; }
127 interface       { return INTERFACE; }
128 monitor         { return MONITOR; }
129 plugin          { return PLUGIN; }
130 plugout         { return PLUGOUT; }
131 batchlimit      { return BATCHLIMIT; }
132 fetchlimit      { return FETCHLIMIT; }
133 fetchsizelimit  { return FETCHSIZELIMIT; }
134 fastuidl        { return FASTUIDL; }
135 expunge         { return EXPUNGE; }
136 properties      { return PROPERTIES; }
137
138 is              { SETSTATE(NAME); return IS; }
139 here            { return HERE; }
140 there           { return THERE; }
141 to              { SETSTATE(NAME); return TO; }
142 =               { SETSTATE(NAME); return MAP; }
143
144 nobouncemail    |
145 nouidl          |
146 nocheckalias    |
147 nodns           |
148 noenvelope      |
149 nokeep          |
150 noflush         |
151 nolimitflush    |
152 nofetchall      |
153 norewrite       |
154 noforcecr       |
155 nostripcr       |
156 nopass8(bits)?  |
157 nodropstatus    |
158 nodropdelivered |
159 nomimedec(ode)? |
160 nospambounce    |
161 noidle          {
162                    yyless(2);
163                    return NO;
164                 }
165
166 no              {return NO;}
167
168 keep            { return KEEP; }
169 flush           { return FLUSH; }
170 limitflush      { return LIMITFLUSH; }
171 fetchall        { return FETCHALL; }
172 rewrite         { return REWRITE; }
173 forcecr         { return FORCECR; }
174 stripcr         { return STRIPCR; }
175 pass8(bits)?    { return PASS8BITS; }
176 dropstatus      { return DROPSTATUS; }
177 dropdelivered   { return DROPDELIVERED; }
178 mimedec(ode)?   { return MIMEDECODE; }
179 idle            { return IDLE; }
180 dns             { return DNS; }
181 uidl            { return UIDL; }
182 ssl             { return SSL; }
183 sslkey          { return SSLKEY; }
184 sslcert         { return SSLCERT; }
185 sslproto        { return SSLPROTO; }
186 sslcertck       { return SSLCERTCK; }
187 sslcertpath     { return SSLCERTPATH; }
188 sslfingerprint  { return SSLFINGERPRINT; }
189 checkalias      { return CHECKALIAS; }
190
191 limit           { return LIMIT; }
192
193 with            {/* EMPTY */}
194 and             {/* EMPTY */}
195 has             {/* EMPTY */}
196 wants           {/* EMPTY */}
197 options         {/* EMPTY */}
198 [;:,]           {/* EMPTY */}
199
200 (auto)|(AUTO)   { yylval.proto = P_AUTO;  return PROTO; }
201 (pop2)|(POP2)   { yylval.proto = P_POP2;  return PROTO; }
202 (sdps)|(SDPS)   { return SDPS; }
203 (pop3)|(POP3)   { yylval.proto = P_POP3;  return PROTO; }
204 (imap)|(IMAP)   { yylval.proto = P_IMAP;  return PROTO; }
205 (apop)|(APOP)   { yylval.proto = P_APOP;  return PROTO; }
206 (rpop)|(RPOP)   { yylval.proto = P_RPOP;  return PROTO; }
207 (etrn)|(ETRN)   { yylval.proto = P_ETRN;  return PROTO; }
208 (odmr)|(ODMR)   { yylval.proto = P_ODMR;  return PROTO; }
209 (kpop)|(KPOP)   { return KPOP; }
210
211 (#.*)?\\?\n     { prc_lineno++; }   /* newline is ignored */
212
213 -?[0-9]+        { yylval.number = atoi(yytext); return NUMBER; }
214
215 [^=;:, \t\r\n]+ {
216                         escapes(yytext, yytext);
217                         yyleng = strlen(yytext);
218                         yylval.sval = yytext;
219                         return STRING;
220                 }
221
222 [ \t\r]+        ;       /* whitespace */
223
224 %%
225
226 void escapes(cp, tp)
227 /* process standard C-style escape sequences in a string,
228  * this can never lengthen the output, so cp and tp may overlap as long
229  * as cp >= tp. */
230 const char      *cp;    /* source string with escapes */
231 char            *tp;    /* target buffer for digested string */
232 {
233     while (*cp)
234     {
235         int     cval = 0;
236
237         /* we MUST check for NUL explicitly, as strchr(string, 0) will
238          * always succeed! */
239         if (*cp == '\\' && cp[1] && strchr("0123456789xX", cp[1]))
240         {
241             char *dp;
242             const char *hex = "00112233445566778899aAbBcCdDeEfF";
243             int dcount = 0;
244
245             if (*++cp == 'x' || *cp == 'X')
246                 for (++cp; *cp && (dp = strchr(hex, *cp)) && (dcount++ < 2); cp++)
247                     cval = (cval * 16) + (dp - hex) / 2;
248             else if (*cp == '0')
249                 while (*cp && strchr("01234567",*cp) != (char*)NULL && (dcount++ < 3))
250                     cval = (cval * 8) + (*cp++ - '0');
251             else
252                 while (*cp && (strchr("0123456789",*cp)!=(char*)NULL)&&(dcount++ < 3))
253                     cval = (cval * 10) + (*cp++ - '0');
254         }
255         else if (*cp == '\\')           /* C-style character escapes */
256         {
257             switch (*++cp)
258             {
259             case '\n': cp++; continue;  /* backslash before LF to join lines */
260             case '\0': goto done;          /* ignore backslash at file end */
261             case '\\': cval = '\\'; break;
262             case 'n': cval = '\n'; break;
263             case 't': cval = '\t'; break;
264             case 'b': cval = '\b'; break;
265             case 'r': cval = '\r'; break;
266             default: cval = *cp;
267             }
268             cp++;
269         }
270         else
271             cval = *cp++;
272         *tp++ = cval;
273     }
274 done:
275     *tp = '\0';
276 }