forked from ar-/incron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incrontab.cpp
285 lines (229 loc) · 6.17 KB
/
incrontab.cpp
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
/// inotify cron table manipulator classes implementation
/**
* \file incrontab.cpp
*
* inotify cron system
*
* Copyright (C) 2006, 2007, 2008, 2012 Lukas Jelinek, <[email protected]>
* Copyright (C) 2014, 2015 Andreas Altair Redmer, <[email protected]>
*
* This program is free software; you can use it, redistribute
* it and/or modify it under the terms of the GNU General Public
* License, version 2 (see LICENSE-GPL).
*
* Credits:
* Christian Ruppert (new include to build with GCC 4.4+)
*
*/
#include <sstream>
#include <cstdio>
#include <errno.h>
//#include <syslog.h> // TODO remove
#include "inotify-cxx.h"
#include "incrontab.h"
#include "incroncfg.h"
#define IN_NO_LOOP_OLD "IN_NO_LOOP" // depricated from original version: no loop is now default
#define CT_LOOPABLE "loopable=true" // no loop is default. loopable must be set
#define CT_NORECURSION "recursive=false" // recursive is default, no recusion must be set
#define CT_DOTDIRS "dotdirs=true" // exclude dotdirs is default, include dotdirs must be set
/*
* ALLOW/DENY SEMANTICS
*
* If /etc/incron.allow exists ONLY users contained here
* are allowed to use incron.
*
* Otherwise, if /etc/incron.deny exists only user NOT
* contained here are allowed to use incron.
*
* Otherwise all users may use incron.
*
*/
IncronTabEntry::IncronTabEntry()
: m_uMask(0),
m_fNoLoop(true),
m_fNoRecursion(false),
m_fDotDirs(false)
{
}
IncronTabEntry::IncronTabEntry(const std::string& rPath, uint32_t uMask, const std::string& rCmd)
: m_path(rPath),
m_uMask(uMask),
m_cmd(rCmd)
{
}
std::string IncronTabEntry::ToString() const
{
std::ostringstream ss;
std::string m;
InotifyEvent::DumpTypes(m_uMask, m);
// don't write IN_NO_LOOP anymore
// if (m.empty())
// m = m_fNoLoop ? IN_NO_LOOP_OLD : m;
// else
// if (m_fNoLoop) m.append(std::string(","+IN_NO_LOOP_OLD);
// add CT_NORECURSION artificially
if (m.empty())
m = m_fNoRecursion ? CT_NORECURSION : m;
else
if (m_fNoRecursion) m.append(std::string(",")+CT_NORECURSION);
// add CT_LOOPABLE artificially
if (m.empty())
m = !m_fNoLoop ? CT_LOOPABLE : m;
else
if (!m_fNoLoop) m.append(std::string(",")+CT_LOOPABLE);
// add CT_DOTDIRS artificially
if (m.empty())
m = m_fDotDirs ? CT_DOTDIRS : m;
else
if (m_fDotDirs) m.append(std::string(",")+CT_DOTDIRS);
// fill a default value for broken lines
if (m.empty())
m = "IN_ALL_EVENTS";
ss << GetSafePath(m_path) << "\t" << m << "\t" << m_cmd;
return ss.str();
}
bool IncronTabEntry::Parse(const std::string& rStr, IncronTabEntry& rEntry)
{
unsigned long u;
std::string s1, s2, s3;
StringTokenizer tok(rStr, " \t", '\\');
if (!tok.HasMoreTokens())
return false;
s1 = tok.GetNextToken(true);
if (!tok.HasMoreTokens())
return false;
s2 = tok.GetNextToken(true);
if (!tok.HasMoreTokens())
return false;
tok.SetNoPrefix();
s3 = tok.GetRemainder();
SIZE len = s3.length();
if (len > 0 && s3[len-1] == '\n')
s3.resize(len-1);
rEntry.m_path = s1;
rEntry.m_cmd = s3;
rEntry.m_uMask = 0;
rEntry.m_fNoLoop = true;
rEntry.m_fNoRecursion = false;
rEntry.m_fDotDirs = false;
if (sscanf(s2.c_str(), "%lu", &u) == 1) {
rEntry.m_uMask = (uint32_t) u;
}
else {
StringTokenizer tok2(s2);
while (tok2.HasMoreTokens()) {
std::string s(tok2.GetNextToken());
if (s == IN_NO_LOOP_OLD)
rEntry.m_fNoLoop = true;
else if (s == CT_LOOPABLE)
rEntry.m_fNoLoop = false;
else if (s == CT_NORECURSION)
rEntry.m_fNoRecursion = true;
else if (s == CT_DOTDIRS)
rEntry.m_fDotDirs = true;
else
rEntry.m_uMask |= InotifyEvent::GetMaskByName(s);
}
}
return true;
}
std::string IncronTabEntry::GetSafePath(const std::string& rPath)
{
std::ostringstream stream;
SIZE len = rPath.length();
for (SIZE i = 0; i < len; i++) {
if (rPath[i] == ' ') {
stream << "\\ ";
}
else if (rPath[i] == '\\') {
stream << "\\\\";
}
else {
stream << rPath[i];
}
}
return stream.str();
}
bool IncronTab::Load(const std::string& rPath)
{
m_tab.clear();
FILE* f = fopen(rPath.c_str(), "r");
if (f == NULL)
return false;
char s[1000];
IncronTabEntry e;
while (fgets(s, 1000, f) != NULL) {
if (IncronTabEntry::Parse(s, e)) {
m_tab.push_back(e);
}
}
fclose(f);
return true;
}
bool IncronTab::Save(const std::string& rPath)
{
FILE* f = fopen(rPath.c_str(), "w");
if (f == NULL)
return false;
std::deque<IncronTabEntry>::iterator it = m_tab.begin();
while (it != m_tab.end()) {
fputs((*it).ToString().c_str(), f);
fputs("\n", f);
it++;
}
fclose(f);
return true;
}
bool IncronTab::CheckUser(const std::string& rUser)
{
char s[100], u[100];
std::string path;
if (!IncronCfg::GetValue("allowed_users", path))
throw InotifyException("configuration is corrupted", EINVAL);
FILE* f = fopen(path.c_str(), "r");
if (f == NULL) {
if (errno == ENOENT) {
if (!IncronCfg::GetValue("denied_users", path))
throw InotifyException("configuration is corrupted", EINVAL);
f = fopen(path.c_str(), "r");
if (f == NULL) {
return errno == ENOENT;
}
while (fgets(s, 100, f) != NULL) {
if (sscanf(s, "%s", u) == 1) {
if (rUser == u) {
fclose(f);
return false;
}
}
}
fclose(f);
return true;
}
return false;
}
while (fgets(s, 100, f) != NULL) {
if (sscanf(s, "%s", u) == 1) {
if (rUser == u) {
fclose(f);
return true;
}
}
}
fclose(f);
return false;
}
std::string IncronTab::GetUserTablePath(const std::string& rUser)
{
std::string s;
if (!IncronCfg::GetValue("user_table_dir", s))
throw InotifyException("configuration is corrupted", EINVAL);
return IncronCfg::BuildPath(s, rUser);
}
std::string IncronTab::GetSystemTablePath(const std::string& rName)
{
std::string s;
if (!IncronCfg::GetValue("system_table_dir", s))
throw InotifyException("configuration is corrupted", EINVAL);
return IncronCfg::BuildPath(s, rName);
}