| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 3 |
/* |
| 4 |
* libmbim-glib -- GLib/GIO based library to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright (C) 2013 - 2021 Aleksander Morgado <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <config.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <limits.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <grp.h> |
| 17 |
#include <pwd.h> |
| 18 |
|
| 19 |
#include "mbim-helpers.h" |
| 20 |
#include "mbim-error-types.h" |
| 21 |
|
| 22 |
/******************************************************************************/ |
| 23 |
|
| 24 |
guint16 |
| 25 |
mbim_helpers_read_unaligned_guint16 (const guint8 *buffer) |
| 26 |
{ |
| 27 |
guint16 tmp; |
| 28 |
|
| 29 |
memcpy (&tmp, buffer, 2); |
| 30 |
return GUINT16_FROM_LE (tmp); |
| 31 |
} |
| 32 |
|
| 33 |
guint32 |
| 34 |
mbim_helpers_read_unaligned_guint32 (const guint8 *buffer) |
| 35 |
{ |
| 36 |
guint32 tmp; |
| 37 |
|
| 38 |
memcpy (&tmp, buffer, 4); |
| 39 |
return GUINT32_FROM_LE (tmp); |
| 40 |
} |
| 41 |
|
| 42 |
guint32 |
| 43 |
mbim_helpers_read_unaligned_gint32 (const guint8 *buffer) |
| 44 |
{ |
| 45 |
gint32 tmp; |
| 46 |
|
| 47 |
memcpy (&tmp, buffer, 4); |
| 48 |
return GINT32_FROM_LE (tmp); |
| 49 |
} |
| 50 |
|
| 51 |
guint64 |
| 52 |
mbim_helpers_read_unaligned_guint64 (const guint8 *buffer) |
| 53 |
{ |
| 54 |
guint64 tmp; |
| 55 |
|
| 56 |
memcpy (&tmp, buffer, 8); |
| 57 |
return GUINT64_FROM_LE (tmp); |
| 58 |
} |
| 59 |
|
| 60 |
/*****************************************************************************/ |
| 61 |
|
| 62 |
gboolean |
| 63 |
mbim_helpers_check_user_allowed (uid_t uid, |
| 64 |
GError **error) |
| 65 |
{ |
| 66 |
#ifndef MBIM_USERNAME_ENABLED |
| 67 |
if (uid == 0) |
| 68 |
return TRUE; |
| 69 |
#else |
| 70 |
# ifndef MBIM_USERNAME |
| 71 |
# error MBIM username not defined |
| 72 |
# endif |
| 73 |
|
| 74 |
struct passwd *expected_usr = NULL; |
| 75 |
|
| 76 |
/* Root user is always allowed, regardless of the specified MBIM_USERNAME */ |
| 77 |
if (uid == 0) |
| 78 |
return TRUE; |
| 79 |
|
| 80 |
expected_usr = getpwnam (MBIM_USERNAME); |
| 81 |
if (!expected_usr) { |
| 82 |
g_set_error (error, |
| 83 |
G_IO_ERROR, |
| 84 |
g_io_error_from_errno (errno), |
| 85 |
"Not enough privileges (unknown username %s)", MBIM_USERNAME); |
| 86 |
return FALSE; |
| 87 |
} |
| 88 |
|
| 89 |
if (uid == expected_usr->pw_uid) |
| 90 |
return TRUE; |
| 91 |
#endif |
| 92 |
|
| 93 |
g_set_error (error, |
| 94 |
MBIM_CORE_ERROR, |
| 95 |
MBIM_CORE_ERROR_FAILED, |
| 96 |
"Not enough privileges"); |
| 97 |
return FALSE; |
| 98 |
} |
| 99 |
|
| 100 |
/*****************************************************************************/ |
| 101 |
|
| 102 |
gboolean |
| 103 |
mbim_helpers_check_group_allowed (uid_t uid, |
| 104 |
GError **error) |
| 105 |
{ |
| 106 |
#ifndef MBIM_GROUPNAME_ENABLED |
| 107 |
if (uid == 0) |
| 108 |
return TRUE; |
| 109 |
#else |
| 110 |
# ifndef MBIM_GROUPNAME |
| 111 |
# error MBIM groupname not defined |
| 112 |
# endif |
| 113 |
|
| 114 |
struct passwd *pw; |
| 115 |
struct group *grp; |
| 116 |
guint i; |
| 117 |
|
| 118 |
grp = getgrnam (MBIM_GROUPNAME); |
| 119 |
if (!grp) { |
| 120 |
g_set_error (error, |
| 121 |
G_IO_ERROR, |
| 122 |
g_io_error_from_errno (errno), |
| 123 |
"Not enough privileges (unknown group %s)", MBIM_GROUPNAME); |
| 124 |
return FALSE; |
| 125 |
} |
| 126 |
|
| 127 |
pw = getpwuid (uid); |
| 128 |
if (pw && pw->pw_gid == grp->gr_gid) |
| 129 |
return TRUE; |
| 130 |
|
| 131 |
if (pw && grp->gr_mem) { |
| 132 |
for (i = 0; grp->gr_mem[i]; i++) { |
| 133 |
if (g_strcmp0 (grp->gr_mem[i], pw->pw_name) == 0) |
| 134 |
return TRUE; |
| 135 |
} |
| 136 |
} |
| 137 |
#endif |
| 138 |
g_set_error (error, |
| 139 |
MBIM_CORE_ERROR, |
| 140 |
MBIM_CORE_ERROR_FAILED, |
| 141 |
"Not enough privileges"); |
| 142 |
return FALSE; |
| 143 |
} |
| 144 |
|
| 145 |
/*****************************************************************************/ |
| 146 |
|
| 147 |
gchar * |
| 148 |
mbim_helpers_get_devpath (const gchar *cdc_wdm_path, |
| 149 |
GError **error) |
| 150 |
{ |
| 151 |
gchar *aux; |
| 152 |
|
| 153 |
if (!g_file_test (cdc_wdm_path, G_FILE_TEST_IS_SYMLINK)) |
| 154 |
return g_strdup (cdc_wdm_path); |
| 155 |
|
| 156 |
aux = realpath (cdc_wdm_path, NULL); |
| 157 |
if (!aux) { |
| 158 |
int saved_errno = errno; |
| 159 |
|
| 160 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED, |
| 161 |
"Couldn't get realpath: %s", g_strerror (saved_errno)); |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
return aux; |
| 166 |
} |
| 167 |
|
| 168 |
/*****************************************************************************/ |
| 169 |
|
| 170 |
gchar * |
| 171 |
mbim_helpers_get_devname (const gchar *cdc_wdm_path, |
| 172 |
GError **error) |
| 173 |
{ |
| 174 |
gchar *aux; |
| 175 |
gchar *devname = NULL; |
| 176 |
|
| 177 |
aux = mbim_helpers_get_devpath (cdc_wdm_path, error); |
| 178 |
if (aux) { |
| 179 |
devname = g_path_get_basename (aux); |
| 180 |
g_free (aux); |
| 181 |
} |
| 182 |
|
| 183 |
return devname; |
| 184 |
} |
| 185 |
|
| 186 |
/*****************************************************************************/ |
| 187 |
|
| 188 |
gboolean |
| 189 |
mbim_helpers_list_links_wdm (GFile *sysfs_file, |
| 190 |
GCancellable *cancellable, |
| 191 |
GPtrArray *previous_links, |
| 192 |
GPtrArray **out_links, |
| 193 |
GError **error) |
| 194 |
{ |
| 195 |
g_autofree gchar *sysfs_path = NULL; |
| 196 |
g_autoptr(GFileEnumerator) direnum = NULL; |
| 197 |
g_autoptr(GPtrArray) links = NULL; |
| 198 |
|
| 199 |
direnum = g_file_enumerate_children (sysfs_file, |
| 200 |
G_FILE_ATTRIBUTE_STANDARD_NAME, |
| 201 |
G_FILE_QUERY_INFO_NONE, |
| 202 |
cancellable, |
| 203 |
error); |
| 204 |
if (!direnum) |
| 205 |
return FALSE; |
| 206 |
|
| 207 |
sysfs_path = g_file_get_path (sysfs_file); |
| 208 |
links = g_ptr_array_new_with_free_func (g_free); |
| 209 |
|
| 210 |
while (TRUE) { |
| 211 |
GFileInfo *info; |
| 212 |
g_autofree gchar *filename = NULL; |
| 213 |
g_autofree gchar *link_path = NULL; |
| 214 |
g_autofree gchar *real_path = NULL; |
| 215 |
g_autofree gchar *basename = NULL; |
| 216 |
|
| 217 |
if (!g_file_enumerator_iterate (direnum, &info, NULL, cancellable, error)) |
| 218 |
return FALSE; |
| 219 |
if (!info) |
| 220 |
break; |
| 221 |
|
| 222 |
filename = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME); |
| 223 |
if (!filename || !g_str_has_prefix (filename, "upper_")) |
| 224 |
continue; |
| 225 |
|
| 226 |
link_path = g_strdup_printf ("%s/%s", sysfs_path, filename); |
| 227 |
real_path = realpath (link_path, NULL); |
| 228 |
if (!real_path) |
| 229 |
continue; |
| 230 |
|
| 231 |
basename = g_path_get_basename (real_path); |
| 232 |
|
| 233 |
/* skip interface if it was already known */ |
| 234 |
if (previous_links && g_ptr_array_find_with_equal_func (previous_links, basename, g_str_equal, NULL)) |
| 235 |
continue; |
| 236 |
|
| 237 |
g_ptr_array_add (links, g_steal_pointer (&basename)); |
| 238 |
} |
| 239 |
|
| 240 |
if (!links || !links->len) { |
| 241 |
*out_links = NULL; |
| 242 |
return TRUE; |
| 243 |
} |
| 244 |
|
| 245 |
g_ptr_array_sort (links, (GCompareFunc) g_ascii_strcasecmp); |
| 246 |
*out_links = g_steal_pointer (&links); |
| 247 |
return TRUE; |
| 248 |
} |
| 249 |
|
| 250 |
/*****************************************************************************/ |
| 251 |
|
| 252 |
gboolean |
| 253 |
mbim_helpers_list_links_wwan (const gchar *base_ifname, |
| 254 |
GFile *sysfs_file, |
| 255 |
GCancellable *cancellable, |
| 256 |
GPtrArray *previous_links, |
| 257 |
GPtrArray **out_links, |
| 258 |
GError **error) |
| 259 |
{ |
| 260 |
g_autofree gchar *sysfs_path = NULL; |
| 261 |
g_autoptr(GFileEnumerator) direnum = NULL; |
| 262 |
g_autoptr(GPtrArray) links = NULL; |
| 263 |
|
| 264 |
direnum = g_file_enumerate_children (sysfs_file, |
| 265 |
G_FILE_ATTRIBUTE_STANDARD_NAME, |
| 266 |
G_FILE_QUERY_INFO_NONE, |
| 267 |
cancellable, |
| 268 |
error); |
| 269 |
if (!direnum) |
| 270 |
return FALSE; |
| 271 |
|
| 272 |
sysfs_path = g_file_get_path (sysfs_file); |
| 273 |
links = g_ptr_array_new_with_free_func (g_free); |
| 274 |
|
| 275 |
while (TRUE) { |
| 276 |
GFileInfo *info; |
| 277 |
g_autofree gchar *filename = NULL; |
| 278 |
g_autofree gchar *link_path = NULL; |
| 279 |
g_autofree gchar *real_path = NULL; |
| 280 |
g_autofree gchar *basename = NULL; |
| 281 |
|
| 282 |
if (!g_file_enumerator_iterate (direnum, &info, NULL, cancellable, error)) |
| 283 |
return FALSE; |
| 284 |
if (!info) |
| 285 |
break; |
| 286 |
|
| 287 |
filename = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME); |
| 288 |
if (!g_strcmp0(base_ifname, filename)) |
| 289 |
continue; |
| 290 |
|
| 291 |
link_path = g_strdup_printf ("%s/%s", sysfs_path, filename); |
| 292 |
real_path = realpath (link_path, NULL); |
| 293 |
if (!real_path) |
| 294 |
continue; |
| 295 |
|
| 296 |
basename = g_path_get_basename (real_path); |
| 297 |
|
| 298 |
/* skip interface if it was already known */ |
| 299 |
if (previous_links && g_ptr_array_find_with_equal_func (previous_links, basename, g_str_equal, NULL)) |
| 300 |
continue; |
| 301 |
|
| 302 |
g_ptr_array_add (links, g_steal_pointer (&basename)); |
| 303 |
} |
| 304 |
|
| 305 |
if (!links || !links->len) { |
| 306 |
*out_links = NULL; |
| 307 |
return TRUE; |
| 308 |
} |
| 309 |
|
| 310 |
g_ptr_array_sort (links, (GCompareFunc) g_ascii_strcasecmp); |
| 311 |
*out_links = g_steal_pointer (&links); |
| 312 |
return TRUE; |
| 313 |
} |