#include "stdio.h" #include #define MAXENTRIES 100 /* biliothŠque */ typedef struct { char *name; char *val; } entry; entry entries[MAXENTRIES]; int cl; FILE *logfile; FILE *base; /****************************************************************************/ /* bibliothŠque */ #define LF '\xA' #define CR '\xD' void getword(char *word, char *line, char stop) { int x = 0,y; for(x=0;((line[x]) && (line[x] != stop));x++) word[x] = line[x]; word[x] = '\0'; if(line[x]) ++x; y=0; while(line[y++] = line[x++]); } char *makeword(char *line, char stop) { int x = 0,y; char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1)); for(x=0;((line[x]) && (line[x] != stop));x++) word[x] = line[x]; word[x] = '\0'; if(line[x]) ++x; y=0; while(line[y++] = line[x++]); return word; } char *fmakeword(FILE *f, char stop, int *cl) { int wsize; char *word; int ll; wsize =10400; ll=0; word = (char *) malloc(sizeof(char) * (wsize + 1)); while(1) { word[ll] = (char)fgetc(f); if(ll==wsize) { word[ll+1] = '\0'; wsize+=10400; word = (char *)realloc(word,sizeof(char)*(wsize+1)); } --(*cl); if((word[ll] == stop) || (feof(f)) || (!(*cl))) { if(word[ll] != stop) ll++; word[ll] = '\0'; return word; } ++ll; } } char x2c(char *what) { register char digit; digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0')); digit *= 16; digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0')); return(digit); } void unescape_url(char *url) { register int x,y; for(x=0,y=0;url[y];++x,++y) { if((url[x] = url[y]) == '%') { url[x] = x2c(&url[y+1]); y+=2; } } url[x] = '\0'; } void plustospace(char *str) { register int x; for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' '; } int rind(char *s, char c) { register int x; for(x=strlen(s) - 1;x != -1; x--) if(s[x] == c) return x; return -1; } int getline(char *s, int n, FILE *f) { register int i=0; while(1) { s[i] = (char)fgetc(f); if(s[i] == CR) s[i] = fgetc(f); if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) { s[i] = '\0'; return (feof(f) ? 1 : 0); } ++i; } } void send_fd(FILE *f, FILE *fd) { int num_chars=0; char c; while (1) { c = fgetc(f); if(feof(f)) return; fputc(c,fd); } } int ind(char *s, char c) { register int x; for(x=0;s[x];x++) if(s[x] == c) return x; return -1; } void escape_shell_cmd(char *cmd) { register int x,y,l; l=strlen(cmd); for(x=0;cmd[x];x++) { if(ind("&;`'\"|*?~<>^()[]{}$\\",cmd[x]) != -1){ for(y=l+1;y>x;y--) cmd[y] = cmd[y-1]; l++; /* length has been increased */ cmd[x] = '\\'; x++; /* skip the character */ } } } char *fgetword(FILE *f, char stop) { int wsize; char *word; int ll; wsize =10; ll=0; word = (char *) malloc(sizeof(char) * (wsize + 1)); while(1) { word[ll] = (char)fgetc(f); if(ll==wsize) { word[ll+1] = '\0'; wsize+=10; word = (char *)realloc(word,sizeof(char)*(wsize+1)); } if((word[ll] == stop) || (feof(f)) ) { if(word[ll] != stop) ll++; word[ll] = '\0'; return word; } ++ll; } } /****************************************************************************/ main(argc,argv) int argc; char **argv; { register int x,m=0; char c; printf("Content-type: text/html%c%c",10,10); /* ouverture du fichier de donn‚es */ if ((base = fopen("/var/spool/www/data/database.dat", "r")) == NULL) { printf("Erreur ouverture de la base : SVP e-mail les d‚tails … assistance@somewhere.ch" ); exit(1); } /* ouverture d'un fichier log pour y mettre les donn‚es*/ if ((logfile = fopen("/var/spool/www/data/application.log", "a")) == NULL) { printf("Erreur serveur: SVP e-mail les d‚tails … assistance@somewhere.ch" ); exit(1); } if(strcmp(getenv("REQUEST_METHOD"),"POST")) { printf("Ce script devrait ˆtre referenc‚ par POST\n"); exit(1); } if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) { printf("Ce script peut seulement ˆtre utilis‚ pour d‚coder les r‚sultats de form. \n"); exit(1); } /* pr‚pare le fichier log avec date, temps, et var d'environ. CGI */ fprintf(logfile, "\n===================================\n"); fprintf(logfile, "AUTH_TYPE : %s\n", getenv("AUTH_TYPE")); fprintf(logfile, "GATEWAY_INT: %s\n", getenv("GATEWAY_INTERFACE")); fprintf(logfile, "REMOTE_ADR : %s\n", getenv("REMOTE_ADDR")); fprintf(logfile, "REMOTE_HOST : %s\n", getenv("REMOTE_HOST")); fprintf(logfile, "REMOTE_IDENT : %s\n", getenv("REMOTE_IDENT")); fprintf(logfile, "REMOTE_USER : %s\n", getenv("REMOTE_USER")); fprintf(logfile, "\n Details \n\n"); cl = atoi(getenv("CONTENT_LENGTH")); for (x=0;cl && (!feof(stdin));x++) { m=x; entries[x].val=fmakeword(stdin,'&',&cl); plustospace(entries[x].val); unescape_url(entries[x].val); entries[x].name=makeword(entries[x].val,'='); } printf(" MERCI ! "); printf("
"); printf("Vous avez soumis les donn‚es:

%c",10); printf("

%c",10); }/* main */