Blame selinux_utils.c

Packit 64c699
/*
Packit 64c699
 * Copyright Red Hat, Inc., 2003,2004.
Packit 64c699
 *
Packit 64c699
 * Redistribution and use in source and binary forms, with or without
Packit 64c699
 * modification, are permitted provided that the following conditions
Packit 64c699
 * are met:
Packit 64c699
 * 1. Redistributions of source code must retain the above copyright
Packit 64c699
 *    notice, and the entire permission notice in its entirety,
Packit 64c699
 *    including the disclaimer of warranties.
Packit 64c699
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 64c699
 *    notice, this list of conditions and the following disclaimer in the
Packit 64c699
 *    documentation and/or other materials provided with the distribution.
Packit 64c699
 * 3. The name of the author may not be used to endorse or promote
Packit 64c699
 *    products derived from this software without specific prior
Packit 64c699
 *    written permission.
Packit 64c699
 *
Packit 64c699
 * ALTERNATIVELY, this product may be distributed under the terms of
Packit 64c699
 * the GNU Public License, in which case the provisions of the GPL are
Packit 64c699
 * required INSTEAD OF the above restrictions.  (This clause is
Packit 64c699
 * necessary due to a potential bad interaction between the GPL and
Packit 64c699
 * the restrictions contained in a BSD-style copyright.)
Packit 64c699
 *
Packit 64c699
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
Packit 64c699
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 64c699
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 64c699
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
Packit 64c699
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit 64c699
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 64c699
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 64c699
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit 64c699
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 64c699
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit 64c699
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 64c699
 */
Packit 64c699
Packit 64c699
/* Written by Daniel Walsh <dwalsh@redhat.com> */
Packit 64c699
Packit 64c699
#include "selinux_utils.h"
Packit 64c699
#include <selinux/selinux.h>
Packit 64c699
#include <stdio.h>
Packit 64c699
#include <string.h>
Packit 64c699
#include <selinux/avc.h>
Packit 64c699
#include <libaudit.h>
Packit 64c699
#include <unistd.h>
Packit 64c699
#include <limits.h>
Packit 64c699
Packit 64c699
/* FD to send audit messages to */
Packit 64c699
static int audit_fd = -1;
Packit 64c699
Packit 64c699
/* log_callback stolen from dbus */
Packit 64c699
static int
Packit 64c699
log_callback (int type, const char *fmt, ...) 
Packit 64c699
{
Packit 64c699
  va_list ap;
Packit 64c699
Packit 64c699
  (void)type;
Packit 64c699
Packit 64c699
  va_start(ap, fmt);
Packit 64c699
Packit 64c699
  if (audit_fd >= 0)
Packit 64c699
  {
Packit 64c699
	  char buf[PATH_MAX*2];
Packit 64c699
    
Packit 64c699
	  vsnprintf(buf, sizeof(buf), fmt, ap);
Packit 64c699
	  audit_log_user_avc_message(audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
Packit 64c699
				     NULL, 0);
Packit 64c699
	  va_end(ap);
Packit 64c699
	  return 0;
Packit 64c699
  }
Packit 64c699
  
Packit 64c699
  vsyslog (LOG_USER | LOG_INFO, fmt, ap);
Packit 64c699
  va_end(ap);
Packit 64c699
  return 0;
Packit 64c699
}
Packit 64c699
int selinux_check_root(void) {
Packit 64c699
	int status = -1;
Packit 64c699
	security_context_t user_context;
Packit 64c699
Packit 64c699
	if (getuid() != 0) return 0;
Packit 64c699
	if (is_selinux_enabled() == 0) return 0;
Packit 64c699
	if ((status = getprevcon(&user_context)) < 0) return status;
Packit 64c699
Packit 64c699
	status = selinux_check_access(user_context, user_context, "passwd", "passwd", NULL);
Packit 64c699
Packit 64c699
	freecon(user_context);
Packit 64c699
Packit 64c699
	return status;
Packit 64c699
}
Packit 64c699
Packit 64c699
void selinux_init(int fd) {
Packit 64c699
	if (is_selinux_enabled() > 0) {
Packit 64c699
		/* initialize audit log */
Packit 64c699
Packit 64c699
		audit_fd = fd;
Packit 64c699
Packit 64c699
		/* setup callbacks */
Packit 64c699
		selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &log_callback);
Packit 64c699
	}
Packit 64c699
}