37
37
#include <stdlib.h>
39
39
#include <net/if.h> /* if_nametoindex */
40
#include <sys/ioctl.h> // ioctl, ifreq, SIOCGIFFLAGS, IFF_UP, SIOCSIFFLAGS
41
#include <net/if.h> // ioctl, ifreq, SIOCGIFFLAGS, IFF_UP, SIOCSIFFLAGS
40
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
42
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
43
45
#include <avahi-core/core.h>
44
46
#include <avahi-core/lookup.h>
85
87
const char *priority;
88
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
91
* Decrypt OpenPGP data using keyrings in HOMEDIR.
94
static ssize_t pgp_packet_decrypt (const char *cryptotext,
90
97
const char *homedir){
91
98
gpgme_data_t dh_crypto, dh_plain;
95
ssize_t new_packet_capacity = 0;
96
ssize_t new_packet_length = 0;
102
ssize_t plaintext_capacity = 0;
103
ssize_t plaintext_length = 0;
97
104
gpgme_engine_info_t engine_info;
100
fprintf(stderr, "Trying to decrypt OpenPGP packet\n");
107
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
112
/* Set GPGME home directory */
119
/* Set GPGME home directory for the OpenPGP engine only */
113
120
rc = gpgme_get_engine_info (&engine_info);
114
121
if (rc != GPG_ERR_NO_ERROR){
115
122
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
125
132
engine_info = engine_info->next;
127
134
if(engine_info == NULL){
128
fprintf(stderr, "Could not set home dir to %s\n", homedir);
135
fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
132
/* Create new GPGME data buffer from packet buffer */
133
rc = gpgme_data_new_from_mem(&dh_crypto, packet, packet_size, 0);
139
/* Create new GPGME data buffer from memory cryptotext */
140
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
134
142
if (rc != GPG_ERR_NO_ERROR){
135
143
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
136
144
gpgme_strsource(rc), gpgme_strerror(rc));
142
150
if (rc != GPG_ERR_NO_ERROR){
143
151
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
144
152
gpgme_strsource(rc), gpgme_strerror(rc));
153
gpgme_data_release(dh_crypto);
150
159
if (rc != GPG_ERR_NO_ERROR){
151
160
fprintf(stderr, "bad gpgme_new: %s: %s\n",
152
161
gpgme_strsource(rc), gpgme_strerror(rc));
162
plaintext_length = -1;
156
/* Decrypt data from the FILE pointer to the plaintext data
166
/* Decrypt data from the cryptotext data buffer to the plaintext
158
168
rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
159
169
if (rc != GPG_ERR_NO_ERROR){
160
170
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
161
171
gpgme_strsource(rc), gpgme_strerror(rc));
172
plaintext_length = -1;
166
fprintf(stderr, "Decryption of OpenPGP packet succeeded\n");
177
fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
170
181
gpgme_decrypt_result_t result;
171
182
result = gpgme_op_decrypt_result(ctx);
198
/* Delete the GPGME FILE pointer cryptotext data buffer */
199
gpgme_data_release(dh_crypto);
201
209
/* Seek back to the beginning of the GPGME plaintext data buffer */
202
210
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
203
211
perror("pgpme_data_seek");
212
plaintext_length = -1;
208
if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
209
*new_packet = realloc(*new_packet,
210
(unsigned int)new_packet_capacity
218
if (plaintext_length + BUFFER_SIZE > plaintext_capacity){
219
*plaintext = realloc(*plaintext,
220
(unsigned int)plaintext_capacity
212
if (*new_packet == NULL){
222
if (*plaintext == NULL){
213
223
perror("realloc");
224
plaintext_length = -1;
216
new_packet_capacity += BUFFER_SIZE;
227
plaintext_capacity += BUFFER_SIZE;
219
ret = gpgme_data_read(dh_plain, *new_packet + new_packet_length,
230
ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
221
232
/* Print the data, if any */
226
238
perror("gpgme_data_read");
239
plaintext_length = -1;
229
new_packet_length += ret;
242
plaintext_length += ret;
232
/* FIXME: check characters before printing to screen so to not print
233
terminal control characters */
235
/* fprintf(stderr, "decrypted password is: "); */
236
/* fwrite(*new_packet, 1, new_packet_length, stderr); */
237
/* fprintf(stderr, "\n"); */
246
fprintf(stderr, "Decrypted password is: ");
247
for(size_t i = 0; i < plaintext_length; i++){
248
fprintf(stderr, "%02hhX ", (*plaintext)[i]);
250
fprintf(stderr, "\n");
255
/* Delete the GPGME cryptotext data buffer */
256
gpgme_data_release(dh_crypto);
240
258
/* Delete the GPGME plaintext data buffer */
241
259
gpgme_data_release(dh_plain);
242
return new_packet_length;
260
return plaintext_length;
245
263
static const char * safer_gnutls_strerror (int value) {
537
static void resolve_callback( AvahiSServiceResolver *r,
538
AvahiIfIndex interface,
539
AVAHI_GCC_UNUSED AvahiProtocol protocol,
540
AvahiResolverEvent event,
544
const char *host_name,
545
const AvahiAddress *address,
547
AVAHI_GCC_UNUSED AvahiStringList *txt,
548
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
555
static void resolve_callback(AvahiSServiceResolver *r,
556
AvahiIfIndex interface,
557
AVAHI_GCC_UNUSED AvahiProtocol protocol,
558
AvahiResolverEvent event,
562
const char *host_name,
563
const AvahiAddress *address,
565
AVAHI_GCC_UNUSED AvahiStringList *txt,
566
AVAHI_GCC_UNUSED AvahiLookupResultFlags
550
569
mandos_context *mc = userdata;
551
570
assert(r); /* Spurious warning */
608
628
the callback function is called the server will free
609
629
the resolver for us. */
611
if (!(avahi_s_service_resolver_new(mc->server, interface, protocol, name,
631
if (!(avahi_s_service_resolver_new(mc->server, interface,
632
protocol, name, type, domain,
613
633
AVAHI_PROTO_INET6, 0,
614
634
resolve_callback, mc)))
615
635
fprintf(stderr, "Failed to resolve service '%s': %s\n", name,