/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: 2011-07-27 17:58:27 UTC
  • mto: (237.4.29 release)
  • mto: This revision was merged to the branch mainline in revision 490.
  • Revision ID: teddy@fukt.bsnet.se-20110727175827-nrd1ysjl4jrh6qw1
Tags: version-1.3.1-1
* Makefile (version): Changed to "1.3.1".
* NEWS (Version 1.3.1): New entry.
* debian/changelog (1.3.1-1): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
  return buffer_capacity;
188
188
}
189
189
 
190
 
/* Add server to set of servers to retry periodically */
191
190
int add_server(const char *ip, uint16_t port,
192
191
                 AvahiIfIndex if_index,
193
192
                 int af){
205
204
    perror_plus("strdup");
206
205
    return -1;
207
206
  }
208
 
  /* Special case of first server */
 
207
  /* unique case of first server */
209
208
  if (mc.current_server == NULL){
210
209
    new_server->next = new_server;
211
210
    new_server->prev = new_server;
212
211
    mc.current_server = new_server;
213
 
  /* Place the new server last in the list */
 
212
  /* Placing the new server last in the list */
214
213
  } else {
215
214
    new_server->next = mc.current_server;
216
215
    new_server->prev = mc.current_server->prev;
283
282
    return false;
284
283
  }
285
284
  
286
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
285
    /* Set GPGME home directory for the OpenPGP engine only */
287
286
  rc = gpgme_get_engine_info(&engine_info);
288
287
  if(rc != GPG_ERR_NO_ERROR){
289
288
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
1206
1205
  struct timespec now;
1207
1206
  struct timespec waited_time;
1208
1207
  intmax_t block_time;
1209
 
  
 
1208
 
1210
1209
  while(true){
1211
1210
    if(mc.current_server == NULL){
1212
1211
      if (debug){
1237
1236
      block_time = ((retry_interval
1238
1237
                     - ((intmax_t)waited_time.tv_sec * 1000))
1239
1238
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1240
 
      
 
1239
 
1241
1240
      if (debug){
1242
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1241
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
1243
1242
      }
1244
 
      
 
1243
 
1245
1244
      if(block_time <= 0){
1246
1245
        ret = start_mandos_communication(mc.current_server->ip,
1247
1246
                                         mc.current_server->port,
1412
1411
        errno = 0;
1413
1412
        retry_interval = strtod(arg, &tmp);
1414
1413
        if(errno != 0 or tmp == arg or *tmp != '\0'
1415
 
           or (retry_interval * 1000) > INT_MAX
1416
 
           or retry_interval < 0){
 
1414
           or (retry_interval * 1000) > INT_MAX){
1417
1415
          argp_error(state, "Bad retry interval");
1418
1416
        }
1419
1417
        break;
1470
1468
      perror_plus("seteuid");
1471
1469
    }
1472
1470
    
1473
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1474
 
      int seckey_fd = open(seckey, O_RDONLY);
1475
 
      if(seckey_fd == -1){
1476
 
        perror_plus("open");
 
1471
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
 
1472
    if(seckey_fd == -1){
 
1473
      perror_plus("open");
 
1474
    } else {
 
1475
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1476
      if(ret == -1){
 
1477
        perror_plus("fstat");
1477
1478
      } else {
1478
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1479
 
        if(ret == -1){
1480
 
          perror_plus("fstat");
1481
 
        } else {
1482
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1483
 
            ret = fchown(seckey_fd, uid, gid);
1484
 
            if(ret == -1){
1485
 
              perror_plus("fchown");
1486
 
            }
 
1479
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1480
          ret = fchown(seckey_fd, uid, gid);
 
1481
          if(ret == -1){
 
1482
            perror_plus("fchown");
1487
1483
          }
1488
1484
        }
1489
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1490
1485
      }
 
1486
      TEMP_FAILURE_RETRY(close(seckey_fd));
1491
1487
    }
1492
1488
    
1493
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1494
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1495
 
      if(pubkey_fd == -1){
1496
 
        perror_plus("open");
 
1489
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
 
1490
    if(pubkey_fd == -1){
 
1491
      perror_plus("open");
 
1492
    } else {
 
1493
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1494
      if(ret == -1){
 
1495
        perror_plus("fstat");
1497
1496
      } else {
1498
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1499
 
        if(ret == -1){
1500
 
          perror_plus("fstat");
1501
 
        } else {
1502
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1503
 
            ret = fchown(pubkey_fd, uid, gid);
1504
 
            if(ret == -1){
1505
 
              perror_plus("fchown");
1506
 
            }
 
1497
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1498
          ret = fchown(pubkey_fd, uid, gid);
 
1499
          if(ret == -1){
 
1500
            perror_plus("fchown");
1507
1501
          }
1508
1502
        }
1509
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1510
1503
      }
 
1504
      TEMP_FAILURE_RETRY(close(pubkey_fd));
1511
1505
    }
1512
1506
    
1513
1507
    /* Lower privileges */
1835
1829
    
1836
1830
    port = (uint16_t)tmpmax;
1837
1831
    *address = '\0';
 
1832
    address = connect_to;
1838
1833
    /* Colon in address indicates IPv6 */
1839
1834
    int af;
1840
 
    if(strchr(connect_to, ':') != NULL){
 
1835
    if(strchr(address, ':') != NULL){
1841
1836
      af = AF_INET6;
1842
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1843
 
      if(connect_to[0] == '[' and address[-1] == ']')
1844
 
        {
1845
 
          connect_to++;
1846
 
          address[-1] = '\0';
1847
 
        }
1848
1837
    } else {
1849
1838
      af = AF_INET;
1850
1839
    }
1851
 
    address = connect_to;
1852
1840
    
1853
1841
    if(quit_now){
1854
1842
      goto end;
1855
1843
    }
1856
 
    
 
1844
 
1857
1845
    while(not quit_now){
1858
1846
      ret = start_mandos_communication(address, port, if_index, af);
1859
1847
      if(quit_now or ret == 0){
1860
1848
        break;
1861
1849
      }
1862
 
      if(debug){
1863
 
        fprintf(stderr, "Retrying in %d seconds\n",
1864
 
                (int)retry_interval);
1865
 
      }
1866
 
      sleep((int)retry_interval);
1867
 
    }
1868
 
    
 
1850
      sleep((int)retry_interval or 1);
 
1851
    };
 
1852
 
1869
1853
    if (not quit_now){
1870
1854
      exitcode = EXIT_SUCCESS;
1871
1855
    }
2008
1992
  if(tempdir_created){
2009
1993
    struct dirent **direntries = NULL;
2010
1994
    struct dirent *direntry = NULL;
2011
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2012
 
                             alphasort);
2013
 
    if (numentries > 0){
2014
 
      for(int i = 0; i < numentries; i++){
 
1995
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
 
1996
    if (ret > 0){
 
1997
      for(int i = 0; i < ret; i++){
2015
1998
        direntry = direntries[i];
2016
1999
        char *fullname = NULL;
2017
2000
        ret = asprintf(&fullname, "%s/%s", tempdir,
2029
2012
      }
2030
2013
    }
2031
2014
 
2032
 
    /* need to clean even if 0 because man page doesn't specify */
 
2015
    /* need to be cleaned even if ret == 0 because man page doesn't
 
2016
       specify */
2033
2017
    free(direntries);
2034
 
    if (numentries == -1){
 
2018
    if (ret == -1){
2035
2019
      perror_plus("scandir");
2036
2020
    }
2037
2021
    ret = rmdir(tempdir);