'Permission Denied' Error When Executing Custom Script via LuCI

Hello. I am a novice developer working on building a router using OpenWrt.

I'm trying to add a feature to LuCI that allows users to send logs from the router I'm developing via email. This router will be used on a ship where remote logging methods are challenging due to connectivity issues. Therefore, the idea is to enable logs to be sent directly by email from a location where the user has data access.

To achieve this, I wrote a script to collect logs and send them by email (named log_user_send.sh). I want this script to be executed when a "Send Log.." button is pressed in LuCI.

For this purpose, I modified the www/luci-static/resources/view/system/flash.js file as follows:

handleSendMailConfirm: function (ev) {
    // Execute your custom script instead of the sysupgrade command
    return fs.exec('/etc/log_user_send.sh').then(function (res) {
        if (res.code != 0) {
            // Notification when script execution fails
            ui.addNotification(null, [E('p', _('The script execution failed with code %d').format(res.code)), res.stderr ? E('pre', {}, [res.stderr]) : '']);
            L.raise('Error', 'Script execution failed');
        } else {
            // Notification when script execution succeeds
            ui.addNotification(null, E('p', _('Script executed successfully')));
        }
    }).catch(function (e) {
        // Error handling
        ui.addNotification(null, E('p', e.message));
    }).finally(function () {
        // Reset the button text
        btn.firstChild.data = _('Original button text');
    });
},
.....
o = s.option(form.SectionValue, 'actions', form.NamedSection, 'actions', 'actions', _('Send log mail'), _('In the event of a malfunction, the system automatically sends log files to the manufacturer for diagnostic purposes. Be aware that utilizing this feature could lead to communication fees.')); ss = o.subsection; o = ss.option(form.Button, 'dl_backup', _('Send Email')); o.inputstyle = 'action important'; o.inputtitle = _('Send log...'); o.onclick = L.bind(this.handleSendMailConfirm, this);

The problem I'm encountering is that the script doesn't run due to a "Permission denied" error.

I suspect it's a permission issue. I've already tried:


chmod 755 /sbin/log_user_send.sh
chown root.root /etc/log_user_send.sh

But I'm still facing the same error.

I need guidance on how to resolve this issue. I'm looking forward to your helpful suggestions. Please assist me in solving this problem so that clicking the “Send Log..” button will successfully execute the script.

You didn't need to reinvent the wheel, there is the System/Custom Commands tab in Luci for that.

2 Likes

You need to extend the acl definition in /usr/share/rpcd/acl.d/ to allow invoking /sbin/log_user_send.sh.

Best is to place a new file (call it e.g. allow-log-sending.json) with the following contents:

{
	"allow-log-sending": {
		"description": "Grant execute permission to log_user_send.sh",
		"write": {
			"file": {
				"/sbin/log_user_send.sh": [ "exec" ]
			},
			"ubus": {
				"file": [ "exec" ]
			}
		}
	}
}

Afterwards log out and in again to apply the new ACL, then execution should work.

3 Likes

Thanks to you, I was able to solve this problem. Thank you so much!

Thank you for your answer. Could you provide some more details about the System/Custom Commands tab that might offer some insights? I'll also look into this part. I'm a beginner in this area, so there's a lot I don't know. :worried:

There is not much to tell. You can run a custom script from Luci.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.