]> git.ipfire.org Git - thirdparty/squid.git/blame - helpers/basic_auth/MSNT/rfcnb-util.c
Bugzilla #369: Don't attempt to close the WCCP socket twice on shutdown /
[thirdparty/squid.git] / helpers / basic_auth / MSNT / rfcnb-util.c
CommitLineData
94439e4e 1/* UNIX RFCNB (RFC1001/RFC1002) NetBIOS implementation
2 *
3 * Version 1.0
4 * RFCNB Utility Routines ...
5 *
6 * Copyright (C) Richard Sharpe 1996
7 *
8 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include "std-includes.h"
27#include "rfcnb-priv.h"
28#include "rfcnb-util.h"
29#include "rfcnb-io.h"
30
31#include <stdlib.h>
32#include <unistd.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <string.h>
38
39char *RFCNB_Error_Strings[] =
40{
41
42 "RFCNBE_OK: Routine completed successfully.",
43 "RFCNBE_NoSpace: No space available for a malloc call.",
44 "RFCNBE_BadName: NetBIOS name could not be translated to IP address.",
45 "RFCNBE_BadRead: Read system call returned an error. Check errno.",
46 "RFCNBE_BadWrite: Write system call returned an error. Check errno.",
47 "RFCNBE_ProtErr: A protocol error has occurred.",
48 "RFCNBE_ConGone: Connection dropped during a read or write system call.",
49 "RFCNBE_BadHandle: Bad connection handle passed.",
50 "RFCNBE_BadSocket: Problems creating socket.",
51 "RFCNBE_ConnectFailed: Connection failed. See errno.",
52 "RFCNBE_CallRejNLOCN: Call rejected. Not listening on called name.",
53 "RFCNBE_CallRejNLFCN: Call rejected. Not listening for called name.",
54 "RFCNBE_CallRejCNNP: Call rejected. Called name not present.",
55 "RFCNBE_CallRejInfRes: Call rejected. Name present, but insufficient resources.",
56 "RFCNBE_CallRejUnSpec: Call rejected. Unspecified error.",
57 "RFCNBE_BadParam: Bad parameters passed to a routine.",
58 "RFCNBE_Timeout: IO Operation timed out ..."
59
60};
61
62extern void (*Prot_Print_Routine) (); /* Pointer to protocol print routine */
63
64/* Convert name and pad to 16 chars as needed */
65/* Name 1 is a C string with null termination, name 2 may not be */
66/* If SysName is true, then put a <00> on end, else space> */
67
68void
69RFCNB_CvtPad_Name(char *name1, char *name2)
70{
71 char c, c1, c2;
72 int i, len;
73
74 len = strlen(name1);
75
76 for (i = 0; i < 16; i++) {
77
78 if (i >= len) {
79
80 c1 = 'C';
81 c2 = 'A'; /* CA is a space */
82
83 } else {
84
85 c = name1[i];
86 c1 = (char) ((int) c / 16 + (int) 'A');
87 c2 = (char) ((int) c % 16 + (int) 'A');
88 }
89
90 name2[i * 2] = c1;
91 name2[i * 2 + 1] = c2;
92
93 }
94
95 name2[32] = 0; /* Put in the nll ... */
96
97}
98
99/* Converts an Ascii NB Name (16 chars) to an RFCNB Name (32 chars)
100 * Uses the encoding in RFC1001. Each nibble of byte is added to 'A'
101 * to produce the next byte in the name.
102 *
103 * This routine assumes that AName is 16 bytes long and that NBName has
104 * space for 32 chars, so be careful ...
105 *
106 */
107
108void
109RFCNB_AName_To_NBName(char *AName, char *NBName)
110{
111 char c, c1, c2;
112 int i;
113
114 for (i = 0; i < 16; i++) {
115
116 c = AName[i];
117
118 c1 = (char) ((c >> 4) + 'A');
119 c2 = (char) ((c & 0xF) + 'A');
120
121 NBName[i * 2] = c1;
122 NBName[i * 2 + 1] = c2;
123 }
124
125 NBName[32] = 0; /* Put in a null */
126
127}
128
129/* Do the reverse of the above ... */
130
131void
132RFCNB_NBName_To_AName(char *NBName, char *AName)
133{
134 char c, c1, c2;
135 int i;
136
137 for (i = 0; i < 16; i++) {
138
139 c1 = NBName[i * 2];
140 c2 = NBName[i * 2 + 1];
141
142 c = (char) (((int) c1 - (int) 'A') * 16 + ((int) c2 - (int) 'A'));
143
144 AName[i] = c;
145
146 }
147
148 AName[i] = 0; /* Put a null on the end ... */
149
150}
151
eb073b3b 152#ifdef RFCNB_DEBUG
94439e4e 153/* Print a string of bytes in HEX etc */
154
155void
156RFCNB_Print_Hex(FILE * fd, struct RFCNB_Pkt *pkt, int Offset, int Len)
157{
158 char c1, c2, outbuf1[33];
159 unsigned char c;
160 int i, j;
161 struct RFCNB_Pkt *pkt_ptr = pkt;
162 static char Hex_List[17] = "0123456789ABCDEF";
163
164 j = 0;
165
166 /* We only want to print as much as sepcified in Len */
167
168 while (pkt_ptr != NULL) {
169
170 for (i = 0;
171 i < ((Len > (pkt_ptr->len) ? pkt_ptr->len : Len) - Offset);
172 i++) {
173
174 c = pkt_ptr->data[i + Offset];
175 c1 = Hex_List[c >> 4];
176 c2 = Hex_List[c & 0xF];
177
178 outbuf1[j++] = c1;
179 outbuf1[j++] = c2;
180
181 if (j == 32) { /* Print and reset */
182 outbuf1[j] = 0;
183 fprintf(fd, " %s\n", outbuf1);
184 j = 0;
185 }
186 }
187
188 Offset = 0;
189 Len = Len - pkt_ptr->len; /* Reduce amount by this much */
190 pkt_ptr = pkt_ptr->next;
191
192 }
193
194 /* Print last lot in the buffer ... */
195
196 if (j > 0) {
197
198 outbuf1[j] = 0;
199 fprintf(fd, " %s\n", outbuf1);
200
201 }
202 fprintf(fd, "\n");
203
204}
eb073b3b 205#endif
94439e4e 206
207/* Get a packet of size n */
208
209struct RFCNB_Pkt *
210RFCNB_Alloc_Pkt(int n)
211{
212 RFCNB_Pkt *pkt;
213
eb073b3b 214 if ((pkt = malloc(sizeof(struct RFCNB_Pkt))) == NULL) {
94439e4e 215 RFCNB_errno = RFCNBE_NoSpace;
216 RFCNB_saved_errno = errno;
217 return (NULL);
94439e4e 218 }
219 pkt->next = NULL;
220 pkt->len = n;
221
222 if (n == 0)
223 return (pkt);
224
eb073b3b 225 if ((pkt->data = malloc(n)) == NULL) {
94439e4e 226 RFCNB_errno = RFCNBE_NoSpace;
227 RFCNB_saved_errno = errno;
228 free(pkt);
229 return (NULL);
94439e4e 230 }
231 return (pkt);
232
233}
234
235/* Free up a packet */
236
237void
238RFCNB_Free_Pkt(struct RFCNB_Pkt *pkt)
239{
240 struct RFCNB_Pkt *pkt_next;
94439e4e 241
242 while (pkt != NULL) {
243
244 pkt_next = pkt->next;
245
eb073b3b 246 if (pkt->data != NULL)
247 free(pkt->data);
94439e4e 248
249 free(pkt);
250
251 pkt = pkt_next;
252
253 }
254
255}
256
eb073b3b 257#ifdef RFCNB_DEBUG
94439e4e 258/* Print an RFCNB packet */
259
260void
261RFCNB_Print_Pkt(FILE * fd, char *dirn, struct RFCNB_Pkt *pkt, int len)
262{
263 char lname[17];
264
265 /* We assume that the first fragment is the RFCNB Header */
266 /* We should loop through the fragments printing them out */
267
268 fprintf(fd, "RFCNB Pkt %s:", dirn);
269
270 switch (RFCNB_Pkt_Type(pkt->data)) {
271
272 case RFCNB_SESSION_MESSAGE:
273
274 fprintf(fd, "SESSION MESSAGE: Length = %i\n", RFCNB_Pkt_Len(pkt->data));
275 RFCNB_Print_Hex(fd, pkt, RFCNB_Pkt_Hdr_Len,
276#ifdef RFCNB_PRINT_DATA
277 RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
278#else
279 40);
280#endif
281
282 if (Prot_Print_Routine != 0) { /* Print the rest of the packet */
283
284 Prot_Print_Routine(fd, strcmp(dirn, "sent"), pkt, RFCNB_Pkt_Hdr_Len,
285 RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
286
287 }
288 break;
289
290 case RFCNB_SESSION_REQUEST:
291
292 fprintf(fd, "SESSION REQUEST: Length = %i\n",
293 RFCNB_Pkt_Len(pkt->data));
294 RFCNB_NBName_To_AName((char *) (pkt->data + RFCNB_Pkt_Called_Offset), lname);
295 fprintf(fd, " Called Name: %s\n", lname);
296 RFCNB_NBName_To_AName((char *) (pkt->data + RFCNB_Pkt_Calling_Offset), lname);
297 fprintf(fd, " Calling Name: %s\n", lname);
298
299 break;
300
301 case RFCNB_SESSION_ACK:
302
303 fprintf(fd, "RFCNB SESSION ACK: Length = %i\n",
304 RFCNB_Pkt_Len(pkt->data));
305
306 break;
307
308 case RFCNB_SESSION_REJ:
309 fprintf(fd, "RFCNB SESSION REJECT: Length = %i\n",
310 RFCNB_Pkt_Len(pkt->data));
311
312 if (RFCNB_Pkt_Len(pkt->data) < 1) {
313 fprintf(fd, " Protocol Error, short Reject packet!\n");
314 } else {
315 fprintf(fd, " Error = %x\n", CVAL(pkt->data, RFCNB_Pkt_Error_Offset));
316 }
317
318 break;
319
320 case RFCNB_SESSION_RETARGET:
321
322 fprintf(fd, "RFCNB SESSION RETARGET: Length = %i\n",
323 RFCNB_Pkt_Len(pkt->data));
324
325 /* Print out the IP address etc and the port? */
326
327 break;
328
329 case RFCNB_SESSION_KEEP_ALIVE:
330
331 fprintf(fd, "RFCNB SESSION KEEP ALIVE: Length = %i\n",
332 RFCNB_Pkt_Len(pkt->data));
333 break;
334
335 default:
336
337 break;
338 }
339
340}
eb073b3b 341#endif
94439e4e 342
343/* Resolve a name into an address */
344
345int
346RFCNB_Name_To_IP(char *host, struct in_addr *Dest_IP)
347{
348 int addr; /* Assumes IP4, 32 bit network addresses */
349 struct hostent *hp;
350
351 /* Use inet_addr to try to convert the address */
352
353 if ((addr = inet_addr(host)) == INADDR_NONE) { /* Oh well, a good try :-) */
354
355 /* Now try a name look up with gethostbyname */
356
357 if ((hp = gethostbyname(host)) == NULL) { /* Not in DNS */
358
359 /* Try NetBIOS name lookup, how the hell do we do that? */
360
361 RFCNB_errno = RFCNBE_BadName; /* Is this right? */
362 RFCNB_saved_errno = errno;
363 return (RFCNBE_Bad);
364
365 } else { /* We got a name */
366
367 memcpy((void *) Dest_IP, (void *) hp->h_addr_list[0], sizeof(struct in_addr));
368
369 }
370 } else { /* It was an IP address */
371
372 memcpy((void *) Dest_IP, (void *) &addr, sizeof(struct in_addr));
373
374 }
375
376 return 0;
377
378}
379
380/* Disconnect the TCP connection to the server */
381
382int
383RFCNB_Close(int socket)
384{
385
386 close(socket);
387
388 /* If we want to do error recovery, here is where we put it */
389
390 return 0;
391
392}
393
394/* Connect to the server specified in the IP address.
395 * Not sure how to handle socket options etc. */
396
397int
398RFCNB_IP_Connect(struct in_addr Dest_IP, int port)
399{
400 struct sockaddr_in Socket;
401 int fd;
402
403 /* Create a socket */
404
405 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { /* Handle the error */
406
407 RFCNB_errno = RFCNBE_BadSocket;
408 RFCNB_saved_errno = errno;
409 return (RFCNBE_Bad);
410 }
411 bzero((char *) &Socket, sizeof(Socket));
412 memcpy((char *) &Socket.sin_addr, (char *) &Dest_IP, sizeof(Dest_IP));
413
414 Socket.sin_port = htons(port);
415 Socket.sin_family = PF_INET;
416
417 /* Now connect to the destination */
418
419 if (connect(fd, (struct sockaddr *) &Socket, sizeof(Socket)) < 0) { /* Error */
420
421 close(fd);
422 RFCNB_errno = RFCNBE_ConnectFailed;
423 RFCNB_saved_errno = errno;
424 return (RFCNBE_Bad);
425 }
426 return (fd);
427
428}
429
430/* handle the details of establishing the RFCNB session with remote
431 * end
432 *
433 */
434
435int
436RFCNB_Session_Req(struct RFCNB_Con *con,
437 char *Called_Name,
438 char *Calling_Name,
439 BOOL * redirect,
440 struct in_addr *Dest_IP,
441 int *port)
442{
443 char *sess_pkt;
444
445 /* Response packet should be no more than 9 bytes, make 16 jic */
446
447 char resp[16];
448 int len;
449 struct RFCNB_Pkt *pkt, res_pkt;
450
451 /* We build and send the session request, then read the response */
452
453 pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Sess_Len);
454
455 if (pkt == NULL) {
456
457 return (RFCNBE_Bad); /* Leave the error that RFCNB_Alloc_Pkt gives) */
458
459 }
460 sess_pkt = pkt->data; /* Get pointer to packet proper */
461
462 sess_pkt[RFCNB_Pkt_Type_Offset] = RFCNB_SESSION_REQUEST;
463 RFCNB_Put_Pkt_Len(sess_pkt, (RFCNB_Pkt_Sess_Len - RFCNB_Pkt_Hdr_Len));
464 sess_pkt[RFCNB_Pkt_N1Len_Offset] = 32;
465 sess_pkt[RFCNB_Pkt_N2Len_Offset] = 32;
466
467 RFCNB_CvtPad_Name(Called_Name, (sess_pkt + RFCNB_Pkt_Called_Offset));
468 RFCNB_CvtPad_Name(Calling_Name, (sess_pkt + RFCNB_Pkt_Calling_Offset));
469
470 /* Now send the packet */
471
472#ifdef RFCNB_DEBUG
473
474 fprintf(stderr, "Sending packet: ");
475
476#endif
477
478 if ((len = RFCNB_Put_Pkt(con, pkt, RFCNB_Pkt_Sess_Len)) < 0) {
479
480 return (RFCNBE_Bad); /* Should be able to write that lot ... */
481
482 }
483#ifdef RFCNB_DEBUG
484
485 fprintf(stderr, "Getting packet.\n");
486
487#endif
488
489 res_pkt.data = resp;
490 res_pkt.len = sizeof(resp);
491 res_pkt.next = NULL;
492
493 if ((len = RFCNB_Get_Pkt(con, &res_pkt, sizeof(resp))) < 0) {
494
495 return (RFCNBE_Bad);
496
497 }
498 /* Now analyze the packet ... */
499
500 switch (RFCNB_Pkt_Type(resp)) {
501
502 case RFCNB_SESSION_REJ: /* Didnt like us ... too bad */
503
504 /* Why did we get rejected ? */
505
506 switch (CVAL(resp, RFCNB_Pkt_Error_Offset)) {
507
508 case 0x80:
509 RFCNB_errno = RFCNBE_CallRejNLOCN;
510 break;
511 case 0x81:
512 RFCNB_errno = RFCNBE_CallRejNLFCN;
513 break;
514 case 0x82:
515 RFCNB_errno = RFCNBE_CallRejCNNP;
516 break;
517 case 0x83:
518 RFCNB_errno = RFCNBE_CallRejInfRes;
519 break;
520 case 0x8F:
521 RFCNB_errno = RFCNBE_CallRejUnSpec;
522 break;
523 default:
524 RFCNB_errno = RFCNBE_ProtErr;
525 break;
526 }
527
528 return (RFCNBE_Bad);
529 break;
530
531 case RFCNB_SESSION_ACK: /* Got what we wanted ... */
532
533 return (0);
534 break;
535
536 case RFCNB_SESSION_RETARGET: /* Go elsewhere */
537
538 *redirect = TRUE; /* Copy port and ip addr */
539
540 memcpy(Dest_IP, (resp + RFCNB_Pkt_IP_Offset), sizeof(struct in_addr));
541 *port = SVAL(resp, RFCNB_Pkt_Port_Offset);
542
543 return (0);
544 break;
545
546 default: /* A protocol error */
547
548 RFCNB_errno = RFCNBE_ProtErr;
549 return (RFCNBE_Bad);
550 break;
551 }
552}