1 /* 2 3 Copyright 1988, 1998 The Open Group 4 5 Permission to use, copy, modify, distribute, and sell this software and its 6 documentation for any purpose is hereby granted without fee, provided that 7 the above copyright notice appear in all copies and that both that 8 copyright notice and this permission notice appear in supporting 9 documentation. 10 11 The above copyright notice and this permission notice shall be included 12 in all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 OTHER DEALINGS IN THE SOFTWARE. 21 22 Except as contained in this notice, the name of The Open Group shall 23 not be used in advertising or otherwise to promote the sale, use or 24 other dealings in this Software without prior written authorization 25 from The Open Group. 26 27 */ 28 29 /* 30 * xdm - display manager daemon 31 * Author: Keith Packard, MIT X Consortium 32 * 33 * policy.c. Implement site-dependent policy for XDMCP connections 34 */ 35 36 #include "dm.h" 37 #include "dm_auth.h" 38 39 #include <errno.h> 40 41 #ifdef XDMCP 42 43 # include <X11/X.h> 44 45 # include "dm_socket.h" 46 47 static ARRAY8 noAuthentication = { (CARD16) 0, (CARD8Ptr) 0 }; 48 49 typedef struct _XdmAuth { 50 ARRAY8 authentication; 51 ARRAY8 authorization; 52 } XdmAuthRec, *XdmAuthPtr; 53 54 static XdmAuthRec auth[] = { 55 # ifdef HASXDMAUTH 56 { {(CARD16) 20, (CARD8 *) "XDM-AUTHENTICATION-1"}, 57 {(CARD16) 19, (CARD8 *) "XDM-AUTHORIZATION-1"}, 58 }, 59 # endif 60 { {(CARD16) 0, (CARD8 *) 0}, 61 {(CARD16) 0, (CARD8 *) 0}, 62 } 63 }; 64 65 # define NumAuth (sizeof auth / sizeof auth[0]) 66 67 ARRAY8Ptr 68 ChooseAuthentication (ARRAYofARRAY8Ptr authenticationNames) 69 { 70 int i, j; 71 72 for (i = 0; i < (int)authenticationNames->length; i++) 73 for (j = 0; j < NumAuth; j++) 74 if (XdmcpARRAY8Equal (&authenticationNames->data[i], 75 &auth[j].authentication)) 76 return &authenticationNames->data[i]; 77 return &noAuthentication; 78 } 79 80 int 81 CheckAuthentication ( 82 struct protoDisplay *pdpy, 83 ARRAY8Ptr displayID, 84 ARRAY8Ptr name, 85 ARRAY8Ptr data) 86 { 87 # ifdef HASXDMAUTH 88 if (name->length && !strncmp ((char *)name->data, "XDM-AUTHENTICATION-1", 20)) 89 return XdmCheckAuthentication (pdpy, displayID, name, data); 90 # endif 91 return TRUE; 92 } 93 94 int 95 SelectAuthorizationTypeIndex ( 96 ARRAY8Ptr authenticationName, 97 ARRAYofARRAY8Ptr authorizationNames) 98 { 99 int i, j; 100 101 for (j = 0; j < NumAuth; j++) 102 if (XdmcpARRAY8Equal (authenticationName, 103 &auth[j].authentication)) 104 break; 105 if (j < NumAuth) 106 { 107 for (i = 0; i < (int)authorizationNames->length; i++) 108 if (XdmcpARRAY8Equal (&authorizationNames->data[i], 109 &auth[j].authorization)) 110 return i; 111 } 112 for (i = 0; i < (int)authorizationNames->length; i++) 113 if (ValidAuthorization (authorizationNames->data[i].length, 114 (char *) authorizationNames->data[i].data)) 115 return i; 116 return -1; 117 } 118 119 /*ARGSUSED*/ 120 int 121 Willing ( 122 ARRAY8Ptr addr, 123 CARD16 connectionType, 124 ARRAY8Ptr authenticationName, 125 ARRAY8Ptr status, 126 xdmOpCode type) 127 { 128 char statusBuf[256]; 129 int ret; 130 131 ret = AcceptableDisplayAddress (addr, connectionType, type); 132 if (!ret) 133 snprintf (statusBuf, sizeof(statusBuf), 134 "Display not authorized to connect"); 135 else 136 { 137 if (*willing) 138 { FILE *fd = NULL; 139 if ((fd = popen(willing, "r"))) 140 { 141 char *s = NULL; 142 errno = 0; 143 while(!(s = fgets(statusBuf, 256, fd)) && errno == EINTR) 144 errno = 0; 145 if (s && strlen(statusBuf) > 0) 146 statusBuf[strlen(statusBuf)-1] = 0; /* chop newline */ 147 else 148 snprintf (statusBuf, sizeof(statusBuf), 149 "Willing, but %s failed", willing); 150 } 151 else 152 snprintf (statusBuf, sizeof(statusBuf), 153 "Willing, but %s failed", willing); 154 if (fd) pclose(fd); 155 } 156 else 157 snprintf (statusBuf, sizeof(statusBuf), "Willing to manage"); 158 } 159 status->length = strlen (statusBuf); 160 status->data = malloc (status->length); 161 if (!status->data) 162 status->length = 0; 163 else 164 memcpy(status->data, statusBuf, status->length); 165 return ret; 166 } 167 168 /*ARGSUSED*/ 169 ARRAY8Ptr 170 Accept ( 171 struct sockaddr *from, 172 int fromlen, 173 CARD16 displayNumber) 174 { 175 return NULL; 176 } 177 178 /*ARGSUSED*/ 179 int 180 SelectConnectionTypeIndex ( 181 ARRAY16Ptr connectionTypes, 182 ARRAYofARRAY8Ptr connectionAddresses) 183 { 184 int i; 185 186 /* 187 * Select one supported connection type 188 */ 189 190 for (i = 0; i < connectionTypes->length; i++) { 191 switch (connectionTypes->data[i]) { 192 case FamilyLocal: 193 # if defined(TCPCONN) 194 case FamilyInternet: 195 # ifdef IPv6 196 case FamilyInternet6: 197 # endif /* IPv6 */ 198 # endif /* TCPCONN */ 199 return i; 200 } 201 } /* for */ 202 return -1; 203 } 204 205 #endif /* XDMCP */ 206