Subversion Repositories public

Rev

Rev 61 | Rev 65 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 61 Rev 63
Line 3... Line 3...
3
/**
3
/**
4
 * \file ict-main.cpp
4
 * \file ict-main.cpp
5
 *
5
 *
6
 * inotify cron system
6
 * inotify cron system
7
 *
7
 *
8
 * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz>
8
 * Copyright (C) 2006, 2007 Lukas Jelinek, <lukas@aiken.cz>
9
 *
9
 *
10
 * This program is free software; you can use it, redistribute
10
 * This program is free software; you can use it, redistribute
11
 * it and/or modify it under the terms of the GNU General Public
11
 * it and/or modify it under the terms of the GNU General Public
12
 * License, version 2 (see LICENSE-GPL).
12
 * License, version 2 (see LICENSE-GPL).
13
 *  
13
 *  
Line 31... Line 31...
31
31
32
32
33
const char* argp_program_version = INCRON_TAB_NAME " " INCRON_VERSION;
33
const char* argp_program_version = INCRON_TAB_NAME " " INCRON_VERSION;
34
const char* argp_program_bug_address = INCRON_BUG_ADDRESS;
34
const char* argp_program_bug_address = INCRON_BUG_ADDRESS;
35
35
36
static char doc[] = "incrontab - incron table manipulator";
36
static char doc[] = "incrontab - incron table manipulator\n(c) Lukas Jelinek, 2006, 2007";
37
37
38
static char args_doc[] = "FILE";
38
static char args_doc[] = "FILE";
39
39
40
static struct argp_option options[] = {
40
static struct argp_option options[] = {
41
  {"list",    'l', 0,      0,  "List the current table" },
41
  {"list",    'l', 0,      0,  "List the current table" },
Line 201... Line 201...
201
  strcpy(s, "/tmp/incron.table-XXXXXX");
201
  strcpy(s, "/tmp/incron.table-XXXXXX");
202
 
202
 
203
  uid_t iu = geteuid();
203
  uid_t iu = geteuid();
204
  uid_t ig = getegid();
204
  uid_t ig = getegid();
205
205
206
  if (seteuid(uid) != 0 || setegid(gid) != 0) {
206
  if (setegid(gid) != 0 || seteuid(uid) != 0) {
207
    fprintf(stderr, "cannot change effective UID/GID for user %s: %s\n", user, strerror(errno));
207
    fprintf(stderr, "cannot change effective UID/GID for user %s: %s\n", user, strerror(errno));
208
    return false;
208
    return false;
209
  }
209
  }
210
 
210
 
211
  int fd = mkstemp(s);
211
  int fd = mkstemp(s);
Line 218... Line 218...
218
  FILE* out = NULL;
218
  FILE* out = NULL;
219
  FILE* in = NULL;
219
  FILE* in = NULL;
220
  time_t mt = (time_t) 0;
220
  time_t mt = (time_t) 0;
221
  const char* e = NULL;
221
  const char* e = NULL;
222
 
222
 
223
  if (fchmod(fd, 0644) != 0) {
-
 
224
    fprintf(stderr, "cannot change mode of temporary file: %s\n", strerror(errno));
-
 
225
    close(fd);
-
 
226
    goto end;
-
 
227
  }
-
 
228
 
-
 
229
  if (seteuid(iu) != 0 || setegid(ig) != 0) {
223
  if (setegid(ig) != 0 || seteuid(iu) != 0) {
230
    fprintf(stderr, "cannot change effective UID/GID: %s\n", strerror(errno));
224
    fprintf(stderr, "cannot change effective UID/GID: %s\n", strerror(errno));
231
    close(fd);
225
    close(fd);
232
    goto end;
226
    goto end;
233
  }
227
  }
234
   
228
   
Line 276... Line 270...
276
    e = INCRON_DEFAULT_EDITOR;
270
    e = INCRON_DEFAULT_EDITOR;
277
 
271
 
278
  {
272
  {
279
    pid_t pid = fork();
273
    pid_t pid = fork();
280
    if (pid == 0) {
274
    if (pid == 0) {
281
      if (setuid(uid) != 0 || setgid(gid) != 0) {
275
      if (setgid(gid) != 0 || setuid(uid) != 0) {
282
        fprintf(stderr, "cannot set user %s: %s\n", user, strerror(errno));
276
        fprintf(stderr, "cannot set user %s: %s\n", user, strerror(errno));
283
        goto end;
277
        goto end;
284
      }    
278
      }    
285
     
279
     
286
      execlp(e, e, s, NULL);
280
      execlp(e, e, s, NULL);
Line 314... Line 308...
314
    goto end;
308
    goto end;
315
  }
309
  }
316
 
310
 
317
  {
311
  {
318
    InCronTab ict;
312
    InCronTab ict;
319
    if (!ict.Load(s) || !ict.Save(tp)) {
313
    if (ict.Load(s) && ict.Save(tp)) {
-
 
314
      if (chmod(tp.c_str(), S_IRUSR | S_IWUSR) != 0) {
-
 
315
        fprintf(stderr, "cannot change mode of temporary file: %s\n", strerror(errno));
-
 
316
      }
-
 
317
    }
-
 
318
    else {
320
      fprintf(stderr, "cannot move temporary table: %s\n", strerror(errno));
319
      fprintf(stderr, "cannot move temporary table: %s\n", strerror(errno));
321
      goto end;
320
      goto end;
322
    }
321
    }
-
 
322
   
323
  }
323
  }
324
 
324
 
325
  ok = true;
325
  ok = true;
326
  fprintf(stderr, "table updated\n");
326
  fprintf(stderr, "table updated\n");
327
 
327