6331N/A#!/bin/sh
6331N/A#
6331N/A# $Id: i.rbac 551 2013-04-12 22:28:02Z elkner $
6331N/A#
6331N/A# CDDL HEADER START
6331N/A#
6331N/A# The contents of this file are subject to the terms of the
6331N/A# Common Development and Distribution License (the "License").
6331N/A# You may not use this file except in compliance with the License.
6331N/A#
6331N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6331N/A# or http://www.opensolaris.org/os/licensing.
6331N/A# See the License for the specific language governing permissions
6331N/A# and limitations under the License.
6331N/A#
6331N/A# When distributing Covered Code, include this CDDL HEADER in each
6331N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
6331N/A# If applicable, add the following below this CDDL HEADER, with the
6331N/A# fields enclosed by brackets "[]" replaced with your own identifying
6331N/A# information: Portions Copyright [yyyy] [name of copyright owner]
6331N/A#
6331N/A# CDDL HEADER END
6331N/A#
6331N/A# Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
6331N/A# Portions Copyright 2013 Jens Elkner.
6331N/A#
6331N/A# class action script for "rbac" class files
6331N/A# installed by pkgadd
6331N/A#
6331N/A# Files in "rbac" class:
6331N/A#
6331N/A# /etc/security/{prof_attr,exec_attr,auth_attr}
6331N/A# /etc/user_attr
6331N/A#
6331N/A# Allowable exit codes
6331N/A#
6331N/A# 0 - success
6331N/A# 2 - warning or possible error condition. Installation continues. A warning
6331N/A# message is displayed at the time of completion.
6331N/A#
6331N/A
6331N/Aumask 022
6331N/A
6331N/Atmp_dir=${TMPDIR:-/tmp}
6331N/A
6331N/APATH="/usr/bin:/usr/sbin:${PATH}"
6331N/Aexport PATH
6331N/A
6331N/Abasename_cmd=basename
6331N/Acp_cmd=cp
6331N/Aegrep_cmd=egrep
6331N/Amv_cmd=mv
6331N/Anawk_cmd=nawk
6331N/Arm_cmd=rm
6331N/Ased_cmd=sed
6331N/Asort_cmd=sort
6331N/A
6331N/A# $1 is the type
6331N/A# $2 is the "old/existing file"
6331N/A# $3 is the "new (to be merged)" file
6331N/A# $4 is the output file
6331N/A# returns 0 on success
6331N/A# returns 2 on failure if nawk fails with non-zero exit status
6331N/A#
6331N/Adbmerge() {
6331N/A#
6331N/A# Remove the ident lines.
6331N/A#
6331N/A ${egrep_cmd} -v '^#[pragma ]*ident' $2 > $4.old 2>/dev/null
6331N/A#
6331N/A# If the new file has a Sun copyright, remove the Sun copyright from the old
6331N/A# file.
6331N/A#
6331N/A newcr=`${egrep_cmd} '^# Copyright.*Sun Microsystems, Inc.' $3 \
6331N/A 2>/dev/null`
6331N/A if [ -n "${newcr}" ]; then
6331N/A $sed_cmd -e '/^# Copyright.*Sun Microsystems, Inc./d' \
6331N/A -e '/^# All rights reserved./d' \
6331N/A -e '/^# Use is subject to license terms./d' \
6331N/A $4.old > $4.$$ 2>/dev/null
6331N/A $mv_cmd $4.$$ $4.old
6331N/A fi
6331N/A#
6331N/A# If the new file has an Oracle copyright, remove both the Sun and Oracle
6331N/A# copyrights from the old file.
6331N/A#
6331N/A oracle_cr=`${egrep_cmd} '^# Copyright.*Oracle and/or its affiliates.' \
6331N/A $3 2>/dev/null`
6331N/A if [ -n "${oracle_cr}" ]; then
6331N/A $sed_cmd -e '/^# Copyright.*Sun Microsystems, Inc./d' \
6331N/A -e '/^# All rights reserved./d' \
6331N/A -e '/^# Use is subject to license terms./d' \
6331N/A -e '/^# Copyright.*Oracle and\/or its affiliates./d' \
6331N/A $4.old > $4.$$ 2>/dev/null
6331N/A $mv_cmd $4.$$ $4.old
6331N/A fi
6331N/A#
6331N/A# If the new file has the CDDL, remove it from the old file.
6331N/A#
6331N/A newcr=`${egrep_cmd} '^# CDDL HEADER START' $3 2>/dev/null`
6331N/A if [ -n "${newcr}" ]; then
6331N/A $sed_cmd -e '/^# CDDL HEADER START/,/^# CDDL HEADER END/d' \
6331N/A $4.old > $4.$$ 2>/dev/null
6331N/A $mv_cmd $4.$$ $4.old
6331N/A fi
6331N/A#
6331N/A# Remove empty lines and multiple instances of these comments:
6331N/A#
6331N/A $sed_cmd -e '/^# \/etc\/security\/exec_attr/d' -e '/^#$/d' \
6331N/A -e '/^# execution attributes for profiles./d' \
6331N/A -e '/^# See exec_attr(4)/d' \
6331N/A -e '/^# \/etc\/user_attr/d' \
6331N/A -e '/^# user attributes. see user_attr(4)/d' \
6331N/A -e '/^# \/etc\/security\/prof_attr/d' \
6331N/A -e '/^# profiles attributes. see prof_attr(4)/d' \
6331N/A -e '/^# See prof_attr(4)/d' \
6331N/A -e '/^# \/etc\/security\/auth_attr/d' \
6331N/A -e '/^# authorizations. see auth_attr(4)/d' \
6331N/A -e '/^# authorization attributes. see auth_attr(4)/d' \
6331N/A $4.old > $4.$$
6331N/A $mv_cmd $4.$$ $4.old
6331N/A#
6331N/A# Retain old and new header comments.
6331N/A#
6331N/A $sed_cmd -n -e '/^[^#]/,$d' -e '/^##/,$d' -e p $4.old > $4
6331N/A $rm_cmd $4.old
6331N/A $sed_cmd -n -e '/^[^#]/,$d' -e '/^##/,$d' -e p $3 >> $4
6331N/A#
6331N/A# If the output file now has both Sun and Oracle copyrights, remove
6331N/A# the Sun copyright.
6331N/A#
6331N/A sun_cr=`${egrep_cmd} '^# Copyright.*Sun Microsystems, Inc.' \
6331N/A $4 2>/dev/null`
6331N/A oracle_cr=`${egrep_cmd} '^# Copyright.*Oracle and/or its affiliates.' \
6331N/A $4 2>/dev/null`
6331N/A if [ -n "${sun_cr}" ] && [ -n "${oracle_cr}" ]; then
6331N/A $sed_cmd -e '/^# Copyright.*Sun Microsystems, Inc./d' \
6331N/A -e '/^# All rights reserved./d' \
6331N/A -e '/^# Use is subject to license terms./d' \
6331N/A $4 > $4.$$ 2>/dev/null
6331N/A $mv_cmd $4.$$ $4
6331N/A fi
6331N/A#
6331N/A# Handle line continuations (trailing \)
6331N/A#
6331N/A $sed_cmd \
6331N/A -e '/\\$/{N;s/\\\n//;}' -e '/\\$/{N;s/\\\n//;}' \
6331N/A -e '/\\$/{N;s/\\\n//;}' -e '/\\$/{N;s/\\\n//;}' \
6331N/A -e '/\\$/{N;s/\\\n//;}' -e '/\\$/{N;s/\\\n//;}' \
6331N/A $2 > $4.old
6331N/A $sed_cmd \
6331N/A -e '/\\$/{N;s/\\\n//;}' -e '/\\$/{N;s/\\\n//;}' \
6331N/A -e '/\\$/{N;s/\\\n//;}' -e '/\\$/{N;s/\\\n//;}' \
6331N/A -e '/\\$/{N;s/\\\n//;}' -e '/\\$/{N;s/\\\n//;}' \
6331N/A $3 > $4.new
6331N/A
6331N/A if [ "$1" = "exec" ]; then
6331N/A $sed_cmd -e "s#@CLIENT_BASEDIR@#$CLIENT_BASEDIR#g" $4.new > $4.$$
6331N/A $mv_cmd $4.$$ $4.new
6331N/A fi
6331N/A#
6331N/A# The nawk script below processes the old and new files using up to
6331N/A# three passes. If the old file is empty, only the final pass over
6331N/A# the new file is required.
6331N/A#
6331N/A if [ -s $4.old ]; then
6331N/A nawk_pass1=$4.old
6331N/A nawk_pass2=$4.new
6331N/A nawk_pass3=$4.new
6331N/A else
6331N/A nawk_pass1=
6331N/A nawk_pass2=
6331N/A nawk_pass3=$4.new
6331N/A fi
6331N/A#
6331N/A#!/usr/bin/nawk -f
6331N/A#
6331N/A# dbmerge type=[auth|prof|user|exec] [ old-file new-file ] new-file
6331N/A#
6331N/A# Merge two versions of an RBAC database file. The output
6331N/A# consists of the lines from the new-file, while preserving
6331N/A# user customizations in the old-file.
6331N/A#
6331N/A# Entries in the new-file replace corresponding entries in the
6331N/A# old-file, except as follows: For exec_attr, all old entries
6331N/A# for profiles contained in the new-file are discarded. For
6331N/A# user_attr, the "root" entry from the old-file is retained,
6331N/A# and new keywords from the new-file are merged into it.
6331N/A#
6331N/A# Records with the same key field(s) are merged, so that the
6331N/A# keyword/value section of each output record contains the union
6331N/A# of the keywords found in all input records with the same key
6331N/A# field(s). For selected multi-value keywords [1] the values from
6331N/A# the new-file are merged with retained values from the old-file.
6331N/A# Otherwise, the value for each keyword is the final value found
6331N/A# in the new-file, except for keywords in the user_attr entry for
6331N/A# "root" where values from the old-file are always retained.
6331N/A#
6331N/A# [1] The following file type and keyword combinations are merged:
6331N/A# prof_attr: auths, profiles, privs
6331N/A# user_attr: auths, profiles, roles
6331N/A#
6331N/A# The output is run through sort except for the comments
6331N/A# which will appear first in the output.
6331N/A#
6331N/A#
6331N/A $nawk_cmd '
6331N/A
6331N/A# This script may be invoked with up to three file names. Each file
6331N/A# name corresponds to a separate processing pass. The passes are
6331N/A# defined as follows:
6331N/A#
6331N/A# Pass 1: Read existing data.
6331N/A# Data from the old-file is read into memory.
6331N/A#
6331N/A# Pass 2: Remove obsolete data.
6331N/A# Discard any data from the old-file that is part of profiles that
6331N/A# are also in the new-file. (As a special case, the user_attr entry
6331N/A# for 'root' is always retained.)
6331N/A#
6331N/A# Pass 3: Merge new data.
6331N/A# Data from the new-file is merged with the remaining old-file data.
6331N/A# (As a special case, exec_attr entries are replaced, not merged.)
6331N/A
6331N/ABEGIN {
6331N/A # The variable 'pass' specifies which type of processing to perform.
6331N/A # When processing only one file, skip passes 1 and 2.
6331N/A if (ARGC == 3)
6331N/A pass += 2;
6331N/A
6331N/A # The array 'keyword_behavior' specifies the special treatment of
6331N/A # [type, keyword] combinations subject to value merging.
6331N/A keyword_behavior["prof", "auths"] = "merge";
6331N/A keyword_behavior["prof", "profiles"] = "merge";
6331N/A keyword_behavior["prof", "privs"] = "merge";
6331N/A keyword_behavior["user", "auths"] = "merge";
6331N/A keyword_behavior["user", "profiles"] = "merge";
6331N/A keyword_behavior["user", "roles"] = "merge";
6331N/A
6331N/A FS=":"
6331N/A}
6331N/A
6331N/A# When FNR (current file record number) is 1 it indicates that nawk
6331N/A# is starting to read the next file specified on its command line,
6331N/A# and is beginning the next processing pass.
6331N/AFNR == 1 {
6331N/A pass++;
6331N/A}
6331N/A
6331N/A/^#/ || /^$/ {
6331N/A continue;
6331N/A}
6331N/A
6331N/A{
6331N/A # For each input line, nawk automatically assigns the complete
6331N/A # line to $0 and also splits the line at field separators and
6331N/A # assigns each field to a variable $1..$n. Assignment to $0
6331N/A # re-splits the line into the field variables. Conversely,
6331N/A # assgnment to a variable $1..$n will cause $0 to be recomputed
6331N/A # from the field variable values.
6331N/A #
6331N/A # This code adds awareness of escaped field separators by using
6331N/A # a custom function to split the line into a temporary array.
6331N/A # It assigns the empty string to $0 to clear any excess field
6331N/A # variables, and assigns the desired elements of the temporary
6331N/A # array back to the field variables $1..$7.
6331N/A #
6331N/A # Subsequent code must not assign directly to $0 or the fields
6331N/A # will be re-split without regard to escaped field separators.
6331N/A split_escape($0, f, ":");
6331N/A $0 = "";
6331N/A $1 = f[1];
6331N/A $2 = f[2];
6331N/A $3 = f[3];
6331N/A $4 = f[4];
6331N/A $5 = f[5];
6331N/A $6 = f[6];
6331N/A $7 = f[7];
6331N/A}
6331N/A
6331N/Atype == "auth" {
6331N/A key = $1 ":" $2 ":" $3 ;
6331N/A if (pass == 1) {
6331N/A short_comment[key] = $4 ;
6331N/A long_comment[key] = $5;
6331N/A record[key] = $6;
6331N/A } else if (pass == 2) {
6331N/A delete short_comment[key];
6331N/A delete long_comment[key];
6331N/A delete record[key];
6331N/A } else if (pass == 3) {
6331N/A if ( $4 != "" ) {
6331N/A short_comment[key] = $4 ;
6331N/A }
6331N/A if ( $5 != "" ) {
6331N/A long_comment[key] = $5 ;
6331N/A }
6331N/A record[key] = merge_attrs(record[key], $6);
6331N/A }
6331N/A}
6331N/A
6331N/Atype == "prof" {
6331N/A key = $1 ":" $2 ":" $3 ;
6331N/A if (pass == 1) {
6331N/A comment[key] = $4;
6331N/A record[key] = $5;
6331N/A } else if (pass == 2) {
6331N/A delete comment[key];
6331N/A delete record[key];
6331N/A } else if (pass == 3) {
6331N/A if ( $4 != "" ) {
6331N/A comment[key] = $4 ;
6331N/A }
6331N/A if (key != "::") {
6331N/A record[key] = merge_attrs(record[key], $5);
6331N/A }
6331N/A }
6331N/A}
6331N/A
6331N/Atype == "exec" {
6331N/A key = $1 ":" $2 ":" $3 ":" $4 ":" $5 ":" $6 ;
6331N/A if (pass == 1) {
6331N/A record[key] = $7;
6331N/A } else if (pass == 2) {
6331N/A # For exec_attr, deletion is based on the 'name' field only,
6331N/A # so that all old entries for the profile are removed.
6331N/A for (oldkey in record) {
6331N/A split_escape(oldkey, oldkey_fields, ":");
6331N/A if (oldkey_fields[1] == $1)
6331N/A delete record[oldkey];
6331N/A }
6331N/A } else if (pass == 3) {
6331N/A # Substitute new entries, do not merge.
6331N/A record[key] = $7;
6331N/A }
6331N/A}
6331N/A
6331N/Atype == "user" {
6331N/A key = $1 ":" $2 ":" $3 ":" $4 ;
6331N/A if (pass == 1) {
6331N/A record[key] = $5;
6331N/A } else if (pass == 2) {
6331N/A if ($1 != "root")
6331N/A delete record[key];
6331N/A } else if (pass == 3) {
6331N/A record[key] = merge_attrs(record[key], $5);
6331N/A }
6331N/A}
6331N/A
6331N/AEND {
6331N/A for (key in record) {
6331N/A if (type == "prof") {
6331N/A if (key != "::") {
6331N/A print key ":" comment[key] ":" record[key];
6331N/A }
6331N/A } else
6331N/A if (type == "auth") {
6331N/A print key ":" short_comment[key] ":" \
6331N/A long_comment[key] ":" record[key];
6331N/A } else
6331N/A print key ":" record[key];
6331N/A }
6331N/A}
6331N/A
6331N/Afunction merge_attrs(old, new, cnt, new_cnt, i, j, list, new_list, keyword)
6331N/A{
6331N/A cnt = split_escape(old, list, ";");
6331N/A new_cnt = split_escape(new, new_list, ";");
6331N/A for (i = 1; i <= new_cnt; i++) {
6331N/A keyword = substr(new_list[i], 1, index(new_list[i], "=")-1);
6331N/A for (j = 1; j <= cnt; j++) {
6331N/A if (match(list[j], "^" keyword "=")) {
6331N/A list[j] = merge_values(keyword, list[j],
6331N/A new_list[i]);
6331N/A break;
6331N/A }
6331N/A }
6331N/A if (j > cnt)
6331N/A list[++cnt] = new_list[i];
6331N/A }
6331N/A
6331N/A return unsplit(list, cnt, ";"); \
6331N/A}
6331N/A
6331N/Afunction merge_values(keyword, old, new, cnt, new_cnt, i, j, list, new_list, d)
6331N/A{
6331N/A # Keywords with multivalued attributes that are subject to merging
6331N/A # are processed by the algorithm implemented further below.
6331N/A # Otherwise, the keyword is not subject to merging, and:
6331N/A # For user_attr, the existing value is retained.
6331N/A # For any other file, the new value is substituted.
6331N/A if (keyword_behavior[type, keyword] != "merge") {
6331N/A if (type == "user") {
6331N/A return old;
6331N/A } else {
6331N/A return new;
6331N/A }
6331N/A }
6331N/A
6331N/A cnt = split(substr(old, length(keyword)+2), list, ",");
6331N/A new_cnt = split(substr(new, length(keyword)+2), new_list, ",");
6331N/A
6331N/A # If the existing list contains "All", remove it and add it
6331N/A # to the new list; that way "All" will appear at the only valid
6331N/A # location, the end of the list.
6331N/A if (keyword == "profiles") {
6331N/A d = 0;
6331N/A for (i = 1; i <= cnt; i++) {
6331N/A if (list[i] != "All")
6331N/A list[++d] = list[i];
6331N/A }
6331N/A if (cnt != d) {
6331N/A new_list[++new_cnt] = "All";
6331N/A cnt = d;
6331N/A }
6331N/A }
6331N/A for (i = 1; i <= new_cnt; i++) {
6331N/A for (j = 1; j <= cnt; j++) {
6331N/A if (list[j] == new_list[i])
6331N/A break;
6331N/A }
6331N/A if (j > cnt)
6331N/A list[++cnt] = new_list[i];
6331N/A }
6331N/A
6331N/A return keyword "=" unsplit(list, cnt, ",");
6331N/A}
6331N/A
6331N/A# This function is similar to the nawk built-in split() function,
6331N/A# except that a "\" character may be used to escape any subsequent
6331N/A# character, so that the escaped character will not be treated as a
6331N/A# field separator or as part of a field separator regular expression.
6331N/A# The "\" characters will remain in the elements of the output array
6331N/A# variable upon completion.
6331N/Afunction split_escape(str, list, fs, cnt, saved, sep)
6331N/A{
6331N/A # default to global FS
6331N/A if (fs == "")
6331N/A fs = FS;
6331N/A # initialize empty list, cnt, saved
6331N/A split("", list, " ");
6331N/A cnt = 0;
6331N/A saved = "";
6331N/A # track whether last token was a field separator
6331N/A sep = 0;
6331N/A # nonzero str length indicates more string left to scan
6331N/A while (length(str)) {
6331N/A if (match(str, fs) == 1) {
6331N/A # field separator, terminates current field
6331N/A list[++cnt] = saved;
6331N/A saved = "";
6331N/A str = substr(str, RLENGTH + 1);
6331N/A sep = 1;
6331N/A } else if (substr(str, 1, 1) == "\\") {
6331N/A # escaped character
6331N/A saved = saved substr(str, 1, 2);
6331N/A str = substr(str, 3);
6331N/A sep = 0;
6331N/A } else {
6331N/A # regular character
6331N/A saved = saved substr(str, 1, 1);
6331N/A str = substr(str, 2);
6331N/A sep = 0;
6331N/A }
6331N/A }
6331N/A # if required, append final field to list
6331N/A if (sep || length(saved))
6331N/A list[++cnt] = saved;
6331N/A
6331N/A return cnt;
6331N/A}
6331N/A
6331N/Afunction unsplit(list, cnt, delim, str)
6331N/A{
6331N/A str = list[1];
6331N/A for (i = 2; i <= cnt; i++)
6331N/A str = str delim list[i];
6331N/A return str;
6331N/A}' \
6331N/A type=$1 $nawk_pass1 $nawk_pass2 $nawk_pass3 > $4.unsorted
6331N/A rc=$?
6331N/A $sort_cmd < $4.unsorted >> $4
6331N/A return $rc
6331N/A}
6331N/A
6331N/A# $1 is the merged file
6331N/A# $2 is the target file
6331N/A#
6331N/Acommit() {
6331N/A # Make sure that the last mv uses rename(2) by first moving to
6331N/A # the same filesystem.
6331N/A $mv_cmd $1 $2.$$
6331N/A $mv_cmd $2.$$ $2
6331N/A return $?
6331N/A}
6331N/A
6331N/Aoutfile=""
6331N/Atype=""
6331N/Aset_type_and_outfile() {
6331N/A #
6331N/A # Assumes basename $1 returns one of
6331N/A # prof_attr, exec_attr, auth_attr, or user_attr
6331N/A #
6331N/A fname=`$basename_cmd $1`
6331N/A type=`echo $fname | $sed_cmd -e s'/^\([a-z][a-z]*\)_attr$/\1/' `
6331N/A case "$type" in
6331N/A "prof"|"exec"|"user"|"auth") ;;
6331N/A *) return 2 ;;
6331N/A esac
6331N/A
6331N/A outfile=$tmp_dir/rbac_${PKGINST}_${fname}_merge.$$
6331N/A
6331N/A return 0
6331N/A}
6331N/A
6331N/Acleanup() {
6331N/A $rm_cmd -f $outfile $outfile.old $outfile.new $outfile.unsorted
6331N/A
6331N/A return 0
6331N/A}
6331N/A
6331N/Aexit_status=0
6331N/A
6331N/A# main
6331N/A
6331N/Awhile read src dst ; do
6331N/A if [ ! -f $dst ]; then
6331N/A sed -e "s#@CLIENT_BASEDIR@#$CLIENT_BASEDIR#g" $src > $dst
6331N/A else
6331N/A set_type_and_outfile $dst
6331N/A if [ $? -ne 0 ]; then
6331N/A echo "$0 : $src not one of {prof,exec,auth,user}_attr"
6331N/A exit_status=2
6331N/A continue
6331N/A fi
6331N/A
6331N/A dbmerge $type $dst $src $outfile
6331N/A if [ $? -ne 0 ]; then
6331N/A echo "$0 : failed to merge $src with $dst"
6331N/A cleanup
6331N/A exit_status=2
6331N/A continue
6331N/A fi
6331N/A
6331N/A commit $outfile $dst
6331N/A if [ $? -ne 0 ]; then
6331N/A echo "$0 : failed to mv $outfile to $2"
6331N/A cleanup
6331N/A exit_status=2
6331N/A continue
6331N/A fi
6331N/A
6331N/A cleanup
6331N/A fi
6331N/Adone
6331N/A
6331N/Aif [ "$1" = "ENDOFCLASS" ]; then
6331N/A exit 0
6331N/Afi
6331N/A
6331N/Aexit $exit_status