~squid/squid/sbuf-use

« back to all changes in this revision

Viewing changes to helpers/ntlm_auth/SMB/smbval/rfcnb-io.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
 * RFCNB IO 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
/* #include <features.h> */
 
26
#include "config.h"
 
27
#include "std-includes.h"
 
28
#include "rfcnb-priv.h"
 
29
#include "rfcnb-util.h"
 
30
#include "rfcnb-io.h"
 
31
#include <sys/uio.h>
 
32
#include <sys/signal.h>
 
33
#include <string.h>
 
34
 
 
35
int RFCNB_Timeout = 0;          /* Timeout in seconds ... */
 
36
 
 
37
void
 
38
rfcnb_alarm(int sig)
 
39
{
 
40
 
 
41
    fprintf(stderr, "IO Timed out ...\n");
 
42
 
 
43
}
 
44
 
 
45
/* Set timeout value and setup signal handling */
 
46
 
 
47
int
 
48
RFCNB_Set_Timeout(int seconds)
 
49
{
 
50
    /* If we are on a Bezerkeley system, use sigvec, else sigaction */
 
51
#if HAVE_SIGACTION
 
52
    struct sigaction inact, outact;
 
53
#else
 
54
    struct sigvec invec, outvec;
 
55
#endif
 
56
 
 
57
    RFCNB_Timeout = seconds;
 
58
 
 
59
    if (RFCNB_Timeout > 0) {    /* Set up handler to ignore but not restart */
 
60
 
 
61
#if HAVE_SIGACTION
 
62
        inact.sa_handler = (void (*)()) rfcnb_alarm;
 
63
        sigemptyset(&inact.sa_mask);
 
64
        inact.sa_flags = 0;     /* Don't restart */
 
65
 
 
66
        if (sigaction(SIGALRM, &inact, &outact) < 0)
 
67
            return (-1);
 
68
#else
 
69
        invec.sv_handler = (void (*)()) rfcnb_alarm;
 
70
        invec.sv_mask = 0;
 
71
        invec.sv_flags = SV_INTERRUPT;
 
72
 
 
73
        if (sigvec(SIGALRM, &invec, &outvec) < 0)
 
74
            return (-1);
 
75
#endif
 
76
 
 
77
    }
 
78
    return (0);
 
79
 
 
80
}
 
81
 
 
82
/* Discard the rest of an incoming packet as we do not have space for it
 
83
 * in the buffer we allocated or were passed ...                         */
 
84
 
 
85
int
 
86
RFCNB_Discard_Rest(struct RFCNB_Con *con, int len)
 
87
{
 
88
    char temp[100];             /* Read into here */
 
89
    int rest, this_read, bytes_read;
 
90
 
 
91
    /* len is the amount we should read */
 
92
 
 
93
#ifdef RFCNB_DEBUG
 
94
    fprintf(stderr, "Discard_Rest called to discard: %i\n", len);
 
95
#endif
 
96
 
 
97
    rest = len;
 
98
 
 
99
    while (rest > 0) {
 
100
 
 
101
        this_read = (rest > sizeof(temp) ? sizeof(temp) : rest);
 
102
 
 
103
        bytes_read = read(con->fd, temp, this_read);
 
104
 
 
105
        if (bytes_read <= 0) {  /* Error so return */
 
106
 
 
107
            if (bytes_read < 0)
 
108
                RFCNB_errno = RFCNBE_BadRead;
 
109
            else
 
110
                RFCNB_errno = RFCNBE_ConGone;
 
111
 
 
112
            RFCNB_saved_errno = errno;
 
113
            return (RFCNBE_Bad);
 
114
 
 
115
        }
 
116
        rest = rest - bytes_read;
 
117
 
 
118
    }
 
119
 
 
120
    return (0);
 
121
 
 
122
}
 
123
 
 
124
 
 
125
/* Send an RFCNB packet to the connection.
 
126
 * 
 
127
 * We just send each of the blocks linked together ... 
 
128
 * 
 
129
 * If we can, try to send it as one iovec ... 
 
130
 * 
 
131
 */
 
132
 
 
133
int
 
134
RFCNB_Put_Pkt(struct RFCNB_Con *con, struct RFCNB_Pkt *pkt, int len)
 
135
{
 
136
    int len_sent, tot_sent, this_len;
 
137
    struct RFCNB_Pkt *pkt_ptr;
 
138
    char *this_data;
 
139
    int i;
 
140
    struct iovec io_list[10];   /* We should never have more      */
 
141
    /* If we do, this will blow up ... */
 
142
 
 
143
    /* Try to send the data ... We only send as many bytes as len claims */
 
144
    /* We should try to stuff it into an IOVEC and send as one write     */
 
145
 
 
146
 
 
147
    pkt_ptr = pkt;
 
148
    len_sent = tot_sent = 0;    /* Nothing sent so far */
 
149
    i = 0;
 
150
 
 
151
    while ((pkt_ptr != NULL) & (i < 10)) {      /* Watch that magic number! */
 
152
 
 
153
        this_len = pkt_ptr->len;
 
154
        this_data = pkt_ptr->data;
 
155
        if ((tot_sent + this_len) > len)
 
156
            this_len = len - tot_sent;  /* Adjust so we don't send too much */
 
157
 
 
158
        /* Now plug into the iovec ... */
 
159
 
 
160
        io_list[i].iov_len = this_len;
 
161
        io_list[i].iov_base = this_data;
 
162
        i++;
 
163
 
 
164
        tot_sent += this_len;
 
165
 
 
166
        if (tot_sent == len)
 
167
            break;              /* Let's not send too much */
 
168
 
 
169
        pkt_ptr = pkt_ptr->next;
 
170
 
 
171
    }
 
172
 
 
173
#ifdef RFCNB_DEBUG
 
174
    fprintf(stderr, "Frags = %i, tot_sent = %i\n", i, tot_sent);
 
175
#endif
 
176
 
 
177
    /* Set up an alarm if timeouts are set ... */
 
178
 
 
179
    if (RFCNB_Timeout > 0)
 
180
        alarm(RFCNB_Timeout);
 
181
 
 
182
    if ((len_sent = writev(con->fd, io_list, i)) < 0) {         /* An error */
 
183
 
 
184
        con->rfc_errno = errno;
 
185
        if (errno == EINTR)     /* We were interrupted ... */
 
186
            RFCNB_errno = RFCNBE_Timeout;
 
187
        else
 
188
            RFCNB_errno = RFCNBE_BadWrite;
 
189
        RFCNB_saved_errno = errno;
 
190
        return (RFCNBE_Bad);
 
191
 
 
192
    }
 
193
    if (len_sent < tot_sent) {  /* Less than we wanted */
 
194
        if (errno == EINTR)     /* We were interrupted */
 
195
            RFCNB_errno = RFCNBE_Timeout;
 
196
        else
 
197
            RFCNB_errno = RFCNBE_BadWrite;
 
198
        RFCNB_saved_errno = errno;
 
199
        return (RFCNBE_Bad);
 
200
    }
 
201
    if (RFCNB_Timeout > 0)
 
202
        alarm(0);               /* Reset that sucker */
 
203
 
 
204
#ifdef RFCNB_DEBUG
 
205
 
 
206
    fprintf(stderr, "Len sent = %i ...\n", len_sent);
 
207
    RFCNB_Print_Pkt(stderr, "sent", pkt, len_sent);     /* Print what send ... */
 
208
 
 
209
#endif
 
210
 
 
211
    return (len_sent);
 
212
 
 
213
}
 
214
 
 
215
/* Read an RFCNB packet off the connection. 
 
216
 * 
 
217
 * We read the first 4 bytes, that tells us the length, then read the
 
218
 * rest. We should implement a timeout, but we don't just yet 
 
219
 * 
 
220
 */
 
221
 
 
222
 
 
223
int
 
224
RFCNB_Get_Pkt(struct RFCNB_Con *con, struct RFCNB_Pkt *pkt, int len)
 
225
{
 
226
    int read_len, pkt_len;
 
227
    char hdr[RFCNB_Pkt_Hdr_Len];        /* Local space for the header */
 
228
    struct RFCNB_Pkt *pkt_frag;
 
229
    int more, this_time, offset, frag_len, this_len;
 
230
    BOOL seen_keep_alive = TRUE;
 
231
 
 
232
    /* Read that header straight into the buffer */
 
233
 
 
234
    if (len < RFCNB_Pkt_Hdr_Len) {      /* What a bozo */
 
235
 
 
236
#ifdef RFCNB_DEBUG
 
237
        fprintf(stderr, "Trying to read less than a packet:");
 
238
        perror("");
 
239
#endif
 
240
        RFCNB_errno = RFCNBE_BadParam;
 
241
        return (RFCNBE_Bad);
 
242
 
 
243
    }
 
244
    /* We discard keep alives here ... */
 
245
 
 
246
    if (RFCNB_Timeout > 0)
 
247
        alarm(RFCNB_Timeout);
 
248
 
 
249
    while (seen_keep_alive) {
 
250
 
 
251
        if ((read_len = read(con->fd, hdr, sizeof(hdr))) < 0) {         /* Problems */
 
252
#ifdef RFCNB_DEBUG
 
253
            fprintf(stderr, "Reading the packet, we got:");
 
254
            perror("");
 
255
#endif
 
256
            if (errno == EINTR)
 
257
                RFCNB_errno = RFCNBE_Timeout;
 
258
            else
 
259
                RFCNB_errno = RFCNBE_BadRead;
 
260
            RFCNB_saved_errno = errno;
 
261
            return (RFCNBE_Bad);
 
262
 
 
263
        }
 
264
        /* Now we check out what we got */
 
265
 
 
266
        if (read_len == 0) {    /* Connection closed, send back eof?  */
 
267
 
 
268
#ifdef RFCNB_DEBUG
 
269
            fprintf(stderr, "Connection closed reading\n");
 
270
#endif
 
271
 
 
272
            if (errno == EINTR)
 
273
                RFCNB_errno = RFCNBE_Timeout;
 
274
            else
 
275
                RFCNB_errno = RFCNBE_ConGone;
 
276
            RFCNB_saved_errno = errno;
 
277
            return (RFCNBE_Bad);
 
278
 
 
279
        }
 
280
        if (RFCNB_Pkt_Type(hdr) == RFCNB_SESSION_KEEP_ALIVE) {
 
281
 
 
282
#ifdef RFCNB_DEBUG
 
283
            fprintf(stderr, "RFCNB KEEP ALIVE received\n");
 
284
#endif
 
285
 
 
286
        } else {
 
287
            seen_keep_alive = FALSE;
 
288
        }
 
289
 
 
290
    }
 
291
 
 
292
    /* What if we got less than or equal to a hdr size in bytes? */
 
293
 
 
294
    if (read_len < sizeof(hdr)) {       /* We got a small packet */
 
295
 
 
296
        /* Now we need to copy the hdr portion we got into the supplied packet */
 
297
 
 
298
        memcpy(pkt->data, hdr, read_len);       /*Copy data */
 
299
 
 
300
#ifdef RFCNB_DEBUG
 
301
        RFCNB_Print_Pkt(stderr, "rcvd", pkt, read_len);
 
302
#endif
 
303
 
 
304
        return (read_len);
 
305
 
 
306
    }
 
307
    /* Now, if we got at least a hdr size, alloc space for rest, if we need it */
 
308
 
 
309
    pkt_len = RFCNB_Pkt_Len(hdr);
 
310
 
 
311
#ifdef RFCNB_DEBUG
 
312
    fprintf(stderr, "Reading Pkt: Length = %i\n", pkt_len);
 
313
#endif
 
314
 
 
315
    /* Now copy in the hdr */
 
316
 
 
317
    memcpy(pkt->data, hdr, sizeof(hdr));
 
318
 
 
319
    /* Get the rest of the packet ... first figure out how big our buf is? */
 
320
    /* And make sure that we handle the fragments properly ... Sure should */
 
321
    /* use an iovec ...                                                    */
 
322
 
 
323
    if (len < pkt_len)          /* Only get as much as we have space for */
 
324
        more = len - RFCNB_Pkt_Hdr_Len;
 
325
    else
 
326
        more = pkt_len;
 
327
 
 
328
    this_time = 0;
 
329
 
 
330
    /* We read for each fragment ... */
 
331
 
 
332
    if (pkt->len == read_len) { /* If this frag was exact size */
 
333
        pkt_frag = pkt->next;   /* Stick next lot in next frag */
 
334
        offset = 0;             /* then we start at 0 in next  */
 
335
    } else {
 
336
        pkt_frag = pkt;         /* Otherwise use rest of this frag */
 
337
        offset = RFCNB_Pkt_Hdr_Len;     /* Otherwise skip the header       */
 
338
    }
 
339
 
 
340
    frag_len = pkt_frag->len;
 
341
 
 
342
    if (more <= frag_len)       /* If len left to get less than frag space */
 
343
        this_len = more;        /* Get the rest ...                        */
 
344
    else
 
345
        this_len = frag_len - offset;
 
346
 
 
347
    while (more > 0) {
 
348
 
 
349
        if ((this_time = read(con->fd, (pkt_frag->data) + offset, this_len)) <= 0) {    /* Problems */
 
350
 
 
351
            if (errno == EINTR) {
 
352
 
 
353
                RFCNB_errno = RFCNB_Timeout;
 
354
 
 
355
            } else {
 
356
                if (this_time < 0)
 
357
                    RFCNB_errno = RFCNBE_BadRead;
 
358
                else
 
359
                    RFCNB_errno = RFCNBE_ConGone;
 
360
            }
 
361
 
 
362
            RFCNB_saved_errno = errno;
 
363
            return (RFCNBE_Bad);
 
364
 
 
365
        }
 
366
#ifdef RFCNB_DEBUG
 
367
        fprintf(stderr, "Frag_Len = %i, this_time = %i, this_len = %i, more = %i\n", frag_len,
 
368
            this_time, this_len, more);
 
369
#endif
 
370
 
 
371
        read_len = read_len + this_time;        /* How much have we read ... */
 
372
 
 
373
        /* Now set up the next part */
 
374
 
 
375
        if (pkt_frag->next == NULL)
 
376
            break;              /* That's it here */
 
377
 
 
378
        pkt_frag = pkt_frag->next;
 
379
        this_len = pkt_frag->len;
 
380
        offset = 0;
 
381
 
 
382
        more = more - this_time;
 
383
 
 
384
    }
 
385
 
 
386
#ifdef RFCNB_DEBUG
 
387
    fprintf(stderr, "Pkt Len = %i, read_len = %i\n", pkt_len, read_len);
 
388
    RFCNB_Print_Pkt(stderr, "rcvd", pkt, read_len + sizeof(hdr));
 
389
#endif
 
390
 
 
391
    if (read_len < (pkt_len + sizeof(hdr))) {   /* Discard the rest */
 
392
 
 
393
        return (RFCNB_Discard_Rest(con, (pkt_len + sizeof(hdr)) - read_len));
 
394
 
 
395
    }
 
396
    if (RFCNB_Timeout > 0)
 
397
        alarm(0);               /* Reset that sucker */
 
398
 
 
399
    return (read_len + sizeof(RFCNB_Hdr));
 
400
}