Subversion Repositories public

Rev

Rev 45 | Rev 51 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 45 Rev 49
Line 27... Line 27...
27
27
28
// #define INCRON_DEFAULT_EDITOR "nano" // for vim haters like me ;-)
28
// #define INCRON_DEFAULT_EDITOR "nano" // for vim haters like me ;-)
29
#define INCRON_DEFAULT_EDITOR "vim"
29
#define INCRON_DEFAULT_EDITOR "vim"
30
30
31
31
32
const char* argp_program_version = "incrontab 0.1.0";
32
const char* argp_program_version = "incrontab 0.3.0";
33
const char* argp_program_bug_address = "<bugs@aiken.cz>";
33
const char* argp_program_bug_address = "<bugs@aiken.cz>";
34
34
35
static char doc[] = "Table manipulator for incrond (inotify Cron daemon)";
35
static char doc[] = "Table manipulator for incrond (inotify cron daemon)";
36
36
37
static char args_doc[] = "FILE";
37
static char args_doc[] = "FILE";
38
38
39
static struct argp_option options[] = {
39
static struct argp_option options[] = {
40
  {"list",    'l', 0,      0,  "List current table" },
40
  {"list",    'l', 0,      0,  "List current table" },
Line 42... Line 42...
42
  {"edit",    'e', 0,      0,  "Edit table" },
42
  {"edit",    'e', 0,      0,  "Edit table" },
43
  {"user",    'u', "USER", 0,  "Override current user" },
43
  {"user",    'u', "USER", 0,  "Override current user" },
44
  { 0 }
44
  { 0 }
45
};
45
};
46
46
-
 
47
/// incrontab operations
47
enum
48
typedef enum
48
{
49
{
49
  OPER_NONE,
50
  OPER_NONE,    /// nothing
50
  OPER_LIST,
51
  OPER_LIST,    /// list table
51
  OPER_REMOVE,
52
  OPER_REMOVE,  /// remove table
52
  OPER_EDIT
53
  OPER_EDIT     /// edit table
53
};
54
} InCronTab_Operation_t;
54
55
-
 
56
/// incrontab arguments
55
struct arguments
57
struct arguments
56
{
58
{
57
  char *user;
59
  char *user;     /// user name
58
  int oper;
60
  int oper;       /// operation code
59
  char *file;
61
  char *file;     /// file to import
60
};
62
};
61
63
-
 
64
/// Parses the program options (arguments).
-
 
65
/**
-
 
66
 * \param[in] key argument key (name)
-
 
67
 * \param[in] arg argument value
-
 
68
 * \param[out] state options setting
-
 
69
 * \return 0 on success, ARGP_ERR_UNKNOWN on unknown argument(s)
-
 
70
 */
62
static error_t parse_opt (int key, char *arg, struct argp_state *state)
71
static error_t parse_opt(int key, char *arg, struct argp_state *state)
63
{
72
{
64
  struct arguments* arguments = (struct arguments*) state->input;
73
  struct arguments* arguments = (struct arguments*) state->input;
65
     
74
     
66
  switch (key) {
75
  switch (key) {
67
    case 'l':
76
    case 'l':
Line 88... Line 97...
88
  }
97
  }
89
 
98
 
90
  return 0;
99
  return 0;
91
}
100
}
92
101
93
-
 
-
 
102
/// Program arguments
94
static struct argp argp = { options, parse_opt, args_doc, doc };
103
static struct argp argp = { options, parse_opt, args_doc, doc };
95
104
-
 
105
/// Unlink a file with temporarily changed UID.
-
 
106
/**
-
 
107
 * \param[in] file file to unlink
-
 
108
 * \param[in] uid UID for unlink processing
-
 
109
 *
-
 
110
 * \attention No error checking is done!
-
 
111
 */
96
void unlink_suid(const char* file, uid_t uid)
112
void unlink_suid(const char* file, uid_t uid)
97
{
113
{
98
  uid_t iu = geteuid();
114
  uid_t iu = geteuid();
99
  seteuid(uid);
115
  seteuid(uid);
100
  unlink(file);
116
  unlink(file);
101
  seteuid(iu);
117
  seteuid(iu);
102
}
118
}
103
119
-
 
120
/// Copies a file to an user table.
-
 
121
/**
-
 
122
 * \param[in] path path to file
-
 
123
 * \param[in] user user name
-
 
124
 * \return true = success, false = failure
-
 
125
 */
104
bool copy_from_file(const char* path, const char* user)
126
bool copy_from_file(const char* path, const char* user)
105
{
127
{
106
  InCronTab tab;
128
  InCronTab tab;
107
  std::string s(path);
129
  std::string s(path);
108
  if (s == "-")
130
  if (s == "-")
Line 119... Line 141...
119
  }
141
  }
120
 
142
 
121
  return true;
143
  return true;
122
}
144
}
123
145
-
 
146
/// Removes an user table.
-
 
147
/**
-
 
148
 * \param[in] user user name
-
 
149
 * \return true = success, false = failure
-
 
150
 */
124
bool remove_table(const char* user)
151
bool remove_table(const char* user)
125
{
152
{
126
  std::string tp(InCronTab::GetUserTablePath(user));
153
  std::string tp(InCronTab::GetUserTablePath(user));
127
 
154
 
128
  if (unlink(tp.c_str()) != 0 && errno != ENOENT) {
155
  if (unlink(tp.c_str()) != 0 && errno != ENOENT) {
Line 131... Line 158...
131
  }
158
  }
132
 
159
 
133
  return true;
160
  return true;
134
}
161
}
135
162
-
 
163
/// Lists an user table.
-
 
164
/**
-
 
165
 * \param[in] user user name
-
 
166
 * \return true = success, false = failure
-
 
167
 *
-
 
168
 * \attention Listing is currently done through 'cat'.
-
 
169
 */
136
bool list_table(const char* user)
170
bool list_table(const char* user)
137
{
171
{
138
  std::string tp(InCronTab::GetUserTablePath(user));
172
  std::string tp(InCronTab::GetUserTablePath(user));
139
 
173
 
140
  if (access(tp.c_str(), R_OK) != 0) {
174
  if (access(tp.c_str(), R_OK) != 0) {
Line 151... Line 185...
151
  std::string cmd("cat ");
185
  std::string cmd("cat ");
152
  cmd.append(tp);
186
  cmd.append(tp);
153
  return system(cmd.c_str()) == 0;
187
  return system(cmd.c_str()) == 0;
154
}
188
}
155
189
-
 
190
/// Allows to edit an user table.
-
 
191
/**
-
 
192
 * \param[in] user user name
-
 
193
 * \return true = success, false = failure
-
 
194
 *
-
 
195
 * \attention This function is very complex and may contain
-
 
196
 *            various bugs including security ones. Please keep
-
 
197
 *            it in mind..
-
 
198
 */
156
bool edit_table(const char* user)
199
bool edit_table(const char* user)
157
{
200
{
158
  std::string tp(InCronTab::GetUserTablePath(user));
201
  std::string tp(InCronTab::GetUserTablePath(user));
159
 
202
 
160
  struct passwd* ppwd = getpwnam(user);
203
  struct passwd* ppwd = getpwnam(user);