Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kdm / backend / policy.c
blobcd95e7e61cb0b9e1d6ad0b78cb1ed53a126db3e9
1 /*
3 Copyright 1988, 1998 The Open Group
4 Copyright 2001 Oswald Buddenhagen <ossi@kde.org>
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.
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
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.
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.
31 * xdm - display manager daemon
32 * Author: Keith Packard, MIT X Consortium
34 * policy.c. Implement site-dependent policy for XDMCP connections
37 #include "dm.h"
38 #include "dm_auth.h"
39 #include "dm_socket.h"
41 static ARRAY8 noAuthentication = { (CARD16)0, (CARD8Ptr) 0 };
43 typedef struct _XdmAuth {
44 ARRAY8 authentication;
45 ARRAY8 authorization;
46 } XdmAuthRec, *XdmAuthPtr;
48 static XdmAuthRec auth[] = {
49 #ifdef HASXDMAUTH
50 { {(CARD16)20, (CARD8 *)"XDM-AUTHENTICATION-1"},
51 {(CARD16)19, (CARD8 *)"XDM-AUTHORIZATION-1"},
53 #endif
54 { {(CARD16)0, (CARD8 *)0},
55 {(CARD16)0, (CARD8 *)0},
59 #define NumAuth as(auth)
61 ARRAY8Ptr
62 chooseAuthentication( ARRAYofARRAY8Ptr authenticationNames )
64 int i, j;
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;
74 int
75 checkAuthentication( struct protoDisplay *pdpy ATTR_UNUSED,
76 ARRAY8Ptr displayID ATTR_UNUSED,
77 ARRAY8Ptr name ATTR_UNUSED,
78 ARRAY8Ptr data ATTR_UNUSED )
80 #ifdef HASXDMAUTH
81 if (name->length && !memcmp( (char *)name->data, "XDM-AUTHENTICATION-1", 20 ))
82 return xdmcheckAuthentication( pdpy, displayID, name, data );
83 #endif
84 return True;
87 int
88 selectAuthorizationTypeIndex( ARRAY8Ptr authenticationName,
89 ARRAYofARRAY8Ptr authorizationNames )
91 int i, j;
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;
110 /*#define WILLING_INTERNAL*/
112 #ifdef WILLING_INTERNAL
113 /* Report the loadavg to chooser. Nice feature ...
115 * Wed Mar 10 1999 -- Steffen Hansen
117 static void
118 willingMsg( char *mbuf )
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];
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;
138 close( fd );
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] );
144 numcpu = 0;
146 if (!(f = fopen( "/proc/cpuinfo", "r" )))
147 return;
149 while (fGets( buf, sizeof(buf), f ) != -1) {
150 float m;
151 if (sscanf( buf, "cpu MHz : %f", &m )) {
152 numcpu++;
153 mhz = m;
157 fclose( f );
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 );
165 strncat( mbuf, buf, 256 );
167 mbuf[255] = 0;
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
181 #endif
183 /*ARGSUSED*/
185 isWilling( ARRAY8Ptr addr, CARD16 connectionType,
186 ARRAY8Ptr authenticationName ATTR_UNUSED,
187 ARRAY8Ptr status, xdmOpCode type )
189 int ret;
190 char statusBuf[256];
191 static time_t lastscan;
193 if (autoRescan && lastscan + 15 < now) {
194 lastscan = now;
195 scanAccessDatabase( False );
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;
212 if (feof( fd ) || errno != EINTR)
213 break;
215 pclose( fd );
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
227 status->length = strlen( statusBuf );
228 status->data = (CARD8Ptr) Malloc( status->length );
229 if (!status->data)
230 status->length = 0;
231 else
232 memmove( status->data, statusBuf, status->length );
233 return ret;
236 /*ARGSUSED*/
237 ARRAY8Ptr
238 isAccepting( struct sockaddr *from ATTR_UNUSED, int fromlen ATTR_UNUSED,
239 CARD16 displayNumber ATTR_UNUSED )
241 return 0;
244 /*ARGSUSED*/
246 selectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
247 ARRAYofARRAY8Ptr connectionAddresses ATTR_UNUSED )
249 int i;
252 * Select one supported connection type
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;
269 } /* for */
270 return -1;