aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/audit/src/auditd-reconfig.c
blob: ac3bd030f8405d5eeec4751953db908e3cbd2812 (plain)
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
/* auditd-reconfig.c -- 
 * Copyright 2005 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *   Steve Grubb <sgrubb@redhat.com>
 * 
 */

#include "config.h"
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "libaudit.h"
#include "auditd-event.h"
#include "auditd-config.h"
#include "private.h"

/* This is the configuration manager code */
static pthread_t config_thread;
static pthread_mutex_t config_lock;
static void *config_thread_main(void *arg);

void init_config_manager(void)
{
	pthread_mutex_init(&config_lock, NULL);
	audit_msg(LOG_DEBUG, "config_manager init complete");
}

int start_config_manager(struct auditd_reply_list *rep)
{
	int retval, rc = 0;
	
	retval = pthread_mutex_trylock(&config_lock);
	if (retval == 0) {
		pthread_attr_t detached;

		pthread_attr_init(&detached);
		pthread_attr_setdetachstate(&detached,
			PTHREAD_CREATE_DETACHED);

	        if (pthread_create(&config_thread, &detached,
        	                config_thread_main, rep) < 0) {
                	audit_msg(LOG_ERR,
			"Couldn't create config thread, no config changes");
			free(rep);
			pthread_mutex_unlock(&config_lock);
        	        rc = 1;
	        }
		pthread_attr_destroy(&detached);
	} else {
               	audit_msg(LOG_ERR, 
			"Config thread already running, no config changes");
		free(rep);
       	        rc = 1;
	}
	return rc;
}

void shutdown_config(void)
{
        pthread_cancel(config_thread);
}

static void *config_thread_main(void *arg)
{
	sigset_t sigs;
	struct auditd_reply_list *rep = (struct auditd_reply_list *)arg;
	struct daemon_conf new_config;
	extern int send_audit_event(int type, const char *str);

	/* This is a worker thread. Don't handle signals. */
	sigemptyset(&sigs);
	sigaddset(&sigs, SIGALRM);
	sigaddset(&sigs, SIGTERM);
	sigaddset(&sigs, SIGHUP);
	sigaddset(&sigs, SIGUSR1);
	sigaddset(&sigs, SIGUSR2);
	pthread_sigmask(SIG_SETMASK, &sigs, NULL);

	if (load_config(&new_config, TEST_AUDITD) == 0) {
		/* We will re-use the current reply */
		new_config.sender_uid = rep->reply.signal_info->uid;
		new_config.sender_pid = rep->reply.signal_info->pid;
		if (rep->reply.len > 24)
			new_config.sender_ctx = 
				strdup(rep->reply.signal_info->ctx);
		else
			new_config.sender_ctx = strdup("?"); 
		memcpy(rep->reply.msg.data, &new_config, sizeof(new_config));
		rep->reply.conf = (struct daemon_conf *)rep->reply.msg.data;
		rep->reply.type = AUDIT_DAEMON_RECONFIG;
		enqueue_event(rep);
	} else {
		// need to send a failed event message
		char txt[MAX_AUDIT_MESSAGE_LENGTH];
		snprintf(txt, sizeof(txt),
	    "reconfig aborted, sending auid=%u pid=%d subj=%s res=failed",
			rep->reply.signal_info->uid,
			rep->reply.signal_info->pid,
			(rep->reply.len > 24) ? 
				rep->reply.signal_info->ctx : "?");
		send_audit_event(AUDIT_DAEMON_CONFIG, txt);
		free_config(&new_config);
		free(rep);
	}

	pthread_mutex_unlock(&config_lock);
	return NULL;		
}