/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- * * Copyright (C) 2006 William Jon McCann * * 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. * */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef HAVE_GETPEERUCRED #include #endif #include "ck-sysdeps.h" /* Adapted from dbus-sysdeps-unix.c:_dbus_read_credentials_socket() */ gboolean ck_get_socket_peer_credentials (int socket_fd, pid_t *pid, uid_t *uid, GError **error) { gboolean ret; uid_t uid_read; pid_t pid_read; pid_read = -1; uid_read = -1; ret = FALSE; #ifdef SO_PEERCRED struct ucred cr; socklen_t cr_len; cr_len = sizeof (cr); if (getsockopt (socket_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 && cr_len == sizeof (cr)) { pid_read = cr.pid; uid_read = cr.uid; ret = TRUE; } else { g_warning ("Failed to getsockopt() credentials, returned len %d/%d: %s\n", cr_len, (int) sizeof (cr), g_strerror (errno)); } #elif defined(HAVE_GETPEERUCRED) ucred_t *ucred; ucred = NULL; if (getpeerucred (socket_fd, &ucred) == 0) { pid_read = ucred_getpid (ucred); uid_read = ucred_geteuid (ucred); ret = TRUE; } else { g_warning ("Failed to getpeerucred() credentials: %s\n", g_strerror (errno)); } if (ucred != NULL) { ucred_free (ucred); } #else /* !SO_PEERCRED && !HAVE_GETPEERUCRED */ g_warning ("Socket credentials not supported on this OS\n"); #endif if (pid != NULL) { *pid = pid_read; } if (uid != NULL) { *uid = uid_read; } return ret; }