dtlogin: Resolve 27 compiler warnings
[oweals/cde.git] / cde / programs / dtlogin / policy.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* (c) Copyright 1997 The Open Group */
24 /*                                                                      *
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.                                *
29  */
30 /*
31  * xdm - display manager daemon
32  *
33  * $TOG: policy.c /main/6 1997/03/14 13:45:03 barstow $
34  *
35  * Copyright 1988 Massachusetts Institute of Technology
36  *
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.
46  *
47  * Author:  Keith Packard, MIT X Consortium
48  */
49
50 /*
51  * policy.c.  Implement site-dependent policy for XDMCP connections
52  */
53
54 # include "dm.h"
55
56 static ARRAY8 noAuthentication = { (CARD16) 0, (CARD8Ptr) 0 };
57 static ARRAY8 loopbackAddress  = { (CARD16) 0, (CARD8Ptr) 0 };
58
59 typedef struct _XdmAuth {
60     ARRAY8  authentication;
61     ARRAY8  authorization;
62 } XdmAuthRec, *XdmAuthPtr;
63
64 XdmAuthRec auth[] = {
65 #ifdef HASXDMAUTH
66 { {(CARD16) 20, (CARD8 *) "XDM-AUTHENTICATION-1"},
67   {(CARD16) 19, (CARD8 *) "XDM-AUTHORIZATION-1"},
68 },
69 #endif
70 { {(CARD16) 0, (CARD8 *) 0},
71   {(CARD16) 0, (CARD8 *) 0},
72 }
73 };
74
75 #define NumAuth (sizeof auth / sizeof auth[0])
76
77
78
79 /***************************************************************************
80  *
81  *  Local procedure declarations
82  *
83  ***************************************************************************/
84
85 static char * WillingMsg( void ) ;
86
87
88 /********    End Local Function Declarations    ********/
89
90
91
92
93
94 ARRAY8Ptr 
95 ChooseAuthentication( ARRAYofARRAY8Ptr authenticationNames )
96 {
97     int i, j;
98
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;
105 }
106
107 int 
108 CheckAuthentication( struct protoDisplay *pdpy, ARRAY8Ptr displayID,
109                      ARRAY8Ptr name, ARRAY8Ptr data )
110 {
111 #ifdef HASXDMAUTH
112     if (name->length && !strncmp (name->data, "XDM-AUTHENTICATION-1", 20))
113         return XdmCheckAuthentication (pdpy, displayID, name, data);
114 #endif
115     return TRUE;
116 }
117
118 int 
119 SelectAuthorizationTypeIndex( ARRAY8Ptr authenticationName,
120                               ARRAYofARRAY8Ptr authorizationNames )
121 {
122     int i, j;
123
124     for (j = 0; j < NumAuth; j++)
125         if (XdmcpARRAY8Equal (authenticationName,
126                               &auth[j].authentication))
127             break;
128     if (j < NumAuth)
129     {
130         for (i = 0; i < authorizationNames->length; i++)
131             if (XdmcpARRAY8Equal (&authorizationNames->data[i],
132                                   &auth[j].authorization))
133                 return i;
134     }
135     for (i = 0; i < authorizationNames->length; i++)
136         if (ValidAuthorization (authorizationNames->data[i].length,
137                                 (char *) authorizationNames->data[i].data))
138             return i;
139     return -1;
140 }
141
142 #if 0
143 int 
144 Willing( struct sockaddr *addr, int addrlen, ARRAY8Ptr authenticationName,
145          ARRAY8Ptr status )
146 #endif
147
148 int 
149 Willing(
150         ARRAY8Ptr addr,
151 #if NeedWidePrototypes
152         int connectionType,
153 #else
154         CARD16 connectionType,
155 #endif /* NeedWidePrototypes */
156         ARRAY8Ptr authenticationName,
157         ARRAY8Ptr status,
158         xdmOpCode type )
159 {
160     static char statusBuf[256];
161     int         ret;
162 #if 0
163     extern char *localHostname ();
164 #endif
165     ret = AcceptableDisplayAddress (addr, connectionType, type);
166     if (!ret)
167         sprintf (statusBuf, "Display not authorized to connect");
168     else
169         sprintf (statusBuf, "%s", WillingMsg());
170 #if 0
171         sprintf (statusBuf, "host %s", localHostname());
172 #endif
173
174     status->length = strlen (statusBuf);
175     status->data = (CARD8Ptr) malloc (status->length);
176     if (!status->data)
177         status->length = 0;
178     else
179         bcopy (statusBuf, (char *)status->data, status->length);
180     return ret;
181 }
182
183 ARRAY8Ptr 
184 Accept( struct sockaddr *from, int fromlen,
185 #if NeedWidePrototypes
186         int displayNumber )
187 #else
188         CARD16 displayNumber )
189 #endif /* NeedWidePrototypes */
190 {
191     return 0;
192 }
193
194 int 
195 SelectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
196                            ARRAYofARRAY8Ptr connectionAddresses )
197 {
198
199     int i;
200     
201     /*
202      *  the current selection policy is to use the first connection address
203      *  that is not the loopback address...
204      */
205      
206     /*
207      *  initialize loopback address array if not already done so...
208      *
209      */
210     if (loopbackAddress.length == 0 &&
211         XdmcpAllocARRAY8 (&loopbackAddress, 4) ) {
212
213         loopbackAddress.data[0] = 127;
214         loopbackAddress.data[1] = 0;
215         loopbackAddress.data[2] = 0;
216         loopbackAddress.data[3] = 1;
217     }
218     
219     for (i = 0; i < connectionAddresses->length; i++) {
220         if (!XdmcpARRAY8Equal (&connectionAddresses->data[i],
221                                 &loopbackAddress))
222             break;
223     }
224
225     return i;
226 }
227
228
229
230
231 /***************************************************************************
232  *
233  *  WillingMsg
234  *
235  *  Generate a message for the "Willing" status field.
236  *  
237  ***************************************************************************/
238
239 # define LINEBUFSIZE 32
240
241 static char *
242 WillingMsg( void )
243 {
244     static char retbuf[LINEBUFSIZE];
245     char        tmpbuf[LINEBUFSIZE * 8];
246     char        *cp;
247     char        tmpfilename[L_tmpnam + 1];
248     FILE        *f;
249
250
251     /* Return selected part from an 'uptime' to Server  */
252     /* for use in hosts status field when XDMCP broadcast is used */
253     /* (useful for selecting host to be managed by)     */
254
255     strcpy(retbuf, "Willing to Manage");
256     
257     strcpy(tmpbuf,"uptime | ");
258     strcat(tmpbuf,"awk '{printf(\"%s %-.5s  load: %.3s, %.3s, %.3s\",$(NF-6),$(NF-5),$(NF-2),$(NF-1),$NF)}'");
259     strcat(tmpbuf," > ");
260
261     if ( tmpnam(tmpfilename) != (char *)NULL ) {
262
263         strcat(tmpbuf,tmpfilename);
264
265         if(-1 == system(tmpbuf)) {
266             perror(strerror(errno));
267         }
268
269         if ((f = fopen(tmpfilename,"r")) != (FILE *) NULL) {
270             fgets(tmpbuf,LINEBUFSIZE,f);
271             if ( (cp = strchr(tmpbuf,'\n')) != NULL) 
272                 *cp = '\0';
273
274             if (strlen(tmpbuf) > 10)    /* seems reasonable? */
275                 strcpy(retbuf, tmpbuf);
276
277             fclose(f);
278         }
279         
280         unlink(tmpfilename);
281     }
282
283     return (retbuf);
284 }