26
26
* along with this program. If not, see
27
27
* <http://www.gnu.org/licenses/>.
29
* Contact the authors at <mandos@recompile.se>.
29
* Contact the authors at <mandos@fukt.bsnet.se>.
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
127
127
bool debug = false;
128
128
static const char mandos_protocol_version[] = "1";
129
129
const char *argp_program_version = "mandos-client " VERSION;
130
const char *argp_program_bug_address = "<mandos@recompile.se>";
130
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
131
static const char sys_class_net[] = "/sys/class/net";
132
132
char *connect_to = NULL;
205
204
perror_plus("strdup");
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 */
215
214
new_server->next = mc.current_server;
216
215
new_server->prev = mc.current_server->prev;
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",
1093
1092
int good_interface(const struct dirent *if_entry){
1094
char *flagname = NULL;
1096
1095
if(if_entry->d_name[0] == '.'){
1099
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1101
perror_plus("socket");
1105
strcpy(ifr.ifr_name, if_entry->d_name);
1106
ret = ioctl(s, SIOCGIFFLAGS, &ifr);
1098
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1101
perror_plus("asprintf");
1104
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
perror_plus("open");
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
/* read line from flags_fd */
1113
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
flagstring[(size_t)to_read] = '\0';
1116
if(flagstring == NULL){
1117
perror_plus("malloc");
1122
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1125
perror_plus("read");
1139
tmpmax = strtoimax(flagstring, &tmp, 0);
1140
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
and not (isspace(*tmp)))
1142
or tmpmax != (ifreq_flags)tmpmax){
1109
perror_plus("ioctl SIOCGIFFLAGS");
1144
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
flagstring, if_entry->d_name);
1151
ifreq_flags flags = (ifreq_flags)tmpmax;
1113
1152
/* Reject the loopback device */
1114
if(ifr.ifr_flags & IFF_LOOPBACK){
1153
if(flags & IFF_LOOPBACK){
1116
1155
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1117
1156
if_entry->d_name);
1121
1160
/* Accept point-to-point devices only if connect_to is specified */
1122
if(connect_to != NULL and (ifr.ifr_flags & IFF_POINTOPOINT)){
1161
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1124
1163
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1125
1164
if_entry->d_name);
1129
1168
/* Otherwise, reject non-broadcast-capable devices */
1130
if(not (ifr.ifr_flags & IFF_BROADCAST)){
1169
if(not (flags & IFF_BROADCAST)){
1132
1171
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1133
1172
if_entry->d_name);
1197
1236
block_time = ((retry_interval
1198
1237
- ((intmax_t)waited_time.tv_sec * 1000))
1199
1238
- ((intmax_t)waited_time.tv_nsec / 1000000));
1202
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1205
1244
if(block_time <= 0){
1206
1245
ret = start_mandos_communication(mc.current_server->ip,
1207
1246
mc.current_server->port,
1373
1412
retry_interval = strtod(arg, &tmp);
1374
1413
if(errno != 0 or tmp == arg or *tmp != '\0'
1375
or (retry_interval * 1000) > INT_MAX
1376
or retry_interval < 0){
1414
or (retry_interval * 1000) > INT_MAX){
1377
1415
argp_error(state, "Bad retry interval");
1430
1468
perror_plus("seteuid");
1433
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1434
int seckey_fd = open(seckey, O_RDONLY);
1435
if(seckey_fd == -1){
1436
perror_plus("open");
1471
int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
if(seckey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1477
perror_plus("fstat");
1438
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1440
perror_plus("fstat");
1442
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1443
ret = fchown(seckey_fd, uid, gid);
1445
perror_plus("fchown");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(seckey_fd, uid, gid);
1482
perror_plus("fchown");
1449
TEMP_FAILURE_RETRY(close(seckey_fd));
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
1453
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1454
int pubkey_fd = open(pubkey, O_RDONLY);
1455
if(pubkey_fd == -1){
1456
perror_plus("open");
1489
int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
if(pubkey_fd == -1){
1491
perror_plus("open");
1493
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1495
perror_plus("fstat");
1458
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1460
perror_plus("fstat");
1462
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1463
ret = fchown(pubkey_fd, uid, gid);
1465
perror_plus("fchown");
1497
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
ret = fchown(pubkey_fd, uid, gid);
1500
perror_plus("fchown");
1469
TEMP_FAILURE_RETRY(close(pubkey_fd));
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1473
1507
/* Lower privileges */
1796
1830
port = (uint16_t)tmpmax;
1797
1831
*address = '\0';
1832
address = connect_to;
1798
1833
/* Colon in address indicates IPv6 */
1800
if(strchr(connect_to, ':') != NULL){
1835
if(strchr(address, ':') != NULL){
1802
/* Accept [] around IPv6 address - see RFC 5952 */
1803
if(connect_to[0] == '[' and address[-1] == ']')
1811
address = connect_to;
1817
1845
while(not quit_now){
1818
1846
ret = start_mandos_communication(address, port, if_index, af);
1819
1847
if(quit_now or ret == 0){
1823
fprintf(stderr, "Retrying in %d seconds\n",
1824
(int)retry_interval);
1826
sleep((int)retry_interval);
1850
sleep((int)retry_interval or 1);
1829
1853
if (not quit_now){
1830
1854
exitcode = EXIT_SUCCESS;
1968
1992
if(tempdir_created){
1969
1993
struct dirent **direntries = NULL;
1970
1994
struct dirent *direntry = NULL;
1971
int numentries = scandir(tempdir, &direntries, notdotentries,
1973
if (numentries > 0){
1974
for(int i = 0; i < numentries; i++){
1995
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1997
for(int i = 0; i < ret; i++){
1975
1998
direntry = direntries[i];
1976
1999
char *fullname = NULL;
1977
2000
ret = asprintf(&fullname, "%s/%s", tempdir,