| 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 "InetFaxServer.h" |
| 28 |
#if CONFIG_UNIXTRANSPORT |
| 29 |
#include "UnixFaxServer.h" |
| 30 |
#endif |
| 31 |
#ifdef SNPP_SUPPORT |
| 32 |
#include "SNPPServer.h" |
| 33 |
#endif |
| 34 |
#ifdef HTTP_SUPPORT |
| 35 |
#include "HTTPServer.h" |
| 36 |
#endif |
| 37 |
#include "Dispatcher.h" |
| 38 |
#include "Array.h" |
| 39 |
#include "Sys.h" |
| 40 |
#include "Socket.h" |
| 41 |
#include "config.h" |
| 42 |
|
| 43 |
static jmp_buf problem; |
| 44 |
|
| 45 |
static void |
| 46 |
sigCleanup(int sig) |
| 47 |
{ |
| 48 |
logError("CAUGHT SIGNAL %d", sig); |
| 49 |
longjmp(problem, 1); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
fatal(const char* fmt ...) |
| 54 |
{ |
| 55 |
va_list ap; |
| 56 |
va_start(ap, fmt); |
| 57 |
vlogError(fmt, ap); |
| 58 |
va_end(ap); |
| 59 |
exit(-1); |
| 60 |
} |
| 61 |
|
| 62 |
#define PATH_DEVNULL "dev/null" |
| 63 |
#define PATH_DEVTCP "dev/tcp" |
| 64 |
#define PATH_NETCONFIG "etc/netconfig" |
| 65 |
#define PATH_DEVSOCKSYS "dev/socksys" |
| 66 |
|
| 67 |
/* |
| 68 |
* Verify and possibly setup the chroot'd filesystem as |
| 69 |
* required by the system. Specifically, create a private |
| 70 |
* copy of /dev/null and any networking-related files |
| 71 |
* required by SVR4-based TCP/IP support. We do this work |
| 72 |
* because the process runs chroot'd to the top of the |
| 73 |
* spooling area so normal files in the root filesystem |
| 74 |
* are inaccessible. |
| 75 |
* |
| 76 |
* NB: This work could be done once in a setup script but |
| 77 |
* creating duplicates of character special device files |
| 78 |
* is not simple from the shell. |
| 79 |
*/ |
| 80 |
static void |
| 81 |
CheckSpoolingSetup(void) |
| 82 |
{ |
| 83 |
struct stat sb; |
| 84 |
uid_t ouid = geteuid(); |
| 85 |
(void) seteuid(0); |
| 86 |
mode_t omask = umask(0); |
| 87 |
/* |
| 88 |
* Craft a private /dev/null in the chroot'd filesystem |
| 89 |
* for use by syslog because some syslogs require this |
| 90 |
* to function correctly. |
| 91 |
*/ |
| 92 |
if (!Sys::isCharSpecialFile(PATH_DEVNULL)) { |
| 93 |
if (!Sys::isCharSpecialFile("/" PATH_DEVNULL, sb)) |
| 94 |
fatal("stat(%s): %s", "/" PATH_DEVNULL, strerror(errno)); |
| 95 |
if (mknod(PATH_DEVNULL, sb.st_mode, sb.st_rdev) < 0) |
| 96 |
fatal("Could not create %s: %s", PATH_DEVNULL, strerror(errno)); |
| 97 |
} |
| 98 |
/* |
| 99 |
* If the system appears to support SVR4-style TCP/IP |
| 100 |
* support then craft a private copy of the necessary |
| 101 |
* files so that socket-related calls can be made after |
| 102 |
* chroot'ing to the top of the spooling area. |
| 103 |
* |
| 104 |
* NB: It is assumed that the dev subdirectory is already |
| 105 |
* present (make install or similar should create it). |
| 106 |
*/ |
| 107 |
if (Sys::isCharSpecialFile("/" PATH_DEVTCP, sb) && |
| 108 |
!Sys::isCharSpecialFile(PATH_DEVTCP)) { |
| 109 |
if (mknod(PATH_DEVTCP, sb.st_mode, sb.st_rdev) < 0) |
| 110 |
fatal("Could not create %s: %s", PATH_DEVTCP, strerror(errno)); |
| 111 |
/* |
| 112 |
* Copy /etc/netconfig if not already present. |
| 113 |
*/ |
| 114 |
if (Sys::stat(PATH_NETCONFIG, sb) < 0) { |
| 115 |
int src = Sys::open("/" PATH_NETCONFIG, O_RDONLY); |
| 116 |
if (src >= 0) { |
| 117 |
int dst = Sys::open(PATH_NETCONFIG, O_WRONLY|O_CREAT, 0444); |
| 118 |
if (dst < 0) |
| 119 |
fatal("creat(%s): %s", PATH_NETCONFIG, strerror(errno)); |
| 120 |
char buf[4096]; |
| 121 |
int cc; |
| 122 |
while ((cc = read(src, buf, sizeof (buf))) > 0) |
| 123 |
if (write(dst, buf, cc) < 0) |
| 124 |
fatal("write(%s): %s", PATH_NETCONFIG, strerror(errno)); |
| 125 |
close(dst); |
| 126 |
close(src); |
| 127 |
} else |
| 128 |
logWarning("%s: Cannot open: %s", |
| 129 |
"/" PATH_NETCONFIG, strerror(errno)); |
| 130 |
} |
| 131 |
} |
| 132 |
/* |
| 133 |
* SCO OS 5 apparently needs a /dev/socksys to implement |
| 134 |
* setsockopt calls (sigh); create one in the chroot'd |
| 135 |
* area if one exists in the root filesystem. |
| 136 |
*/ |
| 137 |
if (Sys::isCharSpecialFile("/" PATH_DEVSOCKSYS, sb) && |
| 138 |
!Sys::isCharSpecialFile(PATH_DEVSOCKSYS)) |
| 139 |
if (mknod(PATH_DEVSOCKSYS, sb.st_mode, sb.st_rdev) < 0) |
| 140 |
fatal("Could not create %s: %s", PATH_DEVSOCKSYS, strerror(errno)); |
| 141 |
(void) umask(omask); |
| 142 |
seteuid(ouid); |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Break the association with the controlling tty. |
| 147 |
* Note that we do not close all the open file descriptors |
| 148 |
* because many systems cache open descriptors within libraries |
| 149 |
* for performance reasons and do not react well when you close |
| 150 |
* them w/o telling them about it (and some don't react well |
| 151 |
* even when you *DO* tell them). Since we know we're called |
| 152 |
* very early on from main in all our apps we just assume that |
| 153 |
* we only need to remove the stdin+stdout+stderr before forking |
| 154 |
* and starting a new session. |
| 155 |
*/ |
| 156 |
static void |
| 157 |
detachFromTTY(void) |
| 158 |
{ |
| 159 |
int fd = Sys::open(_PATH_DEVNULL, O_RDWR); |
| 160 |
if (fd == -1) |
| 161 |
fatal("Could not open null device file %s.", _PATH_DEVNULL); |
| 162 |
dup2(fd, STDIN_FILENO); |
| 163 |
dup2(fd, STDOUT_FILENO); |
| 164 |
dup2(fd, STDERR_FILENO); |
| 165 |
Sys::close(fd); |
| 166 |
switch (fork()) { |
| 167 |
case 0: break; // child, continue |
| 168 |
case -1: _exit(1); // error |
| 169 |
default: _exit(0); // parent, terminate |
| 170 |
} |
| 171 |
(void) setsid(); |
| 172 |
} |
| 173 |
|
| 174 |
static void |
| 175 |
usage(const char* appName) |
| 176 |
{ |
| 177 |
fatal("usage: %s [-d] [-o port] [-O] [-h port] [-H] [-l bindaddress] [-i port] [-I] [-s port] [-S] [-u socket] [-q queue-directory]", |
| 178 |
appName); |
| 179 |
} |
| 180 |
|
| 181 |
fxDECLARE_PtrArray(IOHandlerArray, IOHandler*) |
| 182 |
fxIMPLEMENT_PtrArray(IOHandlerArray, IOHandler*) |
| 183 |
static IOHandlerArray handlers; |
| 184 |
|
| 185 |
static void |
| 186 |
newInetServer(void) |
| 187 |
{ |
| 188 |
InetFaxServer* server = new InetFaxServer; |
| 189 |
server->open(); |
| 190 |
handlers.append(server); |
| 191 |
} |
| 192 |
|
| 193 |
int |
| 194 |
main(int argc, char** argv, char** envp) |
| 195 |
{ |
| 196 |
const char *bindaddress = NULL; |
| 197 |
|
| 198 |
HylaFAXServer::setLogFacility(LOG_FAX); |
| 199 |
HylaFAXServer::setupLogging("HylaFAX"); |
| 200 |
HylaFAXServer::setupPermissions(); |
| 201 |
|
| 202 |
fxStr appName = argv[0]; |
| 203 |
u_int l = appName.length(); |
| 204 |
appName = appName.tokenR(l, '/'); |
| 205 |
|
| 206 |
optind = 1; |
| 207 |
opterr = 0; |
| 208 |
int c; |
| 209 |
const char* opts = "dD:Hh:Ii:Oo:q:Ss:u:l:"; |
| 210 |
/* |
| 211 |
* Deduce the spooling directory and whether or not to |
| 212 |
* detach the process from the controlling tty. The |
| 213 |
* latter is complicated by the fact that we run both |
| 214 |
* as a master-server process and as a subprocess to |
| 215 |
* inetd. If we are to act as a master-server then we |
| 216 |
* detach by default. If we are invoked by inetd then |
| 217 |
* do not detach. If no arguments are specified then |
| 218 |
* we imply a -I option (the new fax protocol) and do |
| 219 |
* not want to detach. The logic is a touch convoluted |
| 220 |
* to do this correctly and is probably not worth the |
| 221 |
* effort (except to reduce configuration errors). |
| 222 |
*/ |
| 223 |
fxStr queueDir(FAX_SPOOLDIR); |
| 224 |
int detach = -1; // unknown state |
| 225 |
while ((c = Sys::getopt(argc, argv, opts)) != -1) |
| 226 |
switch (c) { |
| 227 |
case 'h': case 'i': case 'o': case 's': case 'u': |
| 228 |
if (detach == -1) // detach unless explicitly specified |
| 229 |
detach = true; |
| 230 |
break; |
| 231 |
case 'H': case 'I': case 'O': case 'S': |
| 232 |
if (detach == -1) // don't detach when invoked by inetd |
| 233 |
detach = false; |
| 234 |
break; |
| 235 |
case 'd': detach = false; break; |
| 236 |
case 'D': HylaFAXServer::_debugSleep = atoi(optarg); break; |
| 237 |
case 'q': queueDir = optarg; break; |
| 238 |
case '?': usage(appName); |
| 239 |
} |
| 240 |
if (detach == -1) // no protocol options means -I |
| 241 |
detach = false; |
| 242 |
if (Sys::chdir(queueDir) < 0) |
| 243 |
fatal("Can not change directory to %s", (const char*)queueDir); |
| 244 |
CheckSpoolingSetup(); |
| 245 |
if (detach) |
| 246 |
detachFromTTY(); |
| 247 |
|
| 248 |
/* |
| 249 |
* Rescan the arguments and create the appropriate |
| 250 |
* protocol support threads. We do this after the |
| 251 |
* above work for reasons I can no longer remember. |
| 252 |
*/ |
| 253 |
optind = 1; |
| 254 |
opterr = 0; |
| 255 |
while ((c = Sys::getopt(argc, argv, opts)) != -1) |
| 256 |
switch (c) { |
| 257 |
case 'o': case 'O': |
| 258 |
fatal("No support for old protocol"); |
| 259 |
/*NOTREACHED*/ |
| 260 |
#ifdef HTTP_SUPPORT |
| 261 |
case 'h': handlers.append(new HTTPSuperServer(optarg)); break; |
| 262 |
case 'H': |
| 263 |
{ HTTPFaxServer* server = new HTTPFaxServer; |
| 264 |
server->open(); |
| 265 |
handlers.append(server); |
| 266 |
} |
| 267 |
break; |
| 268 |
#else |
| 269 |
case 'h': case 'H': |
| 270 |
fatal("No HTTP support"); |
| 271 |
/*NOTREACHED*/ |
| 272 |
#endif |
| 273 |
case 'l': |
| 274 |
bindaddress = strdup(optarg); break; |
| 275 |
case 'i': { |
| 276 |
InetSuperServer* iss; |
| 277 |
iss = new InetSuperServer(optarg); |
| 278 |
handlers.append(iss); |
| 279 |
if ((iss!=NULL) && (bindaddress!=NULL)) |
| 280 |
iss->setBindAddress(bindaddress); |
| 281 |
} |
| 282 |
break; |
| 283 |
case 'I': newInetServer(); break; |
| 284 |
#ifdef SNPP_SUPPORT |
| 285 |
case 's': handlers.append(new SNPPSuperServer(optarg)); break; |
| 286 |
case 'S': |
| 287 |
{ SNPPServer* server = new SNPPServer; |
| 288 |
server->open(); |
| 289 |
handlers.append(server); |
| 290 |
} |
| 291 |
break; |
| 292 |
#else |
| 293 |
case 's': case 'S': |
| 294 |
fatal("No SNPP support"); |
| 295 |
/*NOTREACHED*/ |
| 296 |
#endif |
| 297 |
#if CONFIG_UNIXTRANSPORT |
| 298 |
case 'u': handlers.append(new UnixSuperServer(optarg)); break; |
| 299 |
#else |
| 300 |
case 'u': |
| 301 |
fatal("No support for Unix domain sockets"); |
| 302 |
/*NOTREACHED*/ |
| 303 |
#endif |
| 304 |
} |
| 305 |
if (handlers.length() == 0) |
| 306 |
newInetServer(); |
| 307 |
|
| 308 |
/* |
| 309 |
* Startup protocol processing. |
| 310 |
*/ |
| 311 |
if (setjmp(problem) == 0) { |
| 312 |
signal(SIGHUP, fxSIGHANDLER(sigCleanup)); |
| 313 |
signal(SIGINT, fxSIGHANDLER(sigCleanup)); |
| 314 |
signal(SIGQUIT, fxSIGHANDLER(sigCleanup)); |
| 315 |
signal(SIGILL, fxSIGHANDLER(sigCleanup)); |
| 316 |
signal(SIGKILL, fxSIGHANDLER(sigCleanup)); |
| 317 |
signal(SIGBUS, fxSIGHANDLER(sigCleanup)); |
| 318 |
signal(SIGSEGV, fxSIGHANDLER(sigCleanup)); |
| 319 |
|
| 320 |
for (;;) |
| 321 |
Dispatcher::instance().dispatch(); |
| 322 |
} |
| 323 |
/* |
| 324 |
* We explicitly destroy protocol threads so that any |
| 325 |
* resources are reclaimed (e.g. Unix domain sockets). |
| 326 |
*/ |
| 327 |
for (u_int i = 0, n = handlers.length(); i < n; i++) |
| 328 |
delete handlers[i]; |
| 329 |
return 0; |
| 330 |
} |