OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / bpalogin / bpalogin.h
1 /*
2 **      BPALogin v2.0 - lightweight portable BIDS2 login client
3 **      Copyright (c) 1999-2000  Shane Hyde (shyde@trontech.net)
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 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25
26 #ifndef _WIN32
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <syslog.h>
33 #include <arpa/inet.h>
34 #endif
35
36 #include <time.h>
37 #include <signal.h>
38 #include <sys/types.h>
39
40 #ifdef _WIN32
41 #include <winsock.h>
42 #endif
43
44 /*
45 **  Win32 & BeOS use a closesocket call, and unices use a close call, this define fixes this up
46 */
47 #ifndef _WIN32
48 int closesocket(int);
49 //#define closesocket close
50 #endif
51
52
53 #ifdef _WIN32
54 #define NOSYSLOG
55 #define HAS_VNSPRINTF
56
57 #define sleep(x) Sleep((x)*1000)
58 #endif
59
60
61 #define TRUE 1
62 #define FALSE 0
63
64 #define LOGIN_SOFTWARE "bpalogin"
65 #define LOGIN_VERSION  1
66
67 #define MAXUSERNAME 25
68 #define MAXPASSWORD 25
69 #define MAXAUTHSERVER 80
70 #define MAXAUTHDOMAIN 80
71 #define MAXLOGINPROG 256
72 #define MAXCONFFILE 256
73 #define MAXLOCALADDRESS 32
74 #define MAXDDNSCONFFILE 256
75
76 #define DEFAULT_DEBUG           1
77 #define DEFAULT_AUTHSERVER      "dce-server"
78 #define DEFAULT_AUTHDOMAIN      ""
79 #define DEFAULT_AUTHPORT        5050
80 #ifdef EMBED
81 #define DEFAULT_CONFFILE        "/etc/config/bpalogin.conf"
82 #else
83 #define DEFAULT_CONFFILE        "/etc/bpalogin.conf"
84 #endif
85 /*
86 ** state engine codes
87 */
88 #define STATE_NEED_PROTOCOL 0
89 #define STATE_SEND_PROT_REQ 1
90 #define STATE_AWAIT_NEG_RESP 2
91 #define STATE_SEND_LOGIN_REQ 3
92 #define STATE_AWAIT_LOGIN_AUTH_RESP 4
93 #define STATE_SEND_LOGIN_AUTH_REQ 5
94 #define STATE_AWAIT_LOGIN_RESP 6
95 #define STATE_SEND_LOGOUT_REQ 7
96 #define STATE_AWAIT_LOGOUT_AUTH_RESP 8
97 #define STATE_SEND_LOGOUT_AUTH_REQ 9
98 #define STATE_AWAIT_LOGOUT_RESP 10
99 #define STATE_IDLE_LOGIN 11
100 #define STATE_RECEIVED_STATUS_REQ 12
101 #define STATE_RECEIVED_RESTART_REQ 13
102 #define STATE_IDLE_LOGOFF 14
103
104 /*
105 ** message type codes
106 */
107 #define T_MSG_MIN 1
108 #define T_MSG_PROTOCOL_NEG_REQ 1
109 #define T_MSG_PROTOCOL_NEG_RESP 2
110 #define T_MSG_LOGIN_REQ 3
111 #define T_MSG_LOGIN_AUTH_REQ 4
112 #define T_MSG_LOGIN_RESP 5
113 #define T_MSG_LOGOUT_REQ 6
114 #define T_MSG_LOGOUT_AUTH_RESP 7
115 #define T_MSG_LOGOUT_RESP 8
116 #define T_MSG_AUTH_RESP 9
117 #define T_MSG_AUTH_REQ 10
118 #define T_MSG_STATUS_REQ 11
119 #define T_MSG_STATUS_RESP 12
120 #define T_MSG_RESTART_REQ 13
121 #define T_MSG_RESTART_RESP 14
122 #define T_MSG_MAX 14
123
124 /*
125 ** message parameter codes
126 */
127 #define T_PARAM_MIN 1
128 #define T_PARAM_PROTOCOL_LIST 1
129 #define T_PARAM_PROTOCOL_SELECT 2
130 #define T_PARAM_CLIENT_VERSION 3
131 #define T_PARAM_OS_IDENTITY 4
132 #define T_PARAM_OS_VERSION 5
133 #define T_PARAM_REASON_CODE 6
134 #define T_PARAM_USERNAME 7
135 #define T_PARAM_REQ_PORT 8
136 #define T_PARAM_RESPONSE_TEXT 9
137 #define T_PARAM_STATUS_CODE 10
138 #define T_PARAM_AUTH_CREDENTIALS 11
139 #define T_PARAM_NONCE 12
140 #define T_PARAM_SEQNUM 13
141 #define T_PARAM_HASH_METHOD 14
142 #define T_PARAM_LOGIN_SERVICE_PORT 15
143 #define T_PARAM_LOGOUT_SERVICE_PORT 16
144 #define T_PARAM_STATUS_SERVICE_PORT 17
145 #define T_PARAM_SUSPEND_IND 18
146 #define T_PARAM_STATUS_AUTH 19
147 #define T_PARAM_RESTART_AUTH 20
148 #define T_PARAM_TIMESTAMP 21
149 #define T_PARAM_TSMLIST 22
150 #define T_PARAM_LOGIN_PARAM_HASH 23
151 #define T_PARAM_LOGIN_SERVER_HOST 24
152 #define T_PARAM_MAX 24
153
154 /*
155 ** login reason codes
156 */
157 #define T_LOGIN_REASON_CODE_NORMAL 0
158 #define T_LOGIN_REASON_CODE_REAUTH 1
159
160 /*
161 ** logout reason codes
162 */
163 #define T_LOGOUT_REASON_CODE_USER_INITIATED 0
164 #define T_LOGOUT_REASON_CODE_APP_SHUTDOWN 1
165 #define T_LOGOUT_REASON_CODE_OS_SHUTDOWN 2
166 #define T_LOGOUT_REASON_CODE_UNKNOWN 3
167
168 /*
169 ** client status transaction codes
170 */
171 #define T_STATUS_TRANSACTION_OK 0
172
173 /*
174 ** restart reasons
175 */
176 #define T_RESTART_ADMIN 0
177
178 /*
179 ** auth responses
180 */
181 #define T_AUTH_NOHASH 0
182 #define T_AUTH_MD5_HASH 1
183
184 /*
185 ** protocol types
186 */
187 #define T_PROTOCOL_CHAL 1
188
189 /*
190 ** status return codes
191 */
192 #define T_STATUS_SUCCESS 0
193 #define T_STATUS_USERNAME_NOT_FOUND 1
194 #define T_STATUS_INCORRECT_PASSWORD 2
195 #define T_STATUS_ACCOUNT_DISABLED 3
196 #define T_STATUS_USER_DISABLED 4
197 #define T_STATUS_LOGIN_SUCCESSFUL_ALREADY_LOGGED_IN 100
198 #define T_STATUS_LOGIN_RETRY_LIMIT 101
199 #define T_STATUS_LOGIN_SUCCESSFUL_SWVER 102
200 #define T_STATUS_LOGIN_FAIL_SW 103
201 #define T_STATUS_LOGOUT_SUCCESSFUL_ALREADY_DISCONNECTED 200
202 #define T_STATUS_LOGOUT_AUTH_RETRY_LIMIT 201
203 #define T_STATUS_LOGIN_SUCCESS_SWVER 300
204 #define T_STATUS_LOGIN_FAIL_SWVER 301
205 #define T_STATUS_LOGIN_FAIL_INV_PROT 302
206 #define T_STATUS_LOGIN_UNKNOWN 500
207 #define T_STATUS_FAIL_USERNAME_VALIDATE 501
208 #define T_STATUS_FAIL_PASSWORD_VALIDATE 502
209
210 typedef unsigned short INT2;
211 typedef unsigned int INT4;
212
213 struct transaction
214 {
215         char data[1512];
216         int length;
217 };
218
219 /*
220 **  This structure holds all information necessary to connect/disconnect
221 */
222 struct session
223 {
224         /*
225         **  Control paramters
226         */
227         char username[MAXUSERNAME];
228         char password[MAXPASSWORD];
229         char authserver[MAXAUTHSERVER];
230         char authdomain[MAXAUTHDOMAIN];
231         unsigned short authport;
232         char connectedprog[MAXLOGINPROG];
233         char disconnectedprog[MAXLOGINPROG];
234         void * pUserData;
235         int shutdown;
236         char localaddress[32];
237         unsigned short localport;
238         int minheartbeat;
239
240         /*
241         **  Callback functions
242         */
243         void (*debug)(int,char *,...);
244         void (*critical)(char *);
245         void (*noncritical)(char *,...);
246         void (*onconnected)(int listenport);
247         void (*ondisconnected)(int reason);
248
249         /*
250         **  Internal data
251         */
252         INT4 sessionid;
253         INT2 listenport;
254         struct sockaddr_in authhost;
255         char osname[80];
256         char osrelease[80];
257         int listensock;
258         struct sockaddr_in localaddr;
259         struct sockaddr_in localipaddress;
260
261         INT2 protocol;
262         INT2 loginserviceport;
263         char loginserverhost[128];
264         INT2 hashmethod;
265         char nonce[17];
266         INT2 retcode;
267         INT2 logoutport;
268         INT2 statusport;
269         char tsmlist[512];
270         char tsmlist_s[512][20];
271         struct sockaddr_in tsmlist_in[20];
272         int tsmcount;
273         char resptext[512];
274         INT4 timestamp;
275
276         time_t lastheartbeat;
277         int recenthb;
278         INT4 sequence;
279         struct sockaddr_in fromaddr;
280 };
281
282 /*
283 **  Prototypes
284 */
285 int     mainloop(struct session *);
286 int handle_heartbeats(struct session *);
287
288 void start_transaction(struct transaction * t,INT2 msgtype,INT4 sessionid);
289 void send_transaction(struct session *s,int socket,struct transaction * t);
290 INT2 receive_transaction(struct session *s,int socket,struct transaction * t);
291 INT2 receive_udp_transaction(struct session *s,int socket,struct transaction * t,struct sockaddr_in *addr);
292 void send_udp_transaction(struct session * s,struct transaction * t);
293
294 int extract_valueINT2(struct session *s,struct transaction * t,INT2 parm,INT2 *v);
295 int     extract_valueINT4(struct session *s,struct transaction *,INT2,INT4 *);
296 int     extract_valuestring(struct session *s,struct transaction *,INT2,char *);
297
298 void add_field_string(struct session *s,struct transaction * t,INT2 fn,char * p);
299 void add_field_data(struct session *s,struct transaction * t,INT2 fn,char * p,int c);
300 void add_field_INT2(struct session *s,struct transaction * t,INT2 fn,INT2 v);
301 void add_field_INT4(struct session *s,struct transaction * t,INT2 fn,INT4 v);
302
303 int login(struct session *);
304 int logout(INT2,struct session *);
305
306 INT2 read_INT2(void *);
307 INT4 read_INT4(void *);
308
309 void socketerror(struct session *,const char *);
310