root/dagent/dagent.c

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

DEFINITIONS

This source file includes following definitions.
  1. usage
  2. main

   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 
  33 #include <shared/macro.h>
  34 #include <shared/term.h>
  35 #include <shared/session.h>
  36 
  37 #include "checkfile.h"
  38 
  39 void usage(char *name)
  40     {
  41     fatal("%s -k <FILE> -dc\n"
  42           "arg k uses a keyfile\n"
  43           "arg d removes the checkfile (change password)\n"
  44           "arg c closes the current session\n"
  45           "(you can use -dc to do both at once)\n"
  46           "no arguments does default agent behaviour\n", name);
  47     }
  48 
  49 int main(int argc, char **argv)
  50     {
  51     FILE *keyfp;
  52     char *keyfile = NULL;
  53     int destroy = 0, close = 0, ret = -128;
  54 
  55     ARG_LOOP
  56         {
  57         ARG_TYPC;
  58         case 'k': ARG_STR(keyfile   ); break;
  59         case 'd': ARG_FLG(destroy, 1); break;
  60         case 'c': ARG_FLG(close  , 1); break;
  61         }
  62     ARG_END;
  63 
  64     if (destroy)
  65         {
  66         printf("attempting to destroy checkfile\n");
  67         ret = checkfile_destroy();
  68         }
  69     if (close)
  70         {
  71         session_destroy();
  72         printf("session closed.\n");
  73         ret = 0;
  74         }
  75     if (destroy || close)
  76         return ret;
  77 
  78     if (session_open() == 0)
  79         {
  80         session_close();
  81         fatal("session already open.  use -c to close it\n");
  82         }
  83 
  84     if (keyfile)
  85         {
  86         if (!(keyfp = fopen(keyfile, "r")))
  87             fatal("unable to access keyfile\n");
  88         if (!fread(password, 1, MAXPASS, keyfp))
  89             fatal("keyfile seems to be empty\n");
  90         fclose(keyfp);
  91         }
  92 
  93     if (checkfile_prep(0) < 0)
  94         {
  95         if (!keyfile) dgetpass(1);
  96         checkfile_prep(1);
  97         checkfile_commit();
  98         }
  99     else
 100         {
 101         do
 102             {
 103             if (ret != -128)
 104                 {
 105                 if (ret == -2)
 106                     fatal("agent checkfile is invalid, run with -d\n");
 107                 else
 108                     {
 109                     fprintf(stderr, "password incorrect, try again\n");
 110                     if (keyfile) return 1;
 111                     }
 112                 }
 113             if (!keyfile) dgetpass(0);
 114             } while ((ret = checkfile_check()) < 0);
 115         }
 116     session_create((unsigned char *)password, MAXPASS);
 117     printf("new session created successfully.\n");
 118 
 119     return 0;
 120     }

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