1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright 2012 Red Hat, Inc.
4 */
5
6 #include <locale.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <glib.h>
12 #include "libsoup/soup.h"
13
14 const char *helper_protocol, *username, *domain;
15 gboolean use_cached_creds;
16
17 static GOptionEntry entries[] = {
18 { "helper-protocol", 0, 0,
19 G_OPTION_ARG_STRING, &helper_protocol,
20 NULL, NULL },
21 { "use-cached-creds", 0, 0,
22 G_OPTION_ARG_NONE, &use_cached_creds,
23 NULL, NULL },
24 { "username", 0, 0,
25 G_OPTION_ARG_STRING, &username,
26 NULL, NULL },
27 { "domain", 0, 0,
28 G_OPTION_ARG_STRING, &domain,
29 NULL, NULL },
30 { NULL }
31 };
32
33 int
main(int argc,char ** argv)34 main (int argc, char **argv)
35 {
36 GOptionContext *opts;
37 char buf[256], *header;
38 SoupMessage *msg;
39 SoupAuth *auth;
40
41 /* Don't recurse */
42 g_setenv ("SOUP_NTLM_AUTH_DEBUG", "", TRUE);
43
44 setlocale (LC_ALL, "");
45
46 opts = g_option_context_new (NULL);
47 g_option_context_add_main_entries (opts, entries, NULL);
48 if (!g_option_context_parse (opts, &argc, &argv, NULL)) {
49 g_printerr ("Bad arguments\n");
50 exit (1);
51 }
52 g_option_context_free (opts);
53
54 if (!username || !use_cached_creds || !helper_protocol ||
55 !g_str_equal (helper_protocol, "ntlmssp-client-1")) {
56 g_printerr ("Wrong arguments; this program is only intended for use by ntlm-test\n");
57 exit (1);
58 }
59
60 msg = soup_message_new ("GET", "http://localhost/");
61 auth = NULL;
62
63 while (fgets (buf, sizeof (buf), stdin)) {
64 if (strchr (buf, '\n'))
65 *strchr (buf, '\n') = '\0';
66 if (!strcmp (buf, "YR")) {
67 if (g_getenv ("SOUP_NTLM_AUTH_DEBUG_NOCREDS")) {
68 g_print ("PW\n");
69 continue;
70 }
71
72 g_clear_object (&auth);
73 auth = g_object_new (SOUP_TYPE_AUTH_NTLM, NULL);
74 header = soup_auth_get_authorization (auth, msg);
75 g_print ("YR %s\n", header + 5);
76 g_free (header);
77 } else if (g_str_has_prefix (buf, "TT ")) {
78 header = g_strdup_printf ("NTLM %s\n", buf + 3);
79 if (!soup_auth_update (auth, msg, header)) {
80 g_printerr ("Bad challenge\n");
81 exit (1);
82 }
83 g_free (header);
84
85 soup_auth_authenticate (auth, username, "password");
86 header = soup_auth_get_authorization (auth, msg);
87 if (!header) {
88 g_printerr ("Internal authentication failure\n");
89 exit (1);
90 }
91 g_print ("KK %s\n", header + 5);
92 g_free (header);
93 } else {
94 g_printerr ("Unexpected command\n");
95 exit (1);
96 }
97 }
98
99 g_object_unref (msg);
100 g_clear_object (&auth);
101
102 return 0;
103 }
104