Subversion Repositories public

Rev

Rev 45 | Rev 53 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
45 luk 1
 
2
/// inotify cron table manipulator classes implementation
3
/**
4
 * \file incrontab.cpp
5
 *
6
 * inotify cron system
7
 *
8
 * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz>
9
 *
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
12
 * License, version 2 (see LICENSE-GPL).
13
 *  
14
 */
15
 
16
 
17
#include <sstream>
18
#include <stdio.h>
19
#include <errno.h>
20
 
21
#include "inotify-cxx.h"
22
 
23
#include "incrontab.h"
24
 
25
 
47 luk 26
/// Allowed users
27
#define INCRON_ALLOW_PATH "/etc/incron.allow"
45 luk 28
 
47 luk 29
/// Denied users
45 luk 30
#define INCRON_DENY_PATH "/etc/incron.deny"
31
 
32
 
33
 
34
 
35
InCronTabEntry::InCronTabEntry()
47 luk 36
: m_uMask(0),
37
  m_fNoLoop(false)
45 luk 38
{
39
 
40
}
41
 
42
InCronTabEntry::InCronTabEntry(const std::string& rPath, uint32_t uMask, const std::string& rCmd)
43
: m_path(rPath),
44
  m_uMask(uMask),
45
  m_cmd(rCmd)
46
{
47
 
48
}
49
 
50
std::string InCronTabEntry::ToString() const
51
{
52
  std::ostringstream ss;
53
 
54
  std::string m;
55
 
56
  InotifyEvent::DumpTypes(m_uMask, m);
47 luk 57
  if (m.empty()) {
58
    m = m_fNoLoop ? "IN_NO_LOOP" : "0";
59
  }
60
  else {
61
    if (m_fNoLoop)
62
      m.append(",IN_NO_LOOP");
63
  }
45 luk 64
 
65
  ss << m_path << " " << m << " " << m_cmd;
66
  return ss.str();
67
}
68
 
69
bool InCronTabEntry::Parse(const std::string& rStr, InCronTabEntry& rEntry)
70
{
71
  char s1[1000], s2[1000], s3[1000];
72
  unsigned long u;
73
 
74
  if (sscanf(rStr.c_str(), "%s %s %[^\n]", s1, s2, s3) != 3)
75
    return false;
76
 
77
  rEntry.m_path = s1;
78
  rEntry.m_cmd = s3;
79
  rEntry.m_uMask = 0;  
80
 
81
  if (sscanf(s2, "%lu", &u) == 1) {
82
    rEntry.m_uMask = (uint32_t) u;
83
  }
84
  else {
85
    StringTokenizer tok(s2);
86
    while (tok.HasMoreTokens()) {
47 luk 87
      std::string s(tok.GetNextToken());
88
      if (s == "IN_NO_LOOP")
89
        rEntry.m_fNoLoop = true;
90
      else
91
        rEntry.m_uMask |= InotifyEvent::GetMaskByName(s);
45 luk 92
    }
93
  }
94
 
95
  return true;
96
}
97
 
98
bool InCronTab::Load(const std::string& rPath)
99
{
100
  m_tab.clear();
101
 
102
  FILE* f = fopen(rPath.c_str(), "r");
103
  if (f == NULL)
104
    return false;
105
 
106
  char s[1000];
107
  InCronTabEntry e;
108
  while (fgets(s, 1000, f) != NULL) {
109
    if (InCronTabEntry::Parse(s, e)) {
110
      m_tab.push_back(e);
111
    }
112
  }
113
 
114
  fclose(f);
115
 
116
  return true;
117
}
118
 
119
bool InCronTab::Save(const std::string& rPath)
120
{
121
  FILE* f = fopen(rPath.c_str(), "w");
122
  if (f == NULL)
123
    return false;
124
 
125
  std::deque<InCronTabEntry>::iterator it = m_tab.begin();
126
  while (it != m_tab.end()) {
127
    fputs((*it).ToString().c_str(), f);
128
    fputs("\n", f);
129
    it++;
130
  }
131
 
132
  fclose(f);
133
 
134
  return true;
135
}
136
 
137
bool InCronTab::CheckUser(const std::string& rUser)
138
{
139
  char s[100], u[100];
140
 
141
  FILE* f = fopen(INCRON_ALLOW_PATH, "r");
142
  if (f == NULL) {
143
    if (errno == ENOENT) {
144
      f = fopen(INCRON_DENY_PATH, "r");
145
      if (f == NULL) {
146
        return errno == ENOENT;
147
      }
148
      while (fgets(s, 100, f) != NULL) {
149
        if (sscanf(s, "%s", u) == 1) {
150
          if (rUser == u) {
151
            fclose(f);
152
            return false;
153
          }
154
        }
155
      }
156
      fclose(f);
157
      return true;
158
    }
159
 
160
    return false;
161
  }
162
 
163
  while (fgets(s, 100, f) != NULL) {
164
    if (sscanf(s, "%s", u) == 1) {
165
      if (rUser == u) {
166
        fclose(f);
167
        return true;
168
      }
169
    }
170
  }
171
 
172
  fclose(f);
173
  return false;
174
}
175
 
176
std::string InCronTab::GetUserTablePath(const std::string& rUser)
177
{
178
  std::string s(INCRON_TABLE_BASE);
179
  s.append(rUser);
180
  return s;
181
}
182