choose.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. /* *
  24. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  25. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  26. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  27. * (c) Copyright 1993, 1994 Novell, Inc. *
  28. */
  29. /*
  30. * $TOG: choose.c /main/5 1997/08/13 11:42:50 kaleb $
  31. *
  32. * Copyright 1990 Massachusetts Institute of Technology
  33. *
  34. * Permission to use, copy, modify, distribute, and sell this software and its
  35. * documentation for any purpose is hereby granted without fee, provided that
  36. * the above copyright notice appear in all copies and that both that
  37. * copyright notice and this permission notice appear in supporting
  38. * documentation, and that the name of M.I.T. not be used in advertising or
  39. * publicity pertaining to distribution of the software without specific,
  40. * written prior permission. M.I.T. makes no representations about the
  41. * suitability of this software for any purpose. It is provided "as is"
  42. * without express or implied warranty.
  43. *
  44. * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  46. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  47. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  48. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  49. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  50. *
  51. * Author: Keith Packard, MIT X Consortium
  52. */
  53. /*
  54. * choose.c
  55. *
  56. * xdm interface to chooser program
  57. */
  58. # include "dm.h"
  59. # include "vg.h"
  60. # include <X11/X.h>
  61. # include <sys/types.h>
  62. # include <sys/socket.h>
  63. # include <netinet/in.h>
  64. # include <sys/un.h>
  65. # include <ctype.h>
  66. # include <errno.h>
  67. static char *Print8Address (ARRAY8Ptr Address);
  68. static int
  69. FormatBytes (
  70. unsigned char *data,
  71. int length,
  72. char *buf,
  73. int buflen)
  74. {
  75. int i;
  76. static char HexChars[] = "0123456789abcdef";
  77. if (buflen < length * 2 + 1)
  78. return 0;
  79. for (i = 0; i < length; i++)
  80. {
  81. *buf++ = HexChars[(data[i] >> 4) & 0xf];
  82. *buf++ = HexChars[(data[i]) & 0xf];
  83. }
  84. *buf++ = '\0';
  85. return 1;
  86. }
  87. static int
  88. FormatARRAY8 (
  89. ARRAY8Ptr a,
  90. char *buf,
  91. int buflen)
  92. {
  93. return FormatBytes (a->data, a->length, buf, buflen);
  94. }
  95. typedef struct _IndirectUsers {
  96. struct _IndirectUsers *next;
  97. ARRAY8 client;
  98. CARD16 connectionType;
  99. } IndirectUsersRec, *IndirectUsersPtr;
  100. static IndirectUsersPtr indirectUsers;
  101. int
  102. RememberIndirectClient (
  103. ARRAY8Ptr clientAddress,
  104. CARD16 connectionType)
  105. {
  106. IndirectUsersPtr i;
  107. for (i = indirectUsers; i; i = i->next)
  108. if (XdmcpARRAY8Equal (clientAddress, &i->client) &&
  109. connectionType == i->connectionType)
  110. return 1;
  111. i = (IndirectUsersPtr) malloc (sizeof (IndirectUsersRec));
  112. if (!XdmcpCopyARRAY8 (clientAddress, &i->client))
  113. {
  114. free ((char *) i);
  115. return 0;
  116. }
  117. i->connectionType = connectionType;
  118. i->next = indirectUsers;
  119. indirectUsers = i;
  120. return 1;
  121. }
  122. void
  123. ForgetIndirectClient (
  124. ARRAY8Ptr clientAddress,
  125. CARD16 connectionType)
  126. {
  127. IndirectUsersPtr i, prev;
  128. prev = 0;
  129. for (i = indirectUsers; i; i = i->next)
  130. {
  131. if (XdmcpARRAY8Equal (clientAddress, &i->client) &&
  132. connectionType == i->connectionType)
  133. {
  134. if (prev)
  135. prev->next = i->next;
  136. else
  137. indirectUsers = i->next;
  138. XdmcpDisposeARRAY8 (&i->client);
  139. free ((char *) i);
  140. break;
  141. }
  142. prev = i;
  143. }
  144. }
  145. int
  146. IsIndirectClient (
  147. ARRAY8Ptr clientAddress,
  148. CARD16 connectionType)
  149. {
  150. IndirectUsersPtr i;
  151. for (i = indirectUsers; i; i = i->next)
  152. if (XdmcpARRAY8Equal (clientAddress, &i->client) &&
  153. connectionType == i->connectionType)
  154. return 1;
  155. return 0;
  156. }
  157. extern char *NetaddrPort();
  158. static int
  159. FormatChooserArgument (
  160. char *buf,
  161. int len)
  162. {
  163. unsigned char addr_buf[1024];
  164. int addr_len = sizeof (addr_buf);
  165. unsigned char result_buf[1024];
  166. int result_len = 0;
  167. int netfamily;
  168. if (GetChooserAddr (addr_buf, &addr_len) == -1)
  169. {
  170. LogError ((unsigned char *)"Cannot get return address for chooser socket\n");
  171. Debug ("Cannot get chooser socket address\n");
  172. return 0;
  173. }
  174. netfamily = NetaddrFamily((XdmcpNetaddr)addr_buf);
  175. switch (netfamily) {
  176. case AF_INET:
  177. {
  178. char *port;
  179. int portlen;
  180. ARRAY8Ptr localAddress, getLocalAddress ();
  181. port = NetaddrPort((XdmcpNetaddr)addr_buf, &portlen);
  182. result_buf[0] = netfamily >> 8;
  183. result_buf[1] = netfamily & 0xFF;
  184. result_buf[2] = port[0];
  185. result_buf[3] = port[1];
  186. localAddress = getLocalAddress ();
  187. bcopy ((char *)localAddress->data, (char *)result_buf+4, 4);
  188. result_len = 8;
  189. }
  190. break;
  191. #ifdef AF_DECnet
  192. case AF_DECnet:
  193. break;
  194. #endif
  195. default:
  196. Debug ("Chooser family %d isn't known\n", netfamily);
  197. return 0;
  198. }
  199. return FormatBytes (result_buf, result_len, buf, len);
  200. }
  201. typedef struct _Choices {
  202. struct _Choices *next;
  203. ARRAY8 client;
  204. CARD16 connectionType;
  205. ARRAY8 choice;
  206. long time;
  207. } ChoiceRec, *ChoicePtr;
  208. static ChoicePtr choices;
  209. ARRAY8Ptr
  210. IndirectChoice (
  211. ARRAY8Ptr clientAddress,
  212. CARD16 connectionType)
  213. {
  214. ChoicePtr c, next, prev;
  215. long now;
  216. now = time (0);
  217. prev = 0;
  218. for (c = choices; c; c = next)
  219. {
  220. next = c->next;
  221. if (now - c->time > 15)
  222. {
  223. Debug ("Timeout choice\n");
  224. if (prev)
  225. prev->next = next;
  226. else
  227. choices = next;
  228. XdmcpDisposeARRAY8 (&c->client);
  229. XdmcpDisposeARRAY8 (&c->choice);
  230. free ((char *) c);
  231. }
  232. else
  233. {
  234. if (XdmcpARRAY8Equal (clientAddress, &c->client) &&
  235. connectionType == c->connectionType)
  236. return &c->choice;
  237. prev = c;
  238. }
  239. }
  240. return 0;
  241. }
  242. static int
  243. RegisterIndirectChoice (
  244. ARRAY8Ptr clientAddress,
  245. CARD16 connectionType,
  246. ARRAY8Ptr choice)
  247. {
  248. ChoicePtr c;
  249. int insert;
  250. int found = 0;
  251. Debug ("Got indirect choice back (%s)\n", Print8Address(clientAddress));
  252. for (c = choices; c; c = c->next) {
  253. if (XdmcpARRAY8Equal (clientAddress, &c->client) &&
  254. connectionType == c->connectionType) {
  255. found = 1;
  256. break;
  257. }
  258. }
  259. if (!found)
  260. return 0;
  261. insert = 0;
  262. if (!c)
  263. {
  264. c = (ChoicePtr) malloc (sizeof (ChoiceRec));
  265. insert = 1;
  266. if (!c)
  267. return 0;
  268. c->connectionType = connectionType;
  269. if (!XdmcpCopyARRAY8 (clientAddress, &c->client))
  270. {
  271. free ((char *) c);
  272. return 0;
  273. }
  274. }
  275. else
  276. {
  277. XdmcpDisposeARRAY8 (&c->choice);
  278. }
  279. if (!XdmcpCopyARRAY8 (choice, &c->choice))
  280. {
  281. XdmcpDisposeARRAY8 (&c->client);
  282. free ((char *) c);
  283. return 0;
  284. }
  285. if (insert)
  286. {
  287. c->next = choices;
  288. choices = c;
  289. }
  290. c->time = time (0);
  291. Debug("choice=(%s)\n", Print8Address(choice));
  292. return 1;
  293. }
  294. /*ARGSUSED*/
  295. static void
  296. AddChooserHost (
  297. CARD16 connectionType,
  298. ARRAY8Ptr addr,
  299. char *closure)
  300. {
  301. char ***argp, **parseArgs();
  302. char hostbuf[1024];
  303. argp = (char ***) closure;
  304. if (addr->length == strlen ("BROADCAST") &&
  305. !strncmp ((char *)addr->data, "BROADCAST", addr->length))
  306. {
  307. *argp = parseArgs (*argp, "BROADCAST");
  308. }
  309. else if (FormatARRAY8 (addr, hostbuf, sizeof (hostbuf)))
  310. {
  311. *argp = parseArgs (*argp, hostbuf);
  312. }
  313. }
  314. int
  315. ProcessChooserSocket (
  316. int fd)
  317. {
  318. int client_fd;
  319. char buf[1024];
  320. int len;
  321. XdmcpBuffer buffer;
  322. ARRAY8 clientAddress;
  323. CARD16 connectionType;
  324. ARRAY8 choice;
  325. Debug ("Process chooser socket\n");
  326. len = sizeof (buf);
  327. client_fd = accept (fd, (struct sockaddr *)buf, &len);
  328. if (client_fd == -1)
  329. {
  330. LogError ((unsigned char *)"Cannot accept chooser connection\n");
  331. return 0;
  332. }
  333. Debug ("Accepted %d\n", client_fd);
  334. len = read (client_fd, buf, sizeof (buf));
  335. Debug ("Read returns %d\n", len);
  336. if (len > 0)
  337. {
  338. buffer.data = (BYTE *) buf;
  339. buffer.size = sizeof (buf);
  340. buffer.count = len;
  341. buffer.pointer = 0;
  342. clientAddress.data = 0;
  343. clientAddress.length = 0;
  344. choice.data = 0;
  345. choice.length = 0;
  346. if (XdmcpReadARRAY8 (&buffer, &clientAddress) &&
  347. XdmcpReadCARD16 (&buffer, &connectionType) &&
  348. XdmcpReadARRAY8 (&buffer, &choice))
  349. {
  350. Debug ("Read from chooser successfully\n");
  351. if (!RegisterIndirectChoice (&clientAddress, connectionType, &choice))
  352. Debug ("Invalid chooser reply\n");
  353. }
  354. XdmcpDisposeARRAY8 (&clientAddress);
  355. XdmcpDisposeARRAY8 (&choice);
  356. }
  357. else
  358. {
  359. LogError ((unsigned char *)"Choice response read error %s\n", strerror(errno));
  360. }
  361. close (client_fd);
  362. return 1;
  363. }
  364. void
  365. RunChooser (
  366. struct display *d)
  367. {
  368. char **args, **parseArgs(), **systemEnv();
  369. char buf[1024];
  370. char **env;
  371. Debug ("RunChooser %s\n", d->name);
  372. SetTitle (d->name, "chooser");
  373. LoadXloginResources (d);
  374. args = parseArgs ((char **) 0, d->chooser);
  375. strcpy (buf, "-xdmaddress ");
  376. if (FormatChooserArgument (buf + strlen (buf), sizeof (buf) - strlen (buf)))
  377. args = parseArgs (args, buf);
  378. strcpy (buf, "-clientaddress ");
  379. if (FormatARRAY8 (&d->clientAddr, buf + strlen (buf), sizeof (buf) - strlen (buf)))
  380. args = parseArgs (args, buf);
  381. sprintf (buf, "-connectionType %d", d->connectionType);
  382. args = parseArgs (args, buf);
  383. ForEachChooserHost (&d->clientAddr,
  384. d->connectionType,
  385. (int (*)()) AddChooserHost,
  386. (char *) &args);
  387. env = systemEnv (d, (char *) 0, (char *) 0);
  388. if (d->authFile)
  389. env = setEnv (env, "XAUTHORITY", d->authFile);
  390. if (d->pmSearchPath)
  391. env = setEnv(env, "XMICONSEARCHPATH", d->pmSearchPath);
  392. if (d->bmSearchPath)
  393. env = setEnv(env, "XMICONBMSEARCHPATH", d->bmSearchPath);
  394. if ( d->language && strlen(d->language) > 0 )
  395. env = setEnv(env, "LANG", d->language);
  396. if ( d->langList && strlen(d->langList) > 0 )
  397. env = setEnv(env, LANGLIST, d->langList);
  398. #if !defined (ENABLE_DYNAMIC_LANGLIST)
  399. else
  400. if ( strlen(languageList) > 0 )
  401. env = setEnv(env, LANGLIST, languageList);
  402. #endif /* ENABLE_DYNAMIC_LANGLIST */
  403. if ( d->setup)
  404. env = setEnv(env, "XSETUP", d->setup);
  405. if(d->displayType.location == Local)
  406. env = setEnv (env, LOCATION, "local");
  407. else
  408. env = setEnv (env, LOCATION, "remote");
  409. Debug ("Running %s\n", args[0]);
  410. execute (args, env);
  411. Debug ("Couldn't run %s\n", args[0]);
  412. LogError ((unsigned char *)"Cannot execute %s\n", args[0]);
  413. exit (REMANAGE_DISPLAY);
  414. }
  415. static char *
  416. Print8Address (
  417. ARRAY8Ptr Address)
  418. {
  419. static char buf[200];
  420. char *b;
  421. int i;
  422. b = buf;
  423. b[0]='\0';
  424. for (i = 0; i < (int)Address->length; i++) {
  425. sprintf(b, " %d", Address->data[i]);
  426. b = buf + strlen(buf);
  427. }
  428. return(buf);
  429. }