GCC Code Coverage Report


Directory: ./
File: panels/system/users/user-utils.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 156 0.0%
Functions: 0 12 0.0%
Branches: 0 78 0.0%

Line Branch Exec Source
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright 2009-2010 Red Hat, Inc,
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Written by: Matthias Clasen <mclasen@redhat.com>
19 */
20
21 #include "config.h"
22
23 #include <math.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <limits.h>
28 #include <unistd.h>
29 #include <utmpx.h>
30 #include <pwd.h>
31
32 #ifdef __FreeBSD__
33 #include <sysexits.h>
34 #endif
35
36 #include <gio/gio.h>
37 #include <glib/gi18n.h>
38 #include <glib/gstdio.h>
39
40 #include "user-utils.h"
41
42 /* Taken from defines.h in shadow-utils. On Linux, this value is much smaller
43 * than the sysconf limit LOGIN_NAME_MAX, and values larger than this will
44 * result in failure when running useradd. We could check UT_NAMESIZE instead,
45 * but that is nonstandard. Better to use POSIX utmpx.
46 */
47 gsize
48 get_username_max_length (void)
49 {
50 return sizeof (((struct utmpx *)NULL)->ut_user);
51 }
52
53 gboolean
54 is_username_used (const gchar *username)
55 {
56 struct passwd *pwent;
57
58 if (username == NULL || username[0] == '\0') {
59 return FALSE;
60 }
61
62 pwent = getpwnam (username);
63
64 return pwent != NULL;
65 }
66
67 gboolean
68 is_valid_name (const gchar *name)
69 {
70 gboolean is_empty = TRUE;
71 gboolean found_comma = FALSE;
72 const gchar *c;
73
74 if (name == NULL)
75 return is_empty;
76
77 /* Valid names must contain:
78 * 1) at least one character.
79 * 2) at least one non-"space" character.
80 * 3) comma character not allowed. Issue #888
81 */
82 for (c = name; *c; c++) {
83 gunichar unichar;
84
85 unichar = g_utf8_get_char_validated (c, -1);
86
87 /* Partial UTF-8 sequence or end of string */
88 if (unichar == (gunichar) -1 || unichar == (gunichar) -2)
89 break;
90
91 /* Check for non-space character */
92 if (is_empty && !g_unichar_isspace (unichar)) {
93 is_empty = FALSE;
94 }
95
96 if (unichar == ',') {
97 found_comma = TRUE;
98 break;
99 }
100 }
101
102 return !is_empty && !found_comma;
103 }
104
105 typedef struct {
106 gchar *username;
107 gchar *tip;
108 } isValidUsernameData;
109
110 static void
111 is_valid_username_data_free (isValidUsernameData *data)
112 {
113 g_clear_pointer (&data->username, g_free);
114 g_clear_pointer (&data->tip, g_free);
115 g_free (data);
116 }
117
118 #ifdef __FreeBSD__
119 /* Taken from pw(8) man page. */
120 #define E_SUCCESS EX_OK
121 #define E_BAD_ARG EX_DATAERR
122 #define E_NOTFOUND EX_NOUSER
123 #else
124 /* Taken from usermod.c in shadow-utils. */
125 #define E_SUCCESS 0
126 #define E_BAD_ARG 3
127 #define E_NOTFOUND 6
128 #endif
129
130 static void
131 is_valid_username_child_watch_cb (GPid pid,
132 gint status,
133 gpointer user_data)
134 {
135 g_autoptr(GTask) task = G_TASK (user_data);
136 isValidUsernameData *data = g_task_get_task_data (task);
137 GError *error = NULL;
138 gboolean valid = FALSE;
139 const gchar *tip = NULL;
140
141 if (WIFEXITED (status)) {
142 switch (WEXITSTATUS (status)) {
143 case E_NOTFOUND:
144 valid = TRUE;
145 break;
146 case E_BAD_ARG:
147 tip = _("The username should usually only consist of lower case letters from a-z, digits and the following characters: - _");
148 valid = FALSE;
149 break;
150 case E_SUCCESS:
151 tip = _("Sorry, that user name isn’t available. Please try another.");
152 valid = FALSE;
153 break;
154 }
155 }
156
157 if (valid || tip != NULL) {
158 data->tip = g_strdup (tip);
159 g_task_return_boolean (task, valid);
160 }
161 else {
162 g_spawn_check_wait_status (status, &error);
163 g_task_return_error (task, error);
164 }
165
166 g_spawn_close_pid (pid);
167 }
168
169 void
170 is_valid_username_async (const gchar *username,
171 GCancellable *cancellable,
172 GAsyncReadyCallback callback,
173 gpointer callback_data)
174 {
175 g_autoptr(GTask) task = NULL;
176 isValidUsernameData *data;
177 gchar *argv[6];
178 GPid pid;
179 GError *error = NULL;
180
181 task = g_task_new (NULL, cancellable, callback, callback_data);
182 g_task_set_source_tag (task, is_valid_username_async);
183
184 data = g_new0 (isValidUsernameData, 1);
185 data->username = g_strdup (username);
186 g_task_set_task_data (task, data, (GDestroyNotify) is_valid_username_data_free);
187
188 if (username == NULL || username[0] == '\0') {
189 g_task_return_boolean (task, FALSE);
190 return;
191 }
192 else if (strlen (username) > get_username_max_length ()) {
193 data->tip = g_strdup (_("The username is too long."));
194 g_task_return_boolean (task, FALSE);
195 return;
196 }
197
198 #ifdef __FreeBSD__
199 /* Abuse "pw usershow -n <name>" in the same way as the code below. We
200 * don't use "pw usermod -n <name> -N -l <newname>" here because it has
201 * a special case for "root" to reject changes to the root user.
202 */
203 argv[0] = "pw";
204 argv[1] = "usershow";
205 argv[2] = "-n";
206 argv[3] = data->username;
207 argv[4] = NULL;
208 #else
209 /* "usermod --login" is meant to be used to change a username, but the
210 * exit codes can be safely abused to check the validity of username.
211 * However, the current "usermod" implementation may change in the
212 * future, so it would be nice to have some official way for this
213 * instead of relying on the current "--login" implementation.
214 */
215 argv[0] = "/usr/sbin/usermod";
216 argv[1] = "--login";
217 argv[2] = data->username;
218 argv[3] = "--";
219 argv[4] = data->username;
220 argv[5] = NULL;
221 #endif
222
223 if (!g_spawn_async (NULL, argv, NULL,
224 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD |
225 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
226 NULL, NULL, &pid, &error)) {
227 g_task_return_error (task, error);
228 return;
229 }
230
231 g_child_watch_add (pid, (GChildWatchFunc) is_valid_username_child_watch_cb, task);
232 g_steal_pointer (&task);
233 }
234
235 gboolean
236 is_valid_username_finish (GAsyncResult *result,
237 gchar **tip,
238 gchar **username,
239 GError **error)
240 {
241 GTask *task;
242 isValidUsernameData *data;
243
244 g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
245
246 task = G_TASK (result);
247 data = g_task_get_task_data (task);
248
249 if (tip != NULL) {
250 *tip = g_steal_pointer (&data->tip);
251 }
252
253 if (username != NULL)
254 *username = g_steal_pointer (&data->username);
255
256 return g_task_propagate_boolean (task, error);
257 }
258
259 /* This function was taken from AdwAvatar and modified so that it's possible to
260 * export a GdkTexture at a different size than the AdwAvatar is rendered
261 * See: https://gitlab.gnome.org/GNOME/libadwaita/-/blob/afd0fab86ff9b4332d165b985a435ea6f822d41b/src/adw-avatar.c#L751
262 * License: LGPL-2.1-or-later */
263 GdkTexture *
264 draw_avatar_to_texture (AdwAvatar *avatar, int size)
265 {
266 GdkTexture *result;
267 GskRenderNode *node;
268 GtkSnapshot *snapshot;
269 GdkPaintable *paintable;
270 GtkNative *native;
271 GskRenderer *renderer;
272 int real_size;
273 graphene_matrix_t transform;
274 gboolean transform_ok;
275
276 real_size = adw_avatar_get_size (avatar);
277
278 /* This works around the issue that when the custom-image or text of the AdwAvatar changes the
279 * allocation gets invalidated and therefore we can't snapshot the widget till the allocation
280 * is recalculated */
281 gtk_widget_measure (GTK_WIDGET (avatar), GTK_ORIENTATION_HORIZONTAL, real_size, NULL, NULL, NULL, NULL);
282 gtk_widget_allocate (GTK_WIDGET (avatar), real_size, real_size, -1, NULL);
283
284 transform_ok = gtk_widget_compute_transform (GTK_WIDGET (avatar),
285 gtk_widget_get_first_child (GTK_WIDGET (avatar)),
286 &transform);
287
288 g_assert (transform_ok);
289
290 snapshot = gtk_snapshot_new ();
291 gtk_snapshot_transform_matrix (snapshot, &transform);
292 GTK_WIDGET_GET_CLASS (avatar)->snapshot (GTK_WIDGET (avatar), snapshot);
293
294 /* Create first a GdkPaintable at the size the avatar was drawn
295 * then create a GdkSnapshot of it at the size requested */
296 paintable = gtk_snapshot_free_to_paintable (snapshot, &GRAPHENE_SIZE_INIT (real_size, real_size));
297 snapshot = gtk_snapshot_new ();
298 gdk_paintable_snapshot (paintable, snapshot, size, size);
299 g_object_unref (paintable);
300
301 node = gtk_snapshot_free_to_node (snapshot);
302
303 native = gtk_widget_get_native (GTK_WIDGET (avatar));
304 renderer = gtk_native_get_renderer (native);
305
306 result = gsk_renderer_render_texture (renderer, node, &GRAPHENE_RECT_INIT (-1, 0, size, size));
307
308 gsk_render_node_unref (node);
309
310 return result;
311 }
312
313 void
314 set_user_icon_data (ActUser *user,
315 GdkTexture *texture,
316 const gchar *image_source)
317 {
318 g_autofree gchar *path = NULL;
319 g_autoptr(GError) error = NULL;
320 int fd;
321
322 fd = g_file_open_tmp ("gnome-control-center-user-icon-XXXXXX", &path, &error);
323
324 if (fd == -1) {
325 g_warning ("Failed to create temporary user icon: %s", error->message);
326 return;
327 }
328
329 g_autoptr(GdkPixbuf) pixbuf = gdk_pixbuf_get_from_texture (texture);
330 gdk_pixbuf_save (pixbuf, path, "png", &error, IMAGE_SOURCE_KEY, image_source, NULL);
331
332 if (error != NULL) {
333 g_warning ("Failed to create temporary user icon: %s", error->message);
334 }
335
336 close (fd);
337
338 act_user_set_icon_file (user, path);
339
340 /* if we ever make the dbus call async, the g_remove call needs
341 * to wait for its completion
342 */
343 g_remove (path);
344 }
345
346 const gchar *
347 get_real_or_user_name (ActUser *user)
348 {
349 const gchar *name;
350
351 name = act_user_get_real_name (user);
352 if (name == NULL)
353 name = act_user_get_user_name (user);
354
355 return name;
356 }
357
358 void
359 setup_avatar_for_user (AdwAvatar *avatar, ActUser *user)
360 {
361 const gchar *avatar_file;
362
363 adw_avatar_set_custom_image (avatar, NULL);
364 adw_avatar_set_text (avatar, get_real_or_user_name (user));
365
366 avatar_file = act_user_get_icon_file (user);
367 if (avatar_file) {
368 g_autoptr(GdkPixbuf) pixbuf = NULL;
369 const gchar *image_source;
370 gboolean is_generated = TRUE;
371
372 pixbuf = gdk_pixbuf_new_from_file_at_size (avatar_file,
373 adw_avatar_get_size (avatar),
374 adw_avatar_get_size (avatar),
375 NULL);
376
377 if (pixbuf) {
378 image_source = gdk_pixbuf_get_option (pixbuf, IMAGE_SOURCE_KEY);
379
380 if (image_source == NULL)
381 g_debug ("User avatar's source isn't defined");
382 else
383 g_debug ("User avatar's source is %s", image_source);
384
385 is_generated = g_strcmp0 (image_source, "gnome-generated") == 0;
386 }
387
388 if (!is_generated) {
389 g_autoptr(GdkTexture) texture = NULL;
390
391 texture = gdk_texture_new_for_pixbuf (pixbuf);
392 adw_avatar_set_custom_image (avatar, GDK_PAINTABLE (texture));
393 }
394 }
395 }
396
397 GSettings *
398 settings_or_null (const gchar *schema)
399 {
400 GSettingsSchemaSource *source = NULL;
401 g_auto(GStrv) non_relocatable = NULL;
402 GSettings *settings = NULL;
403
404 source = g_settings_schema_source_get_default ();
405 if (!source)
406 return NULL;
407
408 g_settings_schema_source_list_schemas (source, TRUE, &non_relocatable, NULL);
409
410 if (g_strv_contains ((const gchar * const *)non_relocatable, schema))
411 settings = g_settings_new (schema);
412
413 return settings;
414 }
415