-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmkdir.c
155 lines (152 loc) · 3.03 KB
/
cmkdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <rpc/rpc.h>
#include <sys/time.h>
#include "nfsproto.h"
#include "admproto.h"
#include "cfs.h"
#include "shs.h"
main(argc,argv)
int argc;
char **argv;
{
char *pw;
char pword[256];
char *getpassword();
int n;
cfs_admkey k;
cfskey kt;
char path[1024];
char str[8];
FILE *fp;
char *flg;
struct timeval tv;
u_long r;
int i;
int ciph=CFS_SAFER_SK128;
int cfmt=1;
int smsize=LARGESMSIZE;
unsigned char ekey[128];
unsigned char ek1[128];
unsigned int l;
int keycheck=1;
while (--argc && (**++argv == '-')) {
for (flg= ++*argv; *flg; ++flg)
switch (*flg) {
case 'o':
cfmt=0;
break;
case 'p': /* puny memory option */
smsize=SMALLSMSIZE;
break;
case '-':
keycheck=0;
break;
default:
fprintf(stderr,"usage: cmkdir [-osp-] dir\n");
exit(1);
}
}
if (argc!=1) {
fprintf(stderr,"Usage: cmkdir [-osp-] dir\n");
exit(1);
}
if (keycheck) {
if ((pw=getpassword("Key:"))==NULL) {
fprintf(stderr,"Can't get key\n");
exit(1);
}
strcpy(pword,pw);
if (strlen(pw)<16) {
fprintf(stderr,"Key must be at least 16 chars.\n");
exit(1);
}
if ((pw=getpassword("Again:"))==NULL) {
fprintf(stderr,"Can't get key\n");
exit(1);
}
if (strcmp(pword,pw)!=0) {
fprintf(stderr,
"Keys don't match; drink some coffee and try again\n");
exit(1);
}
}
else { /* just accept key from stdio */
if (fgets(pword,256,stdin) == NULL) {
perror("cmkdir");
exit(1);
}
pw=pword;
pw[255]='\0';
n=strlen(pw);
if ((n>0) && (pw[n-1] == '\n'))
pw[n-1] = '\0';
}
if (smsize != LARGESMSIZE)
sprintf(pw,"%s%d",pword,smsize);
k.cipher=ciph;
if (cfmt==0) {
if (old_pwcrunch(pw,&k)!=0) {
fprintf(stderr,"Invalid key\n");
exit(1);
}
} else {
/* this is very ugly and will be replaced but it works */
if (new_pwcrunch(pw,&k)!=0) {
fprintf(stderr,"Invalid key\n");
exit(1);
}
/* now we xor in some truerand bytes for good measure */
bcopy(&k,ekey,32); /* assumes key material < 32 bytes */
for (i=0; i<32; i++) {
ekey[i] ^= randbyte();
}
encrypt_key(&k,ekey);
bcopy(ekey,ek1,32);
decrypt_key(&k,ek1);
/* new &k is our real key */
}
if (mkdir(argv[0],0777)<0) {
perror("cmkdir");
exit(1);
}
sprintf(path,"%s/...",argv[0]);
strcpy(str,"qua!");
/* now randomize the end of str.. */
r = trand32();
for (i=0; i<4; i++)
str[i+4]=(r<<(i*8))&0377;
copykey(&k,&kt);
cipher(&kt,str,0);
mask_cipher(&kt,str,1);
cipher(&kt,str,0);
if ((fp=fopen(path,"w")) == NULL) {
perror("cmkdir");
exit(1);
}
fwrite(str,8,1,fp);
fclose(fp);
sprintf(path,"%s/..c",argv[0]);
if ((fp=fopen(path,"w")) == NULL) {
perror("cmkdir");
exit(1);
}
fprintf(fp,"%d",k.cipher);
fclose(fp);
sprintf(path,"%s/..s",argv[0]);
if ((fp=fopen(path,"w")) == NULL) {
perror("cmkdir");
exit(1);
}
fprintf(fp,"%d\n",smsize);
fclose(fp);
if (cfmt) {
sprintf(path,"%s/..k",argv[0]);
if ((fp=fopen(path,"w")) == NULL) {
perror("cmkdir");
exit(1);
}
fwrite(ekey,32,1,fp);
fclose(fp);
}
exit(0);
}