/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 at bsnet
  • Date: 2011-12-24 23:17:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20111224231702-1ffgu6r02p0bz9co
* plugins.d/splashy.c (error_plus): Check format string.
* plugins.d/askpass-fifo.c (error_plus): - '' -
* plugins.d/plymouth.c (error_plus): - '' -
* plugins.d/password-prompt.c (error_plus): - '' -
* plugins.d/usplash.c (error_plus): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause() */
 
76
                                   setgid(), pause(), _exit() */
77
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h>             /* not, or, and */
79
79
#include <argp.h>               /* struct argp_option, error_t, struct
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
89
                                   WEXITSTATUS(), WTERMSIG() */
 
90
#include <grp.h>                /* setgroups() */
90
91
 
91
92
#ifdef __linux__
92
93
#include <sys/klog.h>           /* klogctl() */
174
175
  perror(print_text);
175
176
}
176
177
 
 
178
__attribute__((format (gnu_printf, 2, 3)))
177
179
int fprintf_plus(FILE *stream, const char *format, ...){
178
180
  va_list ap;
179
181
  va_start (ap, format);
201
203
}
202
204
 
203
205
/* Add server to set of servers to retry periodically */
204
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
205
 
               int af){
 
206
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
207
                int af){
206
208
  int ret;
207
209
  server *new_server = malloc(sizeof(server));
208
210
  if(new_server == NULL){
209
211
    perror_plus("malloc");
210
 
    return -1;
 
212
    return false;
211
213
  }
212
214
  *new_server = (server){ .ip = strdup(ip),
213
215
                          .port = port,
215
217
                          .af = af };
216
218
  if(new_server->ip == NULL){
217
219
    perror_plus("strdup");
218
 
    return -1;
 
220
    return false;
219
221
  }
220
222
  /* Special case of first server */
221
223
  if (mc.current_server == NULL){
232
234
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
233
235
  if(ret == -1){
234
236
    perror_plus("clock_gettime");
235
 
    return -1;
 
237
    return false;
236
238
  }
237
 
  return 0;
 
239
  return true;
238
240
}
239
241
 
240
242
/* 
1029
1031
      if(ret == 0){
1030
1032
        avahi_simple_poll_quit(mc.simple_poll);
1031
1033
      } else {
1032
 
        ret = add_server(ip, port, interface,
1033
 
                         avahi_proto_to_af(proto));
 
1034
        if(not add_server(ip, port, interface,
 
1035
                          avahi_proto_to_af(proto))){
 
1036
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1037
                       " list\n", name);
 
1038
        }
1034
1039
      }
1035
1040
    }
1036
1041
  }
1406
1411
      pid_t hook_pid = fork();
1407
1412
      if(hook_pid == 0){
1408
1413
        /* Child */
 
1414
        /* Raise privileges */
 
1415
        errno = 0;
 
1416
        ret = seteuid(0);
 
1417
        if(ret == -1){
 
1418
          perror_plus("seteuid");
 
1419
        }
 
1420
        /* Raise privileges even more */
 
1421
        errno = 0;
 
1422
        ret = setuid(0);
 
1423
        if(ret == -1){
 
1424
          perror_plus("setuid");
 
1425
        }
 
1426
        /* Set group */
 
1427
        errno = 0;
 
1428
        ret = setgid(0);
 
1429
        if(ret == -1){
 
1430
          perror_plus("setgid");
 
1431
        }
 
1432
        /* Reset supplementary groups */
 
1433
        errno = 0;
 
1434
        ret = setgroups(0, NULL);
 
1435
        if(ret == -1){
 
1436
          perror_plus("setgroups");
 
1437
        }
1409
1438
        dup2(devnull, STDIN_FILENO);
1410
1439
        close(devnull);
1411
1440
        dup2(STDERR_FILENO, STDOUT_FILENO);
1412
1441
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1413
1442
        if(ret == -1){
1414
1443
          perror_plus("setenv");
1415
 
          return false;
 
1444
          _exit(EX_OSERR);
1416
1445
        }
1417
1446
        ret = setenv("DEVICE", interface, 1);
1418
1447
        if(ret == -1){
1419
1448
          perror_plus("setenv");
1420
 
          return false;
 
1449
          _exit(EX_OSERR);
1421
1450
        }
1422
1451
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1423
1452
        if(ret == -1){
1424
1453
          perror_plus("setenv");
1425
 
          return false;
 
1454
          _exit(EX_OSERR);
1426
1455
        }
1427
1456
        ret = setenv("MODE", mode, 1);
1428
1457
        if(ret == -1){
1429
1458
          perror_plus("setenv");
1430
 
          return false;
 
1459
          _exit(EX_OSERR);
1431
1460
        }
1432
1461
        char *delaystring;
1433
1462
        ret = asprintf(&delaystring, "%f", delay);
1434
1463
        if(ret == -1){
1435
1464
          perror_plus("asprintf");
1436
 
          return false;
 
1465
          _exit(EX_OSERR);
1437
1466
        }
1438
1467
        ret = setenv("DELAY", delaystring, 1);
1439
1468
        if(ret == -1){
1440
1469
          free(delaystring);
1441
1470
          perror_plus("setenv");
1442
 
          return false;
 
1471
          _exit(EX_OSERR);
1443
1472
        }
1444
1473
        free(delaystring);
1445
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1446
 
        perror_plus("execl");
 
1474
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1475
          perror_plus("execl");
 
1476
          _exit(EXIT_FAILURE);
 
1477
        }
1447
1478
      } else {
1448
1479
        int status;
1449
1480
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1646
1677
        argp_state_help(state, state->out_stream,
1647
1678
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1648
1679
      case 'V':                 /* --version */
1649
 
        fprintf_plus(state->out_stream,
1650
 
                     "Mandos plugin mandos-client: ");
1651
1680
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1652
1681
        exit(argp_err_exit_status);
1653
1682
        break;
1678
1707
    }
1679
1708
  }
1680
1709
    
1681
 
  if(getuid() == 0){
 
1710
  {
1682
1711
    /* Work around Debian bug #633582:
1683
1712
       <http://bugs.debian.org/633582> */
1684
 
    struct stat st;
1685
1713
    
1686
1714
    /* Re-raise priviliges */
1687
1715
    errno = 0;
1688
1716
    ret = seteuid(0);
1689
1717
    if(ret == -1){
1690
1718
      perror_plus("seteuid");
1691
 
    }
1692
 
    
1693
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1694
 
      int seckey_fd = open(seckey, O_RDONLY);
1695
 
      if(seckey_fd == -1){
1696
 
        perror_plus("open");
1697
 
      } else {
1698
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1699
 
        if(ret == -1){
1700
 
          perror_plus("fstat");
1701
 
        } else {
1702
 
          if(S_ISREG(st.st_mode)
1703
 
             and st.st_uid == 0 and st.st_gid == 0){
1704
 
            ret = fchown(seckey_fd, uid, gid);
1705
 
            if(ret == -1){
1706
 
              perror_plus("fchown");
1707
 
            }
1708
 
          }
1709
 
        }
1710
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1711
 
      }
1712
 
    }
1713
 
    
1714
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1715
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1716
 
      if(pubkey_fd == -1){
1717
 
        perror_plus("open");
1718
 
      } else {
1719
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1720
 
        if(ret == -1){
1721
 
          perror_plus("fstat");
1722
 
        } else {
1723
 
          if(S_ISREG(st.st_mode)
1724
 
             and st.st_uid == 0 and st.st_gid == 0){
1725
 
            ret = fchown(pubkey_fd, uid, gid);
1726
 
            if(ret == -1){
1727
 
              perror_plus("fchown");
1728
 
            }
1729
 
          }
1730
 
        }
1731
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1732
 
      }
1733
 
    }
1734
 
    
1735
 
    /* Lower privileges */
1736
 
    errno = 0;
1737
 
    ret = seteuid(uid);
1738
 
    if(ret == -1){
1739
 
      perror_plus("seteuid");
1740
 
    }
1741
 
  }
1742
 
  
1743
 
  /* Run network hooks */
1744
 
  {
1745
 
    if(getuid() == 0){
1746
 
      /* Re-raise priviliges */
1747
 
      errno = 0;
1748
 
      ret = seteuid(0);
1749
 
      if(ret == -1){
1750
 
        perror_plus("seteuid");
1751
 
      }
1752
 
    }
1753
 
    if(not run_network_hooks("start", interface, delay)){
1754
 
      goto end;
1755
 
    }
1756
 
    if(getuid() == 0){
 
1719
    } else {
 
1720
      struct stat st;
 
1721
      
 
1722
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1723
        int seckey_fd = open(seckey, O_RDONLY);
 
1724
        if(seckey_fd == -1){
 
1725
          perror_plus("open");
 
1726
        } else {
 
1727
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1728
          if(ret == -1){
 
1729
            perror_plus("fstat");
 
1730
          } else {
 
1731
            if(S_ISREG(st.st_mode)
 
1732
               and st.st_uid == 0 and st.st_gid == 0){
 
1733
              ret = fchown(seckey_fd, uid, gid);
 
1734
              if(ret == -1){
 
1735
                perror_plus("fchown");
 
1736
              }
 
1737
            }
 
1738
          }
 
1739
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
1740
        }
 
1741
      }
 
1742
    
 
1743
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1744
        int pubkey_fd = open(pubkey, O_RDONLY);
 
1745
        if(pubkey_fd == -1){
 
1746
          perror_plus("open");
 
1747
        } else {
 
1748
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1749
          if(ret == -1){
 
1750
            perror_plus("fstat");
 
1751
          } else {
 
1752
            if(S_ISREG(st.st_mode)
 
1753
               and st.st_uid == 0 and st.st_gid == 0){
 
1754
              ret = fchown(pubkey_fd, uid, gid);
 
1755
              if(ret == -1){
 
1756
                perror_plus("fchown");
 
1757
              }
 
1758
            }
 
1759
          }
 
1760
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1761
        }
 
1762
      }
 
1763
    
1757
1764
      /* Lower privileges */
1758
1765
      errno = 0;
1759
1766
      ret = seteuid(uid);
1763
1770
    }
1764
1771
  }
1765
1772
  
 
1773
  /* Run network hooks */
 
1774
  if(not run_network_hooks("start", interface, delay)){
 
1775
    goto end;
 
1776
  }
 
1777
  
1766
1778
  if(not debug){
1767
1779
    avahi_set_log_function(empty_log);
1768
1780
  }
2217
2229
    }
2218
2230
  }
2219
2231
  
 
2232
  /* Run network hooks */
 
2233
  run_network_hooks("stop", interface, delay);
 
2234
  
2220
2235
  /* Re-raise priviliges */
2221
2236
  {
2222
 
    if(getuid() == 0){
2223
 
      errno = 0;
2224
 
      ret = seteuid(0);
2225
 
      if(ret == -1){
2226
 
        perror_plus("seteuid");
2227
 
      }
 
2237
    errno = 0;
 
2238
    ret = seteuid(0);
 
2239
    if(ret == -1){
 
2240
      perror_plus("seteuid");
2228
2241
    }
2229
2242
    
2230
 
    /* Run network hooks */
2231
 
    run_network_hooks("stop", interface, delay);
2232
 
    
2233
2243
    /* Take down the network interface */
2234
2244
    if(take_down_interface and geteuid() == 0){
2235
2245
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2248
2258
      }
2249
2259
    }
2250
2260
  }
2251
 
  if(getuid() == 0){
2252
 
    /* Lower privileges permanently */
2253
 
    errno = 0;
2254
 
    ret = setuid(uid);
2255
 
    if(ret == -1){
2256
 
      perror_plus("setuid");
2257
 
    }
 
2261
  /* Lower privileges permanently */
 
2262
  errno = 0;
 
2263
  ret = setuid(uid);
 
2264
  if(ret == -1){
 
2265
    perror_plus("setuid");
2258
2266
  }
2259
2267
  
2260
2268
  /* Removes the GPGME temp directory and all files inside */