[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.6

1.6     ! aaron       1: /*     $OpenBSD: tcfsgenkey.c,v 1.5 2000/06/19 20:35:48 fgsch Exp $    */
1.5       fgsch       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
1.6     ! aaron      29: genkey_main(int argn, char *argv[])
1.1       provos     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]);
1.6     ! aaron      44:                        exit(OK);
1.1       provos     45:                        break; /* Useless code */
                     46:                default:
1.6     ! aaron      47:                        fprintf(stderr, "Try %s --help for more information.\n", argv[0]);
        !            48:                        exit(ER_UNKOPT);
1.1       provos     49:                        break;
                     50:                }
                     51:
                     52:        if (argn - optind)
1.6     ! aaron      53:                tcfs_error(ER_UNKOPT, NULL);
1.1       provos     54:
                     55:        /*
                     56:         * Must be root to do all this stuff
                     57:         */
                     58:        if (geteuid())
1.6     ! aaron      59:                tcfs_error(ER_CUSTOM, "I don't have root privileges!");
1.1       provos     60:
                     61:        /*
                     62:         * Authenticate user
                     63:         */
1.6     ! aaron      64:        if (!unix_auth(&user, &passwd, TRUE))
        !            65:                tcfs_error(ER_CUSTOM, "Who are you?!");
1.1       provos     66:
1.6     ! aaron      67:        if (!tcfs_getpwnam(user, &user_info))
        !            68:                tcfs_error(ER_CUSTOM, "You do not have an entry in the TCFS key database.");
1.1       provos     69:
                     70:        if (strlen(user_info->upw))
1.6     ! aaron      71:                tcfs_error(ER_CUSTOM, "You already have a TCFS key.");
1.1       provos     72:
                     73:        /*
                     74:         * Generate a new key for the user.
                     75:         */
1.6     ! aaron      76:        newkey = gentcfskey();
1.1       provos     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)
1.6     ! aaron      83:                tcfs_error(ER_MEM, NULL);
1.1       provos     84:
                     85:
1.6     ! aaron      86:        if (!tcfs_encrypt_key(passwd, newkey, KEYSIZE, cryptedkey, UUKEYSIZE + 1))
        !            87:                tcfs_error(ER_MEM, NULL);
1.1       provos     88:
                     89:        /*
                     90:         * Update TCFS key database
                     91:         */
1.6     ! aaron      92:        if (!tcfspwdbr_new(&userinfo))
        !            93:                tcfs_error(ER_MEM, NULL);
1.1       provos     94:
1.6     ! aaron      95:        if (!tcfspwdbr_edit(&userinfo, F_USR|F_PWD, user, cryptedkey))
        !            96:                tcfs_error(ER_MEM, NULL);
1.1       provos     97:
                     98:        /* TODO:
1.6     ! aaron      99:           if (!change && tcfs_getpwnam(user, &userinfo))
        !           100:           tcfs_error(ER_CUSTOM, "Use -c to change the key.");
1.1       provos    101:        */
                    102:
1.6     ! aaron     103:        if (!tcfs_putpwnam(user, userinfo, U_CHG))
        !           104:                tcfs_error(ER_CUSTOM, "Error: cannot generate key.");
1.1       provos    105:
1.6     ! aaron     106:        tcfs_error(ER_CUSTOM, "\nKey succesfully generated.");
1.1       provos    107: }