| 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 |
#include "port.h" |
| 27 |
#include "Sys.h" |
| 28 |
#include "config.h" |
| 29 |
#include "tiffio.h" |
| 30 |
|
| 31 |
#include "HylaFAXServer.h" |
| 32 |
#include "Dispatcher.h" |
| 33 |
#include "StackBuffer.h" |
| 34 |
#include "Sequence.h" |
| 35 |
|
| 36 |
#include <ctype.h> |
| 37 |
#include <sys/file.h> |
| 38 |
#include <pwd.h> |
| 39 |
#include <limits.h> |
| 40 |
#include <grp.h> |
| 41 |
#include <unistd.h> |
| 42 |
#include <stdlib.h> |
| 43 |
|
| 44 |
const char* HylaFAXServer::version = HYLAFAX_VERSION; |
| 45 |
int HylaFAXServer::_debugSleep = 0; |
| 46 |
|
| 47 |
/* |
| 48 |
* NB: The remainder of the instance state is |
| 49 |
* initialized in initServer which is expected to |
| 50 |
* be called before any other operation is done |
| 51 |
* (through open). This is required because some |
| 52 |
* initialization work is done by virtual methods |
| 53 |
* that cannot be invoked from the constructor. |
| 54 |
*/ |
| 55 |
HylaFAXServer::HylaFAXServer() |
| 56 |
: defJob("") |
| 57 |
{ |
| 58 |
state = 0; |
| 59 |
xferfaxlog = -1; |
| 60 |
|
| 61 |
loginAttempts = 0; // NB: not reset by REIN command |
| 62 |
adminAttempts = 0; // NB: not reset by REIN command |
| 63 |
idcache = NULL; |
| 64 |
|
| 65 |
data = -1; // current data connection (socket) |
| 66 |
pdata = -1; // passive mode data connect (socket) |
| 67 |
faxqFd = -1; |
| 68 |
clientFd = -1; |
| 69 |
|
| 70 |
char buff[64]; |
| 71 |
(void) gethostname(buff, sizeof(buff)); |
| 72 |
hostname = buff; |
| 73 |
hostaddr = "unknown"; // derived classes should fill-in |
| 74 |
|
| 75 |
lastModTime = 0; // shutdown file mod time |
| 76 |
discTime = 0; // shutdown forced disconnect time |
| 77 |
denyTime = 0; // shutdown service denial time |
| 78 |
/* |
| 79 |
* Calculate the time differential between the local timezone |
| 80 |
* and GMT for adjusting client-specified time values that are |
| 81 |
* given in GMT. |
| 82 |
* |
| 83 |
* We want the actual offset of the local now from UTC, |
| 84 |
* This means we have to "force" daylight savings off, otherwise |
| 85 |
* the offset we get back is of the real timezone (the time |
| 86 |
* functions automatically compensate if DST is set). |
| 87 |
* |
| 88 |
* Because we take the actual offset instead of a real timezone, |
| 89 |
* we force the TZ a format CUT<offset> instead of an actual |
| 90 |
* timezone like EST<offset>EDT, or PST<offset>PDT. This means |
| 91 |
* we no longer have to rely on the system being able to do the |
| 92 |
* DST offset. |
| 93 |
*/ |
| 94 |
|
| 95 |
time_t now = Sys::now(); |
| 96 |
struct tm gmt = *gmtime(&now); |
| 97 |
struct tm tm = *localtime(&now); |
| 98 |
tm.tm_isdst = 0; |
| 99 |
gmtoff = mktime(&gmt) - mktime(&tm); |
| 100 |
#if HAS_TM_ZONE |
| 101 |
/* |
| 102 |
* BSD/OS doesn't support the global timezone |
| 103 |
* information so setup substitutes here. |
| 104 |
*/ |
| 105 |
tzname[0] = tm.tm_zone; |
| 106 |
tzname[1] = NULL; |
| 107 |
#endif |
| 108 |
|
| 109 |
/* |
| 110 |
* Some versions of glibc (like RHEL4) will revert to UTC in |
| 111 |
* the chroot if it can't find the zoneinfo file and no TZ is |
| 112 |
* set in the environment. |
| 113 |
* We'll set the TZ to our crafted TZ to make sure that we |
| 114 |
* have the right offset even if no timezone DST info is |
| 115 |
* available in the chroot. |
| 116 |
*/ |
| 117 |
fxStr tz = fxStr::format("CUT%d:%02d", |
| 118 |
(gmtoff / 3600), ((gmtoff / 60) % 60)); |
| 119 |
setenv("TZ", tz, 0); |
| 120 |
|
| 121 |
cachedTIFF = NULL; |
| 122 |
} |
| 123 |
|
| 124 |
HylaFAXServer::~HylaFAXServer() |
| 125 |
{ |
| 126 |
dologout(0); |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Initialize the state of the server. Note that this |
| 131 |
* is used to implement the REIN command and for that |
| 132 |
* it doesn't work entirely because state set from the |
| 133 |
* configuration files is not accessible after a login |
| 134 |
* because the sever does a chroot to the top of the |
| 135 |
* spooling area. The only way to make this work is |
| 136 |
* to keep all the configuration files in the spooling |
| 137 |
* area which is not folks want when things are shared |
| 138 |
* through NFS (i.e. server binaries and config files |
| 139 |
* are NFS-mounted by the spooling area is private). |
| 140 |
*/ |
| 141 |
void |
| 142 |
HylaFAXServer::initServer(void) |
| 143 |
{ |
| 144 |
if (_debugSleep) // Allow a debuger time to attach |
| 145 |
{ |
| 146 |
logDebug("Sleeping %d for debugger", _debugSleep); |
| 147 |
sleep(_debugSleep); |
| 148 |
} |
| 149 |
|
| 150 |
end_login(); // reset user-related state |
| 151 |
|
| 152 |
/* |
| 153 |
* Default state: |
| 154 |
* o send long replies |
| 155 |
* o use GMT for time values |
| 156 |
* o auto-detect whether filesystem has BSD or SysV |
| 157 |
* semantics for setting the GID on created files |
| 158 |
*/ |
| 159 |
state = S_LREPLIES|S_USEGMT|S_CHECKGID; |
| 160 |
|
| 161 |
restart_point = 0; // data-transfer-related state |
| 162 |
mode = MODE_S; |
| 163 |
form = FORM_PS; |
| 164 |
type = TYPE_A; |
| 165 |
stru = STRU_F; |
| 166 |
|
| 167 |
initDefaultJob(); // reset default job state |
| 168 |
curJob = &defJob; |
| 169 |
|
| 170 |
if (data != -1) // data transfer-related state |
| 171 |
Sys::close(data), data = -1; |
| 172 |
if (pdata != -1) |
| 173 |
Sys::close(pdata), pdata = -1; |
| 174 |
|
| 175 |
if (trigSpec != "") { |
| 176 |
fxStr emsg; |
| 177 |
cancelTrigger(emsg); |
| 178 |
} |
| 179 |
|
| 180 |
// XXX FIFO state |
| 181 |
|
| 182 |
pushedToken = T_NIL; |
| 183 |
recvCC = 0; // no data present in buffer |
| 184 |
recvNext = 0; |
| 185 |
consecutiveBadCmds = 0; |
| 186 |
|
| 187 |
resetConfig(); |
| 188 |
readConfig(FAX_SYSCONF); |
| 189 |
readConfig(FAX_LIBDATA "/hfaxd.conf"); |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
tiffErrorHandler(const char* module, const char* fmt0, va_list ap) |
| 194 |
{ |
| 195 |
fxStr fmt = (module != 0) ? |
| 196 |
fxStr::format("%s: Warning, %s.", module, fmt0) |
| 197 |
: fxStr::format("Warning, %s.", fmt0); |
| 198 |
vlogError(fmt, ap); |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
tiffWarningHandler(const char* module, const char* fmt0, va_list ap) |
| 203 |
{ |
| 204 |
fxStr fmt = (module != 0) ? |
| 205 |
fxStr::format("%s: Warning, %s.", module, fmt0) |
| 206 |
: fxStr::format("Warning, %s.", fmt0); |
| 207 |
vlogWarning(fmt, ap); |
| 208 |
} |
| 209 |
|
| 210 |
void |
| 211 |
HylaFAXServer::open(void) |
| 212 |
{ |
| 213 |
initServer(); // complete state initialization |
| 214 |
|
| 215 |
fxStr emsg; |
| 216 |
if (!initClientFIFO(emsg)) { |
| 217 |
logInfo("connection refused (%s) from %s [%s]", |
| 218 |
(const char*) emsg, |
| 219 |
(const char*) remotehost, (const char*) remoteaddr); |
| 220 |
reply(420, "%s server cannot initialize: %s", |
| 221 |
(const char*) hostname, (const char*) emsg); |
| 222 |
dologout(-1); |
| 223 |
} |
| 224 |
ctrlFlags = fcntl(STDIN_FILENO, F_GETFL); // for parser |
| 225 |
if (isShutdown(true)) |
| 226 |
reply(220, "%s HylaFAX server shut down; available only for admin use.", |
| 227 |
(const char*) hostname); |
| 228 |
else |
| 229 |
reply(220, "%s server (%s) ready.", (const char*) hostname, version); |
| 230 |
|
| 231 |
if (TRACE(TIFF)) { |
| 232 |
TIFFSetErrorHandler(tiffErrorHandler); |
| 233 |
TIFFSetWarningHandler(tiffWarningHandler); |
| 234 |
} else { |
| 235 |
TIFFSetErrorHandler(NULL); |
| 236 |
TIFFSetWarningHandler(NULL); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void |
| 241 |
HylaFAXServer::close() |
| 242 |
{ |
| 243 |
dologout(-1); |
| 244 |
} |
| 245 |
|
| 246 |
void |
| 247 |
HylaFAXServer::setupPermissions(void) |
| 248 |
{ |
| 249 |
uid_t euid = geteuid(); |
| 250 |
struct passwd* pwd = getpwnam(FAX_USER); |
| 251 |
if (!pwd) |
| 252 |
logError("No fax user \"%s\" defined on your system!\n" |
| 253 |
"This software is not installed properly!", FAX_USER); |
| 254 |
else if (euid == 0) { |
| 255 |
if (initgroups(pwd->pw_name, pwd->pw_gid) != 0) |
| 256 |
logError("Can not setup permissions (supplementary groups): %m"); |
| 257 |
if (setegid(pwd->pw_gid) < 0) |
| 258 |
logError("Can not setup permissions (gid): %m"); |
| 259 |
else if (seteuid(pwd->pw_uid) < 0) |
| 260 |
logError("Can not setup permissions (uid): %m"); |
| 261 |
else { |
| 262 |
faxuid = pwd->pw_gid; |
| 263 |
endpwent(); |
| 264 |
return; |
| 265 |
} |
| 266 |
} else { |
| 267 |
faxuid = pwd->pw_gid; |
| 268 |
uid_t faxuid = pwd->pw_uid; |
| 269 |
setpwent(); |
| 270 |
pwd = getpwuid(euid); |
| 271 |
if (!pwd) |
| 272 |
logError("Can not figure out the identity of uid %u", euid); |
| 273 |
else if (pwd->pw_uid != faxuid) |
| 274 |
logError("Configuration error; " |
| 275 |
"the fax server must run as the fax user \"%s\".", FAX_USER); |
| 276 |
else { |
| 277 |
endpwent(); |
| 278 |
return; |
| 279 |
} |
| 280 |
} |
| 281 |
exit(-1); |
| 282 |
} |
| 283 |
|
| 284 |
/* |
| 285 |
* Close all open descriptors and unlink any |
| 286 |
* Dispatcher i/o handlers. |
| 287 |
*/ |
| 288 |
void |
| 289 |
HylaFAXServer::closeAllBut(int fd) |
| 290 |
{ |
| 291 |
Dispatcher& disp = Dispatcher::instance(); |
| 292 |
for (int f = Sys::getOpenMax()-1; f >= 0; f--) |
| 293 |
if (f != fd) { |
| 294 |
IOHandler* h = disp.handler(f, Dispatcher::ReadMask); |
| 295 |
if (h) |
| 296 |
disp.unlink(f); |
| 297 |
Sys::close(f); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/* |
| 302 |
* Unlink all dispatcher i/o handlers and close |
| 303 |
* their file descriptor. |
| 304 |
*/ |
| 305 |
void |
| 306 |
HylaFAXServer::closeAllDispatched() |
| 307 |
{ |
| 308 |
Dispatcher& disp = Dispatcher::instance(); |
| 309 |
for (int f = Sys::getOpenMax()-1; f >= 0; f--) |
| 310 |
{ |
| 311 |
IOHandler* h = disp.handler(f, Dispatcher::ReadMask); |
| 312 |
if (h) |
| 313 |
{ |
| 314 |
disp.unlink(f); |
| 315 |
Sys::close(f); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
const char* |
| 321 |
HylaFAXServer::fixPathname(const char* file) |
| 322 |
{ |
| 323 |
return (!IS(LOGGEDIN) && file[0] == '/' ? file+1 : file); |
| 324 |
} |
| 325 |
|
| 326 |
bool |
| 327 |
HylaFAXServer::readShutdownFile(void) |
| 328 |
{ |
| 329 |
bool ok = false; |
| 330 |
FILE* fd = fopen(fixPathname(shutdownFile), "r"); |
| 331 |
if (fd != NULL) { |
| 332 |
struct tm tm; |
| 333 |
int deny, disc; |
| 334 |
memset(&tm, 0, sizeof (tm)); |
| 335 |
int n = fscanf(fd, "%d %d %d %d %d %d %d\n", |
| 336 |
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, |
| 337 |
&deny, &disc); |
| 338 |
if (n == 7) { |
| 339 |
tm.tm_year -= 1900; |
| 340 |
tm.tm_isdst = -1; |
| 341 |
time_t shut = mktime(&tm); |
| 342 |
if (shut != (time_t) -1) { |
| 343 |
denyTime = shut - 3600 * (deny / 100) + 60 * (deny % 100); |
| 344 |
discTime = shut - 3600 * (disc / 100) + 60 * (disc % 100); |
| 345 |
|
| 346 |
shutdownMsg = ""; |
| 347 |
char buf[1024]; |
| 348 |
while (fgets(buf, sizeof (buf), fd)) |
| 349 |
shutdownMsg.append(buf); |
| 350 |
ok = true; |
| 351 |
} else |
| 352 |
logError("%s: Invalid shutdown time, mktime conversion failed;" |
| 353 |
"Year=%d Mon=%d Day=%d Hour=%d Min=%d" |
| 354 |
, (const char*) shutdownFile |
| 355 |
, tm.tm_year+1900 |
| 356 |
, tm.tm_mon |
| 357 |
, tm.tm_mday |
| 358 |
, tm.tm_hour |
| 359 |
, tm.tm_min |
| 360 |
); |
| 361 |
} else |
| 362 |
logError("%s: shutdown file format error", |
| 363 |
(const char*) shutdownFile); |
| 364 |
(void) fclose(fd); |
| 365 |
} else |
| 366 |
logError("%s: Cannot open shutdown file: %s", |
| 367 |
(const char*) shutdownFile, strerror(errno)); |
| 368 |
return (ok); |
| 369 |
} |
| 370 |
|
| 371 |
bool |
| 372 |
HylaFAXServer::isShutdown(bool quiet) |
| 373 |
{ |
| 374 |
struct stat sb; |
| 375 |
if (shutdownFile == "" || Sys::stat(fixPathname(shutdownFile), sb) < 0) |
| 376 |
return (false); |
| 377 |
if (sb.st_mtime != lastModTime) { |
| 378 |
if (!readShutdownFile()) |
| 379 |
return (false); |
| 380 |
lastModTime = sb.st_mtime; |
| 381 |
} |
| 382 |
time_t now = Sys::now(); |
| 383 |
if (!quiet) { // possibly send client shutdown msg |
| 384 |
time_t timeToDisconnect = discTime - now; |
| 385 |
time_t lastMsg = now-lastTime; |
| 386 |
bool sendShutDownMsg = |
| 387 |
(lastTime == 0) // first time |
| 388 |
|| (timeToDisconnect < 60) // <60 seconds, warn continuously |
| 389 |
// <15 minutes, warn ever 5 minutes |
| 390 |
|| (timeToDisconnect < 15*60 && lastMsg > 5*60) |
| 391 |
// <24 hours, warn every 30 minutes |
| 392 |
|| (timeToDisconnect < 24*60*60 && lastMsg > 30*60) |
| 393 |
// >24 hours, warn ever day |
| 394 |
|| (timeToDisconnect < 24*60*60 && lastMsg >= 24*60*60) |
| 395 |
; |
| 396 |
if (sendShutDownMsg) { |
| 397 |
autospout = shutdownMsg; // XXX append? |
| 398 |
lastTime = now; |
| 399 |
} |
| 400 |
} |
| 401 |
return (now > discTime); |
| 402 |
} |
| 403 |
|
| 404 |
void |
| 405 |
HylaFAXServer::statusCmd(void) |
| 406 |
{ |
| 407 |
lreply(211, "%s HylaFAX server status:", (const char*) hostname); |
| 408 |
printf(" %s\r\n", version); |
| 409 |
printf(" Connected to %s", (const char*) remotehost); |
| 410 |
if (!isdigit(remotehost[0])) |
| 411 |
printf(" (%s)", (const char*) remoteaddr); |
| 412 |
printf("\r\n"); |
| 413 |
if (IS(LOGGEDIN)) { |
| 414 |
printf(" Logged in as user %s (uid %u)%s\r\n" |
| 415 |
, (const char*) the_user |
| 416 |
, uid |
| 417 |
, IS(PRIVILEGED) ? " (with administrative privileges)" : "" |
| 418 |
); |
| 419 |
u_int len = strlen(cwd->pathname)-1; // strip trailing "/" |
| 420 |
printf(" \"%.*s\" is the current directory\r\n", |
| 421 |
len ? len : len+1, cwd->pathname); |
| 422 |
printf(" Current job: "); |
| 423 |
if (curJob->jobid == "default") |
| 424 |
printf("(default)\r\n"); |
| 425 |
else |
| 426 |
printf("jobid %s groupid %s\r\n", |
| 427 |
(const char*) curJob->jobid, (const char*) curJob->groupid); |
| 428 |
} else if (IS(WAITPASS)) |
| 429 |
printf(" Waiting for password\r\n"); |
| 430 |
else |
| 431 |
printf(" Waiting for user name\r\n"); |
| 432 |
printf(" Time values are handled in %s\r\n", |
| 433 |
IS(USEGMT) ? "GMT" : tzname[0]); |
| 434 |
printf(" Idle timeout set to %d seconds\r\n", idleTimeout); |
| 435 |
printf(" %s long replies\r\n", IS(LREPLIES) ? "Using" : "Not using"); |
| 436 |
if (discTime > 0) |
| 437 |
printf(" Server scheduled to be unavailable at %.24s\r\n", |
| 438 |
asctime(cvtTime(discTime))); |
| 439 |
else |
| 440 |
printf(" No server down time currently scheduled\r\n"); |
| 441 |
printf(" HylaFAX scheduler reached at %s (%sconnected)\r\n" |
| 442 |
, (const char*) faxqFIFOName |
| 443 |
, faxqFd == -1 ? "not " : "" |
| 444 |
); |
| 445 |
if (clientFd != -1) |
| 446 |
printf(" Server FIFO is /%s (%sopen)\r\n" |
| 447 |
, (const char*) clientFIFOName |
| 448 |
, clientFd == -1 ? "not " : "" |
| 449 |
); |
| 450 |
if (IS(WAITFIFO)) |
| 451 |
printf(" Waiting for response from HylaFAX scheduler\r\n"); |
| 452 |
FileCache::printStats(stdout); |
| 453 |
printTransferStatus(stdout); |
| 454 |
|
| 455 |
netStatus(stdout); // transport-dependent status |
| 456 |
reply(211, "End of status"); |
| 457 |
} |
| 458 |
|
| 459 |
int |
| 460 |
HylaFAXServer::inputReady(int fd) |
| 461 |
{ |
| 462 |
if (fd == STDIN_FILENO) |
| 463 |
return parse(); |
| 464 |
else if (fd == clientFd) |
| 465 |
return FIFOInput(fd); |
| 466 |
|
| 467 |
fatal("Input ready on unknown file descriptor %d", fd); |
| 468 |
return (0); // to shutup compilers |
| 469 |
} |
| 470 |
|
| 471 |
void |
| 472 |
HylaFAXServer::fatal(const char* fmt, ...) |
| 473 |
{ |
| 474 |
va_list ap; |
| 475 |
va_start(ap, fmt); |
| 476 |
vreply(451, fxStr::format("Error in server: %s", fmt), ap); |
| 477 |
va_end(ap); |
| 478 |
reply(221, "Closing connection due to server error."); |
| 479 |
dologout(0); |
| 480 |
/*NOTREACHED*/ |
| 481 |
} |
| 482 |
|
| 483 |
void |
| 484 |
HylaFAXServer::reply(int code, const char* fmt, ...) |
| 485 |
{ |
| 486 |
va_list ap; |
| 487 |
va_start(ap, fmt); |
| 488 |
vreply(code, fmt, ap); |
| 489 |
va_end(ap); |
| 490 |
} |
| 491 |
|
| 492 |
void |
| 493 |
HylaFAXServer::vreply(int code, const char* fmt, va_list ap) |
| 494 |
{ |
| 495 |
if (autospout != "") { |
| 496 |
printf("%d-", code); |
| 497 |
int i = 0; |
| 498 |
while (autospout[i] != '\0') { |
| 499 |
if (autospout[i] == '\n') { |
| 500 |
fputs("\r\n", stdout); |
| 501 |
if (autospout[++i]) |
| 502 |
printf("%d-", code); |
| 503 |
} else |
| 504 |
putchar(autospout[i++]); |
| 505 |
} |
| 506 |
if (autospout[--i] != '\n') |
| 507 |
printf("\r\n"); |
| 508 |
autospout = ""; |
| 509 |
} |
| 510 |
fxStackBuffer buf; |
| 511 |
buf.vput(fmt, ap); |
| 512 |
fprintf(stdout, "%d %.*s\r\n", code, buf.getLength(), (const char*) buf); |
| 513 |
fflush(stdout); |
| 514 |
|
| 515 |
if (TRACE(PROTOCOL)) |
| 516 |
logDebug("<--- %d %.*s", code, buf.getLength(), (const char*) buf); |
| 517 |
} |
| 518 |
|
| 519 |
void |
| 520 |
HylaFAXServer::lreply(int code, const char* fmt, ...) |
| 521 |
{ |
| 522 |
va_list ap; |
| 523 |
va_start(ap, fmt); |
| 524 |
vlreply(code, fmt, ap); |
| 525 |
va_end(ap); |
| 526 |
} |
| 527 |
|
| 528 |
void |
| 529 |
HylaFAXServer::vlreply(int code, const char* fmt, va_list ap) |
| 530 |
{ |
| 531 |
if (IS(LREPLIES)) { |
| 532 |
fxStackBuffer buf; |
| 533 |
buf.vput(fmt, ap); |
| 534 |
printf("%d-%.*s\r\n", code, buf.getLength(), (const char*) buf); |
| 535 |
fflush(stdout); |
| 536 |
|
| 537 |
if (TRACE(PROTOCOL)) |
| 538 |
logDebug("<--- %d-%.*s", code, buf.getLength(), (const char*) buf); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/* Format and send reply containing system error number. */ |
| 543 |
void |
| 544 |
HylaFAXServer::perror_reply(int code, const char* string, int errnum) |
| 545 |
{ |
| 546 |
reply(code, "%s: %s.", string, strerror(errnum)); |
| 547 |
} |
| 548 |
|
| 549 |
void |
| 550 |
HylaFAXServer::ack(int code, const char* s) |
| 551 |
{ |
| 552 |
reply(code, "%s command successful.", s); |
| 553 |
} |
| 554 |
|
| 555 |
struct tm* |
| 556 |
HylaFAXServer::cvtTime(const time_t& t) const |
| 557 |
{ |
| 558 |
return IS(USEGMT) ? gmtime(&t) : localtime(&t); |
| 559 |
} |
| 560 |
|
| 561 |
u_int HylaFAXServer::getJobNumber(fxStr& emsg) |
| 562 |
{ return (Sequence::getNext(FAX_SENDDIR "/" FAX_SEQF, emsg)); } |
| 563 |
u_int HylaFAXServer::getDocumentNumber(fxStr& emsg) |
| 564 |
{ return (Sequence::getNext(FAX_DOCDIR "/" FAX_SEQF, emsg)); } |
| 565 |
|
| 566 |
void |
| 567 |
HylaFAXServer::sanitize(fxStr& s) |
| 568 |
{ |
| 569 |
for (u_int i = 0; i < s.length(); i++) |
| 570 |
if (!isascii(s[i]) || !isprint(s[i])) |
| 571 |
s[i] = '?'; |
| 572 |
} |
| 573 |
|
| 574 |
/* |
| 575 |
* Convert modem name from canonical format back |
| 576 |
* to a pathname by replacing '_'s with '/'s. |
| 577 |
*/ |
| 578 |
void |
| 579 |
HylaFAXServer::canonModem(fxStr& s) |
| 580 |
{ |
| 581 |
u_int l = 0; |
| 582 |
while ((l = s.next(l, '_')) < s.length()) |
| 583 |
s[l] = '/'; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* Convert pathname to a device ID by |
| 588 |
* replacing '/'s with '_'s. |
| 589 |
*/ |
| 590 |
void |
| 591 |
HylaFAXServer::canonDevID(fxStr& s) |
| 592 |
{ |
| 593 |
u_int l = 0; |
| 594 |
while ((l = s.next(l, '/')) < s.length()) |
| 595 |
s[l] = '_'; |
| 596 |
} |
| 597 |
|
| 598 |
/* |
| 599 |
* Configuration support. |
| 600 |
*/ |
| 601 |
|
| 602 |
void |
| 603 |
HylaFAXServer::resetConfig() |
| 604 |
{ |
| 605 |
FaxConfig::resetConfig(); |
| 606 |
setupConfig(); |
| 607 |
} |
| 608 |
|
| 609 |
#define N(a) (sizeof (a) / sizeof (a[0])) |
| 610 |
|
| 611 |
/* |
| 612 |
* logFacility *must* be tie first entry - see it's handling |
| 613 |
* below in setConfigItem() |
| 614 |
*/ |
| 615 |
HylaFAXServer::stringtag HylaFAXServer::strings[] = { |
| 616 |
{ "logfacility", &HylaFAXServer::logFacility, LOG_FAX }, |
| 617 |
{ "faxcontact", &HylaFAXServer::faxContact, "FaxMaster" }, |
| 618 |
{ "useraccessfile", &HylaFAXServer::userAccessFile, "/" FAX_PERMFILE }, |
| 619 |
{ "shutdownfile", &HylaFAXServer::shutdownFile, "/etc/shutdown" }, |
| 620 |
{ "xferfaxlogfile", &HylaFAXServer::xferfaxLogFile, "/etc/clientlog" }, |
| 621 |
{ "jobfmt", &HylaFAXServer::jobFormat, |
| 622 |
"%-4j %3i %1a %6.6o %-12.12e %5P %5D %7z %.25s" }, |
| 623 |
{ "jobsortfmt", &HylaFAXServer::jobSortFormat }, |
| 624 |
{ "rcvfmt", &HylaFAXServer::recvFormat, |
| 625 |
"%-7m %4p%1z %-8.8o %14.14s %7t %f" }, |
| 626 |
{ "rcvsortfmt", &HylaFAXServer::recvSortFormat}, |
| 627 |
{ "modemfmt", &HylaFAXServer::modemFormat, "Modem %m (%n): %s" }, |
| 628 |
{ "modemsortfmt", &HylaFAXServer::modemSortFormat}, |
| 629 |
{ "filefmt", &HylaFAXServer::fileFormat, |
| 630 |
"%-7p %3l %8o %8s %-12.12m %.48f" }, |
| 631 |
{ "filesortfmt", &HylaFAXServer::fileSortFormat}, |
| 632 |
{ "faxqfifoname", &HylaFAXServer::faxqFIFOName, "/" FAX_FIFO }, |
| 633 |
{ "systemtype", &HylaFAXServer::systemType, |
| 634 |
"UNIX Type: L8 Version: SVR4" }, |
| 635 |
{ "admingroup", &HylaFAXServer::admingroup }, |
| 636 |
}; |
| 637 |
|
| 638 |
/* |
| 639 |
* jobprotection needs to be first as well, see setConfigItem() below |
| 640 |
*/ |
| 641 |
HylaFAXServer::numbertag HylaFAXServer::numbers[] = { |
| 642 |
{ "jobprotection", &HylaFAXServer::jobProtection, 0644 }, |
| 643 |
{ "servertracing", &HylaFAXServer::tracingLevel, TRACE_SERVER }, |
| 644 |
{ "idletimeout", &HylaFAXServer::idleTimeout, 900 }, |
| 645 |
{ "maxidletimeout", &HylaFAXServer::maxIdleTimeout, 7200 }, |
| 646 |
{ "locktimeout", &HylaFAXServer::lockTimeout, 0 }, |
| 647 |
{ "maxlocktimeout", &HylaFAXServer::maxLockTimeout, 60 }, |
| 648 |
{ "maxloginattempts", &HylaFAXServer::maxLoginAttempts, 5 }, |
| 649 |
{ "maxadminattempts", &HylaFAXServer::maxAdminAttempts, 5 }, |
| 650 |
{ "maxconsecutivebadcmds",&HylaFAXServer::maxConsecutiveBadCmds,10 }, |
| 651 |
}; |
| 652 |
HylaFAXServer::booltag HylaFAXServer::booleans[] = { |
| 653 |
{ "allowsorting", &HylaFAXServer::allowSorting, true }, |
| 654 |
{ "publicjobq", &HylaFAXServer::publicJobQ, false }, |
| 655 |
{ "publicrecvq", &HylaFAXServer::publicRecvQ, false }, |
| 656 |
}; |
| 657 |
|
| 658 |
void |
| 659 |
HylaFAXServer::setupConfig() |
| 660 |
{ |
| 661 |
int i; |
| 662 |
|
| 663 |
for (i = N(strings)-1; i >= 0; i--) |
| 664 |
(*this).*strings[i].p = (strings[i].def ? strings[i].def : ""); |
| 665 |
for (i = N(numbers)-1; i >= 0; i--) |
| 666 |
(*this).*numbers[i].p = numbers[i].def; |
| 667 |
for (i = N(booleans)-1; i >= 0; i--) |
| 668 |
(*this).*booleans[i].p = booleans[i].def; |
| 669 |
faxContact.append("@" | hostname); |
| 670 |
admingroup = "faxadmin"; |
| 671 |
} |
| 672 |
|
| 673 |
void |
| 674 |
HylaFAXServer::configError(const char* fmt, ...) |
| 675 |
{ |
| 676 |
va_list ap; |
| 677 |
va_start(ap, fmt); |
| 678 |
vlogError(fmt, ap); |
| 679 |
va_end(ap); |
| 680 |
} |
| 681 |
|
| 682 |
void |
| 683 |
HylaFAXServer::configTrace(const char* fmt, ...) |
| 684 |
{ |
| 685 |
if (TRACE(CONFIG)) { |
| 686 |
va_list ap; |
| 687 |
va_start(ap, fmt); |
| 688 |
vlogError(fmt, ap); |
| 689 |
va_end(ap); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
bool |
| 694 |
HylaFAXServer::setConfigItem(const char* tag, const char* value) |
| 695 |
{ |
| 696 |
u_int ix; |
| 697 |
if (findTag(tag, (const tags*) strings, N(strings), ix)) { |
| 698 |
(*this).*strings[ix].p = value; |
| 699 |
switch (ix) { |
| 700 |
case 0: setLogFacility(logFacility); break; |
| 701 |
} |
| 702 |
} else if (findTag(tag, (const tags*) numbers, N(numbers), ix)) { |
| 703 |
(*this).*numbers[ix].p = getNumber(value); |
| 704 |
switch (ix) { |
| 705 |
case 0: if ( (jobProtection & 0600) != 0600) |
| 706 |
{ |
| 707 |
logError("JobProtection value must include 0600, forcing"); |
| 708 |
jobProtection |= 0600; |
| 709 |
} |
| 710 |
} |
| 711 |
} else if (findTag(tag, (const tags*) booleans, N(booleans), ix)) { |
| 712 |
(*this).*booleans[ix].p = getBoolean(value); |
| 713 |
} else |
| 714 |
return (false); |
| 715 |
return (true); |
| 716 |
} |
| 717 |
#undef N |
| 718 |
|
| 719 |
|
| 720 |
KeyString::KeyString (void) |
| 721 |
{ |
| 722 |
} |
| 723 |
|
| 724 |
KeyString::KeyString (const fxStr& k, const fxStr& data) |
| 725 |
: fxStr(data) |
| 726 |
, key(k) |
| 727 |
{ |
| 728 |
|
| 729 |
} |
| 730 |
|
| 731 |
int KeyString::compare (const KeyString* that) const |
| 732 |
{ |
| 733 |
return key.compare(&that->key); |
| 734 |
} |
| 735 |
|
| 736 |
fxIMPLEMENT_ObjArray(KeyStringArray, KeyString) |