Debian Codegraph @

contrib/mod_readme.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server daemon
3 * Copyright (c) 1997, 1998 Public Flood Software
4 * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <[email protected]>
5 * Copyright (c) 2001-2020 The ProFTPD Project team
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20 *
21 * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
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 /* Notify the user when a given file was last changed
28 *
29 * Configuration:
30 * DisplayReadme <file-or-pattern>
31 *
32 * "DisplayReadme Readme" will tell the user when "Readme" on the current
33 * working directory was last changed. When the current working directory is
34 * changed (i.e. CWD, CDUP, etc), mod_readme will search for Readme again
35 * in that directory and also display its last changing dates if found.
36 */
37
38 #include "conf.h"
39
40 #define MOD_README_VERSION "mod_readme/1.0"
41
42 static void readme_add_path(pool *p, const char *path) {
43 struct stat st;
44
45 if (pr_fsio_stat(path, &st) == 0) {
46 int days = 0;
47 time_t now;
48 struct tm *now_tm = NULL;
49 char time_str[32] = {'\0'};
50
51 (void) time(&now);
52
53 now_tm = pr_gmtime(p, &now);
54 if (now_tm != NULL) {
55 struct tm *mtime_tm = NULL;
56 char *ptr = NULL;
57
58 days = (int) (365.25 * now_tm->tm_year) + now_tm->tm_yday;
59
60 mtime_tm = pr_gmtime(p, &st.st_mtime);
61 if (mtime_tm != NULL) {
62 days -= (int) (365.25 * mtime_tm->tm_year) + mtime_tm->tm_yday;
63
64 } else {
65 pr_log_debug(DEBUG3, MOD_README_VERSION
66 ": error obtaining GMT timestamp: %s", strerror(errno));
67 }
68
69 memset(time_str, '\0', sizeof(time_str));
70 #if defined(HAVE_CTIME_R)
71 pr_snprintf(time_str, sizeof(time_str)-1, "%.26s",
72 ctime_r(&st.st_mtime, time_str));
73 #else
74 pr_snprintf(time_str, sizeof(time_str)-1, "%.26s", ctime(&st.st_mtime));
75 #endif /* HAVE_CTIME_R */
76
77 ptr = strchr(time_str, '\n');
78 if (ptr != NULL) {
79 *ptr = '\0';
80 }
81
82 } else {
83 pr_log_debug(DEBUG3, MOD_README_VERSION
84 ": error obtaining GMT timestamp: %s", strerror(errno));
85 }
86
87 /* As a format nicety, if we're handling the PASS command, automatically
88 * add a blank line before this message, so as to separate the
89 * login message that mod_auth's POST_CMD handler for PASS will add from
90 * our message (see Bug#3605).
91 */
92 if (strcmp(session.curr_cmd, C_PASS) == 0) {
93 pr_response_add(R_DUP, "%s", "");
94 }
95
96 pr_response_add(R_DUP, _("Please read the file %s"), path);
97 if (now_tm != NULL) {
98 pr_response_add(R_DUP, _(" it was last modified on %.26s - %i %s ago"),
99 time_str, days, days == 1 ? _("day") : _("days"));
100 }
101 }
102 }
103
104 static void readme_add_pattern(pool *p, const char *pattern) {
105 glob_t g;
106 int a;
107
108 a = pr_fs_glob(pattern, 0, NULL, &g);
109 if (!a) {
110 char **path;
111
112 path = g.gl_pathv;
113 while (path && *path) {
114 pr_signals_handle();
115
116 readme_add_path(p, *path);
117 path++;
118 }
119
120 } else if (a == GLOB_NOSPACE) {
121 pr_log_debug(DEBUG3, MOD_README_VERSION
122 ": out of memory during globbing of '%s'", pattern);
123
124 } else if (a == GLOB_ABORTED) {
125 pr_log_debug(DEBUG3, MOD_README_VERSION
126 ": read error during globbing of '%s'", pattern);
127
128 } else if (a != GLOB_NOMATCH) {
129 pr_log_debug(DEBUG3, MOD_README_VERSION
130 ": unknown error during globbing of '%s'", pattern);
131 }
132
133 pr_fs_globfree(&g);
134 }
135
136 /* Command handlers
137 */
138
139 MODRET readme_post_cmd(cmd_rec *cmd) {
140 config_rec *c;
141
142 c = find_config(CURRENT_CONF, CONF_PARAM, "DisplayReadme", FALSE);
143 while (c != NULL) {
144 char *path;
145
146 path = c->argv[0];
147
148 pr_log_debug(DEBUG5, "Checking for display pattern %s", path);
149 readme_add_pattern(cmd->tmp_pool, path);
150
151 c = find_config_next(c, c->next, CONF_PARAM, "DisplayReadme",FALSE);
152 }
153
154 return PR_DECLINED(cmd);
155 }
156
157 /* Configuration handlers
158 */
159
160 /* usage: DisplayReadme path|pattern */
161 MODRET set_displayreadme(cmd_rec *cmd) {
162 config_rec *c;
163
164 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
165
166 if (cmd->argc != 2) {
167 CONF_ERROR(cmd, "syntax: DisplayReadme <filename-or-pattern>");
168 }
169
170 c = add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
171 c->flags |= CF_MERGEDOWN;
172
173 pr_log_debug(DEBUG5, "Added pattern %s to readme list",
174 (char *) cmd->argv[1]);
175 return PR_HANDLED(cmd);
176 }
177
178 /* Module API tables
179 */
180
181 static conftable readme_conftab[] = {
182 { "DisplayReadme", set_displayreadme, NULL },
183 { NULL }
184 };
185
186 static cmdtable readme_cmdtab[] = {
187 { POST_CMD, C_CWD, G_NONE, readme_post_cmd, FALSE, FALSE },
188 { POST_CMD, C_CDUP, G_NONE, readme_post_cmd, FALSE, FALSE },
189 { POST_CMD, C_XCWD, G_NONE, readme_post_cmd, FALSE, FALSE },
190 { POST_CMD, C_XCUP, G_NONE, readme_post_cmd, FALSE, FALSE },
191
192 /* We specifically use a LOG_CMD handler here, so that any DisplayReadme
193 * output is append after any possible DisplayLogin data (see Bug#3605).
194 */
195 { LOG_CMD, C_PASS, G_NONE, readme_post_cmd, FALSE, FALSE },
196
197 { 0, NULL }
198 };
199
200 module readme_module = {
201 /* Always NULL */
202 NULL, NULL,
203
204 /* Module API version */
205 0x20,
206
207 /* Module name */
208 "readme",
209
210 /* Module configuration directive table */
211 readme_conftab,
212
213 /* Module command handler table */
214 readme_cmdtab,
215
216 /* Module auth handler table */
217 NULL,
218
219 /* Module initialization */
220 NULL,
221
222 /* Session initialization */
223 NULL,
224
225 /* Module version */
226 MOD_README_VERSION
227 };
228