-
Notifications
You must be signed in to change notification settings - Fork 0
/
htpasswd.c
56 lines (36 loc) · 1.09 KB
/
htpasswd.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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv) {
char password_first[1024], password_repeat[1024];
char *input, *passwordfile, *username, *encrypted;
FILE *htpasswd_file;
if(argc < 3) {
printf("Usage:\n\n");
printf("htpasswd passwordfile username\n");
printf("htpasswd passwordfile username password\n\n");
printf("Creates or updates an Apache/nginx compatible htpasswd file\n");
return 1;
}
passwordfile = argv[1];
username = argv[2];
if(argc != 4) {
input = getpass("Password: ");
strcpy(password_first, input);
input = getpass("Password (again): ");
strcpy(password_repeat, input);
memset(input, 0, strlen(input));
int password_compare = strcmp(password_first, password_repeat);
if(password_compare != 0) {
printf("Passwords did not match\n");
return 2;
}
} else {
strcpy(password_first, argv[3]);
}
encrypted = (char *)crypt(password_first, password_first);
htpasswd_file = fopen(passwordfile, "a");
fprintf(htpasswd_file, "%s:%s\n", username, encrypted);
fclose(htpasswd_file);
return 0;
}