1 /* (c) Copyright 1997 The Open Group */
3 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
4 * (c) Copyright 1993, 1994 International Business Machines Corp. *
5 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
6 * (c) Copyright 1993, 1994 Novell, Inc. *
9 * xdm - display manager daemon
11 * $TOG: policy.c /main/6 1997/03/14 13:45:03 barstow $
13 * Copyright 1988 Massachusetts Institute of Technology
15 * Permission to use, copy, modify, and distribute this software and its
16 * documentation for any purpose and without fee is hereby granted, provided
17 * that the above copyright notice appear in all copies and that both that
18 * copyright notice and this permission notice appear in supporting
19 * documentation, and that the name of M.I.T. not be used in advertising or
20 * publicity pertaining to distribution of the software without specific,
21 * written prior permission. M.I.T. makes no representations about the
22 * suitability of this software for any purpose. It is provided "as is"
23 * without express or implied warranty.
25 * Author: Keith Packard, MIT X Consortium
29 * policy.c. Implement site-dependent policy for XDMCP connections
34 static ARRAY8 noAuthentication = { (CARD16) 0, (CARD8Ptr) 0 };
35 static ARRAY8 loopbackAddress = { (CARD16) 0, (CARD8Ptr) 0 };
37 typedef struct _XdmAuth {
38 ARRAY8 authentication;
40 } XdmAuthRec, *XdmAuthPtr;
44 { {(CARD16) 20, (CARD8 *) "XDM-AUTHENTICATION-1"},
45 {(CARD16) 19, (CARD8 *) "XDM-AUTHORIZATION-1"},
48 { {(CARD16) 0, (CARD8 *) 0},
49 {(CARD16) 0, (CARD8 *) 0},
53 #define NumAuth (sizeof auth / sizeof auth[0])
57 /***************************************************************************
59 * Local procedure declarations
61 ***************************************************************************/
63 static char * WillingMsg( void ) ;
66 /******** End Local Function Declarations ********/
73 ChooseAuthentication( ARRAYofARRAY8Ptr authenticationNames )
77 for (i = 0; i < authenticationNames->length; i++)
78 for (j = 0; j < NumAuth; j++)
79 if (XdmcpARRAY8Equal (&authenticationNames->data[i],
80 &auth[j].authentication))
81 return &authenticationNames->data[i];
82 return &noAuthentication;
86 CheckAuthentication( struct protoDisplay *pdpy, ARRAY8Ptr displayID,
87 ARRAY8Ptr name, ARRAY8Ptr data )
90 if (name->length && !strncmp (name->data, "XDM-AUTHENTICATION-1", 20))
91 return XdmCheckAuthentication (pdpy, displayID, name, data);
97 SelectAuthorizationTypeIndex( ARRAY8Ptr authenticationName,
98 ARRAYofARRAY8Ptr authorizationNames )
102 for (j = 0; j < NumAuth; j++)
103 if (XdmcpARRAY8Equal (authenticationName,
104 &auth[j].authentication))
108 for (i = 0; i < authorizationNames->length; i++)
109 if (XdmcpARRAY8Equal (&authorizationNames->data[i],
110 &auth[j].authorization))
113 for (i = 0; i < authorizationNames->length; i++)
114 if (ValidAuthorization (authorizationNames->data[i].length,
115 (char *) authorizationNames->data[i].data))
122 Willing( struct sockaddr *addr, int addrlen, ARRAY8Ptr authenticationName,
129 #if NeedWidePrototypes
132 CARD16 connectionType,
133 #endif /* NeedWidePrototypes */
134 ARRAY8Ptr authenticationName,
138 static char statusBuf[256];
141 extern char *localHostname ();
143 ret = AcceptableDisplayAddress (addr, connectionType, type);
145 sprintf (statusBuf, "Display not authorized to connect");
147 sprintf (statusBuf, "%s", WillingMsg());
149 sprintf (statusBuf, "host %s", localHostname());
152 status->length = strlen (statusBuf);
153 status->data = (CARD8Ptr) malloc (status->length);
157 bcopy (statusBuf, (char *)status->data, status->length);
162 Accept( struct sockaddr *from, int fromlen,
163 #if NeedWidePrototypes
166 CARD16 displayNumber )
167 #endif /* NeedWidePrototypes */
173 SelectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
174 ARRAYofARRAY8Ptr connectionAddresses )
180 * the current selection policy is to use the first connection address
181 * that is not the loopback address...
185 * initialize loopback address array if not already done so...
188 if (loopbackAddress.length == 0 &&
189 XdmcpAllocARRAY8 (&loopbackAddress, 4) ) {
191 loopbackAddress.data[0] = 127;
192 loopbackAddress.data[1] = 0;
193 loopbackAddress.data[2] = 0;
194 loopbackAddress.data[3] = 1;
197 for (i = 0; i < connectionAddresses->length; i++) {
198 if (!XdmcpARRAY8Equal (&connectionAddresses->data[i],
209 /***************************************************************************
213 * Generate a message for the "Willing" status field.
215 ***************************************************************************/
217 # define LINEBUFSIZE 32
222 static char retbuf[LINEBUFSIZE];
223 char tmpbuf[LINEBUFSIZE * 8];
225 char tmpfilename[L_tmpnam + 1];
229 /* Return selected part from an 'uptime' to Server */
230 /* for use in hosts status field when XDMCP broadcast is used */
231 /* (useful for selecting host to be managed by) */
233 strcpy(retbuf, "Willing to Manage");
235 strcpy(tmpbuf,"uptime | ");
236 strcat(tmpbuf,"awk '{printf(\"%s %-.5s load: %.3s, %.3s, %.3s\",$(NF-6),$(NF-5),$(NF-2),$(NF-1),$NF)}'");
237 strcat(tmpbuf," > ");
239 if ( tmpnam(tmpfilename) != (char *)NULL ) {
241 strcat(tmpbuf,tmpfilename);
245 if ((f = fopen(tmpfilename,"r")) != (FILE *) NULL) {
246 fgets(tmpbuf,LINEBUFSIZE,f);
247 if ( (cp = strchr(tmpbuf,'\n')) != NULL)
250 if (strlen(tmpbuf) > 10) /* seems reasonable? */
251 strcpy(retbuf, tmpbuf);