Back to index

lightdm  1.3.2
guest-account.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <errno.h>
00004 #include <string.h>
00005 #include <glib.h>
00006 #include <glib-object.h>
00007 
00008 #include "status.h"
00009 
00010 static GKeyFile *config;
00011 
00012 static void
00013 request_cb (const gchar *message)
00014 {
00015 }
00016 
00017 int
00018 main (int argc, char **argv)
00019 {
00020     gchar *passwd_path;
00021 
00022     g_type_init ();
00023 
00024     status_connect (request_cb);
00025 
00026     config = g_key_file_new ();
00027     g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
00028 
00029     passwd_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
00030 
00031     if (argc == 2 && strcmp (argv[1], "add") == 0)
00032     {
00033         gchar *home_dir, *username, line[1024];
00034         gint max_uid = 1000;
00035         FILE *passwd;
00036 
00037         /* Create a unique name */
00038         home_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "home", "guest-XXXXXX", NULL);
00039         if (!mkdtemp (home_dir))
00040         {
00041             g_printerr ("Failed to create home directory %s: %s\n", home_dir, strerror (errno));
00042             return EXIT_FAILURE;
00043         }
00044         username = strrchr (home_dir, '/') + 1;
00045 
00046         /* Get the largest UID */
00047         passwd = fopen (passwd_path, "r");
00048         if (passwd)
00049         {
00050             while (fgets (line, 1024, passwd))
00051             {
00052                 gchar **tokens = g_strsplit (line, ":", -1);
00053                 if (g_strv_length (tokens) >= 3)
00054                 {
00055                     gint uid = atoi (tokens[2]);
00056                     if (uid > max_uid)
00057                         max_uid = uid;
00058                 }
00059                 g_strfreev (tokens);
00060             }
00061             fclose (passwd);
00062         }
00063 
00064         /* Add a new account to the passwd file */
00065         passwd = fopen (passwd_path, "a");
00066         fprintf (passwd, "%s::%d:%d:Guest Account:%s:/bin/sh\n", username, max_uid+1, max_uid+1, home_dir);
00067         fclose (passwd);
00068 
00069         status_notify ("GUEST-ACCOUNT ADD USERNAME=%s", username);
00070 
00071         /* Print out the username so LightDM picks it up */
00072         g_print ("%s\n", username);
00073 
00074         return EXIT_SUCCESS;
00075     }
00076     else if (argc == 3 && strcmp (argv[1], "remove") == 0)
00077     {
00078         gchar *username, *path, *prefix, line[1024];
00079         FILE *passwd, *new_passwd;
00080 
00081         username = argv[2];
00082 
00083         status_notify ("GUEST-ACCOUNT REMOVE USERNAME=%s", username);
00084 
00085         /* Open a new file for writing */
00086         passwd = fopen (passwd_path, "r");
00087         path = g_strdup_printf ("%s~", passwd_path);
00088         new_passwd = fopen (path, "w");
00089 
00090         /* Copy the old file, omitting our entry */
00091         prefix = g_strdup_printf ("%s:", username);
00092         while (fgets (line, 1024, passwd))
00093         {
00094             if (!g_str_has_prefix (line, prefix))
00095                 fprintf (new_passwd, "%s", line);
00096         }
00097         fclose (passwd);
00098         fclose (new_passwd);
00099 
00100         /* Move the new file on the old one */
00101         rename (path, passwd_path);
00102 
00103         /* Delete home directory */
00104         gchar *command = g_strdup_printf ("rm -r %s/home/%s", g_getenv ("LIGHTDM_TEST_ROOT"), username);
00105         if (system (command))
00106             perror ("Failed to delete temp directory");
00107 
00108         return EXIT_SUCCESS;
00109     }
00110 
00111     g_printerr ("Usage %s add|remove\n", argv[0]);
00112     return EXIT_FAILURE;
00113 }