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;
606 gethostname(host_name,
sizeof(host_name));
608 phe = gethostbyname(host_name);
610 perror(
"Cannot retrieve host name");
613 phe = gethostbyaddr(phe->h_addr,
sizeof(
int), AF_INET);
615 perror(
"Cannot retrieve host name");
620 if (strchr(host_name,
'.') == NULL)
621 strcpy(host_name, phe->h_name);
623 if (edit || download) {
625 status =
retrieve_elog(host, port, subdir, ssl, experiment, uname, upwd, edit,
626 old_attrib_name, old_attrib,
old_text);
628 status =
retrieve_elog(host, port, subdir, ssl, experiment, uname, upwd, download,
629 old_attrib_name, old_attrib,
old_text);
635 for (index = 0; index < n_attr; index++) {
636 for (i = 0; i <
MAX_N_ATTR && old_attrib_name[i][0]; i++)
640 if (old_attrib_name[i][0])
645 for (i = 0; i <
MAX_N_ATTR && old_attrib_name[i][0]; i++) {
658 printf(
"%s", strstr(
response,
"$@MID@$:"));
666 retrieve_elog(host, port, subdir, ssl, experiment, uname, upwd, reply,
667 old_attrib_name, old_attrib,
old_text);
673 for (index = 0; index < n_attr; index++) {
674 for (i = 0; i <
MAX_N_ATTR && old_attrib_name[i][0]; i++)
678 if (old_attrib_name[i][0])
683 for (i = 0; i <
MAX_N_ATTR && old_attrib_name[i][0]; i++) {
685 attrib_name[i][0] = 0;
696 old_encoding =
"plain";
698 for (i = 0; i < n_attr; i++)
703 old_encoding = attrib[i];
705 if (quote_on_reply) {
713 if (strchr(p,
'\n')) {
714 *strchr(p,
'\n') = 0;
716 if (old_encoding[0] ==
'H') {
730 if (old_encoding[0] ==
'H') {
755 if (ssl_connect(sock, &ssl_con) < 0) {
756 printf(
"elogd server does not run SSL protocol\n");
761 content_length = 100000;
765 content = (
char *)malloc(content_length);
767 printf(
"Not enough memory\n");
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");
779 "%s\r\nContent-Disposition: form-data; name=\"unm\"\r\n\r\n%s\r\n", boundary, uname);
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,
790 "%s\r\nContent-Disposition: form-data; name=\"exp\"\r\n\r\n%s\r\n", boundary, experiment);
794 "%s\r\nContent-Disposition: form-data; name=\"reply_to\"\r\n\r\n%d\r\n", boundary, reply);
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);
805 "%s\r\nContent-Disposition: form-data; name=\"suppress\"\r\n\r\n1\r\n", boundary);
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);
817 for (i = 0; i < n_attr; i++) {
818 strcpy(str, attrib_name[i]);
822 "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", boundary, str, attrib[i]);
828 "%s\r\nContent-Disposition: form-data; name=\"Text\"\r\n\r\n%s\r\n%s\r\n",
829 boundary,
text, boundary);
831 content_length = strlen(
content);
835 if (afilename[i][0]) {
837 "Content-Disposition: form-data; name=\"attfile%d\"; filename=\"%s\"\r\n\r\n",
838 i + 1, afilename[i]);
840 content_length += strlen(p);
856 strcpy(str, experiment);
860 strcat(
request,
" HTTP/1.0\r\n");
862 sprintf(
request + strlen(
request),
"Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
864 sprintf(str,
"%s:%d", host, port);
866 sprintf(str,
"%s", host);
869 sprintf(
request + strlen(
request),
"Content-Length: %d\r\n", content_length);
873 header_length = strlen(
request);
887 SSL_write(ssl_con,
request, header_length);
890 send(sock,
request, header_length, 0);
892 printf(
"Request sent to host:\n");
899 SSL_write(ssl_con,
content, content_length);
902 send(sock,
content, content_length, 0);
904 printf(
"Content sent to host:\n");
917 perror(
"Cannot receive response");
937 SSL_shutdown(ssl_con);
945 printf(
"Response received:\n");
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");
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;
965 if (strrchr(str,
'/'))
966 printf(
"Message successfully transmitted, ID=%s\n", strrchr(str,
'/') + 1);
968 printf(
"Message successfully transmitted, ID=%s\n", str);
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);
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);
989 else if (strstr(
response,
"form name=form1"))
990 printf(
"Error: Missing or invalid user name/password\n");
992 printf(
"Error transmitting message\n");
void url_encode(char *ps, int size)
void do_crypt(char *s, char *d, int size)
size_t strlcpy(char *dst, const char *src, size_t size)
int equal_ustring(const char *str1, const char *str2)
static const size_t buffer_size
int elog_connect(char *host, int port)
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)
size_t strlcat(char *dst, const char *src, size_t size)