aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/utils.c
blob: 8ca366031ed952d6ec704bfca64dcb0e5e2aeb29 (plain) (blame)
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
#include <sys/types.h>

#if !defined(__OpenBSD__)
#define __USE_GNU
#define _GNU_SOURCE
#endif

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <err.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>

#include "libglouglou.h"

FILE *_logfile;
static int _loglevel;

static void logit(int, const char *, const char *, va_list);

/*
 * Log
 * handles only one log file per process
 */

int
gg_log_init(char *filename, int level)
{
	_logfile = fopen(filename, "a+");
	if (!_logfile) {
		printf("cannot open log file %s!\n", filename);
		return -1;
	}
	_loglevel = level;
	return 0;
}

void
gg_log_shutdown(void)
{
  fclose(_logfile);
}

void
gg_log_tmp(const char *msg, ...)
{
  va_list ap;

  va_start(ap, msg);
  logit(GGLOG_FORCED, "XXX ", msg, ap);
  va_end(ap);
}

void
gg_log_debug(const char *msg, ...)
{
  va_list ap;

  va_start(ap, msg);
  logit(GGLOG_DEBUG, "", msg, ap);
  va_end(ap);
}

void
gg_log_info(const char *msg, ...)
{
  va_list ap;

  va_start(ap, msg);
  logit(GGLOG_INFO, "", msg, ap);
  va_end(ap);
}

void
gg_log_warn(const char *msg, ...)
{
  va_list ap;

  va_start(ap, msg);
  logit(GGLOG_WARN, "", msg, ap);
  va_end(ap);
}

#if defined(__OpenBSD__)
void __dead
#else
void
#endif
gg_log_fatal(const char *msg, ...)
{
  va_list ap;

  va_start(ap, msg);
  logit(GGLOG_FATAL, "fatal: ", msg, ap);
  va_end(ap);

  exit(1);
}

/* XXX mpsafe */
static void
logit(int level, const char *prefix, const char *msg, va_list ap)
{
  time_t clock;

  if (level <= _loglevel) {
    time(&clock);
    fprintf(_logfile, "%d ", (int)clock);
    vfprintf(_logfile, prefix, ap);
    vfprintf(_logfile, msg, ap);
    fprintf(_logfile, "\n");
    fflush(_logfile);
  }
}

/*
 * Various utils
 */

void *
xmalloc(size_t size)
{
  void *data;

  data = malloc(size);
  if (!data)
    err(1, "could not malloc %d", (int)size);
  return data;
}

void *
xcalloc(size_t nmemb, size_t size)
{
  void *data;

  data = calloc(nmemb, size);
  if (!data)
    err(1, "could not calloc %d", (int)size);
  return data;
}

void
fd_nonblock(int fd)
{
	int flags = fcntl(fd, F_GETFL, 0);
	int rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
	if (rc == -1)
		err(1, "failed to set fd %i non-blocking", fd);
}

void
addrcpy(struct sockaddr_in *dst, struct sockaddr_in *src)
{
  dst->sin_addr.s_addr = src->sin_addr.s_addr;
  dst->sin_port = src->sin_port;
  dst->sin_family = src->sin_family;
}

int
addrcmp(struct sockaddr_in *a, struct sockaddr_in *b)
{
  if (a->sin_addr.s_addr != b->sin_addr.s_addr)
    return -1;
  if (a->sin_port != b->sin_port)
    return -2;
  if (a->sin_family != b->sin_family)
    return -3;
  return 0;
}

void
droppriv(char *user, int do_chroot, char *chroot_path)
{
	struct passwd	*pw;

	pw = getpwnam(user);
	if (!pw)
		err(1, "unknown user %s", user);
	if (do_chroot) {
    if (!chroot_path)
      chroot_path = pw->pw_dir;
    if (chroot(chroot_path) != 0)
      err(1, "unable to chroot");
  }
	if (chdir("/") != 0)
		err(1, "unable to chdir");
	if (setgroups(1, &pw->pw_gid) == -1)
		err(1, "setgroups() failed");
	if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
		err(1, "setresgid failed");
	if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
		err(1, "setresuid() failed");
	endpwent();
}