main.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2012 Intel Corporation.
  5. *
  6. * Authors: Jussi Kukkonen <jku@linux.intel.com>
  7. * David Woodhouse <dwmw2@infradead.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public License
  11. * version 2.1, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to:
  20. *
  21. * Free Software Foundation, Inc.
  22. * 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #define _GNU_SOURCE
  32. #include <getopt.h>
  33. #include <libxml/parser.h>
  34. #include <libxml/tree.h>
  35. #include <gtk/gtk.h>
  36. #include <glib/gi18n.h>
  37. #include <glib-unix.h>
  38. #include <nm-vpn-plugin-utils.h>
  39. #define SECRET_API_SUBJECT_TO_CHANGE
  40. #include <libsecret/secret.h>
  41. #include "../src/nm-openconnect-service.h"
  42. #include "openconnect.h"
  43. #if !OPENCONNECT_CHECK_VER(2,1)
  44. #define __openconnect_set_token_mode(...) -EOPNOTSUPP
  45. #elif !OPENCONNECT_CHECK_VER(2,2)
  46. #define __openconnect_set_token_mode(vpninfo, mode, secret) openconnect_set_stoken_mode(vpninfo, 1, secret)
  47. #else
  48. #define __openconnect_set_token_mode openconnect_set_token_mode
  49. #endif
  50. #if OPENCONNECT_CHECK_VER(3,0)
  51. #define NEWGROUP_SUPPORTED 1
  52. #define AUTHGROUP_OPT(form) (void *)(form)->authgroup_opt
  53. #define AUTHGROUP_SELECTION(form) (form)->authgroup_selection
  54. #define FORMCHOICE(sopt, i) ((sopt)->choices[i])
  55. #define IGNORE_OPT(opt) ((opt)->flags & OC_FORM_OPT_IGNORE)
  56. #else
  57. #define NEWGROUP_SUPPORTED 0
  58. #define AUTHGROUP_OPT(form) NULL
  59. #define AUTHGROUP_SELECTION(form) 0
  60. #define FORMCHOICE(sopt, i) (&(sopt)->choices[i])
  61. #define IGNORE_OPT(opt) 0
  62. #define OC_FORM_RESULT_ERR -1
  63. #define OC_FORM_RESULT_OK 0
  64. #define OC_FORM_RESULT_CANCELLED 1
  65. #define OC_FORM_RESULT_NEWGROUP 2
  66. #endif
  67. #if OPENCONNECT_CHECK_VER(4,0)
  68. #define dup_option_value(opt) g_strdup((opt)->_value);
  69. #define OC3DUP(x) (x)
  70. #define write_config_const const
  71. #else
  72. #define dup_option_value(opt) g_strdup((opt)->value);
  73. #define openconnect_set_option_value(opt, val) do { \
  74. struct oc_form_opt *_o = (opt); \
  75. free(_o->value); _o->value = g_strdup(val); \
  76. } while (0)
  77. #define openconnect_free_cert_info(v, x) free(x)
  78. #define OC3DUP(x) g_strdup(x)
  79. #define write_config_const /* */
  80. #endif
  81. static const SecretSchema openconnect_secret_schema = {
  82. "org.freedesktop.NetworkManager.Connection.Openconnect",
  83. SECRET_SCHEMA_DONT_MATCH_NAME,
  84. {
  85. { "vpn_uuid", SECRET_SCHEMA_ATTRIBUTE_STRING },
  86. { "auth_id", SECRET_SCHEMA_ATTRIBUTE_STRING },
  87. { "label", SECRET_SCHEMA_ATTRIBUTE_STRING },
  88. { NULL, 0 },
  89. }
  90. };
  91. static char *lasthost;
  92. typedef struct vpnhost {
  93. char *hostname;
  94. char *hostaddress;
  95. char *usergroup;
  96. struct vpnhost *next;
  97. } vpnhost;
  98. vpnhost *vpnhosts;
  99. enum certificate_response{
  100. CERT_DENIED = -1,
  101. CERT_USER_NOT_READY,
  102. CERT_ACCEPTED,
  103. };
  104. /* This struct holds all information we need to add a password to
  105. * the secret store. It’s used in success_passwords. */
  106. struct keyring_password {
  107. char *description;
  108. char *password;
  109. char *vpn_uuid;
  110. char *auth_id;
  111. char *label;
  112. };
  113. static void keyring_password_free(gpointer data);
  114. static void keyring_store_passwords(gpointer key, gpointer value, gpointer user_data);
  115. static void keyring_password_free(gpointer data)
  116. {
  117. struct keyring_password *kp = (struct keyring_password*)data;
  118. g_free(kp->description);
  119. g_free(kp->password);
  120. g_free(kp->vpn_uuid);
  121. g_free(kp->auth_id);
  122. g_free(kp->label);
  123. g_free(kp);
  124. }
  125. static void keyring_store_passwords(gpointer key, gpointer value, gpointer user_data)
  126. {
  127. struct keyring_password *kp = (struct keyring_password*)value;
  128. secret_password_store_sync (&openconnect_secret_schema, NULL,
  129. kp->description, kp->password,
  130. NULL, NULL,
  131. "vpn_uuid", kp->vpn_uuid,
  132. "auth_id", kp->auth_id,
  133. "label", kp->label,
  134. NULL);
  135. }
  136. typedef struct auth_ui_data {
  137. char *vpn_name;
  138. char *vpn_uuid;
  139. GHashTable *options;
  140. GHashTable *secrets;
  141. GHashTable *success_secrets;
  142. GHashTable *success_passwords;
  143. struct openconnect_info *vpninfo;
  144. GtkWidget *dialog;
  145. GtkWidget *combo;
  146. GtkWidget *connect_button;
  147. GtkWidget *no_form_label;
  148. GtkWidget *getting_form_label;
  149. GtkWidget *ssl_box;
  150. GtkWidget *cancel_button;
  151. GtkWidget *login_button;
  152. GtkWidget *last_notice_icon;
  153. GtkTextBuffer *log;
  154. int cookie_retval;
  155. int cancel_pipes[2];
  156. gboolean cancelled; /* fully cancel the whole challenge-response series */
  157. gboolean getting_cookie;
  158. int form_grabbed;
  159. GQueue *form_entries; /* modified from worker thread */
  160. GMutex form_mutex;
  161. GCond form_retval_changed;
  162. gpointer form_retval;
  163. GCond form_shown_changed;
  164. gboolean form_shown;
  165. gboolean newgroup;
  166. gboolean group_set;
  167. GCond cert_response_changed;
  168. enum certificate_response cert_response;
  169. } auth_ui_data;
  170. enum {
  171. AUTH_DIALOG_RESPONSE_LOGIN = 1,
  172. AUTH_DIALOG_RESPONSE_CANCEL,
  173. } auth_dialog_response;
  174. /* this is here because ssl ui (*opener) does not have a userdata pointer... */
  175. static auth_ui_data *_ui_data;
  176. static void connect_host(auth_ui_data *ui_data);
  177. static void container_child_remove(GtkWidget *widget, gpointer data)
  178. {
  179. GtkContainer *container = GTK_CONTAINER(data);
  180. gtk_container_remove(container, widget);
  181. }
  182. static void ssl_box_add_error(auth_ui_data *ui_data, const char *msg)
  183. {
  184. GtkWidget *hbox, *text, *image;
  185. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
  186. gtk_box_pack_start(GTK_BOX(ui_data->ssl_box), hbox, FALSE, FALSE, 0);
  187. image = gtk_image_new_from_icon_name("dialog-error",
  188. GTK_ICON_SIZE_DIALOG);
  189. gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
  190. text = gtk_label_new(msg);
  191. gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
  192. gtk_box_pack_start(GTK_BOX(hbox), text, TRUE, TRUE, 0);
  193. ui_data->last_notice_icon = NULL;
  194. }
  195. static void ssl_box_add_notice(auth_ui_data *ui_data, const char *msg)
  196. {
  197. GtkWidget *hbox, *text, *image;
  198. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
  199. gtk_box_pack_start(GTK_BOX(ui_data->ssl_box), hbox, FALSE, FALSE, 0);
  200. image = gtk_image_new_from_icon_name("dialog-warning",
  201. GTK_ICON_SIZE_DIALOG);
  202. gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
  203. text = gtk_label_new(msg);
  204. gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
  205. gtk_box_pack_start(GTK_BOX(hbox), text, TRUE, TRUE, 0);
  206. gtk_widget_show_all(ui_data->ssl_box);
  207. ui_data->last_notice_icon = image;
  208. }
  209. static void ssl_box_add_info(auth_ui_data *ui_data, const char *msg)
  210. {
  211. GtkWidget *text;
  212. int width;
  213. text = gtk_label_new(msg);
  214. gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
  215. gtk_window_get_size(GTK_WINDOW(ui_data->dialog), &width, NULL);
  216. /* FIXME: this is not very nice -- can't make the window thinner after this */
  217. gtk_widget_set_size_request(text, width - 40, -1);
  218. gtk_box_pack_start(GTK_BOX(ui_data->ssl_box), text, FALSE, FALSE, 0);
  219. }
  220. static void ssl_box_clear(auth_ui_data *ui_data)
  221. {
  222. gtk_widget_hide(ui_data->no_form_label);
  223. gtk_widget_hide(ui_data->getting_form_label);
  224. ui_data->last_notice_icon = NULL;
  225. gtk_container_foreach(GTK_CONTAINER(ui_data->ssl_box),
  226. container_child_remove, ui_data->ssl_box);
  227. gtk_widget_set_sensitive (ui_data->login_button, FALSE);
  228. gtk_widget_set_sensitive (ui_data->cancel_button, FALSE);
  229. }
  230. typedef struct ui_fragment_data {
  231. GtkWidget *widget;
  232. GtkWidget *entry;
  233. GCancellable *cancel;
  234. auth_ui_data *ui_data;
  235. struct oc_form_opt *opt;
  236. char *entry_text;
  237. int initial_selection;
  238. int grab_focus;
  239. } ui_fragment_data;
  240. static void entry_activate_cb(GtkWidget *widget, auth_ui_data *ui_data)
  241. {
  242. gtk_dialog_response(GTK_DIALOG(ui_data->dialog), AUTH_DIALOG_RESPONSE_LOGIN);
  243. }
  244. static void evaluate_login_visibility(auth_ui_data *ui_data)
  245. {
  246. gboolean visible = TRUE;
  247. gtk_widget_set_sensitive (ui_data->login_button, visible);
  248. }
  249. static void entry_changed(GtkEntry *entry, ui_fragment_data *data)
  250. {
  251. g_free (data->entry_text);
  252. data->entry_text = g_strdup(gtk_entry_get_text(entry));
  253. }
  254. static void do_override_label(ui_fragment_data *data, struct oc_choice *choice)
  255. {
  256. const char *new_label = data->opt->label;
  257. if (!data->widget)
  258. return;
  259. if (choice->override_name && !strcmp(choice->override_name, data->opt->name))
  260. new_label = choice->override_label;
  261. gtk_label_set_text(GTK_LABEL(data->widget), new_label);
  262. }
  263. static gboolean do_newgroup(GtkDialog *dialog)
  264. {
  265. gtk_dialog_response(dialog, AUTH_DIALOG_RESPONSE_LOGIN);
  266. return FALSE;
  267. }
  268. static void combo_changed(GtkComboBox *combo, ui_fragment_data *data)
  269. {
  270. struct oc_form_opt_select *sopt = (void *)data->opt;
  271. int entry = gtk_combo_box_get_active(combo);
  272. if (entry < 0)
  273. return;
  274. data->entry_text = FORMCHOICE(sopt, entry)->name;
  275. if (NEWGROUP_SUPPORTED && entry != data->initial_selection) {
  276. data->ui_data->newgroup = TRUE;
  277. g_idle_add ((GSourceFunc)do_newgroup, data->ui_data->dialog);
  278. return;
  279. }
  280. g_queue_foreach(data->ui_data->form_entries, (GFunc)do_override_label,
  281. FORMCHOICE(sopt, entry));
  282. }
  283. static gboolean ui_write_prompt (ui_fragment_data *data)
  284. {
  285. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  286. GtkWidget *hbox, *text, *entry;
  287. int visible;
  288. const char *label;
  289. label = data->opt->label;
  290. visible = (data->opt->type == OC_FORM_OPT_TEXT);
  291. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  292. gtk_box_pack_start(GTK_BOX(data->ui_data->ssl_box), hbox, FALSE, FALSE, 0);
  293. text = gtk_label_new(label);
  294. gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 0);
  295. data->widget = text;
  296. entry = gtk_entry_new();
  297. gtk_box_pack_end(GTK_BOX(hbox), entry, FALSE, FALSE, 0);
  298. data->entry = entry;
  299. if (!visible)
  300. gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
  301. if (data->entry_text)
  302. gtk_entry_set_text(GTK_ENTRY(entry), data->entry_text);
  303. /* If it's the first empty one, grab focus. Otherwise, if
  304. it's the first item of *any* kind, grab focus but don't
  305. admit it (so another empty entry can take focus_ */
  306. if (!data->entry_text && !data->ui_data->form_grabbed) {
  307. data->ui_data->form_grabbed = 1;
  308. gtk_widget_grab_focus (entry);
  309. } else if (g_queue_peek_tail(ui_data->form_entries) == data)
  310. gtk_widget_grab_focus (entry);
  311. g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(entry_changed), data);
  312. g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(entry_activate_cb), ui_data);
  313. /* data is freed in ui_flush in worker thread */
  314. return FALSE;
  315. }
  316. static gboolean ui_add_select (ui_fragment_data *data)
  317. {
  318. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  319. GtkWidget *hbox, *text, *combo;
  320. struct oc_form_opt_select *sopt = (void *)data->opt;
  321. int i, user_selection = -1;
  322. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  323. gtk_box_pack_start(GTK_BOX(data->ui_data->ssl_box), hbox, FALSE, FALSE, 0);
  324. text = gtk_label_new(data->opt->label);
  325. gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 0);
  326. combo = gtk_combo_box_text_new();
  327. gtk_box_pack_end(GTK_BOX(hbox), combo, FALSE, FALSE, 0);
  328. for (i = 0; i < sopt->nr_choices; i++) {
  329. gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), FORMCHOICE(sopt, i)->label);
  330. if (data->entry_text &&
  331. !strcmp(data->entry_text, FORMCHOICE(sopt, i)->name))
  332. user_selection = i;
  333. }
  334. i = data->initial_selection != -1 ? data->initial_selection :
  335. user_selection != -1 ? user_selection : 0;
  336. gtk_combo_box_set_active(GTK_COMBO_BOX(combo), i);
  337. g_free(data->entry_text);
  338. data->entry_text = FORMCHOICE(sopt, i)->name;
  339. data->initial_selection = i;
  340. if (g_queue_peek_tail(ui_data->form_entries) == data)
  341. gtk_widget_grab_focus (combo);
  342. g_signal_connect(G_OBJECT(combo), "changed", G_CALLBACK(combo_changed), data);
  343. /* Hook up the 'show' signal to ensure that we override prompts on
  344. UI elements which may be coming later. */
  345. g_signal_connect(G_OBJECT(combo), "show", G_CALLBACK(combo_changed), data);
  346. /* data is freed in ui_flush in worker thread */
  347. return FALSE;
  348. }
  349. static gboolean ui_show (auth_ui_data *ui_data)
  350. {
  351. gtk_widget_hide (ui_data->getting_form_label);
  352. gtk_widget_show_all (ui_data->ssl_box);
  353. gtk_widget_set_sensitive (ui_data->cancel_button, TRUE);
  354. g_mutex_lock (&ui_data->form_mutex);
  355. evaluate_login_visibility(ui_data);
  356. ui_data->form_shown = TRUE;
  357. g_cond_signal (&ui_data->form_shown_changed);
  358. g_mutex_unlock (&ui_data->form_mutex);
  359. return FALSE;
  360. }
  361. static char *find_form_answer(GHashTable *secrets, struct oc_auth_form *form,
  362. struct oc_form_opt *opt)
  363. {
  364. char *key, *result;
  365. key = g_strdup_printf ("form:%s:%s", form->auth_id, opt->name);
  366. result = g_hash_table_lookup (secrets, key);
  367. g_free(key);
  368. return result;
  369. }
  370. /* Callback which is called when we got a reply from the secret store for any
  371. * password field. Updates the contents of the password field unless the user
  372. * entered anything in the meantime. */
  373. static void got_keyring_pw(GObject *object, GAsyncResult *result, gpointer userdata)
  374. {
  375. ui_fragment_data *data = (ui_fragment_data*)userdata;
  376. GList *list;
  377. SecretItem *item;
  378. SecretValue *value = NULL;
  379. const char *string = NULL;
  380. list = secret_service_search_finish (SECRET_SERVICE (object), result, NULL);
  381. if (list != NULL) {
  382. item = list->data;
  383. value = secret_item_get_secret (item);
  384. string = secret_value_get (value, NULL);
  385. }
  386. if (string != NULL) {
  387. if (data->entry) {
  388. if (!g_ascii_strcasecmp("",
  389. gtk_entry_get_text(GTK_ENTRY(data->entry)))) {
  390. gtk_entry_set_text(GTK_ENTRY(data->entry), string);
  391. if (gtk_widget_has_focus(data->entry))
  392. gtk_editable_select_region(GTK_EDITABLE(data->entry), 0, -1);
  393. }
  394. } else
  395. data->entry_text = g_strdup (string);
  396. }
  397. if (value)
  398. secret_value_unref (value);
  399. g_list_free_full (list, g_object_unref);
  400. /* zero the cancellable so that we don’t attempt to cancel it when
  401. * closing the dialog */
  402. g_clear_object (&data->cancel);
  403. }
  404. /* This part for processing forms from openconnect directly, rather than
  405. through the SSL UI abstraction (which doesn't allow 'select' options) */
  406. static gboolean ui_form (struct oc_auth_form *form)
  407. {
  408. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  409. struct oc_form_opt *opt;
  410. g_mutex_lock(&ui_data->form_mutex);
  411. while (!g_queue_is_empty (ui_data->form_entries)) {
  412. ui_fragment_data *data;
  413. data = g_queue_pop_tail (ui_data->form_entries);
  414. g_slice_free (ui_fragment_data, data);
  415. }
  416. g_mutex_unlock(&ui_data->form_mutex);
  417. if (form->banner)
  418. ssl_box_add_info(ui_data, form->banner);
  419. if (form->error)
  420. ssl_box_add_error(ui_data, form->error);
  421. if (form->message)
  422. ssl_box_add_info(ui_data, form->message);
  423. for (opt = form->opts; opt; opt = opt->next) {
  424. ui_fragment_data *data;
  425. if (opt->type == OC_FORM_OPT_HIDDEN ||
  426. IGNORE_OPT(opt))
  427. continue;
  428. data = g_slice_new0 (ui_fragment_data);
  429. data->ui_data = ui_data;
  430. data->opt = opt;
  431. if (opt->type == OC_FORM_OPT_PASSWORD ||
  432. opt->type == OC_FORM_OPT_TEXT) {
  433. g_mutex_lock (&ui_data->form_mutex);
  434. g_queue_push_head(ui_data->form_entries, data);
  435. g_mutex_unlock (&ui_data->form_mutex);
  436. if (opt->type != OC_FORM_OPT_PASSWORD) {
  437. data->entry_text = g_strdup (find_form_answer(ui_data->secrets,
  438. form, opt));
  439. if (!data->entry_text)
  440. data->entry_text = dup_option_value(opt);
  441. } else {
  442. GHashTable *attrs;
  443. data->cancel = g_cancellable_new ();
  444. attrs = secret_attributes_build (&openconnect_secret_schema,
  445. "vpn_uuid", ui_data->vpn_uuid,
  446. "auth_id", form->auth_id,
  447. "label", data->opt->name,
  448. NULL);
  449. secret_service_search (NULL, &openconnect_secret_schema, attrs,
  450. SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS,
  451. data->cancel, got_keyring_pw, data);
  452. g_hash_table_unref (attrs);
  453. }
  454. ui_write_prompt(data);
  455. } else if (opt->type == OC_FORM_OPT_SELECT) {
  456. g_mutex_lock (&ui_data->form_mutex);
  457. g_queue_push_head(ui_data->form_entries, data);
  458. g_mutex_unlock (&ui_data->form_mutex);
  459. data->entry_text = g_strdup (find_form_answer(ui_data->secrets,
  460. form, opt));
  461. if (opt == AUTHGROUP_OPT(form))
  462. data->initial_selection = AUTHGROUP_SELECTION(form);
  463. else
  464. data->initial_selection = -1;
  465. ui_add_select(data);
  466. } else
  467. g_slice_free (ui_fragment_data, data);
  468. }
  469. return ui_show(ui_data);
  470. }
  471. /* If our stored group_list selection differs from the server default, send a
  472. NEWGROUP request to try to change it before rendering the form */
  473. static gboolean set_initial_authgroup (auth_ui_data *ui_data, struct oc_auth_form *form)
  474. {
  475. struct oc_form_opt *opt;
  476. if (!NEWGROUP_SUPPORTED || ui_data->group_set || !AUTHGROUP_OPT(form))
  477. return FALSE;
  478. ui_data->group_set = TRUE;
  479. for (opt = form->opts; opt; opt = opt->next) {
  480. int i;
  481. char *saved_group;
  482. struct oc_form_opt_select *sopt;
  483. if (opt != AUTHGROUP_OPT(form))
  484. continue;
  485. saved_group = find_form_answer(ui_data->secrets, form, opt);
  486. if (!saved_group)
  487. return FALSE;
  488. sopt = (struct oc_form_opt_select *)opt;
  489. for (i = 0; i < sopt->nr_choices; i++) {
  490. struct oc_choice *ch = FORMCHOICE(sopt, i);
  491. if (!strcmp(saved_group, ch->name) && i != AUTHGROUP_SELECTION(form)) {
  492. openconnect_set_option_value(opt, saved_group);
  493. return TRUE;
  494. }
  495. }
  496. }
  497. return FALSE;
  498. }
  499. static int nm_process_auth_form (void *cbdata, struct oc_auth_form *form)
  500. {
  501. auth_ui_data *ui_data = cbdata;
  502. int response;
  503. if (set_initial_authgroup(ui_data, form))
  504. return OC_FORM_RESULT_NEWGROUP;
  505. ui_data->newgroup = FALSE;
  506. g_idle_add((GSourceFunc)ui_form, form);
  507. g_mutex_lock(&ui_data->form_mutex);
  508. /* wait for ui to show */
  509. while (!ui_data->form_shown) {
  510. g_cond_wait(&ui_data->form_shown_changed, &ui_data->form_mutex);
  511. }
  512. ui_data->form_shown = FALSE;
  513. if (!ui_data->cancelled) {
  514. /* wait for form submission or cancel */
  515. while (!ui_data->form_retval) {
  516. g_cond_wait(&ui_data->form_retval_changed, &ui_data->form_mutex);
  517. }
  518. response = GPOINTER_TO_INT (ui_data->form_retval);
  519. ui_data->form_retval = NULL;
  520. } else
  521. response = AUTH_DIALOG_RESPONSE_CANCEL;
  522. if (response == AUTH_DIALOG_RESPONSE_LOGIN) {
  523. /* set entry results and free temporary data structures */
  524. while (!g_queue_is_empty (ui_data->form_entries)) {
  525. ui_fragment_data *data;
  526. data = g_queue_pop_tail (ui_data->form_entries);
  527. if (data->cancel)
  528. g_cancellable_cancel(data->cancel);
  529. if (data->entry_text) {
  530. openconnect_set_option_value(data->opt, data->entry_text);
  531. if (data->opt->type == OC_FORM_OPT_TEXT ||
  532. data->opt->type == OC_FORM_OPT_SELECT) {
  533. char *keyname;
  534. keyname = g_strdup_printf("form:%s:%s", form->auth_id, data->opt->name);
  535. g_hash_table_insert (ui_data->success_secrets,
  536. keyname, g_strdup (data->entry_text));
  537. }
  538. if (data->opt->type == OC_FORM_OPT_PASSWORD) {
  539. /* store the password in the secret store */
  540. //int result;
  541. struct keyring_password *kp = g_new(struct keyring_password, 1);
  542. kp->description = g_strdup_printf(_("OpenConnect: %s: %s:%s"), ui_data->vpn_name, form->auth_id, data->opt->name);
  543. kp->password = g_strdup(data->entry_text);
  544. kp->vpn_uuid = g_strdup(ui_data->vpn_uuid);
  545. kp->auth_id = g_strdup(form->auth_id);
  546. kp->label = g_strdup(data->opt->name);
  547. g_hash_table_insert (ui_data->success_passwords,
  548. g_strdup(kp->description), kp);
  549. }
  550. }
  551. g_slice_free (ui_fragment_data, data);
  552. }
  553. }
  554. ui_data->form_grabbed = 0;
  555. g_mutex_unlock(&ui_data->form_mutex);
  556. if (response == AUTH_DIALOG_RESPONSE_LOGIN) {
  557. if (ui_data->newgroup)
  558. return OC_FORM_RESULT_NEWGROUP;
  559. else
  560. return OC_FORM_RESULT_OK;
  561. } else
  562. return OC_FORM_RESULT_CANCELLED;
  563. }
  564. static char* get_title(const char *vpn_name)
  565. {
  566. if (vpn_name)
  567. return g_strdup_printf(_("Connect to VPN '%s'"), vpn_name);
  568. else
  569. return g_strdup(_("Connect to VPN"));
  570. }
  571. typedef struct cert_data {
  572. auth_ui_data *ui_data;
  573. char *cert_details;
  574. const char *reason;
  575. } cert_data;
  576. #if !OPENCONNECT_CHECK_VER(1,5)
  577. static char *openconnect_get_cert_details(struct openconnect_info *vpninfo,
  578. OPENCONNECT_X509 *cert)
  579. {
  580. BIO *bp = BIO_new(BIO_s_mem());
  581. BUF_MEM *certinfo;
  582. char zero = 0;
  583. char *ret;
  584. X509_print_ex(bp, cert, 0, 0);
  585. BIO_write(bp, &zero, 1);
  586. BIO_get_mem_ptr(bp, &certinfo);
  587. ret = strdup(certinfo->data);
  588. BIO_free(bp);
  589. return ret;
  590. }
  591. #endif
  592. static gboolean user_validate_cert(cert_data *data)
  593. {
  594. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  595. char *title;
  596. GtkWidget *dlg, *text, *scroll;
  597. GtkTextBuffer *buffer;
  598. int result;
  599. title = get_title(data->ui_data->vpn_name);
  600. dlg = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
  601. GTK_BUTTONS_OK_CANCEL,
  602. _("Certificate from VPN server \"%s\" failed verification.\n"
  603. "Reason: %s\nDo you want to accept it?"),
  604. openconnect_get_hostname(data->ui_data->vpninfo),
  605. data->reason);
  606. gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dlg), FALSE);
  607. gtk_window_set_skip_pager_hint(GTK_WINDOW(dlg), FALSE);
  608. gtk_window_set_title(GTK_WINDOW(dlg), title);
  609. gtk_window_set_default_size(GTK_WINDOW(dlg), 550, 600);
  610. gtk_window_set_resizable(GTK_WINDOW(dlg), TRUE);
  611. gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_CANCEL);
  612. g_free(title);
  613. scroll = gtk_scrolled_window_new(NULL, NULL);
  614. gtk_box_pack_start(GTK_BOX (gtk_dialog_get_content_area(GTK_DIALOG (dlg))), scroll, TRUE, TRUE, 0);
  615. gtk_widget_show(scroll);
  616. text = gtk_text_view_new();
  617. buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
  618. gtk_text_buffer_set_text(buffer, data->cert_details, -1);
  619. gtk_text_view_set_editable(GTK_TEXT_VIEW(text), 0);
  620. gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
  621. gtk_container_add(GTK_CONTAINER(scroll), text);
  622. gtk_widget_show(text);
  623. result = gtk_dialog_run(GTK_DIALOG(dlg));
  624. gtk_widget_destroy(dlg);
  625. g_mutex_lock (&ui_data->form_mutex);
  626. if (result == GTK_RESPONSE_OK)
  627. data->ui_data->cert_response = CERT_ACCEPTED;
  628. else
  629. data->ui_data->cert_response = CERT_DENIED;
  630. g_cond_signal (&ui_data->cert_response_changed);
  631. g_mutex_unlock (&ui_data->form_mutex);
  632. return FALSE;
  633. }
  634. /* runs in worker thread */
  635. static int validate_peer_cert(void *cbdata,
  636. #if !OPENCONNECT_CHECK_VER(5,0)
  637. OPENCONNECT_X509 *peer_cert,
  638. #endif
  639. const char *reason)
  640. {
  641. auth_ui_data *ui_data = cbdata;
  642. int ret = 0;
  643. cert_data *data;
  644. char *certkey;
  645. char *accepted_hash = NULL;
  646. #if OPENCONNECT_CHECK_VER(5,0)
  647. const char *fingerprint = openconnect_get_peer_cert_hash(ui_data->vpninfo);
  648. #else
  649. char fingerprint[41];
  650. ret = openconnect_get_cert_sha1(ui_data->vpninfo, peer_cert, fingerprint);
  651. if (ret)
  652. return ret;
  653. #define openconnect_check_peer_cert_hash(v, h) strcmp(h, fingerprint)
  654. #define openconnect_get_peer_cert_details(v) openconnect_get_cert_details(v, peer_cert);
  655. #endif
  656. certkey = g_strdup_printf ("certificate:%s:%d",
  657. openconnect_get_hostname(ui_data->vpninfo),
  658. openconnect_get_port(ui_data->vpninfo));
  659. accepted_hash = g_hash_table_lookup (ui_data->secrets, certkey);
  660. if (accepted_hash && !openconnect_check_peer_cert_hash(ui_data->vpninfo, accepted_hash))
  661. goto accepted;
  662. data = g_slice_new(cert_data);
  663. data->ui_data = ui_data; /* FIXME uses global */
  664. data->cert_details = openconnect_get_peer_cert_details(ui_data->vpninfo);
  665. data->reason = reason;
  666. g_mutex_lock(&ui_data->form_mutex);
  667. ui_data->cert_response = CERT_USER_NOT_READY;
  668. g_idle_add((GSourceFunc)user_validate_cert, data);
  669. /* wait for user to accept or cancel */
  670. while (ui_data->cert_response == CERT_USER_NOT_READY)
  671. g_cond_wait(&ui_data->cert_response_changed, &ui_data->form_mutex);
  672. openconnect_free_cert_info(data->ui_data->vpninfo, data->cert_details);
  673. g_slice_free(cert_data, data);
  674. if (ui_data->cert_response == CERT_ACCEPTED)
  675. ret = 0;
  676. else
  677. ret = -EINVAL;
  678. g_mutex_unlock (&ui_data->form_mutex);
  679. accepted:
  680. if (!ret) {
  681. g_hash_table_insert (ui_data->secrets, certkey,
  682. g_strdup(fingerprint));
  683. certkey = NULL;
  684. }
  685. g_free (certkey);
  686. return ret;
  687. }
  688. static gboolean get_autoconnect(GHashTable *secrets)
  689. {
  690. char *autoconnect = g_hash_table_lookup (secrets, "autoconnect");
  691. if (autoconnect && !strcmp(autoconnect, "yes"))
  692. return TRUE;
  693. return FALSE;
  694. }
  695. static gboolean get_save_passwords(GHashTable *secrets)
  696. {
  697. char *save = g_hash_table_lookup (secrets, "save_passwords");
  698. if (save && !strcmp(save, "yes"))
  699. return TRUE;
  700. return FALSE;
  701. }
  702. static int parse_xmlconfig(gchar *xmlconfig)
  703. {
  704. xmlDocPtr xml_doc;
  705. xmlNode *xml_node, *xml_node2;
  706. struct vpnhost *newhost, **list_end;
  707. list_end = &vpnhosts->next;
  708. /* gateway may be there already */
  709. while (*list_end) {
  710. list_end = &(*list_end)->next;
  711. }
  712. xml_doc = xmlReadMemory(xmlconfig, strlen(xmlconfig), "noname.xml", NULL, 0);
  713. xml_node = xmlDocGetRootElement(xml_doc);
  714. for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
  715. if (xml_node->type == XML_ELEMENT_NODE &&
  716. !strcmp((char *)xml_node->name, "ServerList")) {
  717. for (xml_node = xml_node->children; xml_node;
  718. xml_node = xml_node->next) {
  719. if (xml_node->type == XML_ELEMENT_NODE &&
  720. !strcmp((char *)xml_node->name, "HostEntry")) {
  721. int match = 0;
  722. newhost = malloc(sizeof(*newhost));
  723. if (!newhost)
  724. return -ENOMEM;
  725. memset(newhost, 0, sizeof(*newhost));
  726. for (xml_node2 = xml_node->children;
  727. match >= 0 && xml_node2; xml_node2 = xml_node2->next) {
  728. if (xml_node2->type != XML_ELEMENT_NODE)
  729. continue;
  730. if (!strcmp((char *)xml_node2->name, "HostName")) {
  731. char *content = (char *)xmlNodeGetContent(xml_node2);
  732. newhost->hostname = content;
  733. } else if (!strcmp((char *)xml_node2->name, "HostAddress")) {
  734. char *content = (char *)xmlNodeGetContent(xml_node2);
  735. newhost->hostaddress = content;
  736. } else if (!strcmp((char *)xml_node2->name, "UserGroup")) {
  737. char *content = (char *)xmlNodeGetContent(xml_node2);
  738. newhost->usergroup = content;
  739. }
  740. }
  741. if (newhost->hostname && newhost->hostaddress) {
  742. *list_end = newhost;
  743. list_end = &newhost->next;
  744. if (!strcasecmp(newhost->hostaddress, vpnhosts->hostaddress) &&
  745. !strcasecmp(newhost->usergroup ?: "", vpnhosts->usergroup ?: "")) {
  746. /* Remove originally configured host if it's in the list */
  747. struct vpnhost *tmp = vpnhosts->next;
  748. free(vpnhosts);
  749. vpnhosts = tmp;
  750. }
  751. } else
  752. free(newhost);
  753. }
  754. }
  755. break;
  756. }
  757. }
  758. xmlFreeDoc(xml_doc);
  759. return 0;
  760. }
  761. static int get_config (GHashTable *options, GHashTable *secrets,
  762. struct openconnect_info *vpninfo)
  763. {
  764. char *proxy;
  765. char *xmlconfig;
  766. char *hostname;
  767. char *group;
  768. char *csd;
  769. char *sslkey, *cert;
  770. char *csd_wrapper;
  771. char *pem_passphrase_fsid;
  772. char *cafile;
  773. char *token_mode;
  774. char *token_secret;
  775. hostname = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_GATEWAY);
  776. if (!hostname) {
  777. fprintf(stderr, "No gateway configured\n");
  778. return -EINVAL;
  779. }
  780. /* add gateway to host list */
  781. vpnhosts = malloc(sizeof(*vpnhosts));
  782. if (!vpnhosts)
  783. return -ENOMEM;
  784. vpnhosts->hostname = g_strdup(hostname);
  785. group = strchr(vpnhosts->hostname, '/');
  786. if (group) {
  787. *(group++) = 0;
  788. vpnhosts->usergroup = g_strdup(group);
  789. } else
  790. vpnhosts->usergroup = NULL;
  791. vpnhosts->hostaddress = g_strdup (hostname);
  792. vpnhosts->next = NULL;
  793. lasthost = g_hash_table_lookup (secrets, "lasthost");
  794. xmlconfig = g_hash_table_lookup (secrets, "xmlconfig");
  795. if (xmlconfig) {
  796. GChecksum *sha1;
  797. gchar *config_str;
  798. gsize config_len;
  799. const char *sha1_text;
  800. config_str = (gchar *)g_base64_decode (xmlconfig, &config_len);
  801. sha1 = g_checksum_new (G_CHECKSUM_SHA1);
  802. g_checksum_update (sha1, (gpointer) config_str, config_len);
  803. sha1_text = g_checksum_get_string(sha1);
  804. openconnect_set_xmlsha1 (vpninfo, (char *)sha1_text, strlen(sha1_text) + 1);
  805. g_checksum_free(sha1);
  806. parse_xmlconfig (config_str);
  807. }
  808. cafile = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_CACERT);
  809. if (cafile)
  810. openconnect_set_cafile(vpninfo, OC3DUP (cafile));
  811. csd = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_CSD_ENABLE);
  812. if (csd && !strcmp(csd, "yes")) {
  813. /* We're not running as root; we can't setuid(). */
  814. csd_wrapper = g_hash_table_lookup (options,
  815. NM_OPENCONNECT_KEY_CSD_WRAPPER);
  816. if (csd_wrapper && !csd_wrapper[0])
  817. csd_wrapper = NULL;
  818. openconnect_setup_csd(vpninfo, getuid(), 1, OC3DUP (csd_wrapper));
  819. }
  820. proxy = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_PROXY);
  821. if (proxy && proxy[0] && openconnect_set_http_proxy(vpninfo, OC3DUP (proxy)))
  822. return -EINVAL;
  823. cert = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_USERCERT);
  824. sslkey = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_PRIVKEY);
  825. openconnect_set_client_cert (vpninfo, OC3DUP (cert), OC3DUP (sslkey));
  826. pem_passphrase_fsid = g_hash_table_lookup (options,
  827. NM_OPENCONNECT_KEY_PEM_PASSPHRASE_FSID);
  828. if (pem_passphrase_fsid && cert && !strcmp(pem_passphrase_fsid, "yes"))
  829. openconnect_passphrase_from_fsid(vpninfo);
  830. token_mode = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_TOKEN_MODE);
  831. token_secret = g_hash_table_lookup (secrets, NM_OPENCONNECT_KEY_TOKEN_SECRET);
  832. if (!token_secret || !token_secret[0])
  833. token_secret = g_hash_table_lookup (options, NM_OPENCONNECT_KEY_TOKEN_SECRET);
  834. if (token_mode) {
  835. int ret = 0;
  836. if (!strcmp(token_mode, "manual") && token_secret)
  837. ret = __openconnect_set_token_mode(vpninfo, OC_TOKEN_MODE_STOKEN, token_secret);
  838. else if (!strcmp(token_mode, "stokenrc"))
  839. ret = __openconnect_set_token_mode(vpninfo, OC_TOKEN_MODE_STOKEN, NULL);
  840. else if (!strcmp(token_mode, "totp") && token_secret)
  841. ret = __openconnect_set_token_mode(vpninfo, OC_TOKEN_MODE_TOTP, token_secret);
  842. #if OPENCONNECT_CHECK_VER(3,4)
  843. else if (!strcmp(token_mode, "hotp") && token_secret)
  844. ret = __openconnect_set_token_mode(vpninfo, OC_TOKEN_MODE_HOTP, token_secret);
  845. #endif
  846. if (ret)
  847. fprintf(stderr, "Failed to initialize software token: %d\n", ret);
  848. }
  849. return 0;
  850. }
  851. static void populate_vpnhost_combo(auth_ui_data *ui_data)
  852. {
  853. struct vpnhost *host;
  854. int i = 0;
  855. GtkComboBoxText *combo = GTK_COMBO_BOX_TEXT (ui_data->combo);
  856. for (host = vpnhosts; host; host = host->next) {
  857. gtk_combo_box_text_append_text(combo, host->hostname);
  858. if (i == 0 ||
  859. (lasthost && !strcmp(host->hostname, lasthost)))
  860. gtk_combo_box_set_active(GTK_COMBO_BOX (combo), i);
  861. i++;
  862. }
  863. }
  864. #if OPENCONNECT_CHECK_VER(3,4)
  865. static int update_token(void *cbdata, const char *tok)
  866. {
  867. auth_ui_data *ui_data = cbdata;
  868. g_hash_table_insert (ui_data->secrets, g_strdup (NM_OPENCONNECT_KEY_TOKEN_SECRET),
  869. g_strdup(tok));
  870. return 0;
  871. }
  872. #endif
  873. static int write_new_config(void *cbdata, write_config_const char *buf, int buflen)
  874. {
  875. auth_ui_data *ui_data = cbdata;
  876. g_hash_table_insert (ui_data->secrets, g_strdup ("xmlconfig"),
  877. g_base64_encode ((guchar *)buf, buflen));
  878. return 0;
  879. }
  880. static void autocon_toggled(GtkWidget *widget)
  881. {
  882. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  883. gchar *enabled;
  884. if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)))
  885. enabled = g_strdup ("yes");
  886. else
  887. enabled = g_strdup ("no");
  888. g_hash_table_insert (ui_data->secrets, g_strdup ("autoconnect"), enabled);
  889. }
  890. static void savepass_toggled(GtkWidget *widget)
  891. {
  892. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  893. gchar *enabled;
  894. if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget)))
  895. enabled = g_strdup ("yes");
  896. else {
  897. enabled = g_strdup ("no");
  898. secret_password_clear (&openconnect_secret_schema, NULL, NULL, NULL,
  899. "vpn_uuid", ui_data->vpn_uuid,
  900. NULL);
  901. }
  902. g_hash_table_insert (ui_data->secrets, g_strdup ("save_passwords"), enabled);
  903. }
  904. static void scroll_log(GtkTextBuffer *log, GtkTextView *view)
  905. {
  906. GtkTextMark *mark;
  907. g_return_if_fail(GTK_IS_TEXT_VIEW(view));
  908. mark = gtk_text_buffer_get_insert(log);
  909. gtk_text_view_scroll_to_mark(view, mark, 0.0, FALSE, 0.0, 0.0);
  910. }
  911. /* NOTE: write_progress_real() will free the given string */
  912. static gboolean write_progress_real(char *message)
  913. {
  914. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  915. GtkTextIter iter;
  916. g_return_val_if_fail(message, FALSE);
  917. gtk_text_buffer_get_end_iter(ui_data->log, &iter);
  918. gtk_text_buffer_insert(ui_data->log, &iter, message, -1);
  919. g_free(message);
  920. return FALSE;
  921. }
  922. /* NOTE: write_progress_real() will free the given string */
  923. static gboolean write_notice_real(char *message)
  924. {
  925. auth_ui_data *ui_data = _ui_data; /* FIXME global */
  926. g_return_val_if_fail(message, FALSE);
  927. ssl_box_add_notice(ui_data, message);
  928. g_free(message);
  929. return FALSE;
  930. }
  931. /* runs in worker thread */
  932. static void write_progress(void *cbdata, int level, const char *fmt, ...)
  933. {
  934. va_list args;
  935. char *msg;
  936. va_start(args, fmt);
  937. msg = g_strdup_vprintf(fmt, args);
  938. va_end(args);
  939. if (level <= PRG_ERR) {
  940. g_idle_add((GSourceFunc)write_notice_real, g_strdup(msg));
  941. }
  942. if (level <= PRG_DEBUG)
  943. g_idle_add((GSourceFunc)write_progress_real, msg);
  944. else
  945. g_free(msg);
  946. }
  947. static gboolean hash_merge_one (gpointer key, gpointer value, gpointer new_hash)
  948. {
  949. g_hash_table_insert (new_hash, key, value);
  950. return TRUE;
  951. }
  952. static void hash_table_merge (GHashTable *old_hash, GHashTable *new_hash)
  953. {
  954. g_hash_table_foreach_steal (old_hash, &hash_merge_one, new_hash);
  955. }
  956. static gboolean cookie_obtained(auth_ui_data *ui_data)
  957. {
  958. ui_data->getting_cookie = FALSE;
  959. gtk_widget_hide (ui_data->getting_form_label);
  960. if (ui_data->cancelled) {
  961. /* user has chosen a new host, start from beginning */
  962. g_hash_table_remove_all (ui_data->success_secrets);
  963. g_hash_table_remove_all (ui_data->success_passwords);
  964. connect_host(ui_data);
  965. return FALSE;
  966. }
  967. if (ui_data->cookie_retval < 0) {
  968. /* error while getting cookie */
  969. if (ui_data->last_notice_icon) {
  970. gtk_image_set_from_icon_name(GTK_IMAGE (ui_data->last_notice_icon),
  971. "dialog-error",
  972. GTK_ICON_SIZE_DIALOG);
  973. gtk_widget_show_all(ui_data->ssl_box);
  974. gtk_widget_set_sensitive(ui_data->cancel_button, FALSE);
  975. }
  976. } else if (!ui_data->cookie_retval) {
  977. const void *cert;
  978. gchar *key, *value;
  979. /* got cookie */
  980. /* Merge in the secrets which we only wanted to remember if
  981. the connection was successful (lasthost, form entries) */
  982. hash_table_merge (ui_data->success_secrets, ui_data->secrets);
  983. /* Merge in the three *real* secrets that are actually used
  984. by nm-openconnect-service to make the connection */
  985. key = g_strdup (NM_OPENCONNECT_KEY_GATEWAY);
  986. value = g_strdup_printf ("%s:%d",
  987. openconnect_get_hostname(ui_data->vpninfo),
  988. openconnect_get_port(ui_data->vpninfo));
  989. g_hash_table_insert (ui_data->secrets, key, value);
  990. key = g_strdup (NM_OPENCONNECT_KEY_COOKIE);
  991. value = g_strdup (openconnect_get_cookie (ui_data->vpninfo));
  992. g_hash_table_insert (ui_data->secrets, key, value);
  993. openconnect_clear_cookie(ui_data->vpninfo);
  994. #if OPENCONNECT_CHECK_VER(5,0)
  995. cert = openconnect_get_peer_cert_hash (ui_data->vpninfo);
  996. if (cert) {
  997. key = g_strdup (NM_OPENCONNECT_KEY_GWCERT);
  998. value = g_strdup (cert);
  999. g_hash_table_insert (ui_data->secrets, key, value);
  1000. }
  1001. #else
  1002. cert = openconnect_get_peer_cert (ui_data->vpninfo);
  1003. if (cert) {
  1004. key = g_strdup (NM_OPENCONNECT_KEY_GWCERT);
  1005. value = g_malloc0 (41);
  1006. openconnect_get_cert_sha1(ui_data->vpninfo, (void *)cert, value);
  1007. g_hash_table_insert (ui_data->secrets, key, value);
  1008. }
  1009. #endif
  1010. if (get_save_passwords(ui_data->secrets)) {
  1011. g_hash_table_foreach(ui_data->success_passwords,
  1012. keyring_store_passwords,
  1013. NULL);
  1014. }
  1015. gtk_main_quit();
  1016. } else {
  1017. /* no cookie; user cancellation */
  1018. gtk_widget_show (ui_data->no_form_label);
  1019. }
  1020. g_hash_table_remove_all (ui_data->success_secrets);
  1021. g_hash_table_remove_all (ui_data->success_passwords);
  1022. return FALSE;
  1023. }
  1024. static gpointer obtain_cookie (auth_ui_data *ui_data)
  1025. {
  1026. int ret;
  1027. char cancelbuf;
  1028. ret = openconnect_obtain_cookie(ui_data->vpninfo);
  1029. /* Suck out the poison */
  1030. while (read(ui_data->cancel_pipes[0], &cancelbuf, 1) == 1)
  1031. ;
  1032. ui_data->cookie_retval = ret;
  1033. g_idle_add ((GSourceFunc)cookie_obtained, ui_data);
  1034. return NULL;
  1035. }
  1036. static void connect_host(auth_ui_data *ui_data)
  1037. {
  1038. GThread *thread;
  1039. vpnhost *host;
  1040. int i;
  1041. int host_nr;
  1042. char cancelbuf;
  1043. ui_data->cancelled = FALSE;
  1044. ui_data->getting_cookie = TRUE;
  1045. g_mutex_lock (&ui_data->form_mutex);
  1046. ui_data->form_retval = NULL;
  1047. g_mutex_unlock (&ui_data->form_mutex);
  1048. ssl_box_clear(ui_data);
  1049. gtk_widget_show(ui_data->getting_form_label);
  1050. gtk_widget_set_sensitive (ui_data->cancel_button, TRUE);
  1051. while (read(ui_data->cancel_pipes[0], &cancelbuf, 1) == 1)
  1052. ;
  1053. /* reset ssl context.
  1054. * TODO: this is probably not the way to go... */
  1055. openconnect_reset_ssl(ui_data->vpninfo);
  1056. host_nr = gtk_combo_box_get_active(GTK_COMBO_BOX(ui_data->combo));
  1057. host = vpnhosts;
  1058. for (i = 0; i < host_nr; i++)
  1059. host = host->next;
  1060. if (openconnect_parse_url(ui_data->vpninfo, host->hostaddress)) {
  1061. fprintf(stderr, "Failed to parse server URL '%s'\n",
  1062. host->hostaddress);
  1063. openconnect_set_hostname (ui_data->vpninfo, OC3DUP (host->hostaddress));
  1064. }
  1065. if (!openconnect_get_urlpath(ui_data->vpninfo) && host->usergroup)
  1066. openconnect_set_urlpath(ui_data->vpninfo, OC3DUP (host->usergroup));
  1067. g_hash_table_insert (ui_data->success_secrets, g_strdup("lasthost"),
  1068. g_strdup(host->hostname));
  1069. thread = g_thread_new("obtain_cookie", (GThreadFunc)obtain_cookie, ui_data);
  1070. g_thread_unref(thread);
  1071. }
  1072. static void queue_connect_host(auth_ui_data *ui_data)
  1073. {
  1074. ssl_box_clear(ui_data);
  1075. gtk_widget_show(ui_data->getting_form_label);
  1076. gtk_widget_hide(ui_data->no_form_label);
  1077. if (!ui_data->getting_cookie) {
  1078. connect_host(ui_data);
  1079. } else if (!ui_data->cancelled) {
  1080. /* set state to cancelled. Current challenge-response-
  1081. * conversation will not be shown to user, and cookie_obtained()
  1082. * will start a new one conversation */
  1083. ui_data->cancelled = TRUE;
  1084. gtk_dialog_response(GTK_DIALOG(ui_data->dialog), AUTH_DIALOG_RESPONSE_CANCEL);
  1085. }
  1086. }
  1087. static void dialog_response (GtkDialog *dialog, int response, auth_ui_data *ui_data)
  1088. {
  1089. switch (response) {
  1090. case AUTH_DIALOG_RESPONSE_CANCEL:
  1091. if (write(ui_data->cancel_pipes[1], "x", 1) < 0) {
  1092. /* Pfft. Not a lot we can do about it */
  1093. }
  1094. /* Fall through... */
  1095. case AUTH_DIALOG_RESPONSE_LOGIN:
  1096. ssl_box_clear(ui_data);
  1097. if (ui_data->getting_cookie)
  1098. gtk_widget_show (ui_data->getting_form_label);
  1099. g_mutex_lock (&ui_data->form_mutex);
  1100. ui_data->form_retval = GINT_TO_POINTER(response);
  1101. g_cond_signal (&ui_data->form_retval_changed);
  1102. g_mutex_unlock (&ui_data->form_mutex);
  1103. break;
  1104. case GTK_RESPONSE_CLOSE:
  1105. gtk_main_quit();
  1106. break;
  1107. default:
  1108. ;
  1109. }
  1110. }
  1111. static void cancel_clicked (GtkButton *btn, auth_ui_data *ui_data)
  1112. {
  1113. gtk_dialog_response (GTK_DIALOG(ui_data->dialog), AUTH_DIALOG_RESPONSE_CANCEL);
  1114. }
  1115. static void login_clicked (GtkButton *btn, auth_ui_data *ui_data)
  1116. {
  1117. gtk_dialog_response (GTK_DIALOG(ui_data->dialog), AUTH_DIALOG_RESPONSE_LOGIN);
  1118. }
  1119. static void build_main_dialog(auth_ui_data *ui_data)
  1120. {
  1121. char *title;
  1122. GtkWidget *vbox, *hbox, *label, *frame, *frame_box;
  1123. GtkWidget *exp, *scrolled, *view, *autocon, *save_pass;
  1124. gtk_window_set_default_icon_name("dialog-password");
  1125. title = get_title(ui_data->vpn_name);
  1126. ui_data->dialog = gtk_dialog_new_with_buttons(title, NULL, GTK_DIALOG_MODAL,
  1127. _("_Close"), GTK_RESPONSE_CLOSE,
  1128. NULL);
  1129. g_signal_connect (ui_data->dialog, "response", G_CALLBACK(dialog_response), ui_data);
  1130. gtk_window_set_default_size(GTK_WINDOW(ui_data->dialog), 350, 300);
  1131. g_signal_connect_swapped(ui_data->dialog, "destroy",
  1132. G_CALLBACK(gtk_main_quit), NULL);
  1133. g_free(title);
  1134. vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
  1135. gtk_box_pack_start(GTK_BOX (gtk_dialog_get_content_area(GTK_DIALOG (ui_data->dialog))), vbox, TRUE, TRUE, 0);
  1136. gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
  1137. gtk_widget_show(vbox);
  1138. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
  1139. gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  1140. gtk_widget_show(hbox);
  1141. label = gtk_label_new(_("VPN host"));
  1142. gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
  1143. gtk_widget_show(label);
  1144. ui_data->combo = gtk_combo_box_text_new();
  1145. populate_vpnhost_combo(ui_data);
  1146. gtk_box_pack_start(GTK_BOX(hbox), ui_data->combo, TRUE, TRUE, 0);
  1147. g_signal_connect_swapped(ui_data->combo, "changed",
  1148. G_CALLBACK(queue_connect_host), ui_data);
  1149. gtk_widget_show(ui_data->combo);
  1150. ui_data->connect_button = gtk_button_new_with_mnemonic(_("C_onnect"));
  1151. gtk_box_pack_end(GTK_BOX(hbox), ui_data->connect_button, FALSE, FALSE, 0);
  1152. gtk_widget_grab_focus(ui_data->connect_button);
  1153. g_signal_connect_swapped(ui_data->connect_button, "clicked",
  1154. G_CALLBACK(queue_connect_host), ui_data);
  1155. gtk_widget_show(ui_data->connect_button);
  1156. if (vpnhosts->next) {
  1157. autocon = gtk_check_button_new_with_label(_("Automatically start connecting next time"));
  1158. gtk_box_pack_start(GTK_BOX(vbox), autocon, FALSE, FALSE, 0);
  1159. if (get_autoconnect (ui_data->secrets))
  1160. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(autocon), 1);
  1161. g_signal_connect(autocon, "toggled", G_CALLBACK(autocon_toggled), NULL);
  1162. gtk_widget_show(autocon);
  1163. }
  1164. frame = gtk_frame_new(NULL);
  1165. gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
  1166. gtk_widget_set_size_request(frame, -1, -1);
  1167. gtk_widget_show(frame);
  1168. frame_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
  1169. gtk_container_set_border_width(GTK_CONTAINER(frame_box), 8);
  1170. gtk_container_add(GTK_CONTAINER(frame), frame_box);
  1171. gtk_widget_show(frame_box);
  1172. ui_data->no_form_label = gtk_label_new(_("Select a host to fetch the login form"));
  1173. gtk_widget_set_sensitive(ui_data->no_form_label, FALSE);
  1174. gtk_box_pack_start(GTK_BOX(frame_box), ui_data->no_form_label, FALSE, FALSE, 0);
  1175. gtk_widget_show(ui_data->no_form_label);
  1176. ui_data->getting_form_label = gtk_label_new(_("Contacting host, please wait..."));
  1177. gtk_widget_set_sensitive(ui_data->getting_form_label, FALSE);
  1178. gtk_box_pack_start(GTK_BOX(frame_box), ui_data->getting_form_label, FALSE, FALSE, 0);
  1179. ui_data->ssl_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
  1180. gtk_box_pack_start(GTK_BOX(frame_box), ui_data->ssl_box, FALSE, FALSE, 0);
  1181. gtk_widget_show(ui_data->ssl_box);
  1182. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  1183. gtk_box_pack_end(GTK_BOX(frame_box), hbox, FALSE, FALSE, 0);
  1184. gtk_widget_show(hbox);
  1185. ui_data->login_button = gtk_button_new_with_mnemonic(_("_Login"));
  1186. gtk_box_pack_end(GTK_BOX(hbox), ui_data->login_button, FALSE, FALSE, 0);
  1187. g_signal_connect (ui_data->login_button, "clicked", G_CALLBACK(login_clicked), ui_data);
  1188. gtk_widget_set_sensitive (ui_data->login_button, FALSE);
  1189. gtk_widget_show(ui_data->login_button);
  1190. ui_data->cancel_button = gtk_button_new_with_mnemonic(_("_Cancel"));
  1191. gtk_box_pack_end(GTK_BOX(hbox), ui_data->cancel_button, FALSE, FALSE, 0);
  1192. g_signal_connect (ui_data->cancel_button, "clicked", G_CALLBACK(cancel_clicked), ui_data);
  1193. gtk_widget_set_sensitive (ui_data->cancel_button, FALSE);
  1194. gtk_widget_show(ui_data->cancel_button);
  1195. save_pass = gtk_check_button_new_with_label(_("Save passwords"));
  1196. gtk_box_pack_start(GTK_BOX(hbox), save_pass, FALSE, FALSE, 0);
  1197. if (get_save_passwords (ui_data->secrets))
  1198. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_pass), 1);
  1199. g_signal_connect(save_pass, "toggled", G_CALLBACK(savepass_toggled), NULL);
  1200. gtk_widget_show(save_pass);
  1201. exp = gtk_expander_new(_("Log"));
  1202. gtk_box_pack_end(GTK_BOX(vbox), exp, FALSE, FALSE, 0);
  1203. gtk_widget_show(exp);
  1204. scrolled = gtk_scrolled_window_new(NULL, NULL);
  1205. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
  1206. GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  1207. gtk_widget_set_size_request(scrolled, -1, 75);
  1208. gtk_container_add(GTK_CONTAINER(exp), scrolled);
  1209. gtk_widget_show(scrolled);
  1210. view = gtk_text_view_new();
  1211. gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
  1212. gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
  1213. gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
  1214. gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 5);
  1215. gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 5);
  1216. gtk_text_view_set_indent(GTK_TEXT_VIEW(view), -10);
  1217. gtk_container_add(GTK_CONTAINER(scrolled), view);
  1218. gtk_widget_show(view);
  1219. ui_data->log = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
  1220. g_signal_connect(ui_data->log, "changed", G_CALLBACK(scroll_log), view);
  1221. }
  1222. static auth_ui_data *init_ui_data (char *vpn_name, GHashTable *options, GHashTable *secrets, char *vpn_uuid)
  1223. {
  1224. auth_ui_data *ui_data;
  1225. ui_data = g_slice_new0(auth_ui_data);
  1226. ui_data->form_entries = g_queue_new();
  1227. g_mutex_init(&ui_data->form_mutex);
  1228. g_cond_init(&ui_data->form_retval_changed);
  1229. g_cond_init(&ui_data->form_shown_changed);
  1230. g_cond_init(&ui_data->cert_response_changed);
  1231. ui_data->vpn_name = vpn_name;
  1232. ui_data->vpn_uuid = vpn_uuid;
  1233. ui_data->options = options;
  1234. ui_data->secrets = secrets;
  1235. ui_data->success_secrets = g_hash_table_new_full (g_str_hash, g_str_equal,
  1236. g_free, g_free);
  1237. ui_data->success_passwords = g_hash_table_new_full (g_str_hash, g_str_equal,
  1238. g_free, keyring_password_free);
  1239. if (pipe(ui_data->cancel_pipes)) {
  1240. /* This should never happen, and the world is probably about
  1241. to come crashing down around our ears. But attempt to cope
  1242. by just disabling the cancellation support... */
  1243. ui_data->cancel_pipes[0] = -1;
  1244. ui_data->cancel_pipes[1] = -1;
  1245. }
  1246. g_unix_set_fd_nonblocking(ui_data->cancel_pipes[0], TRUE, NULL);
  1247. g_unix_set_fd_nonblocking(ui_data->cancel_pipes[1], TRUE, NULL);
  1248. ui_data->vpninfo = (void *)openconnect_vpninfo_new("OpenConnect VPN Agent (NetworkManager)",
  1249. validate_peer_cert, write_new_config,
  1250. nm_process_auth_form, write_progress,
  1251. ui_data);
  1252. #if OPENCONNECT_CHECK_VER(1,4)
  1253. openconnect_set_cancel_fd (ui_data->vpninfo, ui_data->cancel_pipes[0]);
  1254. #endif
  1255. #if 0
  1256. ui_data->vpninfo->proxy_factory = px_proxy_factory_new();
  1257. #endif
  1258. return ui_data;
  1259. }
  1260. static void wait_for_quit (void)
  1261. {
  1262. GString *str;
  1263. char c;
  1264. ssize_t n;
  1265. time_t start;
  1266. str = g_string_sized_new (10);
  1267. start = time (NULL);
  1268. do {
  1269. errno = 0;
  1270. n = read (0, &c, 1);
  1271. if (n == 0 || (n < 0 && errno == EAGAIN))
  1272. g_usleep (G_USEC_PER_SEC / 10);
  1273. else if (n == 1) {
  1274. g_string_append_c (str, c);
  1275. if (strstr (str->str, "QUIT") || (str->len > 10))
  1276. break;
  1277. } else
  1278. break;
  1279. } while (time (NULL) < start + 20);
  1280. g_string_free (str, TRUE);
  1281. }
  1282. static struct option long_options[] = {
  1283. {"reprompt", 0, 0, 'r'},
  1284. {"uuid", 1, 0, 'u'},
  1285. {"name", 1, 0, 'n'},
  1286. {"service", 1, 0, 's'},
  1287. {"allow-interaction", 0, 0, 'i'},
  1288. {NULL, 0, 0, 0},
  1289. };
  1290. int main (int argc, char **argv)
  1291. {
  1292. char *vpn_name = NULL, *vpn_uuid = NULL, *vpn_service = NULL;
  1293. GHashTable *options = NULL, *secrets = NULL;
  1294. gboolean allow_interaction = FALSE;
  1295. GHashTableIter iter;
  1296. gchar *key, *value;
  1297. int opt;
  1298. while ((opt = getopt_long(argc, argv, "ru:n:s:i", long_options, NULL))) {
  1299. if (opt < 0)
  1300. break;
  1301. switch(opt) {
  1302. case 'r':
  1303. /* Reprompt does nothing */
  1304. break;
  1305. case 'i':
  1306. allow_interaction = TRUE;
  1307. break;
  1308. case 'u':
  1309. vpn_uuid = optarg;
  1310. break;
  1311. case 'n':
  1312. vpn_name = optarg;
  1313. break;
  1314. case 's':
  1315. vpn_service = optarg;
  1316. break;
  1317. default:
  1318. fprintf(stderr, "Unknown option\n");
  1319. return 1;
  1320. }
  1321. }
  1322. if (!allow_interaction)
  1323. return 0;
  1324. if (optind != argc) {
  1325. fprintf(stderr, "Superfluous command line options\n");
  1326. return 1;
  1327. }
  1328. if (!vpn_uuid || !vpn_name || !vpn_service) {
  1329. fprintf (stderr, "Have to supply UUID, name, and service\n");
  1330. return 1;
  1331. }
  1332. if (strcmp(vpn_service, NM_DBUS_SERVICE_OPENCONNECT) != 0) {
  1333. fprintf (stderr, "This dialog only works with the '%s' service\n",
  1334. NM_DBUS_SERVICE_OPENCONNECT);
  1335. return 1;
  1336. }
  1337. if (!nm_vpn_plugin_utils_read_vpn_details (0, &options, &secrets)) {
  1338. fprintf (stderr, "Failed to read '%s' (%s) data and secrets from stdin.\n",
  1339. vpn_name, vpn_uuid);
  1340. return 1;
  1341. }
  1342. gtk_init(0, NULL);
  1343. _ui_data = init_ui_data(vpn_name, options, secrets, vpn_uuid);
  1344. if (get_config(options, secrets, _ui_data->vpninfo)) {
  1345. fprintf(stderr, "Failed to find VPN UUID %s\n", vpn_uuid);
  1346. return 1;
  1347. }
  1348. #if OPENCONNECT_CHECK_VER(3,4)
  1349. openconnect_set_token_callbacks (_ui_data->vpninfo, _ui_data, NULL, update_token);
  1350. #endif
  1351. build_main_dialog(_ui_data);
  1352. openconnect_init_ssl();
  1353. /* Start connecting now if there's only one host. Or if configured to */
  1354. if (!vpnhosts->next || get_autoconnect (secrets))
  1355. queue_connect_host(_ui_data);
  1356. gtk_window_present(GTK_WINDOW(_ui_data->dialog));
  1357. gtk_main();
  1358. /* Dump all secrets to stdout */
  1359. g_hash_table_iter_init (&iter, _ui_data->secrets);
  1360. while (g_hash_table_iter_next (&iter, (gpointer *)&key,
  1361. (gpointer *)&value))
  1362. printf("%s\n%s\n", key, value);
  1363. printf("\n\n");
  1364. fflush(stdout);
  1365. wait_for_quit ();
  1366. return 0;
  1367. }