-
Notifications
You must be signed in to change notification settings - Fork 13
/
cpm.c
357 lines (315 loc) · 9.94 KB
/
cpm.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* #############################################################################
* program to manage passwords from the commandline
* #############################################################################
* Copyright (C) 2005-2009 Harry Brueckner
*
* 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 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.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contact: Harry Brueckner <[email protected]>
* Muenchener Strasse 12a
* 85253 Kleinberghofen
* Germany
* #############################################################################
*/
/* #############################################################################
* includes
*/
#include "cpm.h"
#include "configuration.h"
#include "general.h"
#include "gpg.h"
#include <termios.h>
#include "interface_cli.h"
#include "interface_gui.h"
#include "interface_keys.h"
#include "interface_utf8.h"
#include "interface_xml.h"
#include "memory.h"
#include "options.h"
#include "patternparser.h"
#include "resource.h"
#include "security.h"
#include "xml.h"
#include "zlib.h"
/* #############################################################################
* internal functions
*/
RETSIGTYPE sighandler(int signum);
#ifdef TEST_OPTION
void testMemset(void);
#endif
/* #############################################################################
* global variables
*/
#ifdef MANUAL_EXTERN_ENVIRON
char** environ;
#endif
static struct termios tcsaved;
void savetermios(void)
{
tcgetattr(0, &tcsaved);
}
void restoretermios(void)
{
tcsetattr(0, TCSANOW, &tcsaved);
}
/* #############################################################################
*
* Description main function of the program
* Author Harry Brueckner
* Date 2005-03-16
* Arguments int argc - argument counter
* char** argv - commandline arguments
* char** envp - process environment (only on Solaris)
* Return return code of the program
*/
#ifdef HAVE_EXTERN_ENVIRON
/* everything is in place, we have the environ variable */
int main(int argc, char **argv)
#else
#ifdef MANUAL_EXTERN_ENVIRON
/* we can manually declare the variable */
int main(int argc, char **argv)
#else
/* if don't have any envorin variable at all */
int main(int argc, char **argv, char **envp)
#endif
#endif
{
rlim_t memlock_limit = -2;
int error = 0,
max_mem_lock = 0,
memory_safe = 0,
ptrace_safe = 0;
#ifdef TEST_OPTION
int testrun = 0;
#endif
char* binaryname;
savetermios();
TRACE(99, "main()", NULL);
#ifndef HAVE_EXTERN_ENVIRON
#ifndef MANUAL_EXTERN_ENVIRON
/* since in solaris environ does not exist, we manually pass it along */
environ = envp;
#endif
#endif
if (initSecurity(&max_mem_lock, &memory_safe, &ptrace_safe, &memlock_limit))
{ exit(1); }
/* we initialize gettext */
setlocale(LC_ALL, "");
#ifdef TEST_OPTION
bindtextdomain(PACKAGE_NAME, "./po/");
#else
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
#endif
textdomain(PACKAGE_NAME);
#ifndef LIBXML_TREE_ENABLED
fprintf(stderr, _("Tree support not compiled in to libxml2 %s\n"),
LIBXML_DOTTED_VERSION);
exit(1);
#endif
/*
* This function installs "sighandler" to handle the SIGINT and returns a
* pointer to the previously installed handler for this signal (which is
* the default handler SIG_DFL initially). If we try to install another
* handler to handle SIGINT at some other time... Then the new handler
* replaces this current one and returns a pointer to this handler.
*/
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
signal(SIGALRM, sighandler);
/* the SIGWINCH handler is set in userInterface() */
initConfiguration();
runtime -> memlock_limit = memlock_limit;
runtime -> max_mem_lock = max_mem_lock;
runtime -> memory_safe = memory_safe;
runtime -> ptrace_safe = ptrace_safe;
initKeys();
initPatternparser();
initXML();
initXMLInterface();
if (getOptions(argc, argv))
{
fprintf(stderr, _("Try `%s --help' for more information.\n"), argv[0]);
error = 1;
}
if (!error && config -> help)
{ showHelp(); }
else if (!error && config -> version)
{ showVersion(); }
else if (!error)
{
getDefaultOptions();
if (readResources())
return 1;
if (config -> dbfilecmd)
{ /* the --file option must overwrite the resource file */
runtime -> dbfile = resolveFilelink(config -> dbfilecmd);
}
else
{ /* we use the resource file configuration or the compiletime
* default
*/
runtime -> dbfile = resolveFilelink(config -> dbfilerc);
}
}
/* we switch to read-only mode on request */
if (config -> readonly)
{ runtime -> readonly = 1; }
/* in case our basename is cpmv, we switch to read-only mode */
binaryname = basename(argv[0]);
if (!strcmp(binaryname, "cpmv"))
{ runtime -> readonly = 1; }
initGPG();
if (!error && config -> security)
{ checkSecurity(0); }
#ifdef TEST_OPTION
if (!error &&
config -> testrun &&
!strncmp(config -> testrun, "compress", 8))
{
testCompress();
testrun = 1;
}
if (!error &&
config -> testrun &&
!strcmp(config -> testrun, "environment"))
{
testEnvironment();
testrun = 1;
}
if (!error &&
config -> testrun && (
!strcmp(config -> testrun, "backup") ||
!strcmp(config -> testrun, "garbage") ||
!strcmp(config -> testrun, "searchpattern")))
{ testrun = 1; }
#endif
if (config -> configtest &&
!error)
{ fprintf(stderr, _("configuration ok.\n")); }
if (config -> environtmentlist &&
!error)
{ listEnvironment(); }
if (!error &&
!config -> configtest &&
!config -> environtmentlist &&
!config -> help &&
!config -> security &&
!config -> version)
{
#ifdef TEST_OPTION
if (checkSecurity(1) != MAX_SECURITY_LEVEL &&
!config -> testrun)
#else
if (checkSecurity(1) != MAX_SECURITY_LEVEL)
#endif
{
checkSecurity(0);
printf("\n%s %s\n%s\n%s\n",
_("Maximum security level not reached."),
_("Your database will be less protected while CPM is running."),
_("Are you sure you want to continue?"),
_("Press CTRL+C to stop now or ENTER to continue."));
fgetc(stdin);
}
if (runtime -> guimode)
{ /* we run in interactive mode */
/* set inactivity timeout */
alarm(config->inactivetimeout);
userInterface();
}
else
{ /* we run in CLI mode */
error = cliInterface();
#ifdef TEST_OPTION
if (error == 2)
{ /* for testruns, we must modify the stuff a little */
error = 0;
testrun = 1;
}
#endif
}
}
freeGPG();
freeXMLInterface();
freeUTF8Interface();
freeXML();
freePatternparser();
freeKeys();
freeConfiguration();
if (memCheck())
{ /* we validate our memory consumption */
fprintf(stderr, _("error: memory leak detected.\n"));
if (memCheck() > 0)
{
fprintf(stderr, _("%ld byte of memory were not freed.\n"),
memCheck());
}
else
{
fprintf(stderr,
_("%ld byte of memory were freed without being allocated.\n"),
memCheck());
}
fprintf(stderr, _("Please send a report about this problem to Harry Brueckner <[email protected]>.\n"));
error = 1;
}
#ifdef TEST_OPTION
if (testrun)
{ return 0; }
else
{ return error; }
#else
return error;
#endif
}
/* #############################################################################
*
* Description signal handler to catch SIGINT and SIGTERM
* Author Harry Brueckner
* Date 2005-03-30
* Arguments int signum - signal number
* Return void
*/
RETSIGTYPE sighandler(int signum){
char* dummy;
clear_screen();
switch (signum) {
case SIGALRM:
if(runtime->datachanged) {
fprintf(stderr, "\nInactivity but data changed, not quitting\n");
return;
}
case SIGINT:
case SIGTERM:
/* we have to quit right away */
freeGPG();
freeXMLInterface();
freeUTF8Interface();
freeXML();
freePatternparser();
freeKeys();
if(runtime->lockfilecreated) { fileLockRemove(&dummy); }
restoretermios();
freeConfiguration();
if(signum == SIGALRM)
fprintf(stderr, "\nCPM: Inactivity timeout\n");
else
fprintf(stderr,"\nquitting on signal %d\n", signum);
_exit(1);
default:
fprintf(stderr, "\nreceived unknown signal %d\n", signum);
}
}
/* #############################################################################
*/