summaryrefslogtreecommitdiffstats
path: root/contrib/bind/port/prand_conf/prand_conf.c
blob: 5f9bf29410d7b2c148c3f8f1fb1714c83cc01ddc (plain)
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
/* $Id: prand_conf.c,v 1.5 1999/07/31 16:44:13 cyarnell Exp $
 *
 * Portions Copyright (c) 1995-1998 by TIS Labs at Network Assoociates Inc.
 * Portions Copyright (c) 1998-1998 by TIS Labs @ Network Associates Inc.
 *
 * Permission to use, copy modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND NETWORK ASSOCIATES
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
 * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
 *
 * program to find where system commands reside 
 * and what directores are avialable for inspection 
 * this information is stored in the file prand_conf.h in current directory
 *
 * function my_find get variable number of arguments
 * the first argument is the name of the command 
 * all remaining arguments are list of directories to search for the command in
 * this function returns the path to the command
 */

#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define LINE_MAX 256

int 
my_find(char *cmd, char **dir)
{
	int curr = 0, c_len, i;
	char cmd_line[LINE_MAX];
	
	memset(cmd_line, 0, sizeof(cmd_line));
	c_len = strlen(cmd);
	for (i = 0; dir[i]; i++) {
		curr = strlen(dir[i]);
		if (curr + c_len < sizeof(cmd_line)-3) {
			sprintf(cmd_line, "%s%s",dir[i], cmd);
			if (access(cmd_line, X_OK) == 0) 
				return (i);
			memset(cmd_line, 0, c_len + curr + 2);
		}
	}
	return (0);
}

/* 
 * function to simulate the ` ` operator in perl  return the number
 * of bytes read from the pipe 
 */
int
pipe_run(char *cmd_line)
{
	FILE *pd;
	char scratch[LINE_MAX];
	int ex, no_bytes = 0, no = 1;

	pd = popen(cmd_line, "r");
	for (; (pd != NULL) && (no > 0); no_bytes += no) 
		no = fread(scratch, sizeof(char), sizeof(scratch), pd);
	ex = pclose(pd);
	return (no_bytes);
}

/*
 * function that executes a command with certain flags and checks that the
 * output is at least certain length
 * First parameter the command 
 * Second parameter is ther flags 
 * third parameter is the number of bytes required 
 * output is 1 if the command works 0 if not 
 * This function writes to the include file if
 */
int 
ex(FILE *fd, char *path, char *cmd, char *arg, int lower_bound)
{
	char line[LINE_MAX];

	if (strlen(path) + strlen(cmd) + strlen(arg) < sizeof(line)-7) { 
		memset(line, 0, sizeof(line));
		sprintf(line, "%s%s %s 2>&1", path, cmd, arg);
		if (pipe_run(line) > lower_bound) {
			fprintf(fd,"\t\"%s\",\n", line);
			return (1);
		}
	}
	return (0);
}

int 
main() 
{
	extern int errno;
	FILE *fd;
	int res, vm, i;
	int ps, arp, net, dig, cmd;
/*
 * set up list of directories where each command may be found in 
 */
	char *arp_path[] = {"/usr/sbin", "/sbin", "/usr/etc/", "/etc/", 
			   "/usr/bin/", NULL};
	char *ps_path[] = {"/usr/bin", "/bin/", NULL};
	char *net_path[]  = {"/usr/ucb/", "/usr/bin/", "/usr/etc/", 
			     "/usr/sbin/", "/bin/", NULL};
	char *dig_path[] = {"/usr/bin/", "/usr/local/bin/", NULL};
	char **df_path  = ps_path;
	char *uptime_path[] = {"/usr/ucb/", "/usr/bin/", "/usr/bsd/", NULL};
	char *iostat_path[] = { "/usr/bin/", "/bin/", "/usr/sbin/", NULL};
	char *vmstat_path[] = {"/usr/ucb/", "/usr/bin/", "/usr/sbin/", NULL};
	char *vm_stat_path[] = {"/usr/ucb/", "/usr/bin/", NULL};
	char **w_path = uptime_path;

/* find which directories exist  */
	char *dirs[] = {"/tmp", "/usr/tmp", "/var/tmp", ".", "/",  
			"/var/spool", "/usr/spool", 
			"/usr/adm", "/var/adm", "/dev", 
			"/usr/mail", "/var/spool/mail", "/var/mail", 
			"/home", "/usr/home", NULL};

	char *files[] = {"/proc/stat", "/proc/rtc", "/proc/meminfo", 
			 "/proc/interrupts",  "/proc/self/status", 
			 "/proc/self/maps", 
			 "/var/log/messages", "/var/log/wtmp", 
			 "/var/log/lastlog", "/var/adm/messages", 
			 "/var/adm/wtmp", "/var/adm/lastlog", NULL};

	struct stat st;
	time_t tim;
/* main program: */

	if ((fd = fopen("prand_conf.h", "w")) == NULL) {
		perror("Failed creating file prand_conf.h");
		exit(errno);
	}

	fprintf(fd, "#ifndef _PRAND_CMD_H_\n#define _PRAND_CMD_H_\n\n");

	fprintf(fd, "const char *cmds[] = {\n");
       
	if ((ps = my_find("ps", ps_path)) >= 0)
		res = ex(fd, ps_path[ps], "ps","-axlw", 460) || 
			ex(fd, ps_path[ps], "ps", "-ef", 300) || 
				ex(fd, ps_path[ps], "ps", "-ale", 300);

	if ((arp = my_find("arp", arp_path)) >= 0) 
	    res = ex(fd, arp_path[arp], "arp", "-n -a", 40);

	if ((net = my_find("netstat", net_path)) >= 0)
		res = ex(fd, net_path[net], "netstat", "-an", 1000);
	if ((cmd = my_find("df", df_path)) >= 0)
		res = ex(fd, df_path[cmd], "df", "", 40);

	if ((dig = my_find("dig", dig_path)) >= 0)
		res = ex(fd, dig_path[dig], "dig", "com. soa +ti=1 +retry=0", 
			 100);
	if ((cmd = my_find("uptime", uptime_path)) >= 0)
	     res = ex(fd, uptime_path[cmd], "uptime", "", 40);
	if ((cmd = my_find("printenv", uptime_path)) >= 0)
	     res = ex(fd, uptime_path[cmd], "printenv", "", 400);
	if (net >= 0)
		res = ex(fd, net_path[net], "netstat", "-s", 1000);

	if (dig >= 0)
		res = ex(fd, net_path[net], "dig", ". soa +ti=1 +retry=0",100);
	if ((cmd = my_find("iostat", iostat_path)) >= 0)
		res = ex(fd, iostat_path[cmd], "iostat", "", 100);

	vm  = 0;
	if ((cmd = my_find("vmstat", vmstat_path)))
		vm = ex(fd, vmstat_path[cmd], "vmstat", "", 200);
	if (vm ==0 && ((cmd = my_find("vm_stat", vm_stat_path)) >= 0))
	    vm = ex(fd, vm_stat_path[cmd], "vm_stat", "", 200);
	if ((cmd = my_find("w", w_path)))
		res = ex(fd, w_path[cmd], "w", "", 100);
	fprintf(fd,"\tNULL\n};\n\n");

	fprintf(fd, "const char *dirs[] = {\n");

	for (i=0; dirs[i]; i++) { 
		if (lstat(dirs[i], &st) == 0) 
			if (S_ISDIR(st.st_mode))
				fprintf(fd,"\t\"%s\",\n", dirs[i]);
	}
	fprintf(fd,"\tNULL\n};\n\n");


	fprintf(fd, "const char *files[] = {\n");
	tim = time(NULL);
	for (i=0; files[i]; i++) {
		if (lstat(files[i],&st) == 0)
			if (S_ISREG(st.st_mode) && 
			    (tim -st.st_mtime) < 84600) 
				fprintf(fd,"\t\"%s\",\n", files[i]);
	}
	fprintf (fd, "\tNULL\n};\n");
		
	if ((stat("/dev/random", &st) == 0))
		if (S_ISCHR(st.st_mode))
			fprintf(fd, "\n#ifndef HAVE_DEV_RANDOM\n%s%s",
				"# define HAVE_DEV_RANDOM 1\n",
				"#endif /* HAVE_DEV_RANDOM */\n\n");

	fprintf(fd, "\n#endif /* _PRAND_CMD_H_ */\n");
	fclose(fd);
	exit (0);
}
OpenPOWER on IntegriCloud