Change owner of log file WRT uid setting
[yazpp-moved-to-github.git] / src / yaz-proxy-main.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy-main.cpp,v 1.23 2003-10-23 12:14:48 adam Exp $
6  */
7
8 #include <signal.h>
9 #include <unistd.h>
10 #include <pwd.h>
11 #include <sys/types.h>
12
13 #include <yaz/log.h>
14 #include <yaz/options.h>
15
16 #include <yaz++/socket-manager.h>
17 #include <yaz++/pdu-assoc.h>
18 #include <yaz++/proxy.h>
19
20 void usage(char *prog)
21 {
22     fprintf (stderr, "%s: [-c config] [-a log] [-m num] [-v level] [-t target] [-i sec] "
23              "[-u uid] [-p pidfile] [-o optlevel] @:port\n", prog);
24     exit (1);
25 }
26
27 static char *pid_fname = 0;
28 static char *uid = 0;
29 static char *log_file = 0;
30
31 int args(Yaz_Proxy *proxy, int argc, char **argv)
32 {
33     char *addr = 0;
34     char *arg;
35     char *prog = argv[0];
36     int ret;
37
38     while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:", argv, argc, &arg)) != -2)
39     {
40         int err;
41         switch (ret)
42         {
43         case 0:
44             if (addr)
45             {
46                 usage(prog);
47                 return 1;
48             }
49             addr = arg;
50             break;
51         case 'c':
52             err = proxy->set_config(arg);
53             if (err == -2)
54             {
55                 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
56                 exit(1);
57             }
58             else if (err == -1)
59             {
60                 fprintf(stderr, "Bad or missing file %s\n", arg);
61                 exit(1);
62             }
63             break;
64         case 'a':
65             proxy->set_APDU_log(arg);
66             break;
67         case 't':
68             proxy->set_default_target(arg);
69             break;
70         case 'U':
71             proxy->set_proxy_authentication(arg);
72             break;
73         case 'o':
74             proxy->option("optimize", arg);
75             break;
76         case 'v':
77             yaz_log_init_level (yaz_log_mask_str(arg));
78             break;
79         case 'l':
80             yaz_log_init_file (arg);
81             log_file = xstrdup(arg);
82             break;
83         case 'm':
84             proxy->set_max_clients(atoi(arg));
85             break;
86         case 'i':
87             proxy->set_client_idletime(atoi(arg));
88             break;
89         case 'T':
90             proxy->set_target_idletime(atoi(arg));
91             break;
92         case 'p':
93             if (!pid_fname)
94                 pid_fname = xstrdup(arg);
95             break;
96         case 'u':
97             if (!uid)
98                 uid = xstrdup(arg);
99             break;
100         default:
101             usage(prog);
102             return 1;
103         }
104     }
105     if (addr)
106     {
107         if (proxy->server(addr))
108         {
109             yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
110             exit(1);
111         }
112     }
113     else
114     {
115         usage(prog);
116         return 1;
117     }
118     return 0;
119 }
120
121 static Yaz_Proxy *static_yaz_proxy = 0;
122 static void sighup_handler(int num)
123 {
124     if (static_yaz_proxy)
125         static_yaz_proxy->reconfig();
126 }
127
128 int main(int argc, char **argv)
129 {
130     static int mk_pid = 0;
131     Yaz_SocketManager mySocketManager;
132     Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
133
134     static_yaz_proxy = &proxy;
135
136     signal(SIGHUP, sighup_handler);
137
138     args(&proxy, argc, argv);
139
140     if (pid_fname)
141     {
142         FILE *f = fopen(pid_fname, "w");
143         if (!f)
144         {
145             yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
146             exit(0);
147         }
148         fprintf(f, "%ld", (long) getpid());
149         fclose(f);
150         xfree(pid_fname);
151     }
152     if (uid)
153     {
154         struct passwd *pw;
155
156         if (!(pw = getpwnam(uid)))
157         {
158             yaz_log(LOG_FATAL, "%s: Unknown user", uid);
159             exit(3);
160         }
161         if (log_file)
162         {
163             chown(log_file, pw->pw_uid,  pw->pw_gid);
164             xfree(log_file);
165         }
166
167         if (setuid(pw->pw_uid) < 0)
168         {
169             yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
170             exit(4);
171         }
172         xfree(uid);
173     }
174
175     while (mySocketManager.processEvent() > 0)
176         ;
177
178     exit (0);
179     return 0;
180 }