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

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

1.1       provos      1: /*
                      2:  *     Transparent Cryptographic File System (TCFS) for NetBSD
                      3:  *     Author and mantainer:   Luigi Catuogno [luicat@tcfs.unisa.it]
                      4:  *
                      5:  *     references:             http://tcfs.dia.unisa.it
                      6:  *                             tcfs-bsd@tcfs.unisa.it
                      7:  */
                      8:
                      9: /*
                     10:  *     Base utility set v0.1
                     11:  */
                     12:
                     13: #include <sys/types.h>
                     14: #include <sys/param.h>
1.4       fgsch      15: #include <sys/mount.h>
1.1       provos     16: #include <ctype.h>
1.4       fgsch      17: #include <des.h>
1.1       provos     18: #include <pwd.h>
1.4       fgsch      19: #include <stdio.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
1.1       provos     22: #include <unistd.h>
                     23:
                     24: #include <miscfs/tcfs/tcfs.h>
                     25: #include "tcfslib.h"
                     26: #include "tcfserrors.h"
                     27: #include <grp.h>
                     28:
                     29: extern char *optarg;
                     30: extern int optind;
                     31: char *putkey_usage=
                     32: "usage: tcfsputkey [-k][-f fliesystem-label][-g group][-p mount-point]\n";
                     33:
                     34: int
                     35: putkey_main(int argc, char *argv[])
                     36: {
1.5     ! fgsch      37:        char *user,*password,*tcfskey;
1.1       provos     38:        uid_t uid;
                     39:        gid_t gid;
                     40:        int es, treshold;
                     41:        char x;
                     42:        tcfspwdb *info;
                     43:        tcfsgpwdb *ginfo;
                     44:        char fslabel[MAXPATHLEN], fspath[MAXPATHLEN];
                     45:        int def = TRUE, havempname = FALSE, havefsname = FALSE;
                     46:        int isgroupkey = FALSE;
                     47:        int havename = FALSE, havefspath = FALSE, havekey = FALSE;
                     48:
                     49:        while ((x = getopt(argc,argv,"kf:p:g:")) != EOF) {
                     50:                switch(x) {
                     51:                case 'k':
                     52:                        def = FALSE;
                     53:                        break;
                     54:                case 'p':
                     55:                        havempname = TRUE;
                     56:                        strlcpy(fspath, optarg, sizeof(fspath));
                     57:                        break;
                     58:                case 'f':
                     59:                        havefsname = TRUE;
                     60:                        strlcpy(fslabel, optarg, sizeof(fslabel));
                     61:                        break;
                     62:                case 'g':
                     63:                        isgroupkey = TRUE;
                     64:                        def = TRUE;
                     65:                        gid = atoi(optarg);
                     66:                        if (!gid && optarg[0] != 0) {
                     67:                                struct group *grp;
                     68:                                grp = (struct group *)getgrnam(optarg);
                     69:                                if (!grp)
                     70:                                        tcfs_error(ER_CUSTOM, "Nonexistant group\n");
                     71:                                gid = grp->gr_gid;
                     72:                        }
                     73:                        break;
                     74:                default:
                     75:                        tcfs_error(ER_CUSTOM, putkey_usage);
                     76:                        exit(ER_UNKOPT);
                     77:                }
                     78:        }
                     79:        if (argc - optind)
                     80:                tcfs_error(ER_UNKOPT,NULL);
                     81:
                     82:        if (havefsname && havempname) {
                     83:                tcfs_error(ER_CUSTOM, putkey_usage);
                     84:                exit(1);
                     85:        }
                     86:
                     87:        if (havefsname) {
                     88:                es=tcfs_getfspath(fslabel,fspath);
                     89:                havename = TRUE;
                     90:        }
                     91:
                     92:        if (havefspath)
                     93:                havename = TRUE;
                     94:
                     95:        if (!havename)
                     96:                es=tcfs_getfspath("default",fspath);
                     97:
                     98:        if (!es) {
                     99:                tcfs_error(ER_CUSTOM,"fs-label not found!\n");
                    100:                exit(1);
                    101:        }
                    102:
                    103:        uid = getuid();
                    104:
                    105:        if (isgroupkey) {
                    106:                if (!unix_auth(&user,&password,TRUE))
                    107:                        tcfs_error(ER_AUTH,user);
                    108:
                    109:                if (!tcfsgpwdbr_new(&ginfo))
                    110:                        tcfs_error(ER_MEM,NULL);
                    111:
                    112:                if (!tcfs_ggetpwnam(user,gid,&ginfo))
                    113:                        tcfs_error(ER_CUSTOM,"Default key non found");
                    114:
                    115:                if (!strlen(ginfo->gkey))
                    116:                        tcfs_error(ER_CUSTOM,"Invalid default key");
                    117:
                    118:                tcfskey = (char*)malloc(UUKEYSIZE);
                    119:                if (!tcfskey)
                    120:                        tcfs_error(ER_MEM,NULL);
                    121:
                    122:                treshold = ginfo->soglia;
                    123:
1.3       provos    124:                if (!tcfs_decrypt_key(password, ginfo->gkey, tcfskey, GKEYSIZE))
                    125:                        tcfs_error(ER_CUSTOM, "Could not decrypt group key");
1.1       provos    126:
                    127:                es = tcfs_group_enable(fspath,uid,gid,treshold,tcfskey);
                    128:
                    129:                if(es == -1) {
                    130:                        tcfs_error(ER_CUSTOM,"problems updating filesystem");
                    131:                }
                    132:
                    133:                exit(0);
                    134:        }
                    135:
                    136:
                    137:        if(!def) {
                    138:                tcfskey = getpass("Insert tcfs-key:");
                    139:                havekey = TRUE;
                    140:        } else {
                    141:                if(!unix_auth(&user,&password,TRUE))
                    142:                        tcfs_error(ER_AUTH,user);
                    143:
                    144:                if(!tcfspwdbr_new(&info))
                    145:                        tcfs_error(ER_MEM,NULL);
                    146:
                    147:                if(!tcfs_getpwnam(user,&info))
                    148:                        tcfs_error(ER_CUSTOM,"Default key non found");
                    149:
                    150:                if(!strlen(info->upw))
1.3       provos    151:                        tcfs_error(ER_CUSTOM, "Invalid default key");
1.1       provos    152:
                    153:                tcfskey = (char*)malloc(UUKEYSIZE);
                    154:                if(!tcfskey)
1.3       provos    155:                        tcfs_error(ER_MEM, NULL);
1.1       provos    156:
1.3       provos    157:                if (!tcfs_decrypt_key (password, info->upw, tcfskey, KEYSIZE))
                    158:                        tcfs_error(ER_CUSTOM, "Could not decrypt key");
1.1       provos    159:                havekey = TRUE;
                    160:        }
                    161:
                    162:        es = tcfs_user_enable(fspath, uid, tcfskey);
                    163:
                    164:        if(es == -1)
                    165:                tcfs_error(ER_CUSTOM,"problems updating filesystem");
                    166:
                    167:        exit(0);
                    168: }