Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtlogin / policy.c
1 /* (c) Copyright 1997 The Open Group */
2 /*                                                                      *
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.                                *
7  */
8 /*
9  * xdm - display manager daemon
10  *
11  * $TOG: policy.c /main/6 1997/03/14 13:45:03 barstow $
12  *
13  * Copyright 1988 Massachusetts Institute of Technology
14  *
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.
24  *
25  * Author:  Keith Packard, MIT X Consortium
26  */
27
28 /*
29  * policy.c.  Implement site-dependent policy for XDMCP connections
30  */
31
32 # include "dm.h"
33
34 static ARRAY8 noAuthentication = { (CARD16) 0, (CARD8Ptr) 0 };
35 static ARRAY8 loopbackAddress  = { (CARD16) 0, (CARD8Ptr) 0 };
36
37 typedef struct _XdmAuth {
38     ARRAY8  authentication;
39     ARRAY8  authorization;
40 } XdmAuthRec, *XdmAuthPtr;
41
42 XdmAuthRec auth[] = {
43 #ifdef HASXDMAUTH
44 { {(CARD16) 20, (CARD8 *) "XDM-AUTHENTICATION-1"},
45   {(CARD16) 19, (CARD8 *) "XDM-AUTHORIZATION-1"},
46 },
47 #endif
48 { {(CARD16) 0, (CARD8 *) 0},
49   {(CARD16) 0, (CARD8 *) 0},
50 }
51 };
52
53 #define NumAuth (sizeof auth / sizeof auth[0])
54
55
56
57 /***************************************************************************
58  *
59  *  Local procedure declarations
60  *
61  ***************************************************************************/
62
63 static char * WillingMsg( void ) ;
64
65
66 /********    End Local Function Declarations    ********/
67
68
69
70
71
72 ARRAY8Ptr 
73 ChooseAuthentication( ARRAYofARRAY8Ptr authenticationNames )
74 {
75     int i, j;
76
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;
83 }
84
85 int 
86 CheckAuthentication( struct protoDisplay *pdpy, ARRAY8Ptr displayID,
87                      ARRAY8Ptr name, ARRAY8Ptr data )
88 {
89 #ifdef HASXDMAUTH
90     if (name->length && !strncmp (name->data, "XDM-AUTHENTICATION-1", 20))
91         return XdmCheckAuthentication (pdpy, displayID, name, data);
92 #endif
93     return TRUE;
94 }
95
96 int 
97 SelectAuthorizationTypeIndex( ARRAY8Ptr authenticationName,
98                               ARRAYofARRAY8Ptr authorizationNames )
99 {
100     int i, j;
101
102     for (j = 0; j < NumAuth; j++)
103         if (XdmcpARRAY8Equal (authenticationName,
104                               &auth[j].authentication))
105             break;
106     if (j < NumAuth)
107     {
108         for (i = 0; i < authorizationNames->length; i++)
109             if (XdmcpARRAY8Equal (&authorizationNames->data[i],
110                                   &auth[j].authorization))
111                 return i;
112     }
113     for (i = 0; i < authorizationNames->length; i++)
114         if (ValidAuthorization (authorizationNames->data[i].length,
115                                 (char *) authorizationNames->data[i].data))
116             return i;
117     return -1;
118 }
119
120 #if 0
121 int 
122 Willing( struct sockaddr *addr, int addrlen, ARRAY8Ptr authenticationName,
123          ARRAY8Ptr status )
124 #endif
125
126 int 
127 Willing(
128         ARRAY8Ptr addr,
129 #if NeedWidePrototypes
130         int connectionType,
131 #else
132         CARD16 connectionType,
133 #endif /* NeedWidePrototypes */
134         ARRAY8Ptr authenticationName,
135         ARRAY8Ptr status,
136         xdmOpCode type )
137 {
138     static char statusBuf[256];
139     int         ret;
140 #if 0
141     extern char *localHostname ();
142 #endif
143     ret = AcceptableDisplayAddress (addr, connectionType, type);
144     if (!ret)
145         sprintf (statusBuf, "Display not authorized to connect");
146     else
147         sprintf (statusBuf, "%s", WillingMsg());
148 #if 0
149         sprintf (statusBuf, "host %s", localHostname());
150 #endif
151
152     status->length = strlen (statusBuf);
153     status->data = (CARD8Ptr) malloc (status->length);
154     if (!status->data)
155         status->length = 0;
156     else
157         bcopy (statusBuf, (char *)status->data, status->length);
158     return ret;
159 }
160
161 ARRAY8Ptr 
162 Accept( struct sockaddr *from, int fromlen,
163 #if NeedWidePrototypes
164         int displayNumber )
165 #else
166         CARD16 displayNumber )
167 #endif /* NeedWidePrototypes */
168 {
169     return 0;
170 }
171
172 int 
173 SelectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
174                            ARRAYofARRAY8Ptr connectionAddresses )
175 {
176
177     int i;
178     
179     /*
180      *  the current selection policy is to use the first connection address
181      *  that is not the loopback address...
182      */
183      
184     /*
185      *  initialize loopback address array if not already done so...
186      *
187      */
188     if (loopbackAddress.length == 0 &&
189         XdmcpAllocARRAY8 (&loopbackAddress, 4) ) {
190
191         loopbackAddress.data[0] = 127;
192         loopbackAddress.data[1] = 0;
193         loopbackAddress.data[2] = 0;
194         loopbackAddress.data[3] = 1;
195     }
196     
197     for (i = 0; i < connectionAddresses->length; i++) {
198         if (!XdmcpARRAY8Equal (&connectionAddresses->data[i],
199                                 &loopbackAddress))
200             break;
201     }
202
203     return i;
204 }
205
206
207
208
209 /***************************************************************************
210  *
211  *  WillingMsg
212  *
213  *  Generate a message for the "Willing" status field.
214  *  
215  ***************************************************************************/
216
217 # define LINEBUFSIZE 32
218
219 static char *
220 WillingMsg( void )
221 {
222     static char retbuf[LINEBUFSIZE];
223     char        tmpbuf[LINEBUFSIZE * 8];
224     char        *cp;
225     char        tmpfilename[L_tmpnam + 1];
226     FILE        *f;
227
228
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)     */
232
233     strcpy(retbuf, "Willing to Manage");
234     
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," > ");
238
239     if ( tmpnam(tmpfilename) != (char *)NULL ) {
240
241         strcat(tmpbuf,tmpfilename);
242
243         system(tmpbuf);
244
245         if ((f = fopen(tmpfilename,"r")) != (FILE *) NULL) {
246             fgets(tmpbuf,LINEBUFSIZE,f);
247             if ( (cp = strchr(tmpbuf,'\n')) != NULL) 
248                 *cp = '\0';
249
250             if (strlen(tmpbuf) > 10)    /* seems reasonable? */
251                 strcpy(retbuf, tmpbuf);
252
253             fclose(f);
254         }
255         
256         unlink(tmpfilename);
257     }
258
259     return (retbuf);
260 }