Link to home
Start Free TrialLog in
Avatar of sctccomm
sctccomm

asked on

CGI excutable program problem in Linux

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.
Avatar of jlevie
jlevie

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.
Avatar of sctccomm

ASKER

I try the following before and it behave the same

//      sprintf(command,"/home/www/htpasswd /home/www/.htpasswd user2 pass2");
//      system(command);
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.
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).  
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?
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?
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial