about summary refs log tree commit diff
path: root/src/supervision/s6-svperms.c
blob: 178ea08a9eec7a8af55319714c6bb45e0a90ba0b (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* ISC license. */

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>

#include <skalibs/types.h>
#include <skalibs/sgetopt.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>

#include <s6/s6-supervise.h>

#define USAGE "s6-svperms [ -v ] [ -u | -g group | -G group | -o | -O group ] [ -e | -E group ] servicedir..."
#define dieusage() strerr_dieusage(100, USAGE)

static gid_t scangid (char const *s)
{
  if (s[0] == ':')
  {
    gid_t g ;
    if (!gid0_scan(s+1, &g)) dieusage() ;
    return g ;
  }
  else
  {
    struct group *gr ;
    errno = 0 ;
    gr = getgrnam(s) ;
    if (!gr)
    {
      if (errno) strerr_diefu1sys(111, "getgrnam") ;
      else strerr_diefu3x(100, "find entry for ", s, " in group database") ;
    }
    return gr->gr_gid ;
  }
}

static char *gidname (gid_t gid)
{
  struct group *gr ;
  errno = 0 ;
  gr = getgrgid(gid) ;
  if (!gr)
  {
    static char fmt[GID_FMT] ;
    fmt[gid_fmt(fmt, gid)] = 0 ;
    if (errno) strerr_warnwu2sys("getgrgid ", fmt) ;
    return fmt ;
  }
  return gr->gr_name ;
}

static void out (char const *s)
{
  if (buffer_puts(buffer_1, s) < 0)
    strerr_diefu1sys(111, "write to stdout") ;
}

static inline int printsupervise (char const *dir)
{
  struct stat st ;
  size_t len = strlen(dir) ;
  char fn[len + sizeof(S6_SUPERVISE_CTLDIR) + 9] ;
  memcpy(fn, dir, len) ;
  memcpy(fn + len, "/" S6_SUPERVISE_CTLDIR, sizeof(S6_SUPERVISE_CTLDIR) + 1) ;
  if (stat(fn, &st) < 0)
  {
    strerr_warnwu2sys("stat ", fn) ;
    return 1 ;
  }
  if (!S_ISDIR(st.st_mode))
  {
    strerr_warnw2x(fn, " is not a directory") ;
    return 1 ;
  }
  if (st.st_mode & 05066 || (st.st_mode & 0700) != 0700 || ((st.st_mode & 0001) && !(st.st_mode & 0010)))
  {
    char fmt[UINT_OFMT] ;
    fmt[uint_ofmt(fmt, st.st_mode & 07777)] = 0 ;
    strerr_warnw3x(fn, " has incorrect permissions: ", fmt) ;
    return 1 ;
  }
  out(dir) ;
  out(" status: ") ;
  if (st.st_mode & 0011)
  {
    if (st.st_mode & 0001) buffer_puts(buffer_1, "public") ;
    else
    {
      out("group ") ;
      out(gidname(st.st_gid)) ;
    }
  }
  else out("owner") ;
  out("\n") ;
  memcpy(fn + len + sizeof(S6_SUPERVISE_CTLDIR), "/control", 9) ;
  if (stat(fn, &st) < 0)
  {
    strerr_warnwu2sys("stat ", fn) ;
    return 1 ;
  }
  if (!S_ISFIFO(st.st_mode))
  {
    strerr_warnw2x(fn, " is not a named pipe") ;
    return 1 ;
  }
  if (st.st_mode & 0157)
  {
    char fmt[UINT_OFMT] ;
    fmt[uint_ofmt(fmt, st.st_mode & 07777)] = 0 ;
    strerr_warnw3x(fn, " has incorrect permissions: ", fmt) ;
    return 1 ;
  }
  out(dir) ;
  out(" control: ") ;
  if (st.st_mode & 0020)
  {
    out("group ") ;
    out(gidname(st.st_gid)) ;
  }
  else out("owner") ;
  out("\n") ;
  return 0 ;
}

static inline int printevent (char const *dir)
{
  struct stat st ;
  size_t len = strlen(dir) ;
  char fn[len + sizeof(S6_SUPERVISE_EVENTDIR) + 1] ;
  memcpy(fn, dir, len) ;
  memcpy(fn + len, "/" S6_SUPERVISE_EVENTDIR, sizeof(S6_SUPERVISE_EVENTDIR) + 1) ;
  if (stat(fn, &st) < 0)
  {
    strerr_warnwu2sys("stat ", fn) ;
    return 1 ;
  }
  if (!S_ISDIR(st.st_mode))
  {
    strerr_warnw2x(fn, " is not a directory") ;
    return 1 ;
  }
  if ((st.st_mode & 07777) != 01733 && (st.st_mode & 07777) != 03730)
  {
    char fmt[UINT_OFMT] ;
    fmt[uint_ofmt(fmt, st.st_mode & 07777)] = 0 ;
    strerr_warnw3x(fn, " has incorrect permissions: ", fmt) ;
    return 1 ;
  }
  out(dir) ;
  out(" events: ") ;
  if ((st.st_mode & 07777) == 03730)
  {
    out("group ") ;
    out(gidname(st.st_gid)) ;
  }
  else out("public") ;
  out("\n") ;
  return 0 ;
}

static gid_t primarygid (char const *fn)
{
  struct passwd *pw ;
  struct stat st ;
  if (stat(fn, &st) < 0) strerr_diefu2sys(111, "stat ", fn) ;
  errno = 0 ;
  pw = getpwuid(st.st_uid) ;
  if (!pw)
  {
    strerr_warnwu3sys("determine primary gid for the owner of ", fn, " (using root instead)") ;
    return 0 ;
  }
  else return pw->pw_gid ;
}

static inline void modsupervise (char const *dir, unsigned int what, gid_t gid)
{
  size_t len = strlen(dir) ;
  gid_t cgid  = 0 ;
  mode_t mode = 0700 ;
  char fn[len + sizeof(S6_SUPERVISE_CTLDIR) + 9] ;
  memcpy(fn, dir, len) ;
  memcpy(fn + len, "/" S6_SUPERVISE_CTLDIR, sizeof(S6_SUPERVISE_CTLDIR) + 1) ;
  switch (what & 3)
  {
    case 0 : cgid = primarygid(fn) ; mode = 0700 ; break ;
    case 1 : cgid = gid ; mode = 0710 ; break ;
    case 2 : cgid = primarygid(fn) ; mode = 0711 ; break ;
  }
  if (chown(fn, -1, cgid) < 0)
    strerr_diefu2sys(111, "chown ", fn) ;
  if (chmod(fn, mode) < 0)
    strerr_diefu2sys(111, "chmod ", fn) ;
  memcpy(fn + len + sizeof(S6_SUPERVISE_CTLDIR), "/control", 9) ;
  if (what & 4) mode = 0620 ;
  else
  {
    gid = primarygid(fn) ;
    mode = 0600 ;
  }
  if (chown(fn, -1, gid) < 0)
    strerr_diefu2sys(111, "chown ", fn) ;
  if (chmod(fn, mode) < 0)
    strerr_diefu2sys(111, "chmod ", fn) ;
}

static inline void modevent (char const *dir, gid_t gid)
{
  size_t len = strlen(dir) ;
  mode_t mode ;
  char fn[len + sizeof(S6_SUPERVISE_EVENTDIR) + 1] ;
  memcpy(fn, dir, len) ;
  memcpy(fn + len, "/" S6_SUPERVISE_EVENTDIR, sizeof(S6_SUPERVISE_EVENTDIR) + 1) ;
  if (gid == (gid_t)-1)
  {
    gid = primarygid(fn) ;
    mode = 01733 ;
  }
  else mode = 03730 ;
  if (chown(fn, -1, gid) < 0)
    strerr_diefu2sys(111, "chown ", fn) ;
  if (chmod(fn, mode) < 0)
    strerr_diefu2sys(111, "chmod ", fn) ;
}

int main (int argc, char const *const *argv)
{
  int e = 0 ;
  gid_t gid = -1 ;
  gid_t eventgid = -1 ;
  int rw = 0 ;
  unsigned int what = 0 ;
  PROG = "s6-svperms" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "vug:G:oO:eE:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'v' : rw |= 1 ; break ;
        case 'u' : rw |= 2 ; what = 0 ; break ;
        case 'g' : rw |= 2 ; what = 1 ; gid = scangid(l.arg) ; break ;
        case 'G' : rw |= 2 ; what = 5 ; gid = scangid(l.arg) ; break ;
        case 'o' : rw |= 2 ; what = 2 ; break ;
        case 'O' : rw |= 2 ; what = 6 ; gid = scangid(l.arg) ; break ;
        case 'e' : rw |= 4 ; eventgid = -1 ; break ;
        case 'E' : rw |= 4 ; eventgid = scangid(l.arg) ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;

  if (!rw) rw = 1 ;
  for (; *argv ; argv++)
  {
    if (rw & 2) modsupervise(*argv, what, gid) ;
    if (rw & 4) modevent(*argv, eventgid) ;
    if (rw & 1) { e |= printsupervise(*argv) ; e |= printevent(*argv) ; }
  }
  if (rw & 1 && !buffer_flush(buffer_1))
    strerr_diefu1sys(111, "write to stdout") ;
  return e ;
}