dtlogin: Two additional coverity fixes
[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         snprintf (statusBuf, sizeof(statusBuf), "Display not authorized to connect");
168     else
169         snprintf (statusBuf, sizeof(statusBuf), "%s", WillingMsg());
170 #if 0
171         snprintf (statusBuf, sizeof(statusBuf), "host %s", localHostname());
172 #endif
173     /* enforce termination */
174     statusBuf[255] = '\0';
175
176     status->length = strlen(statusBuf);
177     status->data = (CARD8Ptr) malloc (status->length);
178     if (!status->data)
179         status->length = 0;
180     else
181         bcopy (statusBuf, (char *)status->data, status->length);
182     return ret;
183 }
184
185 ARRAY8Ptr 
186 Accept( struct sockaddr *from, int fromlen,
187 #if NeedWidePrototypes
188         int displayNumber )
189 #else
190         CARD16 displayNumber )
191 #endif /* NeedWidePrototypes */
192 {
193     return 0;
194 }
195
196 int 
197 SelectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
198                            ARRAYofARRAY8Ptr connectionAddresses )
199 {
200
201     int i;
202     
203     /*
204      *  the current selection policy is to use the first connection address
205      *  that is not the loopback address...
206      */
207      
208     /*
209      *  initialize loopback address array if not already done so...
210      *
211      */
212     if (loopbackAddress.length == 0 &&
213         XdmcpAllocARRAY8 (&loopbackAddress, 4) ) {
214
215         loopbackAddress.data[0] = 127;
216         loopbackAddress.data[1] = 0;
217         loopbackAddress.data[2] = 0;
218         loopbackAddress.data[3] = 1;
219     }
220     
221     for (i = 0; i < connectionAddresses->length; i++) {
222         if (!XdmcpARRAY8Equal (&connectionAddresses->data[i],
223                                 &loopbackAddress))
224             break;
225     }
226
227     return i;
228 }
229
230
231
232
233 /***************************************************************************
234  *
235  *  WillingMsg
236  *
237  *  Generate a message for the "Willing" status field.
238  *  
239  ***************************************************************************/
240
241 # define LINEBUFSIZE 32
242
243 static char *
244 WillingMsg( void )
245 {
246     static char retbuf[LINEBUFSIZE];
247     char        tmpbuf[LINEBUFSIZE * 8];
248     char        *cp;
249     char        tmpfilename[L_tmpnam + 1];
250     FILE        *f;
251
252
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)     */
256
257     strcpy(retbuf, "Willing to Manage");
258     
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," > ");
262
263     if ( tmpnam(tmpfilename) != (char *)NULL ) {
264
265         strcat(tmpbuf,tmpfilename);
266
267         if(-1 == system(tmpbuf)) {
268             perror(strerror(errno));
269         }
270
271         if ((f = fopen(tmpfilename,"r")) != (FILE *) NULL) {
272             fgets(tmpbuf,LINEBUFSIZE,f);
273             if ( (cp = strchr(tmpbuf,'\n')) != NULL) 
274                 *cp = '\0';
275
276             if (strlen(tmpbuf) > 10)    /* seems reasonable? */
277                 strcpy(retbuf, tmpbuf);
278
279             fclose(f);
280         }
281         
282         unlink(tmpfilename);
283     }
284
285     return (retbuf);
286 }