Rev 67 | 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 daemon user tables implementation |
||
3 | /** |
||
4 | * \file usertable.cpp |
||
5 | * |
||
6 | * inotify cron system |
||
7 | * |
||
63 | luk | 8 | * Copyright (C) 2006, 2007 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 <pwd.h> |
||
18 | #include <syslog.h> |
||
19 | #include <errno.h> |
||
47 | luk | 20 | #include <sys/wait.h> |
65 | luk | 21 | #include <unistd.h> |
22 | #include <grp.h> |
||
23 | #include <sys/stat.h> |
||
45 | luk | 24 | |
25 | #include "usertable.h" |
||
69 | luk | 26 | #include "incroncfg.h" |
45 | luk | 27 | |
65 | luk | 28 | #ifdef IN_DONT_FOLLOW |
67 | luk | 29 | #define DONT_FOLLOW(mask) InotifyEvent::IsType(mask, IN_DONT_FOLLOW) |
65 | luk | 30 | #else // IN_DONT_FOLLOW |
67 | luk | 31 | #define DONT_FOLLOW(mask) (false) |
65 | luk | 32 | #endif // IN_DONT_FOLLOW |
45 | luk | 33 | |
65 | luk | 34 | |
69 | luk | 35 | PROC_MAP UserTable::s_procMap; |
47 | luk | 36 | |
67 | luk | 37 | extern volatile bool g_fFinish; |
38 | extern SUT_MAP g_ut; |
||
47 | luk | 39 | |
67 | luk | 40 | |
47 | luk | 41 | void on_proc_done(InotifyWatch* pW) |
42 | { |
||
43 | pW->SetEnabled(true); |
||
44 | } |
||
45 | |||
46 | |||
67 | luk | 47 | EventDispatcher::EventDispatcher(int iPipeFd, Inotify* pIn, InotifyWatch* pSys, InotifyWatch* pUser) |
45 | luk | 48 | { |
67 | luk | 49 | m_iPipeFd = iPipeFd; |
50 | m_iMgmtFd = pIn->GetDescriptor(); |
||
51 | m_pIn = pIn; |
||
52 | m_pSys = pSys; |
||
53 | m_pUser = pUser; |
||
54 | m_size = 0; |
||
55 | m_pPoll = NULL; |
||
45 | luk | 56 | } |
57 | |||
67 | luk | 58 | EventDispatcher::~EventDispatcher() |
45 | luk | 59 | { |
67 | luk | 60 | if (m_pPoll != NULL) |
61 | delete[] m_pPoll; |
||
62 | } |
||
63 | |||
64 | bool EventDispatcher::ProcessEvents() |
||
65 | { |
||
66 | // consume pipe events if any (and report back) |
||
67 | bool pipe = (m_pPoll[0].revents & POLLIN); |
||
68 | if (pipe) { |
||
69 | char c; |
||
70 | while (read(m_pPoll[0].fd, &c, 1) > 0) {} |
||
71 | m_pPoll[0].revents = 0; |
||
72 | } |
||
73 | |||
74 | // process table management events if any |
||
75 | if (m_pPoll[1].revents & POLLIN) { |
||
76 | ProcessMgmtEvents(); |
||
77 | m_pPoll[1].revents = 0; |
||
78 | } |
||
45 | luk | 79 | |
67 | luk | 80 | InotifyEvent evt; |
45 | luk | 81 | |
67 | luk | 82 | for (size_t i=2; i<m_size; i++) { |
45 | luk | 83 | |
67 | luk | 84 | // process events if occurred |
85 | if (m_pPoll[i].revents & POLLIN) { |
||
86 | FDUT_MAP::iterator it = m_maps.find(m_pPoll[i].fd); |
||
87 | if (it != m_maps.end()) { |
||
88 | Inotify* pIn = ((*it).second)->GetInotify(); |
||
89 | pIn->WaitForEvents(true); |
||
90 | |||
91 | // process events for this object |
||
92 | while (pIn->GetEvent(evt)) { |
||
93 | ((*it).second)->OnEvent(evt); |
||
94 | } |
||
95 | } |
||
96 | m_pPoll[i].revents = 0; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return pipe; |
||
45 | luk | 101 | } |
102 | |||
67 | luk | 103 | void EventDispatcher::Register(UserTable* pTab) |
45 | luk | 104 | { |
67 | luk | 105 | if (pTab != NULL) { |
106 | Inotify* pIn = pTab->GetInotify(); |
||
107 | if (pIn != NULL) { |
||
108 | int fd = pIn->GetDescriptor(); |
||
109 | if (fd != -1) { |
||
110 | m_maps.insert(FDUT_MAP::value_type(fd, pTab)); |
||
111 | Rebuild(); |
||
112 | } |
||
113 | } |
||
114 | } |
||
45 | luk | 115 | } |
116 | |||
67 | luk | 117 | void EventDispatcher::Unregister(UserTable* pTab) |
45 | luk | 118 | { |
67 | luk | 119 | FDUT_MAP::iterator it = m_maps.find(pTab->GetInotify()->GetDescriptor()); |
120 | if (it != m_maps.end()) { |
||
55 | luk | 121 | m_maps.erase(it); |
67 | luk | 122 | Rebuild(); |
123 | } |
||
45 | luk | 124 | } |
67 | luk | 125 | |
126 | void EventDispatcher::Rebuild() |
||
127 | { |
||
128 | // delete old data if exists |
||
129 | if (m_pPoll != NULL) |
||
130 | delete[] m_pPoll; |
||
131 | |||
132 | // allocate memory |
||
133 | m_size = m_maps.size() + 2; |
||
134 | m_pPoll = new struct pollfd[m_size]; |
||
45 | luk | 135 | |
67 | luk | 136 | // add pipe descriptor |
137 | m_pPoll[0].fd = m_iPipeFd; |
||
138 | m_pPoll[0].events = POLLIN; |
||
139 | m_pPoll[0].revents = 0; |
||
140 | |||
141 | // add table management descriptor |
||
142 | m_pPoll[1].fd = m_iMgmtFd; |
||
143 | m_pPoll[1].events = POLLIN; |
||
144 | m_pPoll[1].revents = 0; |
||
145 | |||
146 | // add all inotify descriptors |
||
147 | FDUT_MAP::iterator it = m_maps.begin(); |
||
148 | for (size_t i=2; i<m_size; i++, it++) { |
||
149 | m_pPoll[i].fd = (*it).first; |
||
150 | m_pPoll[i].events = POLLIN; |
||
151 | m_pPoll[i].revents = 0; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | void EventDispatcher::ProcessMgmtEvents() |
||
45 | luk | 156 | { |
67 | luk | 157 | m_pIn->WaitForEvents(true); |
158 | |||
159 | InotifyEvent e; |
||
160 | |||
161 | while (m_pIn->GetEvent(e)) { |
||
162 | if (e.GetWatch() == m_pSys) { |
||
163 | if (e.IsType(IN_DELETE_SELF) || e.IsType(IN_UNMOUNT)) { |
||
164 | syslog(LOG_CRIT, "base directory destroyed, exitting"); |
||
165 | g_fFinish = true; |
||
166 | } |
||
167 | else if (!e.GetName().empty()) { |
||
69 | luk | 168 | SUT_MAP::iterator it = g_ut.find(IncronCfg::BuildPath(m_pSys->GetPath(), e.GetName())); |
67 | luk | 169 | if (it != g_ut.end()) { |
170 | UserTable* pUt = (*it).second; |
||
171 | if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) { |
||
172 | syslog(LOG_INFO, "system table %s changed, reloading", e.GetName().c_str()); |
||
173 | pUt->Dispose(); |
||
174 | pUt->Load(); |
||
175 | } |
||
176 | else if (e.IsType(IN_MOVED_FROM) || e.IsType(IN_DELETE)) { |
||
177 | syslog(LOG_INFO, "system table %s destroyed, removing", e.GetName().c_str()); |
||
178 | delete pUt; |
||
179 | g_ut.erase(it); |
||
180 | } |
||
181 | } |
||
182 | else if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) { |
||
183 | syslog(LOG_INFO, "system table %s created, loading", e.GetName().c_str()); |
||
184 | UserTable* pUt = new UserTable(this, e.GetName(), true); |
||
69 | luk | 185 | g_ut.insert(SUT_MAP::value_type(IncronTab::GetSystemTablePath(e.GetName()), pUt)); |
67 | luk | 186 | pUt->Load(); |
187 | } |
||
188 | } |
||
45 | luk | 189 | } |
67 | luk | 190 | else if (e.GetWatch() == m_pUser) { |
191 | if (e.IsType(IN_DELETE_SELF) || e.IsType(IN_UNMOUNT)) { |
||
192 | syslog(LOG_CRIT, "base directory destroyed, exitting"); |
||
193 | g_fFinish = true; |
||
194 | } |
||
195 | else if (!e.GetName().empty()) { |
||
69 | luk | 196 | SUT_MAP::iterator it = g_ut.find(IncronCfg::BuildPath(m_pUser->GetPath(), e.GetName())); |
67 | luk | 197 | if (it != g_ut.end()) { |
198 | UserTable* pUt = (*it).second; |
||
199 | if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) { |
||
200 | syslog(LOG_INFO, "table for user %s changed, reloading", e.GetName().c_str()); |
||
201 | pUt->Dispose(); |
||
202 | pUt->Load(); |
||
203 | } |
||
204 | else if (e.IsType(IN_MOVED_FROM) || e.IsType(IN_DELETE)) { |
||
205 | syslog(LOG_INFO, "table for user %s destroyed, removing", e.GetName().c_str()); |
||
206 | delete pUt; |
||
207 | g_ut.erase(it); |
||
208 | } |
||
209 | } |
||
210 | else if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) { |
||
211 | if (UserTable::CheckUser(e.GetName().c_str())) { |
||
212 | syslog(LOG_INFO, "table for user %s created, loading", e.GetName().c_str()); |
||
213 | UserTable* pUt = new UserTable(this, e.GetName(), false); |
||
69 | luk | 214 | g_ut.insert(SUT_MAP::value_type(IncronTab::GetUserTablePath(e.GetName()), pUt)); |
67 | luk | 215 | pUt->Load(); |
216 | } |
||
217 | } |
||
218 | } |
||
45 | luk | 219 | } |
220 | } |
||
221 | } |
||
222 | |||
223 | |||
224 | |||
225 | |||
67 | luk | 226 | UserTable::UserTable(EventDispatcher* pEd, const std::string& rUser, bool fSysTable) |
227 | : m_user(rUser), |
||
228 | m_fSysTable(fSysTable) |
||
45 | luk | 229 | { |
230 | m_pEd = pEd; |
||
67 | luk | 231 | |
232 | m_in.SetNonBlock(true); |
||
233 | m_in.SetCloseOnExec(true); |
||
45 | luk | 234 | } |
235 | |||
236 | UserTable::~UserTable() |
||
237 | { |
||
238 | Dispose(); |
||
239 | } |
||
240 | |||
241 | void UserTable::Load() |
||
242 | { |
||
67 | luk | 243 | m_tab.Load(m_fSysTable |
69 | luk | 244 | ? IncronTab::GetSystemTablePath(m_user) |
245 | : IncronTab::GetUserTablePath(m_user)); |
||
45 | luk | 246 | |
247 | int cnt = m_tab.GetCount(); |
||
248 | for (int i=0; i<cnt; i++) { |
||
69 | luk | 249 | IncronTabEntry& rE = m_tab.GetEntry(i); |
45 | luk | 250 | InotifyWatch* pW = new InotifyWatch(rE.GetPath(), rE.GetMask()); |
65 | luk | 251 | |
252 | // warning only - permissions may change later |
||
67 | luk | 253 | if (!(m_fSysTable || MayAccess(rE.GetPath(), DONT_FOLLOW(rE.GetMask())))) |
65 | luk | 254 | syslog(LOG_WARNING, "access denied on %s - events will be discarded silently", rE.GetPath().c_str()); |
255 | |||
47 | luk | 256 | try { |
67 | luk | 257 | m_in.Add(pW); |
47 | luk | 258 | m_map.insert(IWCE_MAP::value_type(pW, &rE)); |
259 | } catch (InotifyException e) { |
||
67 | luk | 260 | if (m_fSysTable) |
261 | syslog(LOG_ERR, "cannot create watch for system table %s: (%i) %s", m_user.c_str(), e.GetErrorNumber(), strerror(e.GetErrorNumber())); |
||
262 | else |
||
263 | syslog(LOG_ERR, "cannot create watch for user %s: (%i) %s", m_user.c_str(), e.GetErrorNumber(), strerror(e.GetErrorNumber())); |
||
47 | luk | 264 | delete pW; |
265 | } |
||
45 | luk | 266 | } |
67 | luk | 267 | |
268 | m_pEd->Register(this); |
||
45 | luk | 269 | } |
270 | |||
271 | void UserTable::Dispose() |
||
272 | { |
||
67 | luk | 273 | m_pEd->Unregister(this); |
274 | |||
45 | luk | 275 | IWCE_MAP::iterator it = m_map.begin(); |
276 | while (it != m_map.end()) { |
||
277 | InotifyWatch* pW = (*it).first; |
||
67 | luk | 278 | m_in.Remove(pW); |
69 | luk | 279 | |
280 | PROC_MAP::iterator it2 = s_procMap.begin(); |
||
281 | while (it2 != s_procMap.end()) { |
||
282 | if ((*it2).second.pWatch == pW) { |
||
283 | PROC_MAP::iterator it3 = it2; |
||
284 | it2++; |
||
285 | s_procMap.erase(it3); |
||
286 | } |
||
287 | else { |
||
288 | it2++; |
||
289 | } |
||
290 | } |
||
291 | |||
45 | luk | 292 | delete pW; |
293 | it++; |
||
294 | } |
||
295 | |||
296 | m_map.clear(); |
||
297 | } |
||
298 | |||
299 | void UserTable::OnEvent(InotifyEvent& rEvt) |
||
300 | { |
||
301 | InotifyWatch* pW = rEvt.GetWatch(); |
||
69 | luk | 302 | IncronTabEntry* pE = FindEntry(pW); |
45 | luk | 303 | |
65 | luk | 304 | // no entry found - this shouldn't occur |
45 | luk | 305 | if (pE == NULL) |
306 | return; |
||
307 | |||
65 | luk | 308 | // discard event if user has no access rights to watch path |
67 | luk | 309 | if (!(m_fSysTable || MayAccess(pW->GetPath(), DONT_FOLLOW(rEvt.GetMask())))) |
65 | luk | 310 | return; |
311 | |||
45 | luk | 312 | std::string cmd; |
313 | const std::string& cs = pE->GetCmd(); |
||
314 | size_t pos = 0; |
||
315 | size_t oldpos = 0; |
||
316 | size_t len = cs.length(); |
||
317 | while ((pos = cs.find('$', oldpos)) != std::string::npos) { |
||
318 | if (pos < len - 1) { |
||
319 | size_t px = pos + 1; |
||
320 | if (cs[px] == '$') { |
||
321 | cmd.append(cs.substr(oldpos, pos-oldpos+1)); |
||
322 | oldpos = pos + 2; |
||
323 | } |
||
324 | else { |
||
325 | cmd.append(cs.substr(oldpos, pos-oldpos)); |
||
55 | luk | 326 | if (cs[px] == '@') { // base path |
327 | cmd.append(pW->GetPath()); |
||
328 | oldpos = pos + 2; |
||
329 | } |
||
330 | else if (cs[px] == '#') { // file name |
||
331 | cmd.append(rEvt.GetName()); |
||
332 | oldpos = pos + 2; |
||
333 | } |
||
334 | else if (cs[px] == '%') { // mask symbols |
||
335 | std::string s; |
||
336 | rEvt.DumpTypes(s); |
||
337 | cmd.append(s); |
||
338 | oldpos = pos + 2; |
||
339 | } |
||
340 | else if (cs[px] == '&') { // numeric mask |
||
341 | char* s; |
||
342 | asprintf(&s, "%u", (unsigned) rEvt.GetMask()); |
||
343 | cmd.append(s); |
||
344 | free(s); |
||
345 | oldpos = pos + 2; |
||
346 | } |
||
347 | else { |
||
348 | oldpos = pos + 1; |
||
349 | } |
||
45 | luk | 350 | } |
351 | } |
||
352 | else { |
||
353 | cmd.append(cs.substr(oldpos, pos-oldpos)); |
||
354 | oldpos = pos + 1; |
||
355 | } |
||
356 | } |
||
357 | cmd.append(cs.substr(oldpos)); |
||
358 | |||
359 | int argc; |
||
360 | char** argv; |
||
361 | if (!PrepareArgs(cmd, argc, argv)) { |
||
362 | syslog(LOG_ERR, "cannot prepare command arguments"); |
||
363 | return; |
||
364 | } |
||
365 | |||
67 | luk | 366 | if (m_fSysTable) |
367 | syslog(LOG_INFO, "(system::%s) CMD (%s)", m_user.c_str(), cmd.c_str()); |
||
368 | else |
||
369 | syslog(LOG_INFO, "(%s) CMD (%s)", m_user.c_str(), cmd.c_str()); |
||
45 | luk | 370 | |
47 | luk | 371 | if (pE->IsNoLoop()) |
372 | pW->SetEnabled(false); |
||
373 | |||
69 | luk | 374 | pid_t pid = fork(); |
375 | if (pid == 0) { |
||
45 | luk | 376 | |
67 | luk | 377 | // for system table |
378 | if (m_fSysTable) { |
||
379 | if (execvp(argv[0], argv) != 0) // exec failed |
||
380 | { |
||
381 | syslog(LOG_ERR, "cannot exec process: %s", strerror(errno)); |
||
382 | _exit(1); |
||
383 | } |
||
45 | luk | 384 | } |
67 | luk | 385 | else { |
386 | // for user table |
||
387 | struct passwd* pwd = getpwnam(m_user.c_str()); |
||
388 | if ( pwd == NULL // user not found |
||
389 | || setgid(pwd->pw_gid) != 0 // setting GID failed |
||
390 | || setuid(pwd->pw_uid) != 0 // setting UID failed |
||
391 | || execvp(argv[0], argv) != 0) // exec failed |
||
392 | { |
||
393 | syslog(LOG_ERR, "cannot exec process: %s", strerror(errno)); |
||
394 | _exit(1); |
||
395 | } |
||
396 | } |
||
45 | luk | 397 | } |
69 | luk | 398 | else if (pid > 0) { |
399 | ProcData_t pd; |
||
47 | luk | 400 | if (pE->IsNoLoop()) { |
401 | pd.onDone = on_proc_done; |
||
402 | pd.pWatch = pW; |
||
403 | } |
||
404 | else { |
||
405 | pd.onDone = NULL; |
||
406 | pd.pWatch = NULL; |
||
407 | } |
||
45 | luk | 408 | |
69 | luk | 409 | s_procMap.insert(PROC_MAP::value_type(pid, pd)); |
45 | luk | 410 | } |
411 | else { |
||
47 | luk | 412 | if (pE->IsNoLoop()) |
413 | pW->SetEnabled(true); |
||
414 | |||
45 | luk | 415 | syslog(LOG_ERR, "cannot fork process: %s", strerror(errno)); |
416 | } |
||
51 | luk | 417 | |
418 | CleanupArgs(argc, argv); |
||
45 | luk | 419 | } |
420 | |||
69 | luk | 421 | IncronTabEntry* UserTable::FindEntry(InotifyWatch* pWatch) |
45 | luk | 422 | { |
423 | IWCE_MAP::iterator it = m_map.find(pWatch); |
||
424 | if (it == m_map.end()) |
||
425 | return NULL; |
||
426 | |||
427 | return (*it).second; |
||
428 | } |
||
429 | |||
430 | bool UserTable::PrepareArgs(const std::string& rCmd, int& argc, char**& argv) |
||
431 | { |
||
432 | if (rCmd.empty()) |
||
433 | return false; |
||
434 | |||
55 | luk | 435 | StringTokenizer tok(rCmd, ' ', '\\'); |
45 | luk | 436 | std::deque<std::string> args; |
437 | while (tok.HasMoreTokens()) { |
||
438 | args.push_back(tok.GetNextToken()); |
||
439 | } |
||
440 | |||
441 | if (args.empty()) |
||
442 | return false; |
||
443 | |||
444 | argc = (int) args.size(); |
||
445 | argv = new char*[argc+1]; |
||
446 | argv[argc] = NULL; |
||
447 | |||
448 | for (int i=0; i<argc; i++) { |
||
449 | const std::string& s = args[i]; |
||
450 | size_t len = s.length(); |
||
451 | argv[i] = new char[len+1]; |
||
452 | strcpy(argv[i], s.c_str()); |
||
453 | } |
||
454 | |||
455 | return true; |
||
456 | } |
||
457 | |||
51 | luk | 458 | void UserTable::CleanupArgs(int argc, char** argv) |
459 | { |
||
460 | for (int i=0; i<argc; i++) { |
||
461 | delete[] argv[i]; |
||
462 | } |
||
463 | |||
464 | delete[] argv; |
||
465 | } |
||
466 | |||
47 | luk | 467 | void UserTable::FinishDone() |
468 | { |
||
69 | luk | 469 | pid_t res = 0; |
470 | int status = 0; |
||
471 | while ((res = waitpid(-1, &status, WNOHANG)) > 0) { |
||
472 | PROC_MAP::iterator it = s_procMap.find(res); |
||
473 | if (it != s_procMap.end()) { |
||
474 | ProcData_t pd = (*it).second; |
||
47 | luk | 475 | if (pd.onDone != NULL) |
476 | (*pd.onDone)(pd.pWatch); |
||
69 | luk | 477 | s_procMap.erase(it); |
47 | luk | 478 | } |
479 | } |
||
480 | } |
||
481 | |||
65 | luk | 482 | bool UserTable::MayAccess(const std::string& rPath, bool fNoFollow) const |
483 | { |
||
484 | // first, retrieve file permissions |
||
485 | struct stat st; |
||
486 | int res = fNoFollow |
||
487 | ? lstat(rPath.c_str(), &st) // don't follow symlink |
||
488 | : stat(rPath.c_str(), &st); |
||
489 | if (res != 0) |
||
490 | return false; // retrieving permissions failed |
||
491 | |||
492 | // file accessible to everyone |
||
493 | if (st.st_mode & S_IRWXO) |
||
494 | return true; |
||
495 | |||
496 | // retrieve user data |
||
497 | struct passwd* pwd = getpwnam(m_user.c_str()); |
||
498 | |||
67 | luk | 499 | // root may always access |
500 | if (pwd->pw_uid == 0) |
||
501 | return true; |
||
502 | |||
65 | luk | 503 | // file accesible to group |
504 | if (st.st_mode & S_IRWXG) { |
||
505 | |||
506 | // user's primary group |
||
507 | if (pwd != NULL && pwd->pw_gid == st.st_gid) |
||
508 | return true; |
||
509 | |||
510 | // now check group database |
||
511 | struct group *gr = getgrgid(st.st_gid); |
||
512 | if (gr != NULL) { |
||
513 | int pos = 0; |
||
514 | const char* un; |
||
515 | while ((un = gr->gr_mem[pos]) != NULL) { |
||
516 | if (strcmp(un, m_user.c_str()) == 0) |
||
517 | return true; |
||
518 | pos++; |
||
519 | } |
||
520 | } |
||
521 | } |
||
522 | |||
523 | // file accessible to owner |
||
524 | if (st.st_mode & S_IRWXU) { |
||
525 | if (pwd != NULL && pwd->pw_uid == st.st_uid) |
||
526 | return true; |
||
527 | } |
||
528 | |||
529 | return false; // no access right found |
||
530 | } |
||
47 | luk | 531 | |
65 | luk | 532 | |
533 |