]> git.sesse.net Git - betaftpd/blob - dcache.c
Fixed a security problem where the custom snprintf() would always be used. Thanks...
[betaftpd] / dcache.c
1 /*  dcache.c: BetaFTPD directory listing cache
2     Copyright (C) 2000 Steinar H. Gunderson
3
4     This program is is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License, version 2 of the
6     License as published by the Free Software Foundation.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18 #if HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #if HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25
26 #if HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29
30 #if HAVE_STRING_H
31 #include <string.h>
32 #endif
33
34 #if HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37
38 #if WANT_DCACHE
39 #include <ftpd.h>
40 #include <dcache.h>
41
42 extern struct dcache *first_dcache;
43
44 /*
45  * alloc_new_dcache():
46  *              Allocates a new directory cache entry (type `struct dcache'),
47  *              and adds it to the linked list.
48  */
49 struct dcache *alloc_new_dcache()
50 {
51         struct dcache *d = (struct dcache *)(malloc(sizeof(struct dcache)));
52
53         if (d == NULL) return d;
54
55         d->use_count = 0;
56         d->last_used = 0;
57         strcpy(d->dir_name, "");
58         d->dir_data = NULL;
59
60         add_to_linked_list((struct list_element *)first_dcache,
61                            (struct list_element *)d);
62
63         return d;
64 }
65
66 /*
67  * destroy_dcache():
68  *              Destroy a directory listing cache entry, remove it from the
69  *              linked list, and clean up after it.
70  *
71  *              If you free a cache entry that is in use (use_count > 0),
72  *              BetaFTPD will most likely crash (later). The thing you're supposed
73  *              to do when you're done with a dcache entry, is to decrement
74  *              its use_count, and let the timeout functions do the destroying
75  *              when it's time to do so.
76  */
77 void destroy_dcache(struct dcache * const d)
78 {
79         if (d == NULL) return;
80
81         if (d->dir_data != NULL) free(d->dir_data);
82         remove_from_linked_list((struct list_element *)d);
83 }
84
85 /*
86  * time_out_dcache():
87  *              Time out expired directory listing cache entries.
88  *              Uses much of the same code as time_out_sockets().
89  */
90 void time_out_dcache()
91 {
92         struct dcache *d = NULL, *next = first_dcache->next_dcache;
93         time_t now = time(NULL);
94
95         /* run through the linked list */
96         while (next != NULL) {
97                 d = next;
98                 next = d->next_dcache;
99
100                 if (d->use_count == 0 && (now - d->last_used > 900)) {
101                         destroy_dcache(d);
102                 }
103         }
104 }
105
106 /*
107  * populate_dcache():
108  *              Add a new entry in the dcache, using information from
109  *              the given file transfer object, and the current directory
110  *              (cwd).
111  */
112 void populate_dcache(struct ftran * const f, const char * const cwd,
113                      const char * const pattern, const struct list_options * const lo)
114 {
115         struct stat buf;
116         struct dcache *d = alloc_new_dcache();
117         if (d != NULL && stat(cwd, &buf) > -1) {
118                 d->use_count++;
119                 f->dir_cache = d;
120                 d->dir_data = f->file_data;
121                 d->dir_size = f->size;
122                 d->generated = buf.st_mtime;
123
124                 strcpy(d->dir_name, cwd);
125                 strncpy(d->pattern, pattern, 255);
126                 d->pattern[255] = 0;
127                 d->lo = *lo;
128         }
129 }
130
131 /*
132  * find_dcache():
133  *              Check if there is an existing dcache object matching our
134  *              criteria.
135  */
136 struct dcache *find_dcache(const char * const cwd, const char * const pattern,
137                            const struct list_options * const lo)
138 {
139         struct dcache *d = NULL, *next = first_dcache->next_dcache;
140         struct stat buf;
141
142         if (stat(cwd, &buf) > -1) {
143                 /* run through the linked list */
144                 while (next != NULL) {
145                         d = next;
146                         next = d->next_dcache;
147
148                         if (buf.st_mtime <= d->generated &&
149                             strcmp(d->dir_name, cwd) == 0 &&
150                             strcmp(d->pattern, pattern) == 0 &&
151                             memcmp(&(d->lo), lo, sizeof(struct list_options)) == 0) {
152                                 return d;
153                         }
154                 }
155         }
156         return NULL;
157 }
158 #endif /* WANT_DCACHE */