root/dagent/checkfile.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. checkfile_name
  2. checkfile_prep
  3. checkfile_commit
  4. checkfile_check
  5. checkfile_destroy

   1 /*
   2 ** Copyright (c) 2024 Amelia Campbell
   3 **
   4 ** Redistribution and use in source and binary forms, with or without
   5 ** modification, are permitted provided that the following conditions are
   6 ** met:
   7 **
   8 **  1. Redistributions of source code must retain the above copyright
   9 **     notice, this list of conditions and the following disclaimer.
  10 **  2. Redistributions in binary form must reproduce the above copyright
  11 **     notice, this list of conditions and the following disclaimer in the
  12 **     documentation and/or other materials provided with the
  13 **     distribution.
  14 **  3. Neither the name of the copyright holder nor the names of its
  15 **     contributors may be used to endorse or promote products derived
  16 **     from this software without specific prior written permission.
  17 **
  18 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19 ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  21 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29 */
  30 
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 
  35 #include <shared/macro.h>
  36 #include <shared/hash.h>
  37 #include <shared/term.h>
  38 
  39 #include "checkfile.h"
  40 
  41 #define MAXCF 128
  42 static char checkfile[MAXCF];
  43 FILE *checkfp = NULL;
  44 
  45 
  46 static void checkfile_name(void)
  47     {
  48     char *home;
  49 
  50     if (!(home = getenv("HOME")))
  51         fatal("failed to establish home directory from HOME env\n");
  52     snprintf(checkfile, MAXCF, "%s/.daverify", home);
  53     }
  54 
  55 
  56 int checkfile_prep(int force)
  57     {
  58     if (checkfp) return 0;
  59     checkfile_name();
  60 
  61     if (!force && !(checkfp = fopen(checkfile, "r+")))
  62         return -1;
  63     if (force && !(checkfp = fopen(checkfile, "w+")))
  64         fatal("failed to create agent checkfile at %s\n", checkfile);
  65 
  66     return 0;
  67     }
  68 
  69 
  70 void checkfile_commit(void)
  71     {
  72     hash_t chk;
  73     salt_t slt;
  74     genchk((unsigned char *)password, MAXPASS, &slt, &chk, 1);
  75     fseek(checkfp, 0, SEEK_SET);
  76     fwrite(slt.r, SALT_SZ , 1, checkfp);
  77     fwrite(chk.r, HASH_TSZ, 1, checkfp);
  78     fseek(checkfp, 0, SEEK_SET);
  79     }
  80 
  81 
  82 int checkfile_check(void)
  83     {
  84     hash_t ch1, ch2;
  85     salt_t slt;
  86     fseek(checkfp, 0, SEEK_SET);
  87     if (fread(slt.r, SALT_SZ, 1, checkfp) != 1)
  88         return -2;
  89     genchk((unsigned char *)password, MAXPASS, &slt, &ch1, 0);
  90     if (fread(ch2.r, HASH_TSZ, 1, checkfp) != 1)
  91         return -2;
  92     if (hashcmp(&ch1, &ch2))
  93         return 0;
  94     return -1;
  95     }
  96 
  97 
  98 int checkfile_destroy(void)
  99     {
 100     checkfile_name();
 101     return remove(checkfile);
 102     }

/* [previous][next][first][last][top][bottom][index][help] */