miniroon

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

header.c (1814B)


      1 #include <skalibs/strerr.h>
      2 
      3 #include "netstring.h"
      4 #include "header.h"
      5 
      6 miniroon_error set_header_version(miniroon_header *header, const bytebuffer source) {
      7   if(strbbcmp(source, "capv0") == 0) {
      8     header->version = V0;
      9   } else {
     10     return miniroon_err1(MINIROON_EHDR_INV_VERSION, "Unhandled miniroon version");
     11   }
     12 }
     13 
     14 miniroon_error set_header_action(miniroon_header *header, const bytebuffer source) {
     15   if(strbbcmp(source, "revoke") == 0) {
     16     header->action = REVOKE;
     17   } else if(strbbcmp(source, "invoke") == 0) {
     18     header->action = INVOKE;
     19   } else if(strbbcmp(source, "invoke-once") == 0) {
     20     header->action = INVOKE_ONCE;
     21   } else {
     22     return miniroon_err1(MINIROON_EHDR_INV_ACTION, "Unhandled miniroon action");
     23   }
     24 }
     25 
     26 miniroon_error parse_header(miniroon_header *header, const bytebuffer source) {
     27   dbg_print_bb1("Got header", source);
     28   netstring_chunk c;
     29   netstring_chunk_init(&c, source);
     30 
     31   if(!netstring_chunk_next(&c)) {
     32     return miniroon_err1(MINIROON_EHDR_NO_VERSION, "Mising version in miniroon header");
     33   }
     34   dbg_print_bb1("Header > Version", c.inner);
     35   if(set_header_version(header, c.inner) != MINIROON_OK) {
     36     return miniroon_current_error;
     37   }
     38 
     39   if(!netstring_chunk_next(&c)) {
     40     return miniroon_err1(MINIROON_EHDR_NO_ID, "Mising ID in miniroon header");
     41   }
     42   dbg_print_bb1("Header > ID", c.inner);
     43   header->id = c.inner;
     44 
     45   if(!netstring_chunk_next(&c)) {
     46     return miniroon_err1(MINIROON_EHDR_NO_ACTION, "Mising action in miniroon header");
     47   }
     48   dbg_print_bb1("Header > Action", c.inner);
     49   if(set_header_action(header, c.inner) != MINIROON_OK) {
     50     return miniroon_current_error;
     51   }
     52 
     53   if(netstring_chunk_next(&c)) {
     54     return miniroon_err1(MINIROON_EHDR_EXTRA, "Extraneous data in miniroon header");
     55   }
     56 
     57   return MINIROON_OK;
     58 }
     59 
     60 /*  vim: sts=2 sw=2 et
     61 */