CURLcode curl_easy_perform(CURL *curl) { int rc; CURLcode err=CURLE_OK; const char *errstr=NULL; char *proxy=NULL; struct http_srv srv; memset(&srv,0,sizeof(srv)); /* Emulate the libcurl proxy behavior. If the calling program set a proxy, use it. If it didn't set a proxy or set it to NULL, check for one in the environment. If the calling program explicitly set a null-string proxy the http code doesn't use a proxy at all. */ if(curl->proxy) proxy=curl->proxy; else proxy=getenv(HTTP_PROXY_ENV); if(curl->srvtag) srv.srvtag=curl->srvtag; if(curl->flags.verbose) { fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null"); fprintf(curl->errors,"* HTTP URL is \"%s\"\n",curl->url); if(srv.srvtag) fprintf(curl->errors, "* SRV tag is \"%s\": host and port may be overridden\n", srv.srvtag); fprintf(curl->errors,"* HTTP auth is \"%s\"\n", curl->auth?curl->auth:"null"); fprintf(curl->errors,"* HTTP method is %s\n", curl->flags.post?"POST":"GET"); } if(curl->flags.post) { rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy, &srv,curl->headers?curl->headers->list:NULL); if(rc==0) { char content_len[50]; unsigned int post_len=strlen(curl->postfields); if(curl->flags.verbose && srv.used_server && srv.used_port) fprintf (curl->errors, "* HTTP host:port post-SRV is \"%s:%hu\"\n", srv.used_server, srv.used_port); iobuf_writestr(curl->hd.fp_write, "Content-Type: application/x-www-form-urlencoded\r\n"); sprintf(content_len,"Content-Length: %u\r\n",post_len); iobuf_writestr(curl->hd.fp_write,content_len); http_start_data(&curl->hd); iobuf_write(curl->hd.fp_write,curl->postfields,post_len); rc=http_wait_response(&curl->hd,&curl->status); if(rc==0 && curl->flags.failonerror && curl->status>=300) err=CURLE_HTTP_RETURNED_ERROR; } } else { rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,proxy, &srv,curl->headers?curl->headers->list:NULL); if(rc==0) { if(curl->flags.verbose && srv.used_server && srv.used_port) fprintf (curl->errors, "* HTTP host:port post-SRV is \"%s:%hu\"\n", srv.used_server, srv.used_port); rc=http_wait_response(&curl->hd,&curl->status); if(rc==0) { if(curl->flags.failonerror && curl->status>=300) err=CURLE_HTTP_RETURNED_ERROR; else { unsigned int maxlen=1024,buflen,len; byte *line=NULL; while((len=iobuf_read_line(curl->hd.fp_read, &line,&buflen,&maxlen))) { size_t ret; maxlen=1024; ret=(curl->writer)(line,len,1,curl->file); if(ret!=len) { err=CURLE_WRITE_ERROR; break; } } xfree(line); http_close(&curl->hd); } } else http_close(&curl->hd); } } free (srv.used_server); switch(rc) { case 0: break; case G10ERR_INVALID_URI: err=CURLE_UNSUPPORTED_PROTOCOL; break; case G10ERR_NETWORK: errstr=strerror(errno); err=CURLE_COULDNT_CONNECT; break; default: errstr=g10_errstr(rc); err=CURLE_COULDNT_CONNECT; break; } return handle_error(curl,err,errstr); }
static int get_key (char *getkey) { int rc; int sock; iobuf_t fp_read; unsigned int maxlen, buflen, gotit=0; byte *line = NULL; if (strncmp (getkey,"0x",2)==0) getkey+=2; /* Frankly we don't know what keys the server will return; we indicated the requested key anyway. */ fprintf(output,"KEY 0x%s BEGIN\n",getkey); rc=send_request(opt->opaque,&sock); if(rc) { fprintf(output,"KEY 0x%s FAILED %d\n",getkey, rc); sock_close (sock); return KEYSERVER_OK; } /* Hmmm, we use iobuf here only to cope with Windows socket peculiarities (we can't used fdopen). */ fp_read = iobuf_sockopen (sock , "r"); if (!fp_read) { fprintf(output,"KEY 0x%s FAILED %d\n",getkey, KEYSERVER_INTERNAL_ERROR); sock_close (sock); return KEYSERVER_OK; } while ( iobuf_read_line ( fp_read, &line, &buflen, &maxlen)) { maxlen=1024; if(gotit) { print_nocr(output, (const char*)line); if (!strncmp((char*)line,END,strlen(END))) break; } else if(!strncmp((char*)line,BEGIN,strlen(BEGIN))) { print_nocr(output, (const char*)line); gotit=1; } } if(gotit) fprintf (output,"KEY 0x%s END\n", getkey); else { fprintf(console,"gpgkeys: no key data found for finger:%s\n", opt->opaque); fprintf(output,"KEY 0x%s FAILED %d\n",getkey,KEYSERVER_KEY_NOT_FOUND); } xfree(line); iobuf_close (fp_read); return KEYSERVER_OK; }
/**************** * Copy data from INP to OUT and do some escaping if requested. * md is updated as required by rfc2440 */ int copy_clearsig_text( IOBUF out, IOBUF inp, gcry_md_hd_t md, int escape_dash, int escape_from) { unsigned int maxlen; byte *buffer = NULL; /* malloced buffer */ unsigned int bufsize; /* and size of this buffer */ unsigned int n; int truncated = 0; int pending_lf = 0; if( !escape_dash ) escape_from = 0; write_status_begin_signing (md); for(;;) { maxlen = MAX_LINELEN; n = iobuf_read_line( inp, &buffer, &bufsize, &maxlen ); if( !maxlen ) truncated++; if( !n ) break; /* read_line has returned eof */ /* update the message digest */ if( escape_dash ) { if( pending_lf ) { gcry_md_putc ( md, '\r' ); gcry_md_putc ( md, '\n' ); } gcry_md_write ( md, buffer, len_without_trailing_chars (buffer, n, " \t\r\n")); } else gcry_md_write ( md, buffer, n ); pending_lf = buffer[n-1] == '\n'; /* write the output */ if( ( escape_dash && *buffer == '-') || ( escape_from && n > 4 && !memcmp(buffer, "From ", 5 ) ) ) { iobuf_put( out, '-' ); iobuf_put( out, ' ' ); } #if 0 /*defined(HAVE_DOSISH_SYSTEM)*/ /* We don't use this anymore because my interpretation of rfc2440 7.1 * is that there is no conversion needed. If one decides to * clearsign a unix file on a DOS box he will get a mixed line endings. * If at some point it turns out, that a conversion is a nice feature * we can make an option out of it. */ /* make sure the lines do end in CR,LF */ if( n > 1 && ( (buffer[n-2] == '\r' && buffer[n-1] == '\n' ) || (buffer[n-2] == '\n' && buffer[n-1] == '\r'))) { iobuf_write( out, buffer, n-2 ); iobuf_put( out, '\r'); iobuf_put( out, '\n'); } else if( n && buffer[n-1] == '\n' ) { iobuf_write( out, buffer, n-1 ); iobuf_put( out, '\r'); iobuf_put( out, '\n'); } else iobuf_write( out, buffer, n ); #else iobuf_write( out, buffer, n ); #endif } /* at eof */ if( !pending_lf ) { /* make sure that the file ends with a LF */ iobuf_writestr( out, LF ); if( !escape_dash ) gcry_md_putc( md, '\n' ); } if( truncated ) log_info(_("input line longer than %d characters\n"), MAX_LINELEN ); return 0; /* okay */ }
static int standard( text_filter_context_t *tfx, IOBUF a, byte *buf, size_t size, size_t *ret_len) { int rc=0; size_t len = 0; unsigned maxlen; log_assert( size > 10 ); size -= 2; /* reserve 2 bytes to append CR,LF */ while( !rc && len < size ) { int lf_seen; while( len < size && tfx->buffer_pos < tfx->buffer_len ) buf[len++] = tfx->buffer[tfx->buffer_pos++]; if( len >= size ) continue; /* read the next line */ maxlen = MAX_LINELEN; tfx->buffer_pos = 0; tfx->buffer_len = iobuf_read_line( a, &tfx->buffer, &tfx->buffer_size, &maxlen ); if( !maxlen ) tfx->truncated++; if( !tfx->buffer_len ) { if( !len ) rc = -1; /* eof */ break; } lf_seen = tfx->buffer[tfx->buffer_len-1] == '\n'; /* The story behind this is that 2440 says that textmode hashes should canonicalize line endings to CRLF and remove spaces and tabs. 2440bis-12 says to just canonicalize to CRLF. 1.4.0 was released using the bis-12 behavior, but it was discovered that many mail clients do not canonicalize PGP/MIME signature text appropriately (and were relying on GnuPG to handle trailing spaces). So, we default to the 2440 behavior, but use the 2440bis-12 behavior if the user specifies --no-rfc2440-text. The default will be changed at some point in the future when the mail clients have been upgraded. Aside from PGP/MIME and broken mail clients, this makes no difference to any signatures in the real world except for a textmode detached signature. PGP always used the 2440bis-12 behavior (ignoring 2440 itself), so this actually makes us compatible with PGP textmode detached signatures for the first time. */ if(opt.rfc2440_text) tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len, " \t\r\n"); else tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len, "\r\n"); if( lf_seen ) { tfx->buffer[tfx->buffer_len++] = '\r'; tfx->buffer[tfx->buffer_len++] = '\n'; } } *ret_len = len; return rc; }
int main (int argc, char *argv[]) { (void) argc; (void) argv; /* A simple test to make sure filters work. We use a static buffer and then add a filter in front of it that returns every other character. */ { char *content = "0123456789abcdefghijklm"; iobuf_t iobuf; int c; int n; int rc; iobuf = iobuf_temp_with_content (content, strlen (content)); rc = iobuf_push_filter (iobuf, every_other_filter, NULL); assert (rc == 0); n = 0; while ((c = iobuf_readbyte (iobuf)) != -1) { /* printf ("%d: %c\n", n + 1, (char) c); */ assert (content[2 * n + 1] == c); n ++; } /* printf ("Got EOF after reading %d bytes (content: %d)\n", */ /* n, strlen (content)); */ assert (n == strlen (content) / 2); iobuf_close (iobuf); } /* A simple test to check buffering. Make sure that when we add a filter to a pipeline, any buffered data gets processed by the */ { char *content = "0123456789abcdefghijklm"; iobuf_t iobuf; int c; int n; int rc; int i; iobuf = iobuf_temp_with_content (content, strlen (content)); n = 0; for (i = 0; i < 10; i ++) { c = iobuf_readbyte (iobuf); assert (content[i] == c); n ++; } rc = iobuf_push_filter (iobuf, every_other_filter, NULL); assert (rc == 0); while ((c = iobuf_readbyte (iobuf)) != -1) { /* printf ("%d: %c\n", n + 1, (char) c); */ assert (content[2 * (n - 5) + 1] == c); n ++; } assert (n == 10 + (strlen (content) - 10) / 2); iobuf_close (iobuf); } /* A simple test to check that iobuf_read_line works. */ { /* - 3 characters plus new line - 4 characters plus new line - 5 characters plus new line - 5 characters, no new line */ char *content = "abc\ndefg\nhijkl\nmnopq"; iobuf_t iobuf; byte *buffer; unsigned size; unsigned max_len; int n; iobuf = iobuf_temp_with_content (content, strlen(content)); /* We read a line with 3 characters plus a newline. If we allocate a buffer that is 5 bytes long, then no reallocation should be required. */ size = 5; buffer = malloc (size); assert (buffer); max_len = 100; n = iobuf_read_line (iobuf, &buffer, &size, &max_len); assert (n == 4); assert (strcmp (buffer, "abc\n") == 0); assert (size == 5); assert (max_len == 100); free (buffer); /* We now read a line with 4 characters plus a newline. This requires 6 bytes of storage. We pass a buffer that is 5 bytes large and we allow the buffer to be grown. */ size = 5; buffer = malloc (size); max_len = 100; n = iobuf_read_line (iobuf, &buffer, &size, &max_len); assert (n == 5); assert (strcmp (buffer, "defg\n") == 0); assert (size >= 6); /* The string shouldn't have been truncated (max_len == 0). */ assert (max_len == 100); free (buffer); /* We now read a line with 5 characters plus a newline. This requires 7 bytes of storage. We pass a buffer that is 5 bytes large and we don't allow the buffer to be grown. */ size = 5; buffer = malloc (size); max_len = 5; n = iobuf_read_line (iobuf, &buffer, &size, &max_len); assert (n == 4); /* Note: the string should still have a trailing \n. */ assert (strcmp (buffer, "hij\n") == 0); assert (size == 5); /* The string should have been truncated (max_len == 0). */ assert (max_len == 0); free (buffer); /* We now read a line with 6 characters without a newline. This requires 7 bytes of storage. We pass a NULL buffer and we don't allow the buffer to be grown larger than 5 bytes. */ size = 5; buffer = NULL; max_len = 5; n = iobuf_read_line (iobuf, &buffer, &size, &max_len); assert (n == 4); /* Note: the string should still have a trailing \n. */ assert (strcmp (buffer, "mno\n") == 0); assert (size == 5); /* The string should have been truncated (max_len == 0). */ assert (max_len == 0); free (buffer); iobuf_close (iobuf); } { /* - 10 characters, EOF - 17 characters, EOF */ char *content = "abcdefghijklmnopq"; char *content2 = "0123456789"; iobuf_t iobuf; int rc; int c; int n; int lastc = 0; struct content_filter_state *state; iobuf = iobuf_temp_with_content (content, strlen(content)); rc = iobuf_push_filter (iobuf, content_filter, state=content_filter_new (content2)); assert (rc == 0); n = 0; while (1) { c = iobuf_readbyte (iobuf); if (c == -1 && lastc == -1) { /* printf("Two EOFs in a row. Done.\n"); */ assert (n == 27); break; } lastc = c; if (c == -1) { /* printf("After %d bytes, got EOF.\n", n); */ assert (n == 10 || n == 27); } else { n ++; /* printf ("%d: '%c' (%d)\n", n, c, c); */ } } iobuf_close (iobuf); free (state); } /* Write some data to a temporary filter. Push a new filter. The already written data should not be processed by the new filter. */ { iobuf_t iobuf; int rc; char *content = "0123456789"; char *content2 = "abc"; char buffer[4096]; int n; iobuf = iobuf_temp (); assert (iobuf); rc = iobuf_write (iobuf, content, strlen (content)); assert (rc == 0); rc = iobuf_push_filter (iobuf, double_filter, NULL); assert (rc == 0); /* Include a NUL. */ rc = iobuf_write (iobuf, content2, strlen (content2) + 1); assert (rc == 0); n = iobuf_temp_to_buffer (iobuf, buffer, sizeof (buffer)); #if 0 printf ("Got %d bytes\n", n); printf ("buffer: `"); fwrite (buffer, n, 1, stdout); fputc ('\'', stdout); fputc ('\n', stdout); #endif assert (n == strlen (content) + 2 * (strlen (content2) + 1)); assert (strcmp (buffer, "0123456789aabbcc") == 0); iobuf_close (iobuf); } { iobuf_t iobuf; int rc; char *content = "0123456789"; int n; int c; char buffer[strlen (content)]; iobuf = iobuf_temp_with_content (content, strlen (content)); assert (iobuf); rc = iobuf_push_filter (iobuf, every_other_filter, NULL); assert (rc == 0); rc = iobuf_push_filter (iobuf, every_other_filter, NULL); assert (rc == 0); for (n = 0; (c = iobuf_get (iobuf)) != -1; n ++) { /* printf ("%d: `%c'\n", n, c); */ buffer[n] = c; } assert (n == 2); assert (buffer[0] == '3'); assert (buffer[1] == '7'); iobuf_close (iobuf); } return 0; }