/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ /* egg-unix-credentials.c - write and read unix credentials on socket Copyright (C) 2003 Red Hat, Inc Gnome keyring 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. Gnome keyring 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. Author: Alexander Larsson Author: Stef Walter */ #include "config.h" #include "egg-unix-credentials.h" #include #include #include #include #include #include #include #if defined(HAVE_GETPEERUCRED) #include #endif int egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid) { struct msghdr msg; struct iovec iov; char buf; int ret; #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS) /* Prefer CMSGCRED over LOCAL_CREDS because the former provides the * remote PID. */ #if defined(HAVE_CMSGCRED) struct cmsgcred *cred; const size_t cmsglen = CMSG_LEN (sizeof (struct cmsgcred)); const size_t cmsgspace = CMSG_SPACE (sizeof (struct cmsgcred)); #else /* defined(LOCAL_CREDS) */ struct sockcred *cred; const size_t cmsglen = CMSG_LEN (sizeof (struct sockcred)); const size_t cmsgspace = CMSG_SPACE (sizeof (struct sockcred)); #endif union { struct cmsghdr hdr; char cred[cmsgspace]; } cmsg; #endif *pid = 0; *uid = 0; /* If LOCAL_CREDS are used in this platform, they have already been * initialized by init_connection prior to sending of the credentials * byte we receive below. */ iov.iov_base = &buf; iov.iov_len = 1; memset (&msg, 0, sizeof (msg)); msg.msg_iov = &iov; msg.msg_iovlen = 1; #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS) memset (&cmsg, 0, sizeof (cmsg)); msg.msg_control = (caddr_t) &cmsg; msg.msg_controllen = cmsgspace; #endif again: ret = recvmsg (sock, &msg, 0); if (ret < 0) { if (errno == EINTR) goto again; return -1; } else if (ret == 0) { /* Disconnected */ return -1; } if (buf != '\0') { fprintf (stderr, "credentials byte was not nul\n"); return -1; } #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS) if (cmsg.hdr.cmsg_len < cmsglen || cmsg.hdr.cmsg_type != SCM_CREDS) { fprintf (stderr, "message from recvmsg() was not SCM_CREDS\n"); return -1; } #endif { #ifdef SO_PEERCRED struct ucred cr; socklen_t cr_len = sizeof (cr); if (getsockopt (sock, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 && cr_len == sizeof (cr)) { *pid = cr.pid; *uid = cr.uid; } else { fprintf (stderr, "failed to getsockopt() credentials, returned len %d/%d\n", cr_len, (int) sizeof (cr)); return -1; } #elif defined(HAVE_CMSGCRED) cred = (struct cmsgcred *) CMSG_DATA (&cmsg.hdr); *pid = cred->cmcred_pid; *uid = cred->cmcred_euid; #elif defined(LOCAL_CREDS) cred = (struct sockcred *) CMSG_DATA (&cmsg.hdr); *pid = 0; *uid = cred->sc_euid; set_local_creds(sock, 0); #elif defined(HAVE_GETPEEREID) /* OpenBSD */ uid_t euid; gid_t egid; *pid = 0; if (getpeereid (sock, &euid, &egid) == 0) { *uid = euid; } else { fprintf (stderr, "getpeereid() failed: %s\n", strerror (errno)); return -1; } #elif defined(HAVE_GETPEERUCRED) ucred_t *uc = NULL; if (getpeerucred (sock, &uc) == 0) { *pid = ucred_getpid (uc); *uid = ucred_geteuid (uc); ucred_free (uc); } else { fprintf (stderr, "getpeerucred() failed: %s\n", strerror (errno)); return -1; } #else /* !SO_PEERCRED && !HAVE_CMSGCRED */ fprintf (stderr, "socket credentials not supported on this OS\n"); return -1; #endif } return 0; } int egg_unix_credentials_write (int socket) { char buf; int bytes_written; #if defined(HAVE_CMSGCRED) && (!defined(LOCAL_CREDS) || defined(__FreeBSD__)) union { struct cmsghdr hdr; char cred[CMSG_SPACE (sizeof (struct cmsgcred))]; } cmsg; struct iovec iov; struct msghdr msg; #endif buf = 0; #if defined(HAVE_CMSGCRED) && (!defined(LOCAL_CREDS) || defined(__FreeBSD__)) iov.iov_base = &buf; iov.iov_len = 1; memset (&msg, 0, sizeof (msg)); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = (caddr_t) &cmsg; msg.msg_controllen = CMSG_SPACE (sizeof (struct cmsgcred)); memset (&cmsg, 0, sizeof (cmsg)); cmsg.hdr.cmsg_len = CMSG_LEN (sizeof (struct cmsgcred)); cmsg.hdr.cmsg_level = SOL_SOCKET; cmsg.hdr.cmsg_type = SCM_CREDS; #endif again: #if defined(HAVE_CMSGCRED) && (!defined(LOCAL_CREDS) || defined(__FreeBSD__)) bytes_written = sendmsg (socket, &msg, 0); #else bytes_written = write (socket, &buf, 1); #endif if (bytes_written < 0 && errno == EINTR) goto again; if (bytes_written <= 0) return -1; return 0; }