~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kdm/backend/policy.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright 1988, 1998  The Open Group
 
4
Copyright 2001 Oswald Buddenhagen <ossi@kde.org>
 
5
 
 
6
Permission to use, copy, modify, distribute, and sell this software and its
 
7
documentation for any purpose is hereby granted without fee, provided that
 
8
the above copyright notice appear in all copies and that both that
 
9
copyright notice and this permission notice appear in supporting
 
10
documentation.
 
11
 
 
12
The above copyright notice and this permission notice shall be included
 
13
in all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
16
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
18
IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
19
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
20
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
21
OTHER DEALINGS IN THE SOFTWARE.
 
22
 
 
23
Except as contained in this notice, the name of a copyright holder shall
 
24
not be used in advertising or otherwise to promote the sale, use or
 
25
other dealings in this Software without prior written authorization
 
26
from the copyright holder.
 
27
 
 
28
*/
 
29
 
 
30
/*
 
31
 * xdm - display manager daemon
 
32
 * Author: Keith Packard, MIT X Consortium
 
33
 *
 
34
 * policy.c.  Implement site-dependent policy for XDMCP connections
 
35
 */
 
36
 
 
37
#include "dm.h"
 
38
#include "dm_auth.h"
 
39
#include "dm_socket.h"
 
40
 
 
41
static ARRAY8 noAuthentication = { (CARD16)0, (CARD8Ptr) 0 };
 
42
 
 
43
typedef struct _XdmAuth {
 
44
    ARRAY8 authentication;
 
45
    ARRAY8 authorization;
 
46
} XdmAuthRec, *XdmAuthPtr;
 
47
 
 
48
static XdmAuthRec auth[] = {
 
49
#ifdef HASXDMAUTH
 
50
{ {(CARD16)20, (CARD8 *)"XDM-AUTHENTICATION-1"},
 
51
  {(CARD16)19, (CARD8 *)"XDM-AUTHORIZATION-1"},
 
52
},
 
53
#endif
 
54
{ {(CARD16)0, (CARD8 *)0},
 
55
  {(CARD16)0, (CARD8 *)0},
 
56
}
 
57
};
 
58
 
 
59
#define NumAuth as(auth)
 
60
 
 
61
ARRAY8Ptr
 
62
chooseAuthentication(ARRAYofARRAY8Ptr authenticationNames)
 
63
{
 
64
    int i, j;
 
65
 
 
66
    for (i = 0; i < (int)authenticationNames->length; i++)
 
67
        for (j = 0; j < NumAuth; j++)
 
68
            if (XdmcpARRAY8Equal(&authenticationNames->data[i],
 
69
                                 &auth[j].authentication))
 
70
                return &authenticationNames->data[i];
 
71
    return &noAuthentication;
 
72
}
 
73
 
 
74
int
 
75
checkAuthentication(struct protoDisplay *pdpy ATTR_UNUSED,
 
76
                    ARRAY8Ptr displayID ATTR_UNUSED,
 
77
                    ARRAY8Ptr name ATTR_UNUSED,
 
78
                    ARRAY8Ptr data ATTR_UNUSED)
 
79
{
 
80
#ifdef HASXDMAUTH
 
81
    if (name->length && !memcmp(name->data, "XDM-AUTHENTICATION-1", 20))
 
82
        return xdmcheckAuthentication(pdpy, displayID, name, data);
 
83
#endif
 
84
    return True;
 
85
}
 
86
 
 
87
int
 
88
selectAuthorizationTypeIndex(ARRAY8Ptr authenticationName,
 
89
                             ARRAYofARRAY8Ptr authorizationNames)
 
90
{
 
91
    int i, j;
 
92
 
 
93
    for (j = 0; j < NumAuth; j++)
 
94
        if (XdmcpARRAY8Equal(authenticationName,
 
95
                             &auth[j].authentication))
 
96
            break;
 
97
    if (j < NumAuth)
 
98
        for (i = 0; i < (int)authorizationNames->length; i++)
 
99
            if (XdmcpARRAY8Equal(&authorizationNames->data[i],
 
100
                                 &auth[j].authorization))
 
101
                return i;
 
102
    for (i = 0; i < (int)authorizationNames->length; i++)
 
103
        if (validAuthorization(authorizationNames->data[i].length,
 
104
                               (char *)authorizationNames->data[i].data))
 
105
            return i;
 
106
    return -1;
 
107
}
 
108
 
 
109
 
 
110
/*#define WILLING_INTERNAL*/
 
111
 
 
112
#ifdef WILLING_INTERNAL
 
113
/* Report the loadavg to chooser. Nice feature ...
 
114
 *
 
115
 * Wed Mar 10 1999 -- Steffen Hansen
 
116
 */
 
117
static void
 
118
willingMsg(char *mbuf)
 
119
{
 
120
#ifdef __linux__
 
121
    int fd;
 
122
    int numcpu;
 
123
    const char *fail_msg = "Willing to manage";
 
124
    FILE *f;
 
125
    float load[3];
 
126
    float mhz = 0.0;
 
127
    char buf[1024];
 
128
 
 
129
    fd = open("/proc/loadavg", O_RDONLY);
 
130
    if (fd == -1) {
 
131
        sprintf(mbuf, fail_msg);
 
132
        return;
 
133
    } else if (read(fd, buf, 100) < 4) {
 
134
        close(fd);
 
135
        sprintf(mbuf, fail_msg);
 
136
        return;
 
137
    }
 
138
    close(fd);
 
139
 
 
140
    sscanf(buf, "%f %f %f", &load[0], &load[1], &load[2]);
 
141
    sprintf(mbuf, "Available (load: %0.2f, %0.2f, %0.2f)",
 
142
            load[0], load[1], load[2]);
 
143
 
 
144
    numcpu = 0;
 
145
 
 
146
    if (!(f = fopen("/proc/cpuinfo", "r")))
 
147
        return;
 
148
 
 
149
    while (fGets(buf, sizeof(buf), f) != -1) {
 
150
        float m;
 
151
        if (sscanf(buf, "cpu MHz : %f", &m)) {
 
152
            numcpu++;
 
153
            mhz = m;
 
154
        }
 
155
    }
 
156
 
 
157
    fclose(f);
 
158
 
 
159
    if (numcpu) {
 
160
        if (numcpu > 1)
 
161
            sprintf(buf, " %d*%0.0f MHz", numcpu, mhz);
 
162
        else
 
163
            sprintf(buf, " %0.0f MHz", mhz);
 
164
 
 
165
        strncat(mbuf, buf, 256);
 
166
 
 
167
        mbuf[255] = 0;
 
168
    }
 
169
#elif HAVE_GETLOADAVG /* !__linux__ */
 
170
#ifdef __GNUC__
 
171
# warning This code is untested...
 
172
#endif
 
173
    double load[3];
 
174
    getloadavg(load, 3);
 
175
    sprintf(mbuf, "Available (load: %0.2f, %0.2f, %0.2f)", load[0],
 
176
            load[1], load[2]);
 
177
#else /* !__linux__ && !GETLOADAVG */
 
178
    strcpy(mbuf, "Willing to manage");
 
179
#endif
 
180
}
 
181
#endif
 
182
 
 
183
/*ARGSUSED*/
 
184
int
 
185
isWilling(ARRAY8Ptr addr, CARD16 connectionType,
 
186
          ARRAY8Ptr authenticationName ATTR_UNUSED,
 
187
          ARRAY8Ptr status, xdmOpCode type)
 
188
{
 
189
    int ret;
 
190
    char statusBuf[256];
 
191
    static time_t lastscan;
 
192
 
 
193
    if (autoRescan && lastscan + 15 < now) {
 
194
        lastscan = now;
 
195
        scanAccessDatabase(False);
 
196
    }
 
197
    ret = acceptableDisplayAddress(addr, connectionType, type);
 
198
    if (!ret) {
 
199
        sprintf(statusBuf, "Display not authorized to connect");
 
200
    } else {
 
201
        if (*willing) {
 
202
            FILE *fd;
 
203
            int len, ok = False;
 
204
            if ((fd = popen(willing, "r"))) {
 
205
                for (;;) {
 
206
                    if ((len = fGets(statusBuf, sizeof(statusBuf), fd)) != -1) {
 
207
                        if (len) {
 
208
                            ok = True;
 
209
                            break;
 
210
                        }
 
211
                    }
 
212
                    if (feof(fd) || errno != EINTR)
 
213
                        break;
 
214
                }
 
215
                pclose(fd);
 
216
            }
 
217
            if (!ok)
 
218
                sprintf(statusBuf, "Willing, but %.*s failed",
 
219
                        (int)(sizeof(statusBuf) - 21), willing);
 
220
        } else
 
221
#ifdef WILLING_INTERNAL
 
222
            willingMsg(statusBuf);
 
223
#else
 
224
            strcpy(statusBuf, "Willing to manage");
 
225
#endif
 
226
    }
 
227
    status->length = strlen(statusBuf);
 
228
    status->data = Malloc(status->length);
 
229
    if (!status->data)
 
230
        status->length = 0;
 
231
    else
 
232
        memmove(status->data, statusBuf, status->length);
 
233
    return ret;
 
234
}
 
235
 
 
236
/*ARGSUSED*/
 
237
ARRAY8Ptr
 
238
isAccepting(struct sockaddr *from ATTR_UNUSED, int fromlen ATTR_UNUSED,
 
239
            CARD16 displayNumber ATTR_UNUSED)
 
240
{
 
241
    return 0;
 
242
}
 
243
 
 
244
/*ARGSUSED*/
 
245
int
 
246
selectConnectionTypeIndex(ARRAY16Ptr  connectionTypes,
 
247
                          ARRAYofARRAY8Ptr connectionAddresses ATTR_UNUSED)
 
248
{
 
249
    int i;
 
250
 
 
251
    /*
 
252
     * Select one supported connection type
 
253
     */
 
254
 
 
255
    for (i = 0; i < connectionTypes->length; i++) {
 
256
        switch (connectionTypes->data[i]) {
 
257
        case FamilyLocal:
 
258
#if defined(TCPCONN)
 
259
        case FamilyInternet:
 
260
# if defined(IPv6) && defined(AF_INET6)
 
261
        case FamilyInternet6:
 
262
# endif /* IPv6 */
 
263
#endif /* TCPCONN */
 
264
#if defined(DNETCONN)
 
265
        case FamilyDECnet:
 
266
#endif /* DNETCONN */
 
267
            return i;
 
268
        }
 
269
    } /* for */
 
270
    return -1;
 
271
}