File: file-perms.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (318 lines) | stat: -rw-r--r-- 7,695 bytes parent folder | download | duplicates (3)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (c) 2012 Balabit
 * Copyright (c) 2012 Balazs Scheidler <bazsi@balabit.hu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "file-perms.h"
#include "userdb.h"
#include "messages.h"
#include "cfg.h"
#include "gprocess.h"

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

#define DONTCHANGE -2

void
file_perm_options_set_file_uid(FilePermOptions *self, const gchar *file_uid)
{
  self->file_uid = 0;
  if (!resolve_user(file_uid, &self->file_uid))
    {
      msg_error("Error resolving user",
                evt_tag_str("user", file_uid));
    }
}

void
file_perm_options_dont_change_file_uid(FilePermOptions *self)
{
  self->file_uid = DONTCHANGE;
}

void
file_perm_options_set_file_gid(FilePermOptions *self, const gchar *file_gid)
{
  self->file_gid = 0;
  if (!resolve_group(file_gid, &self->file_gid))
    {
      msg_error("Error resolving group",
                evt_tag_str("group", file_gid));
    }
}

void
file_perm_options_dont_change_file_gid(FilePermOptions *self)
{
  self->file_gid = DONTCHANGE;
}

void
file_perm_options_set_file_perm(FilePermOptions *self, gint file_perm)
{
  self->file_perm = file_perm;
}

void
file_perm_options_dont_change_file_perm(FilePermOptions *self)
{
  self->file_perm = DONTCHANGE;
}

void
file_perm_options_set_dir_uid(FilePermOptions *self, const gchar *dir_uid)
{
  self->dir_uid = 0;
  if (!resolve_user(dir_uid, &self->dir_uid))
    {
      msg_error("Error resolving user",
                evt_tag_str("user", dir_uid));
    }
}

void
file_perm_options_dont_change_dir_uid(FilePermOptions *self)
{
  self->dir_uid = DONTCHANGE;
}

void
file_perm_options_set_dir_gid(FilePermOptions *self, const gchar *dir_gid)
{
  self->dir_gid = 0;
  if (!resolve_group(dir_gid, &self->dir_gid))
    {
      msg_error("Error resolving group",
                evt_tag_str("group", dir_gid));
    }
}

void
file_perm_options_dont_change_dir_gid(FilePermOptions *self)
{
  self->dir_gid = DONTCHANGE;
}

void
file_perm_options_set_dir_perm(FilePermOptions *self, gint dir_perm)
{
  self->dir_perm = dir_perm;
}

void
file_perm_options_dont_change_dir_perm(FilePermOptions *self)
{
  self->dir_perm = DONTCHANGE;
}

void
file_perm_options_defaults(FilePermOptions *self)
{
  self->file_uid = self->file_gid = -1;
  self->file_perm = -1;
  self->dir_uid = self->dir_gid = -1;
  self->dir_perm = -1;
}

void
file_perm_options_global_defaults(FilePermOptions *self)
{
  self->file_uid = self->file_gid = -1;
  self->file_perm = 0600;
  self->dir_uid = self->dir_gid = -1;
  self->dir_perm = 0700;
}

void
file_perm_options_inherit_from(FilePermOptions *self, const FilePermOptions *from)
{
  if (self->file_uid == -1)
    self->file_uid = from->file_uid;
  if (self->file_gid == -1)
    self->file_gid = from->file_gid;
  if (self->file_perm == -1)
    self->file_perm = from->file_perm;
  if (self->dir_uid == -1)
    self->dir_uid = from->dir_uid;
  if (self->dir_gid == -1)
    self->dir_gid = from->dir_gid;
  if (self->dir_perm == -1)
    self->dir_perm = from->dir_perm;
}

void
file_perm_options_inherit_dont_change(FilePermOptions *self)
{
  FilePermOptions dont_change =
  {
    .file_uid = DONTCHANGE,
    .file_gid = DONTCHANGE,
    .file_perm = DONTCHANGE,
    .dir_uid = DONTCHANGE,
    .dir_gid = DONTCHANGE,
    .dir_perm = DONTCHANGE,
  };

  file_perm_options_inherit_from(self, &dont_change);
}

gboolean
file_perm_options_apply_file(const FilePermOptions *self, const gchar *path)
{
#ifndef _MSC_VER
  gboolean result = TRUE;

  if (self->file_uid >= 0 && chown(path, (uid_t) self->file_uid, -1) < 0)
    result = FALSE;
  if (self->file_gid >= 0 && chown(path, -1, (gid_t) self->file_gid) < 0)
    result = FALSE;
  if (self->file_perm >= 0 && chmod(path, (mode_t) self->file_perm) < 0)
    result = FALSE;
  return result;
#endif
}

gboolean
file_perm_options_apply_symlink(const FilePermOptions *self, const gchar *path)
{
#ifndef _MSC_VER
  gboolean result = TRUE;

  if (self->file_uid >= 0 && lchown(path, (uid_t) self->file_uid, -1) < 0)
    result = FALSE;
  if (self->file_gid >= 0 && lchown(path, -1, (gid_t) self->file_gid) < 0)
    result = FALSE;
  return result;
#endif
}

gboolean
file_perm_options_apply_dir(const FilePermOptions *self, const gchar *path)
{
#ifndef _MSC_VER
  gboolean result = TRUE;

  if (self->dir_uid >= 0 && chown(path, (uid_t) self->dir_uid, -1) < 0)
    result = FALSE;
  if (self->dir_gid >= 0 && chown(path, -1, (gid_t) self->dir_gid) < 0)
    result = FALSE;
  if (self->dir_perm >= 0 && chmod(path, (mode_t) self->dir_perm) < 0)
    result = FALSE;
  return result;
#endif
}

gboolean
file_perm_options_apply_fd(const FilePermOptions *self, gint fd)
{
#ifndef _MSC_VER
  gboolean result = TRUE;

  if (self->file_uid >= 0 && fchown(fd, (uid_t) self->file_uid, -1) < 0)
    result = FALSE;
  if (self->file_gid >= 0 && fchown(fd, -1, (gid_t) self->file_gid) < 0)
    result = FALSE;
  if (self->file_perm >= 0 && fchmod(fd, (mode_t) self->file_perm) < 0)
    result = FALSE;

  return result;
#endif
}

/**
 *
 * This function receives a complete path (directory + filename) and creates
 * the directory portion if it does not exist. The point is that the caller
 * wants to ensure that the given filename can be opened after this function
 * returns. (at least it won't fail because of missing directories).
 **/
gboolean
file_perm_options_create_containing_directory(const FilePermOptions *self, const gchar *path)
{
  gboolean result = FALSE;
  gchar *_path;
  gchar *dirname;
  struct stat st;
  gint rc;
  gchar *p;
  cap_t saved_caps;

  _path = g_strdup(path);

  /* check that the directory exists */
  dirname = g_path_get_dirname(_path);
  rc = stat(dirname, &st);
  g_free(dirname);

  if (rc == 0)
    {
      /* directory already exists */
      result = TRUE;
      goto finish;
    }
  else if (rc < 0 && errno != ENOENT)
    {
      /* some real error occurred */
      result = FALSE;
      goto finish;
    }

  /* directory does not exist */

  p = strchr(_path + 1, '/');
  while (p)
    {
      *p = 0;
      if (stat(_path, &st) == 0)
        {
          if (!S_ISDIR(st.st_mode))
            {
              result = FALSE;
              goto finish;
            }
        }
      else if (errno == ENOENT)
        {
          if (mkdir(_path, self->dir_perm < 0 ? 0700 : (mode_t) self->dir_perm) == -1)
            {
              result = FALSE;
              goto finish;
            }
          saved_caps = g_process_cap_save();
          g_process_enable_cap("cap_chown");
          g_process_enable_cap("cap_fowner");
          file_perm_options_apply_dir(self, _path);
          g_process_cap_restore(saved_caps);
        }
      *p = '/';
      p = strchr(p + 1, '/');
    }

  result = TRUE;

finish:
  g_free(_path);
  return result;
}