* Copyright (c) 1998-2003, Index Data.
* See the file LICENSE for details.
*
- * $Id: yaz-proxy-main.cpp,v 1.21 2003-10-23 09:08:52 adam Exp $
+ * $Id: yaz-proxy-main.cpp,v 1.26 2003-10-24 12:19:23 adam Exp $
*/
#include <signal.h>
#include <unistd.h>
+#include <pwd.h>
+#include <sys/types.h>
+
#include <yaz/log.h>
#include <yaz/options.h>
void usage(char *prog)
{
- fprintf (stderr, "%s: [-c config] [-a log] [-m num] [-v level] [-t target] [-i sec] "
- "[-u auth] [-o optlevel] @:port\n", prog);
+ fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
+ "[-u uid] [-p pidfile] @:port\n", prog);
exit (1);
}
static char *pid_fname = 0;
+static char *uid = 0;
+static char *log_file = 0;
int args(Yaz_Proxy *proxy, int argc, char **argv)
{
char *prog = argv[0];
int ret;
- while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:", argv, argc, &arg)) != -2)
+ while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:", argv, argc, &arg)) != -2)
{
int err;
switch (ret)
case 't':
proxy->set_default_target(arg);
break;
- case 'u':
+ case 'U':
proxy->set_proxy_authentication(arg);
break;
case 'o':
break;
case 'l':
yaz_log_init_file (arg);
+ log_file = xstrdup(arg);
break;
case 'm':
proxy->set_max_clients(atoi(arg));
if (!pid_fname)
pid_fname = xstrdup(arg);
break;
+ case 'u':
+ if (!uid)
+ uid = xstrdup(arg);
+ break;
default:
usage(prog);
return 1;
}
if (addr)
{
- proxy->server(addr);
+ yaz_log(LOG_LOG, "Starting proxy pid=%ld", (long) getpid());
+ if (proxy->server(addr))
+ {
+ yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
+ exit(1);
+ }
}
else
{
static Yaz_Proxy *static_yaz_proxy = 0;
static void sighup_handler(int num)
{
+ signal(SIGHUP, sighup_handler);
if (static_yaz_proxy)
static_yaz_proxy->reconfig();
}
signal(SIGHUP, sighup_handler);
args(&proxy, argc, argv);
- while (mySocketManager.processEvent() > 0)
- if (!mk_pid && pid_fname)
- {
- FILE *f = fopen(pid_fname, "w");
- if (!f)
- {
- yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
- exit(0);
- }
- fprintf(f, "%ld", (long) getpid());
- fclose(f);
- mk_pid = 1;
- }
+
if (pid_fname)
{
- if (mk_pid)
- unlink(pid_fname);
+ FILE *f = fopen(pid_fname, "w");
+ if (!f)
+ {
+ yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
+ exit(0);
+ }
+ fprintf(f, "%ld", (long) getpid());
+ fclose(f);
xfree(pid_fname);
}
+ if (uid)
+ {
+ struct passwd *pw;
+
+ if (!(pw = getpwnam(uid)))
+ {
+ yaz_log(LOG_FATAL, "%s: Unknown user", uid);
+ exit(3);
+ }
+ if (log_file)
+ {
+ chown(log_file, pw->pw_uid, pw->pw_gid);
+ xfree(log_file);
+ }
+
+ if (setuid(pw->pw_uid) < 0)
+ {
+ yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
+ exit(4);
+ }
+ xfree(uid);
+ }
+
+ while (mySocketManager.processEvent() > 0)
+ ;
+
exit (0);
return 0;
}