/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandosclient.c

merge commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
36
 
35
37
#include <stdio.h>
36
38
#include <assert.h>
37
39
#include <stdlib.h>
77
79
 
78
80
bool debug = false;
79
81
 
 
82
const char mandos_protocol_version[] = "1";
 
83
 
80
84
typedef struct {
81
85
  AvahiSimplePoll *simple_poll;
82
86
  AvahiServer *server;
85
89
  const char *priority;
86
90
} mandos_context;
87
91
 
 
92
size_t adjustbuffer(char *buffer, size_t buffer_length,
 
93
                  size_t buffer_capacity){
 
94
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
 
95
    buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
 
96
    if (buffer == NULL){
 
97
      return 0;
 
98
    }
 
99
    buffer_capacity += BUFFER_SIZE;
 
100
  }
 
101
  return buffer_capacity;
 
102
}
 
103
 
88
104
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
89
105
                                   char **new_packet,
90
106
                                   const char *homedir){
92
108
  gpgme_ctx_t ctx;
93
109
  gpgme_error_t rc;
94
110
  ssize_t ret;
95
 
  ssize_t new_packet_capacity = 0;
 
111
  size_t new_packet_capacity = 0;
96
112
  ssize_t new_packet_length = 0;
97
113
  gpgme_engine_info_t engine_info;
98
114
 
205
221
  
206
222
  *new_packet = 0;
207
223
  while(true){
208
 
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
209
 
      *new_packet = realloc(*new_packet,
210
 
                            (unsigned int)new_packet_capacity
211
 
                            + BUFFER_SIZE);
212
 
      if (*new_packet == NULL){
213
 
        perror("realloc");
 
224
    new_packet_capacity = adjustbuffer(*new_packet, new_packet_length,
 
225
                                       new_packet_capacity);
 
226
    if (new_packet_capacity == 0){
 
227
        perror("adjustbuffer");
214
228
        return -1;
215
229
      }
216
230
      new_packet_capacity += BUFFER_SIZE;
362
376
  size_t buffer_length = 0;
363
377
  size_t buffer_capacity = 0;
364
378
  ssize_t decrypted_buffer_size;
365
 
  size_t written = 0;
 
379
  size_t written;
366
380
  int retval = 0;
367
381
  char interface[IF_NAMESIZE];
368
382
  
420
434
    perror("connect");
421
435
    return -1;
422
436
  }
423
 
  
 
437
 
 
438
  char *out = mandos_protocol_version;
 
439
  written = 0;
 
440
  while (true){
 
441
    size_t out_size = strlen(out);
 
442
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
443
                                   out_size - written));
 
444
    if (ret == -1){
 
445
      perror("write");
 
446
      retval = -1;
 
447
      goto end;
 
448
    }
 
449
    written += ret;
 
450
    if(written < out_size){
 
451
      continue;
 
452
    } else {
 
453
      if (out == mandos_protocol_version){
 
454
        written = 0;
 
455
        out = "\r\n";
 
456
      } else {
 
457
        break;
 
458
      }
 
459
    }
 
460
  }
 
461
 
424
462
  ret = initgnutls (&es);
425
463
  if (ret != 0){
426
464
    retval = -1;
453
491
  }
454
492
 
455
493
  while(true){
456
 
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
457
 
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
458
 
      if (buffer == NULL){
459
 
        perror("realloc");
460
 
        goto exit;
461
 
      }
462
 
      buffer_capacity += BUFFER_SIZE;
 
494
    buffer_capacity = adjustbuffer(buffer, buffer_length, buffer_capacity);
 
495
    if (buffer_capacity == 0){
 
496
      perror("adjustbuffer");
 
497
      retval = -1;
 
498
      goto exit;
463
499
    }
464
500
    
465
501
    ret = gnutls_record_recv
499
535
                                               &decrypted_buffer,
500
536
                                               certdir);
501
537
    if (decrypted_buffer_size >= 0){
 
538
      written = 0;
502
539
      while(written < (size_t) decrypted_buffer_size){
503
540
        ret = (int)fwrite (decrypted_buffer + written, 1,
504
541
                           (size_t)decrypted_buffer_size - written,