-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswdm.c
206 lines (191 loc) · 5.29 KB
/
passwdm.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Passwdm: CLI-based password manager.
* Copyright (C) 2012 Daniel Gibbs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database.h"
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#define COMMAND_DELIMETERS " \t\r\n"
#define DEFAULT_PROMPT "> "
static void perform_command(char *);
static void save_and_close();
static char *db_dir = NULL;
static char *prompt = DEFAULT_PROMPT;
static struct database *db = NULL;
int main() {
// Check that password database directory exists, and if not creates it.
char *home_dir = getenv("HOME");
if(home_dir == NULL) {
fprintf(stderr, "Unable to locate home directory.");
return 1;
}
if(asprintf(&db_dir, "%s/.passwdm", home_dir) == -1) {
fprintf(stderr, "Out of memory.\n");
return 1;
}
if(mkdir(db_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1 && errno != EEXIST) {
fprintf(stderr, "Unable to create password database directory.\n");
return 1;
}
// Prevent TAB from auto-completing file names.
rl_bind_key('\t', rl_insert);
// Read commands from the user.
char *command;
while(1) {
command = readline(prompt);
if(command == NULL) {
if(db != NULL) {
printf("close\n");
save_and_close();
continue;
}
else {
printf("exit\n");
break;
}
}
// Maintain a history of commands.
if(*command)
add_history(command);
// Catch exit commands.
if(strcmp(command, "quit") == 0 || strcmp(command, "exit") == 0)
break;
// Handle commands.
else
perform_command(command);
free(command);
}
save_and_close();
return 0;
}
void perform_command(char *command) {
char *keyword = strtok(command, COMMAND_DELIMETERS);
if(keyword == NULL)
return;
// Create a password database.
else if(strcmp(keyword, "create") == 0) {
// Get the database name.
char *db_name = strtok(NULL, COMMAND_DELIMETERS);
char *arg = strtok(NULL, COMMAND_DELIMETERS);
if(arg != NULL) {
printf("Unknown argument: %s\n", arg);
}
else if(db_name == NULL) {
printf("Password database name required.\n");
}
else if(db != NULL) {
printf("Current password database must be closed before another one can be created.\n");
}
// Change to the database and update the prompt.
else {
char *db_filename;
if(asprintf(&db_filename, "%s/%s", db_dir, db_name) == -1) {
fprintf(stderr, "Out of memory.\n");
return;
}
if(asprintf(&prompt, "%s> ", db_name) == -1) {
fprintf(stderr, "Out of memory.\n");
prompt = DEFAULT_PROMPT;
return;
}
char *pass_prompt;
if(asprintf(&pass_prompt, "Passphrase for %s: ", db_name) == -1) {
fprintf(stderr, "Out of memory.\n");
return;
}
char *passphrase = getpass(pass_prompt);
if(create_database(&db, db_filename, passphrase) != 0) {
database_perror("Error creating database");
prompt = DEFAULT_PROMPT;
return;
}
}
}
// Open a password database.
else if(strcmp(keyword, "open") == 0) {
// Get the database name.
char *db_name = strtok(NULL, COMMAND_DELIMETERS);
char *arg = strtok(NULL, COMMAND_DELIMETERS);
if(arg != NULL) {
printf("Unknown argument: %s\n", arg);
}
else if(db_name == NULL) {
printf("Password database name required.\n");
}
else if(db != NULL) {
printf("Current password database must be closed before another one can be opened.\n");
}
// Change to the database and update the prompt.
else {
char *db_filename;
if(asprintf(&db_filename, "%s/%s", db_dir, db_name) == -1) {
fprintf(stderr, "Out of memory.\n");
return;
}
if(asprintf(&prompt, "%s> ", db_name) == -1) {
fprintf(stderr, "Out of memory.\n");
prompt = DEFAULT_PROMPT;
return;
}
char *pass_prompt;
if(asprintf(&pass_prompt, "Passphrase for %s: ", db_name) == -1) {
fprintf(stderr, "Out of memory.\n");
return;
}
char *passphrase = getpass(pass_prompt);
if(open_database(&db, db_filename, passphrase) != 0) {
database_perror("Error opening database");
prompt = DEFAULT_PROMPT;
return;
}
}
}
// Close the current password database.
else if(strcmp(keyword, "close") == 0) {
char *arg = strtok(NULL, COMMAND_DELIMETERS);
if(db == NULL) {
printf("No password database currently open.\n");
}
else if(arg != NULL && strcmp(arg, db->name) != 0) {
printf("Unknown argument: %s\n", arg);
}
else {
save_and_close();
}
}
else {
printf("%s: command not found.\n", keyword);
}
}
void save_and_close() {
if(db == NULL)
return;
if(save_database(db) != 0) {
database_perror("Error saving database");
}
close_database(db);
prompt = DEFAULT_PROMPT;
db = NULL;
}