We help IT Professionals succeed at work.

Source needed for ENCTYPE receiving program

mravell
mravell asked
on
Medium Priority
280 Views
Last Modified: 2008-03-06
I'm in desperate need of some C source for a CGI program that will receive a file via the ENCTYPE tag that Netscape supports and then write it into onto the disk. The source needs to be for Windows (I use the Micro$oft compiler).

Yes I know that this sort of thing is asking for security problems but I have a specific, Intranet application for it.


TIA
Marty
Comment
Watch Question

Author

Commented:
Adjusted points to 150
Commented:
#include <string.h>
here's some c-code that I use. I'll include the html that goes
with it so you can substitute variables.  It's not for
windows, but the algorithm should be portable. It's really thrown
together, but you'll get the idea....

#include <stdio.h>
#include <unistd.h>
#ifndef NO_STDLIB_H
#include <stdlib.h>
#include <fcntl.h>
#else
char *getenv();
#endif


typedef struct {
    char *name;
    char *val;
} entry;

#define MAX_ENTRIES 10

main(int argc, char *argv[]) {
   entry entries[MAX_ENTRIES];
        register int x,m=0;
        char header[50], openfile[128], endline[128];
        int cl, readlen, ctr, filenum;
        char *breakline, *wline, *ptr;
        FILE *out;

        sprintf(header,"Content-type: text/html%c%c",10,10);
        write(STDOUT_FILENO, header, strlen(header));
        printf("<BODY BGCOLOR=#404040 TEXT=#FFFFFF>\n");
        if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
            printf("This script should be referenced with a METHOD of POST.\n");
            exit(1);
        }

        cl = atoi(getenv("CONTENT_LENGTH"));
        total = malloc(cl);
        ctr = 0;
        while(cl > 0){
           read(STDIN_FILENO, &total[ctr++], 1);
           cl--;
        }
        total[ctr] = 0;
        ptr = strchr(total, 13);
        breakline = total;
        *ptr++ = 0;
        total = ++ptr;
        sprintf(endline, "\n%s", breakline);
        do {
           wline=total;
           ptr = strchr(total, 13);
           *ptr = 0;
           for (ctr = 0;ctr < 2;ctr++)
              ptr++;
           total = ptr;
           ptr = strchr(total, 13);
           for (ctr = 0;ctr < 2;ctr++)
              ptr++;
           total = ptr;
           ptr = strstr(wline,"name=");
           for (ctr = 0;ctr < 6;ctr++)
              ptr++;
           entries[m].name = ptr;
           ptr = strchr(entries[m].name,'"');
           *ptr = 0;
           if (strcmp(entries[m].name, "fileitself") == 0) {
              m++;
              break;
           }
           ptr = strstr(entries[m].val, breakline);
           ptr--;
           *--ptr=0;
           x = strlen(breakline) + 4;
           for (ctr = 0;ctr < x;ctr++)
              ptr++;
           total = ptr;
           m++;
        } while (strstr(total, "Content") != NULL);
   for (x = 0; x < m; x++) {
      if (strcmp(entries[x].name,"filenam") == 0)
         filenam = entries[x].val;
      else if (strcmp(entries[x].name,"userid") == 0)
         userid = entries[x].val;
      else if (strcmp(entries[x].name,"password") == 0)
         password = entries[x].val;
      else if (strcmp(entries[x].name,"fileitself") == 0)
         file = entries[x].val;
  if (strcmp(userid,"userid") != 0 || strcmp(password,"passwd") != 0) {
      printf("<H1>BAD USERID OR PASSWORD</H1>\n");
      exit(0);
   }
   strcpy(openfile,"/usr/www/htdocs/chris/");
   strcat(openfile,filenam);
   if ((out = fopen(openfile, "w")) == NULL) {
      printf("file open error\n");
      printf("%s\n", openfile);
      exit(1);
   }
   ptr=file;
   ptr++;
   ptr++;
   while (strstr(ptr,breakline) != ptr) {
      fprintf(out, "%c", *ptr);
      ptr++;
   }
   fclose(out);
   printf("<H1>File uploaded</H1>\n");
}

########################Begin Html###########
<TITLE>Upload a file</TITLE>
<BODY BGCOLOR=#404040 TEXT=#FFFFFF>
<H3>upload a file to your directory</H3>
<FORM ENCTYPE="multipart/form-data" ACTION=/cgi-bin/upload METHOD=POST>
the name you want the file to be called on the server:
<INPUT TYPE=text NAME=filenam><BR>
your userid: <INPUT TYPE=text NAME=userid><BR>
your password: <INPUT TYPE=password NAME=password><BR>
file: <INPUT TYPE=file NAME=fileitself><BR>
<INPUT TYPE=submit value="go for it!">
</FORM>


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Ernst,

This has given me enough to get started. I am porting it to Micro$oft C for use on NT.


Regards
Marty
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.