forked from certnanny/sscep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
274 lines (245 loc) · 5.73 KB
/
net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* sscep -- Simple SCEP client implementation
* Copyright (c) Jarkko Turkulainen 2003. All rights reserved.
* See the file COPYRIGHT for licensing information.
*/
/* HTTP routine */
#include "sscep.h"
#ifdef WIN32
void perror_w32 (const char *message)
{
char buffer[BUFSIZ];
/* letzten Fehlertext holen und formatieren */
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, (LPWSTR) buffer,
sizeof buffer, NULL);
fprintf(stderr, "%s: %s", message, buffer);
}
#define perror perror_w32
#endif
int
send_msg(struct http_reply *http,char *msg,char *host,int port,int operation) {
int sd, rc, used, bytes;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
char tmp[1024], *buf, *p;
#ifdef WIN32
int tv=timeout*1000;
#else
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
#endif
/* resolve name */
h = gethostbyname(host);
if (h == NULL) {
printf("unknown host '%s'\n", host);
return (1);
}
/* fill in server socket structure: */
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr,
h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(port);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) {
perror("cannot open socket ");
return (1);
}
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if (rc < 0) {
printf("cannot bind port TCP %u\n", port);
perror("error ");
return (1);
}
/* connect to server */
/* The two socket options SO_RCVTIMEO and SO_SNDTIMEO do not work with connect
connect has a default timeout of 120 */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if (rc < 0) {
perror("cannot connect");
return (1);
}
setsockopt(sd,SOL_SOCKET, SO_RCVTIMEO,(void *)&tv, sizeof(tv));
setsockopt(sd,SOL_SOCKET, SO_SNDTIMEO,(void *)&tv, sizeof(tv));
/* send data */
rc = send(sd, msg,strlen(msg), 0);
if (rc < 0) {
perror("cannot send data ");
close(sd);
return (1);
}
else if(rc != strlen(msg))
{
fprintf(stderr,"incomplete send\n");
close(sd);
return (1);
}
/* Get response */
buf = (char *)malloc(1024);
used = 0;
while ((bytes = recv(sd,&buf[used],1024,0)) > 0) {
used += bytes;
buf = (char *)realloc(buf, used + 1024);
}
if (bytes < 0) {
perror("error receiving data ");
close(sd);
return (1);
}
buf[used] = '\0';
/* Fetch the status code: */
#ifdef WIN32
sscanf(buf, "%s %d ", tmp, &http->status);
#else
sscanf(buf, "%s %d ", tmp, &http->status);
#endif
if (v_flag)
fprintf(stdout, "%s: server returned status code %d\n",
pname, http->status);
/* Set SCEP reply type */
switch (operation) {
case SCEP_OPERATION_GETCA:
if (strstr(buf, MIME_GETCA)) {
http->type = SCEP_MIME_GETCA;
if (v_flag)
printf("%s: MIME header: %s\n",
pname, MIME_GETCA);
} else if (strstr(buf, MIME_GETCA_RA) ||
strstr(buf, MIME_GETCA_RA_ENTRUST)) {
http->type = SCEP_MIME_GETCA_RA;
if (v_flag)
printf("%s: MIME header: %s\n",
pname, MIME_GETCA_RA);
} else {
if (v_flag)
printf("%s: mime_err: %s\n", pname,buf);
goto mime_err;
}
break;
case SCEP_OPERATION_GETNEXTCA:
if (strstr(buf, MIME_GETNEXTCA)) {
http->type = SCEP_MIME_GETNEXTCA;
if (v_flag)
printf("%s: MIME header: %s\n",
pname, MIME_GETNEXTCA);
}else {
if (v_flag)
printf("%s: mime_err: %s\n", pname,buf);
goto mime_err;
}
break;
default:
if (!strstr(buf, MIME_PKI)) {
if (v_flag)
printf("%s: mime_err: %s\n", pname,buf);
goto mime_err;
}
http->type = SCEP_MIME_PKI;
if (v_flag)
printf("%s: MIME header: %s\n",pname,MIME_PKI);
break;
}
/* Find payload */
for (p = buf; *buf; buf++) {
if (!strncmp(buf, "\n\n", 2) && *(buf + 2)) {
http->payload = buf + 2;
break;
}
if (!strncmp(buf, "\n\r\n\r", 4) && *(buf + 4)) {
http->payload = buf + 4;
break;
}
if (!strncmp(buf, "\r\n\r\n", 4) && *(buf + 4)) {
http->payload = buf + 4;
break;
}
}
http->bytes = used - (http->payload - p);
if (http->payload == NULL) {
/* This is not necessarily error...
* XXXXXXXXXXXXXXXX check */
fprintf(stderr, "%s: cannot find data from http reply\n",pname);
}
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
return (0);
mime_err:
if (v_flag)
fprintf(stderr, "%s: wrong (or missing) MIME content type\n", pname);
return (1);
}
/* URL-encode the input and return back encoded string */
char * url_encode(char *s, size_t n) {
char *r;
size_t len;
unsigned int i;
char ch[2];
/* Allocate 2 times bigger space than the original string */
len = 2 * n;
r = (char *)malloc(len);
if (r == NULL) {
return NULL;
}
#ifdef WIN32
strcpy_s(r, sizeof(r), "");
#else
strcpy(r, "");
#endif
/* Copy data */
for (i = 0; i < n; i++) {
switch (*(s+i)) {
case '+':
#ifdef WIN32
//strncat_s(r, sizeof(r), "%2B", len);
strncat(r, "%2B", len);
#else
strncat(r, "%2B", len);
#endif
break;
case '-':
#ifdef WIN32
//strncat_s(r, sizeof(r), "%2D", len);
strncat(r, "%2D", len);
#else
strncat(r, "%2D", len);
#endif
break;
case '=':
#ifdef WIN32
//strncat_s(r, sizeof(r), "%3D", len);
strncat(r, "%3D", len);
#else
strncat(r, "%3D", len);
#endif
break;
case '\n':
#ifdef WIN32
//strncat_s(r, sizeof(r), "%0A", len);
strncat(r, "%0A", len);
#else
strncat(r, "%0A", len);
#endif
break;
default:
ch[0] = *(s+i);
ch[1] = '\0';
#ifdef WIN32
//strncat_s(r, sizeof(r), ch, len);
strncat(r, ch, len);
#else
strncat(r, ch, len);
#endif
break;
}
}
r[len-1] = '\0';
return r;
}