-
Notifications
You must be signed in to change notification settings - Fork 13
/
security.c
841 lines (749 loc) · 24.1 KB
/
security.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/* #############################################################################
* code for general security like handling the memory protection
* #############################################################################
* 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 <sys/utsname.h>
#if defined(HAVE_SYS_PRCTL_H)
#include <sys/ptrace.h>
#include <sys/prctl.h>
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <regex.h>
#include "configuration.h"
#include "general.h"
#include "memory.h"
#include "security.h"
#include "string.h"
/* #############################################################################
* internal functions
*/
int checkEnvValue(int showerror, int type, char* value);
int clearEnvironment(void);
/* #############################################################################
* global variables
*/
#define STAT_GREEN "[1;32m"
#define STAT_YELLOW "[1;33m"
#define STAT_RED "[1;31m"
#define STAT_OFF "[0m"
#define STRTYPE_NOCHECK 0
#define STRTYPE_GENERAL 1
#define STRTYPE_ALPHANUMERIC 2
#define STRTYPE_NUMERIC 3
#define STRTYPE_FILENAME 4
#define STRTYPE_PATH 5
/* #############################################################################
*
* Description check a given environ variable for the given type
* Author Harry Brueckner
* Date 2005-05-30
* Arguments int type - expected type of the value
* char* value - string to verify
* Return int 0 if ok, 1 on error
*/
int checkEnvValue(int showerror, int type, char* value)
{
regex_t regex;
int result;
char* errormsg;
char* pattern;
TRACE(99, "checkEnvValue()", NULL);
if (!value)
{
if (showerror)
{ fprintf(stderr, _("can not check null environment variable.\n")); }
return 1;
}
switch (type)
{
case STRTYPE_NOCHECK:
pattern = "^.*$";
break;
case STRTYPE_GENERAL:
pattern = "^[^=]*$";
break;
case STRTYPE_ALPHANUMERIC:
pattern = "^[[:alnum:][:blank:]@_\\.-]*$";
break;
case STRTYPE_NUMERIC:
pattern = "^[[:digit:]]+$";
break;
case STRTYPE_FILENAME:
pattern = "^[[:alnum:][:blank:]_\\.\\:/-]*$";
break;
case STRTYPE_PATH:
pattern = "^[[:alnum:][:blank:]_\\./:]*$";
/* for paths we do some other checks as well */
result = strlen(value);
if (result > 0)
{
if (value[0] == ':' ||
value[result - 1] == ':')
{ return 1; }
}
break;
default:
return 1;
}
result = regcomp(®ex, pattern, REG_EXTENDED | REG_NOSUB);
if (showerror && result)
{
errormsg = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
regerror(result, ®ex, errormsg, STDBUFFERLENGTH);
fprintf(stderr, "regerror: %s\n", errormsg);
memFree(__FILE__, __LINE__, errormsg, STDBUFFERLENGTH);
return 1;
}
result = regexec(®ex, value, 0, NULL, 0);
if (showerror && result)
{
errormsg = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
regerror(result, ®ex, errormsg, STDBUFFERLENGTH);
fprintf(stderr, "regerror: %s\n", errormsg);
memFree(__FILE__, __LINE__, errormsg, STDBUFFERLENGTH);
}
regfree(®ex);
return result;
}
/* #############################################################################
*
* Description check and display the currently used protections
* Author Harry Brueckner
* Date 2005-05-30
* Arguments int silent - 1 for no message display; 0 for messages
* Return level of security (yes answers)
*/
int checkSecurity(int silent)
{
struct rlimit rl;
int level = 0;
/* Flawfinder: ignore */
char memlimit[24];
TRACE(99, "checkSecurity()", NULL);
if (runtime -> memlock_limit == -2)
{ snprintf(memlimit, 24, "%s", "---"); }
else if (runtime -> memlock_limit == -1)
{ snprintf(memlimit, 24, "%s", _("unlimited")); }
else
{
snprintf(memlimit, 24, "%ld kB", (long)runtime -> memlock_limit / 1024);
}
if (!silent)
{ printf("%-50s", _("Running without root privileges:")); }
if (geteuid() && getegid() && getuid() && getgid())
{
if (!silent)
{ printf("%s%s%s\n", STAT_GREEN, _("yes"), STAT_OFF); }
level++;
}
else
{
if (!silent)
{ printf("%s%s%s\n", STAT_RED, _("no"), STAT_OFF); }
}
if (getrlimit(RLIMIT_CORE, &rl) == -1)
{
fprintf(stderr, "%s (%s, %d)\n",
_("Can not read RLIMIT_CORE resources."),
strerror(errno),
errno
);
return 1;
}
if (!silent)
{ printf("%-50s", _("Memory protection from core dumps:")); }
if (!rl.rlim_cur && !rl.rlim_max)
{
if (!silent)
{ printf("%s%s%s\n", STAT_GREEN, _("yes"), STAT_OFF); }
level++;
}
else
{
if (!silent)
{ printf("%s%s%s\n", STAT_RED, _("no"), STAT_OFF); }
}
#ifndef NO_MEMLOCK
#ifdef HAVE_MLOCKALL
if (!silent)
{ printf("%-50s", _("Memory protection from swap writings:")); }
if (runtime -> memory_safe)
{
if (!silent)
{ printf("%s%s%s\n", STAT_GREEN, _("yes"), STAT_OFF); }
level++;
}
else
{
if (!silent)
{ printf("%s%s%s\n", STAT_RED, _("no"), STAT_OFF); }
}
#ifdef __linux__
if (!silent)
{ printf("%-50s", _("Max. memory lock ok:")); }
if (runtime -> max_mem_lock)
{
if (!silent)
{ printf("%s%s%s (%s)\n", STAT_GREEN, _("yes"), STAT_OFF, memlimit); }
level++;
}
else
{
if (!silent)
{ printf("%s%s%s (%s)\n", STAT_RED, _("no"), STAT_OFF, memlimit); }
}
#endif
#endif
#endif
if (!silent)
{ printf("%-50s", _("Memory protection from ptrace spying:")); }
if (runtime -> ptrace_safe)
{
if (!silent)
{ printf("%s%s%s\n", STAT_GREEN, _("yes"), STAT_OFF); }
level++;
}
else
{
if (!silent)
{ printf("%s%s%s\n", STAT_RED, _("no"), STAT_OFF); }
}
if (!silent)
{
printf("%-50s", _("Validation of environment variables:"));
printf("%s%s%s\n", STAT_GREEN, _("yes"), STAT_OFF);
}
level++;
#if defined(HAVE_CRACKLIB) && defined(CRACKLIB_DICTPATH)
struct stat buf;
if (stat(CRACKLIB_DICTPATH ".pwd", &buf) == -1) {
config->cracklibstatus = CRACKLIB_OFF;
} else {
level++;
}
if (!silent) {
printf("%-50s", _("Cracklib dictionary (" CRACKLIB_DICTPATH"):"));
if (config->cracklibstatus == CRACKLIB_ON) {
printf("%s%s%s\n", STAT_GREEN, _("yes"), STAT_OFF);
} else {
printf("%s%s%s\n", STAT_RED, _("no"), STAT_OFF);
}
}
#else
if (!silent) {
printf("%-50s", _("Cracklib dictionary:"));
printf("%s%s%s\n", STAT_YELLOW, _("not_"), STAT_OFF);
}
level++;
#endif
return level;
}
/* #############################################################################
*
* Description clear the program environment and check all variables
* Author Harry Brueckner
* Date 2005-05-30
* Arguments void
* Return 1 on error, 0 if all is ok
*/
#define PutEnv(eptr, ptr, ctype, name, value) \
if (value) \
{ \
if (checkEnvValue(0, ctype, value)) \
{ \
fprintf(stderr, _("illegal environment variable found (%s%s).\n"), name, value); \
return 1; \
} \
ptr = strNewcat(name, value); \
/* we must use malloc here because we can't free this string again */ \
eptr = malloc(strlen(ptr) + 1); \
if (eptr) \
{ \
/* Flawfinder: ignore */ \
strcpy(eptr, ptr); \
if (putenv(eptr)) \
{ \
fprintf(stderr, "%s (%s, %s, %d)\n", \
_("Unable to set the environment"), \
eptr, \
strerror(errno), \
errno \
); \
memFreeString(__FILE__, __LINE__, ptr); \
return 1; \
} \
} \
else \
{ \
memFreeString(__FILE__, __LINE__, ptr); \
return 1; \
} \
memFreeString(__FILE__, __LINE__, ptr); \
}
int clearEnvironment(void)
{
extern char** environ;
char* columns;
char* gnupghome;
char* gpg_agent_info;
char* home;
char* lang;
char* lc;
char* lcm;
char* lines;
char* no_utf8_acs;
char* term;
char* termcap;
char* eptr;
char* ptr;
TRACE(99, "clearEnvironment()", NULL);
/* Flawfinder: ignore */
columns = getenv("COLUMNS");
/* Flawfinder: ignore */
gnupghome = getenv("GNUPGHOME");
/* Flawfinder: ignore */
gpg_agent_info = getenv("GPG_AGENT_INFO");
/* Flawfinder: ignore */
home = getenv("HOME");
/* Flawfinder: ignore */
lang = getenv("LANG");
/* Flawfinder: ignore */
lc = getenv("LC_ALL");
/* Flawfinder: ignore */
lcm = getenv("LC_MESSAGES");
/* Flawfinder: ignore */
lines = getenv("LINES");
/* Flawfinder: ignore */
no_utf8_acs = getenv("NCURSES_NO_UTF8_ACS");
/* Flawfinder: ignore */
termcap = getenv("TERMCAP");
/* Flawfinder: ignore */
term = getenv("TERM");
/* we clear the environment */
#ifdef HAVE_CLEARENV
if (clearenv())
{
fprintf(stderr, _("clearenv() reported an error.\n"));
return 1;
}
#elif HAVE_UNSETENV
while (environ && environ[0])
{
int i;
ptr = environ[0];
i = 0;
/* count how many chars the variable name has */
while (ptr[i] && ptr[i] != '=')
{ i++; }
if (ptr[i] == '=')
{ /* we really found a variable */
eptr = memAlloc(__FILE__, __LINE__, i + 1);
strStrncpy(eptr, ptr, i + 1);
/* unset the variable */
if (unsetenv(eptr))
{
fprintf(stderr, "%s (%s, %s, %d)\n",
_("Unable to unset the environment"),
eptr,
strerror(errno),
errno
);
}
memFree(__FILE__, __LINE__, eptr, i + 1);
}
}
#endif
/* general */
PutEnv(eptr, ptr, STRTYPE_ALPHANUMERIC, "LANG=", lang);
PutEnv(eptr, ptr, STRTYPE_ALPHANUMERIC, "LC_ALL=", lc);
PutEnv(eptr, ptr, STRTYPE_ALPHANUMERIC, "LC_MESSAGES=", lcm);
PutEnv(eptr, ptr, STRTYPE_FILENAME, "HOME=", home);
/* terminal */
PutEnv(eptr, ptr, STRTYPE_NUMERIC, "COLUMNS=", columns);
PutEnv(eptr, ptr, STRTYPE_NUMERIC, "LINES=", lines);
PutEnv(eptr, ptr, STRTYPE_NUMERIC, "NCURSES_NO_UTF8_ACS=", no_utf8_acs);
PutEnv(eptr, ptr, STRTYPE_ALPHANUMERIC, "TERM=", term);
PutEnv(eptr, ptr, STRTYPE_NOCHECK, "TERMCAP=", termcap);
/* GPG */
PutEnv(eptr, ptr, STRTYPE_FILENAME, "GNUPGHOME=", gnupghome);
PutEnv(eptr, ptr, STRTYPE_FILENAME, "GPG_AGENT_INFO=", gpg_agent_info);
return 0;
}
#undef PutEnv
int check_kernel_version()
{
struct utsname uts;
if(uname(&uts) == -1){
fprintf(stderr, "%s (%s, %d)\n",
_("Failed to discover kernel version."),
strerror(errno), errno);
}else if(!strncmp(uts.sysname, "Linux", 5)){
int maj,min,rel;
if(sscanf(uts.release, "%d.%d.%d", &maj, &min, &rel) != 3) {
// maybe it's a 3.10-rc3 release.
if(sscanf(uts.release, "%d.%d", &maj, &min) != 2) {
fprintf(stderr, "%s (%s, %d)\n",
_("Failed to scan kernel release."),
strerror(errno),errno);
return 0;
}
rel = 9; // instead of passing garbage
}
//fprintf(stdout, "kernel rel: %d.%d\n", maj, min);
if(maj > 2 ||
(maj == 2 && min > 6) ||
(maj == 2 && min == 6 && rel >= 9))
return 1;
}
return 0;
}
/* #############################################################################
*
* Description enable the protection mechanism and drop all privileges
* Author Harry Brueckner
* Date 2005-05-30
* Arguments int* max_mem_lock - the max memory lock flag
* int* memory_safe - the memory status flag
* int* ptrace_safe - the ptrace status flag
* rlim_t* memlock_limit - the current memory limit
* Return 1 on error, 0 if all is ok
*/
int initSecurity(int* max_mem_lock, int* memory_safe, int* ptrace_safe,
rlim_t* memlock_limit)
{
struct rlimit rl;
int canary;
#ifndef NO_MEMLOCK
#ifdef HAVE_MLOCKALL
int result;
#endif
#endif
TRACE(99, "initSecurity()", NULL);
/* we initialize our flags */
*memlock_limit = -2;
*max_mem_lock = 0;
*memory_safe = 0;
*ptrace_safe = 0;
#if defined(_SYS_PTRACE_H) && !defined(TRACE_DEBUG)
/* Try to fork a child which then ptrace attaches to it's parent
* This will safely prevent other processes (even root) to be able to attach to us */
{
pid_t p0, p;
int status;
p0 = getpid();
p = fork();
if (p == -1) {
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
_exit(1);
}
if (p == 0) {
#if defined(HAVE_SYS_PRCTL_H)
// makes the child unattachable
if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) {
fprintf(stderr, "Can not set child non dumpable\n");
_exit(1);
}
#endif
// kFreeBSD wants PT_*
#if !defined(PT_ATTACH)
#define PT_ATTACH PTRACE_ATTACH
#endif
// while SPARC doesn't have these, due to glibc bug
#if !defined(PT_SYSCALL)
#define PT_SYSCALL PTRACE_SYSCALL
#endif
if (ptrace(PT_ATTACH, p0, 0, 0) != 0) {
// someone is already attached to us;
fprintf(stderr, "Warning: Can't attach to parent, ptrace safety *not* obtained!\n");
// shoot the parent in the head
// kill(p0, SIGKILL);
_exit(1);
}
while (1) {
if(ptrace(PT_SYSCALL, p0, 0, 0) == 0)
waitpid(p0, &status, 0);
if(errno == ESRCH && kill(p0, 0) == -1)
exit(0); // parent is dead
}
_exit(0);
}
*ptrace_safe = 1;
}
#endif
/* drop eventual group root privileges; this must be done twice to
* counter "saved IDs" see Secure Programming HowTo
*/
setgid(getgid());
setgid(getgid());
#ifdef HAVE_SYS_FSUID_H
setfsgid(getgid());
setfsgid(getgid());
#endif
/* mlockall() can be used to avoid swapping
* check if rlimits are fungible
* >= 2.6.9: privileged users dont get limited, regular users get limit
* < 2.6.9: users cant mlock, privileged users can lock up to mlock limit
*
* If we trip the limit, mlockall() won't fail, but the program might segfault,
* so this code checks the limit.
*/
int euid = geteuid();
#ifndef NO_MEMLOCK
#ifdef HAVE_MLOCKALL
#ifdef __linux__
int user_can_mlock = check_kernel_version();
if(getrlimit(RLIMIT_MEMLOCK, &rl) == -1){
fprintf(stderr, "getting memlock limits:%s\n",
strerror(errno));
}else{
int orig_limit = rl.rlim_cur;
if(user_can_mlock){
// 2.6.9
int do_setrlim = 0;
if(rl.rlim_cur == -1){
// unlimited
*max_mem_lock = 1;
}else if(!euid){
// root
rl.rlim_max = -1;
rl.rlim_cur = -1;
do_setrlim = 1;
}else if(
rl.rlim_max >= MEMLOCK_LIMIT * 1024){
// we have enough
if(rl.rlim_cur < rl.rlim_max){
rl.rlim_cur = rl.rlim_max;
do_setrlim = 1;
}else{
*max_mem_lock = 1;
}
}
if(do_setrlim){
if(setrlimit(RLIMIT_MEMLOCK, &rl) == -1){
fprintf(stderr, "setting memlock limits:%s\n",
strerror(errno));
rl.rlim_cur = orig_limit;
}else{
*max_mem_lock = 1;
}
}
}else if(!euid){ // <2.6.9, user cant mlock but we are root
// root can mlock and raise up to max, but has to obey
if( rl.rlim_max >= MEMLOCK_LIMIT * 1024){
// we have enough
if(rl.rlim_cur == rl.rlim_max) {
*max_mem_lock = 1;
}else{
// raise to max
rl.rlim_cur = rl.rlim_max;
if(setrlimit(RLIMIT_MEMLOCK, &rl) == -1){
fprintf(stderr, "root setting memlock limits:%s\n",
strerror(errno));
rl.rlim_cur = orig_limit;
}else{
*max_mem_lock = 1;
}
}
}
}
*memlock_limit = rl.rlim_cur;
if(*max_mem_lock){
/* pointcut: limit is ok, do the memlock */
result = mlockall(MCL_CURRENT | MCL_FUTURE);
if(result){
fprintf(stderr, "memlock:%s\n",
strerror(errno));
return 1;
}else{
*memory_safe = 1; // win!
}
}
}
#elif __sun__
if(!euid){
/* not so much root, actually it's more like PRIV_SYS_CONFIG, but
* well cross that bridge when we reach it.
* we dont have any limit, so locking should always work */
*max_mem_lock = 1;
/* lock the memory */
result = mlockall(MCL_CURRENT | MCL_FUTURE);
if (result) {
fprintf(stderr, "%s\n",
_("The process is suid root, but memory paging can't be locked."));
return 1;
} else {
*memory_safe = 1;
}
}
#endif
#endif
#endif
if(!euid){
/* drop root privileges */
setuid(getuid());
setuid(getuid());
#ifdef HAVE_SYS_FSUID_H
setfsuid(getuid());
setfsuid(getuid());
#endif
#ifdef HAVE_SYS_FSUID_H
if (getuid() && (!setuid(0) || !setfsuid(0)))
#else
if (getuid() && !setuid(0))
#endif
{
fprintf(stderr, "%s (%d)\n", _("Cannot drop root privileges."),
__LINE__);
return 1;
}
*ptrace_safe = 1;
}
#ifdef HAVE_SYS_FSUID_H
if (getuid() && (!setgid(0) || !setfsgid(0)))
#else
if (getuid() && !setgid(0))
#endif
{
fprintf(stderr, "%s (%d)\n", _("Cannot drop root privileges."),
__LINE__);
return 1;
}
/* set core dump generation */
rl.rlim_cur = rl.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rl);
/* validate core dump generation */
if (getrlimit(RLIMIT_CORE, &rl) == -1)
{
fprintf(stderr, "%s (%s, %d)\n",
_("Can not read RLIMIT_CORE resources."),
strerror(errno),
errno
);
return 1;
}
if (rl.rlim_cur || rl.rlim_max)
{
fprintf(stderr, "%s\n",
_("Cannot disable core file generation.")
);
return 1;
}
/* to have a default for the filemask, we call umask - even if it's not
* used anywhere
*/
umask(0022);
/* check that stderr, stdin and stdout are opened */
/* NOTE: no file must be opened before this test! */
canary = dup(0);
close(canary);
if (canary < 3)
{
fprintf(stderr, "%s\n", _("stdin, stdout and/or stderr are invalid."));
return 1;
}
return clearEnvironment();
}
/* #############################################################################
*
* Description display the current environment, displayed after cleanup
* Author Harry Brueckner
* Date 2008-09-28
* Arguments void
* Return void
*/
void listEnvironment(void)
{
int i;
extern char** environ;
TRACE(99, "listEnvironment()", NULL);
printf("Environment:\n");
if (!environ)
{
printf("No environment found.\n");
return;
}
for (i = 0; environ[i] && environ[i]; i++)
{ printf("%s\n", environ[i]); }
}
/* #############################################################################
*
* Description run several tests against the pattern matcher
* Author Harry Brueckner
* Date 2005-05-30
* Arguments void
* Return void
*/
#define runTestOk(name, id, type, value) \
result = checkEnvValue(1, type, value); \
fprintf(stderr, "%s #%d: %s (%d)\n", \
name, id, result ? "[1;31merror[0m" : "[1;32mok[0m", result);
#define runTestError(name, id, type, value) \
result = checkEnvValue(1, type, value); \
fprintf(stderr, "%s #%d: %s (%d)\n", \
name, id, result ? "[1;32mok[0m" : "[1;31merror[0m", result);
void testEnvironment(void)
{
int id,
result;
TRACE(99, "testEnvironment()", NULL);
/* a general variable might contain everything except the '=' sign */
id = 0;
runTestOk("general", ++id, STRTYPE_GENERAL, "test this");
runTestError("general", ++id, STRTYPE_GENERAL, "test=this");
/* a variable might containing alphanumeric values */
id = 0;
runTestOk("alpha", ++id, STRTYPE_ALPHANUMERIC, "abcABC123_. ");
runTestOk("alpha", ++id, STRTYPE_ALPHANUMERIC, "abcABC-123_. ");
runTestError("alpha", ++id, STRTYPE_ALPHANUMERIC, "illegal value <>");
/* a variable must be an integer */
id = 0;
runTestOk("numeric", ++id, STRTYPE_NUMERIC, "1234");
runTestError("numeric", ++id, STRTYPE_NUMERIC, "abc123def");
runTestError("numeric", ++id, STRTYPE_NUMERIC, "123def");
runTestError("numeric", ++id, STRTYPE_NUMERIC, "abc123");
runTestError("numeric", ++id, STRTYPE_NUMERIC, "123.123");
runTestError("numeric", ++id, STRTYPE_NUMERIC, "123-123");
/* a variable must be a filename or directory */
id = 0;
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry/12345.jpg");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry/im-age.jpg");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry/image.jpg");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry/IMAGE.jpg");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry/another image.jpg");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/home/harry/../harry/");
runTestOk("filename", ++id, STRTYPE_FILENAME, "/tmp/gpg-abcdef/S.gpg-agent:12345:1");
/* a variable must be a path */
id = 0;
runTestOk("path", ++id, STRTYPE_PATH, "/dir1");
runTestOk("path", ++id, STRTYPE_PATH, "/dir1:/dir2:/dir3");
runTestOk("path", ++id, STRTYPE_PATH, "/dir1:/dumb dir");
runTestError("path", ++id, STRTYPE_PATH, ":/dir1");
runTestError("path", ++id, STRTYPE_PATH, "/dir1:");
runTestError("path", ++id, STRTYPE_PATH, ":/dir1:");
}
/* #############################################################################
*/