4 * Functions which allow the client to remember usernames and passwords for
15 #include <sys/types.h>
20 #include "citadel_ipc.h"
24 #define PWFILENAME "%s/.citadel.passwords"
26 static void determine_pwfilename(char *pwfile, size_t n) {
29 p = getpwuid(getuid());
30 if (p == NULL) strcpy(pwfile, "");
31 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
36 * Check the password file for a host/port match; if found, stuff the user
37 * name and password into the user/pass buffers
39 void get_stored_password(
45 char pwfile[PATH_MAX];
49 char hostbuf[SIZ], portbuf[SIZ], ubuf[SIZ], pbuf[SIZ];
54 determine_pwfilename(pwfile, sizeof pwfile);
55 if (strlen(pwfile)==0) return;
57 fp = fopen(pwfile, "r");
58 if (fp == NULL) return;
59 while (fgets(buf64, sizeof buf64, fp) != NULL) {
60 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
61 extract(hostbuf, buf, 0);
62 extract(portbuf, buf, 1);
63 extract(ubuf, buf, 2);
64 extract(pbuf, buf, 3);
66 if (!strcasecmp(hostbuf, host)) {
67 if (!strcasecmp(portbuf, port)) {
68 strcpy(username, ubuf);
69 strcpy(password, pbuf);
78 * Set (or clear) stored passwords.
80 void set_stored_password(
86 char pwfile[PATH_MAX];
90 char hostbuf[SIZ], portbuf[SIZ], ubuf[SIZ], pbuf[SIZ];
92 determine_pwfilename(pwfile, sizeof pwfile);
93 if (strlen(pwfile)==0) return;
95 oldfp = fopen(pwfile, "r");
96 if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
98 fp = fopen(pwfile, "w");
99 if (fp == NULL) fp = fopen("/dev/null", "w");
100 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
101 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
102 extract(hostbuf, buf, 0);
103 extract(portbuf, buf, 1);
104 extract(ubuf, buf, 2);
105 extract(pbuf, buf, 3);
107 if ( (strcasecmp(hostbuf, host))
108 || (strcasecmp(portbuf, port)) ) {
109 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
110 hostbuf, portbuf, ubuf, pbuf);
111 encode_base64(buf64, buf);
112 fprintf(fp, "%s\n", buf64);
115 if (strlen(username) > 0) {
116 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
117 host, port, username, password);
118 encode_base64(buf64, buf);
119 fprintf(fp, "%s\n", buf64);
128 * Set the password if the user wants to, clear it otherwise
130 void offer_to_remember_password(CtdlIPC *ipc,
136 if (rc_remember_passwords) {
137 if (boolprompt("Remember username/password for this site", 0)) {
138 set_stored_password(host, port, username, password);
141 set_stored_password(host, port, "", "");