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.
18**
19*/
20
21/**
22 * Changes:
23 *
24 * 2001-12-05:  wdrose     Added errno.h to list of UNIX-only includes.
25 */
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <stdarg.h>
30#include <string.h>
31
32#ifndef _WIN32
33#include <unistd.h>
34#include <errno.h>
35#include <sys/time.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38#include <net/if.h>
39#include <netdb.h>
40#include <syslog.h>
41#include <arpa/inet.h>
42#endif
43
44#include <time.h>
45#include <signal.h>
46#include <sys/types.h>
47
48#ifdef _WIN32
49#include <winsock.h>
50#endif
51
52/* ***Foxconn Add Start : Steve Hsieh : 2005-10-17 (bpalogin porting) *** */
53/*
54**      to do nvram param set
55*/
56#if 0
57#include <typedefs.h>
58#include <proto/ethernet.h>
59#include <bcmnvram.h>
60#include <bcmutils.h>
61#include <shutils.h>
62#include <netconf.h>
63#include <nvparse.h>
64#include <wlutils.h>
65//#include <bcmcvar.h>
66#include <ezc.h>
67#include <bcmconfig.h>
68#endif
69
70#include <sys/ioctl.h>
71
72
73/*
74**  Win32 & BeOS use a closesocket call, and unices use a close call, this define fixes this up
75*/
76#ifndef _WIN32
77int closesocket(int);
78//#define closesocket close
79#endif
80
81
82#ifdef _WIN32
83#define NOSYSLOG
84#define HAS_VNSPRINTF
85
86#define sleep(x) Sleep((x)*1000)
87#endif
88
89/*
90** This is needed for compiling with EMX under OS/2
91*/
92#ifdef __EMX__
93  #define strcasecmp stricmp
94#endif
95
96
97#define TRUE 1
98#define FALSE 0
99
100#define LOGIN_SOFTWARE "bpalogin"
101#define LOGIN_VERSION  1
102
103#define MAXUSERNAME 61 //25
104#define MAXPASSWORD 51 //25
105#define MAXAUTHSERVER 80
106#define MAXAUTHDOMAIN 80
107#define MAXLOGINPROG 256
108#define MAXCONFFILE 256
109#define MAXLOCALADDRESS 32
110#define MAXDDNSCONFFILE 256
111
112#define DEFAULT_DEBUG         1
113#define DEFAULT_AUTHSERVER    "sm-server"
114#define DEFAULT_AUTHDOMAIN    ""
115#define DEFAULT_AUTHPORT      5050
116#define DEFAULT_CONFFILE      "@@BPALOGIN_CONF@@"
117
118/* *** Foxconn Add Start : Steve Hsieh : 2000-10-20 (bpaclient porting) *** */
119#define POLL_CYCLE                  1           //the period to polling wan side ip, in unit of second
120#define LGIN_CYCLE                  10         //the period to do relogin once a error occur
121/* *** Foxconn Add End : Steve Hsieh : 2000-10-20 (bpaclient porting) *** */
122
123/*
124** state engine codes
125*/
126#define STATE_NEED_PROTOCOL 0
127#define STATE_SEND_PROT_REQ 1
128#define STATE_AWAIT_NEG_RESP 2
129#define STATE_SEND_LOGIN_REQ 3
130#define STATE_AWAIT_LOGIN_AUTH_RESP 4
131#define STATE_SEND_LOGIN_AUTH_REQ 5
132#define STATE_AWAIT_LOGIN_RESP 6
133#define STATE_SEND_LOGOUT_REQ 7
134#define STATE_AWAIT_LOGOUT_AUTH_RESP 8
135#define STATE_SEND_LOGOUT_AUTH_REQ 9
136#define STATE_AWAIT_LOGOUT_RESP 10
137#define STATE_IDLE_LOGIN 11
138#define STATE_RECEIVED_STATUS_REQ 12
139#define STATE_RECEIVED_RESTART_REQ 13
140#define STATE_IDLE_LOGOFF 14
141
142/*
143** message type codes
144*/
145#define T_MSG_MIN 1
146#define T_MSG_PROTOCOL_NEG_REQ 1
147#define T_MSG_PROTOCOL_NEG_RESP 2
148#define T_MSG_LOGIN_REQ 3
149#define T_MSG_LOGIN_AUTH_REQ 4
150#define T_MSG_LOGIN_RESP 5
151#define T_MSG_LOGOUT_REQ 6
152#define T_MSG_LOGOUT_AUTH_RESP 7
153#define T_MSG_LOGOUT_RESP 8
154#define T_MSG_AUTH_RESP 9
155#define T_MSG_AUTH_REQ 10
156#define T_MSG_STATUS_REQ 11
157#define T_MSG_STATUS_RESP 12
158#define T_MSG_RESTART_REQ 13
159#define T_MSG_RESTART_RESP 14
160#define T_MSG_MAX 14
161
162/*
163** message parameter codes
164*/
165#define T_PARAM_MIN 1
166#define T_PARAM_PROTOCOL_LIST 1
167#define T_PARAM_PROTOCOL_SELECT 2
168#define T_PARAM_CLIENT_VERSION 3
169#define T_PARAM_OS_IDENTITY 4
170#define T_PARAM_OS_VERSION 5
171#define T_PARAM_REASON_CODE 6
172#define T_PARAM_USERNAME 7
173#define T_PARAM_REQ_PORT 8
174#define T_PARAM_RESPONSE_TEXT 9
175#define T_PARAM_STATUS_CODE 10
176#define T_PARAM_AUTH_CREDENTIALS 11
177#define T_PARAM_NONCE 12
178#define T_PARAM_SEQNUM 13
179#define T_PARAM_HASH_METHOD 14
180#define T_PARAM_LOGIN_SERVICE_PORT 15
181#define T_PARAM_LOGOUT_SERVICE_PORT 16
182#define T_PARAM_STATUS_SERVICE_PORT 17
183#define T_PARAM_SUSPEND_IND 18
184#define T_PARAM_STATUS_AUTH 19
185#define T_PARAM_RESTART_AUTH 20
186#define T_PARAM_TIMESTAMP 21
187#define T_PARAM_TSMLIST 22
188#define T_PARAM_LOGIN_PARAM_HASH 23
189#define T_PARAM_LOGIN_SERVER_HOST 24
190#define T_PARAM_MAX 24
191
192/*
193** login reason codes
194*/
195#define T_LOGIN_REASON_CODE_NORMAL 0
196#define T_LOGIN_REASON_CODE_REAUTH 1
197
198/*
199** logout reason codes
200*/
201#define T_LOGOUT_REASON_CODE_USER_INITIATED 0
202#define T_LOGOUT_REASON_CODE_APP_SHUTDOWN 1
203#define T_LOGOUT_REASON_CODE_OS_SHUTDOWN 2
204#define T_LOGOUT_REASON_CODE_UNKNOWN 3
205
206/*
207** client status transaction codes
208*/
209#define T_STATUS_TRANSACTION_OK 0
210
211/*
212** restart reasons
213*/
214#define T_RESTART_ADMIN 0
215
216/*
217** auth responses
218*/
219#define T_AUTH_NOHASH 0
220#define T_AUTH_MD5_HASH 1
221
222/*
223** protocol types
224*/
225#define T_PROTOCOL_CHAL 1
226
227/*
228** status return codes
229*/
230#define T_STATUS_SUCCESS 0
231#define T_STATUS_USERNAME_NOT_FOUND 1
232#define T_STATUS_INCORRECT_PASSWORD 2
233#define T_STATUS_ACCOUNT_DISABLED 3
234#define T_STATUS_USER_DISABLED 4
235#define T_STATUS_LOGIN_SUCCESSFUL_ALREADY_LOGGED_IN 100
236#define T_STATUS_LOGIN_RETRY_LIMIT 101
237#define T_STATUS_LOGIN_SUCCESSFUL_SWVER 102
238#define T_STATUS_LOGIN_FAIL_SW 103
239#define T_STATUS_LOGOUT_SUCCESSFUL_ALREADY_DISCONNECTED 200
240#define T_STATUS_LOGOUT_AUTH_RETRY_LIMIT 201
241#define T_STATUS_LOGIN_SUCCESS_SWVER 300
242#define T_STATUS_LOGIN_FAIL_SWVER 301
243#define T_STATUS_LOGIN_FAIL_INV_PROT 302
244#define T_STATUS_LOGIN_UNKNOWN 500
245#define T_STATUS_FAIL_USERNAME_VALIDATE 501
246#define T_STATUS_FAIL_PASSWORD_VALIDATE 502
247
248/* *** Foxconn Add Start : Steve Hsieh : 2005-10-19 (bpaclient porting) *** */
249/*
250**  error code
251*/
252#define E_BPA_LONG_HB_INTERVAL                  0xffff
253#define E_BPA_SOCKET_ERROR                          0xfffe
254#define E_BPA_SHORT_HB_INTERVAL                0xfffd
255#define E_BPA_UNKNOWN_STATUS_SERVER      0xfffc
256#define E_BPA_ILLEGAL_PKT_FORMAT               0xfffb
257/* *** Foxconn Add Start : Steve Hsieh : 2005-10-19 (bpaclient porting) *** */
258
259typedef unsigned short INT2;
260typedef unsigned int INT4;
261
262
263struct transaction
264{
265    char data[1512];
266    int length;
267};
268
269/*
270**  This structure holds all information necessary to connect/disconnect
271*/
272struct session
273{
274    /*
275    **  Control paramters
276    */
277    char username[MAXUSERNAME];
278    char password[MAXPASSWORD];
279    //steve add
280    char password_hashed[MAXPASSWORD];
281    char authserver[MAXAUTHSERVER];
282    char authdomain[MAXAUTHDOMAIN];
283    unsigned short authport;
284    char connectedprog[MAXLOGINPROG];
285    char disconnectedprog[MAXLOGINPROG];
286    void * pUserData;
287    int shutdown;
288    char localaddress[MAXLOCALADDRESS];
289    unsigned short localport;
290    int minheartbeat, maxheartbeat;
291    int state;/* Foxconn added, Jared Hsu, 2006/03/15 */
292
293    /*
294    **  Callback functions
295    */
296    void (*debug)(int,char *,...);
297    void (*critical)(char *);
298    void (*noncritical)(char *,...);
299    void (*onconnected)(int listenport);
300    void (*ondisconnected)(int reason);
301
302
303    /*
304    **  Internal data
305    */
306    INT4 sessionid;
307    INT2 listenport;
308    struct sockaddr_in authhost;
309    char osname[80];
310    char osrelease[80];
311    int listensock;
312    struct sockaddr_in localaddr;
313    struct sockaddr_in localipaddress;
314
315    INT2 protocol;
316    INT2 loginserviceport;
317    char loginserverhost[128];
318    INT2 hashmethod;
319    char nonce[17];
320    INT2 retcode;
321    INT2 logoutport;
322    INT2 statusport;
323    char tsmlist[512];
324    char tsmlist_s[512][20];
325    struct sockaddr_in tsmlist_in[20];
326    int tsmcount;
327    char resptext[512];
328    INT4 timestamp;
329
330    time_t lastheartbeat;
331    int recenthb;
332    INT4 sequence;
333    struct sockaddr_in fromaddr;
334};
335
336/*
337**  Prototypes
338*/
339int mainloop(struct session *);
340int handle_heartbeats(struct session *);
341
342void start_transaction(struct transaction * t,INT2 msgtype,INT4 sessionid);
343void send_transaction(struct session *s,int socket,struct transaction * t);
344INT2 receive_transaction(struct session *s,int socket,struct transaction * t);
345INT2 receive_udp_transaction(struct session *s,int socket,struct transaction * t,struct sockaddr_in *addr);
346void send_udp_transaction(struct session * s,struct transaction * t);
347
348int  extract_valueINT2(struct session *s,struct transaction * t,INT2 parm,INT2 *v);
349int  extract_valueINT4(struct session *s,struct transaction *,INT2,INT4 *);
350int  extract_valuestring(struct session *s,struct transaction *,INT2,char *);
351
352void add_field_string(struct session *s,struct transaction * t,INT2 fn,char * p);
353void add_field_data(struct session *s,struct transaction * t,INT2 fn,char * p,int c);
354void add_field_INT2(struct session *s,struct transaction * t,INT2 fn,INT2 v);
355void add_field_INT4(struct session *s,struct transaction * t,INT2 fn,INT4 v);
356
357int  login(struct session *);
358int  logout(INT2,struct session *);
359
360INT2 read_INT2(void *);
361INT4 read_INT4(void *);
362
363void socketerror(struct session *,const char *);
364
365int parse_parms(struct session *,char * conffile);
366void critical(char *s);
367
368void bpa_state(int state);/* Foxconn added, Jared Hsu, 2006/03/15 */