Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CGI excutable program problem in Linux

Posted on 2004-08-09
7
Medium Priority
?
177 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 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…
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.

596 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