util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "../or/or.h"
  5. #ifdef HAVE_UNAME
  6. #include <sys/utsname.h>
  7. #endif
  8. /*
  9. * Memory wrappers
  10. */
  11. void *tor_malloc(size_t size) {
  12. void *result;
  13. result = malloc(size);
  14. if(!result) {
  15. log_fn(LOG_ERR, "Out of memory. Dying.");
  16. exit(1);
  17. }
  18. // memset(result,'X',size); /* deadbeef to encourage bugs */
  19. return result;
  20. }
  21. void *tor_realloc(void *ptr, size_t size) {
  22. void *result;
  23. result = realloc(ptr, size);
  24. if (!result) {
  25. log_fn(LOG_ERR, "Out of memory. Dying.");
  26. exit(1);
  27. }
  28. return result;
  29. }
  30. char *tor_strdup(const char *s) {
  31. char *dup;
  32. assert(s);
  33. dup = strdup(s);
  34. if(!dup) {
  35. log_fn(LOG_ERR,"Out of memory. Dying.");
  36. exit(1);
  37. }
  38. return dup;
  39. }
  40. /*
  41. * Time
  42. */
  43. void tor_gettimeofday(struct timeval *timeval) {
  44. #ifdef HAVE_GETTIMEOFDAY
  45. if (gettimeofday(timeval, NULL)) {
  46. log_fn(LOG_ERR, "gettimeofday failed.");
  47. /* If gettimeofday dies, we have either given a bad timezone (we didn't),
  48. or segfaulted.*/
  49. exit(1);
  50. }
  51. #elif defined(HAVE_FTIME)
  52. ftime(timeval);
  53. #else
  54. #error "No way to get time."
  55. #endif
  56. return;
  57. }
  58. long
  59. tv_udiff(struct timeval *start, struct timeval *end)
  60. {
  61. long udiff;
  62. long secdiff = end->tv_sec - start->tv_sec;
  63. if (secdiff+1 > LONG_MAX/1000000) {
  64. log_fn(LOG_WARN, "comparing times too far apart.");
  65. return LONG_MAX;
  66. }
  67. udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
  68. if(udiff < 0) {
  69. log_fn(LOG_INFO, "start (%ld.%ld) is after end (%ld.%ld). Returning 0.",
  70. (long)start->tv_sec, (long)start->tv_usec, (long)end->tv_sec, (long)end->tv_usec);
  71. return 0;
  72. }
  73. return udiff;
  74. }
  75. int tv_cmp(struct timeval *a, struct timeval *b) {
  76. if (a->tv_sec > b->tv_sec)
  77. return 1;
  78. if (a->tv_sec < b->tv_sec)
  79. return -1;
  80. if (a->tv_usec > b->tv_usec)
  81. return 1;
  82. if (a->tv_usec < b->tv_usec)
  83. return -1;
  84. return 0;
  85. }
  86. void tv_add(struct timeval *a, struct timeval *b) {
  87. a->tv_usec += b->tv_usec;
  88. a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  89. a->tv_usec %= 1000000;
  90. }
  91. void tv_addms(struct timeval *a, long ms) {
  92. a->tv_usec += (ms * 1000) % 1000000;
  93. a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  94. a->tv_usec %= 1000000;
  95. }
  96. time_t tor_timegm (struct tm *tm) {
  97. time_t ret;
  98. char *tz;
  99. tz = getenv("TZ");
  100. setenv("TZ", "", 1);
  101. tzset();
  102. ret = mktime(tm);
  103. if (tz)
  104. setenv("TZ", tz, 1);
  105. else
  106. unsetenv("TZ");
  107. tzset();
  108. return ret;
  109. }
  110. /*
  111. * Low-level I/O.
  112. */
  113. /* a wrapper for write(2) that makes sure to write all count bytes.
  114. * Only use if fd is a blocking fd. */
  115. int write_all(int fd, const char *buf, size_t count) {
  116. int written = 0;
  117. int result;
  118. while(written != count) {
  119. result = write(fd, buf+written, count-written);
  120. if(result<0)
  121. return -1;
  122. written += result;
  123. }
  124. return count;
  125. }
  126. /* a wrapper for read(2) that makes sure to read all count bytes.
  127. * Only use if fd is a blocking fd. */
  128. int read_all(int fd, char *buf, size_t count) {
  129. int numread = 0;
  130. int result;
  131. while(numread != count) {
  132. result = read(fd, buf+numread, count-numread);
  133. if(result<=0)
  134. return -1;
  135. numread += result;
  136. }
  137. return count;
  138. }
  139. void set_socket_nonblocking(int socket)
  140. {
  141. #ifdef MS_WINDOWS
  142. /* Yes means no and no means yes. Do you not want to be nonblocking? */
  143. int nonblocking = 0;
  144. ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
  145. #else
  146. fcntl(socket, F_SETFL, O_NONBLOCK);
  147. #endif
  148. }
  149. /*
  150. * Process control
  151. */
  152. /* Minimalist interface to run a void function in the background. On
  153. * unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  154. * func should not return, but rather should call spawn_exit.
  155. */
  156. int spawn_func(int (*func)(void *), void *data)
  157. {
  158. #ifdef MS_WINDOWS
  159. int rv;
  160. rv = _beginthread(func, 0, data);
  161. if (rv == (unsigned long) -1)
  162. return -1;
  163. return 0;
  164. #else
  165. pid_t pid;
  166. pid = fork();
  167. if (pid<0)
  168. return -1;
  169. if (pid==0) {
  170. /* Child */
  171. func(data);
  172. assert(0); /* Should never reach here. */
  173. return 0; /* suppress "control-reaches-end-of-non-void" warning. */
  174. } else {
  175. /* Parent */
  176. return 0;
  177. }
  178. #endif
  179. }
  180. void spawn_exit()
  181. {
  182. #ifdef MS_WINDOWS
  183. _endthread();
  184. #else
  185. exit(0);
  186. #endif
  187. }
  188. /*
  189. * Windows compatibility.
  190. */
  191. int
  192. tor_socketpair(int family, int type, int protocol, int fd[2])
  193. {
  194. #ifdef HAVE_SOCKETPAIR_XXXX
  195. /* For testing purposes, we never fall back to real socketpairs. */
  196. return socketpair(family, type, protocol, fd);
  197. #else
  198. int listener = -1;
  199. int connector = -1;
  200. int acceptor = -1;
  201. struct sockaddr_in listen_addr;
  202. struct sockaddr_in connect_addr;
  203. int size;
  204. if (protocol
  205. #ifdef AF_UNIX
  206. || family != AF_UNIX
  207. #endif
  208. ) {
  209. #ifdef MS_WINDOWS
  210. errno = WSAEAFNOSUPPORT;
  211. #else
  212. errno = EAFNOSUPPORT;
  213. #endif
  214. return -1;
  215. }
  216. if (!fd) {
  217. errno = EINVAL;
  218. return -1;
  219. }
  220. listener = socket(AF_INET, type, 0);
  221. if (listener == -1)
  222. return -1;
  223. memset (&listen_addr, 0, sizeof (listen_addr));
  224. listen_addr.sin_family = AF_INET;
  225. listen_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  226. listen_addr.sin_port = 0; /* kernel choses port. */
  227. if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
  228. == -1)
  229. goto tidy_up_and_fail;
  230. if (listen(listener, 1) == -1)
  231. goto tidy_up_and_fail;
  232. connector = socket(AF_INET, type, 0);
  233. if (connector == -1)
  234. goto tidy_up_and_fail;
  235. /* We want to find out the port number to connect to. */
  236. size = sizeof (connect_addr);
  237. if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
  238. goto tidy_up_and_fail;
  239. if (size != sizeof (connect_addr))
  240. goto abort_tidy_up_and_fail;
  241. if (connect(connector, (struct sockaddr *) &connect_addr,
  242. sizeof (connect_addr)) == -1)
  243. goto tidy_up_and_fail;
  244. size = sizeof (listen_addr);
  245. acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
  246. if (acceptor == -1)
  247. goto tidy_up_and_fail;
  248. if (size != sizeof(listen_addr))
  249. goto abort_tidy_up_and_fail;
  250. close(listener);
  251. /* Now check we are talking to ourself by matching port and host on the
  252. two sockets. */
  253. if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
  254. goto tidy_up_and_fail;
  255. if (size != sizeof (connect_addr)
  256. || listen_addr.sin_family != connect_addr.sin_family
  257. || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
  258. || listen_addr.sin_port != connect_addr.sin_port) {
  259. goto abort_tidy_up_and_fail;
  260. }
  261. fd[0] = connector;
  262. fd[1] = acceptor;
  263. return 0;
  264. abort_tidy_up_and_fail:
  265. #ifdef MS_WINDOWS
  266. errno = WSAECONNABORTED;
  267. #else
  268. errno = ECONNABORTED; /* I hope this is portable and appropriate. */
  269. #endif
  270. tidy_up_and_fail:
  271. {
  272. int save_errno = errno;
  273. if (listener != -1)
  274. close(listener);
  275. if (connector != -1)
  276. close(connector);
  277. if (acceptor != -1)
  278. close(acceptor);
  279. errno = save_errno;
  280. return -1;
  281. }
  282. #endif
  283. }
  284. #ifdef MS_WINDOWS
  285. int correct_socket_errno(int s)
  286. {
  287. int optval, optvallen=sizeof(optval);
  288. assert(errno == WSAEWOULDBLOCK);
  289. if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
  290. return errno;
  291. if (optval)
  292. return optval;
  293. return WSAEWOULDBLOCK;
  294. }
  295. #endif
  296. /*
  297. * Filesystem operations.
  298. */
  299. /* Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
  300. * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
  301. * directory. */
  302. file_status_t file_status(const char *fname)
  303. {
  304. struct stat st;
  305. if (stat(fname, &st)) {
  306. if (errno == ENOENT) {
  307. return FN_NOENT;
  308. }
  309. return FN_ERROR;
  310. }
  311. if (st.st_mode & S_IFDIR)
  312. return FN_DIR;
  313. else if (st.st_mode & S_IFREG)
  314. return FN_FILE;
  315. else
  316. return FN_ERROR;
  317. }
  318. /* Check whether dirname exists and is private. If yes returns
  319. 0. Else returns -1. */
  320. int check_private_dir(const char *dirname, int create)
  321. {
  322. struct stat st;
  323. if (stat(dirname, &st)) {
  324. if (errno != ENOENT) {
  325. log(LOG_WARN, "Directory %s cannot be read: %s", dirname,
  326. strerror(errno));
  327. return -1;
  328. }
  329. if (!create) {
  330. log(LOG_WARN, "Directory %s does not exist.", dirname);
  331. return -1;
  332. }
  333. log(LOG_INFO, "Creating directory %s", dirname);
  334. if (mkdir(dirname, 0700)) {
  335. log(LOG_WARN, "Error creating directory %s: %s", dirname,
  336. strerror(errno));
  337. return -1;
  338. } else {
  339. return 0;
  340. }
  341. }
  342. if (!(st.st_mode & S_IFDIR)) {
  343. log(LOG_WARN, "%s is not a directory", dirname);
  344. return -1;
  345. }
  346. if (st.st_uid != getuid()) {
  347. log(LOG_WARN, "%s is not owned by this UID (%d)", dirname, getuid());
  348. return -1;
  349. }
  350. if (st.st_mode & 0077) {
  351. log(LOG_WARN, "Fixing permissions on directory %s", dirname);
  352. if (chmod(dirname, 0700)) {
  353. log(LOG_WARN, "Could not chmod directory %s: %s", dirname,
  354. strerror(errno));
  355. return -1;
  356. } else {
  357. return 0;
  358. }
  359. }
  360. return 0;
  361. }
  362. int
  363. write_str_to_file(const char *fname, const char *str)
  364. {
  365. char tempname[1024];
  366. int fd;
  367. FILE *file;
  368. if (strlen(fname) > 1000) {
  369. log(LOG_WARN, "Filename %s is too long.", fname);
  370. return -1;
  371. }
  372. strcpy(tempname,fname);
  373. strcat(tempname,".tmp");
  374. if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
  375. log(LOG_WARN, "Couldn't open %s for writing: %s", tempname,
  376. strerror(errno));
  377. return -1;
  378. }
  379. if (!(file = fdopen(fd, "w"))) {
  380. log(LOG_WARN, "Couldn't fdopen %s for writing: %s", tempname,
  381. strerror(errno));
  382. close(fd); return -1;
  383. }
  384. if (fputs(str,file) == EOF) {
  385. log(LOG_WARN, "Error writing to %s: %s", tempname, strerror(errno));
  386. fclose(file); return -1;
  387. }
  388. fclose(file);
  389. if (rename(tempname, fname)) {
  390. log(LOG_WARN, "Error replacing %s: %s", fname, strerror(errno));
  391. return -1;
  392. }
  393. return 0;
  394. }
  395. char *read_file_to_str(const char *filename) {
  396. int fd; /* router file */
  397. struct stat statbuf;
  398. char *string;
  399. assert(filename);
  400. if(strcspn(filename,CONFIG_LEGAL_FILENAME_CHARACTERS) != 0) {
  401. log_fn(LOG_WARN,"Filename %s contains illegal characters.",filename);
  402. return NULL;
  403. }
  404. if(stat(filename, &statbuf) < 0) {
  405. log_fn(LOG_INFO,"Could not stat %s.",filename);
  406. return NULL;
  407. }
  408. fd = open(filename,O_RDONLY,0);
  409. if (fd<0) {
  410. log_fn(LOG_WARN,"Could not open %s.",filename);
  411. return NULL;
  412. }
  413. string = tor_malloc(statbuf.st_size+1);
  414. if(read_all(fd,string,statbuf.st_size) != statbuf.st_size) {
  415. log_fn(LOG_WARN,"Couldn't read all %ld bytes of file '%s'.",
  416. (long)statbuf.st_size,filename);
  417. free(string);
  418. close(fd);
  419. return NULL;
  420. }
  421. close(fd);
  422. string[statbuf.st_size] = 0; /* null terminate it */
  423. return string;
  424. }
  425. /* read lines from f (no more than maxlen-1 bytes each) until we
  426. * get one with a well-formed "key value".
  427. * point *key to the first word in line, point *value to the second.
  428. * Put a \0 at the end of key, remove everything at the end of value
  429. * that is whitespace or comment.
  430. * Return 1 if success, 0 if no more lines, -1 if error.
  431. */
  432. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out) {
  433. char *s, *key, *end, *value;
  434. try_next_line:
  435. if(!fgets(line, maxlen, f)) {
  436. if(feof(f))
  437. return 0;
  438. return -1; /* real error */
  439. }
  440. if((s = strchr(line,'#'))) /* strip comments */
  441. *s = 0; /* stop the line there */
  442. /* remove end whitespace */
  443. s = strchr(line, 0); /* now we're at the null */
  444. do {
  445. *s = 0;
  446. s--;
  447. } while (s >= line && isspace(*s));
  448. key = line;
  449. while(isspace(*key))
  450. key++;
  451. if(*key == 0)
  452. goto try_next_line; /* this line has nothing on it */
  453. end = key;
  454. while(*end && !isspace(*end))
  455. end++;
  456. value = end;
  457. while(*value && isspace(*value))
  458. value++;
  459. if(!*end || !*value) { /* only a key on this line. no value. */
  460. log_fn(LOG_WARN,"Line has keyword '%s' but no value. Skipping.",s);
  461. goto try_next_line;
  462. }
  463. *end = 0; /* null it out */
  464. log_fn(LOG_DEBUG,"got keyword '%s', value '%s'", key, value);
  465. *key_out = key, *value_out = value;
  466. return 1;
  467. }
  468. static char uname_result[256];
  469. static int uname_result_is_set = 0;
  470. const char *
  471. get_uname(void)
  472. {
  473. #ifdef HAVE_UNAME
  474. struct utsname u;
  475. #endif
  476. if (!uname_result_is_set) {
  477. #ifdef HAVE_UNAME
  478. if (!uname((&u))) {
  479. snprintf(uname_result, 255, "%s %s %s %s %s",
  480. u.sysname, u.nodename, u.release, u.version, u.machine);
  481. uname_result[255] = '\0';
  482. } else
  483. #endif
  484. {
  485. strcpy(uname_result, "Unknown platform");
  486. }
  487. uname_result_is_set = 1;
  488. }
  489. return uname_result;
  490. }
  491. void daemonize(void) {
  492. #ifdef HAVE_DAEMON
  493. if (daemon(0 /* chdir to / */,
  494. 0 /* Redirect std* to /dev/null */)) {
  495. log_fn(LOG_ERR, "Daemon returned an error: %s", strerror(errno));
  496. exit(1);
  497. }
  498. #elif ! defined(MS_WINDOWS)
  499. /* Fork; parent exits. */
  500. if (fork())
  501. exit(0);
  502. /* Create new session; make sure we never get a terminal */
  503. setsid();
  504. if (fork())
  505. exit(0);
  506. chdir("/");
  507. umask(000);
  508. fclose(stdin);
  509. fclose(stdout);
  510. fclose(stderr);
  511. #endif
  512. }
  513. void write_pidfile(char *filename) {
  514. #ifndef MS_WINDOWS
  515. FILE *pidfile;
  516. if ((pidfile = fopen(filename, "w")) == NULL) {
  517. log_fn(LOG_WARN, "unable to open %s for writing: %s", filename,
  518. strerror(errno));
  519. } else {
  520. fprintf(pidfile, "%d", getpid());
  521. fclose(pidfile);
  522. }
  523. #endif
  524. }
  525. int switch_id(char *user, char *group) {
  526. #ifndef MS_WINDOWS
  527. struct passwd *pw = NULL;
  528. struct group *gr = NULL;
  529. if (user) {
  530. pw = getpwnam(user);
  531. if (pw == NULL) {
  532. log_fn(LOG_ERR,"User '%s' not found.", user);
  533. return -1;
  534. }
  535. }
  536. /* switch the group first, while we still have the priveledges to do so */
  537. if (group) {
  538. gr = getgrnam(group);
  539. if (gr == NULL) {
  540. log_fn(LOG_ERR,"Group '%s' not found.", group);
  541. return -1;
  542. }
  543. if (setgid(gr->gr_gid) != 0) {
  544. log_fn(LOG_ERR,"Error setting GID: %s", strerror(errno));
  545. return -1;
  546. }
  547. } else if (user) {
  548. if (setgid(pw->pw_gid) != 0) {
  549. log_fn(LOG_ERR,"Error setting GID: %s", strerror(errno));
  550. return -1;
  551. }
  552. }
  553. /* now that the group is switched, we can switch users and lose
  554. privileges */
  555. if (user) {
  556. if (setuid(pw->pw_uid) != 0) {
  557. log_fn(LOG_ERR,"Error setting UID: %s", strerror(errno));
  558. return -1;
  559. }
  560. }
  561. return 0;
  562. #endif
  563. log_fn(LOG_ERR,
  564. "User or group specified, but switching users is not supported.");
  565. return -1;
  566. }