policy.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 libraries 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. * policy.c. Implement site-dependent policy for XDMCP connections
  51. */
  52. # include "dm.h"
  53. static ARRAY8 noAuthentication = { (CARD16) 0, (CARD8Ptr) 0 };
  54. static ARRAY8 loopbackAddress = { (CARD16) 0, (CARD8Ptr) 0 };
  55. typedef struct _XdmAuth {
  56. ARRAY8 authentication;
  57. ARRAY8 authorization;
  58. } XdmAuthRec, *XdmAuthPtr;
  59. XdmAuthRec auth[] = {
  60. #ifdef HASXDMAUTH
  61. { {(CARD16) 20, (CARD8 *) "XDM-AUTHENTICATION-1"},
  62. {(CARD16) 19, (CARD8 *) "XDM-AUTHORIZATION-1"},
  63. },
  64. #endif
  65. { {(CARD16) 0, (CARD8 *) 0},
  66. {(CARD16) 0, (CARD8 *) 0},
  67. }
  68. };
  69. #define NumAuth (sizeof auth / sizeof auth[0])
  70. /***************************************************************************
  71. *
  72. * Local procedure declarations
  73. *
  74. ***************************************************************************/
  75. static char * WillingMsg( void ) ;
  76. /******** End Local Function Declarations ********/
  77. ARRAY8Ptr
  78. ChooseAuthentication( ARRAYofARRAY8Ptr authenticationNames )
  79. {
  80. int i, j;
  81. for (i = 0; i < authenticationNames->length; i++)
  82. for (j = 0; j < NumAuth; j++)
  83. if (XdmcpARRAY8Equal (&authenticationNames->data[i],
  84. &auth[j].authentication))
  85. return &authenticationNames->data[i];
  86. return &noAuthentication;
  87. }
  88. int
  89. CheckAuthentication( struct protoDisplay *pdpy, ARRAY8Ptr displayID,
  90. ARRAY8Ptr name, ARRAY8Ptr data )
  91. {
  92. #ifdef HASXDMAUTH
  93. if (name->length && !strncmp (name->data, "XDM-AUTHENTICATION-1", 20))
  94. return XdmCheckAuthentication (pdpy, displayID, name, data);
  95. #endif
  96. return TRUE;
  97. }
  98. int
  99. SelectAuthorizationTypeIndex( ARRAY8Ptr authenticationName,
  100. ARRAYofARRAY8Ptr authorizationNames )
  101. {
  102. int i, j;
  103. for (j = 0; j < NumAuth; j++)
  104. if (XdmcpARRAY8Equal (authenticationName,
  105. &auth[j].authentication))
  106. break;
  107. if (j < NumAuth)
  108. {
  109. for (i = 0; i < authorizationNames->length; i++)
  110. if (XdmcpARRAY8Equal (&authorizationNames->data[i],
  111. &auth[j].authorization))
  112. return i;
  113. }
  114. for (i = 0; i < authorizationNames->length; i++)
  115. if (ValidAuthorization (authorizationNames->data[i].length,
  116. (char *) authorizationNames->data[i].data))
  117. return i;
  118. return -1;
  119. }
  120. #if 0
  121. int
  122. Willing( struct sockaddr *addr, int addrlen, ARRAY8Ptr authenticationName,
  123. ARRAY8Ptr status )
  124. #endif
  125. int
  126. Willing(
  127. ARRAY8Ptr addr,
  128. #if NeedWidePrototypes
  129. int connectionType,
  130. #else
  131. CARD16 connectionType,
  132. #endif /* NeedWidePrototypes */
  133. ARRAY8Ptr authenticationName,
  134. ARRAY8Ptr status,
  135. xdmOpCode type )
  136. {
  137. static char statusBuf[256];
  138. int ret;
  139. #if 0
  140. extern char *localHostname ();
  141. #endif
  142. ret = AcceptableDisplayAddress (addr, connectionType, type);
  143. if (!ret)
  144. snprintf (statusBuf, sizeof(statusBuf), "Display not authorized to connect");
  145. else
  146. snprintf (statusBuf, sizeof(statusBuf), "%s", WillingMsg());
  147. #if 0
  148. snprintf (statusBuf, sizeof(statusBuf), "host %s", localHostname());
  149. #endif
  150. /* enforce termination */
  151. statusBuf[255] = '\0';
  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. ARRAY8Ptr
  161. Accept( struct sockaddr *from, int fromlen,
  162. #if NeedWidePrototypes
  163. int displayNumber )
  164. #else
  165. CARD16 displayNumber )
  166. #endif /* NeedWidePrototypes */
  167. {
  168. return 0;
  169. }
  170. int
  171. SelectConnectionTypeIndex( ARRAY16Ptr connectionTypes,
  172. ARRAYofARRAY8Ptr connectionAddresses )
  173. {
  174. int i;
  175. /*
  176. * the current selection policy is to use the first connection address
  177. * that is not the loopback address...
  178. */
  179. /*
  180. * initialize loopback address array if not already done so...
  181. *
  182. */
  183. if (loopbackAddress.length == 0 &&
  184. XdmcpAllocARRAY8 (&loopbackAddress, 4) ) {
  185. loopbackAddress.data[0] = 127;
  186. loopbackAddress.data[1] = 0;
  187. loopbackAddress.data[2] = 0;
  188. loopbackAddress.data[3] = 1;
  189. }
  190. for (i = 0; i < connectionAddresses->length; i++) {
  191. if (!XdmcpARRAY8Equal (&connectionAddresses->data[i],
  192. &loopbackAddress))
  193. break;
  194. }
  195. return i;
  196. }
  197. /***************************************************************************
  198. *
  199. * WillingMsg
  200. *
  201. * Generate a message for the "Willing" status field.
  202. *
  203. ***************************************************************************/
  204. # define LINEBUFSIZE 32
  205. static char *
  206. WillingMsg( void )
  207. {
  208. static char retbuf[LINEBUFSIZE];
  209. char tmpbuf[LINEBUFSIZE * 8];
  210. char *cp;
  211. char tmpfilename[L_tmpnam + 1];
  212. FILE *f;
  213. /* Return selected part from an 'uptime' to Server */
  214. /* for use in hosts status field when XDMCP broadcast is used */
  215. /* (useful for selecting host to be managed by) */
  216. strcpy(retbuf, "Willing to Manage");
  217. strcpy(tmpbuf,"uptime | ");
  218. strcat(tmpbuf,"awk '{printf(\"%s %-.5s load: %.3s, %.3s, %.3s\",$(NF-6),$(NF-5),$(NF-2),$(NF-1),$NF)}'");
  219. strcat(tmpbuf," > ");
  220. if ( tmpnam(tmpfilename) != (char *)NULL ) {
  221. strcat(tmpbuf,tmpfilename);
  222. if(-1 == system(tmpbuf)) {
  223. perror(strerror(errno));
  224. }
  225. if ((f = fopen(tmpfilename,"r")) != (FILE *) NULL) {
  226. fgets(tmpbuf,LINEBUFSIZE,f);
  227. if ( (cp = strchr(tmpbuf,'\n')) != NULL)
  228. *cp = '\0';
  229. if (strlen(tmpbuf) > 10) /* seems reasonable? */
  230. strcpy(retbuf, tmpbuf);
  231. fclose(f);
  232. }
  233. unlink(tmpfilename);
  234. }
  235. return (retbuf);
  236. }