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
|
/* strictmain.cpp
*
* Copyright (c) 1998-2000 Alexander Neundorf
* [email protected]
*
* You may distribute under the terms of the GNU General Public
* License as specified in the COPYING file.
*
* 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.
*
*/
#include "lisadefines.h"
#include "netmanager.h"
#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
// detect linux/glibc for the gnu style --args
#if defined(__linux__) || defined(__linux) || defined(linux)
# include <features.h>
# ifdef __GLIBC__
// only gnu libc has getopt.h... getopt(3) is defined to be in unistd.h
// by POSIX.2
# ifndef _GNU_SOURCE
# define _GNU_SOURCE
# endif
# include <getopt.h>
# endif // __GLIBC__
# define GNU_GETOPT
#endif // linux
#ifdef LISA_DEBUG
#undef LISA_DEBUG
#endif
#define LISA_DEBUG 0
#ifdef dcerr
#undef dcerr
#endif
#define dcerr if (LISA_DEBUG==1) std::cerr<<"strictmain "
void printVersion()
{
const char * versionInfo=\
"\r\nThis is the restricted LAN Information Server resLISa "MYVERSION"\r\n"\
"It is free software according the GNU General Public License\r\n"\
"Copyright (c) 2000-2003 by Alexander Neundorf\r\n"\
"email: [email protected]\r\n";
std::cout<<versionInfo<<std::endl;
}
void usage()
{
printVersion();
const char * usageInfo=\
"-v, --version prints out a short version info\n"\
"-u, --unix deprecated\n"\
"-k, --kde1 deprecated\n"\
"-K, --kde2 deprecated\n"\
" reslisa now always looks first for $(HOME)/.reslisarc, then for /etc/reslisarc\"\n"\
"-c, --config=FILE read this and no other configuration file\n"\
"-q, --quiet start quiet without the greeting message\n"\
"-h, --help you are currently reading it ;-)\n";
std::cout<<usageInfo<<std::endl;
//I thought this would be the way to check wether long options are supported...
//#ifndef _GNU_SOURCE
// cout<<"Please note that the long options are not supported on this system"<<endl;
//#endif
}
void destruct(int sigNumber)
{
signal(sigNumber,SIG_IGN);
std::cout<<"signal caught: "<<sigNumber<<", exiting"<<std::endl;
//signal(sigNumber,&destruct);
exit(0);
}
NetManager *manager(0);
void readConfig(int sigNumber)
{
std::cout<<"readConfig(): signal caught: "<<sigNumber<<std::endl;
signal(SIGHUP,SIG_IGN);
if (manager!=0)
manager->readConfig();
signal(SIGHUP,&readConfig);
}
void printState(int sigNumber)
{
std::cout<<"printState(): signal caught: "<<sigNumber<<std::endl;
signal(SIGUSR1,SIG_IGN);
if (manager!=0)
manager->printState();
signal(SIGUSR1,&printState);
}
void setSignalHandler()
{
signal(SIGHUP,&readConfig);
signal(SIGUSR1,&printState);
signal(SIGINT,&destruct);
signal(SIGQUIT,&destruct);
signal(SIGILL,&destruct);
signal(SIGTRAP,&destruct);
signal(SIGABRT,&destruct);
signal(SIGBUS,&destruct);
signal(SIGSEGV,&destruct);
signal(SIGUSR2,&destruct);
signal(SIGPIPE,&destruct);
signal(SIGALRM,&destruct);
signal(SIGTERM,&destruct);
signal(SIGFPE,&destruct);
#ifdef SIGPOLL
signal(SIGPOLL, &destruct);
#endif
#ifdef SIGSYS
signal(SIGSYS, &destruct);
#endif
#ifdef SIGVTALRM
signal(SIGVTALRM, &destruct);
#endif
#ifdef SIGXCPU
signal(SIGXCPU, &destruct);
#endif
#ifdef SIGXFSZ
signal(SIGXFSZ, &destruct);
#endif
}
#ifdef GNU_GETOPT
static struct option const long_opts[] =
{
{"version", no_argument, 0, 'v'},
{"quiet", no_argument, 0, 'q'},
{"unix", no_argument, 0, 'u'},
{"kde1", no_argument, 0, 'k'},
{"kde2", no_argument, 0, 'K'},
{"config", required_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
#endif
int main(int argc, char** argv)
{
int quiet(0);
int c(0);
int configStyle(UNIXCONFIGSTYLE);
MyString configFile;
int portToUse(MYPORT);
//I thought this would be the way to check wether long options are supported...
#ifdef GNU_GETOPT
while ((c=getopt_long(argc, argv, "vqukKc:h", long_opts, 0))!=-1)
#else
while ((c=getopt(argc, argv, "vqukKc:h"))!=-1)
#endif
{
switch (c)
{
case 0:
break;
case 'v':
printVersion();
exit(0);
break;
case 'q':
quiet=1;
break;
case 'u':
case 'k':
case 'K':
std::cerr<<"\a\nThe command line switches -k, -K, -u and \ntheir long versions "\
"--kde1, --kde2 and --unix are not supported anymore.\n"\
"ResLisa will always first look for $(HOME)/.reslisarc , then for /etc/reslisarc.\n"\
"If your lisa configuration file was created using an older version of \n"\
"the KDE control center, copy the $(HOME)/.trinity/share/config/reslisarc to $(HOME)/.reslisarc.\n"<<std::endl;
break;
case 'c':
configFile = optarg;
configStyle = EXTRACONFIGSTYLE;
break;
case 'h':
default:
usage();
exit(0);
break;
}
}
//fork and let the parent exit
pid_t pid=fork();
if (pid>0)
{
//this is the parent
exit(0);
}
else if (pid<0)
{
dcerr<<"could not fork()"<<std::endl;
exit(0);
}
//we will only read/write to/from this socket in the child process
int rawSocket=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if (rawSocket==-1)
{
std::cout<<"could not create raw socket, root privileges required"<<std::endl;
std::cout<<"take a look at the README for more information"<<std::endl;
exit(0);
}
int bufferSize(60*1024);
int on(1);
setsockopt(rawSocket, SOL_SOCKET, SO_RCVBUF, (char*)&bufferSize,
sizeof(bufferSize));
int result=setsockopt(rawSocket, SOL_SOCKET, SO_BROADCAST, (char*)&on,
sizeof(on));
dcerr<<"setsockopt returns "<<result<<std::endl;
//dropping root privileges
//they will be regained once in the child process
//for creating a raw socket
//now dropping root privileges once and ever
setuid(getuid());
//according to R. Stevens the following three lines
//make daemons feel good :)
setsid();
chdir("/");
umask(0);
dcerr<<"starting, dropped root privileges"<<std::endl;
dcerr<<"port: "<<portToUse<<" file: "<<configFile<<std::endl;
NetManager netmanager(rawSocket,portToUse,configFile,configStyle,1);
manager=&netmanager;
dcerr<<"NetManager created"<<std::endl;
setSignalHandler();
netmanager.readConfig();
if (netmanager.prepare())
{
if (!quiet)
printVersion();
netmanager.run();
}
dcerr<<"server finished"<<std::endl;
}
|