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

1.10    ! aaron       1: /*     $OpenBSD: tcfsgenkey.c,v 1.9 2000/06/20 08:59:53 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:         */
1.10    ! aaron      40:        while ((val = getopt(argn, argv, "h")) != -1)
1.1       provos     41:                switch (val) {
                     42:                case 'h':
1.8       fgsch      43:                        printf(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))
1.7       aaron      68:                tcfs_error(ER_CUSTOM,
                     69:                    "You do not have an entry in the TCFS key database.");
1.1       provos     70:
                     71:        if (strlen(user_info->upw))
1.6       aaron      72:                tcfs_error(ER_CUSTOM, "You already have a TCFS key.");
1.1       provos     73:
                     74:        /*
                     75:         * Generate a new key for the user.
                     76:         */
1.6       aaron      77:        newkey = gentcfskey();
1.1       provos     78:
                     79:        /*
                     80:         * Encrypt the generated key with user password
                     81:         */
1.7       aaron      82:        cryptedkey = (char *)calloc(UUKEYSIZE + 1, sizeof(char));
1.1       provos     83:        if (!cryptedkey)
1.6       aaron      84:                tcfs_error(ER_MEM, NULL);
1.1       provos     85:
                     86:
1.7       aaron      87:        if (!tcfs_encrypt_key(passwd, newkey, KEYSIZE, cryptedkey,
                     88:            UUKEYSIZE + 1))
1.6       aaron      89:                tcfs_error(ER_MEM, NULL);
1.1       provos     90:
                     91:        /*
                     92:         * Update TCFS key database
                     93:         */
1.6       aaron      94:        if (!tcfspwdbr_new(&userinfo))
                     95:                tcfs_error(ER_MEM, NULL);
1.1       provos     96:
1.6       aaron      97:        if (!tcfspwdbr_edit(&userinfo, F_USR|F_PWD, user, cryptedkey))
                     98:                tcfs_error(ER_MEM, NULL);
1.1       provos     99:
                    100:        /* TODO:
1.6       aaron     101:           if (!change && tcfs_getpwnam(user, &userinfo))
                    102:           tcfs_error(ER_CUSTOM, "Use -c to change the key.");
1.1       provos    103:        */
                    104:
1.6       aaron     105:        if (!tcfs_putpwnam(user, userinfo, U_CHG))
                    106:                tcfs_error(ER_CUSTOM, "Error: cannot generate key.");
1.1       provos    107:
1.6       aaron     108:        tcfs_error(ER_CUSTOM, "\nKey succesfully generated.");
1.9       fgsch     109:
                    110:        exit(0);
1.1       provos    111: }