2 ** BPALogin - lightweight portable BIDS2 login client
3 ** Copyright (c) 2001-3 Shane Hyde, and others.
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.
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.
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.
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
33 #define BPALOGIN_BANNER \
34 "BPALogin v2.0.2 - portable BigPond Broadband login client"
37 int debug_level
= DEFAULT_DEBUG
;
40 int test_connect_success
= 0;
42 int parse_parms(struct session
*,char * conffile
);
44 void debug(int l
,char *s
,...);
46 void onconnected(int i
)
48 if(strcmp(s
.connectedprog
,""))
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
);
60 void ondisconnected(int reason
)
62 if(strcmp(s
.disconnectedprog
,""))
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
);
74 void critical(char *s
)
77 syslog(LOG_CRIT
,"Critical error: %s\n",s
);
79 printf("Critical error: %s\n",s
);
83 void debug(int l
,char *s
,...)
108 syslog(pri
,"%s",buf
);
115 void noncritical(char *s
,...)
123 syslog(LOG_CRIT
,buf
);
131 syslog(LOG_INFO
, "heartbeat daemon shut down");
132 debug(1,"Signal caught\n");
139 int main(int argc
,char* argv
[])
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
,"");
161 s
.maxheartbeat
= 840;
163 while( (c
= getopt( argc
, argv
, "c:d:l:Dt" )) > -1 ) {
166 strncpy( conffile
, optarg
, MAXCONFFILE
);
169 test_connect_success
= 1;
178 if(!parse_parms(&s
,conffile
)) {
179 printf( "bpalogin: Could not read configuration file (%s)\n\n",
186 while( (c
= getopt( argc
, argv
, "c:d:l:Dt" )) > -1 ) {
194 debug_level
= atoi(optarg
);
197 if( strcasecmp( optarg
, "stdout" ) == 0 )
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.
223 perror("Could not run BPALogin in the background");
233 perror("Could not run BPALogin in the background");
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.
247 perror("Could not run BPALogin in the background");
258 openlog("bpalogin",LOG_PID
,LOG_DAEMON
);
261 syslog( LOG_INFO
, BPALOGIN_BANNER
"\n" );
263 printf( BPALOGIN_BANNER
"\n");
265 if(!strcmp(s
.username
,""))
267 critical("Username has not been set");
270 if(!strcmp(s
.password
,""))
272 critical("Password has not been set");
276 s
.critical
= critical
;
277 s
.noncritical
= noncritical
;
278 s
.onconnected
= onconnected
;
279 s
.ondisconnected
= ondisconnected
;
287 int parse_parms(struct session
*s
,char * conffile
)
292 f
= fopen(conffile
,"rt");
295 debug(0,"Cannot open conf file\n");
299 while(fgets(buf
,400,f
) != NULL
)
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
);
374 //strcat(s->authserver,s->authdomain);
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 "
391 printf("Command line options override the values in the configuration "
395 int closesocket(int s
)
400 void socketerror(struct session
*s
, const char * str
)
403 sprintf(buf
,"%.100s - %.80s",str
,strerror(errno
));