This repository was archived by the owner on Oct 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.c
More file actions
162 lines (129 loc) · 3.04 KB
/
database.c
File metadata and controls
162 lines (129 loc) · 3.04 KB
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
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "database.h"
#include "error.h"
#ifdef _WIN32
#define ENV_HOME "HOMEPATH"
#else
#define ENV_HOME "HOME"
#endif
bool openssl_valid() {
FILE *result = popen("openssl version", "r");
if (!result) {
last_error = ERR_OPENSSL_INVALID;
return false;
}
char version[20] = {'0'};
fgets(version, 20, result);
pclose(result);
// OpenSSL x.y.z
// ^
if ((version[8] - '0') < 3) {
last_error = ERR_OPENSSL_INVALID;
return false;
}
return true;
}
/**
* Sets users home directory for release builds and the current working
* directory for debug builds.
*/
bool get_db_path(char *db_path) {
#ifndef NDEBUG
sprintf(db_path, "./passdb");
#else
char *homedir = getenv(ENV_HOME);
if (!homedir) {
last_error = ERR_DB_HOME_DIR;
return false;
}
sprintf(db_path, "%s/passdb", homedir);
#endif
return true;
}
bool database_exists() {
char db_path[FS_MAX_PATH_LENGTH];
bool path_ok = get_db_path(db_path);
if (!path_ok) {
return false;
}
FILE *db = fopen(db_path, "r");
if (!db) {
return false;
}
fclose(db);
return true;
}
bool create_database(char *master_pwd) {
char db_path[FS_MAX_PATH_LENGTH];
bool path_ok = get_db_path(db_path);
if (!path_ok) {
return false;
}
// no database, use OpenSSL to create initial database
char command[FS_MAX_PATH_LENGTH];
sprintf(command,
"openssl enc -aes-256-cbc -pbkdf2 -iter 100000 -out %s -pass pass:%s",
db_path, master_pwd);
FILE *db = popen(command, "w");
if (!db) {
last_error = ERR_DB_OPEN_FAILED;
return false;
}
pclose(db);
return true;
}
bool read_database(char *master_pwd, Lines lines, int *lines_read) {
char db_path[FS_MAX_PATH_LENGTH];
bool path_ok = get_db_path(db_path);
if (!path_ok) {
return false;
}
char command[FS_MAX_PATH_LENGTH];
sprintf(
command,
"openssl enc -aes-256-cbc -pbkdf2 -iter 100000 -d -in %s -pass pass:%s",
db_path, master_pwd);
FILE *db = popen(command, "r");
if (!db) {
last_error = ERR_DB_OPEN_FAILED;
return false;
}
Line line;
int count = 0;
while (fgets(line, sizeof(line), db) != NULL) {
strtok(line, "\n"); // remove trailing new line
memcpy(lines[count++], line, sizeof(line));
}
*lines_read = count;
// master password checks out?
int res = pclose(db);
if (res > 0) {
last_error = ERR_DB_MASTER_PWD;
return false;
}
return true;
}
bool save_database(char *master_pwd, Lines lines, int num_lines) {
char db_path[FS_MAX_PATH_LENGTH];
bool path_ok = get_db_path(db_path);
if (!path_ok) {
return false;
}
char command[FS_MAX_PATH_LENGTH];
sprintf(command,
"openssl enc -aes-256-cbc -pbkdf2 -iter 100000 -out %s -pass pass:%s",
db_path, master_pwd);
FILE *db = popen(command, "w");
if (!db) {
last_error = ERR_DB_OPEN_FAILED;
return false;
}
for (int i = 0; i < num_lines; i++) {
fprintf(db, "%s\n", lines[i]);
}
pclose(db);
return true;
}