summaryrefslogtreecommitdiffstats
path: root/kcron/ctcron.cpp
blob: f5cb04c918df92ccbb5f2e841824501c1ed48902 (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/***************************************************************************
 *   CT Cron Implementation                                                *
 *   --------------------------------------------------------------------  *
 *   Copyright (C) 1999, Gary Meyer <gary@meyer.net>                       *
 *   --------------------------------------------------------------------  *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

// Do not introduce any TQt or KDE dependencies into the "CT"-prefixed classes.
// I want to be able to reuse these classes with another GUI toolkit. -GM 11/99

#include "ctcron.h"

#include "cti18n.h"
#include "cttask.h"
#include "ctvariable.h"
#include <unistd.h>      // getuid(), unlink()
#include <pwd.h>         // pwd, getpwnam(), getpwuid()
#include <stdio.h>
#include <assert.h>

#include <tqfile.h>

#include <kprocess.h>
#include <klocale.h>
#include <ktempfile.h>

#include <iostream>

using namespace std;

CTCron::CTCron(bool _syscron, string _login) :
  syscron(_syscron)
{
  int uid(getuid());

  KTempFile tmp;
  tmp.setAutoDelete(true);
  tmp.close();
  tmpFileName = tmp.name();  

  TQString readCommand;

  if (uid == 0)
  // root, so provide requested crontab
  {
    if (syscron)
    {
      readCommand  = "cat /etc/crontab > " + KProcess::quote(tmpFileName);
      writeCommand = "cat " + KProcess::quote(tmpFileName) + " > /etc/crontab";
      login = (const char *)i18n("(System Crontab)").local8Bit();
      name = "";
    }
    else
    {
      readCommand  = TQString("crontab -u ") + _login.c_str() + " -l > " + KProcess::quote(tmpFileName);
      writeCommand = TQString("crontab -u ") + _login.c_str() + " " + KProcess::quote(tmpFileName);
      if (!initFromPasswd(getpwnam(_login.c_str())))
      {
         error = i18n("No password entry found for user '%1'").arg(_login.c_str());
      }
    }
  }
  else
  // regular user, so provide user's own crontab
  {
    readCommand  = "crontab -l > " + KProcess::quote(tmpFileName);
    writeCommand = "crontab "      + KProcess::quote(tmpFileName);
    if (!initFromPasswd(getpwuid(uid)))
    {
      error = i18n("No password entry found for uid '%1'").arg(uid);
    }
  }

  if (name.empty())
    name = login;

  initialTaskCount      = 0;
  initialVariableCount  = 0;
  
  if (isError())
     return;

  // Don't set error if it can't be read, it means the user
  // doesn't have a crontab.
  if (system(TQFile::encodeName(readCommand)) == 0)
  {
    ifstream cronfile(TQFile::encodeName(tmpFileName));
    cronfile >> *this;
  }

  initialTaskCount      = task.size();
  initialVariableCount  = variable.size();
}

CTCron::CTCron(const struct passwd *pwd) :
  syscron(false)
{
  Q_ASSERT(pwd != 0L);

  KTempFile tmp;
  tmp.setAutoDelete(true);
  tmp.close();
  tmpFileName = tmp.name();  

  TQString readCommand  = TQString("crontab -u ") + TQString(pwd->pw_name) + " -l > " + KProcess::quote(tmpFileName);
  writeCommand = TQString("crontab -u ") + TQString(pwd->pw_name) + " " + KProcess::quote(tmpFileName);

  initFromPasswd(pwd);

  initialTaskCount      = 0;
  initialVariableCount  = 0;
  
  if (isError())
     return;

  // Don't set error if it can't be read, it means the user
  // doesn't have a crontab.
  if (system(TQFile::encodeName(readCommand)) == 0)
  {
    ifstream cronfile(TQFile::encodeName(tmpFileName));
    cronfile >> *this;
  }

  initialTaskCount      = task.size();
  initialVariableCount  = variable.size();
}

bool CTCron::initFromPasswd(const struct passwd *pwd)
{
   if (pwd == 0)
   {
      return false;
   }
   else
   {
      login = pwd->pw_name;
      name = pwd->pw_gecos;
      return true;
   }
}

void CTCron::operator = (const CTCron& source)
{
  assert(!source.syscron);

  for (CTVariableIterator i = const_cast<CTCron&>(source).variable.begin();
    i != source.variable.end(); ++i)
  {
    CTVariable* tmp = new CTVariable(**i);
    variable.push_back(tmp);
  }

  for (CTTaskIterator i = const_cast<CTCron&>(source).task.begin();
    i != source.task.end(); ++i)
  {
    CTTask* tmp = new CTTask(**i);
    task.push_back(tmp);
  }
}

istream& operator >> (istream& inputStream, CTCron& cron)
{
  const int MAX = 1024;
  char buffer[MAX];
  string line("");
  string comment("");

  while (inputStream)
  {
    inputStream.getline(buffer, MAX);
    line = buffer;

    // search for comments "#" but not disabled tasks "#\"
    if ((line.find("#") == 0) && (line.find("\\") != 1))
    {
      // If the first 10 characters don't contain a character, it's probably a disabled entry.
      int first_text = line.find_first_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
      if (first_text < 0)
         continue;

      if (first_text < 10) 
      {
         // remove leading pound sign
         line = line.substr(1,line.length()-1);
         // remove leading whitespace
         while (line.find_first_of(" \t") == 0)
            line = line.substr(1,line.length()-1);
         comment = line;
         continue;
      }
    }

    // else
    {
      // either a task or a variable
      int firstWhiteSpace(line.find_first_of(" \t"));
      int firstEquals(line.find("="));

      // if there is an equals sign and either there is no
      // whitespace or the first whitespace is after the equals
      // sign, it must be a variable
      if ((firstEquals > 0) && ((firstWhiteSpace == -1) ||
        firstWhiteSpace > firstEquals))
      {
        // create variable
        CTVariable *tmp = new CTVariable(line, comment);
        cron.variable.push_back(tmp);
        comment = "";
      }
      else
      // must be a task, either enabled or disabled
      {
        if (firstWhiteSpace > 0)
        {
          CTTask *tmp = new CTTask(line, comment, cron.syscron);
          cron.task.push_back(tmp);
          comment = "";
        }
      }
    }
  }
  return inputStream;
}

ostream& operator << (ostream& outputStream, const CTCron& cron)
{
  int itemCount(0);

  for (CTVariableIterator i = const_cast<CTCron&>(cron).variable.begin();
    i != cron.variable.end(); ++i)
  {
    outputStream << **i;
    itemCount++;
  }

  for (CTTaskIterator i = const_cast<CTCron&>(cron).task.begin();
    i != cron.task.end(); ++i)
  {
    outputStream << **i;
    itemCount++;
  }

  if (itemCount > 0)
  {
    outputStream << "# This file was written by KCron. Copyright (c) 1999, Gary Meyer\n";
    outputStream << "# Although KCron supports most crontab formats, use care when editing.\n";
    outputStream << "# Note: Lines beginning with \"#\\\" indicates a disabled task.\n";
  }

  return outputStream;
}

CTCron::~CTCron()
{
  for (CTTaskIterator i = task.begin(); i != task.end(); ++i)
    delete *i;
  for (CTVariableIterator i = variable.begin(); i != variable.end(); ++i)
    delete *i;
}

void CTCron::apply()
{
  // write to temp file
  ofstream cronfile(TQFile::encodeName(tmpFileName));
  cronfile << *this << flush;

  // install temp file into crontab
  if (system(TQFile::encodeName(writeCommand)) != 0)
  {
    error = i18n("An error occurred while updating crontab.");
  }

  // remove the temp file
  (void) unlink(TQFile::encodeName(tmpFileName));

  if (isError())
    return;

  // mark as applied
  for (CTTaskIterator i = task.begin(); i != task.end(); ++i)
    (*i)->apply();

  for (CTVariableIterator i = variable.begin(); i != variable.end(); ++i)
    (*i)->apply();

  initialTaskCount = task.size();
  initialVariableCount = variable.size();
}

void CTCron::cancel()
{
  for (CTTaskIterator i = task.begin(); i != task.end(); ++i)
    (*i)->cancel();

  for (CTVariableIterator i = variable.begin(); i != variable.end(); ++i)
    (*i)->cancel();
}

bool CTCron::dirty()
{
  bool isDirty(false);

  if (initialTaskCount != task.size()) isDirty = true;

  if (initialVariableCount != variable.size()) isDirty = true;

  for (CTTaskIterator i = task.begin(); i != task.end(); ++i)
    if ((*i)->dirty()) isDirty = true;

  for (CTVariableIterator i = variable.begin(); i != variable.end(); ++i)
    if ((*i)->dirty()) isDirty = true;

  return isDirty;
}

string CTCron::path() const
{
  string path;

  for (CTVariableIterator var = const_cast<CTCron*>(this)->variable.begin();
    var != variable.end(); var++)
  {
    if ((*var)->variable == "PATH")
    {
      path = (*var)->value;
    }
  }
  return path;
}