1 /* gdm-session-auditor.c - Object for auditing session login/logout
2 *
3 * Copyright (C) 2004, 2008 Sun Microsystems, Inc.
4 * Copyright (C) 2005, 2008 Red Hat, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 *
21 * Written by: Brian A. Cameron <Brian.Cameron@sun.com>
22 * Gary Winiger <Gary.Winiger@sun.com>
23 * Ray Strode <rstrode@redhat.com>
24 * Steve Grubb <sgrubb@redhat.com>
25 */
26 #include "config.h"
27 #include "gdm-session-auditor.h"
28
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include <glib.h>
34 #include <glib-object.h>
35 #include <glib/gi18n.h>
36
37 struct _GdmSessionAuditorPrivate
38 {
39 char *username;
40 char *hostname;
41 char *display_device;
42 };
43
44 static void gdm_session_auditor_finalize (GObject *object);
45 static void gdm_session_auditor_class_install_properties (GdmSessionAuditorClass *
46 auditor_class);
47
48 static void gdm_session_auditor_set_property (GObject *object,
49 guint prop_id,
50 const GValue *value,
51 GParamSpec *pspec);
52 static void gdm_session_auditor_get_property (GObject *object,
53 guint prop_id,
54 GValue *value,
55 GParamSpec *pspec);
56
57 enum {
58 PROP_0 = 0,
59 PROP_USERNAME,
60 PROP_HOSTNAME,
61 PROP_DISPLAY_DEVICE
62 };
63
G_DEFINE_TYPE(GdmSessionAuditor,gdm_session_auditor,G_TYPE_OBJECT)64 G_DEFINE_TYPE (GdmSessionAuditor, gdm_session_auditor, G_TYPE_OBJECT)
65
66 static void
67 gdm_session_auditor_class_init (GdmSessionAuditorClass *auditor_class)
68 {
69 GObjectClass *object_class;
70
71 object_class = G_OBJECT_CLASS (auditor_class);
72
73 object_class->finalize = gdm_session_auditor_finalize;
74
75 gdm_session_auditor_class_install_properties (auditor_class);
76
77 g_type_class_add_private (auditor_class, sizeof (GdmSessionAuditorPrivate));
78 }
79
80 static void
gdm_session_auditor_class_install_properties(GdmSessionAuditorClass * auditor_class)81 gdm_session_auditor_class_install_properties (GdmSessionAuditorClass *auditor_class)
82 {
83 GObjectClass *object_class;
84 GParamSpec *param_spec;
85
86 object_class = G_OBJECT_CLASS (auditor_class);
87 object_class->set_property = gdm_session_auditor_set_property;
88 object_class->get_property = gdm_session_auditor_get_property;
89
90 param_spec = g_param_spec_string ("username", _("Username"),
91 _("The username"),
92 NULL, G_PARAM_READWRITE);
93 g_object_class_install_property (object_class, PROP_USERNAME, param_spec);
94
95 param_spec = g_param_spec_string ("hostname", _("Hostname"),
96 _("The hostname"),
97 NULL,
98 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
99 g_object_class_install_property (object_class, PROP_HOSTNAME, param_spec);
100
101 param_spec = g_param_spec_string ("display-device", _("Display Device"),
102 _("The display device"),
103 NULL,
104 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
105 g_object_class_install_property (object_class, PROP_DISPLAY_DEVICE, param_spec);
106 }
107
108 static void
gdm_session_auditor_init(GdmSessionAuditor * auditor)109 gdm_session_auditor_init (GdmSessionAuditor *auditor)
110 {
111 auditor->priv = G_TYPE_INSTANCE_GET_PRIVATE (auditor,
112 GDM_TYPE_SESSION_AUDITOR,
113 GdmSessionAuditorPrivate);
114
115 }
116
117 static void
gdm_session_auditor_finalize(GObject * object)118 gdm_session_auditor_finalize (GObject *object)
119 {
120 GdmSessionAuditor *auditor;
121 GObjectClass *parent_class;
122
123 auditor = GDM_SESSION_AUDITOR (object);
124
125 g_free (auditor->priv->username);
126 g_free (auditor->priv->hostname);
127 g_free (auditor->priv->display_device);
128
129 parent_class = G_OBJECT_CLASS (gdm_session_auditor_parent_class);
130
131 if (parent_class->finalize != NULL) {
132 parent_class->finalize (object);
133 }
134 }
135
136 void
gdm_session_auditor_set_username(GdmSessionAuditor * auditor,const char * username)137 gdm_session_auditor_set_username (GdmSessionAuditor *auditor,
138 const char *username)
139 {
140 g_return_if_fail (GDM_IS_SESSION_AUDITOR (auditor));
141
142 if (username == auditor->priv->username) {
143 return;
144 }
145
146 if ((username == NULL || auditor->priv->username == NULL) ||
147 strcmp (username, auditor->priv->username) != 0) {
148 auditor->priv->username = g_strdup (username);
149 g_object_notify (G_OBJECT (auditor), "username");
150 }
151 }
152
153 static void
gdm_session_auditor_set_hostname(GdmSessionAuditor * auditor,const char * hostname)154 gdm_session_auditor_set_hostname (GdmSessionAuditor *auditor,
155 const char *hostname)
156 {
157 g_return_if_fail (GDM_IS_SESSION_AUDITOR (auditor));
158 auditor->priv->hostname = g_strdup (hostname);
159 }
160
161 static void
gdm_session_auditor_set_display_device(GdmSessionAuditor * auditor,const char * display_device)162 gdm_session_auditor_set_display_device (GdmSessionAuditor *auditor,
163 const char *display_device)
164 {
165 g_return_if_fail (GDM_IS_SESSION_AUDITOR (auditor));
166 auditor->priv->display_device = g_strdup (display_device);
167 }
168
169 static char *
gdm_session_auditor_get_username(GdmSessionAuditor * auditor)170 gdm_session_auditor_get_username (GdmSessionAuditor *auditor)
171 {
172 return g_strdup (auditor->priv->username);
173 }
174
175 static char *
gdm_session_auditor_get_hostname(GdmSessionAuditor * auditor)176 gdm_session_auditor_get_hostname (GdmSessionAuditor *auditor)
177 {
178 return g_strdup (auditor->priv->hostname);
179 }
180
181 static char *
gdm_session_auditor_get_display_device(GdmSessionAuditor * auditor)182 gdm_session_auditor_get_display_device (GdmSessionAuditor *auditor)
183 {
184 return g_strdup (auditor->priv->display_device);
185 }
186
187 static void
gdm_session_auditor_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)188 gdm_session_auditor_set_property (GObject *object,
189 guint prop_id,
190 const GValue *value,
191 GParamSpec *pspec)
192 {
193 GdmSessionAuditor *auditor;
194
195 auditor = GDM_SESSION_AUDITOR (object);
196
197 switch (prop_id) {
198 case PROP_USERNAME:
199 gdm_session_auditor_set_username (auditor, g_value_get_string (value));
200 break;
201
202 case PROP_HOSTNAME:
203 gdm_session_auditor_set_hostname (auditor, g_value_get_string (value));
204 break;
205
206 case PROP_DISPLAY_DEVICE:
207 gdm_session_auditor_set_display_device (auditor, g_value_get_string (value));
208 break;
209
210 default:
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 }
213 }
214
215 static void
gdm_session_auditor_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)216 gdm_session_auditor_get_property (GObject *object,
217 guint prop_id,
218 GValue *value,
219 GParamSpec *pspec)
220 {
221 GdmSessionAuditor *auditor;
222
223 auditor = GDM_SESSION_AUDITOR (object);
224
225 switch (prop_id) {
226 case PROP_USERNAME:
227 g_value_take_string (value, gdm_session_auditor_get_username (auditor));
228 break;
229
230 case PROP_HOSTNAME:
231 g_value_take_string (value, gdm_session_auditor_get_hostname (auditor));
232 break;
233
234 case PROP_DISPLAY_DEVICE:
235 g_value_take_string (value, gdm_session_auditor_get_display_device (auditor));
236 break;
237
238 default:
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 }
241 }
242
243 GdmSessionAuditor *
gdm_session_auditor_new(const char * hostname,const char * display_device)244 gdm_session_auditor_new (const char *hostname,
245 const char *display_device)
246 {
247 GdmSessionAuditor *auditor;
248
249 auditor = g_object_new (GDM_TYPE_SESSION_AUDITOR,
250 "hostname", hostname,
251 "display-device", display_device,
252 NULL);
253
254 return auditor;
255 }
256
257 void
gdm_session_auditor_report_password_changed(GdmSessionAuditor * auditor)258 gdm_session_auditor_report_password_changed (GdmSessionAuditor *auditor)
259 {
260 if (GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_password_changed != NULL) {
261 GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_password_changed (auditor);
262 }
263 }
264
265 void
gdm_session_auditor_report_password_change_failure(GdmSessionAuditor * auditor)266 gdm_session_auditor_report_password_change_failure (GdmSessionAuditor *auditor)
267 {
268 if (GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_password_change_failure != NULL) {
269 GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_password_change_failure (auditor);
270 }
271 }
272
273 void
gdm_session_auditor_report_user_accredited(GdmSessionAuditor * auditor)274 gdm_session_auditor_report_user_accredited (GdmSessionAuditor *auditor)
275 {
276 if (GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_user_accredited != NULL) {
277 GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_user_accredited (auditor);
278 }
279 }
280
281 void
gdm_session_auditor_report_login(GdmSessionAuditor * auditor)282 gdm_session_auditor_report_login (GdmSessionAuditor *auditor)
283 {
284 if (GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_login != NULL) {
285 GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_login (auditor);
286 }
287 }
288
289 void
gdm_session_auditor_report_login_failure(GdmSessionAuditor * auditor,int error_code,const char * error_message)290 gdm_session_auditor_report_login_failure (GdmSessionAuditor *auditor,
291 int error_code,
292 const char *error_message)
293 {
294 if (GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_login_failure != NULL) {
295 GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_login_failure (auditor, error_code, error_message);
296 }
297 }
298
299 void
gdm_session_auditor_report_logout(GdmSessionAuditor * auditor)300 gdm_session_auditor_report_logout (GdmSessionAuditor *auditor)
301 {
302 if (GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_logout != NULL) {
303 GDM_SESSION_AUDITOR_GET_CLASS (auditor)->report_logout (auditor);
304 }
305 }
306