tStore/Restore secret list from .meta file - safe - password protected secret keeper
 (HTM) git clone git://git.z3bra.org/safe.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit c985b9ecaa5571f5e89066de0fc6a386a3a7e556
 (DIR) parent 0e3bc29152db3cd63639a159ac9b036191d15f26
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Wed, 22 May 2019 09:36:58 +0200
       
       Store/Restore secret list from .meta file
       
       Diffstat:
         M safe.c                              |      61 +++++++++++++++++++++++++------
       
       1 file changed, 50 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/safe.c b/safe.c
       t@@ -13,6 +13,7 @@
        #include <sodium.h>
        
        #include "arg.h"
       +#include "queue.h"
        #include "readpassphrase.h"
        
        #define MDSIZE crypto_generichash_BYTES
       t@@ -20,12 +21,15 @@
        #define META ".meta"
        
        struct secret {
       -        char hex[256];
       +        char name[64];
       +        char hex[MDSIZE*2 + 1];
       +        SLIST_ENTRY(secret) entry;
        };
        
        struct safe {
                uint8_t salt[crypto_pwhash_SALTBYTES];
       -        struct secret *secrets;
       +        uint32_t nentry;
       +        SLIST_HEAD(secrets, secret) secrets;
        };
        
        char *argv0;
       t@@ -143,11 +147,15 @@ store_secret(struct safe *s, int fd, char *name)
                ssize_t n;
                uint8_t md[MDSIZE];
                char buf[64], fn[MDSIZE*2 + 1];
       +        struct secret *secret;
       +
       +        secret = malloc(sizeof(*secret));
       +        strcpy(secret->name, name);
        
                hash((uint8_t *)name, strlen(name), md, sizeof(md));
       -        bin2str(md, fn, MDSIZE);
       +        bin2str(md, secret->hex, MDSIZE);
        
       -        sfd = open(fn, O_WRONLY | O_CREAT, 0600);
       +        sfd = open(secret->hex, O_WRONLY | O_CREAT, 0600);
                if (sfd < 0)
                        err(1, "open %s", fn);
        
       t@@ -156,6 +164,10 @@ store_secret(struct safe *s, int fd, char *name)
                }
        
                close(sfd);
       +
       +        SLIST_INSERT_HEAD(&s->secrets, secret, entry);
       +        s->nentry++;
       +
                return 0;
        }
        
       t@@ -175,7 +187,6 @@ show_secret(struct safe *s, int fd, char *name)
                        err(1, "open %s", fn);
        
                while((n = xread(sfd, buf, sizeof(buf))) > 0) {
       -                /* xencrypt(buf, sizeof(buf), 1); */
                        xwrite(fd, buf, n);
                }
        
       t@@ -186,13 +197,32 @@ show_secret(struct safe *s, int fd, char *name)
        void
        init(struct safe *s)
        {
       +        int fd;
       +        uint32_t i;
                struct stat sb;
       +        struct secret *secret;
       +
                if (sodium_init() < 0)
                        err(1, "sodium: failed to initialize library");
        
       -        if (stat(META, &sb)) {
       +        SLIST_INIT(&s->secrets);
       +
       +        if (!stat(META, &sb)) {
       +                fd = open(META, O_RDONLY);
       +
       +                xread(fd, s->salt, sizeof(s->salt));
       +                xread(fd, &s->nentry, sizeof(s->nentry));
       +                for (i = 0; i < s->nentry; i++) {
       +                        secret = malloc(sizeof(*secret));
       +                        xread(fd, secret->name, sizeof(secret->name));
       +                        xread(fd, secret->hex, sizeof(secret->hex));
       +                        SLIST_INSERT_HEAD(&s->secrets, secret, entry);
       +                }
       +
       +                close(fd);
       +        } else {
       +                s->nentry = 0;
                        randombytes_buf(s->salt, sizeof(s->salt));
       -                s->secrets = NULL;
                }
        }
        
       t@@ -200,9 +230,17 @@ void
        deinit(struct safe *s)
        {
                int fd;
       +        struct secret *tmp;
       +
       +        fd = open(META, O_WRONLY | O_CREAT, 0600);
       +
       +        xwrite(fd, s->salt, sizeof(s->salt));
       +        xwrite(fd, &s->nentry, sizeof(s->nentry));
       +        SLIST_FOREACH(tmp, &s->secrets, entry) {
       +                xwrite(fd, tmp->name, sizeof(tmp->name));
       +                xwrite(fd, tmp->hex, sizeof(tmp->hex));
       +        }
        
       -        fd = open(META, O_RDWR | O_CREAT | O_EXCL, 0600);
       -        write(fd, s, sizeof(*s));
                fsync(fd);
                close(fd);
        }
       t@@ -243,9 +281,11 @@ main(int argc, char *argv[])
        
                if (aflag) {
                        store_secret(&s, STDIN_FILENO, secret);
       +                deinit(&s);
                } else {
                        show_secret(&s, STDOUT_FILENO, secret);
                }
        
                return 0;
       -}
       -\ No newline at end of file
       +}
       +