scripts: ipkg-build: simplify uid/gid resolving
Use the prepared .packageusergroup file to lookup user and group names when processing the passed file mode. Also replace the various subshell/cut invocations with a sequence of standard variable interpolations which fixes paths with embedded colons as a side-effect. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
		| @@ -69,37 +69,23 @@ pkg_appears_sane() { | |||||||
| } | } | ||||||
|  |  | ||||||
| resolve_file_mode_id() { | resolve_file_mode_id() { | ||||||
| 	type="$1" | 	local var=$1 type=$2 name=$3 id | ||||||
| 	name="$2" |  | ||||||
| 	position=1 |  | ||||||
| 	if [ "$type" = "group" ]; then |  | ||||||
| 		position=2 |  | ||||||
| 	fi |  | ||||||
|  |  | ||||||
| 	# root is always 0 | 	case "$name" in | ||||||
| 	if [ "$name" = "root" ]; then | 		root) | ||||||
| 		echo 0 | 			id=0 | ||||||
| 		exit 0 | 		;; | ||||||
| 	fi | 		*[!0-9]*) | ||||||
|  | 			id=$(sed -ne "s#^$type $name \\([0-9]\\+\\)\\b.*\$#\\1#p" "$TOPDIR/tmp/.packageusergroup" 2>/dev/null) | ||||||
|  | 		;; | ||||||
|  | 		*) | ||||||
|  | 			id=$name | ||||||
|  | 		;; | ||||||
|  | 	esac | ||||||
|  |  | ||||||
| 	# return numeric names | 	export "$var=$id" | ||||||
| 	if [ "$name" -eq "$name" 2>/dev/null ]; then |  | ||||||
| 		echo "$name" |  | ||||||
| 		exit 0 |  | ||||||
| 	fi |  | ||||||
|  |  | ||||||
| 	ids=$(grep "$name" "$TOPDIR/tmp/userids") | 	[ -n "$id" ] | ||||||
| 	for id in $ids; do |  | ||||||
| 		resolved_name=$(echo "$id" | cut -d ":" -f "$position" | cut -d "=" -f 1) |  | ||||||
| 		resolved_id=$(echo "$id" | cut -d ":" -f "$position" | cut -d "=" -f 2) |  | ||||||
| 		if [ "$resolved_name" = "$name" ]; then |  | ||||||
| 			echo "$resolved_id" |  | ||||||
| 			exit 0 |  | ||||||
| 		fi |  | ||||||
| 	done |  | ||||||
|  |  | ||||||
| 	>&2 echo "No $type ID found for $name" |  | ||||||
| 	exit 1 |  | ||||||
| } | } | ||||||
|  |  | ||||||
| ### | ### | ||||||
| @@ -175,13 +161,20 @@ for file_mode in $file_modes; do | |||||||
| 	    exit 1 | 	    exit 1 | ||||||
| 	    ;; | 	    ;; | ||||||
| 	esac | 	esac | ||||||
| 	path=$(echo "$file_mode" | cut -d ':' -f 1) |  | ||||||
| 	user=$(echo "$file_mode" | cut -d ':' -f 2) |  | ||||||
| 	group=$(echo "$file_mode" | cut -d ':' -f 3) |  | ||||||
| 	mode=$(echo "$file_mode" | cut -d ':' -f 4) |  | ||||||
|  |  | ||||||
| 	uid=$(resolve_file_mode_id user "$user") | 	mode=${file_mode##*:}; path=${file_mode%:*} | ||||||
| 	gid=$(resolve_file_mode_id group "$group") | 	group=${path##*:};     path=${path%:*} | ||||||
|  | 	user=${path##*:};      path=${path%:*} | ||||||
|  |  | ||||||
|  | 	if ! resolve_file_mode_id uid user "$user"; then | ||||||
|  | 		echo "ERROR: unable to resolve uid of $user" >&2 | ||||||
|  | 		exit 1 | ||||||
|  | 	fi | ||||||
|  |  | ||||||
|  | 	if ! resolve_file_mode_id gid group "$group"; then | ||||||
|  | 		echo "ERROR: unable to resolve gid of $group" >&2 | ||||||
|  | 		exit 1 | ||||||
|  | 	fi | ||||||
|  |  | ||||||
| 	chown "$uid:$gid" "$pkg_dir/$path" | 	chown "$uid:$gid" "$pkg_dir/$path" | ||||||
| 	chmod  "$mode" "$pkg_dir/$path" | 	chmod  "$mode" "$pkg_dir/$path" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jo-Philipp Wich
					Jo-Philipp Wich