cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

syslog_client.c
Go to the documentation of this file.
1 /*
2  Copyright 2019 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #include <syslog_client.h>
26 #include <printsize.h>
27 
28 #include <cf3.defs.h>
29 
30 /*
31  * Set by cf-agent/cf-serverd from body agent/server control.
32  */
33 static char SYSLOG_HOST[MAXHOSTNAMELEN] = "localhost";
34 /*
35  * Set by cf-agent/cf-serverd from body agent/server control.
36  */
37 static uint16_t SYSLOG_PORT = 514;
38 /*
39  * Set by cf-agent/cf-serverd/cf-execd from body agent/exec/server control.
40  */
41 static int SYSLOG_FACILITY = LOG_USER;
42 
43 
44 void SetSyslogFacility(int facility)
45 {
46  SYSLOG_FACILITY = facility;
47 }
48 
49 bool SetSyslogHost(const char *host)
50 {
51  if (strlen(host) < sizeof(SYSLOG_HOST))
52  {
53  strcpy(SYSLOG_HOST, host);
54  return true;
55  }
56  else
57  {
58  return false;
59  }
60 }
61 
62 void SetSyslogPort(uint16_t port)
63 {
64  SYSLOG_PORT = port;
65 }
66 
67 void RemoteSysLog(int log_priority, const char *log_string)
68 {
69  time_t now = time(NULL);
70 
71  struct addrinfo query = { 0 }, *response = NULL;
72  char strport[PRINTSIZE(unsigned)];
73  xsnprintf(strport, sizeof(strport), "%u", (unsigned) SYSLOG_PORT);
74 
75  query.ai_family = AF_UNSPEC;
76  query.ai_socktype = SOCK_DGRAM;
77 
78  int err = getaddrinfo(SYSLOG_HOST, strport, &query, &response);
79  if (err != 0)
80  {
82  "Unable to find syslog_host or service: (%s/%s) %s",
83  SYSLOG_HOST, strport, gai_strerror(err));
84  if (response != NULL)
85  {
86  freeaddrinfo(response);
87  }
88  return;
89  }
90 
91  for (const struct addrinfo *ap = response; ap != NULL; ap = ap->ai_next)
92  {
93  /* No DNS lookup, just convert IP address to string. */
94  char txtaddr[CF_MAX_IP_LEN] = "";
95  getnameinfo(ap->ai_addr, ap->ai_addrlen,
96  txtaddr, sizeof(txtaddr),
97  NULL, 0, NI_NUMERICHOST);
99  "Connect to syslog '%s' = '%s' on port '%s'",
100  SYSLOG_HOST, txtaddr, strport);
101 
102  int sd = socket(ap->ai_family, ap->ai_socktype, IPPROTO_UDP);
103  if (sd == -1)
104  {
105  Log(LOG_LEVEL_INFO, "Couldn't open a socket. (socket: %s)", GetErrorStr());
106  continue;
107  }
108  else
109  {
110  const size_t rfc3164_len = 1024;
111  char message[rfc3164_len];
112  char timebuffer[26];
113  pid_t pid = getpid();
114 
115  snprintf(
116  message,
117  sizeof(message),
118  "<%i>%.15s %s %s[%ld]: %s",
119  (log_priority | SYSLOG_FACILITY),
120  (cf_strtimestamp_local(now, timebuffer) + 4), // Skips day str
121  VFQNAME,
122  VPREFIX,
123  (long) pid,
124  log_string);
125  err = sendto(sd, message, strlen(message),
126  0, ap->ai_addr, ap->ai_addrlen);
127  if (err == -1)
128  {
129  Log(LOG_LEVEL_VERBOSE, "Couldn't send '%s' to syslog server '%s'. (sendto: %s)",
130  message, SYSLOG_HOST, GetErrorStr());
131  }
132  else
133  {
134  Log(LOG_LEVEL_VERBOSE, "Syslog message: '%s' to server '%s'", message, SYSLOG_HOST);
135  }
136  close(sd);
137  }
138  }
139 
140  freeaddrinfo(response);
141 }
#define CF_MAX_IP_LEN
Definition: cfnet.h:39
char VFQNAME[]
Definition: cf3globals.c:58
void freeaddrinfo(struct addrinfo *res)
Definition: getaddrinfo.c:323
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hintp, struct addrinfo **res)
Definition: getaddrinfo.c:262
const char * gai_strerror(int errcode)
Definition: getaddrinfo.c:340
int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *node, socklen_t nodelen, char *service, socklen_t servicelen, int flags)
Definition: getaddrinfo.c:476
#define NULL
Definition: getopt1.c:56
char VPREFIX[1024]
Definition: logging.c:33
const char * GetErrorStr(void)
Definition: logging.c:275
void Log(LogLevel level, const char *fmt,...)
Definition: logging.c:409
@ LOG_LEVEL_VERBOSE
Definition: logging.h:46
@ LOG_LEVEL_INFO
Definition: logging.h:45
void xsnprintf(char *str, size_t str_size, const char *format,...)
Definition: misc_lib.c:114
#define PRINTSIZE(what)
Definition: printsize.h:66
char * cf_strtimestamp_local(const time_t time, char *buf)
Definition: patches.c:128
static int SYSLOG_FACILITY
Definition: syslog_client.c:41
void SetSyslogFacility(int facility)
Definition: syslog_client.c:44
static uint16_t SYSLOG_PORT
Definition: syslog_client.c:37
void SetSyslogPort(uint16_t port)
Definition: syslog_client.c:62
static char SYSLOG_HOST[MAXHOSTNAMELEN]
Definition: syslog_client.c:33
void RemoteSysLog(int log_priority, const char *log_string)
Definition: syslog_client.c:67
bool SetSyslogHost(const char *host)
Definition: syslog_client.c:49