"Fossies" - the Fresh Open Source Software Archive 
Member "authforce-0.9.9/src/http.c" (13 May 2007, 4138 Bytes) of package /linux/www/old/authforce-0.9.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "http.c" see the
Fossies "Dox" file reference documentation.
1 /* $Id: http.c,v 1.3 2001/04/28 20:32:47 kapheine Exp $ */
2
3 #include <config.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #ifdef USE_CURL
9 #include <curl/curl.h>
10 #include <curl/types.h>
11 #include <curl/easy.h>
12 #endif /* USE_CURL */
13 #ifdef MEMWATCH
14 #include "memwatch.h"
15 #endif /* MEMWATCH */
16 #include "extern.h"
17
18 #define BLANKSPACE " "
19
20 static int num_connects = 0;
21
22 #ifdef USE_CURL
23
24 CURL *curl;
25 FILE *devnull;
26
27 void initialize_submit(void) {
28
29 devnull = fopen("/dev/null", "w");
30 curl = curl_easy_init();
31
32 if (!curl) {
33 fprintf(stderr, "initialize_submit: couldn't allocate curl\n");
34 exit(EXIT_FAILURE);
35 }
36 }
37
38 int submit(char *username, char *password) {
39
40 CURLcode result;
41 char error[CURL_ERROR_SIZE] = "";
42 char authstring[92];
43 time_t before, after;
44
45
46 num_connects++;
47 if (max_connects == num_connects && max_connects != 0) {
48 if (!quiet)
49 printf("max connects reached at %i\n", num_connects);
50 fprintf(logfd, "max connects reached at %i\n", num_connects);
51 exit(EXIT_SUCCESS);
52 }
53
54 snprintf(authstring, sizeof(authstring), "%s:%s", username, password);
55
56 if (!quiet && (debug_level == 0)) {
57 printf("trying %s->%s\r", username, password);
58 fflush(stdout);
59 } else
60 printf("trying %s->%s\n", username, password);
61
62 before = time(0);
63
64 if (curl) {
65 curl_easy_setopt(curl, CURLOPT_URL, url);
66 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
67 if (no_ssl_fail)
68 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
69 if (auth_digest)
70 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
71 curl_easy_setopt(curl, CURLOPT_FILE, devnull);
72 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
73 curl_easy_setopt(curl, CURLOPT_USERPWD, authstring);
74 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
75 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
76 curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent);
77 if (strcmp(proxy, "undef"))
78 curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
79 result = curl_easy_perform(curl);
80 }
81
82 after = time(0);
83 /* num_connects jumps the gun earlier on */
84 // acs = stats(acs, after-before, num_connects-1);
85
86 if (!quiet) {
87 printf("%s\r", BLANKSPACE);
88 fflush(stdout);
89 }
90
91 debug(3, "curl_result[%i]: %s\n", result, error);
92
93 if (result == 6) {
94 fprintf(stderr, "%s\n", error);
95 exit(EXIT_FAILURE);
96 } else if (result == 5) {
97 fprintf(stderr, "%s\n", error);
98 exit(EXIT_FAILURE);
99 } else if (result == 0) {
100 return(EXIT_SUCCESS);
101 } else if (result == 22) {
102 return(EXIT_FAILURE);
103 }
104
105 return(EXIT_FAILURE);
106 }
107
108 void shutdown_submit(void) {
109 curl_easy_cleanup(curl);
110 }
111 #endif /* USE_CURL */
112
113 #ifdef USE_DUMMY
114 char **submit_dummy_list = NULL;
115
116 void initialize_submit(void) {}
117 void shutdown_submit(void) {}
118
119 int submit(char *username, char *password) {
120 int i=0;
121 char *path;
122 char *valid_username;
123 char *valid_password;
124
125 valid_username = (char*)malloc_w(sizeof(char)*81);
126
127 if (max_connects == num_connects && max_connects != 0) {
128 if (!quiet)
129 printf("max connects reached at %i\n", num_connects);
130 fprintf(logfd, "max connects reached at %i\n", num_connects);
131 exit(EXIT_SUCCESS);
132 }
133 num_connects++;
134
135 if (!submit_dummy_list) {
136 path = search_path(submit_dummy_file, pathlist);
137 submit_dummy_list = textlist(path);
138 }
139
140 if (!quiet && (debug_level == 0)) {
141 printf("trying %s->%s\r", username, password);
142 fflush(stdout);
143 } else if (debug_level > 0)
144 printf("trying %s->%s\n", username, password);
145
146 while (submit_dummy_list[i] != NULL) {
147 strncpy(valid_username, submit_dummy_list[i], sizeof(char)*81);
148 valid_password = extract(valid_username, ':');
149 if (!valid_password) {
150 fprintf(stderr, "submit_dummy: list element invalid\n");
151 exit(EXIT_FAILURE);
152 }
153 if (!strcmp(valid_username, username) && !strcmp(valid_password, password)) {
154 free(valid_username);
155 if (!quiet && (debug_level == 0)) {
156 printf("%s\r", BLANKSPACE);
157 fflush(stdout);
158 }
159 return(EXIT_SUCCESS);
160 }
161 i++;
162 }
163
164 if (!quiet && (debug_level == 0)) {
165 printf("%s\r", BLANKSPACE);
166 fflush(stdout);
167 }
168 free(valid_username);
169 return(EXIT_FAILURE);
170 }
171 #endif /* USE_DUMMY */