Coverage Report

Created: 2022-02-19 20:31

/src/php-src/main/php_syslog.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | http://www.php.net/license/3_01.txt                                  |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Author: Philip Prindeville <philipp@redfish-solutions.com>           |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include <stdio.h>
18
#include <string.h>
19
#include <stdlib.h>
20
#include "php.h"
21
#include "php_syslog.h"
22
23
#include "zend.h"
24
#include "zend_smart_string.h"
25
26
/*
27
 * The SCO OpenServer 5 Development System (not the UDK)
28
 * defines syslog to std_syslog.
29
 */
30
31
#ifdef HAVE_STD_SYSLOG
32
#define syslog std_syslog
33
#endif
34
35
#ifdef PHP_WIN32
36
PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
37
{
38
  va_list args;
39
40
  /*
41
   * don't rely on openlog() being called by syslog() if it's
42
   * not already been done; call it ourselves and pass the
43
   * correct parameters!
44
   */
45
  if (!PG(have_called_openlog)) {
46
    php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
47
  }
48
49
  va_start(args, format);
50
  vsyslog(priority, format, args);
51
  va_end(args);
52
}
53
/* }}} */
54
#else
55
PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
56
164
{
57
164
  const char *ptr;
58
164
  unsigned char c;
59
164
  smart_string fbuf = {0};
60
164
  smart_string sbuf = {0};
61
164
  va_list args;
62
63
  /*
64
   * don't rely on openlog() being called by syslog() if it's
65
   * not already been done; call it ourselves and pass the
66
   * correct parameters!
67
   */
68
164
  if (!PG(have_called_openlog)) {
69
38
    php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
70
38
  }
71
72
164
  va_start(args, format);
73
164
  zend_printf_to_smart_string(&fbuf, format, args);
74
164
  smart_string_0(&fbuf);
75
164
  va_end(args);
76
77
164
  if (PG(syslog_filter) == PHP_SYSLOG_FILTER_RAW) {
78
    /* Just send it directly to the syslog */
79
0
    syslog(priority, "%.*s", (int)fbuf.len, fbuf.c);
80
0
    smart_string_free(&fbuf);
81
0
    return;
82
0
  }
83
84
924
  for (ptr = fbuf.c; ; ++ptr) {
85
924
    c = *ptr;
86
924
    if (c == '\0') {
87
164
      syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
88
164
      break;
89
164
    }
90
91
    /* check for NVT ASCII only unless test disabled */
92
760
    if (((0x20 <= c) && (c <= 0x7e)))
93
760
      smart_string_appendc(&sbuf, c);
94
0
    else if ((c >= 0x80) && (PG(syslog_filter) != PHP_SYSLOG_FILTER_ASCII))
95
0
      smart_string_appendc(&sbuf, c);
96
0
    else if (c == '\n') {
97
0
      syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
98
0
      smart_string_reset(&sbuf);
99
0
    } else if ((c < 0x20) && (PG(syslog_filter) == PHP_SYSLOG_FILTER_ALL))
100
0
      smart_string_appendc(&sbuf, c);
101
0
    else {
102
0
      const char xdigits[] = "0123456789abcdef";
103
104
0
      smart_string_appendl(&sbuf, "\\x", 2);
105
0
      smart_string_appendc(&sbuf, xdigits[(c / 0x10)]);
106
0
      c &= 0x0f;
107
0
      smart_string_appendc(&sbuf, xdigits[c]);
108
0
    }
109
760
  }
110
111
164
  smart_string_free(&fbuf);
112
164
  smart_string_free(&sbuf);
113
164
}
114
/* }}} */
115
#endif