1 /* $Id: output.c,v 1.8 2001/05/30 15:47:04 harbourn Exp $
2 * Screen and Log output handeling module for fatback
3 * (c)2000-2001 DoD Computer Forensics Lab
4 * By SrA Nicholas Harbour
5 */
6
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <time.h>
10 #include <unistd.h>
11 #include <limits.h>
12 #include <sys/param.h>
13 #include <sys/utsname.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include "output.h"
17 #include "vars.h"
18
19 static FILE *Orig_ostream;
20 static FILE *ostream;
21 static FILE *Audit_log;
22 static void log_env(char **);
23
24 /*
25 * Initialize audit logging
26 */
audit_init(char * log_name,char * argv[])27 int audit_init(char *log_name, char *argv[])
28 {
29 Orig_ostream = stdout;
30 ostream = stdout;
31 if (!log_name) {
32 log_name = "./fatback.log";
33 printf("No audit log specified, using \"%s\"\n", log_name);
34 }
35 if (!(Audit_log = fopen(log_name, "w"))) {
36 perror("error opening audit log");
37 return -1;
38 }
39 setbuf(Audit_log, NULL); /* Disable stream buffering */
40 log_env(argv);
41 return 0;
42 }
43
44 /*
45 * Display arguments to the screen/audit log
46 */
47 int
display(displaylevel_t level,const char * format,...)48 display(displaylevel_t level, const char *format, ...)
49 {
50 va_list arg_list, arg_copy;
51 fbvar_t *verbose_var;
52 unsigned verbose;
53 int retval;
54
55 /* get the verbosity level from the fatback symbol table */
56 if (!(verbose_var = get_fbvar("verbose"))) {
57 fprintf(stderr, "Error reading variable named `verbose'.\n");
58 return -1;
59 } else {
60 verbose = verbose_var->val.ival;
61 free(verbose_var);
62 }
63
64 /* print the rest of the arguments in standard printf style */
65 va_start(arg_list, format);
66 if ((level < VERBOSE) || (verbose && level == VERBOSE))
67 va_copy(arg_copy, arg_list);
68 retval = vfprintf(Audit_log, format, arg_list);
69 if ((level < VERBOSE) || (verbose && level == VERBOSE))
70 vfprintf(ostream, format, arg_copy);
71 va_end(arg_list);
72 return retval;
73 }
74
75 /*
76 * keep track of and display a |/-\|/-\ sequence
77 */
ticmarker(void)78 void ticmarker(void)
79 {
80 const char *tics = "|/-\\|/-\\";
81 const int numtics = 8;
82 static int currtic;
83
84 printf("\r%c", tics[currtic]);
85 currtic = (currtic + 1) % numtics;
86 }
87
88 /*
89 * Log the current environment
90 */
log_env(char * argv[])91 static void log_env(char *argv[])
92 {
93 time_t start_time;
94 char hostname[MAXHOSTNAMELEN];
95 char cwd[PATH_MAX];
96 #ifdef HAVE_UNAME
97 struct utsname uname_info;
98 #endif /*HAVE_UNAME*/
99 int i;
100 char *version;
101
102 #ifdef VERSION
103 version = VERSION;
104 #else
105 version = "(unknown)";
106 #endif /*VERSION*/
107
108 /* grab some info about the current environment */
109 #ifdef HAVE_GETHOSTNAME
110 gethostname(hostname, MAXHOSTNAMELEN);
111 #endif /*HAVE_GETHOSTNAME*/
112 time(&start_time);
113 display(LOGONLY, "Running Fatback v%s\n", version);
114 display(LOGONLY, "Command Line: ");
115 for (i = 0; argv[i]; i++)
116 display(LOGONLY, "%s ", argv[i]);
117 display(LOGONLY, "\n");
118 display(LOGONLY, "Time: %s", ctime(&start_time));
119 #ifdef HAVE_UNAME
120 uname(&uname_info);
121 display(LOGONLY, "uname: %s %s %s %s %s\n",
122 uname_info.sysname,
123 uname_info.nodename,
124 uname_info.release,
125 uname_info.version,
126 uname_info.machine
127 );
128 #endif /*HAVE_UNAME*/
129 #ifdef HAVE_GETCWD
130 display(LOGONLY, "Working Dir: %s\n", getcwd(cwd, PATH_MAX));
131 #endif /*HAVE_GETCWD*/
132 }
133
134 /*
135 * Close the audit log
136 */
audit_close(void)137 void audit_close(void)
138 {
139 fclose(Audit_log);
140 }
141
set_ostream(FILE * stream)142 void set_ostream(FILE *stream)
143 {
144 ostream = stream;
145 }
146
reset_ostream(void)147 void reset_ostream(void)
148 {
149 ostream = Orig_ostream;
150 }
151