Subversion Repositories public

Rev

Rev 69 | 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
 *
100 luk 8
 * Copyright (C) 2006, 2007, 2008 Lukas Jelinek, <lukas@aiken.cz>
45 luk 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"
69 luk 24
#include "incroncfg.h"
45 luk 25
 
69 luk 26
/*
47 luk 27
/// Allowed users
28
#define INCRON_ALLOW_PATH "/etc/incron.allow"
45 luk 29
 
47 luk 30
/// Denied users
45 luk 31
#define INCRON_DENY_PATH "/etc/incron.deny"
69 luk 32
*/
45 luk 33
 
57 luk 34
/*
35
 * ALLOW/DENY SEMANTICS
36
 *
37
 * If /etc/incron.allow exists ONLY users contained here
38
 * are allowed to use incron.
39
 *
40
 * Otherwise, if /etc/incron.deny exists only user NOT
41
 * contained here are allowed to use incron.
42
 *
43
 * Otherwise all users may use incron.
44
 *
45
 */
45 luk 46
 
47
 
48
 
69 luk 49
IncronTabEntry::IncronTabEntry()
47 luk 50
: m_uMask(0),
51
  m_fNoLoop(false)
45 luk 52
{
53
 
54
}
55
 
69 luk 56
IncronTabEntry::IncronTabEntry(const std::string& rPath, uint32_t uMask, const std::string& rCmd)
45 luk 57
: m_path(rPath),
58
  m_uMask(uMask),
59
  m_cmd(rCmd)
60
{
61
 
62
}
63
 
69 luk 64
std::string IncronTabEntry::ToString() const
45 luk 65
{
66
  std::ostringstream ss;
67
 
68
  std::string m;
69
 
70
  InotifyEvent::DumpTypes(m_uMask, m);
47 luk 71
  if (m.empty()) {
72
    m = m_fNoLoop ? "IN_NO_LOOP" : "0";
73
  }
74
  else {
75
    if (m_fNoLoop)
76
      m.append(",IN_NO_LOOP");
77
  }
45 luk 78
 
55 luk 79
  ss << GetSafePath(m_path) << " " << m << " " << m_cmd;
45 luk 80
  return ss.str();
81
}
82
 
69 luk 83
bool IncronTabEntry::Parse(const std::string& rStr, IncronTabEntry& rEntry)
45 luk 84
{
85
  unsigned long u;
55 luk 86
  std::string s1, s2, s3;
45 luk 87
 
55 luk 88
  StringTokenizer tok(rStr, ' ', '\\');
89
  if (!tok.HasMoreTokens())
45 luk 90
    return false;
55 luk 91
 
92
  s1 = tok.GetNextToken(true);
93
  if (!tok.HasMoreTokens())
94
    return false;
95
 
96
  s2 = tok.GetNextToken(true);
97
  if (!tok.HasMoreTokens())
98
    return false;
45 luk 99
 
55 luk 100
  tok.SetNoPrefix();
101
  s3 = tok.GetRemainder();
102
  SIZE len = s3.length();
103
  if (len > 0 && s3[len-1] == '\n')
104
    s3.resize(len-1);
105
 
45 luk 106
  rEntry.m_path = s1;
107
  rEntry.m_cmd = s3;
53 luk 108
  rEntry.m_uMask = 0;
109
  rEntry.m_fNoLoop = false;
45 luk 110
 
55 luk 111
  if (sscanf(s2.c_str(), "%lu", &u) == 1) {
45 luk 112
    rEntry.m_uMask = (uint32_t) u;
113
  }
114
  else {
115
    StringTokenizer tok(s2);
116
    while (tok.HasMoreTokens()) {
47 luk 117
      std::string s(tok.GetNextToken());
118
      if (s == "IN_NO_LOOP")
119
        rEntry.m_fNoLoop = true;
120
      else
121
        rEntry.m_uMask |= InotifyEvent::GetMaskByName(s);
45 luk 122
    }
123
  }
124
 
125
  return true;
126
}
127
 
69 luk 128
std::string IncronTabEntry::GetSafePath(const std::string& rPath)
55 luk 129
{
130
  std::ostringstream stream;
131
 
132
  SIZE len = rPath.length();
133
  for (SIZE i = 0; i < len; i++) {
134
    if (rPath[i] == ' ') {
135
      stream << "\\ ";
136
    }
137
    else if (rPath[i] == '\\') {
138
      stream << "\\\\";
139
    }
140
    else {
141
      stream << rPath[i];
142
    }
143
  }
144
 
145
  return stream.str();
146
}
147
 
69 luk 148
bool IncronTab::Load(const std::string& rPath)
45 luk 149
{
150
  m_tab.clear();
151
 
152
  FILE* f = fopen(rPath.c_str(), "r");
153
  if (f == NULL)
154
    return false;
155
 
156
  char s[1000];
69 luk 157
  IncronTabEntry e;
45 luk 158
  while (fgets(s, 1000, f) != NULL) {
69 luk 159
    if (IncronTabEntry::Parse(s, e)) {
45 luk 160
      m_tab.push_back(e);
161
    }
162
  }
163
 
164
  fclose(f);
165
 
166
  return true;
167
}
168
 
69 luk 169
bool IncronTab::Save(const std::string& rPath)
45 luk 170
{
171
  FILE* f = fopen(rPath.c_str(), "w");
172
  if (f == NULL)
173
    return false;
174
 
69 luk 175
  std::deque<IncronTabEntry>::iterator it = m_tab.begin();
45 luk 176
  while (it != m_tab.end()) {
177
    fputs((*it).ToString().c_str(), f);
178
    fputs("\n", f);
179
    it++;
180
  }
181
 
182
  fclose(f);
183
 
184
  return true;
185
}
186
 
69 luk 187
bool IncronTab::CheckUser(const std::string& rUser)
45 luk 188
{
189
  char s[100], u[100];
190
 
69 luk 191
  std::string path;
192
  if (!IncronCfg::GetValue("allowed_users", path))
193
    throw InotifyException("configuration is corrupted", EINVAL);
194
 
195
  FILE* f = fopen(path.c_str(), "r");
45 luk 196
  if (f == NULL) {
197
    if (errno == ENOENT) {
69 luk 198
      if (!IncronCfg::GetValue("denied_users", path))
199
        throw InotifyException("configuration is corrupted", EINVAL);
200
 
201
      f = fopen(path.c_str(), "r");
45 luk 202
      if (f == NULL) {
203
        return errno == ENOENT;
204
      }
205
      while (fgets(s, 100, f) != NULL) {
206
        if (sscanf(s, "%s", u) == 1) {
207
          if (rUser == u) {
208
            fclose(f);
209
            return false;
210
          }
211
        }
212
      }
213
      fclose(f);
214
      return true;
215
    }
216
 
217
    return false;
218
  }
219
 
220
  while (fgets(s, 100, f) != NULL) {
221
    if (sscanf(s, "%s", u) == 1) {
222
      if (rUser == u) {
223
        fclose(f);
224
        return true;
225
      }
226
    }
227
  }
228
 
229
  fclose(f);
230
  return false;
231
}
232
 
69 luk 233
std::string IncronTab::GetUserTablePath(const std::string& rUser)
45 luk 234
{
69 luk 235
  std::string s;
236
  if (!IncronCfg::GetValue("user_table_dir", s))
237
    throw InotifyException("configuration is corrupted", EINVAL);
238
 
239
  return IncronCfg::BuildPath(s, rUser);
45 luk 240
}
241
 
69 luk 242
std::string IncronTab::GetSystemTablePath(const std::string& rName)
67 luk 243
{
69 luk 244
  std::string s;
245
  if (!IncronCfg::GetValue("system_table_dir", s))
246
    throw InotifyException("configuration is corrupted", EINVAL);
247
 
248
  return IncronCfg::BuildPath(s, rName);
67 luk 249
}
55 luk 250
 
251
 
67 luk 252