[go: up one dir, main page]

File: misc.c

package info (click to toggle)
fakebo 0.4.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 512 kB
  • ctags: 413
  • sloc: ansic: 4,013; sh: 398; makefile: 65
file content (185 lines) | stat: -rw-r--r-- 4,365 bytes parent folder | download
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

#include "global.h"
#include "misc.h"

/*
 * Help the user.
 *
 */
char usage[]
= "Usage: %s [options]\n"
"\t-c\tspecify the config file. By default, FakeBO will search for its\n"
"\t\tconfig file in various places; see below the help\n"
"\t-d\tprint debugging information.\n"
"\t-i\tdebug BO packet numbers.\n"
"\t-b\tstart FakeBO as a daemon.\n"
"\t-v\tverbose mode.\n"
"\t-a\tprint info about program.\n"
"\t-h\tprint this help message.\n"
"\t-V\tprint version and exit.\n"
"\n";

/*  
   * the GNU licence
 */
char msg_about[]
= "This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
"\n";

volatile int stop = FALSE;	/* for signal handling */
FILE *fptolog;

void removeunprintable(char *s, int n)
{
	int i;

	for (i = 0; i < strlen(s); i++) {
		if (isprint((int) s[i]))
			break;
		if (isspace((int) s[i]))
			break;
		if (s[i] != 0)
			break;
		s[i] = '.';
	}
}

/*
 * log contents from fmt to log file
 *
 */
char repeatbuf[MAXLOGRECSIZE];	/* buffer to compare */
int repeatcount = 0;		/* Counter for 'message repeated x times' */
void logprintf(BOOL dotime, char *fmt,...)
{
	va_list va;
	char buf[MAXLOGRECSIZE], buf2[MAXLOGRECSIZE];

	va_start(va, fmt);
	vsprintf(buf, fmt, va);
	va_end(va);

	if (strcmp(buf, repeatbuf) == 0) {
		repeatcount++;
		return;
	}
	removeunprintable(buf, sizeof(buf));

	if (repeatcount > 0) {
		if (logtimeanddate > 0) {
			whatistime(buf2);
			fprintf(fptolog, "%s Last message repeated %d time(s)\n", buf2, repeatcount);
		} else
			fprintf(fptolog, "Last message repeated %d time(s)\n", repeatcount);
		repeatcount = 0;
		memset(repeatbuf, 0, sizeof(repeatbuf));
	}
	memcpy(repeatbuf, buf, sizeof(buf));

	if (dotime && logtimeanddate > 0) {
		whatistime(buf2);
		fprintf(fptolog, "%s %s", buf2, buf);
	} else
		fprintf(fptolog, "%s", buf);
}

void syslogprintf(char *fmt,...)
{
	va_list va;
	char buf[MAXLOGRECSIZE];

	if (logtosyslog != 0) {
		va_start(va, fmt);
		vsprintf(buf, fmt, va);
		va_end(va);

#ifdef HAVE_OPENLOG
		openlog("FakeBO", LOG_CONS, LOG_USER);
		syslog(LOG_WARNING, buf);
		closelog();
#else
#warning "Can't find usable syslog, disabled!"
#endif

	}
}

/*
 * Returns the current local date and time in timenow. 
 * Here you can modify the output format
 * (default is yyyy-mm-dd hh:mm:ss)
 *   
 */
void whatistime(char *timenow)
{
	struct tm *curtime;
	time_t cartime;

	time(&cartime);
	curtime = localtime(&cartime);
	switch (logtimeanddate) {
	case 1:
		sprintf(timenow, "%02d-%02d-%04d %02d:%02d:%02d", curtime->tm_mon + 1
			,curtime->tm_mday, curtime->tm_year + 1900, curtime->tm_hour
			,curtime->tm_min, curtime->tm_sec);
		break;
	case 2:
		sprintf(timenow, "%02d.%02d.%04d %02d:%02d:%02d", curtime->tm_mday
			,curtime->tm_mon + 1, curtime->tm_year + 1900, curtime->tm_hour
			,curtime->tm_min, curtime->tm_sec);
		break;
	case 3:
		sprintf(timenow, "%04d-%02d-%02d %02d:%02d:%02d", curtime->tm_year + 1900
		 ,curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_hour
			,curtime->tm_min, curtime->tm_sec);
		break;
	default:
		sprintf(timenow, "Config error (`logtimeanddate' != [0-3])!");
		break;
	}

}

/*
 * If we are running as root (EUID = 0),
 * drop root privileges and become USER.
 */
void dropprivileges(const char *user)
{
#ifdef HAVE_PWD_H
	struct passwd *entry;
	static int done = 0;

	/* Nothing to do if we are not root */
	if (done || geteuid() != 0) {
		done = 1;
		return;
	}
	/* Become user */
	entry = getpwnam(user);
	if (entry == NULL) {
		fprintf(stderr, "Warning: user %s not found, "
			"running as root!!\n", user);
		return;
	}
	if (setgid(entry->pw_gid) == -1)
		perror("setgid()");
	if (setuid(entry->pw_uid) == -1)
		perror("setuid()");
	done = 1;
#else
#warning Ignoring drop priviledges!
#endif
}