else return 1;
}
+static ObfuscatedPasswd* readpassword() {
+ while (true) {
+ PlainPasswd passwd(getpassword("Password:"));
+ if (!passwd.buf) {
+ perror("getpassword error");
+ exit(1);
+ }
+ if (strlen(passwd.buf) < 6) {
+ if (strlen(passwd.buf) == 0) {
+ fprintf(stderr,"Password not changed\n");
+ exit(1);
+ }
+ fprintf(stderr,"Password must be at least 6 characters - try again\n");
+ continue;
+ }
+
+ PlainPasswd passwd2(getpassword("Verify:"));
+ if (!passwd2.buf) {
+ perror("getpass error");
+ exit(1);
+ }
+ if (strcmp(passwd.buf, passwd2.buf) != 0) {
+ fprintf(stderr,"Passwords don't match - try again\n");
+ continue;
+ }
+
+ return new ObfuscatedPasswd(passwd);
+ }
+}
+
int main(int argc, char** argv)
{
prog = argv[0];
}
while (true) {
- PlainPasswd passwd(getpassword("Password:"));
- if (!passwd.buf) {
- perror("getpassword error");
- exit(1);
- }
- if (strlen(passwd.buf) < 6) {
- if (strlen(passwd.buf) == 0) {
- fprintf(stderr,"Password not changed\n");
- exit(1);
- }
- fprintf(stderr,"Password must be at least 6 characters - try again\n");
- continue;
- }
+ ObfuscatedPasswd* obfuscated = readpassword();
+ ObfuscatedPasswd* obfuscatedReadOnly = 0;
- PlainPasswd passwd2(getpassword("Verify:"));
- if (!passwd2.buf) {
- perror("getpass error");
- exit(1);
- }
- if (strcmp(passwd.buf, passwd2.buf) != 0) {
- fprintf(stderr,"Passwords don't match - try again\n");
- continue;
+ fprintf(stderr, "Would you like to enter a view-only password (y/n)? ");
+ char yesno[3];
+ if (fgets(yesno, 3, stdin) != NULL && (yesno[0] == 'y' || yesno[0] == 'Y')) {
+ obfuscatedReadOnly = readpassword();
}
FILE* fp = fopen(fname,"w");
}
chmod(fname, S_IRUSR|S_IWUSR);
- ObfuscatedPasswd obfuscated(passwd);
-
- if (fwrite(obfuscated.buf, obfuscated.length, 1, fp) != 1) {
+ if (fwrite(obfuscated->buf, obfuscated->length, 1, fp) != 1) {
fprintf(stderr,"Writing to %s failed\n",fname);
exit(1);
}
+ if (obfuscatedReadOnly) {
+ if (fwrite(obfuscatedReadOnly->buf, obfuscatedReadOnly->length, 1, fp) != 1) {
+ fprintf(stderr,"Writing to %s failed\n",fname);
+ exit(1);
+ }
+ }
+
fclose(fp);
return 0;