1 /* change password or add user to S/KEY authentication system.
2 * S/KEY is a tradmark of Bellcore.
4 * $DragonFly: src/usr.bin/keyinit/skeyinit.c,v 1.2 2003/10/04 20:36:46 hmp Exp $
21 main(int argc
, char **argv
)
24 int rval
,n
,nn
,i
,defaultsetup
;
25 char seed
[18],tmp
[80],key
[8];
26 struct passwd
*ppuser
,*pp
;
27 char defaultseed
[17], passwd
[256],passwd2
[256] ;
32 char tbuf
[27],buf
[60];
37 #if 0 /* Choose a more random seed */
39 strftime(tbuf
, sizeof(tbuf
), "%M%j", tm
);
41 sprintf(tbuf
, "%05ld", (long) (now
% 100000));
43 gethostname(defaultseed
,NAMELEN
);
44 strcpy(&defaultseed
[NAMELEN
],tbuf
);
46 pp
= ppuser
= getpwuid(getuid());
47 strcpy(me
,pp
->pw_name
);
50 if(strcmp("-s", argv
[1]) == 0)
53 pp
= getpwnam(argv
[1]);
55 pp
= getpwnam(argv
[2]);
59 printf("User unknown\n");
62 if(strcmp( pp
->pw_name
,me
) != 0){
64 /* Only root can change other's passwds */
65 printf("Permission denied.\n");
72 rval
= skeylookup(&skey
,pp
->pw_name
);
75 perror("error in opening database");
78 printf("Updating %s:\n",pp
->pw_name
);
79 printf("Old key: %s\n",skey
.seed
);
80 /* lets be nice if they have a skey.seed that ends in 0-8 just add one*/
81 l
= strlen(skey
.seed
);
83 lastc
= skey
.seed
[l
-1];
84 if( isdigit(lastc
) && lastc
!= '9' ){
85 strcpy(defaultseed
, skey
.seed
);
86 defaultseed
[l
-1] = lastc
+ 1;
88 if( isdigit(lastc
) && lastc
== '9' && l
< 16){
89 strcpy(defaultseed
, skey
.seed
);
90 defaultseed
[l
-1] = '0';
92 defaultseed
[l
+1] = '\0';
97 skey
.val
= 0; /* XXX */
98 printf("Adding %s:\n",pp
->pw_name
);
103 printf("Reminder you need the 6 english words from the key command.\n");
106 printf("Enter sequence count from 1 to 9999: ");
107 fgets(tmp
,sizeof(tmp
),stdin
);
109 if(n
> 0 && n
< 10000)
110 break; /* Valid range */
111 printf("Count must be > 0 and < 10000\n");
115 printf("Enter new key [default %s]: ", defaultseed
);
117 fgets(seed
,sizeof(seed
),stdin
);
119 if(strlen(seed
) > 16){
120 printf("Seed truncated to 16 chars\n");
123 if( seed
[0] == '\0') strcpy(seed
,defaultseed
);
126 printf("s/key %d %s\ns/key access password: ",n
,seed
);
127 fgets(tmp
,sizeof(tmp
),stdin
);
130 printf("Enter 6 English words from secure S/Key calculation.\n");
136 if(etob(key
,tmp
) == 1 || atob8(key
,tmp
) == 0)
137 break; /* Valid format */
138 printf("Invalid format, try again with 6 English words.\n");
141 /* Get user's secret password */
142 fprintf(stderr
,"Reminder - Only use this method if you are directly connected.\n");
143 fprintf(stderr
,"If you are using telnet or rlogin exit with no password and use keyinit -s.\n");
146 fprintf(stderr
,"Enter secret password: ");
147 readpass(passwd
,sizeof(passwd
));
148 if(passwd
[0] == '\0'){
151 fprintf(stderr
,"Again secret password: ");
152 readpass(passwd2
,sizeof(passwd
));
153 if(passwd2
[0] == '\0'){
156 if(strlen(passwd
) < 4 && strlen(passwd2
) < 4) {
157 fprintf(stderr
, "Sorry your password must be longer\n\r");
160 if(strcmp(passwd
,passwd2
) == 0) break;
161 fprintf(stderr
, "Sorry no match\n");
165 strcpy(seed
,defaultseed
);
167 /* Crunch seed and password into starting key */
168 if(keycrunch(key
,seed
,passwd
) != 0)
169 errx(1, "key crunch failed");
175 tm
= localtime(&now
);
176 strftime(tbuf
, sizeof(tbuf
), " %b %d,%Y %T", tm
);
177 if (skey
.val
== NULL
)
178 skey
.val
= (char *) malloc(16+1);
182 fprintf(skey
.keyfile
,"%s %04d %-16s %s %-21s\n",pp
->pw_name
,n
,
183 seed
,skey
.val
, tbuf
);
184 fclose(skey
.keyfile
);
185 printf("\nID %s s/key is %d %s\n",pp
->pw_name
,n
,seed
);
186 printf("%s\n",btoe(buf
,key
));
188 printf("%s\n",put8(buf
,key
));