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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* ProFTPD - FTP server daemon
* Copyright (c) 2001-2011 The ProFTPD Project team
*
* 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.
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, The ProFTPD Project team and other respective
* copyright holders give permission to link this program with OpenSSL, and
* distribute the resulting executable, without including the source code for
* OpenSSL in the source distribution.
*/
/* ProFTPD scoreboard support (modified for use by external utilities).
* $Id: scoreboard.c,v 1.17 2011/05/23 20:46:20 castaglia Exp $
*/
#include "utils.h"
static int util_scoreboard_fd = -1;
static char util_scoreboard_file[PR_TUNABLE_PATH_MAX] = PR_RUN_DIR "/proftpd.scoreboard";
static pr_scoreboard_header_t util_header;
static unsigned char util_scoreboard_read_locked = FALSE;
/* Internal routines
*/
static int read_scoreboard_header(pr_scoreboard_header_t *header) {
int res = 0;
/* NOTE: reading a struct from a file using read(2) -- bad (in general). */
while ((res = read(util_scoreboard_fd, &util_header,
sizeof(pr_scoreboard_header_t))) != sizeof(pr_scoreboard_header_t)) {
if (res == 0)
return -1;
if (errno == EINTR)
continue;
return -1;
}
/* Note: these errors will most likely occur only for inetd-run daemons.
* Standalone daemons erase the scoreboard on startup.
*/
if (header->sch_magic != UTIL_SCOREBOARD_MAGIC)
return UTIL_SCORE_ERR_BAD_MAGIC;
if (header->sch_version < UTIL_SCOREBOARD_VERSION)
return UTIL_SCORE_ERR_OLDER_VERSION;
if (header->sch_version > UTIL_SCOREBOARD_VERSION)
return UTIL_SCORE_ERR_NEWER_VERSION;
return 0;
}
static int rlock_scoreboard(void) {
struct flock lock;
lock.l_type = F_RDLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
while (fcntl(util_scoreboard_fd, F_SETLKW, &lock) < 0) {
/* It would be nice to try to restart the fcntl(2) in cases where
* it is interrupted. However, that could possibly lead to a tight
* loop which leaves the scoreboard file locked, which in turn causes
* problems for the proftpd daemon trying to read that scoreboard.
*
* Instead, only retry the fcntl(2) on EAGAIN, and NOT on EINTR.
*/
if (errno == EAGAIN)
continue;
return -1;
}
util_scoreboard_read_locked = TRUE;
return 0;
}
static int unlock_scoreboard(void) {
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
util_scoreboard_read_locked = FALSE;
while (fcntl(util_scoreboard_fd, F_SETLK, &lock) < 0) {
if (errno == EAGAIN)
continue;
return -1;
}
return 0;
}
/* Public routines
*/
int util_close_scoreboard(void) {
if (util_scoreboard_fd == -1)
return 0;
if (util_scoreboard_read_locked)
unlock_scoreboard();
close(util_scoreboard_fd);
util_scoreboard_fd = -1;
return 0;
}
const char *util_get_scoreboard(void) {
return util_scoreboard_file;
}
int util_open_scoreboard(int flags) {
int res;
struct stat st;
/* Prevent writing to a symlink while avoiding a race condition: open
* the file name O_RDWR|O_CREAT first, then check to see if it's a symlink.
* If so, close the file and error out. If not, truncate as necessary,
* and continue.
*/
util_scoreboard_fd = open(util_scoreboard_file, flags);
if (util_scoreboard_fd < 0)
return -1;
if (fstat(util_scoreboard_fd, &st) < 0) {
close(util_scoreboard_fd);
util_scoreboard_fd = -1;
return -1;
}
if (S_ISLNK(st.st_mode)) {
close(util_scoreboard_fd);
util_scoreboard_fd = -1;
errno = EPERM;
return -1;
}
/* Check the header of this scoreboard file. */
res = read_scoreboard_header(&util_header);
if (res < 0)
return res;
return 0;
}
int util_set_scoreboard(const char *path) {
char dir[PR_TUNABLE_PATH_MAX] = {'\0'};
struct stat st;
char *tmp = NULL;
util_sstrncpy(dir, path, sizeof(dir));
tmp = strrchr(dir, '/');
if (tmp == NULL) {
errno = EINVAL;
return -1;
}
*tmp = '\0';
/* Parent directory must not be world-writable. */
if (stat(dir, &st) < 0)
return -1;
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return -1;
}
if (st.st_mode & S_IWOTH) {
errno = EPERM;
return -1;
}
*tmp = '/';
/* Make sure file exists. */
if (stat(dir, &st) < 0)
return -1;
util_sstrncpy(util_scoreboard_file, path, sizeof(util_scoreboard_file));
return 0;
}
pid_t util_scoreboard_get_daemon_pid(void) {
return util_header.sch_pid;
}
time_t util_scoreboard_get_daemon_uptime(void) {
return util_header.sch_uptime;
}
pr_scoreboard_entry_t *util_scoreboard_entry_read(void) {
static pr_scoreboard_entry_t scan_entry;
int res = 0;
if (util_scoreboard_fd < 0) {
errno = EINVAL;
return NULL;
}
/* Make sure the scoreboard file is read-locked. */
if (!util_scoreboard_read_locked)
rlock_scoreboard();
memset(&scan_entry, '\0', sizeof(scan_entry));
/* NOTE: use readv(2)? */
errno = 0;
while (scan_entry.sce_pid == 0) {
while ((res = read(util_scoreboard_fd, &scan_entry,
sizeof(scan_entry))) <= 0) {
if (res < 0 &&
errno == EINTR) {
continue;
} else {
unlock_scoreboard();
if (errno) {
fprintf(stdout, "error reading scoreboard entry: %s\n",
strerror(errno));
}
return NULL;
}
}
if (scan_entry.sce_pid) {
unlock_scoreboard();
return &scan_entry;
}
}
unlock_scoreboard();
return NULL;
}
int util_scoreboard_scrub(int verbose) {
int fd = -1;
off_t curr_offset = 0;
struct flock lock;
pr_scoreboard_entry_t sce;
if (verbose) {
fprintf(stdout, "scrubbing ScoreboardFile %s\n", util_get_scoreboard());
}
/* Manually open the scoreboard. It won't hurt if the process already
* has a descriptor opened on the scoreboard file.
*/
fd = open(util_get_scoreboard(), O_RDWR);
if (fd < 0) {
return -1;
}
/* Lock the entire scoreboard. */
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
/* We can afford to block/wait until we obtain our lock on the file. */
while (fcntl(fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
continue;
}
return -1;
}
/* Skip past the scoreboard header. */
curr_offset = lseek(fd, (off_t) sizeof(pr_scoreboard_header_t), SEEK_SET);
memset(&sce, 0, sizeof(sce));
while (read(fd, &sce, sizeof(sce)) == sizeof(sce)) {
/* Check to see if the PID in this entry is valid. If not, erase
* the slot.
*/
if (sce.sce_pid &&
kill(sce.sce_pid, 0) < 0 &&
errno == ESRCH) {
/* OK, the recorded PID is no longer valid. */
if (verbose) {
fprintf(stdout, "scrubbing scoreboard slot for PID %u",
(unsigned int) sce.sce_pid);
}
/* Rewind to the start of this slot. */
if (lseek(fd, curr_offset, SEEK_SET) < 0) {
if (verbose) {
fprintf(stdout, "error scrubbing scoreboard: %s",
strerror(errno));
}
}
memset(&sce, 0, sizeof(sce));
while (write(fd, &sce, sizeof(sce)) != sizeof(sce)) {
if (errno == EINTR) {
continue;
}
if (verbose) {
fprintf(stdout, "error scrubbing scoreboard: %s",
strerror(errno));
}
}
}
/* Mark the current offset. */
curr_offset = lseek(fd, (off_t) 0, SEEK_CUR);
}
/* Release the scoreboard. */
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
while (fcntl(fd, F_SETLKW, &lock) < 0) {
if (errno == EINTR) {
continue;
}
}
/* Don't need the descriptor anymore. */
(void) close(fd);
return 0;
}
|