595{
  596  int status, sock, i, 
n, header_length, content_length, index;
 
  597  char host_name[256], boundary[80], str[80], encrypted_passwd[256], *p;
  598  const char* old_encoding;
  600  struct hostent *phe;
  601#ifdef HAVE_SSL
  602  SSL *ssl_con = NULL;
  603#endif
  604 
  605  
  606  gethostname(host_name, sizeof(host_name));
  607 
  608  phe = gethostbyname(host_name);
  609  if (phe == NULL) {
  610    perror("Cannot retrieve host name");
  611    return -1;
  612  }
  613  phe = gethostbyaddr(phe->h_addr, sizeof(int), AF_INET);
  614  if (phe == NULL) {
  615    perror("Cannot retrieve host name");
  616    return -1;
  617  }
  618 
  619  
  620  if (strchr(host_name, '.') == NULL)
  621    strcpy(host_name, phe->h_name);
  622 
  623  if (edit || download) {
  624    if (edit)
  625      status = 
retrieve_elog(host, port, subdir, ssl, experiment, uname, upwd, edit,
 
  626                             old_attrib_name, old_attrib, 
old_text);
 
  627    else
  628      status = 
retrieve_elog(host, port, subdir, ssl, experiment, uname, upwd, download,
 
  629                             old_attrib_name, old_attrib, 
old_text);
 
  630 
  631    if (status != 1)
  632      return status;
  633 
  634    
  635    for (index = 0; index < n_attr; index++) {
  636      for (i = 0; i < 
MAX_N_ATTR && old_attrib_name[i][0]; i++)
 
  638          break;
  639 
  640      if (old_attrib_name[i][0])
  642    }
  643 
  644    
  645    for (i = 0; i < 
MAX_N_ATTR && old_attrib_name[i][0]; i++) {
 
  648    }
  649 
  650    n_attr = i;
  651 
  654  }
  655   
  656  if (download) {
  658      printf(
"%s", strstr(
response, 
"$@MID@$:"));
 
  659    else
  661    return 1;
  662  }
  663 
  664  if (reply) {
  665    status =
  666      retrieve_elog(host, port, subdir, ssl, experiment, uname, upwd, reply,
 
  667                    old_attrib_name, old_attrib, 
old_text);
 
  668 
  669    if (status != 1)
  670      return status;
  671 
  672    
  673    for (index = 0; index < n_attr; index++) {
  674      for (i = 0; i < 
MAX_N_ATTR && old_attrib_name[i][0]; i++)
 
  676          break;
  677 
  678      if (old_attrib_name[i][0])
  680    }
  681 
  682    
  683    for (i = 0; i < 
MAX_N_ATTR && old_attrib_name[i][0]; i++) {
 
  685        attrib_name[i][0] = 0;
  686        attrib[i][0] = 0;
  687      } else {
  690      }
  691    }
  692 
  693    n_attr = i;
  694 
  695    
  696    old_encoding = "plain";
  697 
  698    for (i = 0; i < n_attr; i++)
  700        break;
  701 
  702    if (i < n_attr)
  703      old_encoding = attrib[i];
  704 
  705    if (quote_on_reply) {
  707 
  708      
  711 
  712      do {
  713        if (strchr(p, '\n')) {
  714          *strchr(p, '\n') = 0;
  715 
  716          if (old_encoding[0] == 'H') {
  720          } else {
  724          }
  725 
  726          p += strlen(p) + 1;
  727          if (*p == '\n')
  728            p++;
  729        } else {
  730          if (old_encoding[0] == 'H') {
  734          } else {
  738          }
  739 
  740          break;
  741        }
  742 
  743      } while (1);
  744 
  746    }
  747  }
  748 
  750  if (sock < 0)
  751    return sock;
  752 
  753#ifdef HAVE_SSL
  754  if (ssl)
  755    if (ssl_connect(sock, &ssl_con) < 0) {
  756      printf("elogd server does not run SSL protocol\n");
  757      return -1;
  758    }
  759#endif
  760 
  761  content_length = 100000;
  763    if (afilename[i][0])
  765  content = (
char *)malloc(content_length);
 
  767    printf("Not enough memory\n");
  768    return -1;
  769  }
  770 
  771  
  772  srand((unsigned) time(NULL));
  773  sprintf(boundary, "---------------------------%04X%04X%04X", rand(), rand(), rand());
  775  strcat(
content, 
"\r\nContent-Disposition: form-data; name=\"cmd\"\r\n\r\nSubmit\r\n");
 
  776 
  777  if (uname[0])
  779            "%s\r\nContent-Disposition: form-data; name=\"unm\"\r\n\r\n%s\r\n", boundary, uname);
  780 
  781  if (upwd[0]) {
  782    do_crypt(upwd, encrypted_passwd, 
sizeof(encrypted_passwd));
 
  784            "%s\r\nContent-Disposition: form-data; name=\"upwd\"\r\n\r\n%s\r\n", boundary,
  785            encrypted_passwd);
  786  }
  787 
  788  if (experiment[0])
  790            "%s\r\nContent-Disposition: form-data; name=\"exp\"\r\n\r\n%s\r\n", boundary, experiment);
  791 
  792  if (reply)
  794            "%s\r\nContent-Disposition: form-data; name=\"reply_to\"\r\n\r\n%d\r\n", boundary, reply);
  795 
  796  if (edit) {
  798            "%s\r\nContent-Disposition: form-data; name=\"edit_id\"\r\n\r\n%d\r\n", boundary, edit);
  800            "%s\r\nContent-Disposition: form-data; name=\"skiplock\"\r\n\r\n1\r\n", boundary);
  801  }
  802 
  803  if (suppress)
  805            "%s\r\nContent-Disposition: form-data; name=\"suppress\"\r\n\r\n1\r\n", boundary);
  806 
  807  if (encoding == 0)
  809            "%s\r\nContent-Disposition: form-data; name=\"encoding\"\r\n\r\nELCode\r\n", boundary);
  810  else if (encoding == 1)
  812            "%s\r\nContent-Disposition: form-data; name=\"encoding\"\r\n\r\nplain\r\n", boundary);
  813  else if (encoding == 2)
  815            "%s\r\nContent-Disposition: form-data; name=\"encoding\"\r\n\r\nHTML\r\n", boundary);
  816 
  817  for (i = 0; i < n_attr; i++) {
  818    strcpy(str, attrib_name[i]);
  819    if (str[0]) {
  822              "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", boundary, str, attrib[i]);
  823    }
  824  }
  825 
  828            "%s\r\nContent-Disposition: form-data; name=\"Text\"\r\n\r\n%s\r\n%s\r\n",
  829            boundary, 
text, boundary);
 
  830 
  831  content_length = strlen(
content);
 
  833 
  835    if (afilename[i][0]) {
  836      sprintf(p,
  837              "Content-Disposition: form-data; name=\"attfile%d\"; filename=\"%s\"\r\n\r\n",
  838              i + 1, afilename[i]);
  839 
  840      content_length += strlen(p);
  841      p += strlen(p);
  844      strcpy(p, boundary);
  845      strcat(p, "\r\n");
  846 
  848      p += strlen(p);
  849    }
  850 
  851  
  853  if (subdir[0])
  855  if (experiment[0]) {
  856    strcpy(str, experiment);
  859  }
  860  strcat(
request, 
" HTTP/1.0\r\n");
 
  861 
  862  sprintf(
request + strlen(
request), 
"Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
 
  863  if (port != 80)
  864    sprintf(str, "%s:%d", host, port);
  865  else
  866    sprintf(str, "%s", host);
  869  sprintf(
request + strlen(
request), 
"Content-Length: %d\r\n", content_length);
 
  870 
  872 
  873  header_length = strlen(
request);
 
  874 
  875  
  876
  877
  878
  879
  880
  881
  882
  883 
  884  
  885#ifdef HAVE_SSL
  886  if (ssl)
  887    SSL_write(ssl_con, 
request, header_length);
 
  888  else
  889#endif
  890    send(sock, 
request, header_length, 0);
 
  892    printf("Request sent to host:\n");
  894  }
  895 
  896  
  897#ifdef HAVE_SSL
  898  if (ssl)
  899    SSL_write(ssl_con, 
content, content_length);
 
  900  else
  901#endif
  902    send(sock, 
content, content_length, 0);
 
  904    printf("Content sent to host:\n");
  906  }
  907 
  908  
  910#ifdef HAVE_SSL
  911  if (ssl)
  913  else
  914#endif
  916  if (i < 0) {
  917    perror("Cannot receive response");
  918    return -1;
  919  }
  920 
  921  
  923  while (i > 0) {
  924#ifdef HAVE_SSL
  925    if (ssl)
  927    else
  928#endif
  930    if (i > 0)
  932  }
  934 
  935#ifdef HAVE_SSL
  936  if (ssl) {
  937    SSL_shutdown(ssl_con);
  938    SSL_free(ssl_con);
  939  }
  940#endif
  941 
  943 
  945    printf("Response received:\n");
  947  }
  948 
  949  
  950  if (strstr(
response, 
"302 Found")) {
 
  951    if (strstr(
response, 
"Location:")) {
 
  953        printf("Error: elogd server has moved to another location\n");
  955        printf("Error: Invalid user name or password\n");
  956      else {
  957        strncpy(str, strstr(
response, 
"Location:") + 10, 
sizeof(str));
 
  958        if (strchr(str, '?'))
  959          *strchr(str, '?') = 0;
  960        if (strchr(str, '\n'))
  961          *strchr(str, '\n') = 0;
  962        if (strchr(str, '\r'))
  963          *strchr(str, '\r') = 0;
  964 
  965        if (strrchr(str, '/'))
  966          printf("Message successfully transmitted, ID=%s\n", strrchr(str, '/') + 1);
  967        else
  968          printf("Message successfully transmitted, ID=%s\n", str);
  969      }
  970    } else
  971      printf("Message successfully transmitted\n");
  972  } 
else if (strstr(
response, 
"Logbook Selection"))
 
  973    printf("Error: No logbook specified\n");
  974  else if (strstr(
response, 
"enter password"))
 
  975    printf("Error: Missing or invalid password\n");
  976  else if (strstr(
response, 
"Error: Attribute")) {
 
  977    if (strstr(
response, 
"not existing")) {
 
  978      strncpy(str, strstr(
response, 
"Error: Attribute") + 27, 
sizeof(str));
 
  979      if (strchr(str, '<'))
  980        *strchr(str, '<') = 0;
  981      printf("Error: Non existing attribute option \"%s\"\n", str);
  982    } else {
  983      strncpy(str, strstr(
response, 
"Error: Attribute") + 20, 
sizeof(str));
 
  984      if (strchr(str, '<'))
  985        *strchr(str, '<') = 0;
  986      printf("Error: Missing required attribute \"%s\"\n", str);
  987    }
  988  }
  989  else if (strstr(
response, 
"form name=form1"))
 
  990    printf("Error: Missing or invalid user name/password\n");
  991  else
  992    printf("Error transmitting message\n");
  993 
  994  return 1;
  995}
static const size_t buffer_size
 
INT retrieve_elog(char *host, int port, char *subdir, int ssl, char *experiment, char *uname, char *upwd, int message_id, char attrib_name[MAX_N_ATTR][NAME_LENGTH], char attrib[MAX_N_ATTR][NAME_LENGTH], char *text)
 
int equal_ustring(const char *str1, const char *str2)
 
size_t strlcat(char *dst, const char *src, size_t size)