Operating System - HP-UX
1820475 Members
2939 Online
109624 Solutions
New Discussion юеВ

Is it possible to prevent the user using a CTRL-C combination

 
ZezinhoBD
Occasional Contributor

Is it possible to prevent the user using a CTRL-C combination

Is it possible to prevent the user using a CTRL-C combination in a shell script?

I have a .profile that call another ( main menu ) and I need to prevent user using a CTRL-C combination to break the process and stop in a OS prompt. I have to force them that when they leave the called script ( main menu ) the process terminated with exit and close their unix session.

When the users use CTRL-C in the called script it may be that the previous process receive the CTRL signal and the users stay in a OS prompt without closing the Unix session. It's disastrous if they use the command line.

I hope I'm clear enought to you understand my problem.

Thanks

Jose Castro
9 REPLIES 9
Matti_Kurkela
Honored Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

Don't use .profile to start up your menu script.

Instead, use the "usermod" or "chsh" commands to change the user's default shell to be the menu script.

After this change, if the user interrupts the menu script, the session is automatically terminated.

You may have to create/modify the /etc/shells file to allow the modified user to log in with the custom shell. If the file does not exist, list all the standard shells in that file too.
MK
Ivan Krastev
Honored Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

Try in your script before invoking another:

trap '' 2

or

trap 'echo "Control-C disabled."' 2

regards,
ivan
perumal_2
Frequent Advisor

Re: Is it possible to prevent the user using a CTRL-C combination

Hi Jose

Use signal trapping before invoking sequences in your script.
If you are only particular about CTRL C (SIGINT)you can use
trap " " 2
for silently ignoring the interrupt in the fore ground.
If you want to dispaly any message use
trap "echo YOUR MESSAGE" 2

You can refer the file at /usr/include/sys/signals.h file to see more UNIX signals (need to scroll down a bit it is under /* Signal numbers */), which you may be intersted to use within your script to avoid any kind of break in your script execution.

Happy Scripting.

TQ
Perumal
Bill Hassell
Honored Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

The standard HP-UX /etc/profile already disables CTRL-C (for a short time). At thgye top, you'll see:

trap "" 1 2 3

and at the bottome:

trap 1 2 3

Now this looks a bit strange but here's what is happening:

The first trap says:

> When signals 1, 2 or 3 are sensed by the program, do the following: "" (which is do nothing)

and the second trap says:

> Return signals 1, 2 and 3 to their normal behavior.

Signals:
1=hangup (loss of connection has occurred)
2=interrupt (CTRL-C for interactive users)
3=quit (causes a program to core dump)

AS memtioned, you don't want a normal shell that starts your menu. Instead, change the shell from /usr/bin/sh to /usr/contrib/bin/menu_script and put only the required profile items plus the traps into the menu program. In your case, just use trap "" 2 at the start of your menu script.


Bill Hassell, sysadmin
Tor-Arne Nostdal
Trusted Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

Hi Jose,
In the previous posts you have got a description for how to trap the Ctrl+C signal.
#! /usr/bin/sh
# POSIX shell script
# Script name: menu.sh

# Disable Ctrl+C and interrupts
trap 'echo "\aProgram is terminated";rm $TMPFILE;exit' 1
#....your menu script...

Note line 1. You should always specify what kin of shell you are running.
Note 2: As you can see you can run multiple commands in the trap before leaving.
Note 3: If you have written the menu in csh you should use "onintr CTRLC" instead of trap.
#!/usr/bin/csh
onintr CTRLC

LOOP:
echo 'Continue Until \c'
read DUMMY
goto LOOP

CTRLC:
echo "You pressed Ctrl+C"
exit


Secondly, I would propose that you edit the /etc/shells and add this "menu" to the allowed shells and then use it as login script (this file should contain all shells you use as login shells)
#cat /etc/shells
/sbin/sh
/bin/sh
/bin/ksh
/bin/csh
/usr/bin/ksh
/usr/bin/sh
/usr/bin/csh
/usr/bin/false
/usr/bin/ftpshell
/usr/local/bin/menu.sh

When the user exit the menu they will logout.

/Tor-Arne
I'm trying to become President of the state I'm in...
spex
Honored Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

Hi Jose,

Call the menu from ~user/.profile with the 'exec' command. The session will immediately after the menu's process terminates.

PCS
Robert Fritz
Regular Advisor

Re: Is it possible to prevent the user using a CTRL-C combination

Hi Jose,

From your question, you seem to want to prevent a user from running an arbitrary command. Most Shells weren't designed to be run on behalf of a non-trusted user. You may want to reconsider writing your menu in shell, and rather use a "c" program, to better control your inputs.

Shells respond not only to signals, as you noticed, but depending on the type of input you bring in... they respond to field separators escape-sequences, and quoting, that could, in some cases, lead to arbitrary command execution.

-Robert
Those Who Would Sacrifice Liberty for Security Deserve Neither." - Benjamin Franklin
ZezinhoBD
Occasional Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

I'll test all suggestions

Thanks
Hein van den Heuvel
Honored Contributor

Re: Is it possible to prevent the user using a CTRL-C combination

Jose,

Please check back with the 'best' solution for you and award points ackordingly

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards,
Hein (0 points for this)