Solved

CGI excutable program problem in Linux

Posted on 2004-08-09
7
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

731 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