File: smblib-util.c

package info (click to toggle)
squid3 3.4.8-6
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 31,084 kB
  • sloc: cpp: 165,325; ansic: 21,998; sh: 12,166; makefile: 5,964; perl: 2,153; sql: 322; awk: 118
file content (821 lines) | stat: -rw-r--r-- 22,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#include "squid.h"

/* UNIX SMBlib NetBIOS implementation

   Version 1.0
   SMBlib Utility Routines

   Copyright (C) Richard Sharpe 1996

*/

/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "smblib/smblib.h"
#include "smblib/smblib-priv.h"
#include "rfcnb/rfcnb.h"

#if HAVE_STRING_H
#include <string.h>
#endif

int SMB_Types[] = {SMB_P_Core,
                   SMB_P_CorePlus,
                   SMB_P_DOSLanMan1,
                   SMB_P_DOSLanMan1,
                   SMB_P_LanMan1,
                   SMB_P_DOSLanMan2,
                   SMB_P_LanMan2,
                   SMB_P_LanMan2_1,
                   SMB_P_LanMan2_1,
                   SMB_P_NT1,
                   SMB_P_NT1,
                   SMB_P_NT1,
                   -1
                  };

#if UNDEFINED
char *SMB_DOSTimToStr(int DOS_time);
char *SMB_AtrToStr(int attribs, BOOL verbose);
int SMB_Get_Tree_MBS(SMB_Tree_Handle tree);
int SMB_Get_Max_Buf_Siz(SMB_Handle_Type Con_Handle);
int SMB_Get_Protocol_IDX(SMB_Handle_Type Con_Handle);
#endif /* UNDEFINED */
int SMB_Get_Protocol(SMB_Handle_Type Con_Handle);
int SMB_Figure_Protocol(const char *dialects[], int prot_index);
int SMB_TreeDisconnect(SMB_Tree_Handle Tree_Handle, BOOL discard);

#if UNDEFINED
/* Print out an SMB pkt in all its gory detail ... */

void SMB_Print_Pkt(FILE fd, RFCNB_Pkt *pkt, BOOL command, int Offset, int Len)

{

    /* Well, just how do we do this ... print it I suppose */

    /* Print out the SMB header ...                        */

    /* Print the command                                   */

    /* Print the other bits in the header                  */

    /* etc                                                 */

}
#endif

/* Convert a DOS Date_Time to a local host type date time for printing */

#if UNDEFINED
char *SMB_DOSTimToStr(int DOS_time)

{
    static char SMB_Time_Temp[48];
    int DOS_sec, DOS_min, DOS_hour, DOS_day, DOS_month, DOS_year;

    SMB_Time_Temp[0] = 0;

    DOS_sec   = (DOS_time & 0x001F) * 2;
    DOS_min   = (DOS_time & 0x07E0) >> 5;
    DOS_hour  = ((DOS_time & 0xF800) >> 11);

    DOS_day   = (DOS_time & 0x001F0000) >> 16;
    DOS_month = (DOS_time & 0x01E00000) >> 21;
    DOS_year  = ((DOS_time & 0xFE000000) >> 25) + 80;

    snprintf(SMB_Time_Temp, 48, "%2d/%02d/%2d %2d:%02d:%02d", DOS_day, DOS_month,
             DOS_year, DOS_hour, DOS_min, DOS_sec);

    return(SMB_Time_Temp);

}

/* Convert an attribute byte/word etc to a string ... We return a pointer
   to a static string which we guarantee is long enough. If verbose is
   true, we print out long form of strings ...                            */

char *SMB_AtrToStr(int attribs, BOOL verbose)

{
    static char SMB_Attrib_Temp[128];

    SMB_Attrib_Temp[0] = 0;

    if (attribs & SMB_FA_ROF)
        strcat(SMB_Attrib_Temp, (verbose?"Read Only ":"R"));

    if (attribs & SMB_FA_HID)
        strcat(SMB_Attrib_Temp, (verbose?"Hidden ":"H"));

    if (attribs & SMB_FA_SYS)
        strcat(SMB_Attrib_Temp, (verbose?"System ":"S"));

    if (attribs & SMB_FA_VOL)
        strcat(SMB_Attrib_Temp, (verbose?"Volume ":"V"));

    if (attribs & SMB_FA_DIR)
        strcat(SMB_Attrib_Temp, (verbose?"Directory ":"D"));

    if (attribs & SMB_FA_ARC)
        strcat(SMB_Attrib_Temp, (verbose?"Archive ":"A"));

    return(SMB_Attrib_Temp);

}

/* Pick up the Max Buffer Size from the Tree Structure ... */

int SMB_Get_Tree_MBS(SMB_Tree_Handle tree)

{
    if (tree != NULL) {
        return(tree -> mbs);
    } else {
        return(SMBlibE_BAD);
    }
}

/* Pick up the Max buffer size */

int SMB_Get_Max_Buf_Siz(SMB_Handle_Type Con_Handle)

{
    if (Con_Handle != NULL) {
        return(Con_Handle -> max_xmit);
    } else {
        return(SMBlibE_BAD);
    }

}
/* Pickup the protocol index from the connection structure                 */

int SMB_Get_Protocol_IDX(SMB_Handle_Type Con_Handle)

{
    if (Con_Handle != NULL) {
        return(Con_Handle -> prot_IDX);
    } else {
        return(0xFFFF);  /* Invalid protocol */
    }

}
#endif /* UNDEFINED */

/* Pick up the protocol from the connection structure                       */

int SMB_Get_Protocol(SMB_Handle_Type Con_Handle)

{
    if (Con_Handle != NULL) {
        return(Con_Handle -> protocol);
    } else {
        return(0xFFFF);  /* Invalid protocol */
    }

}

/* Figure out what protocol was accepted, given the list of dialect strings */
/* We offered, and the index back from the server. We allow for a user      */
/* supplied list, and assume that it is a subset of our list                */

int SMB_Figure_Protocol(const char *dialects[], int prot_index)

{
    int i;

    if (dialects == SMB_Prots) { /* The jobs is easy, just index into table */

        return(SMB_Types[prot_index]);
    } else { /* Search through SMB_Prots looking for a match */

        for (i = 0; SMB_Prots[i] != NULL; i++) {

            if (strcmp(dialects[prot_index], SMB_Prots[i]) == 0) { /* A match */

                return(SMB_Types[i]);

            }

        }

        /* If we got here, then we are in trouble, because the protocol was not */
        /* One we understand ...                                                */

        return(SMB_P_Unknown);

    }

}

/* Negotiate the protocol we will use from the list passed in Prots       */
/* we return the index of the accepted protocol in NegProt, -1 indicates  */
/* none acceptible, and our return value is 0 if ok, <0 if problems       */

int SMB_Negotiate(SMB_Handle_Type Con_Handle, const char *Prots[])

{
//    struct SMB_Neg_Prot_Def *prot_pkt;
//    struct SMB_Neg_Prot_Resp_Def *resp_pkt;
    struct RFCNB_Pkt *pkt;
    int prots_len, i, pkt_len, prot, alloc_len;
    char *p;

    /* Figure out how long the prot list will be and allocate space for it */

    prots_len = 0;

    for (i = 0; Prots[i] != NULL; i++) {

        prots_len = prots_len + strlen(Prots[i]) + 2; /* Account for null etc */

    }

    /* The -1 accounts for the one byte smb_buf we have because some systems */
    /* don't like char msg_buf[]                                             */

    pkt_len = SMB_negp_len + prots_len;

    /* Make sure that the pkt len is long enough for the max response ...   */
    /* Which is a problem, because the encryption key len eec may be long   */

    if (pkt_len < (SMB_hdr_wct_offset + (19 * 2) + 40)) {

        alloc_len = SMB_hdr_wct_offset + (19 * 2) + 40;

    } else {

        alloc_len = pkt_len;

    }

    pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(alloc_len);

    if (pkt == NULL) {

        SMBlib_errno = SMBlibE_NoSpace;
        return(SMBlibE_BAD);

    }

    /* Now plug in the bits we need */

    memset(SMB_Hdr(pkt), 0, SMB_negp_len);
    SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF);  /* Plunk in IDF */
    *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBnegprot;
    SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Con_Handle -> pid);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, 0);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Con_Handle -> mid);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Con_Handle -> uid);
    *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0;

    SSVAL(SMB_Hdr(pkt), SMB_negp_bcc_offset, prots_len);

    /* Now copy the prot strings in with the right stuff */

    p = (char *)(SMB_Hdr(pkt) + SMB_negp_buf_offset);

    for (i = 0; Prots[i] != NULL; i++) {

        *p = SMBdialectID;
        strcpy(p + 1, Prots[i]);
        p = p + strlen(Prots[i]) + 2; /* Adjust len of p for null plus dialectID */

    }

    /* Now send the packet and sit back ... */

    if (RFCNB_Send(Con_Handle -> Trans_Connect, pkt, pkt_len) < 0) {

#ifdef DEBUG
        fprintf(stderr, "Error sending negotiate protocol\n");
#endif

        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = -SMBlibE_SendFailed; /* Failed, check lower layer errno */
        return(SMBlibE_BAD);

    }

    /* Now get the response ... */

    if (RFCNB_Recv(Con_Handle -> Trans_Connect, pkt, alloc_len) < 0) {

#ifdef DEBUG
        fprintf(stderr, "Error receiving response to negotiate\n");
#endif

        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = -SMBlibE_RecvFailed; /* Failed, check lower layer errno */
        return(SMBlibE_BAD);

    }

    if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) {  /* Process error */

#ifdef DEBUG
        fprintf(stderr, "SMB_Negotiate failed with errorclass = %i, Error Code = %i\n",
                CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset),
                SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset));
#endif

        SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset);
        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = SMBlibE_Remote;
        return(SMBlibE_BAD);

    }

    if (SVAL(SMB_Hdr(pkt), SMB_negrCP_idx_offset) == 0xFFFF) {

#ifdef DEBUG
        fprintf(stderr, "None of our protocols was accepted ... ");
#endif

        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = SMBlibE_NegNoProt;
        return(SMBlibE_BAD);

    }

    /* Now, unpack the info from the response, if any and evaluate the proto */
    /* selected. We must make sure it is one we like ...                     */

    Con_Handle -> prot_IDX = prot = SVAL(SMB_Hdr(pkt), SMB_negrCP_idx_offset);
    Con_Handle -> protocol = SMB_Figure_Protocol(Prots, prot);

    if (Con_Handle -> protocol == SMB_P_Unknown) { /* No good ... */

        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = SMBlibE_ProtUnknown;
        return(SMBlibE_BAD);

    }

    switch (CVAL(SMB_Hdr(pkt), SMB_hdr_wct_offset)) {

    case 0x01:      /* No more info ... */

        break;

    case 13:        /* Up to and including LanMan 2.1 */

        Con_Handle -> Security = SVAL(SMB_Hdr(pkt), SMB_negrLM_sec_offset);
        Con_Handle -> encrypt_passwords = ((Con_Handle -> Security & SMB_sec_encrypt_mask) != 0x00);
        Con_Handle -> Security = Con_Handle -> Security & SMB_sec_user_mask;

        Con_Handle -> max_xmit = SVAL(SMB_Hdr(pkt), SMB_negrLM_mbs_offset);
        Con_Handle -> MaxMPX = SVAL(SMB_Hdr(pkt), SMB_negrLM_mmc_offset);
        Con_Handle -> MaxVC = SVAL(SMB_Hdr(pkt), SMB_negrLM_mnv_offset);
        Con_Handle -> Raw_Support = SVAL(SMB_Hdr(pkt), SMB_negrLM_rm_offset);
        Con_Handle -> SessionKey = IVAL(SMB_Hdr(pkt), SMB_negrLM_sk_offset);
        Con_Handle -> SvrTZ = SVAL(SMB_Hdr(pkt), SMB_negrLM_stz_offset);
        Con_Handle -> Encrypt_Key_Len = SVAL(SMB_Hdr(pkt), SMB_negrLM_ekl_offset);

        p = (SMB_Hdr(pkt) + SMB_negrLM_buf_offset);
        memcpy(Con_Handle->Encrypt_Key, p, 8);

        p = (SMB_Hdr(pkt) + SMB_negrLM_buf_offset + Con_Handle -> Encrypt_Key_Len);

        strncpy(p, Con_Handle -> Svr_PDom, sizeof(Con_Handle -> Svr_PDom) - 1);

        break;

    case 17:        /* NT LM 0.12 and LN LM 1.0 */

        Con_Handle -> Security = SVAL(SMB_Hdr(pkt), SMB_negrNTLM_sec_offset);
        Con_Handle -> encrypt_passwords = ((Con_Handle -> Security & SMB_sec_encrypt_mask) != 0x00);
        Con_Handle -> Security = Con_Handle -> Security & SMB_sec_user_mask;

        Con_Handle -> max_xmit = IVAL(SMB_Hdr(pkt), SMB_negrNTLM_mbs_offset);
        Con_Handle -> MaxMPX = SVAL(SMB_Hdr(pkt), SMB_negrNTLM_mmc_offset);
        Con_Handle -> MaxVC = SVAL(SMB_Hdr(pkt), SMB_negrNTLM_mnv_offset);
        Con_Handle -> MaxRaw = IVAL(SMB_Hdr(pkt), SMB_negrNTLM_mrs_offset);
        Con_Handle -> SessionKey = IVAL(SMB_Hdr(pkt), SMB_negrNTLM_sk_offset);
        Con_Handle -> SvrTZ = SVAL(SMB_Hdr(pkt), SMB_negrNTLM_stz_offset);
        Con_Handle -> Encrypt_Key_Len = CVAL(SMB_Hdr(pkt), SMB_negrNTLM_ekl_offset);

        p = (SMB_Hdr(pkt) + SMB_negrNTLM_buf_offset);
        memcpy(Con_Handle->Encrypt_Key, p, 8);

        p = (SMB_Hdr(pkt) + SMB_negrLM_buf_offset + Con_Handle -> Encrypt_Key_Len);

        strncpy(p, Con_Handle -> Svr_PDom, sizeof(Con_Handle -> Svr_PDom) - 1);

        break;

    default:

#ifdef DEBUG
        fprintf(stderr, "Unknown NegProt response format ... Ignored\n");
        fprintf(stderr, "  wct = %i\n", CVAL(SMB_Hdr(pkt), SMB_hdr_wct_offset));
#endif

        break;
    }

#ifdef DEBUG
    fprintf(stderr, "Protocol selected is: %i:%s\n", prot, Prots[prot]);
#endif

    RFCNB_Free_Pkt(pkt);
    return(0);

}

/* Get our hostname */

void SMB_Get_My_Name(char *name, int len)

{
    if (gethostname(name, len) < 0) { /* Error getting name */

        strncpy(name, "unknown", len);

        /* Should check the error */

#ifdef DEBUG
        fprintf(stderr, "gethostname in SMB_Get_My_Name returned error:");
        perror("");
#endif

    } else {
        char *address;
        /* only keep the portion up to the first "." */
        if (NULL != (address = strchr(name, '.'))) {
            *address = '\0';   /* truncate at first '.' */
        }
    }
}

/* Send a TCON to the remote server ...               */

SMB_Tree_Handle SMB_TreeConnect(SMB_Handle_Type Con_Handle,
                                SMB_Tree_Handle Tree_Handle,
                                const char *path,
                                const char *password,
                                const char *device)

{
    struct RFCNB_Pkt *pkt;
    int param_len, pkt_len;
    char *p;
    SMB_Tree_Handle tree;

    /* Figure out how much space is needed for path, password, dev ... */

    if ((path == NULL) | (password == NULL) | (device == NULL)) {

#ifdef DEBUG
        fprintf(stderr, "Bad parameter passed to SMB_TreeConnect\n");
#endif

        SMBlib_errno = SMBlibE_BadParam;
        return(NULL);

    }

    /* The + 2 is because of the \0 and the marker ...                    */

    param_len = strlen(path) + 2 + strlen(password) + 2 + strlen(device) + 2;

    /* The -1 accounts for the one byte smb_buf we have because some systems */
    /* don't like char msg_buf[]                                             */

    pkt_len = SMB_tcon_len + param_len;

    pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len);

    if (pkt == NULL) {

        SMBlib_errno = SMBlibE_NoSpace;
        return(NULL); /* Should handle the error */

    }

    /* Now allocate a tree for this to go into ... */

    if (Tree_Handle == NULL) {

        tree = (SMB_Tree_Handle)malloc(sizeof(struct SMB_Tree_Structure));

        if (tree == NULL) {

            RFCNB_Free_Pkt(pkt);
            SMBlib_errno = SMBlibE_NoSpace;
            return(NULL);

        }
    } else {

        tree = Tree_Handle;

    }

    tree -> next = tree -> prev = NULL;
    tree -> con = Con_Handle;
    strncpy(tree -> path, path, sizeof(tree -> path));
    strncpy(tree -> device_type, device, sizeof(tree -> device_type));

    /* Now plug in the values ... */

    memset(SMB_Hdr(pkt), 0, SMB_tcon_len);
    SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF);  /* Plunk in IDF */
    *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBtcon;
    SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Con_Handle -> pid);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, 0);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Con_Handle -> mid);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Con_Handle -> uid);
    *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0;

    SSVAL(SMB_Hdr(pkt), SMB_tcon_bcc_offset, param_len);

    /* Now copy the param strings in with the right stuff */

    p = (char *)(SMB_Hdr(pkt) + SMB_tcon_buf_offset);
    *p = SMBasciiID;
    strcpy(p + 1, path);
    p = p + strlen(path) + 2;
    *p = SMBasciiID;
    strcpy(p + 1, password);
    p = p + strlen(password) + 2;
    *p = SMBasciiID;
    strcpy(p + 1, device);

    /* Now send the packet and sit back ... */

    if (RFCNB_Send(Con_Handle -> Trans_Connect, pkt, pkt_len) < 0) {

#ifdef DEBUG
        fprintf(stderr, "Error sending TCon request\n");
#endif

        if (Tree_Handle == NULL)
            free(tree);
        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = -SMBlibE_SendFailed;
        return(NULL);

    }

    /* Now get the response ... */

    if (RFCNB_Recv(Con_Handle -> Trans_Connect, pkt, pkt_len) < 0) {

#ifdef DEBUG
        fprintf(stderr, "Error receiving response to TCon\n");
#endif

        if (Tree_Handle == NULL)
            free(tree);
        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = -SMBlibE_RecvFailed;
        return(NULL);

    }

    /* Check out the response type ... */

    if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) {  /* Process error */

#ifdef DEBUG
        fprintf(stderr, "SMB_TCon failed with errorclass = %i, Error Code = %i\n",
                CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset),
                SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset));
#endif

        if (Tree_Handle == NULL)
            free(tree);
        SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset);
        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = SMBlibE_Remote;
        return(NULL);

    }

    tree -> tid = SVAL(SMB_Hdr(pkt), SMB_tconr_tid_offset);
    tree -> mbs = SVAL(SMB_Hdr(pkt), SMB_tconr_mbs_offset);

#ifdef DEBUG
    fprintf(stderr, "TConn succeeded, with TID=%i, Max Xmit=%i\n",
            tree -> tid, tree -> mbs);
#endif

    /* Now link the Tree to the Server Structure ... */

    if (Con_Handle -> first_tree == NULL) {

        Con_Handle -> first_tree = tree;
        Con_Handle -> last_tree = tree;

    } else {

        Con_Handle -> last_tree -> next = tree;
        tree -> prev = Con_Handle -> last_tree;
        Con_Handle -> last_tree = tree;

    }

    RFCNB_Free_Pkt(pkt);
    return(tree);

}

int SMB_TreeDisconnect(SMB_Tree_Handle Tree_Handle, BOOL discard)

{
    struct RFCNB_Pkt *pkt;
    int pkt_len;

    pkt_len = SMB_tdis_len;

    pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len);

    if (pkt == NULL) {

        SMBlib_errno = SMBlibE_NoSpace;
        return(SMBlibE_BAD); /* Should handle the error */

    }

    /* Now plug in the values ... */

    memset(SMB_Hdr(pkt), 0, SMB_tdis_len);
    SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF);  /* Plunk in IDF */
    *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBtdis;
    SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Tree_Handle -> con -> pid);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Tree_Handle -> con -> mid);
    SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Tree_Handle -> con -> uid);
    *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0;

    SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, Tree_Handle -> tid);
    SSVAL(SMB_Hdr(pkt), SMB_tcon_bcc_offset, 0);

    /* Now send the packet and sit back ... */

    if (RFCNB_Send(Tree_Handle -> con -> Trans_Connect, pkt, pkt_len) < 0) {

#ifdef DEBUG
        fprintf(stderr, "Error sending TDis request\n");
#endif

        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = -SMBlibE_SendFailed;
        return(SMBlibE_BAD);

    }

    /* Now get the response ... */

    if (RFCNB_Recv(Tree_Handle -> con -> Trans_Connect, pkt, pkt_len) < 0) {

#ifdef DEBUG
        fprintf(stderr, "Error receiving response to TCon\n");
#endif

        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = -SMBlibE_RecvFailed;
        return(SMBlibE_BAD);

    }

    /* Check out the response type ... */

    if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) {  /* Process error */

#ifdef DEBUG
        fprintf(stderr, "SMB_TDis failed with errorclass = %i, Error Code = %i\n",
                CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset),
                SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset));
#endif

        SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset);
        RFCNB_Free_Pkt(pkt);
        SMBlib_errno = SMBlibE_Remote;
        return(SMBlibE_BAD);

    }

    Tree_Handle -> tid = 0xFFFF;        /* Invalid TID */
    Tree_Handle -> mbs = 0;             /* Invalid     */

#ifdef DEBUG

    fprintf(stderr, "Tree disconnect successful ...\n");

#endif

    /* What about the tree handle ? */

    if (discard == TRUE) { /* Unlink it and free it ... */

        if (Tree_Handle -> next == NULL)
            Tree_Handle -> con -> first_tree = Tree_Handle -> prev;
        else
            Tree_Handle -> next -> prev = Tree_Handle -> prev;

        if (Tree_Handle -> prev == NULL)
            Tree_Handle -> con -> last_tree = Tree_Handle -> next;
        else
            Tree_Handle -> prev -> next = Tree_Handle -> next;

    }

    RFCNB_Free_Pkt(pkt);
    return(0);

}

/* Pick up the last LMBlib error ... */

int SMB_Get_Last_Error()

{

    return(SMBlib_errno);

}

/* Pick up the last error returned in an SMB packet          */
/* We will need macros to extract error class and error code */

int SMB_Get_Last_SMB_Err()

{

    return(SMBlib_SMB_Error);

}

/* Pick up the error message associated with an error from SMBlib  */

/* Keep this table in sync with the message codes in smblib-common.h */

static const char *SMBlib_Error_Messages[] = {

    "Request completed successfully.",
    "Server returned a non-zero SMB Error Class and Code.",
    "A lower layer protocol error occurred.",
    "Function not yet implemented.",
    "The protocol negotiated does not support the request.",
    "No space available for operation.",
    "One or more bad parameters passed.",
    "None of the protocols we offered were accepted.",
    "The attempt to send an SMB request failed. See protocol error info.",
    "The attempt to get an SMB response failed. See protocol error info.",
    "The logon request failed, but you were logged in as guest.",
    "The attempt to call the remote server failed. See protocol error info.",
    "The protocol dialect specified in a NegProt and accepted by the server is unknown.",
    /* This next one simplifies error handling */
    "No such error code.",
    NULL
};

void SMB_Get_Error_Msg(int msg, char *msgbuf, int len)

{

    if (msg >= 0) {

        strncpy(msgbuf,
                SMBlib_Error_Messages[msg>SMBlibE_NoSuchMsg?SMBlibE_NoSuchMsg:msg],
                len - 1);
        msgbuf[len - 1] = 0; /* Make sure it is a string */
    } else { /* Add the lower layer message ... */

        char prot_msg[1024];

        msg = -msg;   /* Make it positive */

        strncpy(msgbuf,
                SMBlib_Error_Messages[msg>SMBlibE_NoSuchMsg?SMBlibE_NoSuchMsg:msg],
                len - 1);

        msgbuf[len - 1] = 0; /* make sure it is a string */

        if (strlen(msgbuf) < len) { /* If there is space, put rest in */

            strncat(msgbuf, "\n\t", len - strlen(msgbuf));

            RFCNB_Get_Error(prot_msg, sizeof(prot_msg) - 1);

            strncat(msgbuf, prot_msg, len - strlen(msgbuf));

        }
    }

}