dtlogin: Resolve all -Wformat-security warnings
[oweals/cde.git] / cde / programs / dtlogin / socket.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 /* $TOG: socket.c /main/9 1997/12/23 12:25:32 bill $ */
24 /* (c) Copyright 1997 The Open Group */
25 /*                                                                      *
26  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
27  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
28  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
29  * (c) Copyright 1993, 1994 Novell, Inc.                                *
30  */
31
32 /*
33  * @DEC_COPYRIGHT@
34  */
35 /*
36  * HISTORY
37  * $Log$
38  * Revision 1.1.2.3  1995/06/06  20:24:26  Chris_Beute
39  *      Code snapshot merge from March 15 and SIA changes
40  *      [1995/05/31  20:15:01  Chris_Beute]
41  *
42  * Revision 1.1.2.2  1995/04/21  13:05:38  Peter_Derr
43  *      dtlogin auth key fixes from deltacde
44  *      [1995/04/12  19:21:26  Peter_Derr]
45  * 
46  *      R6 xdm version used to pick up XDMCP improvements.
47  *      [1995/04/12  18:05:54  Peter_Derr]
48  * 
49  * $EndLog$
50  */
51 /*
52
53 Copyright (c) 1988  X Consortium
54
55 Permission is hereby granted, free of charge, to any person obtaining
56 a copy of this software and associated documentation files (the
57 "Software"), to deal in the Software without restriction, including
58 without limitation the rights to use, copy, modify, merge, publish,
59 distribute, sublicense, and/or sell copies of the Software, and to
60 permit persons to whom the Software is furnished to do so, subject to
61 the following conditions:
62
63 The above copyright notice and this permission notice shall be included
64 in all copies or substantial portions of the Software.
65
66 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
67 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
70 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
71 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
72 OTHER DEALINGS IN THE SOFTWARE.
73
74 Except as contained in this notice, the name of the X Consortium shall
75 not be used in advertising or otherwise to promote the sale, use or
76 other dealings in this Software without prior written authorization
77 from the X Consortium.
78
79 */
80
81 /*
82  * xdm - display manager daemon
83  * Author:  Keith Packard, MIT X Consortium
84  *
85  * socket.c - Support for BSD sockets
86  */
87
88 #include "dm.h"
89 #include "vgmsg.h"
90
91 #include <errno.h>
92 #include <sys/socket.h>
93 #include <netinet/in.h>
94 #include <sys/un.h>
95 #include <netdb.h>
96
97 #ifdef X_NOT_STDC_ENV
98 extern int errno;
99 #endif
100
101
102 extern int      xdmcpFd;
103 extern int      chooserFd;
104
105 extern FD_TYPE  WellKnownSocketsMask;
106 extern int      WellKnownSocketsMax;
107
108 CreateWellKnownSockets ()
109 {
110     struct sockaddr_in  sock_addr;
111     char                *name, *localHostname();
112
113     if (request_port == 0)
114             return;
115     Debug ("creating socket %d\n", request_port);
116     xdmcpFd = socket (AF_INET, SOCK_DGRAM, 0);
117     if (xdmcpFd == -1) {
118         LogError (ReadCatalog(MC_LOG_SET,MC_LOG_FAIL_SOCK,MC_DEF_LOG_FAIL_SOCK),
119                   request_port);
120         return;
121     }
122     name = localHostname ();
123     registerHostname (name, strlen (name));
124     RegisterCloseOnFork (xdmcpFd);
125     /* zero out the entire structure; this avoids 4.4 incompatibilities */
126     bzero ((char *) &sock_addr, sizeof (sock_addr));
127 #ifdef BSD44SOCKETS
128     sock_addr.sin_len = sizeof(sock_addr);
129 #endif
130     sock_addr.sin_family = AF_INET;
131     sock_addr.sin_port = htons ((short) request_port);
132     sock_addr.sin_addr.s_addr = htonl (INADDR_ANY);
133     if (bind (xdmcpFd, (struct sockaddr *)&sock_addr, sizeof (sock_addr)) == -1)
134     {
135         LogError (ReadCatalog(MC_LOG_SET,MC_LOG_ERR_BIND,MC_DEF_LOG_ERR_BIND),
136                   request_port, errno);
137         close (xdmcpFd);
138         xdmcpFd = -1;
139         return;
140     }
141     WellKnownSocketsMax = xdmcpFd;
142     FD_SET (xdmcpFd, &WellKnownSocketsMask);
143
144     chooserFd = socket (AF_INET, SOCK_STREAM, 0);
145     Debug ("Created chooser socket %d\n", chooserFd);
146     if (chooserFd == -1)
147     {
148         LogError ((unsigned char *)"chooser socket creation failed, errno %d\n", errno);
149         return;
150     }
151     listen (chooserFd, 5);
152     if (chooserFd > WellKnownSocketsMax)
153         WellKnownSocketsMax = chooserFd;
154     FD_SET (chooserFd, &WellKnownSocketsMask);
155 }
156
157 GetChooserAddr (addr, lenp)
158     char        *addr;
159     int         *lenp;
160 {
161     struct sockaddr_in  in_addr;
162     int                 len;
163
164     len = sizeof in_addr;
165 #if defined (_AIX) && (OSMAJORVERSION==4) && (OSMINORVERSION==2)
166     if (getsockname (chooserFd, (struct sockaddr *)&in_addr, (size_t *)&len) < 0)
167 #else
168     if (getsockname (chooserFd, (struct sockaddr *)&in_addr, &len) < 0)
169 #endif
170         return -1;
171     Debug ("Chooser socket port: %d\n", ntohs(in_addr.sin_port));
172     memmove( addr, (char *) &in_addr, len);
173     *lenp = len;
174     return 0;
175 }
176