1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * This program is free software; you can redistribute it and/or |
---|
4 | * modify it under the terms of the GNU General Public License as |
---|
5 | * published by the Free Software Foundation; either version 2 of the |
---|
6 | * License, or (at your option) any later version. |
---|
7 | * |
---|
8 | * This program is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program; if not, write to the Free Software |
---|
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
16 | * 02111-1307, USA. |
---|
17 | * |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | #include <unistd.h> |
---|
26 | #include <sys/ioctl.h> |
---|
27 | #include <termios.h> |
---|
28 | |
---|
29 | #include <glib.h> |
---|
30 | |
---|
31 | #include "gdm-session-direct.h" |
---|
32 | |
---|
33 | static GMainLoop *loop; |
---|
34 | |
---|
35 | static void |
---|
36 | on_open (GdmSession *session, |
---|
37 | const char *username) |
---|
38 | { |
---|
39 | g_debug ("Got opened: calling setup..."); |
---|
40 | |
---|
41 | gdm_session_setup (session, "gdm"); |
---|
42 | } |
---|
43 | |
---|
44 | static void |
---|
45 | on_session_setup_complete (GdmSession *session, |
---|
46 | gpointer data) |
---|
47 | { |
---|
48 | g_debug ("Session setup complete"); |
---|
49 | gdm_session_authenticate (session); |
---|
50 | } |
---|
51 | |
---|
52 | static void |
---|
53 | on_session_setup_failed (GdmSession *session, |
---|
54 | const char *message, |
---|
55 | gpointer data) |
---|
56 | { |
---|
57 | g_print ("Unable to initialize PAM: %s\n", message); |
---|
58 | |
---|
59 | exit (1); |
---|
60 | } |
---|
61 | |
---|
62 | static void |
---|
63 | on_session_reset_complete (GdmSession *session, |
---|
64 | gpointer data) |
---|
65 | { |
---|
66 | g_debug ("Session reset complete"); |
---|
67 | } |
---|
68 | |
---|
69 | static void |
---|
70 | on_session_reset_failed (GdmSession *session, |
---|
71 | const char *message, |
---|
72 | gpointer data) |
---|
73 | { |
---|
74 | g_print ("Unable to reset PAM: %s\n", message); |
---|
75 | |
---|
76 | exit (1); |
---|
77 | } |
---|
78 | |
---|
79 | static void |
---|
80 | on_session_authenticated (GdmSession *session, |
---|
81 | gpointer data) |
---|
82 | { |
---|
83 | g_debug ("Session authenticated"); |
---|
84 | gdm_session_authorize (session); |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | on_session_authentication_failed (GdmSession *session, |
---|
89 | const char *message, |
---|
90 | gpointer data) |
---|
91 | { |
---|
92 | g_print ("Unable to authenticate user: %s\n", message); |
---|
93 | |
---|
94 | exit (1); |
---|
95 | } |
---|
96 | |
---|
97 | static void |
---|
98 | on_session_authorized (GdmSession *session, |
---|
99 | gpointer data) |
---|
100 | { |
---|
101 | g_debug ("Session authorized"); |
---|
102 | gdm_session_accredit (session, GDM_SESSION_CRED_ESTABLISH); |
---|
103 | } |
---|
104 | |
---|
105 | static void |
---|
106 | on_session_authorization_failed (GdmSession *session, |
---|
107 | const char *message, |
---|
108 | gpointer data) |
---|
109 | { |
---|
110 | g_print ("Unable to authorize user: %s\n", message); |
---|
111 | |
---|
112 | exit (1); |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | on_session_accredited (GdmSession *session, |
---|
117 | gpointer data) |
---|
118 | { |
---|
119 | char *username; |
---|
120 | |
---|
121 | username = gdm_session_direct_get_username (GDM_SESSION_DIRECT (session)); |
---|
122 | |
---|
123 | g_print ("%s%ssuccessfully accredited\n", |
---|
124 | username ? username : "", username ? " " : ""); |
---|
125 | g_free (username); |
---|
126 | |
---|
127 | gdm_session_start_session (session); |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | static void |
---|
132 | on_session_accreditation_failed (GdmSession *session, |
---|
133 | const char *message, |
---|
134 | gpointer data) |
---|
135 | { |
---|
136 | g_print ("Unable to accredit user: %s\n", message); |
---|
137 | |
---|
138 | exit (1); |
---|
139 | } |
---|
140 | |
---|
141 | static void |
---|
142 | on_session_started (GdmSession *session) |
---|
143 | { |
---|
144 | g_print ("session started\n"); |
---|
145 | } |
---|
146 | |
---|
147 | static void |
---|
148 | on_session_exited (GdmSession *session, |
---|
149 | int exit_code) |
---|
150 | { |
---|
151 | g_print ("session exited with code %d\n", exit_code); |
---|
152 | exit (0); |
---|
153 | } |
---|
154 | |
---|
155 | static void |
---|
156 | on_session_died (GdmSession *session, |
---|
157 | int signal_number) |
---|
158 | { |
---|
159 | g_print ("session died with signal %d, (%s)\n", |
---|
160 | signal_number, |
---|
161 | g_strsignal (signal_number)); |
---|
162 | exit (1); |
---|
163 | } |
---|
164 | |
---|
165 | static void |
---|
166 | on_info_query (GdmSession *session, |
---|
167 | const char *query_text) |
---|
168 | { |
---|
169 | char answer[1024]; |
---|
170 | |
---|
171 | g_print ("%s ", query_text); |
---|
172 | |
---|
173 | fgets (answer, sizeof (answer), stdin); |
---|
174 | answer[strlen(answer) - 1] = '\0'; |
---|
175 | |
---|
176 | if (answer[0] == '\0') { |
---|
177 | gdm_session_close (session); |
---|
178 | g_main_loop_quit (loop); |
---|
179 | } else { |
---|
180 | gdm_session_answer_query (session, answer); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | static void |
---|
185 | on_info (GdmSession *session, |
---|
186 | const char *info) |
---|
187 | { |
---|
188 | g_print ("\n** NOTE: %s\n", info); |
---|
189 | } |
---|
190 | |
---|
191 | static void |
---|
192 | on_problem (GdmSession *session, |
---|
193 | const char *problem) |
---|
194 | { |
---|
195 | g_print ("\n** WARNING: %s\n", problem); |
---|
196 | } |
---|
197 | |
---|
198 | static void |
---|
199 | on_secret_info_query (GdmSession *session, |
---|
200 | const char *query_text) |
---|
201 | { |
---|
202 | char answer[1024]; |
---|
203 | struct termios ts0; |
---|
204 | struct termios ts1; |
---|
205 | |
---|
206 | tcgetattr (fileno (stdin), &ts0); |
---|
207 | ts1 = ts0; |
---|
208 | ts1.c_lflag &= ~ECHO; |
---|
209 | |
---|
210 | g_print ("%s", query_text); |
---|
211 | |
---|
212 | if (tcsetattr (fileno (stdin), TCSAFLUSH, &ts1) != 0) { |
---|
213 | fprintf (stderr, "Could not set terminal attributes\n"); |
---|
214 | exit (1); |
---|
215 | } |
---|
216 | |
---|
217 | fgets (answer, sizeof (answer), stdin); |
---|
218 | answer[strlen (answer) - 1] = '\0'; |
---|
219 | |
---|
220 | tcsetattr (fileno (stdin), TCSANOW, &ts0); |
---|
221 | |
---|
222 | g_print ("\n"); |
---|
223 | |
---|
224 | gdm_session_answer_query (session, answer); |
---|
225 | } |
---|
226 | |
---|
227 | static void |
---|
228 | import_environment (GdmSessionDirect *session) |
---|
229 | { |
---|
230 | } |
---|
231 | |
---|
232 | int |
---|
233 | main (int argc, |
---|
234 | char *argv[]) |
---|
235 | { |
---|
236 | GdmSessionDirect *session; |
---|
237 | char *username; |
---|
238 | |
---|
239 | g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING); |
---|
240 | |
---|
241 | g_type_init (); |
---|
242 | |
---|
243 | do { |
---|
244 | g_debug ("creating instance of GdmSessionDirect object..."); |
---|
245 | session = gdm_session_direct_new ("/org/gnome/DisplayManager/Display1", |
---|
246 | ":0", |
---|
247 | g_get_host_name (), |
---|
248 | ttyname (STDIN_FILENO), |
---|
249 | getenv("XAUTHORITY"), |
---|
250 | TRUE); |
---|
251 | g_debug ("GdmSessionDirect object created successfully"); |
---|
252 | |
---|
253 | if (argc <= 1) { |
---|
254 | username = NULL; |
---|
255 | } else { |
---|
256 | username = argv[1]; |
---|
257 | } |
---|
258 | |
---|
259 | gdm_session_open (GDM_SESSION (session)); |
---|
260 | |
---|
261 | g_signal_connect (session, |
---|
262 | "opened", |
---|
263 | G_CALLBACK (on_open), |
---|
264 | username); |
---|
265 | g_signal_connect (session, |
---|
266 | "setup-complete", |
---|
267 | G_CALLBACK (on_session_setup_complete), |
---|
268 | NULL); |
---|
269 | g_signal_connect (session, |
---|
270 | "setup-failed", |
---|
271 | G_CALLBACK (on_session_setup_failed), |
---|
272 | NULL); |
---|
273 | g_signal_connect (session, |
---|
274 | "reset-complete", |
---|
275 | G_CALLBACK (on_session_reset_complete), |
---|
276 | NULL); |
---|
277 | g_signal_connect (session, |
---|
278 | "reset-failed", |
---|
279 | G_CALLBACK (on_session_reset_failed), |
---|
280 | NULL); |
---|
281 | g_signal_connect (session, |
---|
282 | "authenticated", |
---|
283 | G_CALLBACK (on_session_authenticated), |
---|
284 | NULL); |
---|
285 | g_signal_connect (session, |
---|
286 | "authentication-failed", |
---|
287 | G_CALLBACK (on_session_authentication_failed), |
---|
288 | NULL); |
---|
289 | g_signal_connect (session, |
---|
290 | "authorized", |
---|
291 | G_CALLBACK (on_session_authorized), |
---|
292 | NULL); |
---|
293 | g_signal_connect (session, |
---|
294 | "authorization-failed", |
---|
295 | G_CALLBACK (on_session_authorization_failed), |
---|
296 | NULL); |
---|
297 | g_signal_connect (session, |
---|
298 | "accredited", |
---|
299 | G_CALLBACK (on_session_accredited), |
---|
300 | NULL); |
---|
301 | g_signal_connect (session, |
---|
302 | "accreditation-failed", |
---|
303 | G_CALLBACK (on_session_accreditation_failed), |
---|
304 | NULL); |
---|
305 | |
---|
306 | g_signal_connect (session, |
---|
307 | "info", |
---|
308 | G_CALLBACK (on_info), |
---|
309 | NULL); |
---|
310 | g_signal_connect (session, |
---|
311 | "problem", |
---|
312 | G_CALLBACK (on_problem), |
---|
313 | NULL); |
---|
314 | g_signal_connect (session, |
---|
315 | "info-query", |
---|
316 | G_CALLBACK (on_info_query), |
---|
317 | NULL); |
---|
318 | g_signal_connect (session, |
---|
319 | "secret-info-query", |
---|
320 | G_CALLBACK (on_secret_info_query), |
---|
321 | NULL); |
---|
322 | |
---|
323 | g_signal_connect (session, |
---|
324 | "session-started", |
---|
325 | G_CALLBACK (on_session_started), |
---|
326 | NULL); |
---|
327 | g_signal_connect (session, |
---|
328 | "session-exited", |
---|
329 | G_CALLBACK (on_session_exited), |
---|
330 | NULL); |
---|
331 | g_signal_connect (session, |
---|
332 | "session-died", |
---|
333 | G_CALLBACK (on_session_died), |
---|
334 | NULL); |
---|
335 | |
---|
336 | import_environment (session); |
---|
337 | |
---|
338 | loop = g_main_loop_new (NULL, FALSE); |
---|
339 | g_main_loop_run (loop); |
---|
340 | g_main_loop_unref (loop); |
---|
341 | |
---|
342 | g_message ("destroying previously created GdmSessionDirect object..."); |
---|
343 | g_object_unref (session); |
---|
344 | g_message ("GdmSessionDirect object destroyed successfully"); |
---|
345 | } while (1); |
---|
346 | } |
---|