SummaryRefsLogTreeCommitDiffStats
path: root/crontab.scm
blob: f7bcf2a6d33e2ad8cfc59913c04e0538dfa994a6 (about) (plain) (blame)
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
;;   Copyright (C) 2003 Dale Mellor
;; 
;;   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, or (at your option)
;;   any later version.
;; 
;;   This program is distributed in the hope that it will be useful,
;;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;   GNU General Public License for more details.
;; 
;;   You should have received a copy of the GNU General Public License
;;   along with this program; if not, write to the Free Software
;;   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;   USA.


;; Apart from the collecting of options and the handling of --help and --version
;; (which are done in the mcron.scm file), this file provides all the
;; functionality of the crontab personality. It is designed to be loaded and run
;; once, and then the calling program can exit and the crontab program will have
;; completed its function.



;; Procedure to communicate with running cron daemon that a user has modified
;; his crontab. The user name is placed in /var/cron/update, and the process
;; whose PID is held in /var/run/cron.pid is sent a SIGHUP.

(define (hit-server user-name)
  (catch #t (lambda ()
              (let ((server-pid (with-input-from-file "/var/run/cron.pid"
                                  (lambda () (string->number (read-line))))))
                (catch #t (lambda ()
                            (with-output-to-file "/var/cron/update" (lambda ()
                              (display user-name)(newline))))
                       (lambda (key . args)
                         (display "Cannot write to /var/cron/update.\n")
                         (primitive-exit 14)))
                (kill server-pid SIGHUP)))
         (lambda (key . args)
           (display "Warning: a cron daemon is not running.\n"))))



;; Procedure to scan a file containing one user name per line (such as
;; /var/cron/allow and /var/cron/deny), and determine if the given name is in
;; there. The procedure returns #t, #f, or '() if the file does not exist.

(define (in-access-file? file name)
  (catch #t (lambda ()
              (with-input-from-file file (lambda ()
                (let loop ((input (read-line)))
                  (if (eof-object? input)
                      #f
                      (if (string=? input name)
                          #t
                          (loop (read-line))))))))
         (lambda (key . args) '())))



;; This program should have been installed SUID root. Here we get the passwd
;; entry for the real user who is running this program.

(define crontab-real-user (passwd:name (getpw (getuid))))



;; If the real user is not allowed to use crontab due to the /var/cron/allow
;; and/or /var/cron/deny files, bomb out now.

(if (or (eq? (in-access-file? "/var/cron/allow" crontab-real-user) #f)
        (eq? (in-access-file? "/var/cron/deny" crontab-real-user) #t))
    (begin
      (display "Access denied by system operator.\n")
      (primitive-exit 6)))



;; Iff the real user is root, he can use the -u option to access files of
;; another user.

(define crontab-user (option-ref options 'user crontab-real-user))



;; So now we know which crontab file we will be manipulating.

(define crontab-file (string-append "/var/cron/tabs/" crontab-user))



;; Check that no more than one of the mutually exclusive options are being used.

(if (> (+ (if (option-ref options 'edit #f) 1 0)
          (if (option-ref options 'list #f) 1 0)
          (if (option-ref options 'remove #f) 1 0))
       1)
    (begin
      (display "crontab: Only one of options -e, -l or -r can be used.\n")
      (primitive-exit 7)))



;; Check that a non-root user is trying to read someone else's files.

(if (and (not (eqv? (getuid) 0))
         (option-ref options 'user #f))
    (begin (display "crontab: Only root can use the -u option.\n")
           (primitive-exit 8)))



;; There are four possible sub-personalities to the crontab personality: list,
;; remove, edit and replace (when the user uses no options but supplies file
;; names on the command line).

(cond


 ;; In the list personality, we simply open the crontab and copy it
 ;; character-by-character to the standard output. If anything goes wrong, it
 ;; can only mean that this user does not have a crontab file.
 
 ((option-ref options 'list #f)
  (catch #t (lambda ()
              (with-input-from-file crontab-file (lambda ()
                 (do ((input (read-char) (read-char)))
                     ((eof-object? input))
                   (display input)))))
         (lambda (key . args)
           (display (string-append "No crontab for "
                                   crontab-user
                                   " exists.\n")))))


 ;; In the edit personality, we determine the name of a temporary file and an
 ;; editor command, copy an existing crontab file (if it is there) to the
 ;; temporary file, making sure the ownership is set so the real user can edit
 ;; it; once the editor returns we try to read the file to check that it is
 ;; parseable (but do nothing more with the configuration), and if it is okay
 ;; (this program is still running!) we move the temporary file to the real
 ;; crontab, wake the cron daemon up, and remove the temporary file.

 ((option-ref options 'edit #f)
  (let ((temp-file (string-append "/tmp/crontab." (number->string (getpid))))
        (editor (cond ((getenv "VISUAL") (getenv "VISUAL"))
                      ((getenv "EDITOR") (getenv "EDITOR"))
                      (else "vi"))))
    (catch #t (lambda () (copy-file crontab-file temp-file))
              (lambda (key . args) (with-output-to-file temp-file noop)))
    (chown temp-file (getuid) (getgid))
    (system (string-append editor " " temp-file))
    (read-vixie-file temp-file)
    (copy-file temp-file crontab-file)
    (delete-file temp-file)
    (hit-server crontab-user)))


 ;; In the remove personality we simply make an effort to delete the crontab and
 ;; wake the daemon. No worries if this fails.

 ((option-ref options 'remove #f)
  (catch #t (lambda () (delete-file crontab-file)
                       (hit-server crontab-user))
            noop))


 ;; In the case of the replace personality we loop over all the arguments on the
 ;; command line, and for each one parse the file to make sure it is parseable
 ;; (but subsequently ignore the configuration), and all being well we copy it
 ;; to the crontab location; we deal with the standard input in the same way but
 ;; different. :-)  In either case the server is woken so that it will read the
 ;; newly installed crontab.

 ((not (null? (option-ref options '() '())))
  (let ((input-file (car (option-ref options '() '()))))
    (if (string=? input-file "-")
        (let ((input-string (stdin->string)))
          (read-vixie-port (open-input-string input-string))
          (with-output-to-file crontab-file (lambda ()
                                              (display input-string))))
        (begin
          (read-vixie-file input-file)
          (copy-file input-file crontab-file))))
  (hit-server crontab-user))
 
 
 ;; The user is being silly. The message here is identical to the one Vixie cron
 ;; used to put out, for total compatibility.

 (else
  (display "crontab: usage error: file name must be specified for replace.\n")
  (primitive-exit 15)))