Blame view

examples/muauth.c 3.06 KB
Sergey Poznyakoff authored
1
/* GNU Mailutils -- a suite of utilities for electronic mail
Sergey Poznyakoff authored
2
   Copyright (C) 2006-2007, 2010-2012, 2014-2017 Free Software
3
   Foundation, Inc.
Sergey Poznyakoff authored
4 5 6

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
Sergey Poznyakoff authored
8 9 10 11 12 13 14 15
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
Sergey Poznyakoff authored
17 18 19 20 21 22 23 24 25 26 27 28 29 30

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <mailutils/mailutils.h>

enum mu_auth_key_type key_type = mu_auth_key_name;
char *password;

31 32
static void
use_uid (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
Sergey Poznyakoff authored
33
{
34 35
  key_type = mu_auth_key_uid;
}
Sergey Poznyakoff authored
36

37 38 39 40 41
static void
use_name (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  key_type = mu_auth_key_name;
}
Sergey Poznyakoff authored
42

43 44 45 46 47 48 49 50 51 52 53 54
static struct mu_option muauth_options[] = {
  { "password", 'p', "STRING", MU_OPTION_DEFAULT,
    "user password",
    mu_c_string, &password },
  { "uid", 'u', NULL, MU_OPTION_DEFAULT,
    "test getpwuid functions",
    mu_c_string, NULL, use_uid },
  { "name", 'n', NULL, MU_OPTION_DEFAULT,
    "test getpwnam functions",
    mu_c_string, NULL, use_name },
  MU_OPTION_END
}, *options[] = { muauth_options, NULL };
Sergey Poznyakoff authored
55

56 57 58 59 60
static char *capa[] = {
  "auth",
  "debug",
  NULL
};
Sergey Poznyakoff authored
61

62
static struct mu_cli_setup cli = {
Sergey Poznyakoff authored
63 64
  options,
  NULL,
65 66
  "muauth -- test mailutils authentication and authorization schemes",
  "key"
Sergey Poznyakoff authored
67 68 69 70 71
};
           
int
main (int argc, char * argv [])
{
72
  int rc;
Sergey Poznyakoff authored
73 74 75 76 77
  struct mu_auth_data *auth;
  void *key;
  uid_t uid;
  
  MU_AUTH_REGISTER_ALL_MODULES ();
78

79 80 81
  mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);

  if (argc == 0)
Sergey Poznyakoff authored
82 83
    {
      mu_error ("not enough arguments, try `%s --help' for more info",
84
		mu_program_name);
Sergey Poznyakoff authored
85 86 87 88 89
      return 1;
    }

  if (key_type == mu_auth_key_uid)
    {
90
      uid = strtoul (argv[0], NULL, 0);
Sergey Poznyakoff authored
91 92 93
      key = &uid;
    }
  else
94
    key = argv[0];
Sergey Poznyakoff authored
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  
  rc = mu_get_auth (&auth, key_type, key);
  printf ("mu_get_auth => %d, %s\n", rc, mu_strerror (rc));
  if (rc == 0)
    {
      printf ("user name:  %s\n", auth->name);
      printf ("password:   %s\n", auth->passwd);
      printf ("uid:        %lu\n", (unsigned long) auth->uid);
      printf ("gid:        %lu\n", (unsigned long) auth->gid);
      printf ("gecos:      %s\n", auth->gecos);
      printf ("home:       %s\n", auth->dir);
      printf ("shell:      %s\n", auth->shell);
      printf ("mailbox:    %s\n", auth->mailbox);
      printf ("change_uid: %d\n", auth->change_uid);
	
      rc = mu_authenticate (auth, password);
      printf ("mu_authenticate => %d, %s\n", rc, mu_strerror (rc));
      mu_auth_data_free (auth);
    }
  return rc != 0;
}