summaryrefslogtreecommitdiff
path: root/utils/gdmaskpass.c
blob: 206b9eda18d45b31681d39117881ea5c3ae2a413 (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
/* Simple pam thingie to ask the root password, for use in XKeepsCrashing
 * script. */
#include "config.h"
#include <glib/gi18n.h>
#include <stdio.h>
#include <locale.h>
#include <unistd.h>
#include <security/pam_appl.h>
#include <security/pam_misc.h>

static struct pam_conv conv = {
	misc_conv,
	NULL
};

int
main (int argc, char *argv[])
{
	pam_handle_t *pamh;
	const char *username = "root";
	int retval;
	int tries = 3;

	if (getuid () != geteuid () ||
	    getuid () != 0) {
		fprintf (stderr, _("gdmaskpass only runs as root\n"));
		return 1;
	}

	if (argc >= 2) {
		username = argv[1];
	}

	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	textdomain (GETTEXT_PACKAGE);

	for (tries = 3; tries > 0; tries --) {
		if ((retval = pam_start ("gdm", username, &conv, &pamh)) != PAM_SUCCESS) {
			pam_end (pamh, retval);
			pamh = NULL;
			printf (_("Authentication failure!\n"));
			continue;
		}
		if ((retval = pam_authenticate (pamh, 0)) != PAM_SUCCESS) {
			pam_end (pamh, retval);
			pamh = NULL;
			printf (_("Authentication failure!\n"));
			continue;
		}
		break;
	}

	if (pamh != NULL)
		pam_end (pamh, retval);

	return (retval == PAM_SUCCESS) ? 0 : 1;
}