4 * Functions which allow the client to remember usernames and passwords for
15 #include <sys/types.h>
19 #include <libcitadel.h>
21 #include "citadel_ipc.h"
23 #include "client_passwords.h"
25 #define PWFILENAME "%s/.citadel.passwords"
27 void determine_pwfilename(char *pwfile
, size_t n
) {
30 p
= getpwuid(getuid());
31 if (p
== NULL
) strcpy(pwfile
, "");
32 snprintf(pwfile
, n
, PWFILENAME
, p
->pw_dir
);
37 * Check the password file for a host/port match; if found, stuff the user
38 * name and password into the user/pass buffers
40 void get_stored_password(
46 char pwfile
[PATH_MAX
];
50 char hostbuf
[256], portbuf
[256], ubuf
[256], pbuf
[256];
55 determine_pwfilename(pwfile
, sizeof pwfile
);
56 if (IsEmptyStr(pwfile
)) return;
58 fp
= fopen(pwfile
, "r");
59 if (fp
== NULL
) return;
60 while (fgets(buf64
, sizeof buf64
, fp
) != NULL
) {
61 CtdlDecodeBase64(buf
, buf64
, sizeof(buf64
));
62 extract_token(hostbuf
, buf
, 0, '|', sizeof hostbuf
);
63 extract_token(portbuf
, buf
, 1, '|', sizeof portbuf
);
64 extract_token(ubuf
, buf
, 2, '|', sizeof ubuf
);
65 extract_token(pbuf
, buf
, 3, '|', sizeof pbuf
);
67 if (!strcasecmp(hostbuf
, host
)) {
68 if (!strcasecmp(portbuf
, port
)) {
69 strcpy(username
, ubuf
);
70 strcpy(password
, pbuf
);
79 * Set (or clear) stored passwords.
81 void set_stored_password(
87 char pwfile
[PATH_MAX
];
91 char hostbuf
[256], portbuf
[256], ubuf
[256], pbuf
[256];
93 determine_pwfilename(pwfile
, sizeof pwfile
);
94 if (IsEmptyStr(pwfile
)) return;
96 oldfp
= fopen(pwfile
, "r");
97 if (oldfp
== NULL
) oldfp
= fopen("/dev/null", "r");
99 fp
= fopen(pwfile
, "w");
100 if (fp
== NULL
) fp
= fopen("/dev/null", "w");
101 while (fgets(buf64
, sizeof buf64
, oldfp
) != NULL
) {
102 CtdlDecodeBase64(buf
, buf64
, sizeof(buf64
));
103 extract_token(hostbuf
, buf
, 0, '|', sizeof hostbuf
);
104 extract_token(portbuf
, buf
, 1, '|', sizeof portbuf
);
105 extract_token(ubuf
, buf
, 2, '|', sizeof ubuf
);
106 extract_token(pbuf
, buf
, 3, '|', sizeof pbuf
);
108 if ( (strcasecmp(hostbuf
, host
))
109 || (strcasecmp(portbuf
, port
)) ) {
110 snprintf(buf
, sizeof buf
, "%s|%s|%s|%s|",
111 hostbuf
, portbuf
, ubuf
, pbuf
);
112 CtdlEncodeBase64(buf64
, buf
, strlen(buf
), 0);
113 fprintf(fp
, "%s\n", buf64
);
116 if (!IsEmptyStr(username
)) {
117 snprintf(buf
, sizeof buf
, "%s|%s|%s|%s|",
118 host
, port
, username
, password
);
119 CtdlEncodeBase64(buf64
, buf
, strlen(buf
), 0);
120 fprintf(fp
, "%s\n", buf64
);
129 * Set the password if the user wants to, clear it otherwise
131 void offer_to_remember_password(CtdlIPC
*ipc
,
137 if (rc_remember_passwords
) {
138 if (boolprompt("Remember username/password for this site", 0)) {
139 set_stored_password(host
, port
, username
, password
);
142 set_stored_password(host
, port
, "", "");