Blame src/examples/digest_auth_example.c

Packit 875988
/*
Packit 875988
     This file is part of libmicrohttpd
Packit 875988
     Copyright (C) 2010 Christian Grothoff (and other contributing authors)
Packit 875988
Packit 875988
     This library is free software; you can redistribute it and/or
Packit 875988
     modify it under the terms of the GNU Lesser General Public
Packit 875988
     License as published by the Free Software Foundation; either
Packit 875988
     version 2.1 of the License, or (at your option) any later version.
Packit 875988
Packit 875988
     This library is distributed in the hope that it will be useful,
Packit 875988
     but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 875988
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 875988
     Lesser General Public License for more details.
Packit 875988
Packit 875988
     You should have received a copy of the GNU Lesser General Public
Packit 875988
     License along with this library; if not, write to the Free Software
Packit 875988
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 875988
*/
Packit 875988
/**
Packit 875988
 * @file digest_auth_example.c
Packit 875988
 * @brief minimal example for how to use digest auth with libmicrohttpd
Packit 875988
 * @author Amr Ali
Packit 875988
 */
Packit 875988
Packit 875988
#include "platform.h"
Packit 875988
#include <microhttpd.h>
Packit 875988
#include <stdlib.h>
Packit 875988
Packit 875988
#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>"
Packit 875988
Packit 875988
#define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>"
Packit 875988
Packit 875988
#define MY_OPAQUE_STR "11733b200778ce33060f31c9af70a870ba96ddd4"
Packit 875988
Packit 875988
static int
Packit 875988
ahc_echo (void *cls,
Packit 875988
          struct MHD_Connection *connection,
Packit 875988
          const char *url,
Packit 875988
          const char *method,
Packit 875988
          const char *version,
Packit 875988
          const char *upload_data, size_t *upload_data_size, void **ptr)
Packit 875988
{
Packit 875988
  struct MHD_Response *response;
Packit 875988
  char *username;
Packit 875988
  const char *password = "testpass";
Packit 875988
  const char *realm = "test@example.com";
Packit 875988
  int ret;
Packit 875988
  (void)cls;               /* Unused. Silent compiler warning. */
Packit 875988
  (void)url;               /* Unused. Silent compiler warning. */
Packit 875988
  (void)method;            /* Unused. Silent compiler warning. */
Packit 875988
  (void)version;           /* Unused. Silent compiler warning. */
Packit 875988
  (void)upload_data;       /* Unused. Silent compiler warning. */
Packit 875988
  (void)upload_data_size;  /* Unused. Silent compiler warning. */
Packit 875988
  (void)ptr;               /* Unused. Silent compiler warning. */
Packit 875988
Packit 875988
  username = MHD_digest_auth_get_username(connection);
Packit 875988
  if (NULL == username)
Packit 875988
    {
Packit 875988
      response = MHD_create_response_from_buffer(strlen (DENIED),
Packit 875988
						 DENIED,
Packit 875988
						 MHD_RESPMEM_PERSISTENT);
Packit 875988
      ret = MHD_queue_auth_fail_response(connection, realm,
Packit 875988
					 MY_OPAQUE_STR,
Packit 875988
					 response,
Packit 875988
					 MHD_NO);
Packit 875988
      MHD_destroy_response(response);
Packit 875988
      return ret;
Packit 875988
    }
Packit 875988
  ret = MHD_digest_auth_check(connection, realm,
Packit 875988
			      username,
Packit 875988
			      password,
Packit 875988
			      300);
Packit 875988
  MHD_free (username);
Packit 875988
  if ( (ret == MHD_INVALID_NONCE) ||
Packit 875988
       (ret == MHD_NO) )
Packit 875988
    {
Packit 875988
      response = MHD_create_response_from_buffer(strlen (DENIED),
Packit 875988
						 DENIED,
Packit 875988
						 MHD_RESPMEM_PERSISTENT);
Packit 875988
      if (NULL == response)
Packit 875988
	return MHD_NO;
Packit 875988
      ret = MHD_queue_auth_fail_response(connection, realm,
Packit 875988
					 MY_OPAQUE_STR,
Packit 875988
					 response,
Packit 875988
					 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
Packit 875988
      MHD_destroy_response(response);
Packit 875988
      return ret;
Packit 875988
    }
Packit 875988
  response = MHD_create_response_from_buffer(strlen(PAGE), PAGE,
Packit 875988
					     MHD_RESPMEM_PERSISTENT);
Packit 875988
  ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
Packit 875988
  MHD_destroy_response(response);
Packit 875988
  return ret;
Packit 875988
}
Packit 875988
Packit 875988
Packit 875988
int
Packit 875988
main (int argc, char *const *argv)
Packit 875988
{
Packit 875988
  int fd;
Packit 875988
  char rnd[8];
Packit 875988
  ssize_t len;
Packit 875988
  size_t off;
Packit 875988
  struct MHD_Daemon *d;
Packit 875988
Packit 875988
  if (argc != 2)
Packit 875988
    {
Packit 875988
      printf ("%s PORT\n", argv[0]);
Packit 875988
      return 1;
Packit 875988
    }
Packit 875988
  fd = open("/dev/urandom", O_RDONLY);
Packit 875988
  if (-1 == fd)
Packit 875988
    {
Packit 875988
      fprintf (stderr, "Failed to open `%s': %s\n",
Packit 875988
	       "/dev/urandom",
Packit 875988
	       strerror (errno));
Packit 875988
      return 1;
Packit 875988
    }
Packit 875988
  off = 0;
Packit 875988
  while (off < 8)
Packit 875988
    {
Packit 875988
      len = read(fd, rnd, 8);
Packit 875988
      if (len == -1)
Packit 875988
	{
Packit 875988
	  fprintf (stderr, "Failed to read `%s': %s\n",
Packit 875988
		   "/dev/urandom",
Packit 875988
		   strerror (errno));
Packit 875988
	  (void) close (fd);
Packit 875988
	  return 1;
Packit 875988
	}
Packit 875988
      off += len;
Packit 875988
    }
Packit 875988
  (void) close(fd);
Packit 875988
  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
Packit 875988
                        atoi (argv[1]),
Packit 875988
                        NULL, NULL, &ahc_echo, PAGE,
Packit 875988
			MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd,
Packit 875988
			MHD_OPTION_NONCE_NC_SIZE, 300,
Packit 875988
			MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
Packit 875988
			MHD_OPTION_END);
Packit 875988
  if (d == NULL)
Packit 875988
    return 1;
Packit 875988
  (void) getc (stdin);
Packit 875988
  MHD_stop_daemon (d);
Packit 875988
  return 0;
Packit 875988
}
Packit 875988
Packit 875988
/* end of digest_auth_example.c */