log.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. Copyright (c) 2003-2006 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "polipo.h"
  20. #ifdef HAVE_SYSLOG
  21. #include <syslog.h>
  22. #endif
  23. static int logLevel = LOGGING_DEFAULT;
  24. static int logSyslog = 0;
  25. static AtomPtr logFile = NULL;
  26. static FILE *logF;
  27. static int logFilePermissions = 0640;
  28. int scrubLogs = 0;
  29. #ifdef HAVE_SYSLOG
  30. static AtomPtr logFacility = NULL;
  31. static int facility;
  32. #endif
  33. #define STR(x) XSTR(x)
  34. #define XSTR(x) #x
  35. static void initSyslog(void);
  36. #ifdef HAVE_SYSLOG
  37. static char *syslogBuf;
  38. static int syslogBufSize;
  39. static int syslogBufLength;
  40. static int translateFacility(AtomPtr facility);
  41. static int translatePriority(int type);
  42. static void accumulateSyslogV(int type, const char *f, va_list args);
  43. static void accumulateSyslogN(int type, const char *s, int len);
  44. #endif
  45. void
  46. preinitLog()
  47. {
  48. CONFIG_VARIABLE_SETTABLE(logLevel, CONFIG_HEX, configIntSetter,
  49. "Logging level (max = " STR(LOGGING_MAX) ").");
  50. CONFIG_VARIABLE(logFile, CONFIG_ATOM, "Log file (stderr if empty and logSyslog is unset, /var/log/polipo if empty and daemonise is true).");
  51. CONFIG_VARIABLE(logFilePermissions, CONFIG_OCTAL,
  52. "Access rights of the logFile.");
  53. CONFIG_VARIABLE_SETTABLE(scrubLogs, CONFIG_BOOLEAN, configIntSetter,
  54. "If true, don't include URLs in logs.");
  55. #ifdef HAVE_SYSLOG
  56. CONFIG_VARIABLE(logSyslog, CONFIG_BOOLEAN, "Log to syslog.");
  57. CONFIG_VARIABLE(logFacility, CONFIG_ATOM, "Syslog facility to use.");
  58. logFacility = internAtom("user");
  59. #endif
  60. logF = stderr;
  61. }
  62. int
  63. loggingToStderr(void) {
  64. return(logF == stderr);
  65. }
  66. static FILE *
  67. openLogFile(void)
  68. {
  69. int fd;
  70. FILE *f;
  71. fd = open(logFile->string, O_WRONLY | O_CREAT | O_APPEND,
  72. logFilePermissions);
  73. if(fd < 0)
  74. return NULL;
  75. f = fdopen(fd, "a");
  76. if(f == NULL) {
  77. int saved_errno = errno;
  78. close(fd);
  79. errno = saved_errno;
  80. return NULL;
  81. }
  82. setvbuf(f, NULL, _IOLBF, 0);
  83. return f;
  84. }
  85. void
  86. initLog(void)
  87. {
  88. if(daemonise && logFile == NULL && !logSyslog)
  89. logFile = internAtom("/var/log/polipo");
  90. if(logFile != NULL && logFile->length > 0) {
  91. FILE *f;
  92. logFile = expandTilde(logFile);
  93. f = openLogFile();
  94. if(f == NULL) {
  95. do_log_error(L_ERROR, errno, "Couldn't open log file %s",
  96. logFile->string);
  97. exit(1);
  98. }
  99. logF = f;
  100. }
  101. if(logSyslog) {
  102. initSyslog();
  103. if(logFile == NULL) {
  104. logF = NULL;
  105. }
  106. }
  107. }
  108. #ifdef HAVE_SYSLOG
  109. static void
  110. initSyslog()
  111. {
  112. if(logSyslog) {
  113. facility = translateFacility(logFacility);
  114. closelog();
  115. openlog("polipo", LOG_PID, facility);
  116. if(!syslogBuf) {
  117. syslogBuf = strdup("");
  118. syslogBufSize = 1;
  119. }
  120. }
  121. }
  122. /* Map a user-provided name to a syslog facility.
  123. This is rendered quite ugly because POSIX hardly defines any, but we
  124. should allow any the local system knows about. */
  125. static int
  126. translateFacility(AtomPtr facility)
  127. {
  128. typedef struct
  129. {
  130. const char *name;
  131. int facility;
  132. } FacilitiesRec;
  133. /* List of all known valid syslog facilities.
  134. This list is terminated by a NULL facility name. */
  135. FacilitiesRec facilities[] = {
  136. /* These are all the facilities found in glibc 2.5. */
  137. #ifdef LOG_AUTH
  138. { "auth", LOG_AUTH },
  139. #endif
  140. #ifdef LOG_AUTHPRIV
  141. { "authpriv", LOG_AUTHPRIV },
  142. #endif
  143. #ifdef LOG_CRON
  144. { "cron", LOG_CRON },
  145. #endif
  146. #ifdef LOG_DAEMON
  147. { "daemon", LOG_DAEMON },
  148. #endif
  149. #ifdef LOG_FTP
  150. { "ftp", LOG_FTP },
  151. #endif
  152. #ifdef LOG_KERN
  153. { "kern", LOG_KERN },
  154. #endif
  155. #ifdef LOG_LPR
  156. { "lpr", LOG_LPR },
  157. #endif
  158. #ifdef LOG_MAIL
  159. { "mail", LOG_MAIL },
  160. #endif
  161. #ifdef LOG_NEWS
  162. { "news", LOG_NEWS },
  163. #endif
  164. #ifdef LOG_SYSLOG
  165. { "syslog", LOG_SYSLOG },
  166. #endif
  167. #ifdef LOG_uucp
  168. { "uucp", LOG_UUCP },
  169. #endif
  170. /* These are required by POSIX. */
  171. { "user", LOG_USER },
  172. { "local0", LOG_LOCAL0 },
  173. { "local1", LOG_LOCAL1 },
  174. { "local2", LOG_LOCAL2 },
  175. { "local3", LOG_LOCAL3 },
  176. { "local4", LOG_LOCAL4 },
  177. { "local5", LOG_LOCAL5 },
  178. { "local6", LOG_LOCAL6 },
  179. { "local7", LOG_LOCAL7 },
  180. { NULL, 0 }};
  181. FacilitiesRec *current;
  182. /* It would be more fitting to return LOG_DAEMON, but POSIX does not
  183. guarantee the existence of that facility. */
  184. if(!facility) {
  185. return LOG_USER;
  186. }
  187. current = facilities;
  188. while(current->name) {
  189. if(!strcmp(current->name, atomString(facility))) {
  190. return current->facility;
  191. }
  192. current++;
  193. }
  194. /* This will go to stderr because syslog is not yet initialized. */
  195. do_log(L_ERROR, "Specified logFacility %s nonexistent on this system.",
  196. atomString(facility));
  197. return LOG_USER;
  198. }
  199. /* Translate a Polipo error type into a syslog priority. */
  200. static int
  201. translatePriority(int type)
  202. {
  203. typedef struct
  204. {
  205. int type;
  206. int priority;
  207. } PrioritiesRec;
  208. /* The list is terminated with a type of zero. */
  209. PrioritiesRec priorities[] = {{ L_ERROR, LOG_ERR },
  210. { L_WARN, LOG_WARNING },
  211. { L_INFO, LOG_NOTICE },
  212. { L_FORBIDDEN, LOG_DEBUG },
  213. { L_UNCACHEABLE, LOG_DEBUG },
  214. { L_SUPERSEDED, LOG_DEBUG },
  215. { L_VARY, LOG_DEBUG },
  216. { L_TUNNEL, LOG_NOTICE },
  217. { 0, 0 }};
  218. PrioritiesRec *current;
  219. current = priorities;
  220. while(current->type) {
  221. if(current->type == type) {
  222. return current->priority;
  223. }
  224. current++;
  225. }
  226. return LOG_DEBUG;
  227. }
  228. static int
  229. expandSyslog(int len)
  230. {
  231. int newsize;
  232. char *newbuf;
  233. if(len < 0)
  234. newsize = syslogBufSize * 2;
  235. else
  236. newsize = syslogBufLength + len + 1;
  237. newbuf = realloc(syslogBuf, newsize);
  238. if(!newbuf)
  239. return -1;
  240. syslogBuf = newbuf;
  241. syslogBufSize = newsize;
  242. return 1;
  243. }
  244. static void
  245. maybeFlushSyslog(int type)
  246. {
  247. char *linefeed;
  248. while(1) {
  249. linefeed = memchr(syslogBuf, '\n', syslogBufLength);
  250. if(linefeed == NULL)
  251. break;
  252. *linefeed = '\0';
  253. syslog(translatePriority(type), "%s", syslogBuf);
  254. linefeed++;
  255. syslogBufLength -= (linefeed - syslogBuf);
  256. if(syslogBufLength > 0)
  257. memmove(syslogBuf, linefeed, syslogBufLength);
  258. }
  259. }
  260. static void
  261. accumulateSyslogV(int type, const char *f, va_list args)
  262. {
  263. int rc;
  264. va_list args_copy;
  265. again:
  266. va_copy(args_copy, args);
  267. rc = vsnprintf(syslogBuf + syslogBufLength,
  268. syslogBufSize - syslogBufLength,
  269. f, args_copy);
  270. va_end(args_copy);
  271. if(rc < 0 || rc >= syslogBufSize - syslogBufLength) {
  272. rc = expandSyslog(rc);
  273. if(rc < 0)
  274. return;
  275. goto again;
  276. }
  277. syslogBufLength += rc;
  278. maybeFlushSyslog(type);
  279. }
  280. static void
  281. accumulateSyslogN(int type, const char *s, int len)
  282. {
  283. while(syslogBufSize - syslogBufLength <= len)
  284. expandSyslog(len);
  285. memcpy(syslogBuf + syslogBufLength, s, len);
  286. syslogBufLength += len;
  287. syslogBuf[syslogBufLength] = '\0';
  288. maybeFlushSyslog(type);
  289. }
  290. #else
  291. static void
  292. initSyslog()
  293. {
  294. return;
  295. }
  296. #endif
  297. /* Flush any messages waiting to be logged. */
  298. void flushLog()
  299. {
  300. if(logF)
  301. fflush(logF);
  302. #ifdef HAVE_SYSLOG
  303. /* There shouldn't really be anything here, but let's be paranoid.
  304. We can't pick a good value for `type', so just invent one. */
  305. if(logSyslog && syslogBuf[0] != '\0') {
  306. accumulateSyslogN(L_INFO, "\n", 1);
  307. }
  308. assert(syslogBufLength == 0);
  309. #endif
  310. }
  311. void
  312. reopenLog()
  313. {
  314. if(logFile && logFile->length > 0) {
  315. FILE *f;
  316. f = openLogFile();
  317. if(f == NULL) {
  318. do_log_error(L_ERROR, errno, "Couldn't reopen log file %s",
  319. logFile->string);
  320. exit(1);
  321. }
  322. fclose(logF);
  323. logF = f;
  324. }
  325. if(logSyslog)
  326. initSyslog();
  327. }
  328. void
  329. really_do_log(int type, const char *f, ...)
  330. {
  331. va_list args;
  332. va_start(args, f);
  333. if(type & LOGGING_MAX & logLevel)
  334. really_do_log_v(type, f, args);
  335. va_end(args);
  336. }
  337. void
  338. really_do_log_v(int type, const char *f, va_list args)
  339. {
  340. va_list args_copy;
  341. if(type & LOGGING_MAX & logLevel) {
  342. if(logF)
  343. {
  344. va_copy(args_copy, args);
  345. vfprintf(logF, f, args_copy);
  346. va_end(args_copy);
  347. }
  348. #ifdef HAVE_SYSLOG
  349. if(logSyslog) {
  350. va_copy(args_copy, args);
  351. accumulateSyslogV(type, f, args_copy);
  352. va_end(args_copy);
  353. }
  354. #endif
  355. }
  356. }
  357. void
  358. really_do_log_error(int type, int e, const char *f, ...)
  359. {
  360. va_list args;
  361. va_start(args, f);
  362. if(type & LOGGING_MAX & logLevel)
  363. really_do_log_error_v(type, e, f, args);
  364. va_end(args);
  365. }
  366. void
  367. really_do_log_error_v(int type, int e, const char *f, va_list args)
  368. {
  369. va_list args_copy;
  370. if((type & LOGGING_MAX & logLevel) != 0) {
  371. char *es = pstrerror(e);
  372. if(es == NULL)
  373. es = "Unknown error";
  374. if(logF) {
  375. va_copy(args_copy, args);
  376. vfprintf(logF, f, args_copy);
  377. fprintf(logF, ": %s\n", es);
  378. va_end(args_copy);
  379. }
  380. #ifdef HAVE_SYSLOG
  381. if(logSyslog) {
  382. char msg[256];
  383. int n = 0;
  384. va_copy(args_copy, args);
  385. n = snnvprintf(msg, n, 256, f, args_copy);
  386. va_end(args_copy);
  387. n = snnprintf(msg, n, 256, ": ");
  388. n = snnprint_n(msg, n, 256, es, strlen (es));
  389. n = snnprintf(msg, n, 256, "\n");
  390. /* Overflow? Vanishingly unlikely; truncate at 255. */
  391. if(n < 0 || n > 256) {
  392. n = 256;
  393. msg[255] = '\0';
  394. }
  395. else
  396. msg[n] = '\0';
  397. accumulateSyslogN(type, msg, n);
  398. }
  399. #endif
  400. }
  401. }
  402. void
  403. really_do_log_n(int type, const char *s, int n)
  404. {
  405. if((type & LOGGING_MAX & logLevel) != 0) {
  406. if(logF) {
  407. fwrite(s, n, 1, logF);
  408. }
  409. #ifdef HAVE_SYSLOG
  410. if(logSyslog)
  411. accumulateSyslogN(type, s, n);
  412. #endif
  413. }
  414. }
  415. const char *
  416. scrub(const char *message)
  417. {
  418. if(scrubLogs)
  419. return "(scrubbed)";
  420. else
  421. return message;
  422. }