2 ** BPALogin v2.0 - lightweight portable BIDS2 login client
3 ** Copyright (c) 1999-2000 Shane Hyde (shyde@trontech.net)
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.
29 #include <sys/socket.h>
30 #include <netinet/in.h>
33 #include <arpa/inet.h>
38 #include <sys/types.h>
45 ** Win32 & BeOS use a closesocket call, and unices use a close call, this define fixes this up
49 //#define closesocket close
57 #define sleep(x) Sleep((x)*1000)
64 #define LOGIN_SOFTWARE "bpalogin"
65 #define LOGIN_VERSION 1
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
76 #define DEFAULT_DEBUG 1
77 #define DEFAULT_AUTHSERVER "dce-server"
78 #define DEFAULT_AUTHDOMAIN ""
79 #define DEFAULT_AUTHPORT 5050
81 #define DEFAULT_CONFFILE "/etc/config/bpalogin.conf"
83 #define DEFAULT_CONFFILE "/etc/bpalogin.conf"
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
105 ** message type codes
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
125 ** message parameter codes
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
155 ** login reason codes
157 #define T_LOGIN_REASON_CODE_NORMAL 0
158 #define T_LOGIN_REASON_CODE_REAUTH 1
161 ** logout reason codes
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
169 ** client status transaction codes
171 #define T_STATUS_TRANSACTION_OK 0
176 #define T_RESTART_ADMIN 0
181 #define T_AUTH_NOHASH 0
182 #define T_AUTH_MD5_HASH 1
187 #define T_PROTOCOL_CHAL 1
190 ** status return codes
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
210 typedef unsigned short INT2;
211 typedef unsigned int INT4;
220 ** This structure holds all information necessary to connect/disconnect
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];
236 char localaddress[32];
237 unsigned short localport;
241 ** Callback functions
243 void (*debug)(int,char *,...);
244 void (*critical)(char *);
245 void (*noncritical)(char *,...);
246 void (*onconnected)(int listenport);
247 void (*ondisconnected)(int reason);
254 struct sockaddr_in authhost;
258 struct sockaddr_in localaddr;
259 struct sockaddr_in localipaddress;
262 INT2 loginserviceport;
263 char loginserverhost[128];
270 char tsmlist_s[512][20];
271 struct sockaddr_in tsmlist_in[20];
276 time_t lastheartbeat;
279 struct sockaddr_in fromaddr;
285 int mainloop(struct session *);
286 int handle_heartbeats(struct session *);
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);
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 *);
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);
303 int login(struct session *);
304 int logout(INT2,struct session *);
306 INT2 read_INT2(void *);
307 INT4 read_INT4(void *);
309 void socketerror(struct session *,const char *);