| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <assert.h> |
| 24 |
#include <ctype.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <stdarg.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#ifdef WIN32 |
| 32 |
#include "process.h" |
| 33 |
#else |
| 34 |
#include <sys/param.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <sys/wait.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
#include "global.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#ifndef HAVE_TEMPNAM |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
char *tempnam(const char *dir, const char *pfx) { |
| 53 |
|
| 54 |
static unsigned int curtmp; |
| 55 |
|
| 56 |
char *name; |
| 57 |
pid_t pid = getpid(); |
| 58 |
|
| 59 |
|
| 60 |
#ifndef MAXPATHLEN |
| 61 |
#define MAXPATHLEN 4096 |
| 62 |
#endif |
| 63 |
|
| 64 |
if (!pfx) |
| 65 |
pfx = "cftmp."; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if (dir != NULL) { |
| 73 |
if (!(name = (char *)malloc(MAXPATHLEN))) |
| 74 |
return(NULL); |
| 75 |
do { |
| 76 |
snprintf(name, MAXPATHLEN, "%s/%s%x.%u", dir, pfx, (unsigned int)pid, curtmp); |
| 77 |
curtmp++; |
| 78 |
} while (access(name, F_OK) != -1); |
| 79 |
return(name); |
| 80 |
} |
| 81 |
return(NULL); |
| 82 |
} |
| 83 |
#endif |
| 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 |
FILE *tempnam_secure(const char *dir, const char *pfx, char **filename) { |
| 109 |
char *tempname = NULL; |
| 110 |
int fd; |
| 111 |
int i; |
| 112 |
FILE *file = NULL; |
| 113 |
const int maxretry = 10; |
| 114 |
|
| 115 |
|
| 116 |
for (i = 0; i < maxretry; i++) { |
| 117 |
tempname = tempnam(dir, pfx); |
| 118 |
if (!tempname) |
| 119 |
return NULL; |
| 120 |
|
| 121 |
fd = open(tempname, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR); |
| 122 |
if (fd != -1) |
| 123 |
break; |
| 124 |
if (errno == EEXIST) |
| 125 |
LOG(llevError, "Created file detected in tempnam_secure. Someone hoping for a race condition?\n"); |
| 126 |
free(tempname); |
| 127 |
} |
| 128 |
|
| 129 |
if (fd == -1) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
file = fdopen(fd, "w+"); |
| 133 |
if (!file) { |
| 134 |
LOG(llevError, "fdopen() failed in tempnam_secure()!\n"); |
| 135 |
free(tempname); |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
*filename = tempname; |
| 140 |
return file; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
void remove_directory(const char *path) { |
| 155 |
DIR *dirp; |
| 156 |
char buf[MAX_BUF]; |
| 157 |
struct stat statbuf; |
| 158 |
int status; |
| 159 |
|
| 160 |
if ((dirp = opendir(path)) != NULL) { |
| 161 |
struct dirent *de; |
| 162 |
|
| 163 |
for (de = readdir(dirp); de; de = readdir(dirp)) { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if (de->d_name[0] == '.') |
| 169 |
continue; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
status = stat(de->d_name, &statbuf); |
| 175 |
if ((status != -1) && (S_ISDIR(statbuf.st_mode))) { |
| 176 |
snprintf(buf, sizeof(buf), "%s/%s", path, de->d_name); |
| 177 |
remove_directory(buf); |
| 178 |
continue; |
| 179 |
} |
| 180 |
snprintf(buf, sizeof(buf), "%s/%s", path, de->d_name); |
| 181 |
if (unlink(buf)) { |
| 182 |
LOG(llevError, "Unable to remove %s\n", path); |
| 183 |
} |
| 184 |
} |
| 185 |
closedir(dirp); |
| 186 |
} |
| 187 |
if (rmdir(path)) { |
| 188 |
LOG(llevError, "Unable to remove directory %s\n", path); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
#ifndef HAVE_STRDUP |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
char *strdup(const char *str) { |
| 201 |
char *c = (char *)malloc(strlen(str)+1); |
| 202 |
if (c != NULL) |
| 203 |
strcpy(c, str); |
| 204 |
return c; |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
#ifndef HAVE_STRNCASECMP |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
int strncasecmp(const char *s1, const char *s2, int n) { |
| 225 |
register int c1, c2; |
| 226 |
|
| 227 |
while (*s1 && *s2 && n) { |
| 228 |
c1 = tolower(*s1); |
| 229 |
c2 = tolower(*s2); |
| 230 |
if (c1 != c2) |
| 231 |
return (c1-c2); |
| 232 |
s1++; |
| 233 |
s2++; |
| 234 |
n--; |
| 235 |
} |
| 236 |
if (!n) |
| 237 |
return (0); |
| 238 |
return (int)(*s1-*s2); |
| 239 |
} |
| 240 |
#endif |
| 241 |
|
| 242 |
#ifndef HAVE_STRCASECMP |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
int strcasecmp(const char *s1, const char *s2) { |
| 257 |
register int c1, c2; |
| 258 |
|
| 259 |
while (*s1 && *s2) { |
| 260 |
c1 = tolower(*s1); |
| 261 |
c2 = tolower(*s2); |
| 262 |
if (c1 != c2) |
| 263 |
return (c1-c2); |
| 264 |
s1++; |
| 265 |
s2++; |
| 266 |
} |
| 267 |
if (*s1 == '\0' && *s2 == '\0') |
| 268 |
return 0; |
| 269 |
return (int)(*s1-*s2); |
| 270 |
} |
| 271 |
#endif |
| 272 |
|
| 273 |
#ifndef HAVE_STRCASESTR |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
char *strcasestr(const char *s, const char *find) { |
| 285 |
char c, sc; |
| 286 |
size_t len; |
| 287 |
|
| 288 |
if ((c = *find++) != 0) { |
| 289 |
c = tolower(c); |
| 290 |
len = strlen(find); |
| 291 |
do { |
| 292 |
do { |
| 293 |
if ((sc = *s++) == 0) |
| 294 |
return NULL; |
| 295 |
} while (tolower(sc) != c); |
| 296 |
} while (strncasecmp(s, find, len) != 0); |
| 297 |
s--; |
| 298 |
} |
| 299 |
return s; |
| 300 |
} |
| 301 |
#endif |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
void make_path_to_file(const char *filename) { |
| 313 |
char buf[MAX_BUF], *cp = buf; |
| 314 |
struct stat statbuf; |
| 315 |
|
| 316 |
if (!filename || !*filename) |
| 317 |
return; |
| 318 |
|
| 319 |
safe_strncpy(buf, filename, sizeof(buf)); |
| 320 |
while ((cp = strchr(cp+1, (int)'/'))) { |
| 321 |
*cp = '\0'; |
| 322 |
if (stat(buf, &statbuf) || !S_ISDIR(statbuf.st_mode)) { |
| 323 |
LOG(llevDebug, "Was not dir: %s\n", buf); |
| 324 |
if (mkdir(buf, SAVE_DIR_MODE)) { |
| 325 |
LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror(errno)); |
| 326 |
return; |
| 327 |
} |
| 328 |
} |
| 329 |
*cp = '/'; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
void safe_strcat(char *dest, const char *orig, size_t *curlen, size_t maxlen) { |
| 347 |
assert(curlen != NULL); |
| 348 |
assert(*curlen < maxlen); |
| 349 |
#ifdef HAVE_STRLCAT |
| 350 |
*curlen = strlcat(dest, orig, maxlen); |
| 351 |
#else |
| 352 |
if (*curlen == (maxlen-1)) |
| 353 |
return; |
| 354 |
strncpy(dest+*curlen, orig, maxlen-*curlen-1); |
| 355 |
dest[maxlen-1] = 0; |
| 356 |
*curlen += strlen(orig); |
| 357 |
#endif |
| 358 |
if (*curlen > (maxlen-1)) |
| 359 |
*curlen = maxlen-1; |
| 360 |
} |
| 361 |
|
| 362 |
#ifndef HAVE_STRLCPY |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
size_t strlcpy(char *dst, const char *src, size_t size) { |
| 367 |
strncpy(dst, src, size - 1); |
| 368 |
dst[size - 1] = '\0'; |
| 369 |
return strlen(src); |
| 370 |
} |
| 371 |
#endif |