~ubuntu-branches/ubuntu/gutsy/syslog-ng/gutsy-security

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***************************************************************************
 *
 * Copyright (c) 1999 Balázs Scheidler
 * Copyright (c) 1999-2001 BalaBit IT Ltd.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Inspired by nsyslog, originally written by Darren Reed.
 *
 * $Id: afuser.c,v 1.9 2003/01/31 14:26:48 bazsi Exp $
 *
 ***************************************************************************/

#include "afuser.h"
#include "destinations.h"
#include "format.h"
#include "utils.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <utmp.h>

#include "afuser.c.x"
#include "macros.h"

/* CLASS:
   (class
     (name afuser_dest)
     (super log_dest_driver)
     (vars
       (username string)
       (template_output string)
       (template_escape . int)
       (cfg object syslog_config)))
*/

void
afuser_dest_set_template(struct log_dest_driver *c, char *t)
{
	CAST(afuser_dest, self, c);
 
	self->template_output = c_format("%z", t);
}

void
afuser_dest_set_template_escape(struct log_dest_driver *c, int enable)
{
	CAST(afuser_dest, self, c);

	self->template_escape = enable;
}

static int 
do_init_afuser_dest(struct log_handler *c, 
		    struct syslog_config *cfg, 
		    struct persistent_config *persistent)
{
	CAST(afuser_dest, self, c);

	self->cfg = cfg;

	return ST_OK | ST_GOON;
}

static void
do_handle_afuser_log(struct log_handler *c, struct log_info *msg)
{
	CAST(afuser_dest, self, c);
	struct utmp *ut;
	struct ol_string *msg_line;

	if (self->template_output) {
		msg_line = c_format("%fS",
				    expand_macros(
					    self->cfg,
					    self->template_output,
					    self->template_escape, msg));
	} else {
		msg_line = c_format("%S %S %S\n",
				    msg->date, msg->host, msg->msg);
	}

	while ((ut = getutent())) {
#if HAVE_MODERN_UTMP
		if (ut->ut_type == USER_PROCESS &&
		    ((self->username->length == 1 && 
		      self->username->data[0] == '*') ||
		     (self->username->length == strlen(ut->ut_user) &&
		      memcmp(self->username->data, ut->ut_user, self->username->length) == 0))) {
#else
		if ((self->username->length == 1 && 
		     self->username->data[0] == '*') ||
		    (self->username->length == strlen(ut->ut_name) &&
		     memcmp(self->username->data, ut->ut_name, self->username->length) == 0)) {
#endif
			char line[128];
			int fd;

			strcpy(line, "/dev/");
			strcat(line, ut->ut_line);
			fd = open(line, O_NOCTTY | O_APPEND | O_WRONLY | O_NONBLOCK);
			if (fd != -1) {
				write_raw_with_poll(fd, msg_line->length, msg_line->data);
				close(fd);
			}

		}
	}
	endutent();
	ol_string_free(msg_line);
	log_info_free(msg);
}

struct log_dest_driver *make_afuser_dest(const char *username)
{
	NEW(afuser_dest, self);
	
	self->super.super.init = do_init_afuser_dest;
	self->super.super.handler = do_handle_afuser_log;
	self->username = c_format("%z", username);
	return &self->super;
}