miniroon

Simplistic macaroon-based authorization for Unix systems
git clone https://ccx.te2000.cz/git/miniroon
Log | Files | Refs

secret.c (841B)


      1 #include <assert.h>
      2 #include <unistd.h>
      3 #include <errno.h>
      4 
      5 #include <skalibs/djbunix.h>
      6 #include <skalibs/strerr.h>
      7 
      8 #include "secret.h"
      9 #include "common.h"
     10 
     11 void read_secret(const bytebuffer secret){
     12   assert(secret.len == MINIROON_HMAC_SIZE);
     13   size_t bytes_read = 0;
     14   int secret_fd = openc_readb("secret");
     15   if (secret_fd < 0) {
     16     strerr_dief1sys(111, "open(secret)");
     17   }
     18   while(bytes_read < secret.len) {
     19     ssize_t r = read(secret_fd, &secret.data[bytes_read], secret.len - bytes_read);
     20     switch(r) {
     21       case 0:
     22         strerr_dief1x(111, "EOF before full secret was read");
     23         break;
     24       case -1:
     25         if(errno != EINTR) {
     26           strerr_dief1sys(111, "read() length");
     27         }
     28         break;
     29     }
     30     bytes_read += r;
     31   }
     32   if(close(secret_fd) != 0) {
     33     strerr_dief1sys(111, "close(secret_fd)");
     34   }
     35 }
     36 
     37