#!/bin/sh # chgroup v1.2 (c) 6.9.2000 by Andreas Ley (u) 12.4.2010 # Add a group or replace an existing or all groups in /etc/group group="/etc/group" usage() { echo "Usage: `basename $0` [options] input-mode" >&2 echo "or: `basename $0` [options] [option value] [...] group [...]" >&2 echo "Options:" >&2 echo "-A Add group" >&2 echo "-R Remove group" >&2 echo "-N Rebuild group file (will always keep groups with gid < 2000 and" >&2 echo " replace all groups with gid >= 2000)" >&2 echo "-F group Use alternate group file" >&2 echo "Valid input modes:" >&2 echo "-E entry group(4) entry" >&2 echo "-G Group mode, read group(4) lines from stdin" >&2 echo "Currently there are no valid option value pairs... will be implemented on need" >&2 echo "Will always keep groups with gid < 2000 and replace all groups with gid >= 2000" >&2 exit 1 } echo "\n" | grep "n" >/dev/null && e="-e" set -- `getopt ARNF:E:Ghx $*` || usage trace=false while :; do case $1 in -h) sed '1d;s/^# *//;/^$/q' $0; usage;; -x) set -x; trace=true; shift;; -A) test -n "${remove}${rebuild}" && usage; add=true; shift;; -R) test -n "${add}${rebuild}" && usage; remove=true; shift;; -N) test -n "${add}${remove}" && usage; rebuild=true; shift;; -F) group="$2"; shift 2;; -E) test -n "${stdin}" && usage; entries="${entries}${entries:+\n}$2"; shift 2;; -G) test -n "${entries}" && usage; stdin=true; shift;; --) shift; break;; esac done test -z "${add}${remove}${rebuild}" \ -o -n "${add}${rebuild}" -a -z "${entries}${stdin}" \ -o $# -eq 0 -a -z "${entries}${stdin}" \ -o $# -ne 0 -a -n "${rebuild}${entries}${stdin}" && usage tmp="`/usr/machine/bin/mktemp -c -d \`dirname ${group}\` -p .\`basename ${group}\`.`" # FIXME: Debug only, w/ race conditon - remove after test! cp -p ${group} /tmp/group.`date '+%Y-%m-%dT%H:%M:%S'` if test -n "${remove}"; then if test -n "${entries}"; then echo ${e} "${entries}" | cut -d: -f1 elif test -n "${stdin}"; then cut -d: -f1 else echo "$@" fi | while read name; do if grep -E "^${name}:[^:]*:([2-9][0-9]{3,}|[0-9]{5,})" ${group} >/dev/null; then grep -v -E "^${name}:[^:]*:([2-9][0-9]{3,}|[0-9]{5,})" ${group} >${tmp} && \ cat ${tmp} >${group} fi done elif test -n "${rebuild}"; then grep -v -E "^(\+|[^:]*:[^:]*:([2-9][0-9]{3,}|[0-9]{5,})):" ${group} >${tmp} && \ if test -n "${entries}"; then echo ${e} "${entries}" else cat fi | \ grep -E "^[^:]*:[^:]*:([2-9][0-9]{3,}|[0-9]{5,})" >>${tmp} && \ cat ${tmp} >${group} else nis="`/usr/machine/bin/mktemp -c -p .\`basename $0\`.`" if test -n "${entries}"; then echo ${e} "${entries}" elif test -n "${stdin}"; then cat fi | while read entry; do gid="`echo ${entry} | cut -d: -f3`" if test ${gid:-0} -ge 2000; then name="`echo ${entry} | cut -d: -f1`" # We have to separate NIS entries, since Linux NIS # doesn't accept normal entries behind NIS entries grep "^+" ${group} >${nis} if grep -E "^${name}:[^:]*:([2-9][0-9]{3,}|[0-9]{5,})" ${group} >/dev/null; then grep -v -E "^(\+|${name}:[^:]*:([2-9][0-9]{3,}|[0-9]{5,}))" ${group} >${tmp} && \ echo "${entry}" >>${tmp} && \ cat ${nis} >>${tmp} && \ cat ${tmp} >${group} else if test -s ${nis}; then grep -v "^+" ${group} >${tmp} && \ echo "${entry}" >>${tmp} && \ cat ${nis} >>${tmp} && \ cat ${tmp} >${group} else echo "${entry}" >>${group} fi fi fi done rm -f ${nis} fi rm -f ${tmp} exit 0