summaryrefslogtreecommitdiff
path: root/src/test-passwd.c
blob: 9359fb1a7608161f091879f9421949a1212d3280 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2005-2006 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "gs-auth.h"
#include "setuid.h"

/* Initializations that potentially take place as a priveleged user:
   If the executable is setuid root, then these initializations
   are run as root, before discarding privileges.
*/
static gboolean
privileged_initialization (void)
{
	gboolean ret;
	char    *nolock_reason;
	char    *orig_uid;
	char    *uid_message;

#ifndef NO_LOCKING
	/* before hack_uid () for proper permissions */
	gs_auth_priv_init ();
#endif /* NO_LOCKING */

	ret = hack_uid (&nolock_reason,
	                &orig_uid,
	                &uid_message);
	if (nolock_reason)
	{
		g_warning ("Locking disabled: %s", nolock_reason);
	}
	if (uid_message && gs_auth_get_verbose ())
	{
		g_print ("Modified UID: %s", uid_message);
	}

	g_free (nolock_reason);
	g_free (orig_uid);
	g_free (uid_message);

	return ret;
}


/* Figure out what locking mechanisms are supported.
 */
static gboolean
lock_initialization (char **nolock_reason)
{
	if (nolock_reason)
	{
		*nolock_reason = NULL;
	}

#ifdef NO_LOCKING
	if (nolock_reason)
	{
		*nolock_reason = g_strdup ("not compiled with locking support");
	}
	return FALSE;
#else /* !NO_LOCKING */

	/* Finish initializing locking, now that we're out of privileged code. */
	if (! gs_auth_init ())
	{
		if (nolock_reason)
		{
			*nolock_reason = g_strdup ("error getting password");
		}
		return FALSE;
	}

	/* If locking is currently enabled, but the environment indicates that
	   we have been launched as MDM's "Background" program, then disable
	   locking just in case.
	*/
	if (getenv ("RUNNING_UNDER_MDM"))
	{
		if (nolock_reason)
		{
			*nolock_reason = g_strdup ("running under MDM");
		}
		return FALSE;
	}

	/* If the server is XDarwin (MacOS X) then disable locking.
	   (X grabs only affect X programs, so you can use Command-Tab
	   to bring any other Mac program to the front, e.g., Terminal.)
	*/
	{
		gboolean macos = FALSE;

#ifdef __APPLE__
		/* Disable locking if *running* on Apple hardware, since we have no
		   reliable way to determine whether the server is running on MacOS.
		   Hopefully __APPLE__ means "MacOS" and not "Linux on Mac hardware"
		   but I'm not really sure about that.
		*/
		macos = TRUE;
#endif

		if (macos)
		{
			if (nolock_reason)
			{
				*nolock_reason = g_strdup ("Cannot lock securely on MacOS X");
			}
			return FALSE;
		}
	}

#endif /* NO_LOCKING */

	return TRUE;
}

static char *
request_password (const char *prompt)
{
	char           buf [255];
	char          *pass;
	char          *password;
	struct termios ts0;
	struct termios ts1;

	tcgetattr (fileno (stdin), &ts0);
	ts1 = ts0;
	ts1.c_lflag &= ~ECHO;

	printf ("%s", prompt);

	if (tcsetattr (fileno (stdin), TCSAFLUSH, &ts1) != 0)
	{
		fprintf (stderr, "Could not set terminal attributes\n");
		exit (1);
	}

	pass = fgets (buf, sizeof (buf) - 1, stdin);

	tcsetattr (fileno (stdin), TCSANOW, &ts0);

	if (!pass || !*pass)
	{
		exit (0);
	}

	if (pass [strlen (pass) - 1] == '\n')
	{
		pass [strlen (pass) - 1] = 0;
	}

	password = g_strdup (pass);

	memset (pass, '\b', strlen (pass));

	return password;
}

static gboolean
auth_message_handler (GSAuthMessageStyle style,
                      const char        *msg,
                      char             **response,
                      gpointer           data)
{
	gboolean ret;

	g_message ("Got message style %d: '%s'", style, msg);

	ret = TRUE;

	switch (style)
	{
	case GS_AUTH_MESSAGE_PROMPT_ECHO_ON:
		break;
	case GS_AUTH_MESSAGE_PROMPT_ECHO_OFF:
	{
		char *password;
		password = request_password (msg);
		*response = password;
	}
	break;
	case GS_AUTH_MESSAGE_ERROR_MSG:
		break;
	case GS_AUTH_MESSAGE_TEXT_INFO:
		break;
	default:
		g_assert_not_reached ();
	}

	return ret;
}

int
main (int    argc,
      char **argv)
{
	GError         *error         = NULL;
	gboolean        verbose       = TRUE;
	char           *nolock_reason = NULL;

#ifdef ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
# ifdef HAVE_BIND_TEXTDOMAIN_CODESET
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
# endif
	textdomain (GETTEXT_PACKAGE);
#endif

	gs_auth_set_verbose (verbose);
	if (! privileged_initialization ())
	{
		exit (1);
	}

	if (! gtk_init_with_args (&argc, &argv, NULL, NULL, NULL, &error))
	{
		fprintf (stderr, "%s", error->message);
		g_error_free (error);
		exit (1);
	}

	if (! lock_initialization (&nolock_reason))
	{
		if (nolock_reason)
		{
			g_warning ("Screen locking disabled: %s", nolock_reason);
			g_free (nolock_reason);
		}

		exit (1);
	}

again:
	error = NULL;

	if (gs_auth_verify_user (g_get_user_name (), g_getenv ("DISPLAY"), auth_message_handler, NULL, &error))
	{
		printf ("Correct!\n");
	}
	else
	{
		if (error != NULL)
		{
			fprintf (stderr, "ERROR: %s\n", error->message);
			g_error_free (error);
		}
		printf ("Incorrect\n");
		goto again;
	}

	return 0;
}