1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*
* ffproxy (c) 2002-2005 Niklas Olmes <niklas@noxa.de>
* http://faith.eu.org
*
* $Id: main.c,v 2.2 2005/01/05 15:12:49 niklas Exp niklas $
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA.
*/
#include "configure.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cfg.h"
#include "print.h"
#include "socket.h"
#include "db.h"
#include "dns.h"
#include "signals.h"
static void usage(void);
static void drop_privileges(void);
static void write_pid(char*, pid_t);
static const char version[] = "1.6";
static const char rcsid[] = "$Id: main.c,v 2.2 2005/01/05 15:12:49 niklas Exp niklas $";
char loop_header[100];
struct cfg config;
int
main(int argc, char *argv[])
{
int c, nowarn;
char *prgname;
pid_t pid;
prgname = argv[0];
nowarn = 0;
(void) memset(&config, 0, sizeof(config));
*config.ipv4 = '\0';
*config.ipv6 = '\0';
config.port = 0;
config.daemon = 0;
config.childs = 10;
config.ccount = 0;
config.backlog = 4;
config.uid = 0L;
config.gid = 0L;
*config.chroot = '\0';
(void) strncpy(config.dbdir, DATADIR, sizeof(config.dbdir) - 1);
config.dbdir[sizeof(config.dbdir) - 1] = '\0';
(void) strncpy(config.file, CFGFILE, sizeof(config.file) - 1);
config.file[sizeof(config.file) - 1] = '\0';
*config.proxyhost = '\0';
config.proxyport = 0;
config.syslog = 1;
config.logrequests = 0;
config.use_ipv6 = 1;
config.aux_proxy_ipv6 = 1;
config.bind_ipv6 = 1;
config.bind_ipv4 = 1;
config.accel = 0;
config.accelusrhost = 1;
*config.accelhost = '\0';
config.accelport = 80;
config.kalive = 1;
config.unr_con = 0;
config.to_con = 5;
config.first = 1;
(void) strncpy(config.piddir, "/var/run", sizeof(config.piddir) - 1);
while ((c = getopt(argc, argv, "vdbBc:C:p:x:X:l:u:g:r:D:F:f:s4a:A:n:h")) != -1) {
switch (c) {
case 'v':
(void) printf("ffproxy version %s, %s\n",
version, rcsid);
exit(0);
break;
case 'd':
config.daemon = 1;
break;
case 'b':
config.bind_ipv4 = 0;
break;
case 'B':
config.bind_ipv6 = 0;
break;
case 'c':
(void) strncpy(config.ipv4, optarg, sizeof(config.ipv4) - 1);
config.ipv4[sizeof(config.ipv4) - 1] = '\0';
break;
case 'C':
(void) strncpy(config.ipv6, optarg, sizeof(config.ipv6) - 1);
config.ipv6[sizeof(config.ipv6) - 1] = '\0';
break;
case 'p':
config.port = atoi(optarg);
if (config.port > MAX_PORTS || !config.port) {
(void) fprintf(stderr, "Invalid port number (-p %s)\n", optarg);
exit(1);
}
break;
case 'x':
if (strlen(optarg) > sizeof(config.proxyhost) - 1 ) {
(void) fprintf(stderr, "Proxy host name too long\n");
exit(1);
}
(void) strncpy(config.proxyhost, optarg, sizeof(config.proxyhost) - 1);
config.proxyhost[sizeof(config.proxyhost) - 1] = '\0';
break;
case 'X':
config.proxyport = atoi(optarg);
if (config.proxyport > MAX_PORTS || !config.proxyport) {
(void) fprintf(stderr, "Invalid port number (-X %s)\n", optarg);
exit(1);
}
break;
case 'l':
config.childs = atoi(optarg);
if (!config.childs || config.childs > MAX_CHILDS) {
(void) fprintf(stderr, "Invalid limit of child processes (-l %s)\n", optarg);
exit(1);
}
break;
case 'u':
if (!(config.uid = atoi(optarg))) {
struct passwd *pwd;
if ((pwd = getpwnam(optarg)))
config.uid = (unsigned long) pwd->pw_uid;
else {
(void) fprintf(stderr, "UID %s not found\n", optarg);
exit(1);
}
}
break;
case 'g':
if (!(config.gid = atoi(optarg))) {
struct group *grp;
if ((grp = getgrnam(optarg)))
config.gid = (unsigned long) grp->gr_gid;
else {
(void) fprintf(stderr, "GID %s not found\n", optarg);
exit(1);
}
}
break;
case 'r':
if (strlen(optarg) > sizeof(config.chroot) - 1 ) {
(void) fprintf(stderr, "chroot directory too long\n");
exit(1);
}
(void) strncpy(config.chroot, optarg, sizeof(config.chroot) - 1);
config.chroot[sizeof(config.chroot) - 1] = '\0';
break;
case 'D':
if (strlen(optarg) > sizeof(config.dbdir) - 1 ) {
(void) fprintf(stderr, "dbdir directory too long\n");
exit(1);
}
(void) strncpy(config.dbdir, optarg, sizeof(config.dbdir) - 1);
config.dbdir[sizeof(config.dbdir) - 1] = '\0';
break;
case 'F':
nowarn = 1;
case 'f':
if (strlen(optarg) > sizeof(config.file) - 1 ) {
(void) fprintf(stderr, "config file name too long\n");
exit(1);
}
(void) strncpy(config.file, optarg, sizeof(config.file) - 1);
config.file[sizeof(config.file) - 1] = '\0';
if (*config.file && !nowarn && strcmp(config.file, "/dev/null") != 0)
(void) fprintf(stdout, "Using config file (%s).\nPlease note that due to design, config file overwrites command line options.\nUse -F instead of -f to omit this warning message.\n", config.file);
break;
case 's':
config.syslog = 0;
break;
case '4':
config.use_ipv6 = 0;
break;
case 'a':
(void) strncpy(config.accelhost, optarg, sizeof(config.accelhost) - 1);
config.accelhost[sizeof(config.accelhost) - 1] = '\0';
break;
case 'A':
config.accelport = atoi(optarg);
break;
case 'h':
usage();
/* NOTREACHED */
break;
case 'n':
if (strlen(optarg) > sizeof(config.piddir) - 1 ) {
(void) fprintf(stderr, "piddir directory too long\n");
exit(1);
}
(void) strncpy(config.piddir, optarg, sizeof(config.piddir) - 1);
config.piddir[sizeof(config.piddir) - 1] = '\0';
break;
default:
(void) fprintf(stderr, "Error, type `%s -h' for help on usage\n", prgname);
exit(1);
break;
}
}
argc -= optind;
argv += optind;
if (*argv) {
(void) fprintf(stderr, "Unknown argument left (%s)\nType `%s -h' for usage\n", *argv, prgname);
exit(1);
}
setup_log_master();
info("started, initializing");
load_databases();
(void) resolve("localhost");
pid = getpid();
if (config.daemon) {
pid = fork();
if (pid == -1)
fatal("daemonize() failed");
if (pid > 0) {
write_pid(config.piddir, pid);
_exit (0);
}
else {
(void) close(0);
(void) close(1);
(void) close(2);
}
}
else
write_pid(config.piddir, pid);
drop_privileges();
(void) snprintf(loop_header, sizeof(loop_header), "X-Loop-%d-%d: true", getpid(), (int) time(NULL));
init_sighandlers();
open_socket();
/* NOTREACHED */
return 0;
}
static void
usage(void)
{
(void) fprintf(stderr, "ffproxy %s -- (c) 2002-2005 Niklas Olmes <niklas@noxa.de>\n", version);
(void) fprintf(stderr, " GNU GPL. Website: http://faith.eu.org/programs.html\n");
(void) fprintf(stderr,
"usage: ffproxy [-vhds4bB] [-c host|ip] [-C host|ip] [-p port]\n"
" [-x host|ip -X port] [-l max] [-u uid|usr -g gid|grp] [-r dir]\n"
" [-D dir] [-f file] [-a host|ip] [-A port]\n"
" -v print version number -h usage (this screen)\n"
" -d become daemon -s silent. don't log to syslog.\n"
" -4 use IPv4 only. don't try contacting via IPv6.\n"
" -b do *not* bind to IPv4\n"
" -B do *not* bind to IPv6\n"
" -c host|ip bind to IPv4 address (default is any)\n");
(void) fprintf(stderr,
" -C host|ip bind to IPv6 address (default is any)\n"
" -p port bind to port\n"
" -x host|ip auxiliary forward proxy\n"
" -X port auxiliary forward proxy port\n"
" -l max maximum number of concurrent requests\n"
" -u uid|user change uid\n"
" -g gid|group change gid\n"
" -r dir chroot to dir\n"
" -D dir databases are in dir (default is %s)\n"
" -f file use config file (default is %s; *overwrites*)\n"
" -a host|ip auxiliary forward server to use\n"
" -A port auxiliary forward server port (default is 80)\n"
" -n dir pid file (ffproxy.pid) will be written in dir\n",
DATADIR, CFGFILE);
exit(1);
}
static void
drop_privileges(void)
{
extern struct cfg config;
struct passwd *pwd;
if (config.uid == 0 || config.gid == 0) {
info("not changing UID/GID");
} else {
if ((pwd = getpwuid(config.uid)) == NULL)
fatal("getpwuid() failed (non-existent UID entry?)");
if (*config.chroot != '\0') {
if (chdir(config.chroot) != 0)
fatal("chdir() failed");
if (chroot(config.chroot) != 0)
fatal("chroot() failed");
info("chroot()ed to %s\n", config.chroot);
}
if (setgid(config.gid) != 0)
fatal("setgid() failed");
if (initgroups(pwd->pw_name, config.gid) != 0)
fatal("initgroups() failed");
if (setuid(config.uid) != 0)
fatal("setuid() failed");
}
info("=> UID(%d), EUID(%d), GID(%d), EGID(%d)", getuid(), geteuid(), getgid(), getegid());
}
static void
write_pid(char *dir, pid_t p)
{
FILE *fp;
if (*dir != '\0') {
if (chdir(dir) == 0) {
if ((fp = fopen("ffproxy.pid", "w")) == NULL)
fatal("cannot create pid file ffproxy.pid in %s", dir);
else {
info("writing pid in %s/ffproxy.pid", dir);
(void) fprintf(fp, "%ld", (long) p);
(void) fclose(fp);
}
}
else
fatal("cannot change directory for %s to write pid file", dir);
}
}
|