]> code.citadel.org Git - citadel.git/blobdiff - citadel/client_passwords.c
* extract_token() now expects to be supplied with the size of the
[citadel.git] / citadel / client_passwords.c
index 75076af09c8db29d99472e2f51d3efaaf6b07604..603588bf7e89d7912126e346a0c2cde2a62ae2a4 100644 (file)
 #include <limits.h>
 #include <stdio.h>
 #include "citadel.h"
+#include "citadel_ipc.h"
 #include "tools.h"
 #include "commands.h"
+#include "client_passwords.h"
 
 #define PWFILENAME "%s/.citadel.passwords"
 
-void determine_pwfilename(char *pwfile) {
+void determine_pwfilename(char *pwfile, size_t n) {
        struct passwd *p;
 
        p = getpwuid(getuid());
        if (p == NULL) strcpy(pwfile, "");
-       sprintf(pwfile, PWFILENAME, p->pw_dir);
+       snprintf(pwfile, n, PWFILENAME, p->pw_dir);
 }
 
 
@@ -45,22 +47,22 @@ void get_stored_password(
        FILE *fp;
        char buf[SIZ];
        char buf64[SIZ];
-       char hostbuf[SIZ], portbuf[SIZ], ubuf[SIZ], pbuf[SIZ];
+       char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
 
        strcpy(username, "");
        strcpy(password, "");
 
-       determine_pwfilename(pwfile);
+       determine_pwfilename(pwfile, sizeof pwfile);
        if (strlen(pwfile)==0) return;
 
        fp = fopen(pwfile, "r");
        if (fp == NULL) return;
        while (fgets(buf64, sizeof buf64, fp) != NULL) {
-               decode_base64(buf, buf64, sizeof(buf64));
-               extract(hostbuf, buf, 0);
-               extract(portbuf, buf, 1);
-               extract(ubuf, buf, 2);
-               extract(pbuf, buf, 3);
+               CtdlDecodeBase64(buf, buf64, sizeof(buf64));
+               extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
+               extract_token(portbuf, buf, 1, '|', sizeof portbuf);
+               extract_token(ubuf, buf, 2, '|', sizeof ubuf);
+               extract_token(pbuf, buf, 3, '|', sizeof pbuf);
 
                if (!strcasecmp(hostbuf, host)) {
                        if (!strcasecmp(portbuf, port)) {
@@ -86,9 +88,9 @@ void set_stored_password(
        FILE *fp, *oldfp;
        char buf[SIZ];
        char buf64[SIZ];
-       char hostbuf[SIZ], portbuf[SIZ], ubuf[SIZ], pbuf[SIZ];
+       char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
 
-       determine_pwfilename(pwfile);
+       determine_pwfilename(pwfile, sizeof pwfile);
        if (strlen(pwfile)==0) return;
 
        oldfp = fopen(pwfile, "r");
@@ -97,24 +99,24 @@ void set_stored_password(
        fp = fopen(pwfile, "w");
        if (fp == NULL) fp = fopen("/dev/null", "w");
        while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
-               decode_base64(buf, buf64, sizeof(buf64));
-               extract(hostbuf, buf, 0);
-               extract(portbuf, buf, 1);
-               extract(ubuf, buf, 2);
-               extract(pbuf, buf, 3);
+               CtdlDecodeBase64(buf, buf64, sizeof(buf64));
+               extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
+               extract_token(portbuf, buf, 1, '|', sizeof portbuf);
+               extract_token(ubuf, buf, 2, '|', sizeof ubuf);
+               extract_token(pbuf, buf, 3, '|', sizeof pbuf);
 
                if ( (strcasecmp(hostbuf, host)) 
                   || (strcasecmp(portbuf, port)) ) {
-                       sprintf(buf, "%s|%s|%s|%s|",
+                       snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
                                hostbuf, portbuf, ubuf, pbuf);
-                       encode_base64(buf64, buf);
+                       CtdlEncodeBase64(buf64, buf, strlen(buf));
                        fprintf(fp, "%s\n", buf64);
                }
        }
        if (strlen(username) > 0) {
-               sprintf(buf, "%s|%s|%s|%s|",
+               snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
                        host, port, username, password);
-               encode_base64(buf64, buf);
+               CtdlEncodeBase64(buf64, buf, strlen(buf));
                fprintf(fp, "%s\n", buf64);
        }
        fclose(oldfp);
@@ -126,7 +128,7 @@ void set_stored_password(
 /*
  * Set the password if the user wants to, clear it otherwise 
  */
-void offer_to_remember_password(
+void offer_to_remember_password(CtdlIPC *ipc,
                char *host,
                char *port,
                char *username,