26
26
* along with this program. If not, see
27
27
* <http://www.gnu.org/licenses/>.
29
* Contact the authors at <mandos@fukt.bsnet.se>.
29
* Contact the authors at <mandos@recompile.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@fukt.bsnet.se>";
130
const char *argp_program_bug_address = "<mandos@recompile.se>";
131
131
static const char sys_class_net[] = "/sys/class/net";
132
132
char *connect_to = NULL;
204
205
perror_plus("strdup");
207
/* unique case of first server */
208
/* Special case of first server */
208
209
if (mc.current_server == NULL){
209
210
new_server->next = new_server;
210
211
new_server->prev = new_server;
211
212
mc.current_server = new_server;
212
/* Placing the new server last in the list */
213
/* Place the new server last in the list */
214
215
new_server->next = mc.current_server;
215
216
new_server->prev = mc.current_server->prev;
285
/* Set GPGME home directory for the OpenPGP engine only */
286
/* Set GPGME home directory for the OpenPGP engine only */
286
287
rc = gpgme_get_engine_info(&engine_info);
287
288
if(rc != GPG_ERR_NO_ERROR){
288
289
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
1236
1237
block_time = ((retry_interval
1237
1238
- ((intmax_t)waited_time.tv_sec * 1000))
1238
1239
- ((intmax_t)waited_time.tv_nsec / 1000000));
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1242
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1244
1245
if(block_time <= 0){
1245
1246
ret = start_mandos_communication(mc.current_server->ip,
1246
1247
mc.current_server->port,
1412
1413
retry_interval = strtod(arg, &tmp);
1413
1414
if(errno != 0 or tmp == arg or *tmp != '\0'
1414
or (retry_interval * 1000) > INT_MAX){
1415
or (retry_interval * 1000) > INT_MAX
1416
or retry_interval < 0){
1415
1417
argp_error(state, "Bad retry interval");
1468
1470
perror_plus("seteuid");
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");
1473
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1474
int seckey_fd = open(seckey, O_RDONLY);
1475
if(seckey_fd == -1){
1476
perror_plus("open");
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");
1478
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1480
perror_plus("fstat");
1482
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1483
ret = fchown(seckey_fd, uid, gid);
1485
perror_plus("fchown");
1489
TEMP_FAILURE_RETRY(close(seckey_fd));
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
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");
1493
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1494
int pubkey_fd = open(pubkey, O_RDONLY);
1495
if(pubkey_fd == -1){
1496
perror_plus("open");
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");
1498
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1500
perror_plus("fstat");
1502
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1503
ret = fchown(pubkey_fd, uid, gid);
1505
perror_plus("fchown");
1509
TEMP_FAILURE_RETRY(close(pubkey_fd));
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1507
1513
/* Lower privileges */
1830
1836
port = (uint16_t)tmpmax;
1831
1837
*address = '\0';
1832
address = connect_to;
1833
1838
/* Colon in address indicates IPv6 */
1835
if(strchr(address, ':') != NULL){
1840
if(strchr(connect_to, ':') != NULL){
1842
/* Accept [] around IPv6 address - see RFC 5952 */
1843
if(connect_to[0] == '[' and address[-1] == ']')
1851
address = connect_to;
1845
1857
while(not quit_now){
1846
1858
ret = start_mandos_communication(address, port, if_index, af);
1847
1859
if(quit_now or ret == 0){
1850
sleep((int)retry_interval or 1);
1863
fprintf(stderr, "Retrying in %d seconds\n",
1864
(int)retry_interval);
1866
sleep((int)retry_interval);
1853
1869
if (not quit_now){
1854
1870
exitcode = EXIT_SUCCESS;
1992
2008
if(tempdir_created){
1993
2009
struct dirent **direntries = NULL;
1994
2010
struct dirent *direntry = NULL;
1995
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1997
for(int i = 0; i < ret; i++){
2011
int numentries = scandir(tempdir, &direntries, notdotentries,
2013
if (numentries > 0){
2014
for(int i = 0; i < numentries; i++){
1998
2015
direntry = direntries[i];
1999
2016
char *fullname = NULL;
2000
2017
ret = asprintf(&fullname, "%s/%s", tempdir,