~ubuntu-branches/ubuntu/dapper/cmake/dapper-backports

« back to all changes in this revision

Viewing changes to Utilities/cmcurl/security.c

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-01-08 10:48:14 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060108104814-jmy1r2fydpohpc1g
Tags: 2.2.3-1
* New upstream release (Closes: #338324)
* support GNU/kFreeBSD in cmake (Closes: #340764)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> for
 
2
 * use in Curl. His latest changes were done 2000-09-18.
 
3
 *
 
4
 * It has since been patched and modified a lot by Daniel Stenberg
 
5
 * <daniel@haxx.se> to make it better applied to curl conditions, and to make
 
6
 * it not use globals, pollute name space and more. This source code awaits a
 
7
 * rewrite to work around the paragraph 2 in the BSD licenses as explained
 
8
 * below.
 
9
 *
 
10
 * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
 
11
 * (Royal Institute of Technology, Stockholm, Sweden).
 
12
 * All rights reserved.
 
13
 *
 
14
 * Redistribution and use in source and binary forms, with or without
 
15
 * modification, are permitted provided that the following conditions
 
16
 * are met:
 
17
 *
 
18
 * 1. Redistributions of source code must retain the above copyright
 
19
 *    notice, this list of conditions and the following disclaimer.
 
20
 *
 
21
 * 2. Redistributions in binary form must reproduce the above copyright
 
22
 *    notice, this list of conditions and the following disclaimer in the
 
23
 *    documentation and/or other materials provided with the distribution.
 
24
 *
 
25
 * 3. Neither the name of the Institute nor the names of its contributors
 
26
 *    may be used to endorse or promote products derived from this software
 
27
 *    without specific prior written permission.
 
28
 *
 
29
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 
30
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
32
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 
33
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
34
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
35
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
36
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
37
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
38
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
39
 * SUCH DAMAGE.  */
 
40
 
 
41
#include "setup.h"
 
42
 
 
43
#ifndef CURL_DISABLE_FTP
 
44
#ifdef HAVE_KRB4
 
45
 
 
46
#define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
 
47
#include <curl/mprintf.h>
 
48
 
 
49
#include "security.h"
 
50
#include <stdlib.h>
 
51
#include <string.h>
 
52
#include <netdb.h>
 
53
 
 
54
#ifdef HAVE_UNISTD_H
 
55
#include <unistd.h>
 
56
#endif
 
57
 
 
58
#include "base64.h"
 
59
#include "sendf.h"
 
60
#include "ftp.h"
 
61
#include "curl_memory.h"
 
62
 
 
63
/* The last #include file should be: */
 
64
#include "memdebug.h"
 
65
 
 
66
#define min(a, b)   ((a) < (b) ? (a) : (b))
 
67
 
 
68
static struct {
 
69
    enum protection_level level;
 
70
    const char *name;
 
71
} level_names[] = {
 
72
    { prot_clear, "clear" },
 
73
    { prot_safe, "safe" },
 
74
    { prot_confidential, "confidential" },
 
75
    { prot_private, "private" }
 
76
};
 
77
 
 
78
static enum protection_level
 
79
name_to_level(const char *name)
 
80
{
 
81
  int i;
 
82
  for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
 
83
    if(!strncasecmp(level_names[i].name, name, strlen(name)))
 
84
      return level_names[i].level;
 
85
  return (enum protection_level)-1;
 
86
}
 
87
 
 
88
static struct Curl_sec_client_mech *mechs[] = {
 
89
#ifdef KRB5
 
90
  /* not supported */
 
91
#endif
 
92
#ifdef HAVE_KRB4
 
93
    &Curl_krb4_client_mech,
 
94
#endif
 
95
    NULL
 
96
};
 
97
 
 
98
int
 
99
Curl_sec_getc(struct connectdata *conn, FILE *F)
 
100
{
 
101
  if(conn->sec_complete && conn->data_prot) {
 
102
    char c;
 
103
    if(Curl_sec_read(conn, fileno(F), &c, 1) <= 0)
 
104
      return EOF;
 
105
    return c;
 
106
  }
 
107
  else
 
108
    return getc(F);
 
109
}
 
110
 
 
111
static int
 
112
block_read(int fd, void *buf, size_t len)
 
113
{
 
114
  unsigned char *p = buf;
 
115
  int b;
 
116
  while(len) {
 
117
    b = read(fd, p, len);
 
118
    if (b == 0)
 
119
      return 0;
 
120
    else if (b < 0)
 
121
      return -1;
 
122
    len -= b;
 
123
    p += b;
 
124
  }
 
125
  return p - (unsigned char*)buf;
 
126
}
 
127
 
 
128
static int
 
129
block_write(int fd, void *buf, size_t len)
 
130
{
 
131
  unsigned char *p = buf;
 
132
  int b;
 
133
  while(len) {
 
134
    b = write(fd, p, len);
 
135
    if(b < 0)
 
136
      return -1;
 
137
    len -= b;
 
138
    p += b;
 
139
  }
 
140
  return p - (unsigned char*)buf;
 
141
}
 
142
 
 
143
static int
 
144
sec_get_data(struct connectdata *conn,
 
145
             int fd, struct krb4buffer *buf)
 
146
{
 
147
  int len;
 
148
  int b;
 
149
 
 
150
  b = block_read(fd, &len, sizeof(len));
 
151
  if (b == 0)
 
152
    return 0;
 
153
  else if (b < 0)
 
154
    return -1;
 
155
  len = ntohl(len);
 
156
  buf->data = realloc(buf->data, len);
 
157
  b = block_read(fd, buf->data, len);
 
158
  if (b == 0)
 
159
    return 0;
 
160
  else if (b < 0)
 
161
    return -1;
 
162
  buf->size = (conn->mech->decode)(conn->app_data, buf->data, len,
 
163
                                   conn->data_prot, conn);
 
164
  buf->index = 0;
 
165
  return 0;
 
166
}
 
167
 
 
168
static size_t
 
169
buffer_read(struct krb4buffer *buf, void *data, size_t len)
 
170
{
 
171
    len = min(len, buf->size - buf->index);
 
172
    memcpy(data, (char*)buf->data + buf->index, len);
 
173
    buf->index += len;
 
174
    return len;
 
175
}
 
176
 
 
177
static size_t
 
178
buffer_write(struct krb4buffer *buf, void *data, size_t len)
 
179
{
 
180
    if(buf->index + len > buf->size) {
 
181
        void *tmp;
 
182
        if(buf->data == NULL)
 
183
            tmp = malloc(1024);
 
184
        else
 
185
            tmp = realloc(buf->data, buf->index + len);
 
186
        if(tmp == NULL)
 
187
            return -1;
 
188
        buf->data = tmp;
 
189
        buf->size = buf->index + len;
 
190
    }
 
191
    memcpy((char*)buf->data + buf->index, data, len);
 
192
    buf->index += len;
 
193
    return len;
 
194
}
 
195
 
 
196
int
 
197
Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
 
198
{
 
199
    size_t len;
 
200
    int rx = 0;
 
201
 
 
202
    if(conn->sec_complete == 0 || conn->data_prot == 0)
 
203
      return read(fd, buffer, length);
 
204
 
 
205
    if(conn->in_buffer.eof_flag){
 
206
      conn->in_buffer.eof_flag = 0;
 
207
      return 0;
 
208
    }
 
209
 
 
210
    len = buffer_read(&conn->in_buffer, buffer, length);
 
211
    length -= len;
 
212
    rx += len;
 
213
    buffer = (char*)buffer + len;
 
214
 
 
215
    while(length) {
 
216
      if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
 
217
        return -1;
 
218
      if(conn->in_buffer.size == 0) {
 
219
        if(rx)
 
220
          conn->in_buffer.eof_flag = 1;
 
221
        return rx;
 
222
      }
 
223
      len = buffer_read(&conn->in_buffer, buffer, length);
 
224
      length -= len;
 
225
      rx += len;
 
226
      buffer = (char*)buffer + len;
 
227
    }
 
228
    return rx;
 
229
}
 
230
 
 
231
static int
 
232
sec_send(struct connectdata *conn, int fd, char *from, int length)
 
233
{
 
234
  int bytes;
 
235
  void *buf;
 
236
  bytes = (conn->mech->encode)(conn->app_data, from, length, conn->data_prot,
 
237
                               &buf, conn);
 
238
  bytes = htonl(bytes);
 
239
  block_write(fd, &bytes, sizeof(bytes));
 
240
  block_write(fd, buf, ntohl(bytes));
 
241
  free(buf);
 
242
  return length;
 
243
}
 
244
 
 
245
int
 
246
Curl_sec_fflush_fd(struct connectdata *conn, int fd)
 
247
{
 
248
  if(conn->data_prot != prot_clear) {
 
249
    if(conn->out_buffer.index > 0){
 
250
      Curl_sec_write(conn, fd,
 
251
                conn->out_buffer.data, conn->out_buffer.index);
 
252
      conn->out_buffer.index = 0;
 
253
    }
 
254
    sec_send(conn, fd, NULL, 0);
 
255
  }
 
256
  return 0;
 
257
}
 
258
 
 
259
int
 
260
Curl_sec_write(struct connectdata *conn, int fd, char *buffer, int length)
 
261
{
 
262
  int len = conn->buffer_size;
 
263
  int tx = 0;
 
264
 
 
265
  if(conn->data_prot == prot_clear)
 
266
    return write(fd, buffer, length);
 
267
 
 
268
  len -= (conn->mech->overhead)(conn->app_data, conn->data_prot, len);
 
269
  while(length){
 
270
    if(length < len)
 
271
      len = length;
 
272
    sec_send(conn, fd, buffer, len);
 
273
    length -= len;
 
274
    buffer += len;
 
275
    tx += len;
 
276
  }
 
277
  return tx;
 
278
}
 
279
 
 
280
int
 
281
Curl_sec_putc(struct connectdata *conn, int c, FILE *F)
 
282
{
 
283
  char ch = c;
 
284
  if(conn->data_prot == prot_clear)
 
285
    return putc(c, F);
 
286
 
 
287
  buffer_write(&conn->out_buffer, &ch, 1);
 
288
  if(c == '\n' || conn->out_buffer.index >= 1024 /* XXX */) {
 
289
    Curl_sec_write(conn, fileno(F), conn->out_buffer.data,
 
290
                   conn->out_buffer.index);
 
291
    conn->out_buffer.index = 0;
 
292
  }
 
293
  return c;
 
294
}
 
295
 
 
296
int
 
297
Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
 
298
{
 
299
  int len;
 
300
  char *buf;
 
301
  int code;
 
302
 
 
303
  buf = malloc(strlen(s));
 
304
  len = Curl_base64_decode(s + 4, buf); /* XXX */
 
305
 
 
306
  len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
 
307
  if(len < 0) {
 
308
    free(buf);
 
309
    return -1;
 
310
  }
 
311
 
 
312
  buf[len] = '\0';
 
313
 
 
314
  if(buf[3] == '-')
 
315
    code = 0;
 
316
  else
 
317
    sscanf(buf, "%d", &code);
 
318
  if(buf[len-1] == '\n')
 
319
    buf[len-1] = '\0';
 
320
  strcpy(s, buf);
 
321
  free(buf);
 
322
  return code;
 
323
}
 
324
 
 
325
enum protection_level
 
326
Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
 
327
{
 
328
  enum protection_level old = conn->command_prot;
 
329
  conn->command_prot = level;
 
330
  return old;
 
331
}
 
332
 
 
333
static int
 
334
sec_prot_internal(struct connectdata *conn, int level)
 
335
{
 
336
  char *p;
 
337
  unsigned int s = 1048576;
 
338
  ssize_t nread;
 
339
 
 
340
  if(!conn->sec_complete){
 
341
    infof(conn->data, "No security data exchange has taken place.\n");
 
342
    return -1;
 
343
  }
 
344
 
 
345
  if(level){
 
346
    int code;
 
347
    if(Curl_ftpsendf(conn, "PBSZ %u", s))
 
348
      return -1;
 
349
 
 
350
    if(Curl_GetFTPResponse(&nread, conn, &code))
 
351
      return -1;
 
352
 
 
353
    if(code/100 != '2'){
 
354
      failf(conn->data, "Failed to set protection buffer size.");
 
355
      return -1;
 
356
    }
 
357
    conn->buffer_size = s;
 
358
 
 
359
    p = strstr(conn->data->state.buffer, "PBSZ=");
 
360
    if(p)
 
361
      sscanf(p, "PBSZ=%u", &s);
 
362
    if(s < conn->buffer_size)
 
363
      conn->buffer_size = s;
 
364
  }
 
365
 
 
366
  if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
 
367
    return -1;
 
368
 
 
369
  if(Curl_GetFTPResponse(&nread, conn, NULL))
 
370
    return -1;
 
371
 
 
372
  if(conn->data->state.buffer[0] != '2'){
 
373
    failf(conn->data, "Failed to set protection level.");
 
374
    return -1;
 
375
  }
 
376
 
 
377
  conn->data_prot = (enum protection_level)level;
 
378
  return 0;
 
379
}
 
380
 
 
381
void
 
382
Curl_sec_set_protection_level(struct connectdata *conn)
 
383
{
 
384
  if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
 
385
    sec_prot_internal(conn, conn->request_data_prot);
 
386
}
 
387
 
 
388
 
 
389
int
 
390
Curl_sec_request_prot(struct connectdata *conn, const char *level)
 
391
{
 
392
  int l = name_to_level(level);
 
393
  if(l == -1)
 
394
    return -1;
 
395
  conn->request_data_prot = (enum protection_level)l;
 
396
  return 0;
 
397
}
 
398
 
 
399
int
 
400
Curl_sec_login(struct connectdata *conn)
 
401
{
 
402
  int ret;
 
403
  struct Curl_sec_client_mech **m;
 
404
  ssize_t nread;
 
405
  struct SessionHandle *data=conn->data;
 
406
  int ftpcode;
 
407
 
 
408
  for(m = mechs; *m && (*m)->name; m++) {
 
409
    void *tmp;
 
410
 
 
411
    tmp = realloc(conn->app_data, (*m)->size);
 
412
    if (tmp == NULL) {
 
413
      failf (data, "realloc %u failed", (*m)->size);
 
414
      return -1;
 
415
    }
 
416
    conn->app_data = tmp;
 
417
 
 
418
    if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
 
419
      infof(data, "Skipping %s...\n", (*m)->name);
 
420
      continue;
 
421
    }
 
422
    infof(data, "Trying %s...\n", (*m)->name);
 
423
 
 
424
    if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
 
425
      return -1;
 
426
 
 
427
    if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
 
428
      return -1;
 
429
 
 
430
    if(conn->data->state.buffer[0] != '3'){
 
431
      switch(ftpcode) {
 
432
      case 504:
 
433
        infof(data,
 
434
              "%s is not supported by the server.\n", (*m)->name);
 
435
        break;
 
436
      case 534:
 
437
        infof(data, "%s rejected as security mechanism.\n", (*m)->name);
 
438
        break;
 
439
      default:
 
440
        if(conn->data->state.buffer[0] == '5') {
 
441
          infof(data, "The server doesn't support the FTP "
 
442
                "security extensions.\n");
 
443
          return -1;
 
444
        }
 
445
        break;
 
446
      }
 
447
      continue;
 
448
    }
 
449
 
 
450
    ret = (*(*m)->auth)(conn->app_data, conn);
 
451
 
 
452
    if(ret == AUTH_CONTINUE)
 
453
      continue;
 
454
    else if(ret != AUTH_OK){
 
455
      /* mechanism is supposed to output error string */
 
456
      return -1;
 
457
    }
 
458
    conn->mech = *m;
 
459
    conn->sec_complete = 1;
 
460
    conn->command_prot = prot_safe;
 
461
    break;
 
462
  }
 
463
 
 
464
  return *m == NULL;
 
465
}
 
466
 
 
467
void
 
468
Curl_sec_end(struct connectdata *conn)
 
469
{
 
470
  if (conn->mech != NULL) {
 
471
    if(conn->mech->end)
 
472
      (conn->mech->end)(conn->app_data);
 
473
    memset(conn->app_data, 0, conn->mech->size);
 
474
    free(conn->app_data);
 
475
    conn->app_data = NULL;
 
476
  }
 
477
  conn->sec_complete = 0;
 
478
  conn->data_prot = (enum protection_level)0;
 
479
  conn->mech=NULL;
 
480
}
 
481
 
 
482
#endif /* HAVE_KRB4 */
 
483
#endif /* CURL_DISABLE_FTP */