sudoers rename cmnd_alias and replace where it is referenced

0

I need to concatenate hundred of sudoers file in one big file.. it is the requirement

the resulting concatenated file must be a valid sudoers file

I verify with visudo -cf huge_sudoers

if i concatenate the files there are duplicated command alias that sometimes differ between hosts.. I already removed duplicated. but there are command alias that are named the same but are in fact different.. eg:

on host_A:

Cmnd_Alias SHELLS= /usr/bin/bash /usr/sh

on Host_B:

Cmd_Alias SHELLS= /bin/ksh /usr/bin/zsh

the idea I have to rename the cmnd_Alias by prepending the hostname like this:

Cmnd_Alias HOSTB_SHELLS = /bin/ksh, /usr/bin/zsh 

I can do this with awk:

awk '{if ($1 == "Cmnd_Alias") $2="$hostname_"$2;}1' sudoers

but how can I also replace where the command are called or referenced..

eg

+operator   HostB = NOPASSWD: SHELLS

it should become:

+operator   HostB = NOPASSWD: HOSTB_SHELLS

I can edit the awk command to put per instance $2 in a var and then go replace every where in the file?

edit: I have done this so far:

#get old name of cmd alias
OLD="$( awk '{if ($1 == "Cmnd_Alias" ) print $2}' sudoers)"

#replace the old alias by the new:
awk '{if ($1 == "Cmnd_Alias" ) $2="$MYHOST_"$2;}1' sudoers > cmd.alias.tmp

#get the new alias name
NEW=$(awk '{if ($1 == "Cmnd_Alias" ) print $2}' cmd.alias.tmp)

#replace
sed /$OLD/s/$OLD/$NEW/ cmd.alias.tmp 

is there a better way?

danidar

Posted 2019-03-27T13:48:47.807

Reputation: 1

No answers