]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/guest-account.c
Releasing 1.20.0
[sojka/lightdm.git] / tests / src / guest-account.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <glib.h>
6 #include <glib-object.h>
7
8 #include "status.h"
9
10 static GKeyFile *config;
11
12 int
13 main (int argc, char **argv)
14 {
15     gchar *passwd_path;
16
17 #if !defined(GLIB_VERSION_2_36)
18     g_type_init ();
19 #endif
20
21     status_connect (NULL, NULL);
22
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);
25
26     passwd_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
27
28     if (argc == 2 && strcmp (argv[1], "add") == 0)
29     {
30         gchar *home_dir, *username, line[1024];
31         gint max_uid = 1000;
32         FILE *passwd;
33
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))
37         {
38             g_printerr ("Failed to create home directory %s: %s\n", home_dir, strerror (errno));
39             return EXIT_FAILURE;
40         }
41         username = strrchr (home_dir, '/') + 1;
42
43         /* Get the largest UID */
44         passwd = fopen (passwd_path, "r");
45         if (passwd)
46         {
47             while (fgets (line, 1024, passwd))
48             {
49                 gchar **tokens = g_strsplit (line, ":", -1);
50                 if (g_strv_length (tokens) >= 3)
51                 {
52                     gint uid = atoi (tokens[2]);
53                     if (uid > max_uid)
54                         max_uid = uid;
55                 }
56                 g_strfreev (tokens);
57             }
58             fclose (passwd);
59         }
60
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);
64         fclose (passwd);
65
66         status_notify ("GUEST-ACCOUNT ADD USERNAME=%s", username);
67
68         /* Print out the username so LightDM picks it up */
69         g_print ("%s\n", username);
70
71         return EXIT_SUCCESS;
72     }
73     else if (argc == 3 && strcmp (argv[1], "remove") == 0)
74     {
75         gchar *username, *path, *prefix, line[1024];
76         FILE *passwd, *new_passwd;
77
78         username = argv[2];
79
80         status_notify ("GUEST-ACCOUNT REMOVE USERNAME=%s", username);
81
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");
86
87         /* Copy the old file, omitting our entry */
88         prefix = g_strdup_printf ("%s:", username);
89         while (fgets (line, 1024, passwd))
90         {
91             if (!g_str_has_prefix (line, prefix))
92                 fprintf (new_passwd, "%s", line);
93         }
94         fclose (passwd);
95         fclose (new_passwd);
96
97         /* Move the new file on the old one */
98         rename (path, passwd_path);
99
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");
104
105         return EXIT_SUCCESS;
106     }
107
108     g_printerr ("Usage %s add|remove\n", argv[0]);
109     return EXIT_FAILURE;
110 }