Solved

CGI excutable program problem in Linux

Posted on 2004-08-09
7
169 Views
Last Modified: 2013-11-18
Hello experts,

I have a simple CGI function that runs correctly if I am using a console but it doesn't work when it is trigger by a web page submit button.

The function below will change username to "user2" and password to "pass2" on a web page, and redirect the user to another web page.

When execute on a console it changes the username and password and print "Location: /ssi/access.shtml" on the screen.  However, it will not change the username and password but will redirect the user to the new page if it is trigger by a web page submit button.

/**********************************************************************************/
#include <stdio.h>            /* Standard input/output definitions */
#include <stdlib.h>
#include <string.h>            /* String function definitions */
#include <errno.h>
#include <unistd.h>            /* UNIX standard function definitions */
#include <fcntl.h>            /* File control definitions */
#include <ctype.h>

int main(int argc, char *argv[])
{
      if(execlp("htpasswd","htpasswd","/home/www/.htpasswd","user2","pass2",0)<0)
      {
            perror("execl:");
      }

      printf("Location: /ssi/access.shtml\n");      //redirect back to access.shtml
      exit(EXIT_SUCCESS);
}
/**********************************************************************************/

htpasswd is the bin executable that change username and password setting in .htpasswd file
.htpasswd is a text file that store the user name and password
(Both files located under /home/www)

The web server I am using is thttpd (http://www.acme.com/software/thttpd/) and it is run as root.
I can run the CGI script correctly on a console using any login including "root" and "nobody".

I am struggle with this problem for over a week now and I would like to know if anyone can give me some suggestion  on how to solve the problem.
0
Comment
Question by:sctccomm
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 11758243
Have you tried specifying the absolute path to htpasswd? It may be in your PATH in an interactive session and can't be found from the PATH of the web server.
0
 

Author Comment

by:sctccomm
ID: 11758288
I try the following before and it behave the same

//      sprintf(command,"/home/www/htpasswd /home/www/.htpasswd user2 pass2");
//      system(command);
0
 
LVL 40

Expert Comment

by:jlevie
ID: 11758603
Well that wouldn't work as written, but:

command = sprintf("/home/www/htpasswd /home/www/.htpasswd %s %s", user2 pass2);
system(command);

might, assuming that the variables user2 & pass2 have been set.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:sctccomm
ID: 11758666
I am not sure what you mean there jlevie

SYNOPSIS
       #include <stdio.h>
 
       int printf(const char *format, ...);
       int fprintf(FILE *stream, const char *format, ...);
       int sprintf(char *str, const char *format, ...);
       int snprintf(char *str, size_t size, const char *format, ...);
......
   Return value
       Upon successful return, these functions return the number of characters
       printed  (not  including  the  trailing  ’\0’  used  to  end  output to
       strings).  
0
 
LVL 40

Expert Comment

by:jlevie
ID: 11758800
My mistake... I had Perl on my mind.

What you had is correct for C code.  When you try using the "system(command" what error is returned?
0
 

Author Comment

by:sctccomm
ID: 11758848
When I run the program in console, there is no error and the program runs correctly.

When it is run as a CGI program, I am not sure how to look at the error message since any printf statment will not show on the terminal.
Do you have any suggestion?
0
 
LVL 40

Accepted Solution

by:
jlevie earned 500 total points
ID: 11759377
Open a file in /tmp and have the error results written to that file. You might also look at the logs for your web server and see if there's anything interesting.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now