Back to index

lightdm  1.3.2
lightdm-set-defaults.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Didier Roche.
00003  * Author: Didier Roche <didrocks@ubuntu.com>
00004  * 
00005  * This program is free software: you can redistribute it and/or modify it under
00006  * the terms of the GNU General Public License as published by the Free Software
00007  * Foundation, either version 3 of the License, or (at your option) any later
00008  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015 
00016 #include <stdlib.h>
00017 #include <unistd.h>
00018 
00019 #include <glib.h>
00020 #include <glib/gi18n.h>
00021 
00022 #define SEATDEFAULT_KEY_GROUP "SeatDefaults"
00023 #define SESSION_KEY_NAME  "user-session"
00024 #define GREETER_KEY_NAME  "greeter-session"
00025 #define AUTOLOGIN_KEY_NAME  "autologin-user"
00026 #define HIDE_USERS_KEY_NAME  "greeter-hide-users"
00027 #define MANUAL_LOGIN_KEY_NAME  "greeter-show-manual-login"
00028 #define ALLOW_GUEST_KEY_NAME  "allow-guest"
00029 
00030 #define IS_STRING_EMPTY(x) ((x)==NULL||(x)[0]=='\0')
00031 
00032 static gboolean debug = FALSE;
00033 static gboolean keep_old = FALSE;
00034 static gboolean remove = FALSE;
00035 static gboolean hide_users = FALSE;
00036 static gboolean show_manual_login = FALSE;
00037 static gboolean allow_guest = FALSE;
00038 
00039 static char    *session = NULL;
00040 static char    *greeter = NULL;
00041 static char    *autologin = NULL;
00042 static char    *str_hide_users = NULL;
00043 static char    *str_show_manual_login = NULL;
00044 static char    *str_allow_guest = NULL;
00045 
00046 static GOptionEntry entries[] =
00047 {
00048   { "debug",    'd', 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL },
00049   { "keep-old", 'k', 0, G_OPTION_ARG_NONE, &keep_old, N_("Only update if no default already set"), NULL },
00050   { "remove",   'r', 0, G_OPTION_ARG_NONE, &remove, N_("Remove default value if it's the current one"), NULL },
00051   { "session",  's', 0, G_OPTION_ARG_STRING, &session, N_("Set default session"), NULL },
00052   { "greeter",  'g', 0, G_OPTION_ARG_STRING, &greeter, N_("Set default greeter"), NULL },
00053   { "autologin",'a', 0, G_OPTION_ARG_STRING, &autologin, N_("Set autologin user"), NULL },
00054   { "hide-users",'i', 0, G_OPTION_ARG_STRING, &str_hide_users, N_("Set greeter-hide-users to true or false"), NULL },
00055   { "show-manual-login",'m', 0, G_OPTION_ARG_STRING, &str_show_manual_login, N_("Set show-manual-login to true or false"), NULL },
00056   { "allow-guest",'l', 0, G_OPTION_ARG_STRING, &str_allow_guest, N_("Set allow-guest to true or false"), NULL },
00057   { NULL }
00058 };
00059 
00060 void
00061 show_nothing(const gchar   *log_domain,
00062              GLogLevelFlags log_level,
00063              const gchar   *message,
00064              gpointer       unused_data) {};
00065 
00066 int
00067 update_boolean(const gboolean new_value,
00068               gboolean     keep_old,
00069               const gchar *key_group,
00070               const gchar *key_name,
00071               GKeyFile    *keyfile)
00072 {
00073     gboolean success = TRUE;
00074 
00075     if (keep_old)
00076         g_debug ("keep-old mode: keep previous default value");
00077     else {
00078         g_debug ("Update to %d for %s", new_value, key_name);
00079         g_key_file_set_boolean (keyfile, key_group, key_name, new_value);
00080     }
00081     if (!success)
00082         return(2);
00083     return(0);
00084 }
00085 
00086 int
00087 update_string(const gchar *default_value,
00088               const gchar *new_value,
00089               gboolean     keep_old,
00090               gboolean     remove,
00091               const gchar *key_group,
00092               const gchar *key_name,
00093               GKeyFile    *keyfile)
00094 {
00095     gboolean success = TRUE;
00096         
00097     if (!(default_value) || (strlen(default_value) < 1)) {
00098         g_debug ("No existing valid value for %s. Set to %s", key_name, new_value);
00099         g_key_file_set_string (keyfile, key_group, key_name, new_value);
00100     }
00101     else {
00102         if (remove) {
00103             if (g_strcmp0 (default_value, new_value) == 0) {
00104                 g_debug ("Remove %s as default value for %s", default_value, key_name);
00105                 g_key_file_set_string (keyfile, key_group, key_name, "");
00106                 if (!success)
00107                     return(2);
00108                 return(0);
00109             }
00110             g_debug ("Can't remove: %s is not the default value for %s", default_value, key_name);
00111             return(4);
00112         }
00113         else {
00114             g_debug ("Found existing default value(%s) for %s", default_value, key_name);
00115             if (keep_old)
00116                 g_debug ("keep-old mode: keep previous default value");
00117             else {
00118                 g_debug ("Update to %s for %s", new_value, key_name);
00119                 g_key_file_set_string (keyfile, key_group, key_name, new_value);
00120             }
00121         }
00122     }
00123     if (!success)
00124         return(2);
00125     return(0);
00126 }
00127 
00128 int 
00129 str_to_bool(const gchar *str, gboolean *bool_out)
00130 {
00131     if (IS_STRING_EMPTY(str)) {
00132         return -1;
00133     }
00134     else if (strncasecmp(str, "true", 4)==0) {
00135         *bool_out = TRUE;
00136         return 0;
00137     }
00138     else if (strncasecmp(str, "false", 5)==0) {
00139         *bool_out = FALSE;
00140         return 0;
00141     }
00142     else {
00143         return -2;
00144     }
00145 }
00146 
00147 int 
00148 main (int argc, char *argv[])
00149 {
00150     GOptionContext *context = NULL;
00151     GError         *error = NULL;
00152 
00153     GKeyFile       *keyfile;
00154     GKeyFileFlags   flags;
00155     gchar          *s_data;
00156     gsize           size;
00157     const gchar    *gdm_conf_file = CONFIG_DIR "/lightdm.conf";
00158 
00159     gchar          *default_session = NULL;
00160     gchar          *default_greeter = NULL;
00161     gchar          *default_autologin = NULL;
00162     gint            return_code = 0;
00163 
00164     bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
00165     bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
00166     textdomain (GETTEXT_PACKAGE);
00167 
00168     g_type_init ();
00169 
00170     context = g_option_context_new (N_("- set lightdm default values"));
00171     g_option_context_add_main_entries (context, entries, NULL);
00172     if (!g_option_context_parse (context, &argc, &argv, &error)) {
00173         g_printerr (N_("option parsing failed: %s\n"), error->message);
00174         g_option_context_free (context);
00175         g_error_free (error);
00176         return 1;
00177     }
00178     if (IS_STRING_EMPTY (session) && IS_STRING_EMPTY (greeter) && IS_STRING_EMPTY (autologin) && IS_STRING_EMPTY(str_hide_users) && IS_STRING_EMPTY(str_show_manual_login) && IS_STRING_EMPTY(str_allow_guest)) {
00179         g_printerr (N_("Wrong usage of the command\n%s"), g_option_context_get_help (context, FALSE, NULL));
00180         g_option_context_free (context);
00181         return 1;
00182     }
00183     if (context)
00184         g_option_context_free (context); 
00185     if (!debug)
00186         g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, show_nothing, NULL);
00187 
00188     keyfile = g_key_file_new ();
00189     flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
00190     if (!(g_key_file_load_from_file (keyfile, gdm_conf_file, flags, &error))) {
00191             g_debug ("File doesn't seem to exist or can't be read: create one (%s)", error->message);
00192             g_error_free (error);
00193             error = NULL;
00194     }
00195 
00196     // try to get the right keys
00197     default_session = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, SESSION_KEY_NAME, NULL);
00198     default_greeter = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, GREETER_KEY_NAME, NULL);
00199     default_autologin = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, AUTOLOGIN_KEY_NAME, NULL);
00200 
00201     if (!(IS_STRING_EMPTY (session)))
00202         return_code = update_string (default_session, session, keep_old, remove, SEATDEFAULT_KEY_GROUP, SESSION_KEY_NAME, keyfile);
00203     if (!(IS_STRING_EMPTY (greeter)) && (return_code == 0))
00204         return_code = update_string (default_greeter, greeter, keep_old, remove, SEATDEFAULT_KEY_GROUP, GREETER_KEY_NAME, keyfile);
00205     if (!(IS_STRING_EMPTY (autologin)) && (return_code == 0))
00206         return_code = update_string (default_autologin, autologin, keep_old, remove, SEATDEFAULT_KEY_GROUP, AUTOLOGIN_KEY_NAME, keyfile);
00207     if (!(IS_STRING_EMPTY(str_hide_users)) && (return_code == 0)) {
00208         if (str_to_bool(str_hide_users, &hide_users) == 0) {
00209             return_code = update_boolean (hide_users, keep_old, SEATDEFAULT_KEY_GROUP, HIDE_USERS_KEY_NAME, keyfile);
00210         }
00211         else {
00212             g_printerr (N_("true and false are the only valid choices for hide-users\n"));
00213             return 1;
00214         }
00215     }
00216     if (!(IS_STRING_EMPTY(str_allow_guest)) && (return_code == 0)) {
00217         if (str_to_bool(str_allow_guest, &allow_guest) == 0) {
00218             return_code = update_boolean (allow_guest, keep_old, SEATDEFAULT_KEY_GROUP, ALLOW_GUEST_KEY_NAME, keyfile);
00219         }
00220         else {
00221             g_printerr (N_("true and false are the only valid choices for allow-guest\n"));
00222             return 1;
00223         }
00224     }
00225     if (!(IS_STRING_EMPTY(str_show_manual_login)) && (return_code == 0)) {
00226         if (str_to_bool(str_show_manual_login, &show_manual_login) == 0) {
00227             return_code = update_boolean (show_manual_login, keep_old, SEATDEFAULT_KEY_GROUP, MANUAL_LOGIN_KEY_NAME, keyfile);
00228         }
00229         else {
00230             g_printerr (N_("true and false are the only valid choices for show-manual-login\n"));
00231             return 1;
00232         }
00233     }
00234 
00235     if (return_code == 0) {
00236         s_data = g_key_file_to_data (keyfile, &size, &error);
00237         if (!s_data) {
00238             g_debug ("Can't convert data to string: %s", error->message);
00239             g_error_free (error);
00240             return_code = 1;
00241         }
00242         else {
00243             if(!g_file_set_contents (gdm_conf_file, s_data, size, &error)) {
00244                 g_printerr ("Can't update: %s\n", error->message);
00245                 g_error_free (error);
00246                 return_code = 1;
00247             }
00248             g_free (s_data);
00249          }
00250     }
00251 
00252     g_key_file_free (keyfile);
00253 
00254     if (default_session)
00255         g_free (default_session);
00256     if (default_greeter)
00257         g_free (default_greeter);
00258     if (default_autologin)
00259         g_free (default_autologin);
00260 
00261     return return_code;
00262 
00263 }