Commit | Line | Data |
---|---|---|
dc0d77d7 CE |
1 | #include <unistd.h> |
2 | #include <sys/types.h> | |
3 | #include <sys/stat.h> | |
4 | #include "strerr.h" | |
5 | #include "buffer.h" | |
6 | #include "open.h" | |
7 | #include "generic-conf.h" | |
8 | ||
9 | static const char *fatal; | |
10 | static const char *dir; | |
11 | static const char *fn; | |
12 | ||
13 | static int fd; | |
14 | static char buf[1024]; | |
15 | static buffer ss; | |
16 | ||
17 | void init(const char *d,const char *f) | |
18 | { | |
19 | dir = d; | |
20 | fatal = f; | |
21 | umask(022); | |
22 | if (mkdir(dir,0700) == -1) | |
23 | strerr_die4sys(111,fatal,"unable to create ",dir,": "); | |
24 | if (chmod(dir,03755) == -1) | |
25 | strerr_die4sys(111,fatal,"unable to set mode of ",dir,": "); | |
26 | if (chdir(dir) == -1) | |
27 | strerr_die4sys(111,fatal,"unable to switch to ",dir,": "); | |
28 | } | |
29 | ||
30 | void fail(void) | |
31 | { | |
32 | strerr_die6sys(111,fatal,"unable to create ",dir,"/",fn,": "); | |
33 | } | |
34 | ||
35 | void makedir(const char *s) | |
36 | { | |
37 | fn = s; | |
38 | if (mkdir(fn,0700) == -1) fail(); | |
39 | } | |
40 | ||
41 | void start(const char *s) | |
42 | { | |
43 | fn = s; | |
44 | fd = open_trunc(fn); | |
45 | if (fd == -1) fail(); | |
46 | buffer_init(&ss,buffer_unixwrite,fd,buf,sizeof buf); | |
47 | } | |
48 | ||
49 | void outs(const char *s) | |
50 | { | |
51 | if (buffer_puts(&ss,s) == -1) fail(); | |
52 | } | |
53 | ||
54 | void out(const char *s,unsigned int len) | |
55 | { | |
56 | if (buffer_put(&ss,s,len) == -1) fail(); | |
57 | } | |
58 | ||
59 | void copyfrom(buffer *b) | |
60 | { | |
61 | if (buffer_copy(&ss,b) < 0) fail(); | |
62 | } | |
63 | ||
64 | void finish(void) | |
65 | { | |
66 | if (buffer_flush(&ss) == -1) fail(); | |
67 | if (fsync(fd) == -1) fail(); | |
68 | close(fd); | |
69 | } | |
70 | ||
71 | void perm(int mode) | |
72 | { | |
73 | if (chmod(fn,mode) == -1) fail(); | |
74 | } | |
75 | ||
76 | void owner(int uid,int gid) | |
77 | { | |
78 | if (chown(fn,uid,gid) == -1) fail(); | |
79 | } | |
80 | ||
81 | void makelog(const char *user,int uid,int gid) | |
82 | { | |
83 | makedir("log"); | |
84 | perm(02755); | |
85 | makedir("log/main"); | |
86 | owner(uid,gid); | |
87 | perm(02755); | |
88 | start("log/status"); | |
89 | finish(); | |
90 | owner(uid,gid); | |
91 | perm(0644); | |
92 | ||
93 | start("log/run"); | |
94 | outs("#!/bin/sh\nexec"); | |
95 | outs(" setuidgid "); outs(user); | |
96 | outs(" multilog t ./main\n"); | |
97 | finish(); | |
98 | perm(0755); | |
99 | } |