First tlm release
[platform/core/system/tlm.git] / src / plugins / default / tlm-account-plugin-default.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of tlm (Tizen Login Manager)
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include <pwd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <glib.h>
31
32 #include "tlm-account-plugin-default.h"
33 #include "tlm-log.h"
34
35 /**
36  * SECTION:tlm-account-plugin-default
37  * @short_description: an default plugin for user account operation
38  *
39  * #TlmAccountPluginDefault provides a default implementation of user account
40  * operations:
41  * - setting up guest account is performed by running 'useradd'
42  * - cleaning up guest account is performed by running 'rm -rf' on the account's
43  * home directory
44  * - check the account validity is done using getpwnam().
45  *
46  * It is recommended to use a GUM plugin instead: see #TlmAccountPluginGumd.
47  *
48  */
49
50 /**
51  * TlmAccountPluginDefault:
52  *
53  * Opaque data structure
54  */
55 /**
56  * TlmAccountPluginDefaultClass:
57  * @parent_class: a reference to a parent class
58  *
59  * The class structure for the #TlmAccountPluginDefault objects,
60  */
61
62 enum {
63     PROP_0,
64     PROP_CONFIG
65 };
66
67 struct _TlmAccountPluginDefault
68 {
69     GObject parent;
70     GHashTable *config;
71 };
72
73
74 static gboolean
75 _setup_guest_account (TlmAccountPlugin *plugin, const gchar *user_name)
76 {
77     gchar *command = NULL;
78     int res;
79     g_return_val_if_fail (plugin, FALSE);
80     g_return_val_if_fail (TLM_IS_ACCOUNT_PLUGIN_DEFAULT(plugin), FALSE);
81     g_return_val_if_fail (user_name && user_name[0], FALSE);
82
83     command = g_strdup_printf ("useradd %s", user_name);
84     res = system (command);
85
86     g_free (command);
87
88     return res != -1;
89 }
90
91 static gboolean
92 _cleanup_guest_user (TlmAccountPlugin *plugin,
93                      const gchar *user_name,
94                      gboolean delete)
95 {
96     struct passwd *pwd_entry = NULL;
97     char *command = NULL;
98     int res;
99
100     (void) delete;
101
102     g_return_val_if_fail (plugin, FALSE);
103     g_return_val_if_fail (TLM_IS_ACCOUNT_PLUGIN_DEFAULT(plugin), FALSE);
104     g_return_val_if_fail (user_name && user_name[0], FALSE);
105
106     /* clear error */
107     errno = 0;
108
109     pwd_entry = getpwnam (user_name);
110
111     if (!pwd_entry) {
112         DBG("Could not get info for user '%s', error : %s", 
113             user_name, strerror(errno));
114         return FALSE;
115     }
116
117     if (!pwd_entry->pw_dir) {
118         DBG("No home folder entry found for user '%s'", user_name);
119         return FALSE;
120     }
121
122     command = g_strdup_printf ("rm -rf %s/*", pwd_entry->pw_dir);
123
124     res = system (command);
125
126     g_free (command);
127
128     return res != -1;
129 }
130
131 static gboolean
132 _is_valid_user (TlmAccountPlugin *plugin, const gchar *user_name)
133 {
134     struct passwd *pwd_entry = NULL;
135
136     g_return_val_if_fail (plugin, FALSE);
137     g_return_val_if_fail (TLM_IS_ACCOUNT_PLUGIN_DEFAULT(plugin), FALSE);
138     g_return_val_if_fail (user_name && user_name[0], FALSE);
139
140     /* clear error */
141     errno = 0;
142
143     pwd_entry = getpwnam (user_name);
144
145     if (!pwd_entry) {
146         DBG("Could not get info for user '%s', error : %s",
147             user_name, strerror(errno));
148         return FALSE;
149     }
150
151     return TRUE;
152 }
153
154 static void
155 _plugin_interface_init (TlmAccountPluginInterface *iface)
156 {
157     iface->setup_guest_user_account = _setup_guest_account;
158     iface->cleanup_guest_user = _cleanup_guest_user;
159     iface->is_valid_user = _is_valid_user;
160 }
161
162 G_DEFINE_TYPE_WITH_CODE (TlmAccountPluginDefault, tlm_account_plugin_default,
163                          G_TYPE_OBJECT,
164                          G_IMPLEMENT_INTERFACE (TLM_TYPE_ACCOUNT_PLUGIN,
165                                                 _plugin_interface_init));
166
167
168 static void
169 _plugin_finalize (GObject *self)
170 {
171     TlmAccountPluginDefault *plugin = TLM_ACCOUNT_PLUGIN_DEFAULT(self);
172
173     if (plugin->config) g_hash_table_unref (plugin->config);
174
175     G_OBJECT_CLASS (tlm_account_plugin_default_parent_class)->finalize(self);
176 }
177
178 static void
179 _plugin_set_property (GObject      *object,
180                       guint         prop_id,
181                       const GValue *value,
182                       GParamSpec   *pspec)
183 {
184     TlmAccountPluginDefault *self = TLM_ACCOUNT_PLUGIN_DEFAULT (object);
185
186     switch (prop_id) {
187         case PROP_CONFIG: {
188             gpointer p = g_value_get_boxed (value);
189             if (p)
190                 self->config = g_hash_table_ref ((GHashTable *)p);
191             break;
192         }
193         default:
194             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195             break;
196     }
197 }
198
199 static void
200 _plugin_get_property (GObject *object,
201                       guint    prop_id,
202                       GValue  *value,
203                       GParamSpec *pspec)
204 {
205     TlmAccountPluginDefault *self = TLM_ACCOUNT_PLUGIN_DEFAULT (object);
206
207     switch (prop_id) {
208         case PROP_CONFIG:
209             g_value_set_boxed (value, self->config);
210             break;
211         default:
212             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213             break;
214     }
215 }
216
217 static void
218 tlm_account_plugin_default_class_init (TlmAccountPluginDefaultClass *kls)
219 {
220     GObjectClass *g_class = G_OBJECT_CLASS (kls);
221
222     g_class->set_property = _plugin_set_property;
223     g_class->get_property = _plugin_get_property;
224     g_class->finalize = _plugin_finalize;
225
226     g_object_class_override_property (g_class, PROP_CONFIG, "config");
227 }
228
229 static void
230 tlm_account_plugin_default_init (TlmAccountPluginDefault *self)
231 {
232     tlm_log_init(G_LOG_DOMAIN);
233 }
234