6 #include <glib-object.h>
10 static GKeyFile *config;
13 main (int argc, char **argv)
17 #if !defined(GLIB_VERSION_2_36)
21 status_connect (NULL, NULL);
23 config = g_key_file_new ();
24 g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
26 passwd_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
28 if (argc == 2 && strcmp (argv[1], "add") == 0)
30 gchar *home_dir, *username, line[1024];
34 /* Create a unique name */
35 home_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "home", "guest-XXXXXX", NULL);
36 if (!mkdtemp (home_dir))
38 g_printerr ("Failed to create home directory %s: %s\n", home_dir, strerror (errno));
41 username = strrchr (home_dir, '/') + 1;
43 /* Get the largest UID */
44 passwd = fopen (passwd_path, "r");
47 while (fgets (line, 1024, passwd))
49 gchar **tokens = g_strsplit (line, ":", -1);
50 if (g_strv_length (tokens) >= 3)
52 gint uid = atoi (tokens[2]);
61 /* Add a new account to the passwd file */
62 passwd = fopen (passwd_path, "a");
63 fprintf (passwd, "%s::%d:%d:Guest Account:%s:/bin/sh\n", username, max_uid+1, max_uid+1, home_dir);
66 status_notify ("GUEST-ACCOUNT ADD USERNAME=%s", username);
68 /* Print out the username so LightDM picks it up */
69 g_print ("%s\n", username);
73 else if (argc == 3 && strcmp (argv[1], "remove") == 0)
75 gchar *username, *path, *prefix, line[1024];
76 FILE *passwd, *new_passwd;
80 status_notify ("GUEST-ACCOUNT REMOVE USERNAME=%s", username);
82 /* Open a new file for writing */
83 passwd = fopen (passwd_path, "r");
84 path = g_strdup_printf ("%s~", passwd_path);
85 new_passwd = fopen (path, "w");
87 /* Copy the old file, omitting our entry */
88 prefix = g_strdup_printf ("%s:", username);
89 while (fgets (line, 1024, passwd))
91 if (!g_str_has_prefix (line, prefix))
92 fprintf (new_passwd, "%s", line);
97 /* Move the new file on the old one */
98 rename (path, passwd_path);
100 /* Delete home directory */
101 gchar *command = g_strdup_printf ("rm -r %s/home/%s", g_getenv ("LIGHTDM_TEST_ROOT"), username);
102 if (system (command))
103 perror ("Failed to delete temp directory");
108 g_printerr ("Usage %s add|remove\n", argv[0]);