/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/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-03-10 07:04:09 UTC
  • Revision ID: teddy@recompile.se-20140310070409-ho6p1wrnpopi2ny1
White space fix: change "if (" to "if(" in C code.

* plugins.d/askpass-fifo.c (error_plus): White space fix.
* plugins.d/password-prompt.c (error_plus, conflict_detection,
                               main): - '' -
* plugins.d/plymouth.c (error_plus, exec_and_wait, get_pid): - '' -
* plugins.d/splashy.c (error_plus): - '' -
* plugins.d/usplash.c (error_plus): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2012 Teddy Hogeborn
13
 
 * Copyright © 2008-2012 Björn Påhlsson
 
12
 * Copyright © 2008-2013 Teddy Hogeborn
 
13
 * Copyright © 2008-2013 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
55
55
                                   opendir(), DIR */
56
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
 
                                   inet_pton(), connect() */
 
58
                                   inet_pton(), connect(),
 
59
                                   getnameinfo() */
59
60
#include <fcntl.h>              /* open() */
60
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
62
                                 */
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
76
                                   setgid(), pause(), _exit() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
91
92
                                   argz_delete(), argz_append(),
92
93
                                   argz_stringify(), argz_add(),
93
94
                                   argz_count() */
 
95
#include <netdb.h>              /* getnameinfo(), NI_NUMERICHOST,
 
96
                                   EAI_SYSTEM, gai_strerror() */
94
97
 
95
98
#ifdef __linux__
96
99
#include <sys/klog.h>           /* klogctl() */
187
190
  
188
191
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
189
192
                             program_invocation_short_name));
190
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
193
  return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
191
194
}
192
195
 
193
196
/*
198
201
size_t incbuffer(char **buffer, size_t buffer_length,
199
202
                 size_t buffer_capacity){
200
203
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
201
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
202
 
    if(buffer == NULL){
 
204
    char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
205
    if(new_buf == NULL){
 
206
      int old_errno = errno;
 
207
      free(*buffer);
 
208
      errno = old_errno;
 
209
      *buffer = NULL;
203
210
      return 0;
204
211
    }
 
212
    *buffer = new_buf;
205
213
    buffer_capacity += BUFFER_SIZE;
206
214
  }
207
215
  return buffer_capacity;
224
232
    perror_plus("strdup");
225
233
    return false;
226
234
  }
 
235
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
 
236
  if(ret == -1){
 
237
    perror_plus("clock_gettime");
 
238
    return false;
 
239
  }
227
240
  /* Special case of first server */
228
241
  if(*current_server == NULL){
229
242
    new_server->next = new_server;
236
249
    new_server->prev->next = new_server;
237
250
    (*current_server)->prev = new_server;
238
251
  }
239
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
240
 
  if(ret == -1){
241
 
    perror_plus("clock_gettime");
242
 
    return false;
243
 
  }
244
252
  return true;
245
253
}
246
254
 
626
634
                                      int af, mandos_context *mc){
627
635
  int ret, tcp_sd = -1;
628
636
  ssize_t sret;
629
 
  union {
630
 
    struct sockaddr_in in;
631
 
    struct sockaddr_in6 in6;
632
 
  } to;
 
637
  struct sockaddr_storage to;
633
638
  char *buffer = NULL;
634
639
  char *decrypted_buffer = NULL;
635
640
  size_t buffer_length = 0;
716
721
  
717
722
  memset(&to, 0, sizeof(to));
718
723
  if(af == AF_INET6){
719
 
    to.in6.sin6_family = (sa_family_t)af;
720
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
724
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
725
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
721
726
  } else {                      /* IPv4 */
722
 
    to.in.sin_family = (sa_family_t)af;
723
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
727
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
728
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
724
729
  }
725
730
  if(ret < 0 ){
726
731
    int e = errno;
735
740
    goto mandos_end;
736
741
  }
737
742
  if(af == AF_INET6){
738
 
    to.in6.sin6_port = htons(port);    
739
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
740
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
741
 
                                -Wunreachable-code*/
 
743
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);    
 
744
    if(IN6_IS_ADDR_LINKLOCAL
 
745
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
742
746
      if(if_index == AVAHI_IF_UNSPEC){
743
747
        fprintf_plus(stderr, "An IPv6 link-local address is"
744
748
                     " incomplete without a network interface\n");
746
750
        goto mandos_end;
747
751
      }
748
752
      /* Set the network interface number as scope */
749
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
753
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
750
754
    }
751
755
  } else {
752
 
    to.in.sin_port = htons(port); /* Spurious warnings from
753
 
                                     -Wconversion and
754
 
                                     -Wunreachable-code */
 
756
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
755
757
  }
756
758
  
757
759
  if(quit_now){
774
776
    }
775
777
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
776
778
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
777
 
    const char *pcret;
778
779
    if(af == AF_INET6){
779
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
780
 
                        sizeof(addrstr));
 
780
      ret = getnameinfo((struct sockaddr *)&to,
 
781
                        sizeof(struct sockaddr_in6),
 
782
                        addrstr, sizeof(addrstr), NULL, 0,
 
783
                        NI_NUMERICHOST);
781
784
    } else {
782
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
783
 
                        sizeof(addrstr));
 
785
      ret = getnameinfo((struct sockaddr *)&to,
 
786
                        sizeof(struct sockaddr_in),
 
787
                        addrstr, sizeof(addrstr), NULL, 0,
 
788
                        NI_NUMERICHOST);
784
789
    }
785
 
    if(pcret == NULL){
786
 
      perror_plus("inet_ntop");
787
 
    } else {
788
 
      if(strcmp(addrstr, ip) != 0){
789
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
790
 
      }
 
790
    if(ret == EAI_SYSTEM){
 
791
      perror_plus("getnameinfo");
 
792
    } else if(ret != 0) {
 
793
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
794
    } else if(strcmp(addrstr, ip) != 0){
 
795
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
791
796
    }
792
797
  }
793
798
  
797
802
  }
798
803
  
799
804
  if(af == AF_INET6){
800
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
805
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
806
                  sizeof(struct sockaddr_in6));
801
807
  } else {
802
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
808
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
809
                  sizeof(struct sockaddr_in));
803
810
  }
804
811
  if(ret < 0){
805
812
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
1484
1491
bool run_network_hooks(const char *mode, const char *interface,
1485
1492
                       const float delay){
1486
1493
  struct dirent **direntries;
1487
 
  struct dirent *direntry;
1488
 
  int ret;
1489
1494
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1490
1495
                         alphasort);
1491
1496
  if(numhooks == -1){
1498
1503
      perror_plus("scandir");
1499
1504
    }
1500
1505
  } else {
 
1506
    struct dirent *direntry;
 
1507
    int ret;
1501
1508
    int devnull = open("/dev/null", O_RDONLY);
1502
1509
    for(int i = 0; i < numhooks; i++){
1503
1510
      direntry = direntries[i];
1723
1730
}
1724
1731
 
1725
1732
error_t take_down_interface(const char *const interface){
1726
 
  int sd = -1;
1727
1733
  error_t old_errno = errno;
1728
 
  error_t ret_errno = 0;
1729
 
  int ret, ret_setflags;
1730
1734
  struct ifreq network;
1731
1735
  unsigned int if_index = if_nametoindex(interface);
1732
1736
  if(if_index == 0){
1735
1739
    return ENXIO;
1736
1740
  }
1737
1741
  if(interface_is_up(interface)){
 
1742
    error_t ret_errno = 0;
1738
1743
    if(not get_flags(interface, &network) and debug){
1739
1744
      ret_errno = errno;
1740
1745
      fprintf_plus(stderr, "Failed to get flags for interface "
1743
1748
    }
1744
1749
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1745
1750
    
1746
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1751
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1747
1752
    if(sd < 0){
1748
1753
      ret_errno = errno;
1749
1754
      perror_plus("socket");
1759
1764
    /* Raise priviliges */
1760
1765
    raise_privileges();
1761
1766
    
1762
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1767
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1763
1768
    ret_errno = errno;
1764
1769
    
1765
1770
    /* Lower privileges */
1766
1771
    lower_privileges();
1767
1772
    
1768
1773
    /* Close the socket */
1769
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1774
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1770
1775
    if(ret == -1){
1771
1776
      perror_plus("close");
1772
1777
    }
1805
1810
  const char *seckey = PATHDIR "/" SECKEY;
1806
1811
  const char *pubkey = PATHDIR "/" PUBKEY;
1807
1812
  char *interfaces_hooks = NULL;
1808
 
  size_t interfaces_hooks_size = 0;
1809
1813
  
1810
1814
  bool gnutls_initialized = false;
1811
1815
  bool gpgme_initialized = false;
2066
2070
        goto end;
2067
2071
      }
2068
2072
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2069
 
      interfaces_hooks_size = mc.interfaces_size;
2070
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2071
 
                     (int)',');
 
2073
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2072
2074
    }
2073
2075
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2074
2076
                             interfaces_hooks : "", delay)){
2167
2169
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2168
2170
                             direntries[i]->d_name);
2169
2171
        if(ret_errno != 0){
 
2172
          errno = ret_errno;
2170
2173
          perror_plus("argz_add");
2171
2174
          continue;
2172
2175
        }
2215
2218
          ret_errno = argz_add(&interfaces_to_take_down,
2216
2219
                               &interfaces_to_take_down_size,
2217
2220
                               interface);
 
2221
          if(ret_errno != 0){
 
2222
            errno = ret_errno;
 
2223
            perror_plus("argz_add");
 
2224
          }
2218
2225
        }
2219
2226
      }
2220
2227
    }
2331
2338
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2332
2339
                     (int)retry_interval);
2333
2340
      }
2334
 
      sleep((int)retry_interval);
 
2341
      sleep((unsigned int)retry_interval);
2335
2342
    }
2336
2343
    
2337
2344
    if (not quit_now){