| 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 |
#ifdef SNPP_SUPPORT |
| 28 |
/* |
| 29 |
* Simple Network Paging Protocol (SNPP) Support. |
| 30 |
*/ |
| 31 |
#include "config.h" |
| 32 |
#include "Sys.h" |
| 33 |
#include "Socket.h" |
| 34 |
#include "SNPPServer.h" |
| 35 |
#include "Dispatcher.h" |
| 36 |
#include "RE.h" |
| 37 |
|
| 38 |
#include <ctype.h> |
| 39 |
#if HAS_CRYPT_H |
| 40 |
#include <crypt.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
extern "C" { |
| 44 |
#include <netdb.h> |
| 45 |
#include <arpa/inet.h> |
| 46 |
|
| 47 |
#include <netinet/in_systm.h> |
| 48 |
#include <netinet/ip.h> |
| 49 |
} |
| 50 |
|
| 51 |
SNPPSuperServer::SNPPSuperServer(const char* p, int bl) |
| 52 |
: SuperServer("SNPP", bl) |
| 53 |
, port(p) |
| 54 |
{} |
| 55 |
SNPPSuperServer::~SNPPSuperServer() {} |
| 56 |
|
| 57 |
bool |
| 58 |
SNPPSuperServer::startServer(void) |
| 59 |
{ |
| 60 |
/* |
| 61 |
* Switch to super-user to do the bind in case |
| 62 |
* we are to use a port in the privileged region |
| 63 |
* on a BSD system (ports <=1024 are reserved |
| 64 |
* and the default SNPP port is 444). |
| 65 |
* |
| 66 |
* NB: We do it for both the socket+bind calls |
| 67 |
* to workaround a bug in Solaris 2.5. |
| 68 |
*/ |
| 69 |
uid_t ouid = geteuid(); |
| 70 |
(void) seteuid(0); |
| 71 |
int s = socket(AF_INET, SOCK_STREAM, 0); |
| 72 |
if (s >= 0) { |
| 73 |
struct sockaddr_in sin; |
| 74 |
memset(&sin, 0, sizeof (sin)); |
| 75 |
sin.sin_family = AF_INET; |
| 76 |
const char* cp = port; |
| 77 |
struct servent* sp = getservbyname(cp, SNPP_PROTONAME); |
| 78 |
if (!sp) { |
| 79 |
if (isdigit(cp[0])) |
| 80 |
sin.sin_port = htons(atoi(cp)); |
| 81 |
else |
| 82 |
sin.sin_port = htons(SNPP_DEFPORT); |
| 83 |
} else |
| 84 |
sin.sin_port = sp->s_port; |
| 85 |
if (Socket::bind(s, &sin, sizeof (sin)) >= 0) { |
| 86 |
(void) listen(s, getBacklog()); |
| 87 |
(void) seteuid(ouid); |
| 88 |
Dispatcher::instance().link(s, Dispatcher::ReadMask, this); |
| 89 |
return (true); // success |
| 90 |
} |
| 91 |
Sys::close(s); |
| 92 |
logError("HylaFAX %s: bind (port %u): %m", |
| 93 |
getKind(), ntohs(sin.sin_port)); |
| 94 |
} else |
| 95 |
logError("HylaFAX %s: socket: %m", getKind()); |
| 96 |
(void) seteuid(ouid); |
| 97 |
return (false); |
| 98 |
} |
| 99 |
HylaFAXServer* SNPPSuperServer::newChild(void) { return new SNPPServer; } |
| 100 |
|
| 101 |
SNPPServer::SNPPServer() {} |
| 102 |
SNPPServer::~SNPPServer() {} |
| 103 |
|
| 104 |
void |
| 105 |
SNPPServer::open(void) |
| 106 |
{ |
| 107 |
setupNetwork(STDIN_FILENO); |
| 108 |
initServer(); // complete state initialization |
| 109 |
fxStr emsg; |
| 110 |
if (!initClientFIFO(emsg)) { |
| 111 |
logInfo("connection refused (%s) from %s [%s]", |
| 112 |
(const char*) emsg, |
| 113 |
(const char*) remotehost, (const char*) remoteaddr); |
| 114 |
reply(420, "%s server cannot initialize: %s", |
| 115 |
(const char*) hostname, (const char*) emsg); |
| 116 |
dologout(-1); |
| 117 |
} |
| 118 |
ctrlFlags = fcntl(STDIN_FILENO, F_GETFL); // for parser |
| 119 |
if (isShutdown(true)) { |
| 120 |
reply(421, "%s SNPP server unavailable; try again later.", |
| 121 |
(const char*) hostname); |
| 122 |
dologout(-1); |
| 123 |
} |
| 124 |
reply(220, "%s SNPP server (%s) ready.", (const char*) hostname, version); |
| 125 |
if (TRACE(CONNECT)) |
| 126 |
logInfo("SNPP connection from %s [%s]", |
| 127 |
(const char*) remotehost, (const char*) remoteaddr); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
SNPPServer::initServer(void) |
| 132 |
{ |
| 133 |
InetFaxServer::initServer(); |
| 134 |
state &= ~S_USEGMT; // SNPP uses local times |
| 135 |
resetState(); |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
SNPPServer::initDefaultJob(void) |
| 140 |
{ |
| 141 |
InetFaxServer::initDefaultJob(); |
| 142 |
|
| 143 |
defJob.jobtype = "pager"; |
| 144 |
initSNPPJob(); |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
SNPPServer::initSNPPJob(void) |
| 149 |
{ |
| 150 |
defJob.queued = false; // jobs not queued--per protocol |
| 151 |
defJob.maxdials = SNPP_DEFREDIALS; // configuration defaults... |
| 152 |
defJob.maxtries = SNPP_DEFRETRIES; |
| 153 |
defJob.killtime = 60*killMap[SNPP_DEFLEVEL]; |
| 154 |
defJob.retrytime = retryMap[SNPP_DEFLEVEL]; |
| 155 |
defJob.usrpri = priMap[SNPP_DEFLEVEL]; |
| 156 |
// XXX default notification |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
SNPPServer::resetState(void) |
| 161 |
{ |
| 162 |
initDefaultJob(); // default job state |
| 163 |
msgFile = ""; |
| 164 |
haveText = false; // no message text yet |
| 165 |
msgs.resize(0); // purge any message refs |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
SNPPServer::dologout(int status) |
| 170 |
{ |
| 171 |
/* |
| 172 |
* Purge any partially constructed jobs. If we were |
| 173 |
* doing a SEND when the connection was dropped then |
| 174 |
* we do not purge jobs but instead just let them run |
| 175 |
* detached. This may not be the best thing; we might |
| 176 |
* instead want to kill the jobs--we'll need to think |
| 177 |
* about this some more before changing this behaviour. |
| 178 |
*/ |
| 179 |
if (msgs.length() > 0 && !IS(SENDWAIT)) { |
| 180 |
fxStr emsg; |
| 181 |
for (u_int i = 0, n = msgs.length(); i < n; i++) { |
| 182 |
Job* job = findJob(msgs[i], emsg); |
| 183 |
if (job) { |
| 184 |
for (u_int j = 0, m = job->items.length(); j < m; j++) |
| 185 |
if (job->items[j].op == FaxRequest::send_data) |
| 186 |
(void) Sys::unlink(job->items[j].item); |
| 187 |
(void) Sys::unlink(job->qfile); |
| 188 |
delete job; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
InetFaxServer::dologout(status); |
| 193 |
} |
| 194 |
|
| 195 |
static inline bool |
| 196 |
isMagic(char c) |
| 197 |
{ |
| 198 |
return (c == '[' || c == ']' || c == '*' || c == '.' || c == '^' || |
| 199 |
c == '$' || c == '-' || c == '+' || c == '{' || c == '}' || |
| 200 |
c == '(' || c == ')'); |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* Handle \escapes for a pager ID replacement string. |
| 205 |
*/ |
| 206 |
static void |
| 207 |
subRHS(fxStr& result, const RE& re, const fxStr& match) |
| 208 |
{ |
| 209 |
/* |
| 210 |
* Do ``&'' and ``\n'' interpolations in the replacement. |
| 211 |
*/ |
| 212 |
for (u_int i = 0, n = result.length(); i < n; i++) { |
| 213 |
if (result[i] == '\\') { // process \<char> escapes |
| 214 |
result.remove(i), n--; |
| 215 |
if (isdigit(result[i])) { |
| 216 |
int mn = result[i] - '0'; |
| 217 |
int ms = re.StartOfMatch(mn); |
| 218 |
int mlen = re.EndOfMatch(mn) - ms; |
| 219 |
result.remove(i); // delete \n |
| 220 |
if (ms >= 0) |
| 221 |
result.insert(match.extract(ms, mlen), i); |
| 222 |
else |
| 223 |
logError("Invalid backreference in pagermap: \\%d", mn); |
| 224 |
n = result.length(); // adjust string length ... |
| 225 |
i += mlen - 1; // ... and scan index |
| 226 |
} |
| 227 |
} else if (result[i] == '&') { // process & replacement |
| 228 |
int ms = re.StartOfMatch(0); |
| 229 |
int mlen = re.EndOfMatch(0) - ms; |
| 230 |
result.remove(i); // delete & |
| 231 |
result.insert(match.extract(ms, mlen), i); |
| 232 |
n = result.length(); // adjust string length ... |
| 233 |
i += mlen - 1; // ... and scan index |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/* |
| 239 |
* Given a client-supplied pager ID return the phone number |
| 240 |
* to dial of the service provider and the PIN to supply |
| 241 |
* to the provider when talking IXO/TAP (for aliases). |
| 242 |
*/ |
| 243 |
bool |
| 244 |
SNPPServer::mapPagerID(const char* pagerID, fxStr& number, fxStr& pin, fxStr& emsg) |
| 245 |
{ |
| 246 |
if (pagerIDMapFile != "") { |
| 247 |
FILE* fd = fopen(fixPathname(pagerIDMapFile), "r"); |
| 248 |
if (fd != NULL) { |
| 249 |
char buf[1024]; |
| 250 |
while (fgets(buf, sizeof (buf), fd)) { |
| 251 |
char* cp; |
| 252 |
for (cp = buf; isspace(*cp); cp++) // leading whitespace |
| 253 |
; |
| 254 |
if (*cp == '#' || *cp == '\0') |
| 255 |
continue; |
| 256 |
/* |
| 257 |
* Syntax is: |
| 258 |
* |
| 259 |
* <pattern> <dialstring>[/<PIN>] |
| 260 |
* |
| 261 |
* where <pattern> can be a simple string of alpha |
| 262 |
* numerics or a regular expression. The first line |
| 263 |
* that matches the client-specified pager ID is used. |
| 264 |
* If no <PIN> is specified then the client-specified |
| 265 |
* string is sent to the provider. If <dialstring> |
| 266 |
* is "reject" (verbatim) then matches are rejected. |
| 267 |
* <PIN> is treated as the RHS of an RE-style substitution: |
| 268 |
* \n and & escapes are replaced according to the RE |
| 269 |
* matching work. |
| 270 |
* |
| 271 |
* Leading white space on a line is ignored. Lines |
| 272 |
* that begin with '#' are ignored (i.e. comments). |
| 273 |
*/ |
| 274 |
const char* pattern = cp; |
| 275 |
bool isRE = false; |
| 276 |
for (; *cp != '\0' && !isspace(*cp); cp++) |
| 277 |
if (isMagic(*cp)) |
| 278 |
isRE = true; |
| 279 |
if (*cp != '\0') // \0-term. <pattern> |
| 280 |
*cp++ = '\0'; |
| 281 |
bool match; |
| 282 |
RE* re; |
| 283 |
if (isRE) { |
| 284 |
re = new RE(pattern); |
| 285 |
match = re->Find(pagerID, strlen(pagerID)); |
| 286 |
} else { |
| 287 |
match = streq(pattern, pagerID); |
| 288 |
re = NULL; |
| 289 |
} |
| 290 |
if (match) { |
| 291 |
while (isspace(*cp)) // leading whitespace |
| 292 |
cp++; |
| 293 |
if (*cp != '\n' && *cp != '\0') { // got <dialstring> |
| 294 |
char* np = strchr(cp, '\n'); // remove trailing \n |
| 295 |
if (np) |
| 296 |
*np = '\0'; |
| 297 |
np = cp; |
| 298 |
for (; *cp && *cp != '/'; cp++) |
| 299 |
; |
| 300 |
if (*cp == '/') { // <dialstring>/<PIN> |
| 301 |
*cp++ = '\0'; |
| 302 |
number = np; |
| 303 |
pin = cp; |
| 304 |
if (re) { // do substitutions |
| 305 |
subRHS(pin, *re, pagerID); |
| 306 |
subRHS(number, *re, pagerID); |
| 307 |
} |
| 308 |
} else { // <dialstring> |
| 309 |
number = np; |
| 310 |
pin = pagerID; |
| 311 |
} |
| 312 |
delete re; |
| 313 |
(void) fclose(fd); |
| 314 |
fxStr s(number); |
| 315 |
s.raisecase(); |
| 316 |
if (s == "REJECT") { |
| 317 |
emsg = fxStr::format("Invalid pager ID %s",pagerID); |
| 318 |
return (false); |
| 319 |
} else |
| 320 |
return (true); |
| 321 |
} |
| 322 |
} |
| 323 |
delete re; |
| 324 |
} |
| 325 |
(void) fclose(fd); |
| 326 |
emsg = fxStr::format("Unknown or illegal pager ID %s", pagerID); |
| 327 |
} else { |
| 328 |
emsg = fxStr::format("Cannot open pager ID mapping file %s (%s)", |
| 329 |
(const char*) pagerIDMapFile, strerror(errno)); |
| 330 |
logError("%s", (const char*) emsg); |
| 331 |
} |
| 332 |
} else { |
| 333 |
emsg = "No pager ID mapping file found"; |
| 334 |
logError("%s", (const char*) emsg); |
| 335 |
} |
| 336 |
return (false); |
| 337 |
} |
| 338 |
|
| 339 |
/* |
| 340 |
* SNPP Parser. |
| 341 |
*/ |
| 342 |
|
| 343 |
#define N(a) (sizeof (a) / sizeof (a[0])) |
| 344 |
|
| 345 |
/* |
| 346 |
* Standard protocol commands. |
| 347 |
*/ |
| 348 |
static const tab cmdtab[] = { |
| 349 |
{ "2WAY", T_2WAY, true,false, "(preface 2-way transaction)" }, |
| 350 |
{ "ABOR", T_ABOR, false, true, "(abort operation)" }, |
| 351 |
{ "ACKR", T_ACKREAD, true,false, "0|1" }, |
| 352 |
{ "ALER", T_ALERT, true,false, "alert-level" }, |
| 353 |
{ "CALL", T_CALLERID, true,false, "caller-ID" }, |
| 354 |
{ "COVE", T_COVERAGE, true,false, "alternate-area" }, |
| 355 |
{ "DATA", T_DATA, true, true, "(specify multi-line message)" }, |
| 356 |
{ "EXPT", T_EXPTAG, true,false, "hours" }, |
| 357 |
{ "HELP", T_HELP, false, true, "[<string>]" }, |
| 358 |
{ "HOLD", T_HOLDUNTIL, true, true, "YYYYMMDDHHMMSS [+/-GMT-diff]" }, |
| 359 |
{ "KTAG", T_KTAG, true,false, "message-tag pass-code" }, |
| 360 |
{ "LEVE", T_LEVEL, true, true, "service-level" }, |
| 361 |
{ "LOGI", T_LOGIN, false, true, "username [password]" }, |
| 362 |
{ "MCRE", T_MCRESPONSE, true,false, "2-byte-code response-text" }, |
| 363 |
{ "MESS", T_MESSAGE, true, true, "message" }, |
| 364 |
{ "MSTA", T_MSTATUS, true,false, "message-tag pass-code" }, |
| 365 |
{ "NOQU", T_NOQUEUEING, true,false, "(disable message queueing)" }, |
| 366 |
{ "PAGE", T_PAGER, true, true, "pager-ID|alias [PIN]" }, |
| 367 |
{ "PING", T_PING, true, true, "pager-ID|alias" }, |
| 368 |
{ "QUIT", T_QUIT, false, true, "(terminate service)" }, |
| 369 |
{ "RESE", T_REST, true, true, "(reset state)" }, |
| 370 |
{ "RTYP", T_RTYPE, true,false, "reply-type-code" }, |
| 371 |
{ "SEND", T_SEND, true, true, "(send message)" }, |
| 372 |
{ "SITE", T_SITE, true, true, "site-cmd [arguments]" }, |
| 373 |
{ "STAT", T_STAT, false, true, "(return server status)" }, |
| 374 |
{ "SUBJ", T_SUBJECT, true, true, "message-subject" }, |
| 375 |
}; |
| 376 |
|
| 377 |
/* |
| 378 |
* Site-specific commands. |
| 379 |
*/ |
| 380 |
static const tab sitetab[] = { |
| 381 |
{ "FROMUSER", T_FROM_USER, true, true, "[<string>]" }, |
| 382 |
{ "HELP", T_HELP, false, true, "[<string>]" }, |
| 383 |
{ "IDLE", T_IDLE, true, true, "[max-idle-timeout]" }, |
| 384 |
{ "JPARM", T_JPARM, true, true, "(print job parameter status)" }, |
| 385 |
{ "JQUEUE", T_JQUEUE, true, true, "[on|off]" }, |
| 386 |
{ "LASTTIME", T_LASTTIME, false, true, "[DDHHSS]" }, |
| 387 |
{ "MAXDIALS", T_MAXDIALS, true, true, "[<number>]" }, |
| 388 |
{ "MAXTRIES", T_MAXTRIES, true, true, "[<number>]" }, |
| 389 |
{ "MODEM", T_MODEM, true, true, "[device|class]" }, |
| 390 |
{ "NOTIFY", T_NOTIFY, true, true, "[NONE|DONE|REQUEUE|DONE+REQUEUE]"}, |
| 391 |
{ "MAILADDR", T_NOTIFYADDR, true, true, "[email-address]" }, |
| 392 |
{ "RETRYTIME", T_RETRYTIME, true, true, "[HHSS]" }, |
| 393 |
{ "SCHEDPRI", T_SCHEDPRI, true, true, "[<number>]" }, |
| 394 |
}; |
| 395 |
|
| 396 |
static const tab* |
| 397 |
lookup(const tab* p, u_int n, const char* cmd) |
| 398 |
{ |
| 399 |
while (n != 0) { |
| 400 |
if (strneq(cmd, p->name, 4)) // NB: match on first 4 chars |
| 401 |
return (p); |
| 402 |
p++, n--; |
| 403 |
} |
| 404 |
return (NULL); |
| 405 |
} |
| 406 |
static const char* |
| 407 |
tokenName(const tab* p, u_int n, Token t) |
| 408 |
{ |
| 409 |
while (n != 0) { |
| 410 |
if (p->token == t) |
| 411 |
return (p->name); |
| 412 |
p++, n--; |
| 413 |
} |
| 414 |
return ("???"); |
| 415 |
} |
| 416 |
const char* |
| 417 |
SNPPServer::cmdToken(Token t) |
| 418 |
{ |
| 419 |
return tokenName(cmdtab, N(cmdtab), t); |
| 420 |
} |
| 421 |
const char* |
| 422 |
SNPPServer::siteToken(Token t) |
| 423 |
{ |
| 424 |
return tokenName(sitetab, N(sitetab), t); |
| 425 |
} |
| 426 |
|
| 427 |
bool |
| 428 |
SNPPServer::checklogin(Token t) |
| 429 |
{ |
| 430 |
if (!IS(LOGGEDIN)) { |
| 431 |
cmdFailure(t, "not logged in"); |
| 432 |
reply(530, "Please login with LOGIN."); |
| 433 |
return (false); |
| 434 |
} else |
| 435 |
return (true); |
| 436 |
} |
| 437 |
|
| 438 |
/* |
| 439 |
* Parse and process command input received on the |
| 440 |
* control channel. This method is invoked whenever |
| 441 |
* data is present on the control channel. We read |
| 442 |
* everything and parse (and execute) as much as possible |
| 443 |
* but do not block waiting for more data except when |
| 444 |
* a partial line of input is received. This is done |
| 445 |
* to ensure other processing will be handled in a |
| 446 |
* timely fashion (e.g. processing of messages from |
| 447 |
* the scheduler received via the FIFO). |
| 448 |
*/ |
| 449 |
int |
| 450 |
SNPPServer::parse() |
| 451 |
{ |
| 452 |
if (IS(WAITDATA)) { // recursive invocation |
| 453 |
state &= ~S_WAITDATA; |
| 454 |
return (0); |
| 455 |
} |
| 456 |
if (IS(SENDWAIT)) { // waiting in SEND, discard data |
| 457 |
while (getCmdLine(cbuf, sizeof (cbuf))) |
| 458 |
; |
| 459 |
return (0); |
| 460 |
} |
| 461 |
// stop control channel idle timeout |
| 462 |
Dispatcher::instance().stopTimer(this); |
| 463 |
pushToken(T_NIL); // reset state |
| 464 |
for (;;) { |
| 465 |
/* |
| 466 |
* Fetch the next complete line of input received on the |
| 467 |
* control channel. This call will fail when no more data |
| 468 |
* is *currently* waiting on the control channel. Note |
| 469 |
* that this does not mean the connection has dropped; just |
| 470 |
* that data is not available at this instant. Note also |
| 471 |
* that if a partial line of input is received a complete |
| 472 |
* line will be waited for (see below). |
| 473 |
*/ |
| 474 |
if (!getCmdLine(cbuf, sizeof (cbuf))) |
| 475 |
break; |
| 476 |
/* |
| 477 |
* Parse the line of input read above. |
| 478 |
*/ |
| 479 |
if (getToken(T_STRING, "command token")) { |
| 480 |
tokenBody.raisecase(); |
| 481 |
const tab* p = lookup(cmdtab, N(cmdtab), tokenBody); |
| 482 |
if (p == NULL) |
| 483 |
reply(500, "%s: Command not recognized.", |
| 484 |
(const char*) tokenBody); |
| 485 |
else if (!p->implemented) |
| 486 |
reply(500, "%s: Command not implemented.", p->name); |
| 487 |
else if (isShutdown(!IS(LOGGEDIN))) { |
| 488 |
reply(421, "Server shutting down. Goodbye."); |
| 489 |
dologout(0); // XXX |
| 490 |
/* |
| 491 |
* If command requires client to be logged in check |
| 492 |
* for this. Note that some commands have variants |
| 493 |
* that do not require the client be logged in--these |
| 494 |
* cannot check here and must do it specially below. |
| 495 |
*/ |
| 496 |
} else if (p->checklogin && !checklogin(p->token)) |
| 497 |
; |
| 498 |
/* |
| 499 |
* Command is valid, implemented, and the server |
| 500 |
* is available to service it. If the syntax is |
| 501 |
* correct then reset the number of consecutive |
| 502 |
* bad commands. Note that since part of the syntax |
| 503 |
* checking is login validation this logic will also |
| 504 |
* catch people typing syntacitcally valid but |
| 505 |
* unacceptable commands just to remain connected. |
| 506 |
*/ |
| 507 |
else if (cmd(p->token)) { |
| 508 |
consecutiveBadCmds = 0; |
| 509 |
continue; |
| 510 |
} |
| 511 |
} |
| 512 |
/* |
| 513 |
* If too many consecutive bad commands have been |
| 514 |
* received disconnect. This is to safeguard against |
| 515 |
* a client that spews trash down the control connection. |
| 516 |
*/ |
| 517 |
if (++consecutiveBadCmds >= maxConsecutiveBadCmds) { |
| 518 |
/* |
| 519 |
* Check for shutdown so that any shutdown message |
| 520 |
* will get prepended to client reply. |
| 521 |
*/ |
| 522 |
(void) isShutdown(!IS(LOGGEDIN)); |
| 523 |
reply(421, "Too many errors, server shutting down. Goodbye."); |
| 524 |
dologout(0); |
| 525 |
} |
| 526 |
} |
| 527 |
Dispatcher::instance().startTimer(idleTimeout, 0, this); |
| 528 |
return (0); |
| 529 |
} |
| 530 |
|
| 531 |
/* |
| 532 |
* Protocol command (one line). |
| 533 |
*/ |
| 534 |
bool |
| 535 |
SNPPServer::cmd(Token t) |
| 536 |
{ |
| 537 |
fxStr s; |
| 538 |
long n; |
| 539 |
time_t tv; |
| 540 |
|
| 541 |
switch (t) { |
| 542 |
case T_ABOR: // abort active command (e.g. SEND) |
| 543 |
if (CRLF()) { |
| 544 |
logcmd(t); |
| 545 |
ack(250, cmdToken(t)); |
| 546 |
return (true); |
| 547 |
} |
| 548 |
break; |
| 549 |
case T_DATA: // submit multi-line message data |
| 550 |
if (CRLF()) { |
| 551 |
logcmd(t); |
| 552 |
dataCmd(); |
| 553 |
return (true); |
| 554 |
} |
| 555 |
break; |
| 556 |
case T_HELP: // return help |
| 557 |
if (opt_CRLF()) { |
| 558 |
logcmd(t); |
| 559 |
helpCmd(cmdtab, (char*) NULL); |
| 560 |
return (true); |
| 561 |
} else if (string_param(s, "command name")) { |
| 562 |
logcmd(t, "%s", (const char*) s); |
| 563 |
s.raisecase(); |
| 564 |
if (s == "SITE") |
| 565 |
helpCmd(sitetab, NULL); |
| 566 |
else |
| 567 |
helpCmd(cmdtab, s); |
| 568 |
return (true); |
| 569 |
} |
| 570 |
break; |
| 571 |
case T_HOLDUNTIL: // set time to send |
| 572 |
if (SPACE() && SNPPTime(tv)) { |
| 573 |
if (opt_CRLF()) { |
| 574 |
holdCmd(tv); |
| 575 |
return (true); |
| 576 |
} else if (string_param(s, "GMT-difference")) { |
| 577 |
// conver GMT offset to numeric value |
| 578 |
int sign = 1; |
| 579 |
const char* cp = s; |
| 580 |
if (*cp == '-') |
| 581 |
cp++, sign = -1; |
| 582 |
else if (*cp == '+') |
| 583 |
cp++; |
| 584 |
time_t off = (time_t) (strtoul(cp, 0, 10)*60 / 100); |
| 585 |
holdCmd(tv + sign*off); |
| 586 |
return (true); |
| 587 |
} |
| 588 |
} |
| 589 |
break; |
| 590 |
case T_LEVEL: // set level of operation |
| 591 |
if (number_param(n)) { |
| 592 |
logcmd(t, "%lu", n); |
| 593 |
serviceLevel((u_int) n); |
| 594 |
return (true); |
| 595 |
} |
| 596 |
break; |
| 597 |
case T_LOGIN: // login as user |
| 598 |
if (SPACE() && STRING(s, "login-ID")) { |
| 599 |
fxStr pwd; |
| 600 |
if (opt_CRLF()) { |
| 601 |
logcmd(t, (const char*) s); |
| 602 |
loginCmd(s); |
| 603 |
return (true); |
| 604 |
} else if (string_param(pwd, "password")) { |
| 605 |
logcmd(t, "%s <passwd>", (const char*) s); |
| 606 |
loginCmd(s, pwd); |
| 607 |
return (true); |
| 608 |
} |
| 609 |
} |
| 610 |
break; |
| 611 |
case T_MESSAGE: // specify 1-line message data |
| 612 |
if (SPACE() && multi_STRING(s) && CRLF()) { |
| 613 |
logcmd(t, "%s", (const char*) s); |
| 614 |
messageCmd(s); |
| 615 |
return (true); |
| 616 |
} |
| 617 |
break; |
| 618 |
case T_PAGER: // specify destination pager ID |
| 619 |
if (SPACE() && STRING(s, "pager-ID")) { |
| 620 |
fxStr pwd; |
| 621 |
if (opt_CRLF()) { |
| 622 |
logcmd(t, "%s", (const char*) s); |
| 623 |
pagerCmd(s); |
| 624 |
return (true); |
| 625 |
} else if (string_param(pwd, "password/PIN")) { |
| 626 |
logcmd(t, "%s <passwd/PIN>", (const char*) s); |
| 627 |
pagerCmd(s, pwd); |
| 628 |
return (true); |
| 629 |
} |
| 630 |
} |
| 631 |
break; |
| 632 |
case T_PING: // localize/verify pager ID |
| 633 |
if (string_param(s, "pager-ID")) { |
| 634 |
logcmd(t, "%s", (const char*) s); |
| 635 |
pingCmd(s); |
| 636 |
return (true); |
| 637 |
} |
| 638 |
break; |
| 639 |
case T_QUIT: // terminate session |
| 640 |
if (CRLF()) { |
| 641 |
logcmd(t); |
| 642 |
reply(221, "Goodbye."); |
| 643 |
dologout(0); |
| 644 |
return (true); |
| 645 |
} |
| 646 |
break; |
| 647 |
case T_REST: // reset server state |
| 648 |
logcmd(t); |
| 649 |
initServer(); |
| 650 |
reply(220, "%s server (%s) ready.", (const char*) hostname, version); |
| 651 |
break; |
| 652 |
case T_SEND: // initiate send operation |
| 653 |
if (CRLF()) { |
| 654 |
logcmd(t); |
| 655 |
sendCmd(); |
| 656 |
return (true); |
| 657 |
} |
| 658 |
break; |
| 659 |
case T_SITE: // site-specific command |
| 660 |
if (SPACE() && getToken(T_STRING, "site command")) { |
| 661 |
tokenBody.raisecase(); |
| 662 |
const tab* p = lookup(sitetab, N(sitetab), tokenBody); |
| 663 |
if (p == NULL) { |
| 664 |
reply(500, "SITE %s: Command not recognized.", |
| 665 |
(const char*) tokenBody); |
| 666 |
} else if (!p->implemented) |
| 667 |
reply(500, "SITE %s: Command not implemented.", p->name); |
| 668 |
else |
| 669 |
return (site_cmd(p->token)); |
| 670 |
} |
| 671 |
break; |
| 672 |
case T_STAT: // server status |
| 673 |
if (CRLF()) { |
| 674 |
logcmd(t); |
| 675 |
statusCmd(); |
| 676 |
return (true); |
| 677 |
} |
| 678 |
break; |
| 679 |
case T_SUBJECT: // message subject |
| 680 |
if (SPACE() && multi_STRING(s) && CRLF()) { |
| 681 |
logcmd(t, "%s", (const char*) s); |
| 682 |
subjectCmd(s); |
| 683 |
return (true); |
| 684 |
} |
| 685 |
break; |
| 686 |
default: |
| 687 |
break; |
| 688 |
} |
| 689 |
return (false); |
| 690 |
} |
| 691 |
|
| 692 |
/* |
| 693 |
* Site-specific protocol commands (one line). |
| 694 |
*/ |
| 695 |
bool |
| 696 |
SNPPServer::site_cmd(Token t) |
| 697 |
{ |
| 698 |
fxStr s; |
| 699 |
long n; |
| 700 |
time_t tv; |
| 701 |
bool b; |
| 702 |
|
| 703 |
switch (t) { |
| 704 |
case T_IDLE: // set/query idle timeout |
| 705 |
if (opt_CRLF()) { |
| 706 |
logcmd(t); |
| 707 |
reply(250, "%u seconds.", idleTimeout); |
| 708 |
return (true); |
| 709 |
} else if (number_param(n)) { |
| 710 |
logcmd(t, "%lu", n); |
| 711 |
if ((unsigned)n > maxIdleTimeout && !IS(PRIVILEGED)) { |
| 712 |
idleTimeout = maxIdleTimeout; |
| 713 |
reply(250, "%lu: Idle timeout too large, set to %u.", |
| 714 |
n, maxIdleTimeout); |
| 715 |
} else { |
| 716 |
idleTimeout = (int) n; |
| 717 |
reply(250, "Idle timeout set to %u.", idleTimeout); |
| 718 |
} |
| 719 |
return (true); |
| 720 |
} |
| 721 |
break; |
| 722 |
case T_HELP: // return help |
| 723 |
if (opt_CRLF()) { |
| 724 |
helpCmd(sitetab, (char*) NULL); |
| 725 |
return (true); |
| 726 |
} else if (string_param(s, "command name")) { |
| 727 |
logcmd(T_SITE, "HELP %s", (const char*) s); |
| 728 |
s.raisecase(); |
| 729 |
helpCmd(sitetab, s); |
| 730 |
return (true); |
| 731 |
} |
| 732 |
break; |
| 733 |
case T_JQUEUE: |
| 734 |
if (boolean_param(b)) { |
| 735 |
logcmd(t, "%s", b ? "YES" : "NO"); |
| 736 |
defJob.queued = b; |
| 737 |
reply(250, "Job will%s be queued.", b ? "" : " not"); |
| 738 |
return (true); |
| 739 |
} |
| 740 |
break; |
| 741 |
case T_FROM_USER: |
| 742 |
case T_MODEM: |
| 743 |
case T_NOTIFY: |
| 744 |
case T_NOTIFYADDR: |
| 745 |
if (SPACE() && multi_STRING(s) && CRLF() && setJobParameter(defJob, t, s)) { |
| 746 |
logcmd(t, "%s", (const char*) s); |
| 747 |
reply(250, "%s set to \"%s\".", parmToken(t), (const char*) s); |
| 748 |
return (true); |
| 749 |
} |
| 750 |
break; |
| 751 |
case T_MAXDIALS: |
| 752 |
case T_MAXTRIES: |
| 753 |
case T_SCHEDPRI: |
| 754 |
if (number_param(n) && setJobParameter(defJob, t, (u_short) n)) { |
| 755 |
logcmd(t, "%lu", n); |
| 756 |
reply(250, "%s set to %u.", parmToken(t), n); |
| 757 |
return (true); |
| 758 |
} |
| 759 |
break; |
| 760 |
case T_LASTTIME: // time to kill job |
| 761 |
if (timespec_param(6, tv) && setJobParameter(defJob, t, tv)) { |
| 762 |
logcmd(t, "%02d%02d%02d" |
| 763 |
, tv/(24*60*60) , (tv/(60*60))%24 , (tv/60)%60); |
| 764 |
reply(250, "%s set to %02d%02d%02d." |
| 765 |
, parmToken(t) |
| 766 |
, tv/(24*60*60) , (tv/(60*60))%24 , (tv/60)%60); |
| 767 |
return (true); |
| 768 |
} |
| 769 |
break; |
| 770 |
case T_RETRYTIME: // retry interval for job |
| 771 |
if (timespec_param(4, tv) && setJobParameter(defJob, t, tv)) { |
| 772 |
logcmd(t, "%02d%02d" , tv/60, tv%60); |
| 773 |
reply(250, "%s set to %02d%02d.", parmToken(t), tv/60, tv%60); |
| 774 |
return (true); |
| 775 |
} |
| 776 |
break; |
| 777 |
case T_JPARM: // query job parameters |
| 778 |
if (CRLF()) { |
| 779 |
logcmd(t); |
| 780 |
jstatCmd(defJob); |
| 781 |
return (true); |
| 782 |
} |
| 783 |
break; |
| 784 |
default: |
| 785 |
break; |
| 786 |
} |
| 787 |
return (false); |
| 788 |
} |
| 789 |
|
| 790 |
/* |
| 791 |
* Parse an SNPP time specification. |
| 792 |
*/ |
| 793 |
bool |
| 794 |
SNPPServer::SNPPTime(time_t& result) |
| 795 |
{ |
| 796 |
if (getToken(T_STRING, "time specification") && checkNUMBER(tokenBody)) { |
| 797 |
u_int tlen = tokenBody.length(); |
| 798 |
const char* cp = tokenBody; |
| 799 |
// YYMMDDHHMM[SS] |
| 800 |
if (tlen == 12 || tlen == 10) { |
| 801 |
struct tm tm; |
| 802 |
tm.tm_sec = (tlen == 12 ? twodigits(cp+10, 60) : 0); |
| 803 |
tm.tm_min = twodigits(cp+8, 60); |
| 804 |
tm.tm_hour = twodigits(cp+6, 24); |
| 805 |
tm.tm_mday = twodigits(cp+4, 32); |
| 806 |
tm.tm_mon = twodigits(cp+2, 13) - 1; |
| 807 |
tm.tm_year = twodigits(cp+0, 100); |
| 808 |
/* |
| 809 |
* SNPP botched time specifications by not using 4 digits |
| 810 |
* to specify a year. This means that we have to guess at |
| 811 |
* the intended year when the value is far in the future |
| 812 |
* (as opposed to a year in the past that was given by |
| 813 |
* mistake). We arbitrarily assume years prior to 1990 |
| 814 |
* are in the next century and adjust them accordingly. |
| 815 |
*/ |
| 816 |
if (tm.tm_year < 90) |
| 817 |
tm.tm_year += 100; |
| 818 |
tm.tm_isdst= -1; // XXX not sure about this??? |
| 819 |
/* |
| 820 |
* The above time is assumed to be relative to |
| 821 |
* GMT and mktime returns locally adjusted time |
| 822 |
* so we need to adjust the result to get things |
| 823 |
* in the right timezone. Note that any additional |
| 824 |
* timezone correction factor specified by the |
| 825 |
* client will then be applied to this result. |
| 826 |
*/ |
| 827 |
result = mktime(&tm) - gmtoff; |
| 828 |
return (true); |
| 829 |
} |
| 830 |
syntaxError(fxStr::format( |
| 831 |
"bad time specification (expecting 10/12 digits, got %u)", tlen)); |
| 832 |
} |
| 833 |
return (false); |
| 834 |
} |
| 835 |
|
| 836 |
void |
| 837 |
SNPPServer::syntaxError(const char* msg) |
| 838 |
{ |
| 839 |
const char* cp = strchr(cbuf, '\0'); |
| 840 |
if (cp[-1] == '\n') |
| 841 |
cp--; |
| 842 |
reply(500, "'%.*s': Syntax error, %s.", cp-cbuf, cbuf, msg); |
| 843 |
} |
| 844 |
|
| 845 |
/* |
| 846 |
* Command support methods. |
| 847 |
*/ |
| 848 |
|
| 849 |
/* |
| 850 |
* Process a multi-line text message. |
| 851 |
*/ |
| 852 |
void |
| 853 |
SNPPServer::dataCmd(void) |
| 854 |
{ |
| 855 |
if (!haveText) { |
| 856 |
fxStr emsg; |
| 857 |
u_int seqnum = getDocumentNumber(emsg); |
| 858 |
if (seqnum == (u_int) -1) { |
| 859 |
reply(554, "%s", (const char*)emsg); |
| 860 |
return; |
| 861 |
} |
| 862 |
msgFile = fxStr::format("/%s/doc%u.page", FAX_TMPDIR, seqnum); |
| 863 |
FILE* fout = Sys::fopen(msgFile, "w"); |
| 864 |
if (fout != NULL) { |
| 865 |
setFileOwner(msgFile); |
| 866 |
FileCache::chmod(msgFile, 0660); // sync cache |
| 867 |
tempFiles.append(msgFile); |
| 868 |
reply(354, "Begin message input; end with <CRLF>'.'<CRLF>."); |
| 869 |
char buf[1024]; |
| 870 |
u_int len = 0; |
| 871 |
for (;;) { |
| 872 |
if (getCmdLine(buf, sizeof (buf), true)) { |
| 873 |
const char* bp = buf; |
| 874 |
if (bp[0] == '.') { |
| 875 |
if ((bp[1] == '\n' && bp[2] == '\0') || bp[1] == '\0') |
| 876 |
break; |
| 877 |
if (bp[1] == '.' && bp[2] == '\0') // .. -> . |
| 878 |
bp++; |
| 879 |
} |
| 880 |
u_int blen = strlen(bp); |
| 881 |
if ((len += blen) > maxMsgLength) { |
| 882 |
reply(550, |
| 883 |
"Error, message too long; max %u characters.", |
| 884 |
maxMsgLength); |
| 885 |
(void) fclose(fout); |
| 886 |
Sys::unlink(msgFile); |
| 887 |
return; |
| 888 |
} |
| 889 |
// if buf only contains .., we skipped the first . |
| 890 |
// hence we must write bp, not buf. also blen is |
| 891 |
// the length of bp, not buf. |
| 892 |
(void) fwrite((const char*) bp, blen, 1, fout); |
| 893 |
} |
| 894 |
} |
| 895 |
if (fclose(fout)) { |
| 896 |
perror_reply(554, msgFile, errno); |
| 897 |
Sys::unlink(msgFile); |
| 898 |
} else { |
| 899 |
haveText = true; |
| 900 |
reply(250, "Message text OK."); |
| 901 |
} |
| 902 |
} else |
| 903 |
perror_reply(554, msgFile, errno); |
| 904 |
} else |
| 905 |
reply(503, "Error, message already entered."); |
| 906 |
} |
| 907 |
|
| 908 |
/* |
| 909 |
* Provide help. We cannot share the base class |
| 910 |
* implementation of this command because SNPP |
| 911 |
* defines a different style for responses (sigh). |
| 912 |
*/ |
| 913 |
void |
| 914 |
SNPPServer::helpCmd(const tab* ctab, const char* s) |
| 915 |
{ |
| 916 |
const char* type; |
| 917 |
u_int NCMDS; |
| 918 |
if (ctab == sitetab) { |
| 919 |
type = "SITE "; |
| 920 |
NCMDS = N(sitetab); |
| 921 |
} else { |
| 922 |
type = ""; |
| 923 |
NCMDS = N(cmdtab); |
| 924 |
} |
| 925 |
int width = 0; |
| 926 |
const tab* c = ctab; |
| 927 |
for (u_int n = NCMDS; n != 0; c++, n--) { |
| 928 |
int len = strlen(c->name); |
| 929 |
if (len > width) |
| 930 |
width = len; |
| 931 |
} |
| 932 |
width = (width + 8) &~ 7; |
| 933 |
if (s == NULL) { |
| 934 |
reply(214, "The following %scommands are recognized %s.", |
| 935 |
type, "(* =>'s unimplemented)"); |
| 936 |
int columns = 76 / width; |
| 937 |
if (columns == 0) |
| 938 |
columns = 1; |
| 939 |
int lines = (NCMDS + columns - 1) / columns; |
| 940 |
for (int i = 0; i < lines; i++) { |
| 941 |
printf("214 "); |
| 942 |
for (int j = 0; j < columns; j++) { |
| 943 |
c = &ctab[j*lines + i]; |
| 944 |
printf("%s%c", c->name, !c->implemented ? '*' : ' '); |
| 945 |
if (c + lines >= &ctab[NCMDS]) |
| 946 |
break; |
| 947 |
int w = strlen(c->name) + 1; |
| 948 |
while (w < width) { |
| 949 |
putchar(' '); |
| 950 |
w++; |
| 951 |
} |
| 952 |
} |
| 953 |
printf("\r\n"); |
| 954 |
} |
| 955 |
(void) fflush(stdout); |
| 956 |
reply(250, "Direct comments to %s.", (const char*) faxContact); |
| 957 |
} else { |
| 958 |
c = lookup(ctab, NCMDS, s); |
| 959 |
if (c == NULL) |
| 960 |
reply(550, "Unknown command %s.", s); |
| 961 |
else if (c->implemented) |
| 962 |
reply(218, "Syntax: %s%s %s", type, c->name, c->help); |
| 963 |
else |
| 964 |
reply(218, "%s%-*s\t%s; unimplemented.", |
| 965 |
type, width, c->name, c->help); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
/* |
| 970 |
* Specify the hold time (time to send) for a request. |
| 971 |
*/ |
| 972 |
void |
| 973 |
SNPPServer::holdCmd(time_t when) |
| 974 |
{ |
| 975 |
time_t now = Sys::now(); |
| 976 |
if (when > now) { |
| 977 |
defJob.tts = when; |
| 978 |
const struct tm* tm = cvtTime(defJob.tts); |
| 979 |
reply(250, "Message will be processed at %02d%02d%02d%02d%02d." |
| 980 |
, tm->tm_year |
| 981 |
, tm->tm_mon+1 |
| 982 |
, tm->tm_mday |
| 983 |
, tm->tm_hour |
| 984 |
, tm->tm_min |
| 985 |
); |
| 986 |
} else |
| 987 |
reply(550, "Invalid delivery date/time; time in the past."); |
| 988 |
} |
| 989 |
|
| 990 |
/* |
| 991 |
* Login as the specified user. |
| 992 |
*/ |
| 993 |
void |
| 994 |
SNPPServer::loginCmd(const char* loginID, const char* pass) |
| 995 |
{ |
| 996 |
if (IS(LOGGEDIN)) |
| 997 |
end_login(); |
| 998 |
the_user = loginID; |
| 999 |
state &= ~S_PRIVILEGED; |
| 1000 |
adminwd = "*"; // make sure no admin privileges |
| 1001 |
passwd = "*"; // just in case... |
| 1002 |
|
| 1003 |
if (checkUser(loginID)) { |
| 1004 |
if (passwd != "" && ! checkPasswd(pass)) { |
| 1005 |
if (++loginAttempts >= maxLoginAttempts) { |
| 1006 |
reply(421, "Login incorrect (closing connection)."); |
| 1007 |
logNotice("Repeated SNPP login failures for user %s from %s [%s]" |
| 1008 |
, (const char*) the_user |
| 1009 |
, (const char*) remotehost |
| 1010 |
, (const char*) remoteaddr |
| 1011 |
); |
| 1012 |
dologout(0); |
| 1013 |
} |
| 1014 |
reply(550, "Login incorrect."); |
| 1015 |
logInfo("SNPP login failed from %s [%s], %s" |
| 1016 |
, (const char*) remotehost |
| 1017 |
, (const char*) remoteaddr |
| 1018 |
, (const char*) the_user |
| 1019 |
); |
| 1020 |
return; |
| 1021 |
} |
| 1022 |
login(250); |
| 1023 |
} else { |
| 1024 |
if (++loginAttempts >= maxLoginAttempts) { |
| 1025 |
reply(421, "Login incorrect (closing connection)."); |
| 1026 |
logNotice("Repeated SNPP login failures for user %s from %s [%s]" |
| 1027 |
, (const char*) the_user |
| 1028 |
, (const char*) remotehost |
| 1029 |
, (const char*) remoteaddr |
| 1030 |
); |
| 1031 |
dologout(0); |
| 1032 |
} else { |
| 1033 |
reply(421, "User %s access denied.", (const char*) the_user); |
| 1034 |
logNotice("SNPP LOGIN REFUSED (%s) FROM %s [%s], %s" |
| 1035 |
, "user denied" |
| 1036 |
, (const char*) remotehost |
| 1037 |
, (const char*) remoteaddr |
| 1038 |
, (const char*) the_user |
| 1039 |
); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* |
| 1045 |
* Specify a one-line message text. |
| 1046 |
*/ |
| 1047 |
void |
| 1048 |
SNPPServer::messageCmd(const char* msg) |
| 1049 |
{ |
| 1050 |
if (!haveText) { |
| 1051 |
u_int len = strlen(msg); |
| 1052 |
if (len > maxMsgLength) { |
| 1053 |
reply(550, |
| 1054 |
"Error, message too long; no more than %u characters accepted.", |
| 1055 |
maxMsgLength); |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
fxStr emsg; |
| 1059 |
u_int seqnum = getDocumentNumber(emsg); |
| 1060 |
if (seqnum == (u_int) -1) { |
| 1061 |
reply(554, "%s", (const char*)emsg); |
| 1062 |
return; |
| 1063 |
} |
| 1064 |
msgFile = fxStr::format("/%s/doc%u.page", FAX_TMPDIR, seqnum); |
| 1065 |
FILE* fout = Sys::fopen(msgFile, "w"); |
| 1066 |
if (fout != NULL) { |
| 1067 |
setFileOwner(msgFile); |
| 1068 |
FileCache::chmod(msgFile, 0660); // sync cache |
| 1069 |
tempFiles.append(msgFile); |
| 1070 |
(void) fwrite(msg, len, 1, fout); |
| 1071 |
if (fclose(fout)) { |
| 1072 |
perror_reply(554, msgFile, errno); |
| 1073 |
Sys::unlink(msgFile); |
| 1074 |
} else { |
| 1075 |
haveText = true; |
| 1076 |
reply(250, "Message text OK."); |
| 1077 |
} |
| 1078 |
} else |
| 1079 |
perror_reply(554, msgFile, errno); |
| 1080 |
} else |
| 1081 |
reply(503, "Error, message already entered."); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* |
| 1085 |
* Process a PAGE command. Map the client-specified pager |
| 1086 |
* identification string to a service provider phone number |
| 1087 |
* and destination PIN and create a new job for this request. |
| 1088 |
*/ |
| 1089 |
void |
| 1090 |
SNPPServer::pagerCmd(const char* pagerID, const char* pin) |
| 1091 |
{ |
| 1092 |
/* |
| 1093 |
* Lookup pager ID and map to a service provider |
| 1094 |
* and, optionally, PIN. |
| 1095 |
*/ |
| 1096 |
fxStr provider; |
| 1097 |
fxStr PIN; |
| 1098 |
fxStr emsg; |
| 1099 |
if (!mapPagerID(pagerID, provider, PIN, emsg)) { |
| 1100 |
reply(550, "%s.", (const char*) emsg); |
| 1101 |
return; |
| 1102 |
} |
| 1103 |
/* |
| 1104 |
* RFC 1861 says we should lock the Level, Coverage, |
| 1105 |
* Holdtime, and Alert values for the request at this point. |
| 1106 |
* To do this we create a job (inheriting the current state |
| 1107 |
* from the default job) and fill in any other information. |
| 1108 |
* However we do not submit it until later (when a SEND |
| 1109 |
* request is made). |
| 1110 |
*/ |
| 1111 |
curJob = &defJob; // inherit from default job |
| 1112 |
// XXX merge items to same provider (maybe?) |
| 1113 |
if (newJob(emsg) && updateJobOnDisk(*curJob, emsg)) { |
| 1114 |
fxStr file("/" | curJob->qfile); |
| 1115 |
setFileOwner(file); // force ownership |
| 1116 |
FileCache::chmod(file, 0660); // sync cache |
| 1117 |
curJob->lastmod = Sys::now(); // no one else should update |
| 1118 |
|
| 1119 |
curJob->number = provider; // destination phone number |
| 1120 |
if (!pin) // use mapped value |
| 1121 |
pin = PIN; |
| 1122 |
curJob->items.append(FaxItem(FaxRequest::send_page, 0, "", pin)); |
| 1123 |
curJob->items.append( |
| 1124 |
FaxItem(FaxRequest::send_page_saved, 0, "", pin)); |
| 1125 |
reply(250, "Pager ID accepted; provider: %s pin: %s jobid: %s." |
| 1126 |
, (const char*) provider |
| 1127 |
, pin |
| 1128 |
, (const char*) curJob->jobid |
| 1129 |
); |
| 1130 |
msgs.append(curJob->jobid); |
| 1131 |
} else |
| 1132 |
reply(554, "%s.", (const char*) emsg); |
| 1133 |
initSNPPJob(); // reset job-related state |
| 1134 |
} |
| 1135 |
|
| 1136 |
/* |
| 1137 |
* Validate a client-specified pager ID string. |
| 1138 |
* This is only sort of like the intended usage |
| 1139 |
* but is the only thing that makes sense in |
| 1140 |
* our environment (where the provider is not |
| 1141 |
* directly accessible). |
| 1142 |
*/ |
| 1143 |
void |
| 1144 |
SNPPServer::pingCmd(const char* pagerID) |
| 1145 |
{ |
| 1146 |
/* |
| 1147 |
* Lookup pager ID and map to a service provider |
| 1148 |
* and, optionally, PIN. |
| 1149 |
*/ |
| 1150 |
fxStr provider; |
| 1151 |
fxStr PIN; |
| 1152 |
fxStr emsg; |
| 1153 |
if (mapPagerID(pagerID, provider, PIN, emsg)) |
| 1154 |
reply(821, "Valid pager ID, no location information available."); |
| 1155 |
else |
| 1156 |
reply(550, "%s.", (const char*) emsg); |
| 1157 |
} |
| 1158 |
|
| 1159 |
static bool |
| 1160 |
notPresent(FaxItemArray& a, const char* name) |
| 1161 |
{ |
| 1162 |
for (u_int i = 0, n = a.length(); i < n; i++) |
| 1163 |
if (a[i].op == FaxRequest::send_data && a[i].item == name) |
| 1164 |
return (false); |
| 1165 |
return (true); |
| 1166 |
} |
| 1167 |
|
| 1168 |
/* |
| 1169 |
* Send (submit) a pager request. We complete the |
| 1170 |
* formulation of the job and submit it to the |
| 1171 |
* scheduler. If this job is marked for no queueing |
| 1172 |
* then we also wait around for the job to complete. |
| 1173 |
*/ |
| 1174 |
void |
| 1175 |
SNPPServer::sendCmd(void) |
| 1176 |
{ |
| 1177 |
if (msgs.length() == 0) { |
| 1178 |
reply(503, "Error, no pager ID specified with PAGE."); |
| 1179 |
return; |
| 1180 |
} |
| 1181 |
/* |
| 1182 |
* If we need to wait for jobs to complete, construct |
| 1183 |
* and register a trigger now before we submit the jobs. |
| 1184 |
*/ |
| 1185 |
fxStr emsg; |
| 1186 |
u_int i, n = msgs.length(); |
| 1187 |
bool waitForJobs = false; |
| 1188 |
for (i = 0; i < n; i++) { |
| 1189 |
Job* job = findJob(msgs[i], emsg); |
| 1190 |
if (!job) |
| 1191 |
msgs.remove(i), n--; |
| 1192 |
else if (!job->queued) |
| 1193 |
waitForJobs = true; |
| 1194 |
} |
| 1195 |
if (waitForJobs) { |
| 1196 |
state &= ~S_LOGTRIG; // just process events |
| 1197 |
if (!newTrigger(emsg, "J%04x", 1<<Trigger::JOB_DEAD)) { |
| 1198 |
reply(550, |
| 1199 |
"Cannot register trigger to wait for job completion: %s.", |
| 1200 |
(const char*) emsg); |
| 1201 |
return; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
const char* docname = msgFile; |
| 1205 |
const char* cp = strrchr(docname, '/'); |
| 1206 |
if (!cp) // relative name, e.g. doc123 |
| 1207 |
cp = docname; |
| 1208 |
for (i = 0; i < n; i++) { |
| 1209 |
Job* job = findJob(msgs[i], emsg); |
| 1210 |
if (!job) { |
| 1211 |
msgs.remove(i), n--; |
| 1212 |
continue; |
| 1213 |
} |
| 1214 |
if (*cp != '\0') { |
| 1215 |
/* |
| 1216 |
* Add a reference to the message text for |
| 1217 |
* the current job, if not already present. |
| 1218 |
*/ |
| 1219 |
fxStr document = |
| 1220 |
fxStr::format("/" FAX_DOCDIR "%s.", cp) | job->jobid; |
| 1221 |
if (notPresent(job->items, &document[1])) { |
| 1222 |
if (Sys::link(docname, document) < 0) { |
| 1223 |
reply(554, "Unable to link document %s to %s: %s.", |
| 1224 |
docname, (const char*) document, strerror(errno)); |
| 1225 |
return; |
| 1226 |
} |
| 1227 |
job->items.append( |
| 1228 |
FaxItem(FaxRequest::send_data, 0, "", &document[1])); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
/* |
| 1232 |
* Submit the job for scheduling. |
| 1233 |
*/ |
| 1234 |
if (job->mailaddr == "") |
| 1235 |
replyBadJob(*job, T_NOTIFYADDR); |
| 1236 |
else if (job->sender == "") |
| 1237 |
replyBadJob(*job, T_FROM_USER); |
| 1238 |
else if (job->modem == "") |
| 1239 |
replyBadJob(*job, T_MODEM); |
| 1240 |
else if (job->client == "") |
| 1241 |
replyBadJob(*job, T_CLIENT); |
| 1242 |
else { |
| 1243 |
if (job->external == "") |
| 1244 |
job->external = job->number; |
| 1245 |
if (job->tts == 0) |
| 1246 |
job->tts = Sys::now(); |
| 1247 |
job->killtime += job->tts; // adjust based on any hold time |
| 1248 |
if (updateJobOnDisk(*job, emsg)) { |
| 1249 |
const char* jobid = job->jobid; |
| 1250 |
/* |
| 1251 |
* NB: we don't mark the lastmod time for the |
| 1252 |
* job since the scheduler should re-write the |
| 1253 |
* queue file to reflect what it did with it |
| 1254 |
* (e.g. what state it placed the job in). |
| 1255 |
*/ |
| 1256 |
if (sendQueuerACK(emsg, "S%s", jobid)) |
| 1257 |
continue; |
| 1258 |
reply(554, "Failed to submit message %s: %s.", |
| 1259 |
jobid, (const char*) emsg); |
| 1260 |
} else |
| 1261 |
reply(554, "%s.", (const char*) emsg); |
| 1262 |
} |
| 1263 |
if (waitForJobs) { // cleanup trigger on error |
| 1264 |
cancelTrigger(emsg); |
| 1265 |
/* |
| 1266 |
* Not sure what to do here. If some jobs got |
| 1267 |
* submitted then we want to remove them so the |
| 1268 |
* client can just reissue SEND if the error was |
| 1269 |
* transient (e.g. faxq was temporarily stopped). |
| 1270 |
* However we don't track which jobs got started |
| 1271 |
* and which did not so for now we'll just leave |
| 1272 |
* everything so the client can resubmit things |
| 1273 |
* with a subsequent SEND. Unfortunately this |
| 1274 |
* can result in duplicate pages being sent. |
| 1275 |
*/ |
| 1276 |
} |
| 1277 |
return; // failure |
| 1278 |
} |
| 1279 |
if (waitForJobs) { // no queueing, wait for submitted jobs |
| 1280 |
if (setjmp(urgcatch) == 0) { |
| 1281 |
Dispatcher& disp = Dispatcher::instance(); |
| 1282 |
state |= S_WAITTRIG|S_SENDWAIT; |
| 1283 |
bool jobsPending; |
| 1284 |
do { |
| 1285 |
disp.dispatch(); |
| 1286 |
/* |
| 1287 |
* The trigger event handlers update our notion |
| 1288 |
* of the job state asynchronously so we can just |
| 1289 |
* monitor the job state(s) after each event we |
| 1290 |
* receive. |
| 1291 |
*/ |
| 1292 |
jobsPending = false; |
| 1293 |
for (i = 0; i < n; i++) { |
| 1294 |
Job* job = findJob(msgs[i], emsg); |
| 1295 |
if (!job) |
| 1296 |
msgs.remove(i), n--; |
| 1297 |
else if (!job->queued && |
| 1298 |
job->state != FaxRequest::state_done && |
| 1299 |
job->state != FaxRequest::state_failed) |
| 1300 |
jobsPending = true; |
| 1301 |
} |
| 1302 |
} while (IS(WAITTRIG) && jobsPending); |
| 1303 |
reply(250, "Message processing completed."); |
| 1304 |
} |
| 1305 |
state &= ~(S_WAITTRIG|S_SENDWAIT); |
| 1306 |
(void) cancelTrigger(emsg); |
| 1307 |
} else // jobs queued, just acknowledge |
| 1308 |
reply(250, "Message%s successfully queued.", |
| 1309 |
msgs.length() > 1 ? "s" : ""); |
| 1310 |
resetState(); // reset SEND-related state |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* |
| 1314 |
* Set the message service level. We currently use |
| 1315 |
* this just to select a scheduling priority and |
| 1316 |
* job expiration time. |
| 1317 |
*/ |
| 1318 |
void |
| 1319 |
SNPPServer::serviceLevel(u_int level) |
| 1320 |
{ |
| 1321 |
if (level <= 11) { |
| 1322 |
defJob.usrpri = priMap[level]; |
| 1323 |
defJob.killtime = 60*killMap[level]; |
| 1324 |
defJob.retrytime = retryMap[level]; |
| 1325 |
reply(250, "OK, service level %u accepted.", level); |
| 1326 |
} else |
| 1327 |
reply(550, "Invalid service level %u; we only handle 0-11.", level); |
| 1328 |
} |
| 1329 |
|
| 1330 |
/* |
| 1331 |
* Return server status. |
| 1332 |
*/ |
| 1333 |
void |
| 1334 |
SNPPServer::statusCmd(void) |
| 1335 |
{ |
| 1336 |
reply(214, "%s SNPP server status:", (const char*) hostname); |
| 1337 |
reply(214, " %s", version); |
| 1338 |
if (!isdigit(remotehost[0])) |
| 1339 |
reply(214, " Connected to %s (%s).", |
| 1340 |
(const char*) remotehost, (const char*) remoteaddr); |
| 1341 |
else |
| 1342 |
reply(214, " Connected to %s.", (const char*) remotehost); |
| 1343 |
if (IS(LOGGEDIN)) { |
| 1344 |
reply(214, " Logged in as user %s (uid %u)." |
| 1345 |
, (const char*) the_user |
| 1346 |
, uid |
| 1347 |
); |
| 1348 |
} else |
| 1349 |
reply(214, " Waiting for login."); |
| 1350 |
reply(214, " Idle timeout set to %d seconds.", idleTimeout); |
| 1351 |
if (discTime > 0) |
| 1352 |
reply(214, " Server scheduled to be unavailable at %.24s.", |
| 1353 |
asctime(cvtTime(discTime))); |
| 1354 |
else |
| 1355 |
reply(214, " No server down time currently scheduled."); |
| 1356 |
reply(214, " HylaFAX scheduler reached at %s (%sconnected)." |
| 1357 |
, (const char*) faxqFIFOName |
| 1358 |
, faxqFd == -1 ? "not " : "" |
| 1359 |
); |
| 1360 |
if (clientFd != -1) |
| 1361 |
reply(214, " Server FIFO is /%s (%sopen)." |
| 1362 |
, (const char*) clientFIFOName |
| 1363 |
, clientFd == -1 ? "not " : "" |
| 1364 |
); |
| 1365 |
if (IS(WAITFIFO)) |
| 1366 |
reply(214, " Waiting for response from HylaFAX scheduler."); |
| 1367 |
if (msgs.length() > 0) { |
| 1368 |
reply(214, " %u message%s prepared for transmission.", |
| 1369 |
msgs.length(), msgs.length() > 1 ? "s" : ""); |
| 1370 |
// XXX dump status of msgs |
| 1371 |
} else |
| 1372 |
reply(214, " No messages prepared for transmission."); |
| 1373 |
reply(214, " %s message text.", haveText ? "Received" : "No received"); |
| 1374 |
|
| 1375 |
reply(250, "End of status"); |
| 1376 |
} |
| 1377 |
|
| 1378 |
/* |
| 1379 |
* Set the subject for outgoing messages. |
| 1380 |
* For now we just use it to tag the jobs. |
| 1381 |
*/ |
| 1382 |
void |
| 1383 |
SNPPServer::subjectCmd(const char* subj) |
| 1384 |
{ |
| 1385 |
defJob.jobtag = subj; // XXX |
| 1386 |
reply(250, "Message subject OK."); |
| 1387 |
} |
| 1388 |
|
| 1389 |
/* |
| 1390 |
* Configuration support. |
| 1391 |
*/ |
| 1392 |
|
| 1393 |
void |
| 1394 |
SNPPServer::resetConfig() |
| 1395 |
{ |
| 1396 |
InetFaxServer::resetConfig(); |
| 1397 |
setupConfig(); |
| 1398 |
} |
| 1399 |
|
| 1400 |
void |
| 1401 |
SNPPServer::setupConfig() |
| 1402 |
{ |
| 1403 |
setConfigItem("maxmsglength", "128"); |
| 1404 |
setConfigItem("pageridmapfile","/etc/pagermap"); |
| 1405 |
setConfigItem("prioritymap", |
| 1406 |
"63 127 127 127 127 127 127 127 127 127 127 127"); |
| 1407 |
setConfigItem("killtimemap", |
| 1408 |
" 5 5 5 15 60 240 720 1440 1440 1440 1440 1440"); |
| 1409 |
setConfigItem("retrytimemap", |
| 1410 |
"30 60 60 180 0 0 0 0 0 0 0 0"); |
| 1411 |
} |
| 1412 |
|
| 1413 |
static void |
| 1414 |
setShortMap(u_short map[12], const char* value) |
| 1415 |
{ |
| 1416 |
char* cp; |
| 1417 |
for (int i = 0; i < 12; i++) { |
| 1418 |
map[i] = (u_short) strtoul(value, &cp, 0); |
| 1419 |
if (!cp && *cp == '\0') |
| 1420 |
break; |
| 1421 |
value = cp; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
static void |
| 1426 |
setTimeMap(time_t map[12], const char* value) |
| 1427 |
{ |
| 1428 |
char* cp; |
| 1429 |
for (int i = 0; i < 12; i++) { |
| 1430 |
map[i] = (time_t) strtoul(value, &cp, 0); |
| 1431 |
if (!cp && *cp == '\0') |
| 1432 |
break; |
| 1433 |
value = cp; |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
bool |
| 1438 |
SNPPServer::setConfigItem(const char* tag, const char* value) |
| 1439 |
{ |
| 1440 |
if (streq(tag, "maxmsglength")) { |
| 1441 |
maxMsgLength = getNumber(value); |
| 1442 |
} else if (streq(tag, "pageridmapfile")) { |
| 1443 |
pagerIDMapFile = value; |
| 1444 |
} else if (streq(tag, "prioritymap")) { |
| 1445 |
setShortMap(priMap, value); |
| 1446 |
} else if (streq(tag, "killtimemap")) { |
| 1447 |
setTimeMap(killMap, value); |
| 1448 |
} else if (streq(tag, "retrytimemap")) { |
| 1449 |
setTimeMap(retryMap, value); |
| 1450 |
} else if (!InetFaxServer::setConfigItem(tag, value)) |
| 1451 |
return (false); |
| 1452 |
return (true); |
| 1453 |
} |
| 1454 |
#endif /* SNPP_SUPPORT */ |