Subversion Repositories public

Rev

Rev 53 | Rev 57 | 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
 
55 luk 65
  ss << GetSafePath(m_path) << " " << m << " " << m_cmd;
45 luk 66
  return ss.str();
67
}
68
 
69
bool InCronTabEntry::Parse(const std::string& rStr, InCronTabEntry& rEntry)
70
{
71
  unsigned long u;
55 luk 72
  std::string s1, s2, s3;
45 luk 73
 
55 luk 74
  StringTokenizer tok(rStr, ' ', '\\');
75
  if (!tok.HasMoreTokens())
45 luk 76
    return false;
55 luk 77
 
78
  s1 = tok.GetNextToken(true);
79
  if (!tok.HasMoreTokens())
80
    return false;
81
 
82
  s2 = tok.GetNextToken(true);
83
  if (!tok.HasMoreTokens())
84
    return false;
45 luk 85
 
55 luk 86
  tok.SetNoPrefix();
87
  s3 = tok.GetRemainder();
88
  SIZE len = s3.length();
89
  if (len > 0 && s3[len-1] == '\n')
90
    s3.resize(len-1);
91
 
45 luk 92
  rEntry.m_path = s1;
93
  rEntry.m_cmd = s3;
53 luk 94
  rEntry.m_uMask = 0;
95
  rEntry.m_fNoLoop = false;
45 luk 96
 
55 luk 97
  if (sscanf(s2.c_str(), "%lu", &u) == 1) {
45 luk 98
    rEntry.m_uMask = (uint32_t) u;
99
  }
100
  else {
101
    StringTokenizer tok(s2);
102
    while (tok.HasMoreTokens()) {
47 luk 103
      std::string s(tok.GetNextToken());
104
      if (s == "IN_NO_LOOP")
105
        rEntry.m_fNoLoop = true;
106
      else
107
        rEntry.m_uMask |= InotifyEvent::GetMaskByName(s);
45 luk 108
    }
109
  }
110
 
111
  return true;
112
}
113
 
55 luk 114
std::string InCronTabEntry::GetSafePath(const std::string& rPath)
115
{
116
  std::ostringstream stream;
117
 
118
  SIZE len = rPath.length();
119
  for (SIZE i = 0; i < len; i++) {
120
    if (rPath[i] == ' ') {
121
      stream << "\\ ";
122
    }
123
    else if (rPath[i] == '\\') {
124
      stream << "\\\\";
125
    }
126
    else {
127
      stream << rPath[i];
128
    }
129
  }
130
 
131
  return stream.str();
132
}
133
 
45 luk 134
bool InCronTab::Load(const std::string& rPath)
135
{
136
  m_tab.clear();
137
 
138
  FILE* f = fopen(rPath.c_str(), "r");
139
  if (f == NULL)
140
    return false;
141
 
142
  char s[1000];
143
  InCronTabEntry e;
144
  while (fgets(s, 1000, f) != NULL) {
145
    if (InCronTabEntry::Parse(s, e)) {
146
      m_tab.push_back(e);
147
    }
148
  }
149
 
150
  fclose(f);
151
 
152
  return true;
153
}
154
 
155
bool InCronTab::Save(const std::string& rPath)
156
{
157
  FILE* f = fopen(rPath.c_str(), "w");
158
  if (f == NULL)
159
    return false;
160
 
161
  std::deque<InCronTabEntry>::iterator it = m_tab.begin();
162
  while (it != m_tab.end()) {
163
    fputs((*it).ToString().c_str(), f);
164
    fputs("\n", f);
165
    it++;
166
  }
167
 
168
  fclose(f);
169
 
170
  return true;
171
}
172
 
173
bool InCronTab::CheckUser(const std::string& rUser)
174
{
175
  char s[100], u[100];
176
 
177
  FILE* f = fopen(INCRON_ALLOW_PATH, "r");
178
  if (f == NULL) {
179
    if (errno == ENOENT) {
180
      f = fopen(INCRON_DENY_PATH, "r");
181
      if (f == NULL) {
182
        return errno == ENOENT;
183
      }
184
      while (fgets(s, 100, f) != NULL) {
185
        if (sscanf(s, "%s", u) == 1) {
186
          if (rUser == u) {
187
            fclose(f);
188
            return false;
189
          }
190
        }
191
      }
192
      fclose(f);
193
      return true;
194
    }
195
 
196
    return false;
197
  }
198
 
199
  while (fgets(s, 100, f) != NULL) {
200
    if (sscanf(s, "%s", u) == 1) {
201
      if (rUser == u) {
202
        fclose(f);
203
        return true;
204
      }
205
    }
206
  }
207
 
208
  fclose(f);
209
  return false;
210
}
211
 
212
std::string InCronTab::GetUserTablePath(const std::string& rUser)
213
{
214
  std::string s(INCRON_TABLE_BASE);
215
  s.append(rUser);
216
  return s;
217
}
218
 
55 luk 219
 
220