Debian Codegraph @

hfaxd/Parser.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1995-1996 Sam Leffler
4 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * HylaFAX Client-Server Protocol Parser.
29 */
30 #include "port.h"
31 #include "config.h"
32 #include "Sys.h"
33 #include "Socket.h"
34 #include "HylaFAXServer.h"
35 #include "Dispatcher.h"
36
37 #include <ctype.h>
38
39 #define N(a) (sizeof (a) / sizeof (a[0]))
40
41 /*
42 * Standard protocol commands.
43 */
44 static const tab cmdtab[] = {
45 { "ABOR", T_ABOR, false, true, "[modem] (abort operation)" },
46 { "ACCT", T_ACCT, false,false, "(specify account)" },
47 { "ADMIN", T_ADMIN, true, true, "password" },
48 { "ALLO", T_ALLO, false,false, "(allocate disk space)" },
49 { "ANSWER", T_ANSWER, true, true, "modem [DATA|VOICE|FAX]" },
50 { "APPE", T_APPE, true, true, "file-name" },
51 { "CWD", T_CWD, true, true, "[directory-name]" },
52 { "CDUP", T_CDUP, true, true, "(change directory up one level)"},
53 { "CHMOD", T_CHMOD, true, true, "file-name mode" },
54 { "CHOWN", T_CHOWN, true, true, "file-name user" },
55 { "DELE", T_DELE, true, true, "file-name" },
56 { "DISABLE", T_DISABLE, true, true, "modem [reason]" },
57 { "ENABLE", T_ENABLE, true, true, "modem" },
58 { "EPRT", T_EPRT, true, true, "|f|addr|port|" },
59 { "EPSV", T_EPSV, true, true, "(set server in passive mode with extended result)" },
60 { "HELP", T_HELP, false, true, "[<string>]" },
61 { "FILEFMT", T_FILEFMT, true, true, "[format-string]" },
62 { "FILESORTFMT", T_FILESFMT, true, true, "[format-string]" },
63 { "FORM", T_FORM, true, true, "format-type" },
64 { "IDLE", T_IDLE, true, true, "[max-idle-timeout]" },
65 { "JDELE", T_JDELE, true, true, "[job-id]" },
66 { "JINTR", T_JINTR, true, true, "[job-id]" },
67 { "JKILL", T_JKILL, true, true, "[job-id]" },
68 { "JNEW", T_JNEW, true, true, "" },
69 { "JOB", T_JOB, true, true, "[job-id]" },
70 { "JOBFMT", T_JOBFMT, true, true, "[format-string]" },
71 { "JOBSORTFMT", T_JOBSFMT, true, true, "[format-string]" },
72 { "JPARM", T_JPARM, true, true, "[parm-name [parm-value]]" },
73 { "JREST", T_JREST, true, true, "(reset current job state)" },
74 { "JSUBM", T_JSUB, true, true, "[job-id]" },
75 { "JSUSP", T_JSUSP, true, true, "[job-id]" },
76 { "JWAIT", T_JWAIT, true, true, "[job-id]" },
77 { "JGDELE", T_JGDELE, true,false, "[jobgroup-id]" },
78 { "JGINTR", T_JGINTR, true,false, "[jobgroup-id]" },
79 { "JGKILL", T_JGKILL, true,false, "[jobgroup-id]" },
80 { "JGNEW", T_JGNEW, true, true, "" },
81 { "JGPARM", T_JGPARM, true,false, "parm-name [parm-value]" },
82 { "JGREST", T_JGREST, true,false, "(reset current job group state)"},
83 { "JGRP", T_JGRP, true,false, "[jobgroup-id]" },
84 { "JGSUBM", T_JGSUB, true,false, "[jobgroup-id]" },
85 { "JGSUSP", T_JGSUSP, true,false, "[jobgroup-id]" },
86 { "JGWAIT", T_JGWAIT, true,false, "[jobgroup-id]" },
87 { "LIST", T_LIST, true, true, "[path-name]" },
88 { "MDTM", T_MDTM, true, true, "path-name" },
89 { "MODE", T_MODE, false, true, "(specify transfer mode)" },
90 { "MDMFMT", T_MODEMFMT, true, true, "[format-string]" },
91 { "MDMSORTFMT", T_MODEMSFMT, true, true, "[format-string]" },
92 { "NLST", T_NLST, true, true, "[path-name]" },
93 { "NOOP", T_NOOP, false, true, "" },
94 { "PASS", T_PASS, false, true, "password" },
95 { "PASV", T_PASV, true, true, "(set server in passive mode)" },
96 { "PORT", T_PORT, true, true, "a0,a1,a2,a3,p0,p1" },
97 { "PWD", T_PWD, true, true, "(print working directory)" },
98 { "QUIT", T_QUIT, false, true, "(terminate service)", },
99 { "RCVFMT", T_RCVFMT, true, true, "[format-string]" },
100 { "RCVSORTFMT", T_RCVSFMT, true, true, "[format-string]" },
101 { "REIN", T_REIN, false, true, "(reinitialize server state)" },
102 { "REST", T_REST, true, true, "restart-marker" },
103 { "RETP", T_RETP, true, true, "file-name" },
104 { "RETR", T_RETR, true, true, "file-name" },
105 { "RNFR", T_RNFR, true,false, "file-name" },
106 { "RNTO", T_RNTO, true,false, "file-name" },
107 { "SHUT", T_SHUT, true, true, "NOW|HHSS|YYYYMMDDHHSS [reason]" },
108 { "SITE", T_SITE, true, true, "site-cmd [arguments]" },
109 { "SIZE", T_SIZE, true, true, "path-name" },
110 { "STAT", T_STAT, false, true, "[path-name]" },
111 { "STOR", T_STOR, true, true, "file-name" },
112 { "STOT", T_STOT, true, true, "(store unique temporary)" },
113 { "STOU", T_STOU, true, true, "(store unique)" },
114 { "STRU", T_STRU, false, true, "file-structure" },
115 { "SYST", T_SYST, true, true, "(return system type)" },
116 { "TZONE", T_TZONE, true, true, "[GMT|LOCAL]" },
117 { "TYPE", T_TYPE, false, true, "transfer-type" },
118 { "USER", T_USER, false, true, "username" },
119 { "VRFY", T_VRFY, false, true, "dialstring" },
120 };
121
122 /*
123 * Job parameter commands/keys.
124 */
125 static const tab parmtab[] = {
126 { "ACCTINFO", T_ACCTINFO, false,false, "[<string>]" },
127 { "BEGBR", T_BEGBR, false, true, "[ANY|bit-rate]" },
128 { "BEGST", T_BEGST, false, true, "[ANY|scanline-time]" },
129 { "CHOPTHRESHOLD",T_CHOPTHRESH, false, true, "[inches]" },
130 { "CLIENT", T_CLIENT, false, true, "[<string>]" },
131 { "COMMENTS", T_COMMENTS, false, true, "[<string>]" },
132 { "COMMID", T_COMMID, false, true, "(communication identifier)" },
133 { "COVER", T_COVER, false, true, "path-name" },
134 { "DATAFORMAT", T_DATAFORMAT, false, true, "[ANY|G31D|G32D|G4]" },
135 { "DIALSTRING", T_DIALSTRING, false, true, "[<string>]" },
136 { "DOCUMENT", T_DOCUMENT, false, true, "path-name" },
137 { "DONEOP", T_DONEOP, false, true, "[<string>]" },
138 { "EXTERNAL", T_EXTERNAL, false, true, "[<string>]" },
139 { "ERRORCODE", T_ERRORCODE, false, true, "[<string>]" }, // For compatiblity
140 { "FAXNUMBER", T_FAXNUMBER, false, true, "[<string>]" },
141 { "FROMCOMPANY", T_FROM_COMPANY, false, true, "[<string>]" },
142 { "FROMLOCATION", T_FROM_LOCATION,false, true, "[<string>]" },
143 { "FROMUSER", T_FROM_USER, false, true, "[<string>]" },
144 { "FROMVOICE", T_FROM_VOICE, false, true, "[<string>]" },
145 { "GROUPID", T_GROUPID, false, true, "(job group identifier)" },
146 { "HRES", T_HRES, false,false, "[dots-per-inch]" },
147 { "JOBID", T_JOBID, false, true, "(job identifier)" },
148 { "JOBINFO", T_JOBINFO, false, true, "[<string>]" },
149 { "JOBTYPE", T_JOBTYPE, false, true, "(job type)" },
150 { "LASTTIME", T_LASTTIME, false, true, "[DDHHSS]" },
151 { "MAXDIALS", T_MAXDIALS, false, true, "[<number>]" },
152 { "MAXPAGES", T_MAXPAGES, false, true, "[<number>]" },
153 { "MAXTRIES", T_MAXTRIES, false, true, "[<number>]" },
154 { "MINBR", T_MINBR, false, true, "[ANY|bit-rate]" },
155 { "MODEM", T_MODEM, false, true, "[device|class]" },
156 { "NDIALS", T_NDIALS, false, true, "[<number>]" },
157 { "NOTIFY", T_NOTIFY, false, true, "[NONE|DONE|REQUEUE|DONE+REQUEUE]" },
158 { "NOTIFYADDR", T_NOTIFYADDR, false, true, "[email-address]" },
159 { "NPAGES", T_NPAGES, false, true, "[<number>]" },
160 { "NTRIES", T_NTRIES, false, true, "[<number>]" },
161 { "OWNER", T_OWNER, false, true, "[<name>|<number>]" },
162 { "PAGECHOP", T_PAGECHOP, false, true, "[DEFAULT|NONE|ALL|LAST]" },
163 { "PAGELENGTH", T_PAGELENGTH, false, true, "[millimeters]" },
164 { "PAGERANGE", T_PAGERANGE, false, true, "[<page range>]" },
165 { "PAGEWIDTH", T_PAGEWIDTH, false, true, "[millimeters]" },
166 { "PASSWD", T_PASSWD, false, true, "[<string>]" },
167 { "POLL", T_POLL, false, true, "selector [passwd]" },
168 { "REGARDING", T_REGARDING, false, true, "[<string>]" },
169 { "RETRYTIME", T_RETRYTIME, false, true, "[HHSS]" },
170 { "SCHEDPRI", T_SCHEDPRI, false, true, "[<number>]" },
171 { "SENDTIME", T_SENDTIME, false, true, "[NOW|YYYYMMDDHHSS]" },
172 { "STATE", T_STATE, false, true, "(job state)" },
173 { "STATUS", T_STATUS, false, true, "[string]" },
174 { "STATUSCODE", T_STATUSCODE, false, true, "[code]" },
175 { "SUBADDR", T_SUBADDR, false, true, "[<string>]" },
176 { "TAGLINE", T_TAGLINE, false, true, "[<string>]" },
177 { "TOCOMPANY", T_TO_COMPANY, false, true, "[<string>]" },
178 { "TOLOCATION", T_TO_LOCATION, false, true, "[<string>]" },
179 { "TOTDIALS", T_TOTDIALS, false, true, "[<number>]" },
180 { "TOTPAGES", T_TOTPAGES, false, true, "[<number>]" },
181 { "TOTTRIES", T_TOTTRIES, false, true, "[<number>]" },
182 { "TOUSER", T_TO_USER, false, true, "[<string>]" },
183 { "TOVOICE", T_TO_VOICE, false, true, "[<string>]" },
184 { "TSI", T_TSI, false, true, "[<string>]" },
185 { "USECONTCOVER", T_USE_CONTCOVER,false, true, "[YES|NO]" },
186 { "USEXVRES", T_USE_XVRES, false, true, "[YES|NO]" },
187 { "USEECM", T_USE_ECM, false, true, "[YES|NO]" },
188 { "USETAGLINE", T_USE_TAGLINE, false, true, "[YES|NO]" },
189 { "USRKEY", T_USRKEY, false, true, "[<string>]" },
190 { "VRES", T_VRES, false, true, "[lines-per-inch]" },
191
192 { "HELP", T_HELP, false, true, "[<string>]" },
193 };
194
195 /*
196 * Site-specific commands.
197 */
198 static const tab sitetab[] = {
199 { "ADDMODEM", T_ADDMODEM, false,false, "modem [speed]"},
200 { "ADDUSER", T_ADDUSER, false, true, "user-spec [passwd [adminwd]]"},
201 { "CONFIG", T_CONFIG, false, true, "[parm-name [parm-value]]" },
202 { "DELMODEM", T_DELMODEM, false,false, "modem" },
203 { "DELUSER", T_DELUSER, false, true, "user-spec" },
204 { "TRIGGER", T_TRIGGER, false, true, "spec" },
205 { "HELP", T_HELP, false, true, "[<string>]" },
206 { "LOCKWAIT", T_LOCKWAIT, false, true, "max-lockwait-timeout" },
207 };
208
209 static const tab*
210 lookup(const tab* p, u_int n, const char* cmd)
211 {
212 while (n != 0) {
213 if (strcmp(cmd, p->name) == 0)
214 return (p);
215 p++, n--;
216 }
217 return (NULL);
218 }
219 static const char*
220 tokenName(const tab* p, u_int n, Token t)
221 {
222 while (n != 0) {
223 if (p->token == t)
224 return (p->name);
225 p++, n--;
226 }
227 return ("???");
228 }
229 const char*
230 HylaFAXServer::cmdToken(Token t)
231 {
232 return tokenName(cmdtab, N(cmdtab), t);
233 }
234 const char*
235 HylaFAXServer::siteToken(Token t)
236 {
237 return tokenName(sitetab, N(sitetab), t);
238 }
239 const char*
240 HylaFAXServer::parmToken(Token t)
241 {
242 return tokenName(parmtab, N(parmtab), t);
243 }
244
245 u_int
246 HylaFAXServer::twodigits(const char* cp, u_int range)
247 {
248 return ((cp[0]-'0')*10 + (cp[1]-'0')) % range;
249 }
250 u_int
251 HylaFAXServer::fourdigits(const char* cp)
252 {
253 return (cp[0]-'0')*1000 +
254 (cp[1]-'0')*100 +
255 (cp[2]-'0')*10 +
256 (cp[3]-'0');
257 }
258 inline bool
259 isLoginToken(Token t)
260 {
261 return (t == T_USER || t == T_PASS || t == T_ADMIN);
262 }
263
264 /*
265 * Parse and process command input received on the
266 * control channel. This method is invoked whenever
267 * data is present on the control channel. We read
268 * everything and parse (and execute) as much as possible
269 * but do not block waiting for more data except when
270 * a partial line of input is received. This is done
271 * to ensure other processing will be handled in a
272 * timely fashion (e.g. processing of messages from
273 * the scheduler received via the FIFO).
274 */
275 int
276 HylaFAXServer::parse()
277 {
278 if (IS(WAITDATA)) { // recursive invocation
279 state &= ~S_WAITDATA;
280 return (0);
281 }
282 // stop control channel idle timeout
283 Dispatcher::instance().stopTimer(this);
284 pushToken(T_NIL); // reset state
285 for (;;) {
286 /*
287 * Fetch the next complete line of input received on the
288 * control channel. This call will fail when no more data
289 * is *currently* waiting on the control channel. Note
290 * that this does not mean the connection has dropped; just
291 * that data is not available at this instant. Note also
292 * that if a partial line of input is received a complete
293 * line will be waited for (see below).
294 */
295 if (!getCmdLine(cbuf, sizeof (cbuf)))
296 break;
297 /*
298 * Parse the line of input read above.
299 */
300 if (getToken(T_STRING, "command token")) {
301 tokenBody.raisecase();
302 const tab* p = lookup(cmdtab, N(cmdtab), tokenBody);
303 if (p == NULL)
304 reply(500, "%s: Command not recognized.",
305 (const char*) tokenBody);
306 else if (!p->implemented)
307 reply(502, "%s: Command not implemented.", p->name);
308 /*
309 * If the user is not privileged, then check for
310 * the service being shutdown if the command is
311 * not part of the login procedure (USER-PASS-ADMIN).
312 * This permits administrators to gain access to
313 * a system that is shutdown. Other users will get
314 * dropped if they type something else.
315 */
316 else if (!IS(PRIVILEGED) && !isLoginToken(p->token) &&
317 isShutdown(!IS(LOGGEDIN))) {
318 reply(221, "Server shutting down. Goodbye.");
319 dologout(0); // XXX
320 /*
321 * If command requires client to be logged in check
322 * for this. Note that some commands have variants
323 * that do not require the client be logged in--these
324 * cannot check here and must do it specially below.
325 */
326 } else if (p->checklogin && !checklogin(p->token))
327 ;
328 /*
329 * Command is valid, implemented, and the server
330 * is available to service it. If the syntax is
331 * correct then reset the number of consecutive
332 * bad commands. Note that since part of the syntax
333 * checking is login validation this logic will also
334 * catch people typing syntacitcally valid but
335 * unacceptable commands just to remain connected.
336 */
337 else if (cmd(p->token)) {
338 if (p->token != T_REST)
339 restart_point = 0;
340 consecutiveBadCmds = 0;
341 continue;
342 }
343 }
344 /*
345 * If too many consecutive bad commands have been
346 * received disconnect. This is to safeguard against
347 * a client that spews trash down the control connection.
348 */
349 if (++consecutiveBadCmds >= maxConsecutiveBadCmds) {
350 /*
351 * Check for shutdown so that any shutdown message
352 * will get prepended to client reply.
353 */
354 (void) isShutdown(!IS(LOGGEDIN));
355 reply(221, "Server shutting down. Goodbye.");
356 dologout(0);
357 }
358 }
359 Dispatcher::instance().startTimer(idleTimeout, 0, this);
360 return (0);
361 }
362
363 /*
364 * Protocol command (one line).
365 */
366 bool
367 HylaFAXServer::cmd(Token t)
368 {
369 fxStr s;
370 long n;
371
372 switch (t) {
373 case T_USER: // user name
374 if (string_param(s, "user name")) {
375 logcmd(t, "%s", (const char*) s);
376 userCmd(s);
377 return (true);
378 }
379 break;
380 case T_PASS: // user password
381 if (string_param(s, "password")) {
382 logcmd(t, "<password>");
383 passCmd(s);
384 return (true);
385 }
386 break;
387 case T_ADMIN: // administrator password
388 if (opt_CRLF())
389 s = "";
390 else if (!string_param(s, "password"))
391 break;
392 logcmd(t, "<password>");
393 adminCmd(s);
394 return (true);
395 case T_REIN: // reinitialize server
396 logcmd(t);
397 if (IS(LOGGEDIN))
398 (void) chdir("/"); // return to top of spooling area
399 initServer();
400 reply(220, "%s server (%s) ready.", (const char*) hostname, version);
401 break;
402 case T_REST: // restart data transfer
403 if (number_param(n)) {
404 logcmd(t, "%lu", n);
405 restart_point = n;
406 reply(350, "Data transfer will restart at %lu, "
407 "send transfer command", n);
408 return (true);
409 }
410 break;
411 case T_SYST: // system identification
412 if (CRLF()) {
413 logcmd(t);
414 // this is what we *emulate*
415 reply(215, "%s", (const char*) systemType);
416 return (true);
417 }
418 break;
419 case T_EPRT: // extended port for data transfer
420 case T_PORT: // port for data transfer
421 if (SPACE() && hostPort() && CRLF()) {
422 portCmd(t);
423 return (true);
424 }
425 break;
426 case T_EPSV: // extended passive mode
427 case T_PASV: // enable passive mode
428 if (CRLF()) {
429 logcmd(t);
430 passiveCmd();
431 return (true);
432 }
433 break;
434 case T_FORM: // document format
435 if (string_param(s, "document format")) {
436 logcmd(t, "%s", (const char*) s);
437 formCmd(s);
438 return (true);
439 }
440 break;
441 case T_MODE: // data transfer mode
442 if (string_param(s, "transfer mode")) {
443 logcmd(t, "%s", (const char*) s);
444 modeCmd(s);
445 return (true);
446 }
447 break;
448 case T_STRU: // data transfer file structure
449 if (string_param(s, "file structure")) {
450 logcmd(t, "%s", (const char*) s);
451 struCmd(s);
452 return (true);
453 }
454 break;
455 case T_TYPE: // data transfer type
456 if (string_param(s, "transfer type")) {
457 logcmd(t, "%s", (const char*) s);
458 typeCmd(s);
459 return (true);
460 }
461 break;
462 case T_RETP: // retrieve next page of document/file
463 if (pathname_param(s)) {
464 logcmd(t, "%s", (const char*) s);
465 retrievePageCmd(s);
466 return (true);
467 }
468 break;
469 case T_RETR: // retrieve document/file
470 if (pathname_param(s)) {
471 logcmd(t, "%s", (const char*) s);
472 retrieveCmd(s);
473 return (true);
474 }
475 break;
476 case T_STOR: // store document/file
477 if (pathname_param(s)) {
478 logcmd(t, "%s", (const char*) s);
479 // XXX file must exist if not admin, check for bad chars in pathname
480 storeCmd(s, "w");
481 return (true);
482 }
483 break;
484 case T_APPE: // append to document/file
485 if (pathname_param(s)) {
486 logcmd(t, "%s", (const char*) s);
487 storeCmd(s, "a");
488 return (true);
489 }
490 break;
491 case T_STOT: // store uniquely named document/file
492 if (CRLF()) {
493 logcmd(t);
494 storeUniqueCmd(true);
495 return (true);
496 }
497 break;
498 case T_STOU: // store uniquely named document/file
499 if (CRLF()) {
500 logcmd(t);
501 storeUniqueCmd(false);
502 return (true);
503 }
504 break;
505 case T_LIST: // list directory/file
506 case T_NLST: // list directory/file names
507 if (opt_CRLF()) {
508 logcmd(t);
509 if (t == T_LIST)
510 listCmd(".");
511 else
512 nlstCmd(".");
513 return (true);
514 } else if (pathname_param(s)) {
515 logcmd(t, "%s", (const char*) s);
516 if (t == T_LIST)
517 listCmd(s);
518 else
519 nlstCmd(s);
520 return (true);
521 }
522 break;
523 case T_CWD: // change working directory
524 if (opt_CRLF()) {
525 logcmd(t);
526 cwdCmd("/");
527 return (true);
528 } else if (pathname_param(s)) {
529 logcmd(t, "%s", (const char*) s);
530 cwdCmd(s);
531 return (true);
532 }
533 break;
534 case T_CDUP: // CWD ..
535 logcmd(t);
536 cwdCmd("..");
537 return (true);
538 case T_PWD: // print working directory
539 if (CRLF()) {
540 logcmd(t);
541 pwdCmd();
542 return (true);
543 }
544 break;
545 case T_STAT: // stat file/server status
546 if (opt_CRLF()) {
547 logcmd(t);
548 statusCmd();
549 return (true);
550 } else if (checklogin(T_STAT) && pathname_param(s)) {
551 logcmd(t, "%s", (const char*) s);
552 statFileCmd(s);
553 return (true);
554 }
555 break;
556 case T_SIZE: // return file size
557 if (pathname_param(s)) {
558 logcmd(t, "%s", (const char*) s);
559 struct stat sb;
560 if (Sys::stat(s, sb) < 0 || (sb.st_mode&S_IFMT) != S_IFREG)
561 reply(550, "%s: not a plain file.", (const char*) s);
562 else
563 reply(213, "%lu", (u_long) sb.st_size);
564 return (true);
565 }
566 break;
567 case T_CHMOD: // set file protection
568 if (SPACE() && STRING(s, "filename") && SPACE() && NUMBER(n)) {
569 chmodCmd(s, (u_int) n);
570 return (true);
571 }
572 break;
573 case T_MDTM: // return file last modification time
574 if (pathname_param(s)) {
575 logcmd(t, "%s", (const char*) s);
576 mdtmCmd(s);
577 return (true);
578 }
579 break;
580 case T_DELE: // delete file/document
581 if (pathname_param(s)) {
582 logcmd(t, "%s", (const char*) s);
583 deleCmd(s);
584 return (true);
585 }
586 break;
587 case T_ABOR: // abort active command/incoming call
588 if (opt_CRLF()) {
589 logcmd(t);
590 ack(225, cmdToken(t));
591 return (true);
592 } else if (checkadmin(T_ABOR) && string_param(s, "modem")) {
593 abortCallCmd(s);
594 return (true);
595 }
596 break;
597 case T_HELP: // return help
598 if (opt_CRLF()) {
599 logcmd(t);
600 helpCmd(cmdtab, (char*) NULL);
601 return (true);
602 } else if (string_param(s, "command name")) {
603 logcmd(t, "%s", (const char*) s);
604 s.raisecase();
605 if (s == "SITE")
606 helpCmd(sitetab, NULL);
607 else if (s == "JPARM" || s == "JGPARM")
608 helpCmd(parmtab, NULL);
609 else
610 helpCmd(cmdtab, s);
611 return (true);
612 }
613 break;
614 case T_JNEW: // create new job
615 if (CRLF()) {
616 newJobCmd();
617 return (true);
618 }
619 break;
620 case T_JOB: // select current job
621 if (job_param(s)) {
622 setCurrentJob(s);
623 return (true);
624 }
625 break;
626 case T_JREST: // reset job state
627 if (job_param(s)) {
628 resetJob(s);
629 return (true);
630 }
631 break;
632 case T_JDELE: // delete job
633 if (job_param(s)) {
634 deleteJob(s);
635 return (true);
636 }
637 break;
638 case T_JINTR: // interrupt job
639 if (job_param(s)) {
640 interruptJob(s);
641 return (true);
642 }
643 break;
644 case T_JKILL: // kill job
645 if (job_param(s)) {
646 killJob(s);
647 return (true);
648 }
649 break;
650 case T_JSUSP: // suspend job
651 if (job_param(s)) {
652 suspendJob(s);
653 return (true);
654 }
655 break;
656 case T_JSUB: // submit job
657 if (job_param(s)) {
658 submitJob(s);
659 return (true);
660 }
661 break;
662 case T_JWAIT: // wait for job
663 if (job_param(s)) {
664 waitForJob(s);
665 return (true);
666 }
667 break;
668 case T_JGNEW: // create job group
669 if (CRLF()) {
670 curJob->groupid = curJob->jobid;
671 ack(200, cmdToken(t));
672 return (true);
673 }
674 break;
675 case T_JGREST: // reset current job group state
676 if (CRLF()) {
677 return (true);
678 }
679 break;
680 case T_JGRP: // select current job group
681 if (jgrp_param(s)) {
682 return (true);
683 }
684 break;
685 case T_JGDELE: // delete job group
686 if (jgrp_param(s)) {
687 return (true);
688 }
689 break;
690 case T_JGINTR: // interrupt job group
691 if (jgrp_param(s)) {
692 return (true);
693 }
694 break;
695 case T_JGKILL: // kill job group
696 if (jgrp_param(s)) {
697 return (true);
698 }
699 break;
700 case T_JGSUSP: // suspend job group
701 if (jgrp_param(s)) {
702 return (true);
703 }
704 break;
705 case T_JGSUB: // submit job group
706 if (jgrp_param(s)) {
707 return (true);
708 }
709 break;
710 case T_JGWAIT: // wait for job group
711 if (jgrp_param(s)) {
712 return (true);
713 }
714 break;
715 case T_NOOP: // no-op
716 if (CRLF()) {
717 logcmd(t);
718 ack(200, cmdToken(t));
719 return (true);
720 }
721 break;
722 case T_IDLE: // set/query idle timeout
723 if (opt_CRLF()) {
724 logcmd(t);
725 reply(213, "%u seconds.", idleTimeout);
726 return (true);
727 } else if (number_param(n)) {
728 logcmd(t, "%lu", n);
729 if ((unsigned)n > maxIdleTimeout && !IS(PRIVILEGED)) {
730 idleTimeout = maxIdleTimeout;
731 reply(213, "%lu: Idle timeout too large, set to %u.",
732 n, maxIdleTimeout);
733 } else {
734 idleTimeout = (int) n;
735 reply(213, "Idle timeout set to %u.", idleTimeout);
736 }
737 return (true);
738 }
739 break;
740 case T_QUIT: // terminate session
741 if (CRLF()) {
742 logcmd(t);
743 reply(221, "Goodbye.");
744 dologout(0);
745 return (true);
746 }
747 break;
748 case T_TZONE: // set/query timezone usage
749 if (opt_CRLF()) {
750 reply(200, "Time values are handled in %s.",
751 IS(USEGMT) ? "GMT" : tzname[0]);
752 return (true);
753 } else if (string_param(s, "timezone specification")) {
754 logcmd(t, "%s", (const char*) s);
755 s.raisecase();
756 if (s == "GMT") {
757 state |= S_USEGMT;
758 reply(200, "Using time values in GMT.");
759 } else if (s == "LOCAL") {
760 state &= ~S_USEGMT;
761 reply(200, "Using time values in %s.", tzname[0]);
762 } else {
763 reply(503, "Unknown timezone specification %s.",
764 (const char*) s);
765 }
766 return (true);
767 }
768 break;
769 case T_JOBFMT: // query/specify job format string
770 case T_RCVFMT: // query/specify recvq format string
771 case T_MODEMFMT: // query/specify modem format string
772 case T_FILEFMT: // query/specify file format string
773 if (opt_CRLF()) {
774 reply(200, "%s",
775 t == T_JOBFMT ? (const char*) jobFormat :
776 t == T_RCVFMT ? (const char*) recvFormat:
777 t == T_MODEMFMT ? (const char*) modemFormat :
778 (const char*) fileFormat
779 );
780 return (true);
781 } else if (string_param(s, "format-string")) {
782 if (t == T_JOBFMT)
783 jobFormat = s;
784 else if (t == T_RCVFMT)
785 recvFormat = s;
786 else if (t == T_MODEMFMT)
787 modemFormat = s;
788 else if (t == T_FILEFMT)
789 fileFormat = s;
790 ack(200, cmdToken(t));
791 return (true);
792 }
793 break;
794 case T_JOBSFMT: // query/specify job format string
795 case T_RCVSFMT: // query/specify recvq format string
796 case T_MODEMSFMT: // query/specify modem format string
797 case T_FILESFMT: // query/specify file format string
798 if (opt_CRLF()) {
799 reply(200, "%s",
800 t == T_JOBSFMT ? (const char*) jobSortFormat :
801 t == T_RCVSFMT ? (const char*) recvSortFormat:
802 t == T_MODEMSFMT ? (const char*) modemSortFormat :
803 (const char*) fileSortFormat
804 );
805 return (true);
806 } else if (string_param(s, "format-string")) {
807 if (! allowSorting) {
808 reply(530, "Local policy disables sorting.");
809 break;
810 }
811 if (t == T_JOBSFMT)
812 jobSortFormat = s;
813 else if (t == T_RCVSFMT)
814 recvSortFormat = s;
815 else if (t == T_MODEMSFMT)
816 modemSortFormat = s;
817 else if (t == T_FILESFMT)
818 fileSortFormat = s;
819 ack(200, cmdToken(t));
820 return (true);
821 }
822 break;
823 case T_JPARM: // set/query job parameter
824 if (opt_CRLF()) {
825 logcmd(T_JPARM);
826 if (checkJobState(curJob))
827 jstatCmd(*curJob);
828 return (true);
829 } else if (SPACE() && getToken(T_STRING, "parameter name")) {
830 tokenBody.raisecase();
831 const tab* p = lookup(parmtab, N(parmtab), tokenBody);
832 if (p == NULL) {
833 reply(500, "JPARM %s: Parameter not recognized.",
834 (const char*) tokenBody);
835 } else if (!p->implemented)
836 reply(502, "JPARM %s: Parameter not implemented.", p->name);
837 else
838 return (param_cmd(p->token));
839 }
840 break;
841 case T_JGPARM: // set/query job group parameter
842 if (SPACE() && getToken(T_STRING, "parameter name")) {
843 tokenBody.raisecase();
844 const tab* p = lookup(parmtab, N(parmtab), tokenBody);
845 if (p == NULL) {
846 reply(500, "JPARM %s: Parameter not recognized.",
847 (const char*) tokenBody);
848 } else if (!p->implemented)
849 reply(502, "JPARM %s: Parameter not implemented.", p->name);
850 else
851 return (param_cmd(p->token));
852 }
853 break;
854 case T_SITE: // site-specific command
855 if (SPACE() && getToken(T_STRING, "site command")) {
856 tokenBody.raisecase();
857 const tab* p = lookup(sitetab, N(sitetab), tokenBody);
858 if (p == NULL) {
859 reply(500, "SITE %s: Command not recognized.",
860 (const char*) tokenBody);
861 } else if (!p->implemented)
862 reply(502, "SITE %s: Command not implemented.", p->name);
863 else
864 return (site_cmd(p->token));
865 }
866 break;
867 case T_ANSWER: // answer phone on specific line
868 if (checkadmin(T_ANSWER) && SPACE() && STRING(s, "modem")) {
869 fxStr how;
870 if (opt_CRLF())
871 how = "any";
872 else if (!string_param(how, "answer-how"))
873 break;
874 answerCallCmd(s, how);
875 return (true);
876 }
877 break;
878 case T_CHOWN: // assign file ownership
879 if (checkadmin(T_CHOWN) && SPACE() && STRING(s, "filename")) {
880 fxStr who;
881 if (string_param(who, "user")) {
882 chownCmd(s, who);
883 return (true);
884 }
885 }
886 break;
887 case T_DISABLE: // disable outbound-usage of modem
888 if (checkadmin(T_DISABLE) && SPACE() && STRING(s, "modem")) {
889 fxStr reason;
890 if (opt_CRLF())
891 reason = "<unspecified reason>";
892 else if (!SPACE() || !multi_STRING(reason))
893 break;
894 disableModemCmd(s, reason);
895 return (true);
896 }
897 break;
898 case T_ENABLE: // enable outbound-usage of modem
899 if (checkadmin(T_ENABLE) && string_param(s, "modem")) {
900 enableModemCmd(s);
901 return (true);
902 }
903 break;
904 case T_SHUT: // shutdown server
905 if (checkadmin(T_SHUT) && SPACE() && STRING(s, "shutdown-time")) {
906 fxStr reason;
907 if (opt_CRLF())
908 reason = "<unspecified reason>";
909 else if (!SPACE() || !multi_STRING(reason))
910 break;
911 const char* cp = s;
912 if (s.length() == 3 && strcasecmp(cp, "NOW") == 0) {
913 time_t t = Sys::now();
914 shutCmd(*localtime(&t), reason);
915 return (true);
916 } else if (s.length() == 4 && checkNUMBER(cp)) {
917 time_t t = Sys::now();
918 struct tm tm = *localtime(&t);
919 tm.tm_hour = 60*60*twodigits(cp, 60);
920 tm.tm_min = 60*twodigits(cp+2, 60);
921 shutCmd(tm, reason);
922 return (true);
923 } else if (s.length() == 12 && checkNUMBER(cp)) {
924 struct tm tm;
925 tm.tm_sec = 0;
926 tm.tm_min = twodigits(cp+10, 60);
927 tm.tm_hour = twodigits(cp+8, 24);
928 tm.tm_mday = twodigits(cp+6, 31);
929 tm.tm_mon = twodigits(cp+4, 12) -1;
930 tm.tm_year = fourdigits(cp+0) - 1900;
931 shutCmd(tm, reason);
932 return (true);
933 } else
934 syntaxError("bad time specification");
935 }
936 break;
937 case T_VRFY: // verify server to use for destination
938 if (string_param(s, "dialstring")) {
939 // XXX at least support a static file...
940 reply(200, "%s (%s)"
941 , (const char*) hostname
942 , (const char*) hostaddr
943 );
944 return (true);
945 }
946 break;
947 default:
948 break;
949 }
950 return (false);
951 }
952
953 /*
954 * Site-specific protocol commands (one line).
955 */
956 bool
957 HylaFAXServer::site_cmd(Token t)
958 {
959 fxStr s;
960 long n;
961
962 switch (t) {
963 case T_ADDUSER:
964 if (checkadmin(T_ADDUSER) && SPACE() && STRING(s, "user-spec")) {
965 fxStr pass;
966 if (opt_CRLF()) {
967 addUserCmd(s, "", "");
968 return (true);
969 } else if (SPACE() && STRING(pass, "password")) {
970 fxStr apass;
971 if (opt_CRLF()) {
972 addUserCmd(s, pass, "");
973 return (true);
974 } else if (string_param(apass, "admin-password")) {
975 addUserCmd(s, pass, apass);
976 return (true);
977 }
978 }
979 }
980 break;
981 case T_DELUSER:
982 if (checkadmin(T_DELUSER) && string_param(s, "user-spec")) {
983 delUserCmd(s);
984 return (true);
985 }
986 break;
987 case T_DELMODEM:
988 if (checkadmin(T_DELMODEM) && string_param(s, "modem")) {
989 delModemCmd(s);
990 return (true);
991 }
992 break;
993 case T_ADDMODEM:
994 if (checkadmin(T_ADDMODEM) && string_param(s, "modem")) {
995 addModemCmd(s);
996 return (true);
997 }
998 break;
999 case T_CONFIG:
1000 if (checkadmin(T_CONFIG) && SPACE() && STRING(s, "modem")) {
1001 fxStr config;
1002 if (opt_CRLF()) {
1003 configQueryCmd(s);
1004 return (true);
1005 } else if (SPACE() && multi_STRING(config)) {
1006 configCmd(s, config);
1007 return (true);
1008 }
1009 }
1010 break;
1011 case T_TRIGGER:
1012 if (string_param(s, "trigger-spec")) {
1013 logcmd(t, "%s", (const char*)s);
1014 triggerCmd("%s", (const char*) s);
1015 return (true);
1016 }
1017 break;
1018 case T_HELP: // return help
1019 if (opt_CRLF()) {
1020 helpCmd(sitetab, (char*) NULL);
1021 return (true);
1022 } else if (string_param(s, "command name")) {
1023 logcmd(T_SITE, "HELP %s", (const char*) s);
1024 s.raisecase();
1025 helpCmd(sitetab, s);
1026 return (true);
1027 }
1028 break;
1029 case T_LOCKWAIT:
1030 if (opt_CRLF()) {
1031 logcmd(t);
1032 reply(213, "%u seconds.", lockTimeout);
1033 return (true);
1034 } else if (number_param(n)) {
1035 logcmd(t, "%lu", n);
1036 if ((unsigned)n > maxLockTimeout && !IS(PRIVILEGED)) {
1037 lockTimeout = maxLockTimeout;
1038 reply(213, "%lu: Lock timeout too large, set to %u.",
1039 n, maxLockTimeout);
1040 } else {
1041 lockTimeout = (int) n;
1042 reply(213, "Lock timeout set to %u.", lockTimeout);
1043 }
1044 return (true);
1045 }
1046 default:
1047 break;
1048 }
1049 return (false);
1050 }
1051
1052 bool
1053 HylaFAXServer::param_cmd(Token t)
1054 {
1055 fxStr s;
1056 long n;
1057 time_t ticks;
1058 bool b;
1059
1060 switch (t) {
1061 case T_SENDTIME: // time to send job
1062 if (opt_CRLF()) {
1063 replyJobParamValue(*curJob, 213, t);
1064 return (true);
1065 } else if (SPACE() && getToken(T_STRING, "time specification")) {
1066 tokenBody.raisecase();
1067 if (tokenBody == "NOW") {
1068 if (CRLF() && setJobParameter(*curJob, t, (time_t) 0)) {
1069 reply(213, "%s set to NOW.", parmToken(t));
1070 return (true);
1071 }
1072 } else {
1073 pushToken(T_STRING); // XXX
1074 if (TIMESPEC(12, ticks) && CRLF() &&
1075 setJobParameter(*curJob, t, ticks)) {
1076 if (ticks != 0) {
1077 const struct tm* tm = cvtTime(ticks);
1078 // XXX should response include seconds?
1079 reply(213, "%s set to %4d%02d%02d%02d%02d%02d."
1080 , parmToken(t)
1081 , tm->tm_year+1900
1082 , tm->tm_mon+1
1083 , tm->tm_mday
1084 , tm->tm_hour
1085 , tm->tm_min
1086 , tm->tm_sec
1087 );
1088 } else
1089 reply(213, "%s set to NOW.", parmToken(t));
1090 return (true);
1091 }
1092 }
1093 }
1094 break;
1095 case T_LASTTIME: // time to kill job
1096 if (opt_CRLF()) {
1097 replyJobParamValue(*curJob, 213, t);
1098 return (true);
1099 } else if (timespec_param(6, ticks) &&
1100 setJobParameter(*curJob, t, ticks)) {
1101 reply(213, "%s set to %02d%02d%02d."
1102 , parmToken(t)
1103 , ticks/(24*60*60)
1104 , (ticks/(60*60))%24
1105 , (ticks/60)%60
1106 );
1107 return (true);
1108 }
1109 break;
1110 case T_RETRYTIME: // retry interval for job
1111 if (opt_CRLF()) {
1112 replyJobParamValue(*curJob, 213, t);
1113 return (true);
1114 } else if (timespec_param(4, ticks) &&
1115 setJobParameter(*curJob, t, ticks)) {
1116 reply(213, "%s set to %02d%02d."
1117 , parmToken(t)
1118 , ticks/60
1119 , ticks%60
1120 );
1121 return (true);
1122 }
1123 break;
1124 case T_MAXDIALS:
1125 case T_MAXTRIES:
1126 case T_VRES:
1127 case T_PAGEWIDTH:
1128 case T_PAGELENGTH:
1129 case T_MINBR:
1130 case T_BEGBR:
1131 case T_BEGST:
1132 case T_MAXPAGES:
1133 case T_SCHEDPRI:
1134 case T_NPAGES:
1135 case T_TOTPAGES:
1136 case T_NTRIES:
1137 case T_TOTTRIES:
1138 case T_NDIALS:
1139 case T_TOTDIALS:
1140 if (opt_CRLF()) {
1141 replyJobParamValue(*curJob, 213, t);
1142 return (true);
1143 } else if (number_param(n) &&
1144 setJobParameter(*curJob, t, (u_short) n)) {
1145 reply(213, "%s set to %u.", parmToken(t), n);
1146 return (true);
1147 }
1148 break;
1149 case T_NOTIFYADDR:
1150 case T_NOTIFY:
1151 case T_MODEM:
1152 case T_DIALSTRING:
1153 case T_EXTERNAL:
1154 case T_SUBADDR:
1155 case T_DATAFORMAT:
1156 case T_TO_USER:
1157 case T_TO_LOCATION:
1158 case T_TO_COMPANY:
1159 case T_TO_VOICE:
1160 case T_FROM_USER:
1161 case T_FROM_LOCATION:
1162 case T_FROM_COMPANY:
1163 case T_FROM_VOICE:
1164 case T_USRKEY:
1165 case T_PASSWD:
1166 case T_CLIENT:
1167 case T_PAGECHOP:
1168 case T_TAGLINE:
1169 case T_GROUPID:
1170 case T_JOBID:
1171 case T_JOBINFO:
1172 case T_OWNER:
1173 case T_STATE:
1174 case T_STATUS:
1175 case T_STATUSCODE:
1176 case T_ERRORCODE: // For compatibility
1177 case T_DONEOP:
1178 case T_COMMID:
1179 case T_REGARDING:
1180 case T_COMMENTS:
1181 case T_FAXNUMBER:
1182 case T_TSI:
1183 case T_PAGERANGE:
1184 if (opt_CRLF()) {
1185 replyJobParamValue(*curJob, 213, t);
1186 return (true);
1187 } else if (SPACE() && multi_STRING(s) && CRLF() &&
1188 setJobParameter(*curJob, t, s)) {
1189 reply(213, "%s set to \"%s\".", parmToken(t), (const char*) s);
1190 return (true);
1191 }
1192 break;
1193 case T_USE_ECM:
1194 case T_USE_TAGLINE:
1195 case T_USE_CONTCOVER:
1196 if (opt_CRLF()) {
1197 replyJobParamValue(*curJob, 213, t);
1198 return (true);
1199 } else if (boolean_param(b) &&
1200 setJobParameter(*curJob, t, b)) {
1201 reply(213, "%s set to %s.", parmToken(t), b ? "YES" : "NO");
1202 return (true);
1203 }
1204 break;
1205 case T_USE_XVRES:
1206 if (opt_CRLF()) {
1207 replyJobParamValue(*curJob, 213, t);
1208 return (true);
1209 } else if (boolean_param(b) &&
1210 setJobParameter(*curJob, t, b)) {
1211 reply(213, "%s set to %s.", parmToken(t), b ? "YES" : "NO");
1212 return (true);
1213 }
1214 break;
1215 case T_CHOPTHRESH:
1216 if (opt_CRLF()) {
1217 replyJobParamValue(*curJob, 213, t);
1218 return (true);
1219 } else if (string_param(s, "floating point number") &&
1220 setJobParameter(*curJob, t, (float) atof(s))) {
1221 reply(213, "%s set to %g.", parmToken(t), (float) atof(s));
1222 return (true);
1223 }
1224 break;
1225 case T_COVER: // specify/query cover page document
1226 if (opt_CRLF()) {
1227 replyJobParamValue(*curJob, 213, t);
1228 return (true);
1229 } else if (pathname_param(s)) {
1230 addCoverDocument(*curJob, s);
1231 return (true);
1232 }
1233 break;
1234 case T_DOCUMENT: // specify/query normal document
1235 if (opt_CRLF()) {
1236 replyJobParamValue(*curJob, 213, t);
1237 return (true);
1238 } else if (pathname_param(s)) {
1239 addDocument(*curJob, s);
1240 return (true);
1241 }
1242 break;
1243 case T_POLL: // specify/query poll operation
1244 if (opt_CRLF()) {
1245 replyJobParamValue(*curJob, 213, t);
1246 return (true);
1247 } else if (SPACE() && STRING(s, "polling selector")) {
1248 fxStr pwd;
1249 if (opt_CRLF()) {
1250 addPollOp(*curJob, s, ""); // sep but no pwd
1251 return (true);
1252 } else if (SPACE() && pwd_param(pwd) && CRLF()) {
1253 addPollOp(*curJob, s, pwd); // sep & pwd
1254 return (true);
1255 }
1256 }
1257 break;
1258 case T_HELP: // return help
1259 if (opt_CRLF()) {
1260 helpCmd(parmtab, (char*) NULL);
1261 return (true);
1262 } else if (string_param(s, "parameter name")) {
1263 s.raisecase();
1264 logcmd(T_JPARM, "HELP %s", (const char*) s);
1265 helpCmd(parmtab, s);
1266 return (true);
1267 }
1268 break;
1269 default:
1270 break;
1271 }
1272 return (false);
1273 }
1274
1275 /*
1276 * Collect a single string parameter.
1277 */
1278 bool
1279 HylaFAXServer::string_param(fxStr& s, const char* what)
1280 {
1281 return SPACE() && STRING(s, what) && CRLF();
1282 }
1283
1284 /*
1285 * Collect a single number parameter.
1286 */
1287 bool
1288 HylaFAXServer::number_param(long& n)
1289 {
1290 return SPACE() && NUMBER(n) && CRLF();
1291 }
1292
1293 /*
1294 * Collect a single boolean parameter.
1295 */
1296 bool
1297 HylaFAXServer::boolean_param(bool& b)
1298 {
1299 return SPACE() && BOOLEAN(b) && CRLF();
1300 }
1301
1302 /*
1303 * Collect a single time parameter.
1304 */
1305 bool
1306 HylaFAXServer::timespec_param(int ndigits, time_t& t)
1307 {
1308 return SPACE() && TIMESPEC(ndigits, t) && CRLF();
1309 }
1310
1311 /*
1312 * File manipulation command.
1313 */
1314 bool
1315 HylaFAXServer::pathname_param(fxStr& s)
1316 {
1317 return SPACE() && pathname(s) && CRLF();
1318 }
1319
1320 /*
1321 * Job control commands.
1322 */
1323 bool
1324 HylaFAXServer::job_param(fxStr& jid)
1325 {
1326 if (opt_CRLF()) {
1327 jid = curJob->jobid;
1328 return (true);
1329 } else if (SPACE() && STRING(jid, "job identifier") && CRLF()) {
1330 jid.lowercase();
1331 return (true);
1332 }
1333 return (false);
1334 }
1335
1336 /*
1337 * Job group control commands.
1338 */
1339 bool
1340 HylaFAXServer::jgrp_param(fxStr& jgid)
1341 {
1342 if (opt_CRLF()) {
1343 jgid = curJob->groupid;
1344 return (true);
1345 } else if (SPACE() && STRING(jgid, "job group identifier") && CRLF()) {
1346 jgid.lowercase();
1347 return (true);
1348 }
1349 return (false);
1350 }
1351
1352 bool
1353 HylaFAXServer::pwd_param(fxStr& s)
1354 {
1355 return STRING(s, "polling password");
1356 }
1357
1358 bool
1359 HylaFAXServer::pathname(fxStr& s)
1360 {
1361 return STRING(s, "pathname");
1362 }
1363
1364 bool
1365 HylaFAXServer::TIMESPEC(u_int len, time_t& result)
1366 {
1367 if (getToken(T_STRING, "time specification")) {
1368 if (tokenBody.length() == len) {
1369 if (checkNUMBER(tokenBody)) {
1370 const char* cp = tokenBody;
1371 if (len == 12) { // YYYYMMDDHHMM
1372 struct tm tm;
1373 tm.tm_sec = 0;
1374 tm.tm_min = twodigits(cp+10, 60);
1375 tm.tm_hour = twodigits(cp+8, 24);
1376 tm.tm_mday = twodigits(cp+6, 32);
1377 tm.tm_mon = twodigits(cp+4, 13) - 1;
1378 tm.tm_year = fourdigits(cp+0) - 1900;
1379 tm.tm_isdst= -1; // XXX not sure about this???
1380 /*
1381 * Client specifies time relative to GMT
1382 * and mktime returns locally adjusted
1383 * time so we need to adjust the result
1384 * here to get things in the right timezone.
1385 */
1386 result = mktime(&tm) - gmtoff;
1387 } else if (len == 6) { // DDHHMM
1388 result = 24*60*60*twodigits(cp, 100)
1389 + 60*60*twodigits(cp+2, 24)
1390 + 60*twodigits(cp+4, 60);
1391 } else { // MMSS
1392 result = 60*twodigits(cp, 60)
1393 + twodigits(cp+2, 60);
1394 }
1395 return (true);
1396 }
1397 } else
1398 syntaxError(fxStr::format(
1399 "bad time specification (expecting %d digits)", len));
1400 }
1401 return (false);
1402 }
1403
1404 bool
1405 HylaFAXServer::BOOLEAN(bool& b)
1406 {
1407 if (getToken(T_STRING, "boolean")) {
1408 tokenBody.raisecase();
1409 if (tokenBody == "YES") {
1410 b = true;
1411 return (true);
1412 } else if (tokenBody == "NO") {
1413 b = false;
1414 return (true);
1415 } else
1416 syntaxError("invalid boolean value, use YES or NO");
1417 }
1418 return (false);
1419 }
1420
1421 bool
1422 HylaFAXServer::checkToken(Token wanted)
1423 {
1424 Token t = nextToken();
1425 if (t != wanted) {
1426 pushToken(t);
1427 return (false);
1428 } else
1429 return (true);
1430 }
1431
1432 bool
1433 HylaFAXServer::getToken(Token wanted, const char* expected)
1434 {
1435 Token t = nextToken();
1436 if (t != wanted) {
1437 if (t == T_LEXERR)
1438 syntaxError("unmatched quote mark");
1439 else
1440 syntaxError(fxStr::format("expecting %s", expected));
1441 return (false);
1442 } else
1443 return (true);
1444 }
1445
1446 bool HylaFAXServer::SPACE() { return getToken(T_SP, "<SP>"); }
1447 bool HylaFAXServer::COMMA() { return getToken(T_COMMA, "\",\""); }
1448 bool HylaFAXServer::CRLF() { return getToken(T_CRLF, "<CRLF>"); }
1449 bool HylaFAXServer::opt_CRLF() { return checkToken(T_CRLF); }
1450
1451 bool
1452 HylaFAXServer::opt_STRING(fxStr& s)
1453 {
1454 if (checkToken(T_STRING)) {
1455 s = tokenBody;
1456 return (true);
1457 } else
1458 return (false);
1459 }
1460 bool
1461 HylaFAXServer::STRING(fxStr& s, const char* what)
1462 {
1463 if (getToken(T_STRING, what != NULL ? what : "<STRING>")) {
1464 s = tokenBody;
1465 return (true);
1466 } else
1467 return (false);
1468 }
1469 bool
1470 HylaFAXServer::multi_STRING(fxStr& s)
1471 {
1472 if (!STRING(s))
1473 return (false);
1474 for (;;) {
1475 switch (nextToken()) {
1476 case T_CRLF:
1477 pushToken(T_CRLF);
1478 return (true);
1479 case T_COMMA:
1480 s.append(',');
1481 break;
1482 case T_STRING:
1483 s.append(tokenBody);
1484 break;
1485 case T_SP:
1486 s.append(' ');
1487 break;
1488 case T_LEXERR:
1489 syntaxError("unmatched quote mark");
1490 return (false);
1491 default:
1492 break;
1493 }
1494 }
1495 }
1496
1497 bool
1498 HylaFAXServer::checkNUMBER(const char* cp)
1499 {
1500 if (cp[0] == '-' || cp[0] == '+')
1501 cp++;
1502 for (; *cp; cp++)
1503 if (!isdigit(*cp)) {
1504 syntaxError("invalid number");
1505 return (false);
1506 }
1507 return (true);
1508 }
1509
1510 bool
1511 HylaFAXServer::NUMBER(long& n)
1512 {
1513 if (!getToken(T_STRING, "decimal number")) {
1514 return (false);
1515 } else if (!checkNUMBER(tokenBody)) {
1516 return (false);
1517 } else {
1518 n = strtol(tokenBody, NULL, 0);
1519 return (true);
1520 }
1521 }
1522
1523 void
1524 HylaFAXServer::syntaxError(const char* msg)
1525 {
1526 const char* cp = strchr(cbuf, '\0');
1527 if (cp[-1] == '\n')
1528 cp--;
1529 reply(500, "'%.*s': Syntax error, %s.", cp-cbuf, cbuf, msg);
1530 }
1531
1532 /*
1533 * Lexical Scanner.
1534 */
1535
1536 /*
1537 * Return the next byte of data from the control channel.
1538 * If no data is available, either wait for new data to
1539 * be received or return EOF.
1540 */
1541 int
1542 HylaFAXServer::getChar(bool waitForInput)
1543 {
1544 if (recvCC <= 0) {
1545 // enable non-blocking i/o on control channel
1546 (void) fcntl(STDIN_FILENO, F_SETFL, ctrlFlags | O_NONBLOCK);
1547 again:
1548 do {
1549 recvCC = Sys::read(STDIN_FILENO, recvBuf, sizeof (recvBuf));
1550 } while (recvCC < 0 && errno == EINTR);
1551 if (recvCC < 0 && errno == EWOULDBLOCK && waitForInput) {
1552 Dispatcher& disp = Dispatcher::instance();
1553 disp.startTimer(idleTimeout, 0, this);
1554 for (state |= S_WAITDATA; IS(WAITDATA); disp.dispatch())
1555 ;
1556 disp.stopTimer(this);
1557 goto again;
1558 }
1559 (void) fcntl(STDIN_FILENO, F_SETFL, ctrlFlags);
1560 if (recvCC <= 0) {
1561 if (recvCC == 0) { // no more input coming, remove handler
1562 Dispatcher::instance().unlink(STDIN_FILENO);
1563 dologout(0); // XXX what about server interactions???
1564 }
1565 return (EOF);
1566 }
1567 recvNext = 0;
1568 }
1569 recvCC--;
1570 return recvBuf[recvNext++] & 0377;
1571 }
1572
1573 /*
1574 * Push back control channel data.
1575 */
1576 void
1577 HylaFAXServer::pushCmdData(const char* data, int n)
1578 {
1579 if ((unsigned) recvNext + n > sizeof (recvBuf)) {
1580 logError("No space to push back urgent data \"%.*s\"", n, data);
1581 return;
1582 }
1583 if (recvNext < n) { // not enough space, copy existing data
1584 memmove(&recvBuf[n], &recvBuf[recvNext], recvCC);
1585 recvNext = 0;
1586 } else // space available, just adjust recvNext
1587 recvNext -= n;
1588 memcpy(&recvBuf[recvNext], data, n);
1589 recvCC += n;
1590 }
1591
1592 /*
1593 * Return the next line of data received on the control
1594 * channel, ignoring any Telnet protocol command sequences.
1595 */
1596 bool
1597 HylaFAXServer::getCmdLine(char* s, int n, bool waitForInput)
1598 {
1599 char* cp = s;
1600 cpos = 0;
1601 while (n > 1) {
1602 int c = getChar(cp != s || waitForInput);
1603 if (c == EOF)
1604 return (false);
1605 if (c == IAC) { // telnet protocol command
1606 c = getChar(true);
1607 if (c == EOF)
1608 return (false);
1609 switch (c&0377) {
1610 case WILL:
1611 case WONT:
1612 c = getChar(true); // telnet option
1613 if (c == EOF)
1614 return (false);
1615 printf("%c%c%c", IAC, DONT, c&0377);
1616 (void) fflush(stdout);
1617 continue;
1618 case DO:
1619 case DONT:
1620 c = getChar(true); // telnet option
1621 if (c == EOF)
1622 return (false);
1623 printf("%c%c%c", IAC, WONT, c&0377);
1624 (void) fflush(stdout);
1625 continue;
1626 case IAC: // <IAC><IAC> -> <IAC>
1627 break;
1628 default: // ignore command
1629 continue;
1630 }
1631 }
1632 if (c == '\n') {
1633 // convert \r\n -> \n
1634 if (cp > s && cp[-1] == '\r')
1635 cp[-1] = '\n';
1636 else
1637 *cp++ = '\n', n--;
1638 break;
1639 }
1640 *cp++ = c, n--;
1641 }
1642 *cp = '\0';
1643 if (TRACE(PROTOCOL))
1644 logDebug("command: %s", s);
1645 return (true);
1646 }
1647
1648 void
1649 HylaFAXServer::timerExpired(long, long)
1650 {
1651 reply(421,
1652 "Timeout (%d seconds): closing control connection.", idleTimeout);
1653 if (TRACE(SERVER)) {
1654 time_t now = Sys::now();
1655 logInfo("User %s timed out after %d seconds at %.24s"
1656 , IS(LOGGEDIN) ? (const char*) the_user : "(no one)"
1657 , idleTimeout
1658 , asctime(cvtTime(now))
1659 );
1660 }
1661 dologout(1);
1662 }
1663
1664 Token
1665 HylaFAXServer::nextToken(void)
1666 {
1667 if (pushedToken != T_NIL) {
1668 Token t = pushedToken;
1669 pushedToken = T_NIL;
1670 return (t);
1671 }
1672 switch (cbuf[cpos]) {
1673 case ' ':
1674 while (cbuf[++cpos] == ' ') // compress multiple spaces
1675 ;
1676 return (T_SP);
1677 case '\n':
1678 cpos++;
1679 return (T_CRLF);
1680 case ',':
1681 cpos++;
1682 return (T_COMMA);
1683 case '"':
1684 /*
1685 * Parse quoted string and deal with \ escapes.
1686 */
1687 tokenBody.resize(0);
1688 for (;;) {
1689 int c = cbuf[++cpos];
1690 if (c == '"') {
1691 cpos++;
1692 return (T_STRING);
1693 }
1694 if (c == '\n') // unmatched quote mark
1695 return (T_LEXERR);
1696 if (c == '\\') { // \ escape handling
1697 c = cbuf[++cpos];
1698 if (isdigit(c)) { // \nnn octal escape
1699 int v = c - '0';
1700 if (isdigit(c = cbuf[cpos+1])) {
1701 cpos++, v = (v << 3) + (c - '0');
1702 if (isdigit(c = cbuf[cpos+1]))
1703 cpos++, v = (v << 3) + (c - '0');
1704 }
1705 c = v;
1706 }
1707 }
1708 tokenBody.append(c);
1709 }
1710 /*NOTREACHED*/
1711 }
1712 int base = cpos;
1713 do {
1714 cpos++;
1715 } while (cbuf[cpos] != ' ' && cbuf[cpos] != '\n' && cbuf[cpos] != ',');
1716 tokenBody = fxStr(&cbuf[base], cpos - base);
1717 return (T_STRING);
1718 }
1719
1720 void
1721 HylaFAXServer::helpCmd(const tab* ctab, const char* s)
1722 {
1723 const char* type;
1724 u_int NCMDS;
1725 if (ctab == sitetab) {
1726 type = "SITE ";
1727 NCMDS = N(sitetab);
1728 } else if (ctab == parmtab) {
1729 type = "JPARM ";
1730 NCMDS = N(parmtab);
1731 } else {
1732 type = "";
1733 NCMDS = N(cmdtab);
1734 }
1735 int width = 0;
1736 const tab* c = ctab;
1737 for (u_int n = NCMDS; n != 0; c++, n--) {
1738 int len = strlen(c->name);
1739 if (len > width)
1740 width = len;
1741 }
1742 width = (width + 8) &~ 7;
1743 if (s == NULL) {
1744 lreply(214, "The following %scommands are recognized %s.",
1745 type, "(* =>'s unimplemented)");
1746 int columns = 76 / width;
1747 if (columns == 0)
1748 columns = 1;
1749 int lines = (NCMDS + columns - 1) / columns;
1750 for (int i = 0; i < lines; i++) {
1751 printf(" ");
1752 for (int j = 0; j < columns; j++) {
1753 c = &ctab[j*lines + i];
1754 printf("%s%c", c->name, !c->implemented ? '*' : ' ');
1755 if (c + lines >= &ctab[NCMDS])
1756 break;
1757 int w = strlen(c->name) + 1;
1758 while (w < width) {
1759 putchar(' ');
1760 w++;
1761 }
1762 }
1763 printf("\r\n");
1764 }
1765 (void) fflush(stdout);
1766 reply(214, "Direct comments to %s.", (const char*) faxContact);
1767 return;
1768 }
1769 c = lookup(ctab, NCMDS, s);
1770 if (c == NULL) {
1771 reply(502, "Unknown command %s.", s);
1772 return;
1773 }
1774 if (c->implemented)
1775 reply(214, "Syntax: %s%s %s", type, c->name, c->help);
1776 else
1777 reply(214, "%s%-*s\t%s; unimplemented.", type, width, c->name, c->help);
1778 }
1779
1780 void
1781 HylaFAXServer::logcmd(Token t, const char* fmt ...)
1782 {
1783 if (TRACE(PROTOCOL)) {
1784 const char* name = cmdToken(t);
1785 if (!name)
1786 name = siteToken(t);
1787 if (!name)
1788 name = "???";
1789 if (fmt != NULL) {
1790 va_list ap;
1791 va_start(ap, fmt);
1792 vlogInfo(fxStr::format("%s %s", name, fmt), ap);
1793 va_end(ap);
1794 } else
1795 logInfo("%s", name);
1796 }
1797 }
1798
1799 void
1800 HylaFAXServer::cmdFailure(Token t, const char* why)
1801 {
1802 if (TRACE(SERVER)) {
1803 const char* cp = cmdToken(t);
1804 if (!cp)
1805 cp = siteToken(t);
1806 if (cp)
1807 logInfo("%s cmd failure - %s", cp, why);
1808 else
1809 logInfo("#%u cmd failure - %s", t, why);
1810 }
1811 }
1812
1813 bool
1814 HylaFAXServer::checklogin(Token t)
1815 {
1816 if (!IS(LOGGEDIN)) {
1817 cmdFailure(t, "not logged in");
1818 reply(530, "Please login with USER and PASS.");
1819 return (false);
1820 } else
1821 return (true);
1822 }
1823
1824 bool
1825 HylaFAXServer::checkadmin(Token t)
1826 {
1827 if (checklogin(t)) {
1828 if (IS(PRIVILEGED))
1829 return (true);
1830 cmdFailure(t, "user is not privileged");
1831 reply(530, "Please use ADMIN to establish administrative privileges.");
1832 }
1833 return (false);
1834 }