-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpasswd.c
201 lines (192 loc) · 4.45 KB
/
cpasswd.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* The author of this software is Matt Blaze.
* Copyright (c) 1995 by AT&T.
* Permission to use, copy, and modify this software without fee
* is hereby granted, provided that this entire notice is included in
* all copies of any software which is or includes a copy or
* modification of this software and in all copies of the supporting
* documentation for such software.
*
* This software is subject to United States export controls.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*/
/*
* client cfs cpasswd - 1.3
*/
#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 buf[1024];
char *getpassword();
cfs_admkey oldkey;
cfs_admkey newkey;
cfskey kt;
char nname[1024];
char oname[1024];
char cname[1024];
char kname[1024];
char sname[1024];
char lname[1024];
char dir[1024];
int smsize;
FILE *fp;
char *flg;
int ciph=CFS_SAFER_SK128;
int cfmt=1;
unsigned char ekey[128];
while (--argc && (**++argv == '-')) {
for (flg= ++*argv; *flg; ++flg)
switch (*flg) {
default:
fprintf(stderr,"usage: cpasswd dir\n");
exit(1);
}
}
if (argc!=1) {
fprintf(stderr,"Usage: cpasswd dir\n");
exit(1);
}
if (*argv[0]!='/') {
if (getcwd(buf,1024) == NULL) {
fprintf(stderr,"Can't stat current directory\n");
exit(1);
}
sprintf(dir,"%s/%s",buf,argv[0]);
} else
strcpy(dir,argv[0]);
sprintf(kname,"%s/..k",dir);
sprintf(nname,"%s/..n",dir);
sprintf(oname,"%s/..o",dir);
sprintf(lname,"%s/..data",dir);
if (chdir(lname) >= 0)
strcpy(dir,lname);
else if (chdir(dir)<0) {
perror(dir);
exit(1);
}
sprintf(cname,"%s/..c",dir);
sprintf(sname,"%s/..s",dir);
if ((fp=fopen(cname,"r")) == NULL) {
fprintf(stderr,"Can only change passphrase on new format CFS directories\n");
exit(1);
} else {
fscanf(fp,"%d",&ciph);
fclose(fp);
}
if (((fp=fopen(kname,"r")) == NULL) || (fread(ekey,1,32,fp)<16)) {
perror(dir);
fprintf(stderr,"Can only change passphrase on new format CFS directories\n");
exit(1);
}
fclose(fp);
if ((fp=fopen(sname,"r")) == NULL) {
smsize=LARGESMSIZE;
} else {
if (fscanf(fp,"%d",&smsize) != 1)
smsize=LARGESMSIZE;
fclose(fp);
if ((smsize < CFSBLOCK) || (smsize > (LARGESMSIZE*2)))
smsize=LARGESMSIZE;
}
oldkey.cipher=ciph;
if ((pw=getpassword("Old passphrase:"))==NULL) {
fprintf(stderr,"Can't get key\n");
exit(1);
}
if (smsize != LARGESMSIZE)
sprintf(pw,"%s%d",pw,smsize);
if (new_pwcrunch(pw,&oldkey)!=0) {
fprintf(stderr,"Invalid key\n");
exit(1);
}
decrypt_key(&oldkey,ekey);
if (!checkkey(dir,&oldkey)) {
fprintf(stderr,"Incorrect passphrase\n");
exit(1);
}
newkey.cipher=ciph;
if ((pw=getpassword("New passphrase:"))==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);
}
if (smsize != LARGESMSIZE)
sprintf(pw,"%s%d",pw,smsize);
if (new_pwcrunch(pw,&newkey)!=0) {
fprintf(stderr,"Invalid key\n");
exit(1);
}
encrypt_key(&newkey,ekey);
if ((fp=fopen(nname,"w")) == NULL) {
perror("cmkdir");
exit(1);
}
if (fwrite(ekey,1,32,fp) != 32) {
perror("can't create new key file");
exit(1);
}
fclose(fp);
/* do this in 3 phases, ultra paranoid */
if (rename(kname,oname) < 0) {
perror("can't rename old key file");
exit(1);
}
if (rename(nname,kname) < 0) {
perror("can't link new key file");
exit(1);
}
if (unlink(oname)<0)
perror("warning: old key file not removed");
exit(0);
}
checkkey(path,ak)
char *path;
cfs_admkey *ak;
{
FILE *fp;
char fn[1024];
char buf[9];
cfskey k;
copykey(ak,&k);
sprintf(fn,"%s/...",path);
if ((fp=fopen(fn,"r"))==NULL)
return 0;
if (fread(buf,8,1,fp)!=1) {
fclose (fp);
return 0;
}
fclose (fp);
cipher(&k,buf,1); /* note order here */
mask_cipher(&k,buf,0);
cipher(&k,buf,1); /* note order here */
if (bcmp(buf,"qua!",4)!=0)
return 0;
return 1;
}