33
32
#define _LARGEFILE_SOURCE
34
33
#define _FILE_OFFSET_BITS 64
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
37
#include <stdio.h> /* fprintf(), stderr, fwrite(), stdout,
41
39
#include <stdint.h> /* uint16_t, uint32_t */
42
40
#include <stddef.h> /* NULL, size_t, ssize_t */
43
41
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
45
#include <stdbool.h> /* bool, false, true */
43
#include <stdbool.h> /* bool, true */
46
44
#include <string.h> /* memset(), strcmp(), strlen(),
47
strerror(), asprintf(), strcpy() */
48
#include <sys/ioctl.h> /* ioctl */
45
strerror(), memcpy(), strcpy() */
46
#include <sys/ioctl.h> /* ioctl */
47
#include <net/if.h> /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
49
49
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
50
50
sockaddr_in6, PF_INET6,
51
SOCK_STREAM, uid_t, gid_t, open(),
53
#include <sys/stat.h> /* open() */
51
SOCK_STREAM, INET6_ADDRSTRLEN,
53
#include <inttypes.h> /* PRIu16 */
54
54
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
55
inet_pton(), connect() */
56
#include <fcntl.h> /* open() */
57
#include <dirent.h> /* opendir(), struct dirent, readdir()
59
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
55
struct in6_addr, inet_pton(),
60
57
#include <assert.h> /* assert() */
61
58
#include <errno.h> /* perror(), errno */
62
#include <time.h> /* nanosleep(), time() */
59
#include <time.h> /* time() */
63
60
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
64
61
SIOCSIFFLAGS, if_indextoname(),
65
62
if_nametoindex(), IF_NAMESIZE */
66
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
67
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
69
63
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
70
64
getuid(), getgid(), setuid(),
66
#include <netinet/in.h>
72
67
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, or, and */
68
#include <iso646.h> /* not, and */
74
69
#include <argp.h> /* struct argp_option, error_t, struct
75
70
argp_state, struct argp,
76
71
argp_parse(), ARGP_KEY_ARG,
77
72
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
79
#include <sys/klog.h> /* klogctl() */
83
75
/* All Avahi types, constants and functions
135
* Decrypt OpenPGP data using keyrings in HOMEDIR.
136
* Returns -1 on error
152
static bool init_gpgme(mandos_context *mc, const char *seckey,
153
const char *pubkey, const char *tempdir){
138
static ssize_t pgp_packet_decrypt (const char *cryptotext,
141
const char *homedir){
142
gpgme_data_t dh_crypto, dh_plain;
155
144
gpgme_error_t rc;
146
size_t plaintext_capacity = 0;
147
ssize_t plaintext_length = 0;
156
148
gpgme_engine_info_t engine_info;
160
* Helper function to insert pub and seckey to the engine keyring.
162
bool import_key(const char *filename){
164
gpgme_data_t pgp_data;
166
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
172
rc = gpgme_data_new_from_fd(&pgp_data, fd);
173
if(rc != GPG_ERR_NO_ERROR){
174
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
175
gpgme_strsource(rc), gpgme_strerror(rc));
179
rc = gpgme_op_import(mc->ctx, pgp_data);
180
if(rc != GPG_ERR_NO_ERROR){
181
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
182
gpgme_strsource(rc), gpgme_strerror(rc));
186
ret = (int)TEMP_FAILURE_RETRY(close(fd));
190
gpgme_data_release(pgp_data);
195
fprintf(stderr, "Initialize gpgme\n");
151
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
199
155
gpgme_check_version(NULL);
200
156
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
201
if(rc != GPG_ERR_NO_ERROR){
157
if (rc != GPG_ERR_NO_ERROR){
202
158
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
203
159
gpgme_strsource(rc), gpgme_strerror(rc));
207
/* Set GPGME home directory for the OpenPGP engine only */
208
rc = gpgme_get_engine_info(&engine_info);
209
if(rc != GPG_ERR_NO_ERROR){
163
/* Set GPGME home directory for the OpenPGP engine only */
164
rc = gpgme_get_engine_info (&engine_info);
165
if (rc != GPG_ERR_NO_ERROR){
210
166
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
211
167
gpgme_strsource(rc), gpgme_strerror(rc));
214
170
while(engine_info != NULL){
215
171
if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
216
172
gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
217
engine_info->file_name, tempdir);
173
engine_info->file_name, homedir);
220
176
engine_info = engine_info->next;
222
178
if(engine_info == NULL){
223
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
227
/* Create new GPGME "context" */
228
rc = gpgme_new(&(mc->ctx));
229
if(rc != GPG_ERR_NO_ERROR){
230
fprintf(stderr, "bad gpgme_new: %s: %s\n",
231
gpgme_strsource(rc), gpgme_strerror(rc));
235
if(not import_key(pubkey) or not import_key(seckey)){
243
* Decrypt OpenPGP data.
244
* Returns -1 on error
246
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
247
const char *cryptotext,
250
gpgme_data_t dh_crypto, dh_plain;
253
size_t plaintext_capacity = 0;
254
ssize_t plaintext_length = 0;
257
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
179
fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
260
183
/* Create new GPGME data buffer from memory cryptotext */
261
184
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
263
if(rc != GPG_ERR_NO_ERROR){
186
if (rc != GPG_ERR_NO_ERROR){
264
187
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
265
188
gpgme_strsource(rc), gpgme_strerror(rc));
745
/* Combines file name and path and returns the malloced new
746
string. some sane checks could/should be added */
747
static const char *combinepath(const char *first, const char *second){
748
size_t f_len = strlen(first);
749
size_t s_len = strlen(second);
750
char *tmp = malloc(f_len + s_len + 2);
755
memcpy(tmp, first, f_len); /* Spurious warning */
759
memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
761
tmp[f_len + 1 + s_len] = '\0';
873
766
int main(int argc, char *argv[]){
874
AvahiSServiceBrowser *sb = NULL;
879
int exitcode = EXIT_SUCCESS;
880
const char *interface = "eth0";
881
struct ifreq network;
885
char *connect_to = NULL;
886
char tempdir[] = "/tmp/mandosXXXXXX";
887
bool tempdir_created = false;
888
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
889
const char *seckey = PATHDIR "/" SECKEY;
890
const char *pubkey = PATHDIR "/" PUBKEY;
892
mandos_context mc = { .simple_poll = NULL, .server = NULL,
893
.dh_bits = 1024, .priority = "SECURE256"
894
":!CTYPE-X.509:+CTYPE-OPENPGP" };
895
bool gnutls_initialized = false;
896
bool gpgme_initialized = false;
900
struct argp_option options[] = {
901
{ .name = "debug", .key = 128,
902
.doc = "Debug mode", .group = 3 },
903
{ .name = "connect", .key = 'c',
904
.arg = "ADDRESS:PORT",
905
.doc = "Connect directly to a specific Mandos server",
907
{ .name = "interface", .key = 'i',
909
.doc = "Network interface that will be used to search for"
912
{ .name = "seckey", .key = 's',
914
.doc = "OpenPGP secret key file base name",
916
{ .name = "pubkey", .key = 'p',
918
.doc = "OpenPGP public key file base name",
920
{ .name = "dh-bits", .key = 129,
922
.doc = "Bit length of the prime number used in the"
923
" Diffie-Hellman key exchange",
925
{ .name = "priority", .key = 130,
927
.doc = "GnuTLS priority string for the TLS handshake",
929
{ .name = "delay", .key = 131,
931
.doc = "Maximum delay to wait for interface startup",
936
error_t parse_opt(int key, char *arg,
937
struct argp_state *state){
939
case 128: /* --debug */
942
case 'c': /* --connect */
945
case 'i': /* --interface */
948
case 's': /* --seckey */
951
case 'p': /* --pubkey */
954
case 129: /* --dh-bits */
955
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
956
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
957
or arg[numchars] != '\0'){
958
fprintf(stderr, "Bad number of DH bits\n");
961
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
963
case 130: /* --priority */
966
case 131: /* --delay */
967
ret = sscanf(arg, "%lf%n", &delay, &numchars);
968
if(ret < 1 or arg[numchars] != '\0'){
969
fprintf(stderr, "Bad delay\n");
978
return ARGP_ERR_UNKNOWN;
983
struct argp argp = { .options = options, .parser = parse_opt,
985
.doc = "Mandos client -- Get and decrypt"
986
" passwords from a Mandos server" };
987
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
988
if(ret == ARGP_ERR_UNKNOWN){
989
fprintf(stderr, "Unknown error while parsing arguments\n");
990
exitcode = EXIT_FAILURE;
995
/* If the interface is down, bring it up */
996
if(interface[0] != '\0'){
998
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
999
messages to mess up the prompt */
1000
ret = klogctl(8, NULL, 5);
1001
bool restore_loglevel = true;
1003
restore_loglevel = false;
1008
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1011
exitcode = EXIT_FAILURE;
1013
if(restore_loglevel){
1014
ret = klogctl(7, NULL, 0);
1022
strcpy(network.ifr_name, interface);
1023
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1025
perror("ioctl SIOCGIFFLAGS");
1027
if(restore_loglevel){
1028
ret = klogctl(7, NULL, 0);
1034
exitcode = EXIT_FAILURE;
1037
if((network.ifr_flags & IFF_UP) == 0){
1038
network.ifr_flags |= IFF_UP;
1039
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1041
perror("ioctl SIOCSIFFLAGS");
767
AvahiSServiceBrowser *sb = NULL;
770
int exitcode = EXIT_SUCCESS;
771
const char *interface = "eth0";
772
struct ifreq network;
776
char *connect_to = NULL;
777
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
778
const char *pubkeyfile = "pubkey.txt";
779
const char *seckeyfile = "seckey.txt";
780
mandos_context mc = { .simple_poll = NULL, .server = NULL,
781
.dh_bits = 1024, .priority = "SECURE256"};
782
bool gnutls_initalized = false;
785
struct argp_option options[] = {
786
{ .name = "debug", .key = 128,
787
.doc = "Debug mode", .group = 3 },
788
{ .name = "connect", .key = 'c',
790
.doc = "Connect directly to a sepcified mandos server",
792
{ .name = "interface", .key = 'i',
794
.doc = "Interface that Avahi will conntect through",
796
{ .name = "keydir", .key = 'd',
798
.doc = "Directory where the openpgp keyring is",
800
{ .name = "seckey", .key = 's',
802
.doc = "Secret openpgp key for gnutls authentication",
804
{ .name = "pubkey", .key = 'p',
806
.doc = "Public openpgp key for gnutls authentication",
808
{ .name = "dh-bits", .key = 129,
810
.doc = "dh-bits to use in gnutls communication",
812
{ .name = "priority", .key = 130,
814
.doc = "GNUTLS priority", .group = 1 },
819
error_t parse_opt (int key, char *arg,
820
struct argp_state *state) {
821
/* Get the INPUT argument from `argp_parse', which we know is
822
a pointer to our plugin list pointer. */
844
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
859
return ARGP_ERR_UNKNOWN;
864
struct argp argp = { .options = options, .parser = parse_opt,
866
.doc = "Mandos client -- Get and decrypt"
867
" passwords from mandos server" };
868
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
869
if (ret == ARGP_ERR_UNKNOWN){
870
fprintf(stderr, "Unkown error while parsing arguments\n");
1042
871
exitcode = EXIT_FAILURE;
1044
if(restore_loglevel){
1045
ret = klogctl(7, NULL, 0);
1054
/* sleep checking until interface is running */
1055
for(int i=0; i < delay * 4; i++){
1056
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1058
perror("ioctl SIOCGIFFLAGS");
1059
} else if(network.ifr_flags & IFF_RUNNING){
1062
struct timespec sleeptime = { .tv_nsec = 250000000 };
1063
ret = nanosleep(&sleeptime, NULL);
1064
if(ret == -1 and errno != EINTR){
1065
perror("nanosleep");
1068
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1073
if(restore_loglevel){
1074
/* Restores kernel loglevel to default */
1075
ret = klogctl(7, NULL, 0);
1097
ret = init_gnutls_global(&mc, pubkey, seckey);
1099
fprintf(stderr, "init_gnutls_global failed\n");
1100
exitcode = EXIT_FAILURE;
1103
gnutls_initialized = true;
1106
if(mkdtemp(tempdir) == NULL){
1110
tempdir_created = true;
1112
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1113
fprintf(stderr, "init_gpgme failed\n");
1114
exitcode = EXIT_FAILURE;
1117
gpgme_initialized = true;
1120
if(interface[0] != '\0'){
876
pubkeyfile = combinepath(keydir, pubkeyfile);
877
if (pubkeyfile == NULL){
878
perror("combinepath");
879
exitcode = EXIT_FAILURE;
883
seckeyfile = combinepath(keydir, seckeyfile);
884
if (seckeyfile == NULL){
885
perror("combinepath");
889
ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
891
fprintf(stderr, "init_gnutls_global\n");
894
gnutls_initalized = true;
1121
910
if_index = (AvahiIfIndex) if_nametoindex(interface);
1122
911
if(if_index == 0){
1123
912
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1124
exitcode = EXIT_FAILURE;
1129
if(connect_to != NULL){
1130
/* Connect directly, do not use Zeroconf */
1131
/* (Mainly meant for debugging) */
1132
char *address = strrchr(connect_to, ':');
1133
if(address == NULL){
1134
fprintf(stderr, "No colon in address\n");
1135
exitcode = EXIT_FAILURE;
1139
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1140
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1141
or address[numchars+1] != '\0'){
1142
fprintf(stderr, "Bad port number\n");
1143
exitcode = EXIT_FAILURE;
1146
port = (uint16_t)tmpmax;
1148
address = connect_to;
1149
/* Colon in address indicates IPv6 */
1151
if(strchr(address, ':') != NULL){
1156
ret = start_mandos_communication(address, port, if_index, &mc,
1159
exitcode = EXIT_FAILURE;
1161
exitcode = EXIT_SUCCESS;
1167
avahi_set_log_function(empty_log);
1170
/* Initialize the pseudo-RNG for Avahi */
1171
srand((unsigned int) time(NULL));
1173
/* Allocate main Avahi loop object */
1174
mc.simple_poll = avahi_simple_poll_new();
1175
if(mc.simple_poll == NULL){
1176
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1177
exitcode = EXIT_FAILURE;
1182
AvahiServerConfig config;
1183
/* Do not publish any local Zeroconf records */
1184
avahi_server_config_init(&config);
1185
config.publish_hinfo = 0;
1186
config.publish_addresses = 0;
1187
config.publish_workstation = 0;
1188
config.publish_domain = 0;
1190
/* Allocate a new server */
1191
mc.server = avahi_server_new(avahi_simple_poll_get
1192
(mc.simple_poll), &config, NULL,
1195
/* Free the Avahi configuration data */
1196
avahi_server_config_free(&config);
1199
/* Check if creating the Avahi server object succeeded */
1200
if(mc.server == NULL){
1201
fprintf(stderr, "Failed to create Avahi server: %s\n",
1202
avahi_strerror(error));
1203
exitcode = EXIT_FAILURE;
1207
/* Create the Avahi service browser */
1208
sb = avahi_s_service_browser_new(mc.server, if_index,
1209
AVAHI_PROTO_INET6, "_mandos._tcp",
1210
NULL, 0, browse_callback, &mc);
1212
fprintf(stderr, "Failed to create service browser: %s\n",
1213
avahi_strerror(avahi_server_errno(mc.server)));
1214
exitcode = EXIT_FAILURE;
1218
/* Run the main loop */
1221
fprintf(stderr, "Starting Avahi loop search\n");
1224
avahi_simple_poll_loop(mc.simple_poll);
916
if(connect_to != NULL){
917
/* Connect directly, do not use Zeroconf */
918
/* (Mainly meant for debugging) */
919
char *address = strrchr(connect_to, ':');
921
fprintf(stderr, "No colon in address\n");
922
exitcode = EXIT_FAILURE;
926
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
928
perror("Bad port number");
929
exitcode = EXIT_FAILURE;
933
address = connect_to;
934
ret = start_mandos_communication(address, port, if_index, &mc);
936
exitcode = EXIT_FAILURE;
938
exitcode = EXIT_SUCCESS;
943
/* If the interface is down, bring it up */
945
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
948
exitcode = EXIT_FAILURE;
951
strcpy(network.ifr_name, interface); /* Spurious warning */
952
ret = ioctl(sd, SIOCGIFFLAGS, &network);
954
perror("ioctl SIOCGIFFLAGS");
955
exitcode = EXIT_FAILURE;
958
if((network.ifr_flags & IFF_UP) == 0){
959
network.ifr_flags |= IFF_UP;
960
ret = ioctl(sd, SIOCSIFFLAGS, &network);
962
perror("ioctl SIOCSIFFLAGS");
963
exitcode = EXIT_FAILURE;
971
avahi_set_log_function(empty_log);
974
/* Initialize the pseudo-RNG for Avahi */
975
srand((unsigned int) time(NULL));
977
/* Allocate main Avahi loop object */
978
mc.simple_poll = avahi_simple_poll_new();
979
if (mc.simple_poll == NULL) {
980
fprintf(stderr, "Avahi: Failed to create simple poll"
982
exitcode = EXIT_FAILURE;
987
AvahiServerConfig config;
988
/* Do not publish any local Zeroconf records */
989
avahi_server_config_init(&config);
990
config.publish_hinfo = 0;
991
config.publish_addresses = 0;
992
config.publish_workstation = 0;
993
config.publish_domain = 0;
995
/* Allocate a new server */
996
mc.server = avahi_server_new(avahi_simple_poll_get
997
(mc.simple_poll), &config, NULL,
1000
/* Free the Avahi configuration data */
1001
avahi_server_config_free(&config);
1004
/* Check if creating the Avahi server object succeeded */
1005
if (mc.server == NULL) {
1006
fprintf(stderr, "Failed to create Avahi server: %s\n",
1007
avahi_strerror(error));
1008
exitcode = EXIT_FAILURE;
1012
/* Create the Avahi service browser */
1013
sb = avahi_s_service_browser_new(mc.server, if_index,
1015
"_mandos._tcp", NULL, 0,
1016
browse_callback, &mc);
1018
fprintf(stderr, "Failed to create service browser: %s\n",
1019
avahi_strerror(avahi_server_errno(mc.server)));
1020
exitcode = EXIT_FAILURE;
1024
/* Run the main loop */
1027
fprintf(stderr, "Starting Avahi loop search\n");
1030
avahi_simple_poll_loop(mc.simple_poll);
1229
fprintf(stderr, "%s exiting\n", argv[0]);
1232
/* Cleanup things */
1234
avahi_s_service_browser_free(sb);
1236
if(mc.server != NULL)
1237
avahi_server_free(mc.server);
1239
if(mc.simple_poll != NULL)
1240
avahi_simple_poll_free(mc.simple_poll);
1242
if(gnutls_initialized){
1243
gnutls_certificate_free_credentials(mc.cred);
1244
gnutls_global_deinit();
1245
gnutls_dh_params_deinit(mc.dh_params);
1248
if(gpgme_initialized){
1249
gpgme_release(mc.ctx);
1252
/* Removes the temp directory used by GPGME */
1253
if(tempdir_created){
1255
struct dirent *direntry;
1256
d = opendir(tempdir);
1258
if(errno != ENOENT){
1263
direntry = readdir(d);
1264
if(direntry == NULL){
1267
/* Skip "." and ".." */
1268
if(direntry->d_name[0] == '.'
1269
and (direntry->d_name[1] == '\0'
1270
or (direntry->d_name[1] == '.'
1271
and direntry->d_name[2] == '\0'))){
1274
char *fullname = NULL;
1275
ret = asprintf(&fullname, "%s/%s", tempdir,
1281
ret = remove(fullname);
1283
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1290
ret = rmdir(tempdir);
1291
if(ret == -1 and errno != ENOENT){
1035
fprintf(stderr, "%s exiting\n", argv[0]);
1038
/* Cleanup things */
1040
avahi_s_service_browser_free(sb);
1042
if (mc.server != NULL)
1043
avahi_server_free(mc.server);
1045
if (mc.simple_poll != NULL)
1046
avahi_simple_poll_free(mc.simple_poll);
1050
if (gnutls_initalized){
1051
gnutls_certificate_free_credentials(mc.cred);
1052
gnutls_global_deinit ();