File: cc_test.c

package info (click to toggle)
libpam-ccreds 10-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 644 kB
  • sloc: sh: 2,981; ansic: 2,066; makefile: 60
file content (89 lines) | stat: -rw-r--r-- 2,137 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2004 PADL Software Pty Ltd.
 * All rights reserved.
 * Use is subject to license.
 */

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

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>

#include "cc.h"

static int usage(void)
{
	fprintf(stderr, "Usage: cc_test [-validate|-store|-update] [service] [user] [password] [ccredsfile]\n");
	fprintf(stderr, "       where service may be \"any\"\n");
	fprintf(stderr, "       where password may be \"-\" to delete a user\n");

	return PAM_SYSTEM_ERR;
}

int main(int argc, char *argv[])
{
	pam_cc_handle_t *pamcch;
	int rc;
	char *user;
	char *service;
	char *password;
	char *ccredsfile;
	char *action;
	const char *function = NULL;
	unsigned int cc_flags;

	if (argc < 5 || argc > 6) {
		exit(usage());
	}

	action = argv[1];
	service = (strcasecmp(argv[2], "any") == 0) ? NULL : argv[2];
	user = argv[3];
	password = (strcasecmp(argv[4], "-") == 0) ? NULL : argv[4];
	ccredsfile = (argc > 5) ? argv[5] : NULL;

	if (strcmp(action, "-validate") == 0)
		cc_flags = CC_FLAGS_READ_ONLY;
	else
		cc_flags = 0;

	rc = pam_cc_start(service, user, ccredsfile, cc_flags, &pamcch);
	if (rc != PAM_SUCCESS) {
		fprintf(stderr, "pam_cc_start failed: %s\n", pam_strerror(NULL, rc));
		exit(rc);
	}

	if (strcmp(action, "-validate") == 0 && password) {
		rc = pam_cc_validate_credentials(pamcch, PAM_CC_TYPE_DEFAULT,
						 password, strlen(password));
		function = "pam_cc_validate_credentials";
	} else if (strcmp(action, "-store") == 0 && password) {
		rc = pam_cc_store_credentials(pamcch, PAM_CC_TYPE_DEFAULT,
					      password, strlen(password));
		function = "pam_cc_store_credentials";
	} else if (strcmp(action, "-update") == 0) {
		rc = pam_cc_delete_credentials(pamcch, PAM_CC_TYPE_DEFAULT,
					       password,
					       (password == NULL) ? 0 : strlen(password));
		function = "pam_cc_delete_credentials";
	} else {
		rc = usage();
	}

	if (function != NULL) {
		fprintf(stderr, "%s: %s\n", function, pam_strerror(NULL, rc));
	}

	pam_cc_end(&pamcch);

	exit(rc);
	return rc;
}