summaryrefslogtreecommitdiff
path: root/plugin/auth_pam/testing/pam_mariadb_mtr.c
blob: 108aeb941ac88157ba927f35a00d743c579b4228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
  This code is in the public domain and has no copyright.

  Pam module to test pam authentication plugin. Used in pam tests.
  Linux only.
  
  Install as appropriate (for example, in /lib/security/).
  see also mariadb_mtr.conf
*/

#include <stdlib.h>
#include <string.h>
#include <security/pam_modules.h>
#include <security/pam_appl.h>

#define N 3

int pam_sm_authenticate(pam_handle_t *pamh, int flags __attribute__((unused)),
                        int argc, const char *argv[])
{
  struct pam_conv *conv;
  struct pam_response *resp = 0;
  int pam_err, retval = PAM_SYSTEM_ERR;
  struct pam_message msg[N] = {
    { PAM_TEXT_INFO, (char*)"Challenge input first." },
    { PAM_PROMPT_ECHO_OFF, (char*)"Enter:" },
    { PAM_ERROR_MSG, (char*)"Now, the magic number!" }
  };
  const struct pam_message *msgp[N] = { msg, msg+1, msg+2 };
  char *r1 = 0, *r2 = 0;

  pam_err = pam_get_item(pamh, PAM_CONV, (const void **)&conv);
  if (pam_err != PAM_SUCCESS)
    goto ret;

  pam_err = (*conv->conv)(N, msgp, &resp, conv->appdata_ptr);

  if (pam_err != PAM_SUCCESS || !resp || !((r1= resp[1].resp)))
    goto ret;

  if (strcmp(r1, "cleartext good") == 0)
    retval = PAM_SUCCESS;
  else if (strcmp(r1, "cleartext bad") == 0)
    retval = PAM_AUTH_ERR;
  else
  {
    free(resp);
    resp= NULL;
    msg[0].msg_style = PAM_PROMPT_ECHO_ON;
    msg[0].msg = (char*)"PIN:";
    pam_err = (*conv->conv)(1, msgp, &resp, conv->appdata_ptr);

    if (pam_err != PAM_SUCCESS || !resp || !((r2= resp[0].resp)))
      goto ret;

    /* Produce the crash for testing purposes. */
    if (strcmp(r1, "crash pam module") == 0 && atoi(r2) == 616)
      abort();

    if (strlen(r1) == (size_t)atoi(r2) % 100)
      retval = PAM_SUCCESS;
    else
      retval = PAM_AUTH_ERR;
  }

  if (argc > 0 && argv[0])
    pam_set_item(pamh, PAM_USER, argv[0]);

ret:
  free(resp);
  free(r1);
  free(r2);
  return retval;
}

int pam_sm_setcred(pam_handle_t *pamh __attribute__((unused)),
                   int flags __attribute__((unused)),
                   int argc __attribute__((unused)),
                   const char *argv[] __attribute__((unused)))
{

    return PAM_SUCCESS;
}