1 /* vi:ai:et:ts=8 sw=2
2  */
3 /*
4  * wzdftpd - a modular and cool ftp server
5  * Copyright (C) 2002-2004  Pierre Chifflier
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * As a special exemption, Pierre Chifflier
22  * and other respective copyright holders give permission to link this program
23  * with OpenSSL, and distribute the resulting executable, without including
24  * the source code for OpenSSL in the source distribution.
25  */
26 
27 #ifndef WIN32
28 #include <unistd.h>
29 #endif
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 
35 #include <libwzd-core/wzd_structs.h>
36 #include <libwzd-core/wzd_libmain.h>
37 #include <libwzd-core/wzd_log.h>
38 #include <libwzd-core/wzd_messages.h>
39 
40 #include <libwzd-core/wzd_crontab.h>
41 
42 #include "debug_crontab.h"
43 
44 static int _cron_find_and_execute(const char * jobname, wzd_cronjob_t * crontab);
45 
do_site_listcrontab(wzd_string_t * name,wzd_string_t * param,wzd_context_t * context)46 int do_site_listcrontab(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
47 {
48   int ret;
49   char buffer[4096];
50   wzd_cronjob_t * cronjob;
51   time_t now;
52 
53   send_message_raw("200-\r\n",context);
54   send_message_raw(" Name                              Min  Hour Day  Mon  DayOfWeek Next\r\n",context);
55 
56   WZD_MUTEX_LOCK(SET_MUTEX_CRONTAB);
57   cronjob = getlib_mainConfig()->crontab;
58 
59   time(&now);
60 
61   while (cronjob != NULL) {
62 
63     snprintf(buffer,sizeof(buffer)," %-33s %-4s %-4s %-4s %-4s %-9s %-5ld\n",cronjob->hook->external_command,
64         cronjob->minutes, cronjob->hours, cronjob->day_of_month, cronjob->month,
65         cronjob->day_of_week, (long)(cronjob->next_run - now));
66     ret = send_message_raw(buffer,context);
67 
68     cronjob = cronjob->next_cronjob;
69   }
70 
71   ret = send_message_raw("200 command ok\r\n",context);
72 
73   WZD_MUTEX_UNLOCK(SET_MUTEX_CRONTAB);
74   return 0;
75 }
76 
do_site_cronjob(wzd_string_t * name,wzd_string_t * param,wzd_context_t * context)77 int do_site_cronjob(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
78 {
79   int ret, status;
80   char buffer[4096];
81   wzd_string_t * commandname, * jobname;
82 
83   commandname = str_tok(param," \t\r\n");
84   if (!commandname) {
85     ret = send_message_with_args(501,context,"site cronjob exec jobname");
86     return -1;
87   }
88 
89   ret = -1;
90   if (strcasecmp(str_tochar(commandname),"exec")==0) {
91     jobname = str_read_token(param);
92 
93     if (jobname) {
94       send_message_raw("200-\r\n",context);
95 
96       status = _cron_find_and_execute(str_tochar(jobname),getlib_mainConfig()->crontab);
97 
98       snprintf(buffer,sizeof(buffer)-1," cron job: %s\n",str_tochar(jobname));
99       ret = send_message_raw(buffer,context);
100 
101       if (status == 0)
102         ret = send_message_raw("200 command ok\r\n",context);
103       else if (status == -1)
104         ret = send_message_raw("200 command failed (no cron job with this name)\r\n",context);
105       else
106         ret = send_message_raw("200 command ok (with errors)\r\n",context);
107       ret = 0;
108     } else {
109       ret = send_message_with_args(501,context,"site cronjob exec jobname");
110       ret = -1;
111     }
112   } else {
113     ret = send_message_with_args(501,context,"site cronjob exec jobname");
114     ret = -1;
115   }
116 
117   str_deallocate(jobname);
118   str_deallocate(commandname);
119 
120   return ret;
121 }
122 
123 
124 
_cron_find_and_execute(const char * jobname,wzd_cronjob_t * crontab)125 static int _cron_find_and_execute(const char * jobname, wzd_cronjob_t * crontab)
126 {
127   int status = -1;
128   wzd_cronjob_t *job;
129   time_t now;
130 
131   job = malloc(sizeof(wzd_cronjob_t));
132 
133   WZD_MUTEX_LOCK(SET_MUTEX_CRONTAB);
134   while (crontab != NULL) {
135     if (crontab->hook && crontab->hook->external_command &&
136         strcmp(crontab->hook->external_command,jobname)==0) {
137       memcpy(job, crontab, sizeof(wzd_cronjob_t));
138       time(&now);
139       job->next_run = now;
140       job->next_cronjob = NULL;
141       status = 0;
142       break;
143     }
144     crontab = crontab->next_cronjob;
145   }
146 
147   WZD_MUTEX_UNLOCK(SET_MUTEX_CRONTAB);
148 
149   if (status == 0) {
150     cronjob_run(&job);
151   }
152 
153   free(job);
154 
155   return status;
156 }
157 
158