Blame tests/ntlm-test-helper.c

rpm-build 4f3c61
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
rpm-build 4f3c61
/*
rpm-build 4f3c61
 * Copyright 2012 Red Hat, Inc.
rpm-build 4f3c61
 */
rpm-build 4f3c61
rpm-build 4f3c61
#include <locale.h>
rpm-build 4f3c61
#include <stdio.h>
rpm-build 4f3c61
#include <stdlib.h>
rpm-build 4f3c61
#include <string.h>
rpm-build 4f3c61
rpm-build 4f3c61
#include <glib.h>
rpm-build 4f3c61
#include "libsoup/soup.h"
rpm-build 4f3c61
rpm-build 4f3c61
const char *helper_protocol, *username, *domain;
rpm-build 4f3c61
gboolean use_cached_creds;
rpm-build 4f3c61
rpm-build 4f3c61
static GOptionEntry entries[] = {
rpm-build 4f3c61
	{ "helper-protocol", 0, 0,
rpm-build 4f3c61
	  G_OPTION_ARG_STRING, &helper_protocol,
rpm-build 4f3c61
	  NULL, NULL },
rpm-build 4f3c61
	{ "use-cached-creds", 0, 0,
rpm-build 4f3c61
	  G_OPTION_ARG_NONE, &use_cached_creds,
rpm-build 4f3c61
	  NULL, NULL },
rpm-build 4f3c61
	{ "username", 0, 0,
rpm-build 4f3c61
	  G_OPTION_ARG_STRING, &username,
rpm-build 4f3c61
	  NULL, NULL },
rpm-build 4f3c61
	{ "domain", 0, 0,
rpm-build 4f3c61
	  G_OPTION_ARG_STRING, &domain,
rpm-build 4f3c61
	  NULL, NULL },
rpm-build 4f3c61
	{ NULL }
rpm-build 4f3c61
};
rpm-build 4f3c61
rpm-build 4f3c61
int
rpm-build 4f3c61
main (int argc, char **argv)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GOptionContext *opts;
rpm-build 4f3c61
	char buf[256], *header;
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	SoupAuth *auth;
rpm-build 4f3c61
rpm-build 4f3c61
	/* Don't recurse */
rpm-build 4f3c61
	g_setenv ("SOUP_NTLM_AUTH_DEBUG", "", TRUE);
rpm-build 4f3c61
rpm-build 4f3c61
	setlocale (LC_ALL, "");
rpm-build 4f3c61
rpm-build 4f3c61
	opts = g_option_context_new (NULL);
rpm-build 4f3c61
	g_option_context_add_main_entries (opts, entries, NULL);
rpm-build 4f3c61
	if (!g_option_context_parse (opts, &argc, &argv, NULL)) {
rpm-build 4f3c61
		g_printerr ("Bad arguments\n");
rpm-build 4f3c61
		exit (1);
rpm-build 4f3c61
	}
rpm-build 4f3c61
	g_option_context_free (opts);
rpm-build 4f3c61
rpm-build 4f3c61
	if (!username || !use_cached_creds || !helper_protocol ||
rpm-build 4f3c61
	    !g_str_equal (helper_protocol, "ntlmssp-client-1")) {
rpm-build 4f3c61
		g_printerr ("Wrong arguments; this program is only intended for use by ntlm-test\n");
rpm-build 4f3c61
		exit (1);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	msg = soup_message_new ("GET", "http://localhost/");
rpm-build 4f3c61
	auth = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	while (fgets (buf, sizeof (buf), stdin)) {
rpm-build 4f3c61
		if (strchr (buf, '\n'))
rpm-build 4f3c61
			*strchr (buf, '\n') = '\0';
rpm-build 4f3c61
		if (!strcmp (buf, "YR")) {
rpm-build 4f3c61
			if (g_getenv ("SOUP_NTLM_AUTH_DEBUG_NOCREDS")) {
rpm-build 4f3c61
				g_print ("PW\n");
rpm-build 4f3c61
				continue;
rpm-build 4f3c61
			}
rpm-build 4f3c61
rpm-build 4f3c61
			g_clear_object (&auth);
rpm-build 4f3c61
			auth = g_object_new (SOUP_TYPE_AUTH_NTLM, NULL);
rpm-build 4f3c61
			header = soup_auth_get_authorization (auth, msg);
rpm-build 4f3c61
			g_print ("YR %s\n", header + 5);
rpm-build 4f3c61
			g_free (header);
rpm-build 4f3c61
		} else if (g_str_has_prefix (buf, "TT ")) {
rpm-build 4f3c61
			header = g_strdup_printf ("NTLM %s\n", buf + 3);
rpm-build 4f3c61
			if (!soup_auth_update (auth, msg, header)) {
rpm-build 4f3c61
				g_printerr ("Bad challenge\n");
rpm-build 4f3c61
				exit (1);
rpm-build 4f3c61
			}
rpm-build 4f3c61
			g_free (header);
rpm-build 4f3c61
rpm-build 4f3c61
			soup_auth_authenticate (auth, username, "password");
rpm-build 4f3c61
			header = soup_auth_get_authorization (auth, msg);
rpm-build 4f3c61
			if (!header) {
rpm-build 4f3c61
				g_printerr ("Internal authentication failure\n");
rpm-build 4f3c61
				exit (1);
rpm-build 4f3c61
			}
rpm-build 4f3c61
			g_print ("KK %s\n", header + 5);
rpm-build 4f3c61
			g_free (header);
rpm-build 4f3c61
		} else {
rpm-build 4f3c61
			g_printerr ("Unexpected command\n");
rpm-build 4f3c61
			exit (1);
rpm-build 4f3c61
		}
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	g_object_unref (msg);
rpm-build 4f3c61
	g_clear_object (&auth);
rpm-build 4f3c61
rpm-build 4f3c61
	return 0;
rpm-build 4f3c61
}