~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to helpers/basic_auth/MSNT/rfcnb-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2010-05-04 11:15:49 UTC
  • mfrom: (1.3.1 upstream)
  • mto: (20.3.1 squeeze) (21.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100504111549-1apjh2g5sndki4te
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* UNIX RFCNB (RFC1001/RFC1002) NetBIOS implementation
2
 
 * 
 
2
 *
3
3
 * Version 1.0
4
4
 * RFCNB Utility Routines ...
5
 
 * 
 
5
 *
6
6
 * Copyright (C) Richard Sharpe 1996
7
 
 * 
 
7
 *
8
8
 */
9
9
 
10
10
/*
12
12
 * it under the terms of the GNU General Public License as published by
13
13
 * the Free Software Foundation; either version 2 of the License, or
14
14
 * (at your option) any later version.
15
 
 * 
 
15
 *
16
16
 * This program is distributed in the hope that it will be useful,
17
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
19
 * GNU General Public License for more details.
20
 
 * 
 
20
 *
21
21
 * You should have received a copy of the GNU General Public License
22
22
 * along with this program; if not, write to the Free Software
23
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36
36
#include <arpa/inet.h>
37
37
#include <string.h>
38
38
 
39
 
const char *RFCNB_Error_Strings[] =
40
 
{
 
39
const char *RFCNB_Error_Strings[] = {
41
40
 
42
41
    "RFCNBE_OK: Routine completed successfully.",
43
42
    "RFCNBE_NoSpace: No space available for a malloc call.",
77
76
 
78
77
    for (i = 0; i < 16; i++) {
79
78
 
80
 
        if (i >= len) {
81
 
 
82
 
            c1 = 'C';
83
 
            c2 = 'A';           /* CA is a space */
84
 
 
85
 
        } else {
86
 
 
87
 
            c = name1[i];
88
 
            c1 = (char) ((int) c / 16 + (int) 'A');
89
 
            c2 = (char) ((int) c % 16 + (int) 'A');
90
 
        }
91
 
 
92
 
        name2[i * 2] = c1;
93
 
        name2[i * 2 + 1] = c2;
 
79
        if (i >= len) {
 
80
 
 
81
            c1 = 'C';
 
82
            c2 = 'A';           /* CA is a space */
 
83
 
 
84
        } else {
 
85
 
 
86
            c = name1[i];
 
87
            c1 = (char) ((int) c / 16 + (int) 'A');
 
88
            c2 = (char) ((int) c % 16 + (int) 'A');
 
89
        }
 
90
 
 
91
        name2[i * 2] = c1;
 
92
        name2[i * 2 + 1] = c2;
94
93
 
95
94
    }
96
95
 
101
100
/* Converts an Ascii NB Name (16 chars) to an RFCNB Name (32 chars)
102
101
 * Uses the encoding in RFC1001. Each nibble of byte is added to 'A'
103
102
 * to produce the next byte in the name.
104
 
 * 
105
 
 * This routine assumes that AName is 16 bytes long and that NBName has 
106
 
 * space for 32 chars, so be careful ... 
107
 
 * 
 
103
 *
 
104
 * This routine assumes that AName is 16 bytes long and that NBName has
 
105
 * space for 32 chars, so be careful ...
 
106
 *
108
107
 */
109
108
 
110
109
void
115
114
 
116
115
    for (i = 0; i < 16; i++) {
117
116
 
118
 
        c = AName[i];
119
 
 
120
 
        c1 = (char) ((c >> 4) + 'A');
121
 
        c2 = (char) ((c & 0xF) + 'A');
122
 
 
123
 
        NBName[i * 2] = c1;
124
 
        NBName[i * 2 + 1] = c2;
 
117
        c = AName[i];
 
118
 
 
119
        c1 = (char) ((c >> 4) + 'A');
 
120
        c2 = (char) ((c & 0xF) + 'A');
 
121
 
 
122
        NBName[i * 2] = c1;
 
123
        NBName[i * 2 + 1] = c2;
125
124
    }
126
125
 
127
126
    NBName[32] = 0;             /* Put in a null */
138
137
 
139
138
    for (i = 0; i < 16; i++) {
140
139
 
141
 
        c1 = NBName[i * 2];
142
 
        c2 = NBName[i * 2 + 1];
143
 
 
144
 
        c = (char) (((int) c1 - (int) 'A') * 16 + ((int) c2 - (int) 'A'));
145
 
 
146
 
        AName[i] = c;
 
140
        c1 = NBName[i * 2];
 
141
        c2 = NBName[i * 2 + 1];
 
142
 
 
143
        c = (char) (((int) c1 - (int) 'A') * 16 + ((int) c2 - (int) 'A'));
 
144
 
 
145
        AName[i] = c;
147
146
 
148
147
    }
149
148
 
169
168
 
170
169
    while (pkt_ptr != NULL) {
171
170
 
172
 
        for (i = 0;
173
 
            i < ((Len > (pkt_ptr->len) ? pkt_ptr->len : Len) - Offset);
174
 
            i++) {
175
 
 
176
 
            c = pkt_ptr->data[i + Offset];
177
 
            c1 = Hex_List[c >> 4];
178
 
            c2 = Hex_List[c & 0xF];
179
 
 
180
 
            outbuf1[j++] = c1;
181
 
            outbuf1[j++] = c2;
182
 
 
183
 
            if (j == 32) {      /* Print and reset */
184
 
                outbuf1[j] = 0;
185
 
                fprintf(fd, "    %s\n", outbuf1);
186
 
                j = 0;
187
 
            }
188
 
        }
189
 
 
190
 
        Offset = 0;
191
 
        Len = Len - pkt_ptr->len;       /* Reduce amount by this much */
192
 
        pkt_ptr = pkt_ptr->next;
 
171
        for (i = 0;
 
172
                i < ((Len > (pkt_ptr->len) ? pkt_ptr->len : Len) - Offset);
 
173
                i++) {
 
174
 
 
175
            c = pkt_ptr->data[i + Offset];
 
176
            c1 = Hex_List[c >> 4];
 
177
            c2 = Hex_List[c & 0xF];
 
178
 
 
179
            outbuf1[j++] = c1;
 
180
            outbuf1[j++] = c2;
 
181
 
 
182
            if (j == 32) {      /* Print and reset */
 
183
                outbuf1[j] = 0;
 
184
                fprintf(fd, "    %s\n", outbuf1);
 
185
                j = 0;
 
186
            }
 
187
        }
 
188
 
 
189
        Offset = 0;
 
190
        Len = Len - pkt_ptr->len;       /* Reduce amount by this much */
 
191
        pkt_ptr = pkt_ptr->next;
193
192
 
194
193
    }
195
194
 
197
196
 
198
197
    if (j > 0) {
199
198
 
200
 
        outbuf1[j] = 0;
201
 
        fprintf(fd, "    %s\n", outbuf1);
 
199
        outbuf1[j] = 0;
 
200
        fprintf(fd, "    %s\n", outbuf1);
202
201
 
203
202
    }
204
203
    fprintf(fd, "\n");
209
208
/* Get a packet of size n */
210
209
 
211
210
struct RFCNB_Pkt *
212
 
RFCNB_Alloc_Pkt(int n)
213
 
{
 
211
RFCNB_Alloc_Pkt(int n) {
214
212
    RFCNB_Pkt *pkt;
215
213
 
216
214
    if ((pkt = malloc(sizeof(struct RFCNB_Pkt))) == NULL) {
217
 
        RFCNB_errno = RFCNBE_NoSpace;
218
 
        RFCNB_saved_errno = errno;
219
 
        return (NULL);
 
215
        RFCNB_errno = RFCNBE_NoSpace;
 
216
        RFCNB_saved_errno = errno;
 
217
        return (NULL);
220
218
    }
221
219
    pkt->next = NULL;
222
220
    pkt->len = n;
223
221
 
224
222
    if (n == 0)
225
 
        return (pkt);
 
223
        return (pkt);
226
224
 
227
225
    if ((pkt->data = malloc(n)) == NULL) {
228
 
        RFCNB_errno = RFCNBE_NoSpace;
229
 
        RFCNB_saved_errno = errno;
230
 
        free(pkt);
231
 
        return (NULL);
 
226
        RFCNB_errno = RFCNBE_NoSpace;
 
227
        RFCNB_saved_errno = errno;
 
228
        free(pkt);
 
229
        return (NULL);
232
230
    }
233
231
    return (pkt);
234
232
 
243
241
 
244
242
    while (pkt != NULL) {
245
243
 
246
 
        pkt_next = pkt->next;
247
 
 
248
 
        if (pkt->data != NULL)
249
 
            free(pkt->data);
250
 
 
251
 
        free(pkt);
252
 
 
253
 
        pkt = pkt_next;
 
244
        pkt_next = pkt->next;
 
245
 
 
246
        if (pkt->data != NULL)
 
247
            free(pkt->data);
 
248
 
 
249
        free(pkt);
 
250
 
 
251
        pkt = pkt_next;
254
252
 
255
253
    }
256
254
 
273
271
 
274
272
    case RFCNB_SESSION_MESSAGE:
275
273
 
276
 
        fprintf(fd, "SESSION MESSAGE: Length = %i\n", RFCNB_Pkt_Len(pkt->data));
277
 
        RFCNB_Print_Hex(fd, pkt, RFCNB_Pkt_Hdr_Len,
 
274
        fprintf(fd, "SESSION MESSAGE: Length = %i\n", RFCNB_Pkt_Len(pkt->data));
 
275
        RFCNB_Print_Hex(fd, pkt, RFCNB_Pkt_Hdr_Len,
278
276
#ifdef RFCNB_PRINT_DATA
279
 
            RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
 
277
                        RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
280
278
#else
281
 
            40);
 
279
                        40);
282
280
#endif
283
281
 
284
 
        if (Prot_Print_Routine != 0) {  /* Print the rest of the packet */
285
 
 
286
 
            Prot_Print_Routine(fd, strcmp(dirn, "sent"), pkt, RFCNB_Pkt_Hdr_Len,
287
 
                RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
288
 
 
289
 
        }
290
 
        break;
 
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;
291
289
 
292
290
    case RFCNB_SESSION_REQUEST:
293
291
 
294
 
        fprintf(fd, "SESSION REQUEST: Length = %i\n",
295
 
            RFCNB_Pkt_Len(pkt->data));
296
 
        RFCNB_NBName_To_AName((char *) (pkt->data + RFCNB_Pkt_Called_Offset), lname);
297
 
        fprintf(fd, "  Called Name: %s\n", lname);
298
 
        RFCNB_NBName_To_AName((char *) (pkt->data + RFCNB_Pkt_Calling_Offset), lname);
299
 
        fprintf(fd, "  Calling Name: %s\n", lname);
 
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);
300
298
 
301
 
        break;
 
299
        break;
302
300
 
303
301
    case RFCNB_SESSION_ACK:
304
302
 
305
 
        fprintf(fd, "RFCNB SESSION ACK: Length = %i\n",
306
 
            RFCNB_Pkt_Len(pkt->data));
 
303
        fprintf(fd, "RFCNB SESSION ACK: Length = %i\n",
 
304
                RFCNB_Pkt_Len(pkt->data));
307
305
 
308
 
        break;
 
306
        break;
309
307
 
310
308
    case RFCNB_SESSION_REJ:
311
 
        fprintf(fd, "RFCNB SESSION REJECT: Length = %i\n",
312
 
            RFCNB_Pkt_Len(pkt->data));
313
 
 
314
 
        if (RFCNB_Pkt_Len(pkt->data) < 1) {
315
 
            fprintf(fd, "   Protocol Error, short Reject packet!\n");
316
 
        } else {
317
 
            fprintf(fd, "   Error = %x\n", CVAL(pkt->data, RFCNB_Pkt_Error_Offset));
318
 
        }
319
 
 
320
 
        break;
 
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;
321
319
 
322
320
    case RFCNB_SESSION_RETARGET:
323
321
 
324
 
        fprintf(fd, "RFCNB SESSION RETARGET: Length = %i\n",
325
 
            RFCNB_Pkt_Len(pkt->data));
326
 
 
327
 
        /* Print out the IP address etc and the port? */
328
 
 
329
 
        break;
 
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;
330
328
 
331
329
    case RFCNB_SESSION_KEEP_ALIVE:
332
330
 
333
 
        fprintf(fd, "RFCNB SESSION KEEP ALIVE: Length = %i\n",
334
 
            RFCNB_Pkt_Len(pkt->data));
335
 
        break;
 
331
        fprintf(fd, "RFCNB SESSION KEEP ALIVE: Length = %i\n",
 
332
                RFCNB_Pkt_Len(pkt->data));
 
333
        break;
336
334
 
337
335
    default:
338
336
 
339
 
        break;
 
337
        break;
340
338
    }
341
339
 
342
340
}
354
352
 
355
353
    if ((addr = inet_addr(host)) == INADDR_NONE) {      /* Oh well, a good try :-) */
356
354
 
357
 
        /* Now try a name look up with gethostbyname */
358
 
 
359
 
        if ((hp = gethostbyname(host)) == NULL) {       /* Not in DNS */
360
 
 
361
 
            /* Try NetBIOS name lookup, how the hell do we do that? */
362
 
            RFCNB_errno = RFCNBE_BadName;       /* Is this right? */
363
 
            RFCNB_saved_errno = errno;
364
 
            return (RFCNBE_Bad);
365
 
 
366
 
        } else { /* We got a name */
 
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
            RFCNB_errno = RFCNBE_BadName;       /* Is this right? */
 
361
            RFCNB_saved_errno = errno;
 
362
            return (RFCNBE_Bad);
 
363
 
 
364
        } else { /* We got a name */
367
365
            Dest_IP->s_addr = (*((struct in_addr*)hp->h_addr_list[0])).s_addr;
368
 
        }
 
366
        }
369
367
    } else { /* It was an IP address */
370
 
        Dest_IP->s_addr = addr;
 
368
        Dest_IP->s_addr = addr;
371
369
    }
372
370
 
373
371
    return 0;
377
375
/* Disconnect the TCP connection to the server */
378
376
 
379
377
int
380
 
RFCNB_Close(int socket)
 
378
RFCNB_Close(int aSocket)
381
379
{
382
380
 
383
 
    close(socket);
 
381
    close(aSocket);
384
382
 
385
383
    /* If we want to do error recovery, here is where we put it */
386
384
 
401
399
 
402
400
    if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {   /* Handle the error */
403
401
 
404
 
        RFCNB_errno = RFCNBE_BadSocket;
405
 
        RFCNB_saved_errno = errno;
406
 
        return (RFCNBE_Bad);
 
402
        RFCNB_errno = RFCNBE_BadSocket;
 
403
        RFCNB_saved_errno = errno;
 
404
        return (RFCNBE_Bad);
407
405
    }
408
406
    memset((char *) &Socket, 0, sizeof(Socket));
409
407
    memcpy((char *) &Socket.sin_addr, (char *) &Dest_IP, sizeof(Dest_IP));
415
413
 
416
414
    if (connect(fd, (struct sockaddr *) &Socket, sizeof(Socket)) < 0) {         /* Error */
417
415
 
418
 
        close(fd);
419
 
        RFCNB_errno = RFCNBE_ConnectFailed;
420
 
        RFCNB_saved_errno = errno;
421
 
        return (RFCNBE_Bad);
 
416
        close(fd);
 
417
        RFCNB_errno = RFCNBE_ConnectFailed;
 
418
        RFCNB_saved_errno = errno;
 
419
        return (RFCNBE_Bad);
422
420
    }
423
421
    return (fd);
424
422
 
425
423
}
426
424
 
427
 
/* handle the details of establishing the RFCNB session with remote 
428
 
 * end 
429
 
 * 
 
425
/* handle the details of establishing the RFCNB session with remote
 
426
 * end
 
427
 *
430
428
 */
431
429
 
432
430
int
433
431
RFCNB_Session_Req(struct RFCNB_Con *con,
434
 
    char *Called_Name,
435
 
    char *Calling_Name,
436
 
    BOOL * redirect,
437
 
    struct in_addr *Dest_IP,
438
 
    int *port)
 
432
                  char *Called_Name,
 
433
                  char *Calling_Name,
 
434
                  BOOL * redirect,
 
435
                  struct in_addr *Dest_IP,
 
436
                  int *port)
439
437
{
440
438
    char *sess_pkt;
441
439
 
451
449
 
452
450
    if (pkt == NULL) {
453
451
 
454
 
        return (RFCNBE_Bad);    /* Leave the error that RFCNB_Alloc_Pkt gives) */
 
452
        return (RFCNBE_Bad);    /* Leave the error that RFCNB_Alloc_Pkt gives) */
455
453
 
456
454
    }
457
455
    sess_pkt = pkt->data;       /* Get pointer to packet proper */
474
472
 
475
473
    if ((len = RFCNB_Put_Pkt(con, pkt, RFCNB_Pkt_Sess_Len)) < 0) {
476
474
 
477
 
        return (RFCNBE_Bad);    /* Should be able to write that lot ... */
 
475
        return (RFCNBE_Bad);    /* Should be able to write that lot ... */
478
476
 
479
477
    }
480
478
#ifdef RFCNB_DEBUG
489
487
 
490
488
    if ((len = RFCNB_Get_Pkt(con, &res_pkt, sizeof(resp))) < 0) {
491
489
 
492
 
        return (RFCNBE_Bad);
 
490
        return (RFCNBE_Bad);
493
491
 
494
492
    }
495
493
    /* Now analyze the packet ... */
498
496
 
499
497
    case RFCNB_SESSION_REJ:     /* Didnt like us ... too bad */
500
498
 
501
 
        /* Why did we get rejected ? */
502
 
 
503
 
        switch (CVAL(resp, RFCNB_Pkt_Error_Offset)) {
504
 
 
505
 
        case 0x80:
506
 
            RFCNB_errno = RFCNBE_CallRejNLOCN;
507
 
            break;
508
 
        case 0x81:
509
 
            RFCNB_errno = RFCNBE_CallRejNLFCN;
510
 
            break;
511
 
        case 0x82:
512
 
            RFCNB_errno = RFCNBE_CallRejCNNP;
513
 
            break;
514
 
        case 0x83:
515
 
            RFCNB_errno = RFCNBE_CallRejInfRes;
516
 
            break;
517
 
        case 0x8F:
518
 
            RFCNB_errno = RFCNBE_CallRejUnSpec;
519
 
            break;
520
 
        default:
521
 
            RFCNB_errno = RFCNBE_ProtErr;
522
 
            break;
523
 
        }
524
 
 
525
 
        return (RFCNBE_Bad);
526
 
        break;
 
499
        /* Why did we get rejected ? */
 
500
 
 
501
        switch (CVAL(resp, RFCNB_Pkt_Error_Offset)) {
 
502
 
 
503
        case 0x80:
 
504
            RFCNB_errno = RFCNBE_CallRejNLOCN;
 
505
            break;
 
506
        case 0x81:
 
507
            RFCNB_errno = RFCNBE_CallRejNLFCN;
 
508
            break;
 
509
        case 0x82:
 
510
            RFCNB_errno = RFCNBE_CallRejCNNP;
 
511
            break;
 
512
        case 0x83:
 
513
            RFCNB_errno = RFCNBE_CallRejInfRes;
 
514
            break;
 
515
        case 0x8F:
 
516
            RFCNB_errno = RFCNBE_CallRejUnSpec;
 
517
            break;
 
518
        default:
 
519
            RFCNB_errno = RFCNBE_ProtErr;
 
520
            break;
 
521
        }
 
522
 
 
523
        return (RFCNBE_Bad);
 
524
        break;
527
525
 
528
526
    case RFCNB_SESSION_ACK:     /* Got what we wanted ...      */
529
527
 
530
 
        return (0);
531
 
        break;
 
528
        return (0);
 
529
        break;
532
530
 
533
531
    case RFCNB_SESSION_RETARGET:        /* Go elsewhere                */
534
532
 
535
 
        *redirect = TRUE;       /* Copy port and ip addr       */
536
 
 
537
 
        memcpy(Dest_IP, (resp + RFCNB_Pkt_IP_Offset), sizeof(struct in_addr));
538
 
        *port = SVAL(resp, RFCNB_Pkt_Port_Offset);
539
 
 
540
 
        return (0);
541
 
        break;
 
533
        *redirect = TRUE;       /* Copy port and ip addr       */
 
534
 
 
535
        memcpy(Dest_IP, (resp + RFCNB_Pkt_IP_Offset), sizeof(struct in_addr));
 
536
        *port = SVAL(resp, RFCNB_Pkt_Port_Offset);
 
537
 
 
538
        return (0);
 
539
        break;
542
540
 
543
541
    default:                    /* A protocol error */
544
542
 
545
 
        RFCNB_errno = RFCNBE_ProtErr;
546
 
        return (RFCNBE_Bad);
547
 
        break;
 
543
        RFCNB_errno = RFCNBE_ProtErr;
 
544
        return (RFCNBE_Bad);
 
545
        break;
548
546
    }
549
547
}