/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

fixed incorrect include comments

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
 
74
                                   getuid(), getgid(), setuid(),
75
75
                                   setgid() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
474
474
static int init_gnutls_session(gnutls_session_t *session){
475
475
  int ret;
476
476
  /* GnuTLS session creation */
477
 
  do {
478
 
    ret = gnutls_init(session, GNUTLS_SERVER);
479
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
477
  ret = gnutls_init(session, GNUTLS_SERVER);
480
478
  if(ret != GNUTLS_E_SUCCESS){
481
479
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
482
480
            safer_gnutls_strerror(ret));
484
482
  
485
483
  {
486
484
    const char *err;
487
 
    do {
488
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
489
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
485
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
490
486
    if(ret != GNUTLS_E_SUCCESS){
491
487
      fprintf(stderr, "Syntax error at: %s\n", err);
492
488
      fprintf(stderr, "GnuTLS error: %s\n",
496
492
    }
497
493
  }
498
494
  
499
 
  do {
500
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
501
 
                                 mc.cred);
502
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
495
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
496
                               mc.cred);
503
497
  if(ret != GNUTLS_E_SUCCESS){
504
498
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
505
499
            safer_gnutls_strerror(ret));
508
502
  }
509
503
  
510
504
  /* ignore client certificate if any. */
511
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
505
  gnutls_certificate_server_set_request(*session,
 
506
                                        GNUTLS_CERT_IGNORE);
512
507
  
513
508
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
514
509
  
1000
995
  bool gpgme_initialized = false;
1001
996
  float delay = 2.5f;
1002
997
  
1003
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
998
  struct sigaction old_sigterm_action;
1004
999
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1005
1000
  
1006
 
  uid = getuid();
1007
 
  gid = getgid();
1008
 
  
1009
 
  /* Lower any group privileges we might have, just to be safe */
1010
 
  errno = 0;
1011
 
  ret = setgid(gid);
1012
 
  if(ret == -1){
1013
 
    perror("setgid");
1014
 
  }
1015
 
  
1016
 
  /* Lower user privileges (temporarily) */
1017
 
  errno = 0;
1018
 
  ret = seteuid(uid);
1019
 
  if(ret == -1){
1020
 
    perror("seteuid");
1021
 
  }
1022
 
  
1023
 
  if(quit_now){
1024
 
    goto end;
1025
 
  }
1026
 
  
1027
1001
  {
1028
1002
    struct argp_option options[] = {
1029
1003
      { .name = "debug", .key = 128,
1213
1187
      goto end;
1214
1188
    }
1215
1189
    
1216
 
    /* Re-raise priviliges */
1217
 
    errno = 0;
1218
 
    ret = seteuid(0);
1219
 
    if(ret == -1){
1220
 
      perror("seteuid");
1221
 
    }
1222
 
    
1223
1190
#ifdef __linux__
1224
1191
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1225
1192
       messages to mess up the prompt */
1243
1210
        }
1244
1211
      }
1245
1212
#endif  /* __linux__ */
1246
 
      /* Lower privileges */
1247
 
      errno = 0;
1248
 
      ret = seteuid(uid);
1249
 
      if(ret == -1){
1250
 
        perror("seteuid");
1251
 
      }
1252
1213
      goto end;
1253
1214
    }
1254
1215
    strcpy(network.ifr_name, interface);
1264
1225
      }
1265
1226
#endif  /* __linux__ */
1266
1227
      exitcode = EXIT_FAILURE;
1267
 
      /* Lower privileges */
1268
 
      errno = 0;
1269
 
      ret = seteuid(uid);
1270
 
      if(ret == -1){
1271
 
        perror("seteuid");
1272
 
      }
1273
1228
      goto end;
1274
1229
    }
1275
1230
    if((network.ifr_flags & IFF_UP) == 0){
1288
1243
          }
1289
1244
        }
1290
1245
#endif  /* __linux__ */
1291
 
        /* Lower privileges */
1292
 
        errno = 0;
1293
 
        ret = seteuid(uid);
1294
 
        if(ret == -1){
1295
 
          perror("seteuid");
1296
 
        }
1297
1246
        goto end;
1298
1247
      }
1299
1248
    }
1327
1276
      }
1328
1277
    }
1329
1278
#endif  /* __linux__ */
1330
 
    /* Lower privileges */
1331
 
    errno = 0;
1332
 
    if(take_down_interface){
1333
 
      /* Lower privileges */
1334
 
      ret = seteuid(uid);
1335
 
      if(ret == -1){
1336
 
        perror("seteuid");
1337
 
      }
1338
 
    } else {
1339
 
      /* Lower privileges permanently */
1340
 
      ret = setuid(uid);
1341
 
      if(ret == -1){
1342
 
        perror("setuid");
1343
 
      }
1344
 
    }
 
1279
  }
 
1280
  
 
1281
  if(quit_now){
 
1282
    goto end;
 
1283
  }
 
1284
  
 
1285
  uid = getuid();
 
1286
  gid = getgid();
 
1287
  
 
1288
  errno = 0;
 
1289
  setgid(gid);
 
1290
  if(ret == -1){
 
1291
    perror("setgid");
 
1292
  }
 
1293
  
 
1294
  ret = setuid(uid);
 
1295
  if(ret == -1){
 
1296
    perror("setuid");
1345
1297
  }
1346
1298
  
1347
1299
  if(quit_now){
1521
1473
  
1522
1474
  /* Take down the network interface */
1523
1475
  if(take_down_interface){
1524
 
    /* Re-raise priviliges */
1525
 
    errno = 0;
1526
 
    ret = seteuid(0);
 
1476
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1527
1477
    if(ret == -1){
1528
 
      perror("seteuid");
 
1478
      perror("ioctl SIOCGIFFLAGS");
 
1479
    } else if(network.ifr_flags & IFF_UP) {
 
1480
      network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1481
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1482
      if(ret == -1){
 
1483
        perror("ioctl SIOCSIFFLAGS");
 
1484
      }
1529
1485
    }
1530
 
    if(geteuid() == 0){
1531
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1532
 
      if(ret == -1){
1533
 
        perror("ioctl SIOCGIFFLAGS");
1534
 
      } else if(network.ifr_flags & IFF_UP) {
1535
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1536
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1537
 
        if(ret == -1){
1538
 
          perror("ioctl SIOCSIFFLAGS");
1539
 
        }
1540
 
      }
1541
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1542
 
      if(ret == -1){
1543
 
        perror("close");
1544
 
      }
1545
 
      /* Lower privileges permanently */
1546
 
      errno = 0;
1547
 
      ret = setuid(uid);
1548
 
      if(ret == -1){
1549
 
        perror("setuid");
1550
 
      }
 
1486
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1487
    if(ret == -1){
 
1488
      perror("close");
1551
1489
    }
1552
1490
  }
1553
1491