[BACK]Return to tcfsgenkey.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tcfs

Annotation of src/usr.bin/tcfs/tcfsgenkey.c, Revision 1.5

1.5     ! fgsch       1: /*     $OpenBSD$       */
        !             2:
1.1       provos      3: /*
                      4:  *     Transparent Cryptographic File System (TCFS) for NetBSD
                      5:  *     Author and mantainer:   Luigi Catuogno [luicat@tcfs.unisa.it]
                      6:  *
                      7:  *     references:             http://tcfs.dia.unisa.it
                      8:  *                             tcfs-bsd@tcfs.unisa.it
                      9:  */
                     10:
                     11: /*
                     12:  *     Base utility set v0.1
                     13:  */
                     14:
                     15: #include <stdio.h>
1.4       fgsch      16: #include <stdlib.h>
1.1       provos     17: #include <strings.h>
                     18:
                     19: #include <miscfs/tcfs/tcfs.h>
                     20: #include "tcfslib.h"
                     21: #include "tcfserrors.h"
                     22:
                     23: char *genkey_usage="Usage: %s [OPTION]
                     24: Generate a TCFS key adding it to the user entry into the TCFS database.
                     25:
                     26:   -h       Shows this help\n";
                     27:
                     28: int
                     29: genkey_main (int argn, char *argv[])
                     30: {
                     31:        int val;
                     32:        char *user, *passwd;
                     33:        tcfspwdb *userinfo;
                     34:        unsigned char *newkey, *cryptedkey;
                     35:        tcfspwdb *user_info = NULL;
                     36:
                     37:        /*
                     38:         * Going to check arguments
                     39:         */
                     40:        while ((val = getopt(argn, argv, "h")) != EOF)
                     41:                switch (val) {
                     42:                case 'h':
                     43:                        show_usage(genkey_usage, argv[0]);
                     44:                        exit (OK);
                     45:                        break; /* Useless code */
                     46:                default:
                     47:                        fprintf (stderr, "Try %s --help for more information.\n", argv[0]);
                     48:                        exit (ER_UNKOPT);
                     49:                        break;
                     50:                }
                     51:
                     52:        if (argn - optind)
                     53:                tcfs_error (ER_UNKOPT, NULL);
                     54:
                     55:        /*
                     56:         * Must be root to do all this stuff
                     57:         */
                     58:        if (geteuid())
                     59:                tcfs_error (ER_CUSTOM, "I don't have root privileges!");
                     60:
                     61:        /*
                     62:         * Authenticate user
                     63:         */
                     64:        if (!unix_auth (&user, &passwd, TRUE))
                     65:                tcfs_error (ER_CUSTOM, "Who are you?!");
                     66:
                     67:        if (!tcfs_getpwnam (user, &user_info))
                     68:                tcfs_error (ER_CUSTOM, "You do not have an entry in the TCFS key database.");
                     69:
                     70:        if (strlen(user_info->upw))
                     71:                tcfs_error (ER_CUSTOM,"You already have a TCFS key.");
                     72:
                     73:        /*
                     74:         * Generate a new key for the user.
                     75:         */
                     76:        newkey = gentcfskey ();
                     77:
                     78:        /*
                     79:         * Encrypt the generated key with user password
                     80:         */
1.3       provos     81:        cryptedkey = (char*)calloc(UUKEYSIZE + 1, sizeof(char));
1.1       provos     82:        if (!cryptedkey)
                     83:                tcfs_error (ER_MEM, NULL);
                     84:
                     85:
1.3       provos     86:        if (!tcfs_encrypt_key (passwd, newkey, KEYSIZE, cryptedkey, UUKEYSIZE + 1))
1.1       provos     87:                tcfs_error (ER_MEM, NULL);
                     88:
                     89:        /*
                     90:         * Update TCFS key database
                     91:         */
                     92:        if (!tcfspwdbr_new (&userinfo))
                     93:                tcfs_error (ER_MEM, NULL);
                     94:
                     95:        if (!tcfspwdbr_edit (&userinfo, F_USR|F_PWD, user, cryptedkey))
                     96:                tcfs_error (ER_MEM, NULL);
                     97:
                     98:        /* TODO:
                     99:           if (!change && tcfs_getpwnam (user, &userinfo))
                    100:           tcfs_error (ER_CUSTOM, "Use -c to change the key.");
                    101:        */
                    102:
                    103:        if (!tcfs_putpwnam (user, userinfo, U_CHG))
                    104:                tcfs_error (ER_CUSTOM, "Error: cannot generate key.");
                    105:
                    106:        tcfs_error (ER_CUSTOM, "\nKey succesfully generated.");
                    107: }