File: test.c

package info (click to toggle)
pam-p11 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: ansic: 1,276; makefile: 48; sh: 8
file content (108 lines) | stat: -rw-r--r-- 2,502 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2018 Frank Morgner <frankmorgner@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <security/pam_appl.h>
#include <security/pam_modules.h>
#ifdef HAVE_SECURITY_PAM_MISC_H
#include <security/pam_misc.h>
#endif

#ifndef LIBDIR
#define LIBDIR "/usr/lib"
#endif

extern int pam_sm_test(pam_handle_t *pamh, int flags, int argc, const char **argv);

int main(int argc, const char **argv)
{
	char user[32];
	char module[4096];
	const char *pam_argv[] = {
		module,
	};
	pam_handle_t *pamh = NULL;
	struct pam_conv conv = {
#ifdef HAVE_OPENPAM_TTYCONV
		openpam_ttyconv,
#else
		misc_conv,
#endif
		NULL,
	};
	int r;
	int status = EXIT_FAILURE;

	/* initialize default values */
	strcpy(module, LIBDIR "/opensc-pkcs11.so");
	if (argc < 3) {
		if (0 != getlogin_r(user, sizeof user)) {
			perror("getlogin_r");
			goto err;
		}
	}

	switch (argc) {
		case 3:
			strncpy(user, argv[2], (sizeof user) - 1);
			/* fall through */
		case 2:
			strncpy(module, argv[1], (sizeof module) - 1);
			/* fall through */
		case 1:
			break;

		default:
			printf("Usage: %s [pkcs11_module.so [username]]\n", argv[0]);
			/* fall through */
		case 0:
			goto err;
	}
	module[(sizeof module) - 1] = '\0';
	user[(sizeof user) - 1] = '\0';
	printf("Using '%s' for '%s'\n", module, user);

	r = pam_start("test", user, &conv, &pamh);
	if (PAM_SUCCESS != r)
		goto pam_err;

	r = pam_sm_test(pamh, 0, sizeof pam_argv/sizeof *pam_argv, pam_argv);
	if (PAM_SUCCESS != r)
		goto pam_err;

	status = EXIT_SUCCESS;

pam_err:
	if (PAM_SUCCESS != r) {
		printf("Error: %s\n", pam_strerror(pamh, r));
	} else {
		printf("OK\n");
	}
	pam_end(pamh, r);

err:
	exit(status);
}