#include #include #include #include #include #include #include #include #include extern struct tm *localtime_r(const time_t *clock, struct tm *res); #define MAILHOST "hugo" #define HOMEREP "../../XEDT" /*************************************************************/ void error(char *mess) { printf("error %s\n", mess); exit(1); } /*************************************************************/ int readfield(char *name, int namemax, char *value, int valuemax) { char c; int ind; ind = 0; while ((ind < (namemax - 1)) && ((c=getchar()) != EOF) && (c != '=')) { name[ind++] = c; } name[ind] = 0; ind = 0; while ((ind < (valuemax - 1)) && ((c=getchar()) != EOF) && (c != '&')) { value[ind++] = c; } value[ind] = 0; return(c); } /*************************************************************/ int urldecode(char *url) { char *in, *out; char hexa[3]; int val; in = out = url; while (*in) { switch(*in) { case '+': *out = ' '; in++; out++; break; case '%': in++; hexa[0] = *in; in++; hexa[1] = *in; hexa[2] = 0; if (sscanf(hexa,"%x", &val) != 1) { return(-1); } *out = val; out++; in++; break; default: *out = *in; out++; in++; } } *out = 0; } /*************************************************************/ #define LENMAX 1024 struct value_st { char name[LENMAX]; char value[LENMAX]; struct value_st *next; } *value_list = NULL; void loadvalue(char *name, char *value) { struct value_st *value_cel; if ((value_cel = calloc(1, sizeof(struct value_st))) == NULL) { error("3"); exit(1); } strncpy(value_cel->name, name, LENMAX-1); strncpy(value_cel->value, value, LENMAX-1); value_cel->next = value_list; value_list = value_cel; } static char *month_str[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}; void datevalue() { struct value_st *value_cel; time_t now; struct tm now_tm; char now_value[LENMAX]; time(&now); now += 32*24*3600; /* 32 days license */ if (localtime_r (&now, &now_tm) == NULL) { error("6"); exit(1); } // sprintf(now_value,"%d-%s-%d", now_tm.tm_mday,month_str[now_tm.tm_mon], now_tm.tm_year+1900); strcpy(now_value,"permanent"); if ((value_cel = calloc(1, sizeof(struct value_st))) == NULL) { error("5"); exit(1); } strncpy(value_cel->name, "date", LENMAX-1); strncpy(value_cel->value, now_value, LENMAX-1); value_cel->next = value_list; value_list = value_cel; } /*************************************************************/ void displayvalue(FILE *out, char *name) { struct value_st *cel = value_list; while(cel) { if (strcmp(cel->name, name) == 0) { fprintf(out, "%s", cel->value); return; } cel = cel->next; } } /*************************************************************/ #define OK 1 #define NOTOK 0 int nberrors ; void newerror() { if (!nberrors) { printf("\n
\n
\n Some errors occured, could you correct them please?\n"); } nberrors++; } int verifemail() { struct value_st *cel = value_list; while(cel) { if (strcmp(cel->name, "email") == 0) { if (strlen(cel->value) == 0) { break; } if (strchr(cel->value,'@') == NULL) { newerror(); printf("
  • The email address is not valid.
  • \n"); return(NOTOK); } else { return(OK); } } cel = cel->next; } newerror(); printf("
  • Please give a value for the email address
  • \n"); return(NOTOK); } int verifstring(char *name) { struct value_st *cel = value_list; while(cel) { if (strcmp(cel->name, name) == 0) { if (strlen(cel->value)) { return(OK); } else { break; } } cel = cel->next; } newerror(); printf("
  • Please give a value for the %s field
  • \n", name); return(NOTOK); } int verifid() { struct value_st *cel = value_list; char *p; while(cel) { if (strcmp(cel->name, "hostid") == 0) { if (strlen(cel->value) == 0) { break; } p = cel->value; while (*p) { if (!isxdigit(*p)) { newerror(); printf("
  • The server ID should be an hexadecimal number without \"Ox\" at the beginning
  • \n"); return(NOTOK); } p++; } return(OK); } cel = cel->next; } newerror(); printf("
  • Please give a value for the server ID
  • \n"); return(NOTOK); } int verifvalue() { nberrors = 0; verifemail(); verifstring("name"); verifstring("address"); verifstring("city"); verifstring("hostname"); verifid(); if (nberrors != 0) { printf("
    \n
    \n
    \n
     
    \n"); return(NOTOK); } else { return(OK); } } /*************************************************************/ void computevalue() { datevalue(); return; } /*************************************************************/ #define LINELEN 1024 #define REPERE "%xedt" int loadpage(char *inname, FILE *out) { char linein[LINELEN]; char *line,*p, *name; FILE *in; if ((in = fopen(inname,"r")) == NULL) { error("1"); exit(1); } while (fgets(linein, LINELEN, in) != NULL) { line = linein; while((p= strstr(line,REPERE)) != NULL) { *p = 0; fprintf(out,"%s", line); p += strlen(REPERE); name = p; while ((*p) && (*p != '%')) { p++; } if (*p != '%') { error("2"); exit(1); } *p = 0; displayvalue(out, name); p++; line = p; } fprintf(out, "%s", line); fflush(out); } fclose(in); } /*************************************************************/ FILE *opensendmail() { int sock; struct sockaddr_in serv_addr ; struct hostent *hp; FILE *fic; /* if ((fic = fopen("test","w")) == NULL) { ** error("14"); ** exit(1); **} ** ** return(fic); */ sock = socket ( PF_INET, SOCK_STREAM, 0); if (sock < 0) { error("10"); exit(1); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(25); if ((hp = gethostbyname(MAILHOST)) == NULL) { error("11"); exit(1); } bcopy(hp->h_addr,(char *)&serv_addr.sin_addr,hp->h_length); bzero(serv_addr.sin_zero,8); if (connect(sock,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { error("12"); exit(1); } if ((fic = fdopen(sock,"r+")) == NULL) { error("13"); exit(1); } return(fic); } #define MAXURL 1024 int main() { char c; char url[MAXURL]; char name[LENMAX], value[LENMAX], licname[LENMAX]; int new_form = 1; FILE *licfile, *mail; int status; while(readfield(name, LENMAX, value, LENMAX) != EOF) { urldecode(name); urldecode(value); loadvalue(name, value); new_form = 0; } printf("Content-type: text/html\n\n"); if (chdir(HOMEREP) == -1) { error("0"); exit(1); } loadpage("headedt.dat", stdout); if (new_form) { loadpage("formedt.dat", stdout); } else { if (verifvalue() != OK) { loadpage("formedt.dat", stdout); } else { computevalue(); sprintf(licname,"licence.%d", getpid()); if ((licfile = fopen(licname, "w")) == NULL) { error("7"); exit(1); } loadpage("licence.dat", licfile); fclose(licfile); switch(fork()) { case -1: { error("8"); exit(1); } case 0: { execl("./lmcrypt","lmcrypt", licname, 0); error("9"); exit(1); } default: { wait(&status); } } mail = opensendmail(); loadpage("mail.dat", mail); loadpage(licname, mail); fprintf(mail,"\n.\nquit\n"); fflush(mail); { FILE *resp; char line[LENMAX]; if ((resp = fopen("resp", "w")) == NULL) { error("16"); exit(1); } while (fgets(line, LENMAX, mail)) { fputs(line, resp); fflush(resp); } } fclose(mail); loadpage("bye.dat", stdout); unlink(licname); } } exit(0); }