-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
157 lines (135 loc) · 3.19 KB
/
main.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
/*
* opm - Open Password Manager.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Alexander Miroch
* Email: <alexander.miroch@gmail.com>
*/
#include "opm.h"
void usage(int retcode) {
FILE *stream = (retcode > 0) ? stderr : stdout;
fprintf(stream, "%s", help_string);
exit(retcode);
}
void emsg(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
exit(255);
}
int main(int argc, char *argv[]) {
int opt, option_index;
int opt_add_entry = 0;
int opt_list = 0;
int opt_verbose = 0;
int opt_console = 0;
int opt_remove_entry = 0;
int opt_stop = 0;
char *string;
while ((opt = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
switch (opt) {
case 0x300:
break;
case 'L':
opt_list = 1;
break;
case 'A':
opt_add_entry = 1;
break;
case 'R':
opt_remove_entry = atoi(optarg);
break;
case 'D':
database_file = optarg;
break;
case 'c':
opt_console = 1;
break;
case 'S':
opt_stop = 1;
break;
case 'v':
opt_verbose = 1;
break;
case 'h':
case 'H':
usage(0);
case ':':
case '?':
usage(1);
}
}
string = argv[optind];
if (!database_file) {
pid_t uid;
struct passwd *pw;
uid = getuid();
pw = getpwuid(uid);
if (!pw) {
fprintf(stderr, "Can not get your homedir\n");
exit(1);
}
database_file = (char *) malloc(sizeof(char) * strlen(pw->pw_dir) + strlen(DEFAULT_DATABASE_FILE) + 1);
if (!database_file) {
fprintf(stderr, "Memory allocation error\n");
exit(1);
}
strcpy(database_file, pw->pw_dir);
strcat(database_file, "/");
strcat(database_file, DEFAULT_DATABASE_FILE);
}
if (strlen(database_file) >= PATH_MAX) {
fprintf(stderr, "Too long path for database\n");
exit(1);
}
if (opt_stop) {
if (!is_daemon_started()) {
fprintf(stderr, "Daemon is not started\n");
exit(1);
}
stop_daemon();
printf("Daemon stopped\n");
exit(0);
}
if (!is_daemon_started()) {
start_daemon();
wait_for_daemon();
}
if (opt_add_entry) {
add_entry();
exit(0);
}
if (opt_list) {
if (!list_db(opt_verbose)) {
fprintf(stderr, "Failed to list password database\n");
exit(1);
}
exit(0);
}
if (opt_remove_entry) {
if (!remove_entry(opt_remove_entry)) {
fprintf(stderr, "Failed to remove entry from database\n");
exit(1);
}
exit(0);
}
if (!get_entry(string, opt_verbose, opt_console)) {
fprintf(stderr, "Failed to get entry\n");
exit(1);
}
return 0;
}