Solved

CGI excutable program problem in Linux

Posted on 2004-08-09
7
173 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

680 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