-
Notifications
You must be signed in to change notification settings - Fork 0
/
changepw.cpp
55 lines (45 loc) · 1.67 KB
/
changepw.cpp
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
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include "client.hpp"
#include "openssl-sign-by-ca-master/openssl1.1/main.cpp"
int main(int argc, char* argv[]) {
Info info;
info.action = changepw;
std::string private_key_path = "";
if (argc <= 1 or argc > 4) {
// argc must be 1 to 3
fprintf(stderr, "Usage: ./changepw.out <username> <password>? <new_password>?\n");
exit(1);
}
// argc is 2, 3 or 4
info.username = std::string(argv[1]);
info.password = (argc == 2) ? std::string(getpass("Input a password:")) : std::string(argv[2]);
info.new_password = (argc == 3) ? std::string(getpass("Input a new password:")) : std::string(argv[3]);
std::string key, csr;
generate_key_and_csr(info.username, key, csr);
info.csr = csr;
// info.print_info();
std::string code, body;
try {
client_send(info, code, body, private_key_path);
} catch(...) {
fprintf(stderr, "Failed to get response from server. Please check your input and the liveness of the server.\n");
exit(1);
}
// If get 200 OK, save key and received certificate
if (code == "200") {
std::ofstream key_pem_file("./" + info.username + "_private_key.pem");
key_pem_file << key;
key_pem_file.close();
std::ofstream certificate_pem_file("./" + info.username + "_certificate.pem");
certificate_pem_file << body;
certificate_pem_file.close();
fprintf(stdout, "Password is changed!\nGot a new certificate:\n\n%s\n", body.c_str());
} else {
fprintf(stderr, "Error from server: %s.\n", body.c_str());
exit(1);
}
return 0;
}