Changes for kernel and Busybox
[tomato.git] / release / src / router / bpalogin / unixmain.c
blob576896d8dd895dd4d97dc86ed43da2a3fa4d71a6
1 /*
2 ** BPALogin - lightweight portable BIDS2 login client
3 ** Copyright (c) 2001-3 Shane Hyde, and others.
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
21 /**
22 * Changes:
23 * 2001-09-19: wdrose Fixed incorrect use of single fork() to put
24 * BPALogin into background. Replaced with
25 * fork(), setsid(), fork().
27 * 2001-12-05: wdrose Added fix gleaned from Sam Johnston to include
28 * errno.h for errno, rather than assuming it is an
29 * extern int.
31 #include "bpalogin.h"
33 #define BPALOGIN_BANNER \
34 "BPALogin v2.0.2 - portable BigPond Broadband login client"
36 struct session s;
37 int debug_level = DEFAULT_DEBUG;
38 int dosyslog = 1;
40 int test_connect_success = 0;
42 int parse_parms(struct session *,char * conffile);
43 void usage( void );
44 void debug(int l,char *s,...);
46 void onconnected(int i)
48 if(strcmp(s.connectedprog,""))
50 char buf[500];
51 sprintf(buf,"%.500s %d %d",s.connectedprog,s.listenport,getpid()); // modify by honor
53 syslog(LOG_INFO, "The user logged in successfully");
55 debug(0,"Executing external command - %s\n",buf);
56 system(buf);
60 void ondisconnected(int reason)
62 if(strcmp(s.disconnectedprog,""))
64 char buf[500];
65 sprintf(buf,"%.500s %d",s.disconnectedprog,reason);
67 syslog(LOG_INFO, "The user logged in successfully");
69 debug(0,"Executing external command - %s\n",buf);
70 system(buf);
74 void critical(char *s)
76 if(dosyslog)
77 syslog(LOG_CRIT,"Critical error: %s\n",s);
78 else
79 printf("Critical error: %s\n",s);
80 exit(1);
83 void debug(int l,char *s,...)
85 va_list ap;
86 va_start(ap,s);
87 if(debug_level > l)
89 int pri;
90 char buf[256];
92 switch(l)
94 case 0:
95 pri = LOG_INFO;
96 break;
97 case 1:
98 pri = LOG_INFO;
99 break;
100 case 2:
101 case 3:
102 default:
103 pri = LOG_INFO;
104 break;
106 vsprintf(buf,s,ap);
107 if(dosyslog)
108 syslog(pri,"%s",buf);
109 else
110 printf("%s",buf);
112 va_end(ap);
115 void noncritical(char *s,...)
117 char buf[256];
119 va_list ap;
120 va_start(ap,s);
121 vsprintf(buf,s,ap);
122 if(dosyslog)
123 syslog(LOG_CRIT,buf);
124 else
125 printf(buf);
126 va_end(ap);
129 void onsignal(int i)
131 syslog(LOG_INFO, "heartbeat daemon shut down");
132 debug(1,"Signal caught\n");
133 logout(0,&s);
134 s.ondisconnected(0);
135 closelog();
136 exit(1);
139 int main(int argc,char* argv[])
141 int makedaemon = 1;
142 char conffile[256];
144 int c;
146 signal(SIGINT,onsignal);
147 signal(SIGHUP,onsignal);
148 signal(SIGTERM,onsignal);
150 strcpy(s.authserver,DEFAULT_AUTHSERVER);
151 strcpy(s.authdomain,DEFAULT_AUTHDOMAIN);
152 s.authport = DEFAULT_AUTHPORT;
153 strcpy(s.username,"");
154 strcpy(s.password,"");
155 strcpy(s.connectedprog,"");
156 strcpy(s.disconnectedprog,"");
157 strcpy(conffile,DEFAULT_CONFFILE);
158 strcpy(s.localaddress,"");
159 s.localport = 0;
160 s.minheartbeat = 60;
161 s.maxheartbeat = 840;
163 while( (c = getopt( argc, argv, "c:d:l:Dt" )) > -1 ) {
164 switch( c ) {
165 case 'c':
166 strncpy( conffile, optarg, MAXCONFFILE);
167 break;
168 case 't':
169 test_connect_success = 1;
170 break;
171 case '?':
172 usage();
173 exit(1);
174 break;
178 if(!parse_parms(&s,conffile)) {
179 printf( "bpalogin: Could not read configuration file (%s)\n\n",
180 conffile );
181 usage();
182 exit(1);
185 optind = 1;
186 while( (c = getopt( argc, argv, "c:d:l:Dt" )) > -1 ) {
187 switch( c ) {
188 case 'D':
189 makedaemon = 0;
190 break;
191 case 'c':
192 break;
193 case 'd':
194 debug_level = atoi(optarg);
195 break;
196 case 'l':
197 if( strcasecmp( optarg, "stdout" ) == 0 )
198 dosyslog = 0;
199 else
200 dosyslog = 1;
201 break;
202 case '?':
203 break;
204 case ':':
205 break;
206 case 't':
207 break;
211 if(makedaemon) {
213 * Original code did not perform the setsid() or second fork(), and
214 * hence did not correctly make itself a daemon. There is a library
215 * call in FreeBSD (daemon) that does the actions below, but the
216 * portability is unknown.
218 switch( fork() ) {
219 case 0:
220 break;
222 case -1:
223 perror("Could not run BPALogin in the background");
224 exit(1);
225 break;
227 default:
228 exit(0);
229 break;
232 if( setsid() < 0 ) {
233 perror("Could not run BPALogin in the background");
234 exit(1);
238 * while not strictly necessary, the second fork ensures we stay
239 * detached from a terminal by preventing the program using its
240 * status as session leader to regain a terminal.
242 switch( fork() ) {
243 case 0:
244 break;
246 case -1:
247 perror("Could not run BPALogin in the background");
248 exit(1);
249 break;
251 default:
252 exit(0);
253 break;
258 openlog("bpalogin",LOG_PID,LOG_DAEMON);
260 if(dosyslog)
261 syslog( LOG_INFO, BPALOGIN_BANNER "\n" );
262 else
263 printf( BPALOGIN_BANNER "\n");
265 if(!strcmp(s.username,""))
267 critical("Username has not been set");
268 exit(1);
270 if(!strcmp(s.password,""))
272 critical("Password has not been set");
273 exit(1);
275 s.debug = debug;
276 s.critical = critical;
277 s.noncritical = noncritical;
278 s.onconnected = onconnected;
279 s.ondisconnected = ondisconnected;
281 while(mainloop(&s));
282 s.ondisconnected(0);
284 exit(0);
287 int parse_parms(struct session *s,char * conffile)
289 char buf[512];
290 FILE * f;
292 f = fopen(conffile,"rt");
293 if(!f)
295 debug(0,"Cannot open conf file\n");
296 return FALSE;
299 while(fgets(buf,400,f) != NULL)
301 char parm[100];
302 char value[100];
304 if(buf[0] == '#')
305 continue;
308 * Problem with using sscanf(buf, "%s %s"), parm, value):
309 * usernames with periods et al are not picked up correctly.
310 * Really need to use strtok.
312 sscanf(buf,"%s %s",parm,value);
313 debug(2,"Parameter %s set to %s\n",parm,value);
315 if(!strcasecmp(parm,"username"))
317 strcpy(s->username,value);
319 else if(!strcasecmp(parm,"password"))
321 strcpy(s->password,value);
323 else if(!strcasecmp(parm,"authdomain"))
325 strcpy(s->authdomain,".");
326 strcat(s->authdomain,value);
328 else if(!strcasecmp(parm,"authserver"))
330 strcpy(s->authserver,value);
332 else if(!strcasecmp(parm,"localaddress"))
334 strcpy(s->localaddress,value);
336 else if(!strcasecmp(parm,"logging"))
339 if(!strcasecmp("stdout",value) ||
340 !strcasecmp("sysout",value)) dosyslog = 0; // for compatibility
341 if(!strcasecmp("syslog",value)) dosyslog = 1;
343 if(!strcasecmp("stdout",value) ||
344 !strcasecmp("sysout",value)) dosyslog = 1; // for compatibility
345 if(!strcasecmp("syslog",value)) dosyslog = 0;
347 else if(!strcasecmp(parm,"debuglevel"))
349 debug_level = atoi(value);
351 else if(!strcasecmp(parm,"minheartbeatinterval"))
353 s->minheartbeat = atoi(value);
355 else if(!strcasecmp(parm,"maxheartbeatinterval"))
357 s->maxheartbeat = atoi(value);
359 else if(!strcasecmp(parm,"localport"))
361 s->localport = atoi(value);
363 else if(!strcasecmp(parm,"connectedprog"))
365 strcpy(s->connectedprog,value);
367 else if(!strcasecmp(parm,"disconnectedprog"))
369 strcpy(s->disconnectedprog,value);
373 fclose(f);
374 //strcat(s->authserver,s->authdomain);
375 return TRUE;
378 void usage( void )
380 printf( BPALOGIN_BANNER "\n");
381 printf("Copyright (c) 2001-3 Shane Hyde and others\n\n");
382 printf("This program is *not* a product of Big Pond Advance\n\n");
383 printf("Usage: bpalogin [-c file] [-d level] [-l style] [-D]\n\n");
384 printf(" -c file Specifies the configuration file to use\n");
385 printf(" (default is %s)\n\n", DEFAULT_CONFFILE);
386 printf(" -d level Set the verbosity of log messages\n");
387 printf(" (0 is quiet, 2 is most verbose)\n\n");
388 printf(" -l style Use syslog or stdout for messages\n\n" );
389 printf(" -D Dont run bpalogin as a daemon (run in "
390 "foreground)\n\n");
391 printf("Command line options override the values in the configuration "
392 "file\n");
395 int closesocket(int s)
397 return close(s);
400 void socketerror(struct session *s, const char * str)
402 char buf[200];
403 sprintf(buf,"%.100s - %.80s",str,strerror(errno));
404 s->noncritical(buf);