forked from balena/djbdns-srv-naptr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic-conf.c
99 lines (85 loc) · 1.71 KB
/
generic-conf.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
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "strerr.h"
#include "buffer.h"
#include "open.h"
#include "generic-conf.h"
static const char *fatal;
static const char *dir;
static const char *fn;
static int fd;
static char buf[1024];
static buffer ss;
void init(const char *d,const char *f)
{
dir = d;
fatal = f;
umask(022);
if (mkdir(dir,0700) == -1)
strerr_die4sys(111,fatal,"unable to create ",dir,": ");
if (chmod(dir,03755) == -1)
strerr_die4sys(111,fatal,"unable to set mode of ",dir,": ");
if (chdir(dir) == -1)
strerr_die4sys(111,fatal,"unable to switch to ",dir,": ");
}
void fail(void)
{
strerr_die6sys(111,fatal,"unable to create ",dir,"/",fn,": ");
}
void makedir(const char *s)
{
fn = s;
if (mkdir(fn,0700) == -1) fail();
}
void start(const char *s)
{
fn = s;
fd = open_trunc(fn);
if (fd == -1) fail();
buffer_init(&ss,buffer_unixwrite,fd,buf,sizeof buf);
}
void outs(const char *s)
{
if (buffer_puts(&ss,s) == -1) fail();
}
void out(const char *s,unsigned int len)
{
if (buffer_put(&ss,s,len) == -1) fail();
}
void copyfrom(buffer *b)
{
if (buffer_copy(&ss,b) < 0) fail();
}
void finish(void)
{
if (buffer_flush(&ss) == -1) fail();
if (fsync(fd) == -1) fail();
close(fd);
}
void perm(int mode)
{
if (chmod(fn,mode) == -1) fail();
}
void owner(int uid,int gid)
{
if (chown(fn,uid,gid) == -1) fail();
}
void makelog(const char *user,int uid,int gid)
{
makedir("log");
perm(02755);
makedir("log/main");
owner(uid,gid);
perm(02755);
start("log/status");
finish();
owner(uid,gid);
perm(0644);
start("log/run");
outs("#!/bin/sh\nexec");
outs(" setuidgid "); outs(user);
outs(" multilog t ./main\n");
finish();
perm(0755);
}