|
|
|
@ -1144,6 +1144,73 @@ netpgp_export_key(netpgp_t *netpgp, char *name)
|
|
|
|
|
return pgp_export_key(io, key, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
netpgp_save_pubkeys(netpgp_t *netpgp)
|
|
|
|
|
{
|
|
|
|
|
pgp_io_t *io;
|
|
|
|
|
pgp_key_t *key;
|
|
|
|
|
unsigned n;
|
|
|
|
|
pgp_keyring_t *keyring;
|
|
|
|
|
pgp_output_t *create;
|
|
|
|
|
int fd;
|
|
|
|
|
int err = 0;
|
|
|
|
|
char swpfile[MAXPATHLEN];
|
|
|
|
|
char backup[MAXPATHLEN];
|
|
|
|
|
char *ringfile;
|
|
|
|
|
int cur;
|
|
|
|
|
char *name = "pubkey";
|
|
|
|
|
time_t curtime;
|
|
|
|
|
|
|
|
|
|
io = netpgp->io;
|
|
|
|
|
keyring = netpgp->pubring;
|
|
|
|
|
|
|
|
|
|
/* file names */
|
|
|
|
|
if ((ringfile = netpgp_getvar(netpgp, name)) == NULL) {
|
|
|
|
|
/* only save if already loaded by readkeyring(name) */
|
|
|
|
|
(void) fprintf(io->errs,
|
|
|
|
|
"netpgp_save_%s : No ring file defined\n", name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
curtime = time(NULL);
|
|
|
|
|
if (snprintf(swpfile, sizeof(swpfile),
|
|
|
|
|
"%s.swp", ringfile) >= sizeof(swpfile) ||
|
|
|
|
|
(cur = snprintf(backup, sizeof(backup),
|
|
|
|
|
"%s.backup_", ringfile)) >= sizeof(backup) ||
|
|
|
|
|
strftime(&backup[cur], sizeof(backup)-cur,
|
|
|
|
|
"%F.%T", localtime(&curtime)) >= sizeof(backup)-cur){
|
|
|
|
|
(void) fprintf(io->errs,
|
|
|
|
|
"netpgp_save_%s : file path too long\n", name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((fd = pgp_setup_file_write(&create, swpfile, 0)) < 0) {
|
|
|
|
|
(void) fprintf(io->errs,
|
|
|
|
|
"netpgp_save_%s : can't setup write for %s\n", name, swpfile);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (n = 0, key = keyring->keys; n < keyring->keyc; ++n, ++key) {
|
|
|
|
|
if (!pgp_write_xfer_pubkey(create, key, 0 /* noarmor */)) {
|
|
|
|
|
err = 1;
|
|
|
|
|
(void) fprintf(io->errs,
|
|
|
|
|
"netpgp_save_%s : couldn't save key #%d\n", name, n);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pgp_teardown_file_write(create, fd);
|
|
|
|
|
|
|
|
|
|
if(err){
|
|
|
|
|
unlink(swpfile);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rename(ringfile, backup);
|
|
|
|
|
rename(swpfile, ringfile);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define IMPORT_ARMOR_HEAD "-----BEGIN PGP PUBLIC KEY BLOCK-----"
|
|
|
|
|
|
|
|
|
|
/* import a key into our keyring */
|
|
|
|
|