choose.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. int LoadXloginResources(struct display *d); // session.c
  69. void GetSysParms(char **tzpp, char **fhpp, char **ftpp); // fontpath.c
  70. int GetChooserAddr(char *addr, int *lenp); // socket.c
  71. int NetaddrFamily(XdmcpNetaddr netaddrp); // netaddr.c
  72. static int
  73. FormatBytes (
  74. unsigned char *data,
  75. int length,
  76. char *buf,
  77. int buflen)
  78. {
  79. int i;
  80. static char HexChars[] = "0123456789abcdef";
  81. if (buflen < length * 2 + 1)
  82. return 0;
  83. for (i = 0; i < length; i++)
  84. {
  85. *buf++ = HexChars[(data[i] >> 4) & 0xf];
  86. *buf++ = HexChars[(data[i]) & 0xf];
  87. }
  88. *buf++ = '\0';
  89. return 1;
  90. }
  91. static int
  92. FormatARRAY8 (
  93. ARRAY8Ptr a,
  94. char *buf,
  95. int buflen)
  96. {
  97. return FormatBytes (a->data, a->length, buf, buflen);
  98. }
  99. typedef struct _IndirectUsers {
  100. struct _IndirectUsers *next;
  101. ARRAY8 client;
  102. CARD16 connectionType;
  103. } IndirectUsersRec, *IndirectUsersPtr;
  104. static IndirectUsersPtr indirectUsers;
  105. int
  106. RememberIndirectClient (
  107. ARRAY8Ptr clientAddress,
  108. CARD16 connectionType)
  109. {
  110. IndirectUsersPtr i;
  111. for (i = indirectUsers; i; i = i->next)
  112. if (XdmcpARRAY8Equal (clientAddress, &i->client) &&
  113. connectionType == i->connectionType)
  114. return 1;
  115. i = (IndirectUsersPtr) malloc (sizeof (IndirectUsersRec));
  116. if (!XdmcpCopyARRAY8 (clientAddress, &i->client))
  117. {
  118. free ((char *) i);
  119. return 0;
  120. }
  121. i->connectionType = connectionType;
  122. i->next = indirectUsers;
  123. indirectUsers = i;
  124. return 1;
  125. }
  126. void
  127. ForgetIndirectClient (
  128. ARRAY8Ptr clientAddress,
  129. CARD16 connectionType)
  130. {
  131. IndirectUsersPtr i, prev;
  132. prev = 0;
  133. for (i = indirectUsers; i; i = i->next)
  134. {
  135. if (XdmcpARRAY8Equal (clientAddress, &i->client) &&
  136. connectionType == i->connectionType)
  137. {
  138. if (prev)
  139. prev->next = i->next;
  140. else
  141. indirectUsers = i->next;
  142. XdmcpDisposeARRAY8 (&i->client);
  143. free ((char *) i);
  144. break;
  145. }
  146. prev = i;
  147. }
  148. }
  149. int
  150. IsIndirectClient (
  151. ARRAY8Ptr clientAddress,
  152. CARD16 connectionType)
  153. {
  154. IndirectUsersPtr i;
  155. for (i = indirectUsers; i; i = i->next)
  156. if (XdmcpARRAY8Equal (clientAddress, &i->client) &&
  157. connectionType == i->connectionType)
  158. return 1;
  159. return 0;
  160. }
  161. extern char *NetaddrPort();
  162. static int
  163. FormatChooserArgument (
  164. char *buf,
  165. int len)
  166. {
  167. unsigned char addr_buf[1024];
  168. int addr_len = sizeof (addr_buf);
  169. unsigned char result_buf[1024];
  170. int result_len = 0;
  171. int netfamily;
  172. if (GetChooserAddr (addr_buf, &addr_len) == -1)
  173. {
  174. LogError ((unsigned char *)"Cannot get return address for chooser socket\n");
  175. Debug ("Cannot get chooser socket address\n");
  176. return 0;
  177. }
  178. netfamily = NetaddrFamily((XdmcpNetaddr)addr_buf);
  179. switch (netfamily) {
  180. case AF_INET:
  181. {
  182. char *port;
  183. int portlen;
  184. ARRAY8Ptr localAddress, getLocalAddress ();
  185. port = NetaddrPort((XdmcpNetaddr)addr_buf, &portlen);
  186. result_buf[0] = netfamily >> 8;
  187. result_buf[1] = netfamily & 0xFF;
  188. result_buf[2] = port[0];
  189. result_buf[3] = port[1];
  190. localAddress = getLocalAddress ();
  191. bcopy ((char *)localAddress->data, (char *)result_buf+4, 4);
  192. result_len = 8;
  193. }
  194. break;
  195. #ifdef AF_DECnet
  196. case AF_DECnet:
  197. break;
  198. #endif
  199. default:
  200. Debug ("Chooser family %d isn't known\n", netfamily);
  201. return 0;
  202. }
  203. return FormatBytes (result_buf, result_len, buf, len);
  204. }
  205. typedef struct _Choices {
  206. struct _Choices *next;
  207. ARRAY8 client;
  208. CARD16 connectionType;
  209. ARRAY8 choice;
  210. long time;
  211. } ChoiceRec, *ChoicePtr;
  212. static ChoicePtr choices;
  213. ARRAY8Ptr
  214. IndirectChoice (
  215. ARRAY8Ptr clientAddress,
  216. CARD16 connectionType)
  217. {
  218. ChoicePtr c, next, prev;
  219. long now;
  220. now = time (0);
  221. prev = 0;
  222. for (c = choices; c; c = next)
  223. {
  224. next = c->next;
  225. if (now - c->time > 15)
  226. {
  227. Debug ("Timeout choice\n");
  228. if (prev)
  229. prev->next = next;
  230. else
  231. choices = next;
  232. XdmcpDisposeARRAY8 (&c->client);
  233. XdmcpDisposeARRAY8 (&c->choice);
  234. free ((char *) c);
  235. }
  236. else
  237. {
  238. if (XdmcpARRAY8Equal (clientAddress, &c->client) &&
  239. connectionType == c->connectionType)
  240. return &c->choice;
  241. prev = c;
  242. }
  243. }
  244. return 0;
  245. }
  246. static int
  247. RegisterIndirectChoice (
  248. ARRAY8Ptr clientAddress,
  249. CARD16 connectionType,
  250. ARRAY8Ptr choice)
  251. {
  252. ChoicePtr c;
  253. int insert;
  254. int found = 0;
  255. Debug ("Got indirect choice back (%s)\n", Print8Address(clientAddress));
  256. for (c = choices; c; c = c->next) {
  257. if (XdmcpARRAY8Equal (clientAddress, &c->client) &&
  258. connectionType == c->connectionType) {
  259. found = 1;
  260. break;
  261. }
  262. }
  263. if (!found)
  264. return 0;
  265. insert = 0;
  266. if (!c)
  267. {
  268. c = (ChoicePtr) malloc (sizeof (ChoiceRec));
  269. insert = 1;
  270. if (!c)
  271. return 0;
  272. c->connectionType = connectionType;
  273. if (!XdmcpCopyARRAY8 (clientAddress, &c->client))
  274. {
  275. free ((char *) c);
  276. return 0;
  277. }
  278. }
  279. else
  280. {
  281. XdmcpDisposeARRAY8 (&c->choice);
  282. }
  283. if (!XdmcpCopyARRAY8 (choice, &c->choice))
  284. {
  285. XdmcpDisposeARRAY8 (&c->client);
  286. free ((char *) c);
  287. return 0;
  288. }
  289. if (insert)
  290. {
  291. c->next = choices;
  292. choices = c;
  293. }
  294. c->time = time (0);
  295. Debug("choice=(%s)\n", Print8Address(choice));
  296. return 1;
  297. }
  298. /*ARGSUSED*/
  299. static void
  300. AddChooserHost (
  301. CARD16 connectionType,
  302. ARRAY8Ptr addr,
  303. char *closure)
  304. {
  305. char ***argp, **parseArgs();
  306. char hostbuf[1024];
  307. argp = (char ***) closure;
  308. if (addr->length == strlen ("BROADCAST") &&
  309. !strncmp ((char *)addr->data, "BROADCAST", addr->length))
  310. {
  311. *argp = parseArgs (*argp, "BROADCAST");
  312. }
  313. else if (FormatARRAY8 (addr, hostbuf, sizeof (hostbuf)))
  314. {
  315. *argp = parseArgs (*argp, hostbuf);
  316. }
  317. }
  318. int
  319. ProcessChooserSocket (
  320. int fd)
  321. {
  322. int client_fd;
  323. char buf[1024];
  324. int len;
  325. XdmcpBuffer buffer;
  326. ARRAY8 clientAddress;
  327. CARD16 connectionType;
  328. ARRAY8 choice;
  329. Debug ("Process chooser socket\n");
  330. len = sizeof (buf);
  331. client_fd = accept (fd, (struct sockaddr *)buf, &len);
  332. if (client_fd == -1)
  333. {
  334. LogError ((unsigned char *)"Cannot accept chooser connection\n");
  335. return 0;
  336. }
  337. Debug ("Accepted %d\n", client_fd);
  338. len = read (client_fd, buf, sizeof (buf));
  339. Debug ("Read returns %d\n", len);
  340. if (len > 0)
  341. {
  342. buffer.data = (BYTE *) buf;
  343. buffer.size = sizeof (buf);
  344. buffer.count = len;
  345. buffer.pointer = 0;
  346. clientAddress.data = 0;
  347. clientAddress.length = 0;
  348. choice.data = 0;
  349. choice.length = 0;
  350. if (XdmcpReadARRAY8 (&buffer, &clientAddress) &&
  351. XdmcpReadCARD16 (&buffer, &connectionType) &&
  352. XdmcpReadARRAY8 (&buffer, &choice))
  353. {
  354. Debug ("Read from chooser successfully\n");
  355. if (!RegisterIndirectChoice (&clientAddress, connectionType, &choice))
  356. Debug ("Invalid chooser reply\n");
  357. }
  358. XdmcpDisposeARRAY8 (&clientAddress);
  359. XdmcpDisposeARRAY8 (&choice);
  360. }
  361. else
  362. {
  363. LogError ((unsigned char *)"Choice response read error %s\n", strerror(errno));
  364. }
  365. close (client_fd);
  366. return 1;
  367. }
  368. void
  369. RunChooser (
  370. struct display *d)
  371. {
  372. char **args, **parseArgs(), **systemEnv();
  373. char buf[1024];
  374. char **env;
  375. Debug ("RunChooser %s\n", d->name);
  376. SetTitle (d->name, "chooser");
  377. LoadXloginResources (d);
  378. args = parseArgs ((char **) 0, d->chooser);
  379. strcpy (buf, "-xdmaddress ");
  380. if (FormatChooserArgument (buf + strlen (buf), sizeof (buf) - strlen (buf)))
  381. args = parseArgs (args, buf);
  382. strcpy (buf, "-clientaddress ");
  383. if (FormatARRAY8 (&d->clientAddr, buf + strlen (buf), sizeof (buf) - strlen (buf)))
  384. args = parseArgs (args, buf);
  385. sprintf (buf, "-connectionType %d", d->connectionType);
  386. args = parseArgs (args, buf);
  387. ForEachChooserHost (&d->clientAddr,
  388. d->connectionType,
  389. (int (*)()) AddChooserHost,
  390. (char *) &args);
  391. env = systemEnv (d, (char *) 0, (char *) 0);
  392. if (d->authFile)
  393. env = setEnv (env, "XAUTHORITY", d->authFile);
  394. if (d->pmSearchPath)
  395. env = setEnv(env, "XMICONSEARCHPATH", d->pmSearchPath);
  396. if (d->bmSearchPath)
  397. env = setEnv(env, "XMICONBMSEARCHPATH", d->bmSearchPath);
  398. if ( d->language && strlen(d->language) > 0 )
  399. env = setEnv(env, "LANG", d->language);
  400. if ( d->langList && strlen(d->langList) > 0 )
  401. env = setEnv(env, LANGLIST, d->langList);
  402. #if !defined (ENABLE_DYNAMIC_LANGLIST)
  403. else
  404. if ( strlen(languageList) > 0 )
  405. env = setEnv(env, LANGLIST, languageList);
  406. #endif /* ENABLE_DYNAMIC_LANGLIST */
  407. if ( d->setup)
  408. env = setEnv(env, "XSETUP", d->setup);
  409. if(d->displayType.location == Local)
  410. env = setEnv (env, LOCATION, "local");
  411. else
  412. env = setEnv (env, LOCATION, "remote");
  413. Debug ("Running %s\n", args[0]);
  414. execute (args, env);
  415. Debug ("Couldn't run %s\n", args[0]);
  416. LogError ((unsigned char *)"Cannot execute %s\n", args[0]);
  417. exit (REMANAGE_DISPLAY);
  418. }
  419. static char *
  420. Print8Address (
  421. ARRAY8Ptr Address)
  422. {
  423. static char buf[200];
  424. char *b;
  425. int i;
  426. b = buf;
  427. b[0]='\0';
  428. for (i = 0; i < (int)Address->length; i++) {
  429. sprintf(b, " %d", Address->data[i]);
  430. b = buf + strlen(buf);
  431. }
  432. return(buf);
  433. }