~squid/squid/sbuf-use

« back to all changes in this revision

Viewing changes to helpers/basic_auth/MSNT/session.c

  • Committer: hno
  • Date: 2001-01-08 06:32:04 UTC
  • Revision ID: cvs-1:hno-20010108063204-w6a8e1zz6eprqnp8
Major rewrite of proxy authentication to support other schemes than
Basic (auth_rewrite branch on SourceForge).
Contributors:
   Andy Doran
   Robert Collins
   Chemolli Francesco
   Henrik Nordstrom

For details about the new API's, see Programmers Guide.

As part of this change everything from auth_modules has been moved to
src/auth/basic/helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* UNIX RFCNB (RFC1001/RFC1002) NetBIOS implementation
 
2
 * 
 
3
 * Version 1.0
 
4
 * Session 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
int RFCNB_errno = 0;
 
27
int RFCNB_saved_errno = 0;
 
28
#define RFCNB_ERRNO
 
29
 
 
30
#include "std-includes.h"
 
31
#include <netinet/tcp.h>
 
32
#include "rfcnb-priv.h"
 
33
#include "rfcnb-util.h"
 
34
#include "rfcnb-io.h"
 
35
 
 
36
#include <stdlib.h>
 
37
#include <unistd.h>
 
38
#include <string.h>
 
39
 
 
40
int RFCNB_Stats[RFCNB_MAX_STATS];
 
41
 
 
42
void (*Prot_Print_Routine) () = NULL;   /* Pointer to print routine */
 
43
 
 
44
/* Set up a session with a remote name. We are passed Called_Name as a
 
45
 * string which we convert to a NetBIOS name, ie space terminated, up to
 
46
 * 16 characters only if we need to. If Called_Address is not empty, then
 
47
 * we use it to connect to the remote end, but put in Called_Name ... Called 
 
48
 * Address can be a DNS based name, or a TCP/IP address ...
 
49
 */
 
50
 
 
51
void *
 
52
RFCNB_Call(char *Called_Name, char *Calling_Name, char *Called_Address,
 
53
    int port)
 
54
{
 
55
    struct RFCNB_Con *con;
 
56
    struct in_addr Dest_IP;
 
57
    int Client;
 
58
    BOOL redirect;
 
59
    struct redirect_addr *redir_addr;
 
60
    char *Service_Address;
 
61
 
 
62
    /* Now, we really should look up the port in /etc/services ... */
 
63
 
 
64
    if (port == 0)
 
65
        port = RFCNB_Default_Port;
 
66
 
 
67
    /* Create a connection structure first */
 
68
 
 
69
    if ((con = (struct RFCNB_Con *) malloc(sizeof(struct RFCNB_Con))) == NULL) {        /* Error in size */
 
70
 
 
71
        RFCNB_errno = RFCNBE_NoSpace;
 
72
        RFCNB_saved_errno = errno;
 
73
        return (NULL);
 
74
 
 
75
    }
 
76
    con->fd = -0;               /* no descriptor yet */
 
77
    con->rfc_errno = 0;         /* no error yet */
 
78
    con->timeout = 0;           /* no timeout   */
 
79
    con->redirects = 0;
 
80
    con->redirect_list = NULL;  /* Fix bug still in version 0.50 */
 
81
 
 
82
    /* Resolve that name into an IP address */
 
83
 
 
84
    Service_Address = Called_Name;
 
85
    if (strcmp(Called_Address, "") != 0) {      /* If the Called Address = "" */
 
86
        Service_Address = Called_Address;
 
87
    }
 
88
    if ((errno = RFCNB_Name_To_IP(Service_Address, &Dest_IP)) < 0) {    /* Error */
 
89
 
 
90
        /* No need to modify RFCNB_errno as it was done by RFCNB_Name_To_IP */
 
91
 
 
92
        return (NULL);
 
93
 
 
94
    }
 
95
    /* Now connect to the remote end */
 
96
 
 
97
    redirect = TRUE;            /* Fudge this one so we go once through */
 
98
 
 
99
    while (redirect) {          /* Connect and get session info etc */
 
100
 
 
101
        redirect = FALSE;       /* Assume all OK */
 
102
 
 
103
        /* Build the redirect info. First one is first addr called */
 
104
        /* And tack it onto the list of addresses we called        */
 
105
 
 
106
        if ((redir_addr = (struct redirect_addr *) malloc(sizeof(struct redirect_addr))) == NULL) {     /* Could not get space */
 
107
 
 
108
            RFCNB_errno = RFCNBE_NoSpace;
 
109
            RFCNB_saved_errno = errno;
 
110
            return (NULL);
 
111
 
 
112
        }
 
113
        memcpy((char *) &(redir_addr->ip_addr), (char *) &Dest_IP, sizeof(Dest_IP));
 
114
        redir_addr->port = port;
 
115
        redir_addr->next = NULL;
 
116
 
 
117
        if (con->redirect_list == NULL) {       /* Stick on head */
 
118
 
 
119
            con->redirect_list = con->last_addr = redir_addr;
 
120
 
 
121
        } else {
 
122
 
 
123
            con->last_addr->next = redir_addr;
 
124
            con->last_addr = redir_addr;
 
125
 
 
126
        }
 
127
 
 
128
        /* Now, make that connection */
 
129
 
 
130
        if ((Client = RFCNB_IP_Connect(Dest_IP, port)) < 0) {   /* Error */
 
131
 
 
132
            /* No need to modify RFCNB_errno as it was done by RFCNB_IP_Connect */
 
133
 
 
134
            return (NULL);
 
135
 
 
136
        }
 
137
        con->fd = Client;
 
138
 
 
139
        /* Now send and handle the RFCNB session request              */
 
140
        /* If we get a redirect, we will comeback with redirect true 
 
141
         * and a new IP address in DEST_IP                            */
 
142
 
 
143
        if ((errno = RFCNB_Session_Req(con,
 
144
                    Called_Name,
 
145
                    Calling_Name,
 
146
                    &redirect, &Dest_IP, &port)) < 0) {
 
147
 
 
148
            /* No need to modify RFCNB_errno as it was done by RFCNB_Session.. */
 
149
 
 
150
            return (NULL);
 
151
 
 
152
        }
 
153
        if (redirect) {
 
154
 
 
155
            /* We have to close the connection, and then try again */
 
156
 
 
157
            (con->redirects)++;
 
158
 
 
159
            RFCNB_Close(con->fd);       /* Close it */
 
160
 
 
161
        }
 
162
    }
 
163
 
 
164
    return (con);
 
165
 
 
166
}
 
167
 
 
168
/* We send a packet to the other end ... for the moment, we treat the 
 
169
 * data as a series of pointers to blocks of data ... we should check the
 
170
 * length ... */
 
171
 
 
172
int
 
173
RFCNB_Send(struct RFCNB_Con *Con_Handle, struct RFCNB_Pkt *udata, int Length)
 
174
{
 
175
    struct RFCNB_Pkt *pkt;
 
176
    char *hdr;
 
177
    int len;
 
178
 
 
179
    /* Plug in the header and send the data */
 
180
 
 
181
    pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Hdr_Len);
 
182
 
 
183
    if (pkt == NULL) {
 
184
 
 
185
        RFCNB_errno = RFCNBE_NoSpace;
 
186
        RFCNB_saved_errno = errno;
 
187
        return (RFCNBE_Bad);
 
188
 
 
189
    }
 
190
    pkt->next = udata;          /* The user data we want to send */
 
191
 
 
192
    hdr = pkt->data;
 
193
 
 
194
    /* Following crap is for portability across multiple UNIX machines */
 
195
 
 
196
    *(hdr + RFCNB_Pkt_Type_Offset) = RFCNB_SESSION_MESSAGE;
 
197
    RFCNB_Put_Pkt_Len(hdr, Length);
 
198
 
 
199
#ifdef RFCNB_DEBUG
 
200
 
 
201
    fprintf(stderr, "Sending packet: ");
 
202
 
 
203
#endif
 
204
 
 
205
    if ((len = RFCNB_Put_Pkt(Con_Handle, pkt, Length + RFCNB_Pkt_Hdr_Len)) < 0) {
 
206
 
 
207
        /* No need to change RFCNB_errno as it was done by put_pkt ...     */
 
208
 
 
209
        return (RFCNBE_Bad);    /* Should be able to write that lot ... */
 
210
 
 
211
    }
 
212
    /* Now we have sent that lot, let's get rid of the RFCNB Header and return */
 
213
 
 
214
    pkt->next = NULL;
 
215
 
 
216
    RFCNB_Free_Pkt(pkt);
 
217
 
 
218
    return (len);
 
219
 
 
220
}
 
221
 
 
222
/* We pick up a message from the internet ... We have to worry about 
 
223
 * non-message packets ...                                           */
 
224
 
 
225
int
 
226
RFCNB_Recv(void *con_Handle, struct RFCNB_Pkt *Data, int Length)
 
227
{
 
228
    struct RFCNB_Pkt *pkt;
 
229
    int ret_len;
 
230
 
 
231
    if (con_Handle == NULL) {
 
232
 
 
233
        RFCNB_errno = RFCNBE_BadHandle;
 
234
        RFCNB_saved_errno = errno;
 
235
        return (RFCNBE_Bad);
 
236
 
 
237
    }
 
238
    /* Now get a packet from below. We allocate a header first */
 
239
 
 
240
    /* Plug in the header and send the data */
 
241
 
 
242
    pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Hdr_Len);
 
243
 
 
244
    if (pkt == NULL) {
 
245
 
 
246
        RFCNB_errno = RFCNBE_NoSpace;
 
247
        RFCNB_saved_errno = errno;
 
248
        return (RFCNBE_Bad);
 
249
 
 
250
    }
 
251
    pkt->next = Data;           /* Plug in the data portion */
 
252
 
 
253
    if ((ret_len = RFCNB_Get_Pkt(con_Handle, pkt, Length + RFCNB_Pkt_Hdr_Len)) < 0) {
 
254
 
 
255
#ifdef RFCNB_DEBUG
 
256
        fprintf(stderr, "Bad packet return in RFCNB_Recv... \n");
 
257
#endif
 
258
 
 
259
        return (RFCNBE_Bad);
 
260
 
 
261
    }
 
262
    /* We should check that we go a message and not a keep alive */
 
263
 
 
264
    pkt->next = NULL;
 
265
 
 
266
    RFCNB_Free_Pkt(pkt);
 
267
 
 
268
    return (ret_len);
 
269
 
 
270
}
 
271
 
 
272
/* We just disconnect from the other end, as there is nothing in the RFCNB */
 
273
/* protocol that specifies any exchange as far as I can see                */
 
274
 
 
275
int
 
276
RFCNB_Hangup(struct RFCNB_Con *con_Handle)
 
277
{
 
278
 
 
279
    if (con_Handle != NULL) {
 
280
        RFCNB_Close(con_Handle->fd);    /* Could this fail? */
 
281
        free(con_Handle);
 
282
    }
 
283
    return 0;
 
284
 
 
285
 
 
286
}
 
287
 
 
288
/* Set TCP_NODELAY on the socket                                          */
 
289
 
 
290
int
 
291
RFCNB_Set_Sock_NoDelay(struct RFCNB_Con *con_Handle, BOOL yn)
 
292
{
 
293
 
 
294
    return (setsockopt(con_Handle->fd, IPPROTO_TCP, TCP_NODELAY,
 
295
            (char *) &yn, sizeof(yn)));
 
296
 
 
297
}
 
298
 
 
299
 
 
300
/* Listen for a connection on a port???, when                             */
 
301
/* the connection comes in, we return with the connection                 */
 
302
 
 
303
void
 
304
RFCNB_Listen()
 
305
{
 
306
 
 
307
}
 
308
 
 
309
/* Pick up the last error response as a string, hmmm, this routine should */
 
310
/* have been different ...                                                */
 
311
 
 
312
void
 
313
RFCNB_Get_Error(char *buffer, int buf_len)
 
314
{
 
315
 
 
316
    if (RFCNB_saved_errno <= 0) {
 
317
        sprintf(buffer, "%s", RFCNB_Error_Strings[RFCNB_errno]);
 
318
    } else {
 
319
        sprintf(buffer, "%s\n\terrno:%s", RFCNB_Error_Strings[RFCNB_errno],
 
320
            strerror(RFCNB_saved_errno));
 
321
    }
 
322
 
 
323
}
 
324
 
 
325
/* Pick up the last error response and returns as a code                 */
 
326
 
 
327
int
 
328
RFCNB_Get_Last_Error()
 
329
{
 
330
 
 
331
    return (RFCNB_errno);
 
332
 
 
333
}
 
334
 
 
335
/* Pick up saved errno as well */
 
336
 
 
337
int
 
338
RFCNB_Get_Last_Errno()
 
339
{
 
340
 
 
341
    return (RFCNB_saved_errno);
 
342
 
 
343
}
 
344
 
 
345
/* Pick up the last error response and return in string ...             */
 
346
 
 
347
void
 
348
RFCNB_Get_Error_Msg(int code, char *msg_buf, int len)
 
349
{
 
350
 
 
351
    strncpy(msg_buf, RFCNB_Error_Strings[abs(code)], len);
 
352
 
 
353
}
 
354
 
 
355
/* Register a higher level protocol print routine */
 
356
 
 
357
void
 
358
RFCNB_Register_Print_Routine(void (*fn) ())
 
359
{
 
360
 
 
361
    Prot_Print_Routine = fn;
 
362
 
 
363
}