Changes for kernel and Busybox
[tomato.git] / release / src / router / bpalogin / bpalogin.h
blobc812cdb6b41f81049bf45be4fc26b617617214cd
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:
24 * 2001-12-05: wdrose Added errno.h to list of UNIX-only includes.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
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 <netdb.h>
39 #include <syslog.h>
40 #include <arpa/inet.h>
41 #endif
43 #include <time.h>
44 #include <signal.h>
45 #include <sys/types.h>
46 #include "get_time.h"
48 #ifdef _WIN32
49 #include <winsock.h>
50 #endif
53 ** Win32 & BeOS use a closesocket call, and unices use a close call, this define fixes this up
55 #ifndef _WIN32
56 int closesocket(int);
57 //#define closesocket close
58 #endif
61 #ifdef _WIN32
62 #define NOSYSLOG
63 #define HAS_VNSPRINTF
65 #define sleep(x) Sleep((x)*1000)
66 #endif
69 ** This is needed for compiling with EMX under OS/2
71 #ifdef __EMX__
72 #define strcasecmp stricmp
73 #endif
76 #define TRUE 1
77 #define FALSE 0
79 #define LOGIN_SOFTWARE "bpalogin"
80 #define LOGIN_VERSION 1
82 #define MAXUSERNAME 25
83 #define MAXPASSWORD 25
84 #define MAXAUTHSERVER 80
85 #define MAXAUTHDOMAIN 80
86 #define MAXLOGINPROG 256
87 #define MAXCONFFILE 256
88 #define MAXLOCALADDRESS 32
89 #define MAXDDNSCONFFILE 256
91 #define DEFAULT_DEBUG 1
92 #define DEFAULT_AUTHSERVER "sm-server"
93 #define DEFAULT_AUTHDOMAIN ""
94 #define DEFAULT_AUTHPORT 5050
95 #define DEFAULT_CONFFILE "/usr/local/etc/bpalogin.conf"
97 ** state engine codes
99 #define STATE_NEED_PROTOCOL 0
100 #define STATE_SEND_PROT_REQ 1
101 #define STATE_AWAIT_NEG_RESP 2
102 #define STATE_SEND_LOGIN_REQ 3
103 #define STATE_AWAIT_LOGIN_AUTH_RESP 4
104 #define STATE_SEND_LOGIN_AUTH_REQ 5
105 #define STATE_AWAIT_LOGIN_RESP 6
106 #define STATE_SEND_LOGOUT_REQ 7
107 #define STATE_AWAIT_LOGOUT_AUTH_RESP 8
108 #define STATE_SEND_LOGOUT_AUTH_REQ 9
109 #define STATE_AWAIT_LOGOUT_RESP 10
110 #define STATE_IDLE_LOGIN 11
111 #define STATE_RECEIVED_STATUS_REQ 12
112 #define STATE_RECEIVED_RESTART_REQ 13
113 #define STATE_IDLE_LOGOFF 14
116 ** message type codes
118 #define T_MSG_MIN 1
119 #define T_MSG_PROTOCOL_NEG_REQ 1
120 #define T_MSG_PROTOCOL_NEG_RESP 2
121 #define T_MSG_LOGIN_REQ 3
122 #define T_MSG_LOGIN_AUTH_REQ 4
123 #define T_MSG_LOGIN_RESP 5
124 #define T_MSG_LOGOUT_REQ 6
125 #define T_MSG_LOGOUT_AUTH_RESP 7
126 #define T_MSG_LOGOUT_RESP 8
127 #define T_MSG_AUTH_RESP 9
128 #define T_MSG_AUTH_REQ 10
129 #define T_MSG_STATUS_REQ 11
130 #define T_MSG_STATUS_RESP 12
131 #define T_MSG_RESTART_REQ 13
132 #define T_MSG_RESTART_RESP 14
133 #define T_MSG_MAX 14
136 ** message parameter codes
138 #define T_PARAM_MIN 1
139 #define T_PARAM_PROTOCOL_LIST 1
140 #define T_PARAM_PROTOCOL_SELECT 2
141 #define T_PARAM_CLIENT_VERSION 3
142 #define T_PARAM_OS_IDENTITY 4
143 #define T_PARAM_OS_VERSION 5
144 #define T_PARAM_REASON_CODE 6
145 #define T_PARAM_USERNAME 7
146 #define T_PARAM_REQ_PORT 8
147 #define T_PARAM_RESPONSE_TEXT 9
148 #define T_PARAM_STATUS_CODE 10
149 #define T_PARAM_AUTH_CREDENTIALS 11
150 #define T_PARAM_NONCE 12
151 #define T_PARAM_SEQNUM 13
152 #define T_PARAM_HASH_METHOD 14
153 #define T_PARAM_LOGIN_SERVICE_PORT 15
154 #define T_PARAM_LOGOUT_SERVICE_PORT 16
155 #define T_PARAM_STATUS_SERVICE_PORT 17
156 #define T_PARAM_SUSPEND_IND 18
157 #define T_PARAM_STATUS_AUTH 19
158 #define T_PARAM_RESTART_AUTH 20
159 #define T_PARAM_TIMESTAMP 21
160 #define T_PARAM_TSMLIST 22
161 #define T_PARAM_LOGIN_PARAM_HASH 23
162 #define T_PARAM_LOGIN_SERVER_HOST 24
163 #define T_PARAM_MAX 24
166 ** login reason codes
168 #define T_LOGIN_REASON_CODE_NORMAL 0
169 #define T_LOGIN_REASON_CODE_REAUTH 1
172 ** logout reason codes
174 #define T_LOGOUT_REASON_CODE_USER_INITIATED 0
175 #define T_LOGOUT_REASON_CODE_APP_SHUTDOWN 1
176 #define T_LOGOUT_REASON_CODE_OS_SHUTDOWN 2
177 #define T_LOGOUT_REASON_CODE_UNKNOWN 3
180 ** client status transaction codes
182 #define T_STATUS_TRANSACTION_OK 0
185 ** restart reasons
187 #define T_RESTART_ADMIN 0
190 ** auth responses
192 #define T_AUTH_NOHASH 0
193 #define T_AUTH_MD5_HASH 1
196 ** protocol types
198 #define T_PROTOCOL_CHAL 1
201 ** status return codes
203 #define T_STATUS_SUCCESS 0
204 #define T_STATUS_USERNAME_NOT_FOUND 1
205 #define T_STATUS_INCORRECT_PASSWORD 2
206 #define T_STATUS_ACCOUNT_DISABLED 3
207 #define T_STATUS_USER_DISABLED 4
208 #define T_STATUS_LOGIN_SUCCESSFUL_ALREADY_LOGGED_IN 100
209 #define T_STATUS_LOGIN_RETRY_LIMIT 101
210 #define T_STATUS_LOGIN_SUCCESSFUL_SWVER 102
211 #define T_STATUS_LOGIN_FAIL_SW 103
212 #define T_STATUS_LOGOUT_SUCCESSFUL_ALREADY_DISCONNECTED 200
213 #define T_STATUS_LOGOUT_AUTH_RETRY_LIMIT 201
214 #define T_STATUS_LOGIN_SUCCESS_SWVER 300
215 #define T_STATUS_LOGIN_FAIL_SWVER 301
216 #define T_STATUS_LOGIN_FAIL_INV_PROT 302
217 #define T_STATUS_LOGIN_UNKNOWN 500
218 #define T_STATUS_FAIL_USERNAME_VALIDATE 501
219 #define T_STATUS_FAIL_PASSWORD_VALIDATE 502
221 typedef unsigned short INT2;
222 typedef unsigned int INT4;
224 struct transaction
226 char data[1512];
227 int length;
231 ** This structure holds all information necessary to connect/disconnect
233 struct session
236 ** Control paramters
238 char username[MAXUSERNAME];
239 char password[MAXPASSWORD];
240 char authserver[MAXAUTHSERVER];
241 char authdomain[MAXAUTHDOMAIN];
242 unsigned short authport;
243 char connectedprog[MAXLOGINPROG];
244 char disconnectedprog[MAXLOGINPROG];
245 void * pUserData;
246 int shutdown;
247 char localaddress[MAXLOCALADDRESS];
248 unsigned short localport;
249 int minheartbeat, maxheartbeat;
252 ** Callback functions
254 void (*debug)(int,char *,...);
255 void (*critical)(char *);
256 void (*noncritical)(char *,...);
257 void (*onconnected)(int listenport);
258 void (*ondisconnected)(int reason);
261 ** Internal data
263 INT4 sessionid;
264 INT2 listenport;
265 struct sockaddr_in authhost;
266 char osname[80];
267 char osrelease[80];
268 int listensock;
269 struct sockaddr_in localaddr;
270 struct sockaddr_in localipaddress;
272 INT2 protocol;
273 INT2 loginserviceport;
274 char loginserverhost[128];
275 INT2 hashmethod;
276 char nonce[17];
277 INT2 retcode;
278 INT2 logoutport;
279 INT2 statusport;
280 char tsmlist[512];
281 char tsmlist_s[512][20];
282 struct sockaddr_in tsmlist_in[20];
283 int tsmcount;
284 char resptext[512];
285 INT4 timestamp;
287 time_t lastheartbeat;
288 int recenthb;
289 INT4 sequence;
290 struct sockaddr_in fromaddr;
294 ** Prototypes
296 int mainloop(struct session *);
297 int handle_heartbeats(struct session *);
299 void start_transaction(struct transaction * t,INT2 msgtype,INT4 sessionid);
300 void send_transaction(struct session *s,int socket,struct transaction * t);
301 INT2 receive_transaction(struct session *s,int socket,struct transaction * t);
302 INT2 receive_udp_transaction(struct session *s,int socket,struct transaction * t,struct sockaddr_in *addr);
303 void send_udp_transaction(struct session * s,struct transaction * t);
305 int extract_valueINT2(struct session *s,struct transaction * t,INT2 parm,INT2 *v);
306 int extract_valueINT4(struct session *s,struct transaction *,INT2,INT4 *);
307 int extract_valuestring(struct session *s,struct transaction *,INT2,char *);
309 void add_field_string(struct session *s,struct transaction * t,INT2 fn,char * p);
310 void add_field_data(struct session *s,struct transaction * t,INT2 fn,char * p,int c);
311 void add_field_INT2(struct session *s,struct transaction * t,INT2 fn,INT2 v);
312 void add_field_INT4(struct session *s,struct transaction * t,INT2 fn,INT4 v);
314 int login(struct session *);
315 int logout(INT2,struct session *);
317 INT2 read_INT2(void *);
318 INT4 read_INT4(void *);
320 void socketerror(struct session *,const char *);
322 extern int test_connect_success;