Rev 47 | Rev 55 | 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; |
||
53 | luk | 79 | rEntry.m_uMask = 0; |
80 | rEntry.m_fNoLoop = false; |
||
45 | luk | 81 | |
82 | if (sscanf(s2, "%lu", &u) == 1) { |
||
83 | rEntry.m_uMask = (uint32_t) u; |
||
84 | } |
||
85 | else { |
||
86 | StringTokenizer tok(s2); |
||
87 | while (tok.HasMoreTokens()) { |
||
47 | luk | 88 | std::string s(tok.GetNextToken()); |
89 | if (s == "IN_NO_LOOP") |
||
90 | rEntry.m_fNoLoop = true; |
||
91 | else |
||
92 | rEntry.m_uMask |= InotifyEvent::GetMaskByName(s); |
||
45 | luk | 93 | } |
94 | } |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | |||
99 | bool InCronTab::Load(const std::string& rPath) |
||
100 | { |
||
101 | m_tab.clear(); |
||
102 | |||
103 | FILE* f = fopen(rPath.c_str(), "r"); |
||
104 | if (f == NULL) |
||
105 | return false; |
||
106 | |||
107 | char s[1000]; |
||
108 | InCronTabEntry e; |
||
109 | while (fgets(s, 1000, f) != NULL) { |
||
110 | if (InCronTabEntry::Parse(s, e)) { |
||
111 | m_tab.push_back(e); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | fclose(f); |
||
116 | |||
117 | return true; |
||
118 | } |
||
119 | |||
120 | bool InCronTab::Save(const std::string& rPath) |
||
121 | { |
||
122 | FILE* f = fopen(rPath.c_str(), "w"); |
||
123 | if (f == NULL) |
||
124 | return false; |
||
125 | |||
126 | std::deque<InCronTabEntry>::iterator it = m_tab.begin(); |
||
127 | while (it != m_tab.end()) { |
||
128 | fputs((*it).ToString().c_str(), f); |
||
129 | fputs("\n", f); |
||
130 | it++; |
||
131 | } |
||
132 | |||
133 | fclose(f); |
||
134 | |||
135 | return true; |
||
136 | } |
||
137 | |||
138 | bool InCronTab::CheckUser(const std::string& rUser) |
||
139 | { |
||
140 | char s[100], u[100]; |
||
141 | |||
142 | FILE* f = fopen(INCRON_ALLOW_PATH, "r"); |
||
143 | if (f == NULL) { |
||
144 | if (errno == ENOENT) { |
||
145 | f = fopen(INCRON_DENY_PATH, "r"); |
||
146 | if (f == NULL) { |
||
147 | return errno == ENOENT; |
||
148 | } |
||
149 | while (fgets(s, 100, f) != NULL) { |
||
150 | if (sscanf(s, "%s", u) == 1) { |
||
151 | if (rUser == u) { |
||
152 | fclose(f); |
||
153 | return false; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | fclose(f); |
||
158 | return true; |
||
159 | } |
||
160 | |||
161 | return false; |
||
162 | } |
||
163 | |||
164 | while (fgets(s, 100, f) != NULL) { |
||
165 | if (sscanf(s, "%s", u) == 1) { |
||
166 | if (rUser == u) { |
||
167 | fclose(f); |
||
168 | return true; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | fclose(f); |
||
174 | return false; |
||
175 | } |
||
176 | |||
177 | std::string InCronTab::GetUserTablePath(const std::string& rUser) |
||
178 | { |
||
179 | std::string s(INCRON_TABLE_BASE); |
||
180 | s.append(rUser); |
||
181 | return s; |
||
182 | } |
||
183 |