/* * kldp.org login test. * * gcc -o curltest2 curltest2.c -Wall `pkg-config --cflags --libs libcurl` * * ./curltest2 your_id your_passwd * * bushi at mizi dot com * */ #include #include #include #include #include #include #include #include #define URL_DEFAULT "http://kldp.org/front-1007" #define COOKIE_DEFAULT "kldp_cookie_1" #define URL_LOGIN "http://kldp.org/front-1007?destination=front-1007" #define COOKIE_LOGIN "kldp_cookie_2" #define POST_LOGIN_FMT "name=%s&pass=%s&form_id=user_login_block" typedef struct content_s { size_t len; char *buf; } content_t; static void dump_content(content_t *content) { if (!content || !content->buf) return; content->buf[content->len] = 0; printf("%s\n", content->buf); return; } static void init_content(content_t *content) { if (content) content->len = 0; } static void free_content(content_t *content) { if (content->buf) free(content->buf); free(content); } static content_t *new_content(void) { content_t *content; content = malloc(sizeof(content)); if (!content) { perror("malloc()"); return NULL; } content->buf = NULL; init_content(content); return content; } static int write_to_file(const char *name, content_t *content) { int fd, ret; if (!content || !content->buf) return -1; fd = open(name, O_CREAT | O_WRONLY, 0600); if (fd < 0) { perror(name); return fd; } ret = write(fd, content->buf, content->len); close(fd); if (ret < 0) { perror(name); } return ret; } static size_t curl_write_cb(void *ptr, size_t size, size_t nmemb, void *stream) { size_t addition = size * nmemb; char *new_buf; content_t *content = stream; if (!addition) return 0; if (!content) return addition; new_buf = realloc(content->buf, content->len + addition + 1); if (!new_buf) { perror("realloc()"); return 0; } content->buf = new_buf; memcpy(content->buf + content->len, ptr, addition); content->len += addition; return addition; } static CURLcode request(const char *id, const char *url, content_t *body, content_t *header, const char *cookie, const char *postdata, int postlen) { CURL *curl; CURLcode res; curl = curl_easy_init(); if (!curl) return -1; init_content(body); init_content(header); /* verbosity */ curl_easy_setopt(curl, CURLOPT_VERBOSE, 10); /* header, body */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb); curl_easy_setopt(curl, CURLOPT_WRITEDATA, body); curl_easy_setopt(curl, CURLOPT_WRITEHEADER, header); /* POST */ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postlen); /* cookie */ curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie); /* url */ curl_easy_setopt(curl, CURLOPT_URL, url); res = curl_easy_perform(curl); if (res) fprintf(stderr, "CURL: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); return res; } int main(int argc, char **argv) { CURLcode res; content_t *body, *header; char poststr[128]; int ret; if (argc < 3) { fprintf(stderr, "%s \n", argv[0]); return 1; } snprintf(poststr, sizeof(poststr), POST_LOGIN_FMT, argv[1], argv[2]); unlink(COOKIE_DEFAULT); unlink(COOKIE_LOGIN); body = new_content(); header = new_content(); res = request("first", URL_DEFAULT, NULL, header, NULL, NULL, 0); if (res) goto out; ret = write_to_file(COOKIE_DEFAULT, header); if (ret < 0) goto out; res = request("second", URL_LOGIN, NULL, header, COOKIE_DEFAULT, poststr, strlen(poststr)); if (res) goto out; ret = write_to_file(COOKIE_LOGIN, header); if (ret < 0) goto out; res = request("third", URL_DEFAULT, body, header, COOKIE_LOGIN, NULL, 0); if (res) goto out; // dump_content(header); dump_content(body); // write_to_file("kldp.html", body); out: free_content(body); free_content(header); return 0; }