summaryrefslogtreecommitdiffstats
path: root/kdesu/kdesud/secure.cpp
blob: 9b30ab4eef4e05a6bfb2c1bb3688261a4dbf7ce9 (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
/* vi: ts=8 sts=4 sw=4
 *
 * This file is part of the KDE project, module tdesu.
 * Copyright (C) 1999,2000 Geert Jansen <g.t.jansen@stud.tue.nl>
 *
 * secure.cpp: Peer credentials for a UNIX socket.
 */

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include <kdebug.h>
#include <ksockaddr.h>
#include "secure.h"


/**
 * Under Linux, Socket_security is supported.
 */

#if defined(SO_PEERCRED)

SocketSecurity::SocketSecurity(int sockfd)
{
    ksocklen_t len = sizeof(struct ucred);
    if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) {
	kdError() << "getsockopt(SO_PEERCRED) " << perror << endl;
	return;
    }

    ok = true;
}

#else
# if defined(HAVE_GETPEEREID)
SocketSecurity::SocketSecurity(int sockfd)
{
    uid_t euid;
    gid_t egid;
    if (getpeereid(sockfd, &euid, &egid) == 0) {
	cred.uid = euid;
	cred.gid = egid;
	cred.pid = -1;
	ok = true;
    }
}

# else


/**
 * The default version does nothing.
 */

SocketSecurity::SocketSecurity(int sockfd)
{
    static bool warned_him = FALSE;

    if (!warned_him) {
        kdWarning() << "Using void socket security. Please add support for your" << endl;
        kdWarning() << "platform to tdesu/tdesud/secure.cpp" << endl;
        warned_him = TRUE;
    }

    // This passes the test made in handler.cpp
    cred.uid = getuid();
    ok = true;
}

# endif
#endif