[Winpt-commits] r314 - trunk/Src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun May 13 11:44:04 CEST 2007


Author: twoaday
Date: 2007-05-13 11:44:03 +0200 (Sun, 13 May 2007)
New Revision: 314

Modified:
   trunk/Src/wptHTTP.cpp
   trunk/Src/wptKeyEditDlgs.cpp
   trunk/Src/wptKeyManager.cpp
   trunk/Src/wptKeygenDlg.cpp
Log:


Modified: trunk/Src/wptHTTP.cpp
===================================================================
--- trunk/Src/wptHTTP.cpp	2007-05-13 09:43:51 UTC (rev 313)
+++ trunk/Src/wptHTTP.cpp	2007-05-13 09:44:03 UTC (rev 314)
@@ -1,5 +1,5 @@
 /* wptHTTP.cpp - Generic HTTP support
- *      Copyright (C) 2004, 2005, 2006, 2007 Timo Schulz
+ *      Copyright (C) 2004-2007 Timo Schulz
  *
  * This file is part of WinPT.
  *
@@ -28,17 +28,18 @@
 #include "wptHTTP.h"
 #include "wptTypes.h"
 #include "wptErrors.h"
+#include "StringBuffer.h"
 
 
 /* Empty constructur to allow advanced requests. */
-wHTTP::wHTTP (void)
+NetHTTP::NetHTTP (void)
 {
     reset ();
 }
 
 
 /* Constructur to allow alternative syntax. */
-wHTTP::wHTTP (const char *_host, int _port, const char *_url)
+NetHTTP::NetHTTP (const char *_host, int _port, const char *_url)
 {
     reset ();
     this->host = strdup (_host);
@@ -51,37 +52,38 @@
 
 
 /* Standard constructur. */
-wHTTP::wHTTP (const char *_url)
+NetHTTP::NetHTTP (const char *_url)
 {
     reset ();
     open (_url);
 }
 
-
-/* Open a connection to the given URL. */
+/* Open a connection to the given URL @_url. */
 int
-wHTTP::open (const char *_url)
+NetHTTP::open (const char *_url)
 {
-    /* In case the function is used more than once, we
-       need to release the old structures first. */
-    releaseHeaders ();
-    
+    int err;
+
+    /* A connection were privously established. */
+    if (this->fd != 0)
+	return 0;
+
     safe_free (this->host);
     safe_free (this->url);
-    
-    if (extractHostInfo (_url, &this->host, &this->url))
-	return -1;
-    
-    if (!sendRequest (this->host, this->port, this->url))
-	return parseResponse (&statcode);
-    
-    return 0;
+
+    err = extractHostInfo (_url, &this->host, &this->url);
+    if (err)
+	return err;
+    err = sendRequest (this->host, this->port, this->url);
+    if (err)
+	return err;
+    return parseResponse (&statcode);
 }
 
 
 /* Reset object contents for first use. */
 void
-wHTTP::reset (void)
+NetHTTP::reset (void)
 {
     ver = 1;
     port = 80;
@@ -96,7 +98,7 @@
 
 /* Perform a HTTP 'HEAD' request. */
 int
-wHTTP::head (const char *_url)
+NetHTTP::head (const char *_url)
 {
     int err;
 
@@ -106,23 +108,23 @@
     err = extractHostInfo (_url, &this->host, &this->url);
     if (err)
 	return err;
-    
+
     method = HTTP_HEAD;
-    if (!sendRequest (this->host, this->port, this->url))
-	parseResponse (&statcode);
+    if (!sendRequest (host, port, url))
+	err = parseResponse (&statcode);
 
-    return 0;
+    return err;
 }
 
 
 /* Perform a HTTP 'GET' request. */
 int
-wHTTP::get (const char *_url)
+NetHTTP::get (const char *_url)
 {
     int err;
 
-    safe_free (this->host);
-    safe_free (this->url);
+    safe_free (host);
+    safe_free (url);
 
     err = extractHostInfo (_url, &this->host, &this->url);
     if (err)
@@ -130,15 +132,15 @@
 
     method = HTTP_GET;
     if (!sendRequest (this->host, this->port, this->url))
-	parseResponse (&statcode);
+	err = parseResponse (&statcode);
 
-    return 0;
+    return err;
 }
 
 
 /* Return HTTP status code. */
 int 
-wHTTP::getStatusCode (void)
+NetHTTP::getStatusCode (void)
 {
     return statcode;
 }
@@ -146,7 +148,7 @@
 
 /* Return MIME content type. */
 const char*
-wHTTP::getContentType (void)
+NetHTTP::getContentType (void)
 {
     const char *type = NULL;
 
@@ -157,7 +159,7 @@
 
 /* Return content length. */
 unsigned int
-wHTTP::getContentLength (void)
+NetHTTP::getContentLength (void)
 {
     const char *len = NULL;
 
@@ -168,7 +170,7 @@
 
 
 void
-wHTTP::addHeader (http_head_t *root, const char *val)
+NetHTTP::addHeader (http_head_t *root, const char *val)
 {
     http_head_t n, t;
     
@@ -188,8 +190,8 @@
 
 
 bool
-wHTTP::findHeader (http_head_t root, 
-		   const char *name, const char **val)
+NetHTTP::findHeader (http_head_t root, 
+		     const char *name, const char **val)
 {
     http_head_t n;
     char *p;
@@ -208,7 +210,7 @@
 
 
 int
-wHTTP::connect (const char *_host, int _port)
+NetHTTP::connect (const char *_host, int _port)
 {
     struct hostent *hp;
     struct sockaddr_in srv;
@@ -250,7 +252,7 @@
 	return WPTERR_WINSOCK_SOCKET;
     }
     
-    if (::connect (fd, (struct sockaddr *)&srv, sizeof srv)) {
+    if (::connect (fd, (struct sockaddr *)&srv, sizeof (srv))) {
 	closesocket (fd);
 	fd = 0;
 	this->error = (int)WSAGetLastError ();
@@ -261,8 +263,9 @@
 }
 
 
+/* Return 0 if the next recv() call would be block. */
 int
-wHTTP::isDataAvailable (int _fd)
+NetHTTP::isDataAvailable (int _fd)
 {
     struct timeval tv;
     fd_set inp;
@@ -280,8 +283,11 @@
 }
 
 
+/* Read a line from the open socket. A line means
+   a sequence of octets terminated with a \r\n.
+   Return value: 0 on success. */
 int
-wHTTP::readLine (char *buf, unsigned int nbuf,
+NetHTTP::readLine (char *buf, unsigned int nbuf,
                  int nonblock, int *nn, int *eof)
 {
     char c;
@@ -331,7 +337,7 @@
 /* Extract the host from the given url @url. Return the host in
    @host and also return the resource part of the url in @new_url. */
 int
-wHTTP::extractHostInfo (const char *_url, char **_host, char **new_url)
+NetHTTP::extractHostInfo (const char *_url, char **_host, char **new_url)
 {   
     char tmpbuf[2*MAX_PATH+1];
     char *p;
@@ -344,7 +350,7 @@
     strncpy (tmpbuf, _url, DIM (tmpbuf)-1);
     
     p = "http://";
-    if (strlen (_url) < 10 || strncmp (_url, p, 7))
+    if (strlen (_url) < 10 || strncmp (_url, p, strlen (p)))
         return WPTERR_GENERAL;
     _url += strlen (p);
     p = strtok (tmpbuf+7, "/");
@@ -352,32 +358,27 @@
         return WPTERR_GENERAL;
     *_host = strdup (p);   
     p = strchr (_url, '/');
-    if (!p) /* document were given so we assume the root '/'. */
-	*new_url = strdup ("/");
-    else
-	*new_url = strdup (p);
+    /* if no document were given so we assume the root '/'. */
+    *new_url = !p? strdup ("/") : strdup (p);   
+
     return 0;
 }
 
 
 /* Add a request header. */
 int 
-wHTTP::addRequestHeader (const char *name, const char *val)
+NetHTTP::addRequestHeader (const char *name, const char *val)
 {
-    char *p;
-    char *fmt = "%s: %s";
+    StringBuffer p;
 
-    p = (char*) malloc (strlen (name)+strlen (val)+strlen (fmt)+4);
-    sprintf (p, fmt, name, val);
-    addHeader (&req_headers, p);
-    safe_free (p);
-
+    p = p + name + ": " + val;
+    addHeader (&req_headers, p.getBuffer ());
     return 0;
 }
 
 
 void 
-wHTTP::setVersion (int _ver)
+NetHTTP::setVersion (int _ver)
 {
     if (_ver > 9)
 	_ver = 0;
@@ -386,7 +387,7 @@
 
 
 int
-wHTTP::addRequestHeader (const char *name, unsigned int val)
+NetHTTP::addRequestHeader (const char *name, unsigned int val)
 {
     char buf[32];
 
@@ -398,17 +399,15 @@
 /* Prepare the request resource @url and send 
    it to host @host (port @port). */
 int
-wHTTP::sendRequest (const char *_host, int _port, const char *_url)
+NetHTTP::sendRequest (const char *_host, int _port, const char *_url)
 {
     const char *id[] = {"GET", "HEAD", "PUT", "POST"};
     http_head_t h;
-    const char *h_head;
-    char *p;
+    StringBuffer out;
     int n;
-    int rc;
 
-    if (!this->fd) {
-	rc = connect (_host, _port);
+    if (this->fd == 0) {
+	int rc = connect (_host, _port);
 	if (rc)
 	    return rc;
     }
@@ -422,30 +421,20 @@
     if (ver < 1)
 	addRequestHeader ("Connection", "close");
 
-    n = 0;
+    out = out + id[method] + " " + "/" + _url + " " + "HTTP/1." + (int)ver + "\r\n";
     for (h = req_headers; h; h = h->next)
-	n += strlen (h->d) + 2 + 1;
-    h_head = "%s /%s HTTP/1.%d\r\n";
-    n += strlen (id[method]) + strlen (h_head)
-	 + strlen (_url) + n + 2 + 1 + 4;
-    p = (char*)calloc (1, n+1);
-    if (!p)
-	BUG (0);
-    sprintf (p, h_head, id[method], _url, ver);
-    for (h = req_headers; h; h = h->next) {
-	strcat (p, h->d);
-	strcat (p, "\r\n");
-    }
-    strcat (p, "\r\n");
-    n = send (fd, p, strlen (p), 0);
-    safe_free (p);
+	out = out + h->d + "\r\n";
+    out = out + "\r\n";
+
+    n = write (out.getBuffer (), strlen (out.getBuffer ()));
+    log_box ("debug", 0, "%d", n);
     return n > 0? 0 : WPTERR_GENERAL;
 }
 
 
 /* Parse all response resp_headers. */
 int
-wHTTP::parseHeaders (http_head_t *r_head)
+NetHTTP::parseHeaders (http_head_t *r_head)
 {
     char buf[300];
     int nn;
@@ -466,13 +455,17 @@
 }
 
 
+/* Default buffer size */
+#define BUFSIZE 1024
+
+
 /* Read data from the response and write it to @out. */
 int
-wHTTP::readData (FILE *out)
+NetHTTP::readData (FILE *out)
 {
     const char *val;
-    char buf[1024+1];
-    int nlen = 0, nn = 0, n, eof=0;
+    char buf[BUFSIZE+1];
+    int nlen, nn = 0, n, eof=0;
     int rc = 0;
     
     if (this->fd == 0 || this->resp_headers == NULL)
@@ -490,7 +483,7 @@
         return WPTERR_GENERAL;
     if (strnicmp (val, "text", 4)) { /* binary */
 	do {
-	    n = recv (fd, buf, nlen > 1024? 1024 : nlen, 0);
+	    n = recv (fd, buf, nlen > BUFSIZE? BUFSIZE : nlen, 0);
 	    if (n == -1) {
 		this->error = (int)WSAGetLastError ();
 		return SOCKET_ERROR;
@@ -522,7 +515,7 @@
 
 /* Parse the HTTP response line. */
 int
-wHTTP::parseResponse (int *_statcode)
+NetHTTP::parseResponse (int *_statcode)
 {
     http_head_t n;
     const char *tmp, *p;
@@ -531,7 +524,7 @@
     int rc;
 
     *_statcode = 0;
-    rc = readLine (buf, 299, 1, &nn, NULL);
+    rc = readLine (buf, DIM (buf)-1, 1, &nn, NULL);
     if (rc)
 	return rc;
 
@@ -557,37 +550,30 @@
 	resp_headers = n;
     }
     resp_headers = NULL;
-    return parseHeaders (&resp_headers);
+    rc = parseHeaders (&resp_headers);
+    if (rc)
+	return rc;
+    return 0;
 }
 
 
-/* Release all used structures. */
-void
-wHTTP::releaseHeaders (void)
+/* Destroy HTTP object. */
+NetHTTP::~NetHTTP (void)
 {
     http_head_t h;
     
-    while (resp_headers != NULL) {
+    while (resp_headers) {
 	h = resp_headers->next;
 	free (resp_headers);
 	resp_headers = h;
     }
-    resp_headers = NULL;
-    while (req_headers != NULL) {
+    while (req_headers) {
 	h = req_headers->next;
 	free (req_headers);
 	req_headers = h;
     }
-    req_headers = NULL;
-}
-
-
-/* Destroy HTTP object. */
-wHTTP::~wHTTP (void)
-{
-    releaseHeaders();
-    safe_free (host);
     safe_free (url);
+    safe_free (host);
     if (fd != 0) {
 	closesocket (fd);
 	fd = 0;
@@ -597,7 +583,7 @@
 
 /* Read @buflen bytes from the http stream into the buffer @buf. */
 int
-wHTTP::read (void *buf, unsigned int buflen)
+NetHTTP::read (void *buf, unsigned int buflen)
 {
     int n;
 
@@ -616,26 +602,39 @@
     n = recv (fd, (char*)buf, (int)buflen, 0);
     if (n > 0) {
 	nleft -= n;
-	if (nleft < 0) nleft = 0;
+	if (nleft < 0) 
+	    nleft = 0;
     }
     return n;
 }
 
 
-/* Write the buffer @buf to the http stream. */
+/* Write the buffer @buf to the http stream.
+   The function should be considered general as a replacement
+   for send() to make sure the entire buffer is written. */
 int
-wHTTP::write (const void *buf, unsigned buflen)
+NetHTTP::write (const void *buf, unsigned int buflen)
 {
+    const char *buffer = (const char*)buf;
+    unsigned int bytesleft = buflen;
+    int nwritten;
+
     if (this->fd == 0)
 	return -1;
-    if (method == HTTP_GET || method == HTTP_HEAD)
-	return -1;
-    return send (fd, (const char*)buf, (int)buflen, 0);
+
+    while (bytesleft > 0) {
+	nwritten = send (this->fd, buffer, bytesleft, 0);
+	if (nwritten == -1)
+	    return -1;
+	bytesleft -= nwritten;
+	buffer += nwritten;
+    }
+    return buflen;
 }
 
 
 /* Return the Winsock specific error code. */
-int wHTTP::getErrorCode (void)
+int NetHTTP::getErrorCode (void)
 {
     return error;
 }

Modified: trunk/Src/wptKeyEditDlgs.cpp
===================================================================
--- trunk/Src/wptKeyEditDlgs.cpp	2007-05-13 09:43:51 UTC (rev 313)
+++ trunk/Src/wptKeyEditDlgs.cpp	2007-05-13 09:44:03 UTC (rev 314)
@@ -78,7 +78,7 @@
     {"DELKEY", 1, CMD_DELKEY},
     {"EXPIRE", 1, CMD_EXPIRE},
     {"SHOWPREF", 0, CMD_SHOWPREF},
-    {"SETPREF", 1, CMD_SETPREF},
+    /*{"SETPREF", 1, CMD_SETPREF},*/
     {"PASSWD", 1, CMD_PASSWD},
     {"PRIMARY", 1, CMD_PRIMARY},
     {"TRUST", 0, CMD_TRUST},
@@ -1242,7 +1242,7 @@
 	 "ADDREVOKER  add a revocation key\r\n"
 	 "EXPIRE      change the expire date\r\n"
 	 "SHOWPREF    list preferences (verbose)\r\n"
-	 "SETPREF     update specific algorithm preferences\r\n"
+	 /*"SETPREF     update specific algorithm preferences\r\n"*/
 	 "PASSWD      change the passphrase\r\n"
 	 "PRIMARY     flag user ID as primary\r\n"
 	 "TRUST       change the ownertrust\r\n"

Modified: trunk/Src/wptKeyManager.cpp
===================================================================
--- trunk/Src/wptKeyManager.cpp	2007-05-13 09:43:51 UTC (rev 313)
+++ trunk/Src/wptKeyManager.cpp	2007-05-13 09:44:03 UTC (rev 314)
@@ -468,7 +468,7 @@
 km_http_import (HWND dlg, const char *url)
 {
     FILE *fp;
-    wHTTP hd;
+    NetHTTP hd;
     char tmpfile[MAX_PATH+64];
     int rc = 0, err = 0;
 

Modified: trunk/Src/wptKeygenDlg.cpp
===================================================================
--- trunk/Src/wptKeygenDlg.cpp	2007-05-13 09:43:51 UTC (rev 313)
+++ trunk/Src/wptKeygenDlg.cpp	2007-05-13 09:44:03 UTC (rev 314)
@@ -1,5 +1,5 @@
 /* wptKeygenDlg.cpp - Key Generation dialog
- *	Copyright (C) 2000-2006 Timo Schulz
+ *	Copyright (C) 2000-2007 Timo Schulz
  *
  * This file is part of WinPT.
  *
@@ -12,10 +12,6 @@
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with WinPT; if not, write to the Free Software Foundation, 
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -29,11 +25,12 @@
 #include "wptNLS.h"
 #include "wptGPG.h"
 #include "wptCommonCtl.h"
-#include "wptContext.h" /* for passphrase_s */
+#include "wptContext.h"
 #include "wptDlgs.h"
 #include "wptW32API.h"
 #include "wptVersion.h"
 #include "wptErrors.h"
+#include "StringBuffer.h"
 
 
 /* All valid key generation combination. */
@@ -48,57 +45,6 @@
 };
 
 
-static const char key_params[] =
-	"<GnupgKeyParms format=\"internal\">\n"
-	"Key-Type: %s\n"
-	"Key-Usage: sign\n"
-	"Key-Length: %d\n"
-	"Subkey-Type: %s\n"
-	"Subkey-Length: %d\n"
-	"Name-Real: %s\n"	
-	"Name-Email: %s\n"
-	"Expire-Date: %s\n"
-	"Passphrase: %s\n"
-	"</GnupgKeyParms>\n";
-
-static const char key_params_with_comment[] = 
-	"<GnupgKeyParms format=\"internal\">\n"
-	"Key-Type: %s\n"
-	"Key-Usage: sign\n"
-	"Key-Length: %d\n"
-	"Subkey-Type: %s\n"
-	"Subkey-Length: %d\n"
-	"Name-Real: %s\n"
-	"Name-Comment: %s\n"
-	"Name-Email: %s\n"
-	"Expire-Date: %s\n"
-	"Passphrase: %s\n"
-	"</GnupgKeyParms>\n";
-
-static const char key_params_one[] = 
-	"<GnupgKeyParms format=\"internal\">\n"
-	"Key-Type: %s\n"
-	"Key-Length: %d\n"
-	"Key-Usage: %s\n"	
-	"Name-Real: %s\n"	
-	"Name-Email: %s\n"
-	"Expire-Date: %s\n"
-	"Passphrase: %s\n"
-	"</GnupgKeyParms>\n";
-
-static const char key_params_one_with_comment[] = 
-	"<GnupgKeyParms format=\"internal\">\n"
-	"Key-Type: %s\n"
-	"Key-Length: %d\n"
-	"Key-Usage: %s\n"	
-	"Name-Real: %s\n"	
-	"Name-Comment: %s\n"
-	"Name-Email: %s\n"
-	"Expire-Date: %s\n"
-	"Passphrase: %s\n"
-	"</GnupgKeyParms>\n";
-
-
 /* Generate the GPG specific genkey params with the given information.
    @keytype: See valid combinations.
    @bits: Length in bits.
@@ -109,121 +55,70 @@
    @passphrase: the actual passphrase.
    Return value: the gen. params. */
 static char*
-gpg_genkey_params (int keytype, int bits, 
-                   const char *user, const char *comment, const char *email,
-                   const char *expdate, const char *passphrase)
+gpg_genkey_params (int keytype, int bits,
+		    const char *user, const char *comment, const char *email,
+		    const char *expdate, const char *pass)
 {
-    char *p = NULL;
-    int addsize = strlen ("sign encrypt");
-    int size = 0;
-    
+    StringBuffer p;
+    char *param;
+
     if (keytype == GPG_KEYGEN_NONE)
         return NULL;
-    
-    size = strlen (user) + strlen (email) + strlen (passphrase) + addsize + 48;
-    if (comment && *comment)
-        size += strlen (key_params_with_comment) + strlen (comment);
-    else
-        size += strlen (key_params);
-    if (expdate)
-        size += strlen (expdate) + 1;
-    p = new char[size+1];
-    if (!p)
-        BUG (0);
-    /* XXX: simply the whole switch-case code. */
-    if (comment && *comment) {
-        switch( keytype ) {
-        case GPG_KEYGEN_DSA_ELG:
-            sprintf (p, key_params_with_comment,
-                     "DSA", 1024, "ELG-E", bits, user, comment, email,
-                     expdate ? expdate : "0", passphrase);
-            break;
-            
-        case GPG_KEYGEN_DSA_RSA:
-            sprintf( p, key_params_with_comment, 
-                     "DSA", 1024, "RSA", bits, user, comment, email,
-                     expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_DSA_SIG:
-            sprintf( p, key_params_one_with_comment, 
-                     "DSA", 1024, "sign",
-                     user, comment, email,
-                     expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_RSA_SIG:
-            sprintf( p, key_params_one_with_comment, 
-                     "RSA", bits, "sign",
-                     user, comment, email,
-                     expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_RSA:
-            sprintf( p, key_params_one_with_comment, 
-                    "RSA", bits, "sign encrypt",
-                    user, comment, email,
-                    expdate ? expdate : "0", passphrase );
-            break;
 
-	case GPG_KEYGEN_RSA_RSA:
-	    sprintf( p, key_params_with_comment,
-		     "RSA", bits, "RSA", bits, user, comment, email,
-		     expdate? expdate : "0", passphrase );
-	    break;
-            
-        default:
-	    free_if_alloc (p);
-            break;
-        }
+    p = "<GnupgKeyParms format=\"internal\">\n";
+    /* In this phase we set the primary key information fields. */
+    switch (keytype) {
+    case GPG_KEYGEN_DSA_ELG:
+    case GPG_KEYGEN_DSA_RSA:
+    case GPG_KEYGEN_DSA_SIG:
+	p = p + "Key-Type: DSA\n";
+	p = p + "Key-Usage: sign\n";
+	p = p + "Key-Length: 1024\n";
+	break;
+
+    case GPG_KEYGEN_RSA_SIG:
+    case GPG_KEYGEN_RSA:
+    case GPG_KEYGEN_RSA_RSA:
+	p = p + "Key-Type: RSA\n";
+	if (keytype == GPG_KEYGEN_RSA)
+	    p = p + "Key-Usage: sign encrypt\n";
+	else
+	    p = p + "Key-Usage: sign\n";
+	p = p + "Key-Length: " + (int)bits + "\n";
+	break;
+
+    default:
+	break;
     }
-    else {
-        switch ( keytype ) {
-        case GPG_KEYGEN_DSA_ELG:
-            sprintf( p, key_params,
-                    "DSA", 1024, "ELG-E", bits, user, email,
-                    expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_DSA_RSA:
-            sprintf( p, key_params,
-                    "DSA", 1024, "RSA", bits, user, email,
-                    expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_DSA_SIG:
-            sprintf( p, key_params_one, 
-                    "DSA", 1024, "sign",
-                    user, email,
-                    expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_RSA_SIG:
-            sprintf( p, key_params_one, 
-                    "RSA", bits, "sign",
-                    user, email,
-                    expdate ? expdate : "0", passphrase );
-            break;
-            
-        case GPG_KEYGEN_RSA:
-            sprintf( p, key_params_one, 
-                    "RSA", bits, "sign encrypt",
-                    user, email,
-                    expdate ? expdate : "0", passphrase );
-            break;
 
-	case GPG_KEYGEN_RSA_RSA:
-	    sprintf( p, key_params,
-		     "RSA", bits, "RSA", bits, user, email,
-		     expdate? expdate : "0", passphrase );
-	    break;
-            
-        default:
-	    free_if_alloc (p);
-            break;
-        }
+    /* The next phase is the subkey information if needed. */
+    if (keytype == GPG_KEYGEN_DSA_ELG || keytype == GPG_KEYGEN_DSA_RSA ||
+	keytype == GPG_KEYGEN_RSA_RSA) {
+	if (keytype == GPG_KEYGEN_DSA_ELG)
+	    p = p + "Subkey-Type: ELG-E\n";
+	else if (keytype == GPG_KEYGEN_DSA_RSA || keytype == GPG_KEYGEN_RSA_RSA)
+	    p = p + "Subkey-Type: RSA\n";
+	p = p + "Subkey-Usage: encrypt\n";
+	p = p + "Subkey-Length: " + (int)bits + "\n";
     }
-    return p;
+
+    /* Followed by the user ID information. */
+    p = p + "Name-Real: " + user + "\n";
+    if (comment != NULL)
+	p = p + "Name-Comment: " + comment + "\n";
+    p = p + "Name-Email: " + email + "\n";
+
+    if (expdate != NULL)
+	p = p + "Expire-Date: " + expdate + "\n";
+    else
+	p = p + "Expire-Date: 0\n";
+    p = p + "Passphrase: " + pass + "\n";
+
+    p = p + "</GnupgKeyParms>\n";
+    param = p.getBufferCopy ();
+    p.wipeContents ();
+
+    return param;
 }
 
 
@@ -254,9 +149,9 @@
 
 
 
-/* Clear all dialog fields. */
+/* Reset all dialog fields to its initial value. */
 static void 
-clear_dlg_fields (HWND dlg)
+reset_dlg_fields (HWND dlg)
 {
     SetDlgItemText (dlg, IDC_KEYGEN_SUBKEYBITS, "");
     SetDlgItemText (dlg, IDC_KEYGEN_NAME, "");
@@ -267,19 +162,17 @@
 
 
 /* Ask the user if a keyring backup is wanted and if so,
-   backup both keyrings to the selected folder. @dlg is
-   the handle of the parent window.*/
-static void
+   backup both keyrings to the selected folder. 
+   Return value: true on success. */
+static bool
 backup_keyrings (HWND dlg)
 {
     const char *name;
-    char *path = NULL;
-    char *keyring = NULL;
+    char *keyring;
+    char *path;
+    bool success = true;
     int id;
-    
-    path = get_gnupg_path ();
-    if (!path)
-	BUG (dlg);
+
     id = msg_box (dlg, 
                  _("It is STRONGLY recommend that you backup your keyrings because they both "
                    "contain VERY important data.\nRemember that your hard disk can crash or the "
@@ -287,27 +180,35 @@
                    "a different mass stoarge like a floppy or CDR!\n\n"
                    "Backup your keyrings now?"),
                  _("WARNING - Important hint" ), MB_YESNO);
-    if (id == IDYES) {
-        name = get_filesave_dlg (dlg, _("Destination for Public Keyring"), 
-				 NULL, "pubring_bak.gpg");
-        if( name ) {
-	    keyring = make_filename (path, "pubring", "gpg");
-            if (!CopyFile (keyring, name, FALSE))
-		log_box (_("Key Generation"), MB_ERR,
-		         _("Could not copy %s -> %s"), keyring, name);
-	    free_if_alloc (keyring);
-        }
-        name = get_filesave_dlg (dlg, _("Destination for Secret Keyring"), 
-				 NULL, "secring_bak.gpg");
-        if (name) {
-	    keyring = make_filename (path, "secring", "gpg");
-            if (!CopyFile (keyring, name, FALSE))
-		log_box (_("Key Generation"), MB_ERR,
+    if (id != IDYES)
+	return false;
+
+    path = get_gnupg_path ();
+    name = get_filesave_dlg (dlg, _("Destination for Public Keyring"), 
+			     NULL, "pubring_bak.gpg");
+    if (name != NULL) {
+	keyring = make_filename (path, "pubring", "gpg");
+	if (!CopyFile (keyring, name, FALSE)) {
+	    log_box (_("Key Generation"), MB_ERR,
+		     _("Could not copy %s -> %s"), keyring, name);
+	    success = false;
+	}
+	free_if_alloc (keyring);    
+    }
+
+    name = get_filesave_dlg (dlg, _("Destination for Secret Keyring"), 
+			     NULL, "secring_bak.gpg");
+    if (name != NULL) {
+	keyring = make_filename (path, "secring", "gpg");
+	if (!CopyFile (keyring, name, FALSE)) {
+	    log_box (_("Key Generation"), MB_ERR,
 			 _("Could not copy %s -> %s"), keyring, name);
-	    free_if_alloc (keyring);
-        }
+	    success = false;
+	}
+	free_if_alloc (keyring);   
     }
     free_if_alloc (path);
+    return success;
 }
 
 
@@ -355,9 +256,9 @@
     SYSTEMTIME st;
     gpgme_error_t err;
     char *utf8_name =NULL, *utf8_comment = NULL;
-    char email[128];
+    char email[128], t[64];
     char *pwd;
-    char t[64], *expire = NULL, *fpr=NULL;
+    char *expire = NULL, *fpr=NULL;
     int bits, use_comment, keytype = 0;
     int cancel = 0;
     char *p;
@@ -430,7 +331,7 @@
 		free_if_alloc (utf8_name);
 		return FALSE;
 	    }
-            if (!GetDlgItemText (dlg, IDC_KEYGEN_EMAIL, email, sizeof (email) -1)
+            if (!GetDlgItemText (dlg, IDC_KEYGEN_EMAIL, email, DIM (email) -1)
                 || check_email_address (email)) {
                 msg_box (dlg, _("Please enter a valid email address."),
 			 _("Key Generation"), MB_ERR);
@@ -462,8 +363,9 @@
                 expire = t;
 	    }
 
+	    /* We don't allow empty passphrases during key generation. */
             pwd = request_passphrase2 (_("Key Generation"), 
-				       PASSDLG_STRICT|PASSDLG_WARN_UTF8, &cancel);
+				       PASSDLG_STRICT|PASSDLG_WARN_UTF8|PASSDLG_NOTEMPTY, &cancel);
 	    if (cancel) {
 		sfree_if_alloc (pwd);
 		free_if_alloc (utf8_name);
@@ -478,18 +380,15 @@
                 return FALSE;
             }
 
-            if (!use_comment && !utf8_comment)
-                p = gpg_genkey_params (keytype, bits, utf8_name, NULL, 
-				       email, expire, pwd);
-	    else
-                p = gpg_genkey_params (keytype, bits, utf8_name, utf8_comment, 
-				       email, expire, pwd);
+	    p = gpg_genkey_params (keytype, bits, utf8_name, 
+				   !use_comment && !utf8_comment? NULL :utf8_comment,
+			           email, expire, pwd);
 	    free_if_alloc (utf8_name);
 	    free_if_alloc (utf8_comment);
             keygen_cb_dlg_create ();
             err = gpg_genkey (p, keygen_cb, &fpr);
 	    sfree_if_alloc (pwd);
-	    sfree_if_alloc (p); /* burn the passphrase! */
+	    sfree_if_alloc (p); /* burn the passphrase */
             keygen_cb_dlg_destroy (1);
             if (err) {
 		free_if_alloc (fpr);
@@ -502,7 +401,7 @@
 	    keycache_update (1, fpr);
 	    free_if_alloc (fpr);	    
 
-            clear_dlg_fields (dlg);
+            reset_dlg_fields (dlg);
             backup_keyrings (dlg);
 	    if (ctx)
 		ctx->cancel = 0;
@@ -563,7 +462,7 @@
 		free_if_alloc (utf8_name);
 		return FALSE;
 	    }
-	    if (!GetDlgItemText(dlg, IDC_KEYWIZARD_EMAIL, email, sizeof email-1 )
+	    if (!GetDlgItemText (dlg, IDC_KEYWIZARD_EMAIL, email, DIM (email)-1)
 		|| check_email_address (email)) { 
 		msg_box (dlg, _("Please enter a valid email address."), 
 		         _("Key Generation Wizard"), MB_ERR);
@@ -576,8 +475,10 @@
 		free_if_alloc (utf8_name);
 		return FALSE;
 	    }
-	    pass = request_passphrase2 (_("Key Generation"), 
-					PASSDLG_STRICT|PASSDLG_WARN_UTF8, &cancel);
+
+	    /* We don't allow empty passphrases during key generation. */
+	    pass = request_passphrase2 (_("Key Generation"),
+					PASSDLG_STRICT|PASSDLG_WARN_UTF8|PASSDLG_NOTEMPTY, &cancel);
 	    if (cancel) {
 		free_if_alloc (utf8_name);
 		return FALSE;
@@ -585,7 +486,7 @@
 	    if (IsDlgButtonChecked (dlg, IDC_KEYWIZARD_USERSA))
 		pubkey_algo = GPG_KEYGEN_DSA_RSA;
 	    p = gpg_genkey_params (pubkey_algo, DFAULT_KEYSIZE, utf8_name, 
-				     NULL, email, NULL, pass);
+				   NULL, email, NULL, pass);
 	    free_if_alloc (utf8_name);
 	    keygen_cb_dlg_create();
 	    err = gpg_genkey (p, keygen_cb, &fpr);
@@ -593,7 +494,8 @@
 	    sfree_if_alloc (p);
 	    sfree_if_alloc (pass);
 	    if (err) {
-                msg_box (dlg, gpgme_strerror (err), _("Key Generation Wizard"), MB_ERR);
+                msg_box (dlg, gpgme_strerror (err), 
+			 _("Key Generation Wizard"), MB_ERR);
 		free_if_alloc (fpr);
                 return FALSE;
             }
@@ -603,13 +505,13 @@
 	    free_if_alloc (fpr);
 
 	    backup_keyrings (dlg);
-	    if (ctx)
+	    if (ctx != NULL)
 		ctx->cancel = 0;
 	    EndDialog (dlg, TRUE);
 	    break;
 
 	case IDCANCEL:
-	    if (ctx)
+	    if (ctx != NULL)
 		ctx->cancel = 1;
 	    EndDialog (dlg, FALSE);
 	    break;



More information about the Winpt-commits mailing list