The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
"unexpected end of file" when Iīm use EOF inside block if ricardo.ludwig Shell Programming and Scripting 4 03-28-2008 11:45 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 09:52 PM
check input = "empty" and "numeric" geoffry Shell Programming and Scripting 6 12-13-2007 01:12 AM
a weird issue with "while" block sleepy_11 Shell Programming and Scripting 7 08-06-2007 08:33 PM
Maximum input file size in "Diff" Command Neeraja UNIX for Dummies Questions & Answers 1 01-17-2007 06:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-19-2006
Registered User
 

Join Date: Jul 2004
Location: New York State
Posts: 65
Block ";" in input string

To prevent injection, I want to exit the attached routine if a semi-colon is in the input string. I am using gcc as the compiler.

#include<stdio.h>
#include<stdlib.h>
int sysrun(char *command) {
int num;
char str[80];
char process[39] = "/xxxx/xxxx/xxxxx/xxxxx/xxxxxx2unix.sh ";
num=0;
strcpy(str,process);
strncat(str,command,35);
num = system(str);
return num;
}

Any thing that is passed via the command string, will be appended as replacement values on the command line. What I want to do is detect if a ";" is in the command string so that I can exit the application without allowing injection. Any help would be appreciated.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-19-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
I don't quite get what you are trying to do, but this is probably what you want.
Code:
if(strstr(command,";")) {
                fprintf(stdout,"command strings has a ';' in it!\n");
                exit(-1);
}
Reply With Quote
  #3 (permalink)  
Old 06-19-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,454
Shells let you do stuff like this:
command1 ; command2
and that is exactly what you want to prevent. But what about:
command1 && command2
command || command2
One of those would work, depending on the exit code from your shell script. This is probably legal too:
command1 & command2
And there are other variants. The best way to protect yourself is to get rid of system() and instead just fork() and exec().
Reply With Quote
  #4 (permalink)  
Old 06-20-2006
Registered User
 

Join Date: Jul 2004
Location: New York State
Posts: 65
I am passing a command line option to an oracle external procedure script. Internally the shell script will parse the command line option, but I needed to protect my invokation from imbedded unix commands, which is why I wanted to exit on a semi colon.
Reply With Quote
  #5 (permalink)  
Old 06-20-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,868
I think I get what you want.
Code:
#!/bin/ksh
echo "$@" | grep -q ';' 
if [ $? -eq 0 ] ; then
   echo "invalid parameters"
   exit 1
fi
Reply With Quote
  #6 (permalink)  
Old 06-20-2006
Registered User
 

Join Date: Jul 2004
Location: New York State
Posts: 65
Quote:
Originally Posted by jim mcnamara
I think I get what you want.
Code:
#!/bin/ksh
echo "$@" | grep -q ';' 
if [ $? -eq 0 ] ; then
   echo "invalid parameters"
   exit 1
fi
Thanks for the idea, however the test needed to be inside my "C" application.
Reply With Quote
  #7 (permalink)  
Old 06-20-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,868
ok -
Code:
for(i=1;i<argc;i++)
{
   if(strchr(argv[i],';')!=NULL)
   {
       fprintf(stderr,"%s\n","invalid parameter");
       exit(EXIT_FAILURE);      
   }
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0