Skip to content

Commit

Permalink
Merge branch 'jb50129' into 'master'
Browse files Browse the repository at this point in the history
[usb-moded] Add support for removing user config. Fixes JB#50129

See merge request mer-core/usb-moded!62
  • Loading branch information
kende committed Jul 7, 2020
2 parents a8ffe2f + f1c409e commit 2e52ff0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 3 deletions.
3 changes: 3 additions & 0 deletions rpm/usb-moded.spec
Expand Up @@ -351,6 +351,8 @@ install -m 644 -D systemd/usb-moded.conf %{buildroot}/%{_sysconfdir}/tmpfiles.d/
install -m 644 -D systemd/adbd-prepare.service %{buildroot}/lib/systemd/system/adbd-prepare.service
install -m 644 -D systemd/adbd-prepare.service %{buildroot}/lib/systemd/system/graphical.target.wants/adbd-prepare.service
install -m 744 -D systemd/adbd-functionfs.sh %{buildroot}/usr/sbin/adbd-functionfs.sh
install -d %{buildroot}/usr/share/user-managerd/remove.d/
install -m 744 -D scripts/usb_mode_user_clear.sh %{buildroot}/usr/share/user-managerd/remove.d/

%preun
systemctl daemon-reload || :
Expand All @@ -377,6 +379,7 @@ systemctl daemon-reload || :
%{_sysconfdir}/tmpfiles.d/usb-moded.conf
%dir %{_sharedstatedir}/usb-moded
%ghost %{_sharedstatedir}/usb-moded/usb-moded.ini
/usr/share/user-managerd/remove.d/usb_mode_user_clear.sh

%files devel
%defattr(-,root,root,-)
Expand Down
7 changes: 7 additions & 0 deletions scripts/usb_mode_user_clear.sh
@@ -0,0 +1,7 @@
#!/bin/sh

if [ -z "$1" ]; then
exit 1;
fi

/usr/sbin/usb_moded_util -U $1
3 changes: 3 additions & 0 deletions src/com.meego.usb_moded.xml
Expand Up @@ -67,6 +67,9 @@
<arg name="value" type="s" direction="out"/>
</method>
<method name="rescue_off"/>
<method name="clear_config">
<arg name="uid" type="u" direction="in"/>
</method>
<signal name="sig_usb_state_ind">
<arg name="mode_or_event" type="s"/>
</signal>
Expand Down
1 change: 1 addition & 0 deletions src/usb_moded-config-private.h
Expand Up @@ -98,6 +98,7 @@ char *config_get_android_product_id (void);
char *config_get_hidden_modes (void);
char *config_get_mode_whitelist (void);
int config_is_roaming_not_allowed (void);
bool config_user_clear (uid_t uid);

/* ========================================================================= *
* Macros
Expand Down
25 changes: 25 additions & 0 deletions src/usb_moded-config.c
Expand Up @@ -118,6 +118,7 @@ char *config_get_android_product_id (void);
char *config_get_hidden_modes (void);
char *config_get_mode_whitelist (void);
int config_is_roaming_not_allowed (void);
bool config_user_clear (uid_t uid);

/* ========================================================================= *
* Functions
Expand Down Expand Up @@ -1101,3 +1102,27 @@ int config_is_roaming_not_allowed(void)

return config_get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY);
}

/**
* Remove user configs
*/
bool config_user_clear(uid_t uid)
{
if (uid < MIN_ADDITIONAL_USER || uid > MAX_ADDITIONAL_USER) {
log_err("Invalid uid value: %d\n", uid);
return false;
}

GKeyFile *active_ini = g_key_file_new();
config_load_dynamic_config(active_ini);

char *key = config_make_user_key_string(MODE_SETTING_KEY, uid);
if (key) {
if (g_key_file_remove_key(active_ini, MODE_SETTING_ENTRY, key, NULL))
config_save_dynamic_config(active_ini);
g_free(key);
}

g_key_file_free(active_ini);
return true;
}
26 changes: 26 additions & 0 deletions src/usb_moded-dbus.c
Expand Up @@ -224,6 +224,7 @@ static void usb_moded_whitelisted_set_cb (umdbus_context_t *context);
static void usb_moded_network_set_cb (umdbus_context_t *context);
static void usb_moded_network_get_cb (umdbus_context_t *context);
static void usb_moded_rescue_off_cb (umdbus_context_t *context);
static void usb_moded_user_config_clear_cb (umdbus_context_t *context);

/* ------------------------------------------------------------------------- *
* UMDBUS
Expand Down Expand Up @@ -827,6 +828,28 @@ usb_moded_whitelisted_modes_set_cb(umdbus_context_t *context)
dbus_error_free(&err);
}

/** Clear user config
*/
static void
usb_moded_user_config_clear_cb(umdbus_context_t *context)
{
LOG_REGISTER_CONTEXT;

dbus_uint32_t uid = 0;
DBusError err = DBUS_ERROR_INIT;

if( !dbus_message_get_args(context->msg, &err, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID) ) {
context->rsp = dbus_message_new_error(context->msg, DBUS_ERROR_INVALID_ARGS, context->member);
}
else {
if ( !config_user_clear(uid) )
context->rsp = dbus_message_new_error(context->msg, DBUS_ERROR_INVALID_ARGS, context->member);
else if( (context->rsp = dbus_message_new_method_return(context->msg)) )
dbus_message_append_args(context->rsp, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID);
}
dbus_error_free(&err);
}

/** Add usb mode to whitelist
*/
static void
Expand Down Expand Up @@ -994,6 +1017,9 @@ static const member_info_t usb_moded_members[] =
ADD_METHOD(USB_MODE_RESCUE_OFF,
usb_moded_rescue_off_cb,
0),
ADD_METHOD(USB_MODE_USER_CONFIG_CLEAR,
usb_moded_user_config_clear_cb,
" <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"),
ADD_SIGNAL(USB_MODE_SIGNAL_NAME,
" <arg name=\"mode_or_event\" type=\"s\"/>\n"),
ADD_SIGNAL(USB_MODE_CURRENT_STATE_SIGNAL_NAME,
Expand Down
1 change: 1 addition & 0 deletions src/usb_moded-dbus.h
Expand Up @@ -80,6 +80,7 @@
# define USB_MODE_AVAILABLE_MODES_GET "get_available_modes" /* returns a comma separated list of modes which are currently available for selection */
# define USB_MODE_AVAILABLE_MODES_FOR_USER "get_available_modes_for_user" /* returns a comma separated list of modes which are currently available and permitted for user to select */
# define USB_MODE_TARGET_CONFIG_GET "get_target_mode_config" /* returns current target mode configuration */
# define USB_MODE_USER_CONFIG_CLEAR "clear_config" /* clear config for a user */

/**
* (Transient) states reported by "sig_usb_state_ind" that are not modes.
Expand Down
41 changes: 38 additions & 3 deletions src/usb_moded-util.c
Expand Up @@ -49,6 +49,7 @@ static int util_set_hide_mode_config (char *mode);
static int util_set_unhide_mode_config(char *mode);
static int util_get_hiddenlist (void);
static int util_handle_network (char *network);
static int util_clear_user_config (char *uid);

/* ------------------------------------------------------------------------- *
* MAIN
Expand Down Expand Up @@ -369,10 +370,37 @@ static int util_handle_network(char *network)
return 1;
}

static int util_clear_user_config(char *uid)
{
if (!uid) {
fprintf(stderr, "No uid given, try -h for more information\n");
return true;
}
dbus_uint32_t user = atoi(uid);

DBusMessage *req = NULL;
DBusMessage *reply = NULL;
int ret = 1;

printf("Clearing config for user uid %d\n", user);
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_USER_CONFIG_CLEAR)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_UINT32, &user, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_unref(reply);
ret = 0;
}
dbus_message_unref(req);
}

return ret;
}

int main (int argc, char *argv[])
{
int query = 0, network = 0, setmode = 0, config = 0;
int modelist = 0, mode_configured = 0, hide = 0, unhide = 0, hiddenlist = 0;
int modelist = 0, mode_configured = 0, hide = 0, unhide = 0, hiddenlist = 0, clear = 0;
int res = 1, opt, rescue = 0;
char *option = 0;

Expand All @@ -382,7 +410,7 @@ int main (int argc, char *argv[])
exit(1);
}

while ((opt = getopt(argc, argv, "c:dhi:mn:qrs:u:v")) != -1)
while ((opt = getopt(argc, argv, "c:dhi:mn:qrs:u:vU:")) != -1)
{
switch (opt) {
case 'c':
Expand Down Expand Up @@ -420,6 +448,10 @@ int main (int argc, char *argv[])
case 'v':
hiddenlist = 1;
break;
case 'U':
clear = 1;
option = optarg;
break;
case 'h':
default:
fprintf(stderr, "\nUsage: %s -<option> <args>\n\n \
Expand All @@ -434,7 +466,8 @@ int main (int argc, char *argv[])
\t-r turn rescue mode off,\n \
\t-s to set/activate a mode,\n \
\t-u unhide a mode,\n \
\t-v to get the list of hidden modes\n",
\t-v to get the list of hidden modes\n \
\t-U <uid> to clear config for a user\n",
argv[0]);
exit(1);
}
Expand Down Expand Up @@ -471,6 +504,8 @@ int main (int argc, char *argv[])
res = util_set_unhide_mode_config(option);
else if (hiddenlist)
res = util_get_hiddenlist();
else if (clear)
res = util_clear_user_config(option);

/* subfunctions will return 1 if an error occured, print message */
if(res)
Expand Down

0 comments on commit 2e52ff0

Please sign in to comment.