Commit Graph

79 Commits (4a6795409d1520fd3da3e909a8bcf9d7fd0927bb)

Author SHA1 Message Date
Jo-Philipp Wich 4a6795409d base-files: functions.sh: fix config_get() on invalid identifiers
When passing a section or option value to config_get() which contains
characters that happen to be valid variable interpolation expressions,
the function returns a nonsensical expression result instead of the
expected empty string.

When the passed section or option name contains other characters which
are not valid within a shell variable name, a substitution error is
occuring instead.

The issue can be easily reproduced by one of the following examples:

    root@OpenWrt:~# . /lib/functions.sh
    root@OpenWrt:~# config load system
    root@OpenWrt:~# config_get variable invalid-section option
    root@OpenWrt:~# echo "$variable"
    section_option:-

    root@OpenWrt:~# . /lib/functions.sh
    root@OpenWrt:~# config load system
    root@OpenWrt:~# config_get variable section invalid-option
    root@OpenWrt:~# echo "$variable"
    option:-

    root@OpenWrt:~# . /lib/functions.sh
    root@OpenWrt:~# config load system
    root@OpenWrt:~# config_get variable section invalid@option
    -ash: eval: syntax error: bad substitution

Fix this issue by only performing interpolations when the given section
and option arguments are free of illegal characters.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
4 years ago
Rosen Penev 8e6f2c029f base-files/functions.sh: use command -v instead of which
which must be executed. command -v is a shell builtin.

https://github.com/koalaman/shellcheck/wiki/SC2230

Signed-off-by: Rosen Penev <rosenp@gmail.com>
4 years ago
Adrian Schmutzler 282e817350 base-files: do not source system.sh in functions.sh
The default_postinst() function in /lib/functions.sh sources
/lib/functions/system.sh before cycling through uci-defaults files.

This creates a pseudo-cyclic dependency as system.sh also uses
functions that are located in functions.sh. Despite that, there
is actually only one uci-defaults file in the entire repo that needs
system.sh, and this one contains an explicit source for system.sh
anyway.

Consequently, this patch removes the sourcing of system.sh in
functions.sh. There are no relevant uses in packages, routing and
luci repositories.
This may require adjustments for downstream, though.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
4 years ago
Adrian Schmutzler 5c2e409be7 base-files: convert leading spaces to tabs in functions.sh
Use tabs for indent consistently.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
4 years ago
Rosen Penev 8df14c229c base-files/functions.sh: use grep -q instead of []
It's cleaner and faster as it does not need to do extra work.

Also removed $() to avoid executing the output. The shell can handle it.

https://github.com/koalaman/shellcheck/wiki/SC2143

Signed-off-by: Rosen Penev <rosenp@gmail.com>
[correct || to && for one conversion]
Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
4 years ago
Rosen Penev fb56573dc4 base-files/functions.sh: use && instead of -a
-a is not well defined.

https://github.com/koalaman/shellcheck/wiki/SC2166

Signed-off-by: Rosen Penev <rosenp@gmail.com>
4 years ago
Rosen Penev b8e17aefea base-files/functions.sh: remove useless cat
The cut command can take a file as an input.

https://github.com/koalaman/shellcheck/wiki/SC2002

Signed-off-by: Rosen Penev <rosenp@gmail.com>
4 years ago
Rosen Penev cba5fa0352 base-files/functions.sh: don't use $var in $(())
It's not needed. It can also lead to subtle bugs.

https://github.com/koalaman/shellcheck/wiki/Sc2004

Signed-off-by: Rosen Penev <rosenp@gmail.com>
4 years ago
Adrian Schmutzler c1388a2deb base-files: move xor() from caldata extraction to functions.sh
The xor() function is defined in each of the caldata extraction
scripts for several targets. Move it to functions.sh to reduce
duplicate code.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
5 years ago
Yousong Zhou 3dc4f59eab base-files: apply new sysctl.conf at postinst
This is mainly for kmod-br-netfilter.  To turn off
bridge-netfilter-call-xxx immediately after installation

While at it

 - Define filelist="/usr/lib/opkg/info/${pkgname}.list"
 - Reuse "[ -z "$root" ]"
 - Grep with "-m1"

Fixes FS#2300

Reported-by: Marco Sartorius <tidbits@ormoorgmen.info>
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
5 years ago
Günther Kelleter c3389ab135 base-files: config_get: prevent filename globbing
When config_get is called as "config_get section option" the option
is unexpectedly globbed by the shell which differs from the way options
are read to a variable with "config_get variable section option".
Add another layer of double quotes to fix it.

Signed-off-by: Günther Kelleter <guenther.kelleter@devolo.de>
5 years ago
Tony Ambardar 8806da86f5 base-files: fix prerm return value, align with postinst code
The return value of a package prerm script is discarded and not returned
correctly by default_prerm(). This allows other operations like service
shutdown to "leak" their return value, prompting workarounds like commit
48cfc826 which do not address the root cause.

Preserve a package prerm script return value for use by default_prerm(),
sharing the corresponding code from default_postinst() for consistency.
Also use consistent code for handling of /etc/init.d/ scripts.

Run Tested on: LEDE 17.01.4 running ar71xx.

Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
6 years ago
Tony Ambardar 4097ab6a97 base-files: fix postinstall uci-defaults removal
Commit 7f694582 introduced a bug where default_postinst() often fails to
remove a uci-defaults script after application, leaving it to run again
after a reboot.
(Note: commit 7f694582 also introduced FS#1021, now fixed by 73c745f6)

The subtle problem arises from the shell logical chain:
[ -f "$i" ] && . "$i" && rm -f "$i"

Most uci-defaults scripts contain a terminal 'exit 0' statement which,
when sourced, results in the logic chain exiting before executing 'rm -f'.
This was observed while testing upgrades of 'luci-app-sqm'.

The solution is to wrap the shell sourcing in a subshell relative to the
command 'rm -f':
( [ -f "$i" ] && . "$i" ) && rm -f "$i"

Revert to using 'grep' to prefilter the list of entries from the control
file, which yields the full path of uci-defaults scripts. This allows
keeping the existence check, directory change and script sourcing inside
the subshell, with the script removal correctly outside.

This approach avoids adding a second subshell only around the "." (source)
command. The change also preserves the fix FS#1021, since the full path is
used to source the script, which is POSIX-portable irrespective of PATH
variable or reference to the CWD.

Run Tested on: LEDE 17.01.4 running ar71xx, while tracing installation of
package luci-app-sqm with its associated /etc/uci-defaults/luci-sqm file.

Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
6 years ago
Tony Ambardar 73d8a6ab76 base-files: fix UCI config parsing and callback handling
There are several long-standing issues present in the UCI shell API as
documented in https://wiki.openwrt.org/doc/devel/config-scripting. They
relate both to high-level, user-defined callback functions used to
process UCI config files, and also to low-level functions used within
scripts generally.

The related problems have been encountered now and in the past, e.g.
https://forum.openwrt.org/viewtopic.php?id=54295, and include:

a) UCI parsing option() function and user-defined option_cb() callbacks
being erroneously called during processing of "list" config file entries;

b) normal usage of the low-level config_set() unexpectedy calling any
defined option_cb() if present; and

c) handling of the list_cb() not respecting the NO_CALLBACK variable.

Root causes include a function stack "inversion", where the low-level
config_set() function incorrectly calls the high-level option() function,
intended only for processing the "option" keyword of UCI config files.

This change addresses the inversion and other issues, making the option
handling code more consistent and smaller, and simplifying developers'
usage of UCI callbacks.

Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com> [PKG_RELEASE increase]
6 years ago
Matthias Schiffer 14c78a7dfc
base-files: /lib/functions.sh: remove unused insert_modules
insert_modules has been unused since r5279.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
6 years ago
Yousong Zhou b2aa820b48 base-files: fix getting gid from group_add_next
Shell function return code only has range [0, 255].  Other values will
be truncated, e.g. return 65536 will have the same effect as return 0

While at it, drop other "return $rc" where rc will almost always take
value 0 and whose value current callers actually do not check

Fixes FS#988

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
7 years ago
Marko Ratkaj 73c745f64f functions.sh: fix default_postinst function
When we run "opkg install" on a package that installs an uci-defaults
script, functions.sh will fail to evaluate that script in its
default_postinst function.

This happens because there is no "./" present and it searches for the
file in paths specified by the PATH variable. This would work on bash,
but it will not work on ash and some other shells like sh, zsh. This
applys to the ". filename" directive used in this case.

This patch will make the path relative to the /etc/uci-defaults
directory.

Fixes: FS#1021

Signed-off-by: Marko Ratkaj <marko.ratkaj@sartura.hr>
7 years ago
Jo-Philipp Wich 2b6facc8d4 include: kernel.mk: simplify module autoloading
Let the generic postinstall script invoke "kmodloader" when the just
installed package contains any /etc/module.d/ entries.

This allows us to skip the explicit "insert_module()" calls in the
package postinstall.

Due to the removed insert_module calls we do not need to assemble a
complete list of modules per package anymore, which allows for vast
simplification of the package generation code.

While we're at it, also support specifying default parameters for
modules using either the MODPARAM or MODPARAM.modulename variables
in KernelPackage.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years ago
Yousong Zhou f334a0cdb8 base-files: allocate uid/gid starting from 65536
There already exist static assignment of uid/gid 65533 in packages feed
and we have nobody/nogroup taking 65534 as their ids.  Let's change the
pid of dynamic assignment to start from 65536 so that the two assignment
scheme will not collide with each other

While at it, fix the scan command checking existence of uid/gid

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
7 years ago
Felix Fietkau ec99142474 base-files: add generic board_name function to functions.sh
This will be used to replace all those nasty board specific scripts
that do basically the same thing

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years ago
Rafał Miłecki b522292405 base-files: add support for overlaying rootfs content
This adds support for install-overlay define. When used in package it
allows installing files to a special directory that gets copied to the
root when installing it.
It allows overwriting files provided by other packages.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
7 years ago
Jonas Gorski 48cfc826eb base-files: ignore failure of stopping services on removal
Packages that do a killall <cmd> with the same name as the init script
will fail the prerm step when the service isn't running. Do make them
removable without having to restart the service, ignore the return code.

Signed-off-by: Jonas Gorski <jonas.gorski@gmail.com>
Acked-by: Jo-Philipp Wich <jo@mein.io>
8 years ago
Jonas Gorski afaa34ccd7 base-files: don't modify enabled state of service on upgrade
Properly stop/start services on upgrade, but don't change the enabled
state.

Signed-off-by: Jonas Gorski <jonas.gorski@gmail.com>
Acked-by: Jo-Philipp Wich <jo@mein.io>
8 years ago
Matthias Schiffer 26b4216f95
base-files: make default_prerm work offline
IPKG_INSTROOT must be respected for offline removal (used for per-device
rootfs).

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
8 years ago
Jo-Philipp Wich 7f69458296 base-files: rework postinstall uci-defaults handling
Some package postinstall operations, e.g. those emitted by the LuCI build
system, source and delete the uci-defaults themselves upon package insteall,
causing the generic defaults apply code to trigger shell errors like:

    .../luci-app-ddns.postinst: .: line 130: can't open './40_luci-ddns'

Rework the generic apply code to check the existence of the uci-defaults
script before trying to source it, use sed to prefilter the list of entries
from the control file and perform the directory change in a subshell,
avoiding the need for cd $OLDPWD.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years ago
Daniel Golle 9b9c78e071 base-files: evaluate uci-defaults on run-time installations
Packages may install scripts into /etc/uci-defaults to be executed once
after installation, usually at the first boot of the target. This works
fine if the package was installed to the rootfs during build or using
the ImageBuilder.
If the package is installed using opkg during run-time uci-defaults
were applied only after a reboot of the device. Avoid the need to
reboot by evaluting the package's uci-defaults in default-postinst.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
8 years ago
John Crispin ed07ef1601 base-files: split user/group addition code into a function
Signed-off-by: John Crispin <john@phrozen.org>
8 years ago
Jo-Philipp Wich 33b3594332 base-files: refactor default_postinst()
- Simplify user spec parsing code
 - Alias IPKG_INSTROOT to shorten code
 - Quote variables where appropriate
 - Remove a useless-use-of-cat instance
 - Suppress errors in case control or list files are missing

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>

SVN-Revision: 46975
9 years ago
John Crispin e83643d31c base-files: default_postinst: propagate the real postinst return code
Using the postinst script for sanity checks and expecting opkg to fail
if the postinst didn't return 0 was possible in Barrier Breaker, propagate
the real postinst return code through default_postinst to restore this
behaviour.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>

SVN-Revision: 46653
9 years ago
Jonas Gorski d0af85c9a5 base-files: fix default pre-/postrm for packages with a dot
Instead of stripping everything after the first dot, strip everything
after the last dot.
This fixes pre-/postrm actions for packages with a dot in their name,
like libusb-1.0.

Fixes #19668.

Signed-off-by: Jonas Gorski <jogo@openwrt.org>

SVN-Revision: 45702
9 years ago
John Crispin bbad3f360e modules: fix postinst generation for kernel modules
Fixes ticket #19352.

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 45367
9 years ago
John Crispin 5cd8a482f3 base-files: default_postinst() fix variable colision
The name variable contains at first the package name,
and after the last group name.
This patch fixes /etc/rc.d symlink creation (at least).

Change name in pkgname, change the other name in ugname
(user group name), and id in ugid (user group id)

Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>

SVN-Revision: 44253
9 years ago
John Crispin b0d2a9e77a base-files: default_postinst() - create user:group first (resend)
create user:group before running postinst-pkg. the postinst hook might require
the user:group to already exist.

Signed-off-by: Christian Schoenebeck <christian.schoenebeck@gmail.com>

SVN-Revision: 43472
10 years ago
John Crispin 666659fb31 default_postinst() fix wrong call of group_add
[base-files] default_postinst() fix wrong call of group_add
after 12bae65d07d29854204715cebc1ef1eae237fd9b group_add is
always called. shell scripting can be tricky...

wrong call:
[ -n "$gid" ] && group_exists $name || group_add $name $gid

this leads to a call of 'group_add' when $gid is emtpy, proof:

root@box:~ functionA() { echo A; }
root@box:~ functionB() { echo B; }
root@box:~ VAR=
root@box:~ [ -n "$VAR" ] && functionA || functionB
B
root@box:~ # functionB was called, but VAR is not filled
root@box:~ VAR=filled
root@box:~ [ -n "$VAR" ] && functionA || functionB
A

Signed-off-by: Bastian Bittorf <bittorf@bluebottle.com>

SVN-Revision: 42926
10 years ago
John Crispin d099a34980 base-files: default_postinst() force clear luci-indexcache
Inside every LuCI package you need to clear luci-indexcache and
sometimes when installing non LuCI pacakges it's also needed to clear
it. Easier put it into default_postinst().

Signed-off-by: Christian Schoenebeck <christian.schoenebeck@gmail.com>

SVN-Revision: 42923
10 years ago
John Crispin 5efe2e5e3c base-files: the USERID code path did not check for duplicate groups
Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 42919
10 years ago
John Crispin a3bf98495a base-files: extend USERID:=syntax
it is now possible to inlie the uid and gid in the syntax

USERID:=username=uid:group=gid:group2=gid2:...

Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 42838
10 years ago
John Crispin f9cccd2c14 base-files: make postinst script run inside a subshell
Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 42800
10 years ago
John Crispin 44249f71a8 postinst trigger: the new postinst trigger broke IB
Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 42571
10 years ago
John Crispin d6dbc22bbc ipkg: add a default postinst/prerm script
the postinst script enables/starts the init.d scripts upon package installation
and installs the users required by the package.

the prerm script stops and disables the init.d scripts.

Signed-off-by: John Crispin <blogic@openwrt.org>

SVN-Revision: 42470
10 years ago
Luka Perkov be2a057787 base-files: define yes/no as valid boolean options
ubox validate_data defines yes/no as valid boolean options, do the same in
config_get_bool too.

Signed-off-by: Luka Perkov <luka@openwrt.org>

SVN-Revision: 41405
10 years ago
Felix Fietkau dd58e15b84 base-files: add back missing function for find_mtd_part to /lib/functions.sh (fixes #15496)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40426
10 years ago
Felix Fietkau 28f4f4a6d7 /lib/functions.sh: remove config_rename()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40412
10 years ago
Felix Fietkau 4a9ea80ef0 /lib/functions.sh: move rarely used mtd and macaddr related functions to /lib/functions/system.sh
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40411
10 years ago
Felix Fietkau e700dcaf04 /lib/functions.sh: remove jffs2_mark_erase()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40410
10 years ago
Felix Fietkau d368cc63fe base-files: remove config_clear() and list_remove()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40409
10 years ago
Felix Fietkau 3d275a8deb base-files: move uci_apply_defaults() to /etc/init.d/boot
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40408
10 years ago
Felix Fietkau 4a6ad541d8 /lib/functions.sh: remove hotplug_dev()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40406
10 years ago
Felix Fietkau ba2b6f661b /lib/functions.sh: remove strtok()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40405
10 years ago
Felix Fietkau c27c5b9cbc /lib/functions.sh: move preinit related functions to /lib/functions/preinit.sh
Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40404
10 years ago