2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* (c) Copyright 1997 The Open Group */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
31 * xdm - display manager daemon
33 * $TOG: policy.c /main/6 1997/03/14 13:45:03 barstow $
35 * Copyright 1988 Massachusetts Institute of Technology
37 * Permission to use, copy, modify, and distribute this software and its
38 * documentation for any purpose and without fee is hereby granted, provided
39 * that the above copyright notice appear in all copies and that both that
40 * copyright notice and this permission notice appear in supporting
41 * documentation, and that the name of M.I.T. not be used in advertising or
42 * publicity pertaining to distribution of the software without specific,
43 * written prior permission. M.I.T. makes no representations about the
44 * suitability of this software for any purpose. It is provided "as is"
45 * without express or implied warranty.
47 * Author: Keith Packard, MIT X Consortium
51 * policy.c. Implement site-dependent policy for XDMCP connections
56 static ARRAY8 noAuthentication = { (CARD16) 0, (CARD8Ptr) 0 };
57 static ARRAY8 loopbackAddress = { (CARD16) 0, (CARD8Ptr) 0 };
59 typedef struct _XdmAuth {
60 ARRAY8 authentication;
62 } XdmAuthRec, *XdmAuthPtr;
66 { {(CARD16) 20, (CARD8 *) "XDM-AUTHENTICATION-1"},
67 {(CARD16) 19, (CARD8 *) "XDM-AUTHORIZATION-1"},
70 { {(CARD16) 0, (CARD8 *) 0},
71 {(CARD16) 0, (CARD8 *) 0},
75 #define NumAuth (sizeof auth / sizeof auth[0])
79 /***************************************************************************
81 * Local procedure declarations
83 ***************************************************************************/
85 static char * WillingMsg( void ) ;
88 /******** End Local Function Declarations ********/
95 ChooseAuthentication( ARRAYofARRAY8Ptr authenticationNames )
99 for (i = 0; i < authenticationNames->length; i++)
100 for (j = 0; j < NumAuth; j++)
101 if (XdmcpARRAY8Equal (&authenticationNames->data[i],
102 &auth[j].authentication))
103 return &authenticationNames->data[i];
104 return &noAuthentication;
108 CheckAuthentication( struct protoDisplay *pdpy, ARRAY8Ptr displayID,
109 ARRAY8Ptr name, ARRAY8Ptr data )
112 if (name->length && !strncmp (name->data, "XDM-AUTHENTICATION-1", 20))
113 return XdmCheckAuthentication (pdpy, displayID, name, data);
119 SelectAuthorizationTypeIndex( ARRAY8Ptr authenticationName,
120 ARRAYofARRAY8Ptr authorizationNames )
124 for (j = 0; j < NumAuth; j++)
125 if (XdmcpARRAY8Equal (authenticationName,
126 &auth[j].authentication))
130 for (i = 0; i < authorizationNames->length; i++)
131 if (XdmcpARRAY8Equal (&authorizationNames->data[i],
132 &auth[j].authorization))
135 for (i = 0; i < authorizationNames->length; i++)
136 if (ValidAuthorization (authorizationNames->data[i].length,
137 (char *) authorizationNames->data[i].data))
144 Willing( struct sockaddr *addr, int addrlen, ARRAY8Ptr authenticationName,
151 #if NeedWidePrototypes
154 CARD16 connectionType,
155 #endif /* NeedWidePrototypes */
156 ARRAY8Ptr authenticationName,
160 static char statusBuf[256];
163 extern char *localHostname ();
165 ret = AcceptableDisplayAddress (addr, connectionType, type);
167 snprintf (statusBuf, sizeof(statusBuf), "Display not authorized to connect");
169 snprintf (statusBuf, sizeof(statusBuf), "%s", WillingMsg());
171 snprintf (statusBuf, sizeof(statusBuf), "host %s", localHostname());
173 /* enforce termination */
174 statusBuf[255] = '\0';
176 status->length = strlen(statusBuf);
177 status->data = (CARD8Ptr) malloc (status->length);
181 bcopy (statusBuf, (char *)status->data, status->length);
186 Accept( struct sockaddr *from, int fromlen,
187 #if NeedWidePrototypes
190 CARD16 displayNumber )
191 #endif /* NeedWidePrototypes */
197 SelectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
198 ARRAYofARRAY8Ptr connectionAddresses )
204 * the current selection policy is to use the first connection address
205 * that is not the loopback address...
209 * initialize loopback address array if not already done so...
212 if (loopbackAddress.length == 0 &&
213 XdmcpAllocARRAY8 (&loopbackAddress, 4) ) {
215 loopbackAddress.data[0] = 127;
216 loopbackAddress.data[1] = 0;
217 loopbackAddress.data[2] = 0;
218 loopbackAddress.data[3] = 1;
221 for (i = 0; i < connectionAddresses->length; i++) {
222 if (!XdmcpARRAY8Equal (&connectionAddresses->data[i],
233 /***************************************************************************
237 * Generate a message for the "Willing" status field.
239 ***************************************************************************/
241 # define LINEBUFSIZE 32
246 static char retbuf[LINEBUFSIZE];
247 char tmpbuf[LINEBUFSIZE * 8];
249 char tmpfilename[L_tmpnam + 1];
253 /* Return selected part from an 'uptime' to Server */
254 /* for use in hosts status field when XDMCP broadcast is used */
255 /* (useful for selecting host to be managed by) */
257 strcpy(retbuf, "Willing to Manage");
259 strcpy(tmpbuf,"uptime | ");
260 strcat(tmpbuf,"awk '{printf(\"%s %-.5s load: %.3s, %.3s, %.3s\",$(NF-6),$(NF-5),$(NF-2),$(NF-1),$NF)}'");
261 strcat(tmpbuf," > ");
263 if ( tmpnam(tmpfilename) != (char *)NULL ) {
265 strcat(tmpbuf,tmpfilename);
267 if(-1 == system(tmpbuf)) {
268 perror(strerror(errno));
271 if ((f = fopen(tmpfilename,"r")) != (FILE *) NULL) {
272 fgets(tmpbuf,LINEBUFSIZE,f);
273 if ( (cp = strchr(tmpbuf,'\n')) != NULL)
276 if (strlen(tmpbuf) > 10) /* seems reasonable? */
277 strcpy(retbuf, tmpbuf);