4 * Functions which allow the client to remember usernames and passwords for
15 #include <sys/types.h>
23 #define PWFILENAME "%s/.citadel.passwords"
25 static void determine_pwfilename(char *pwfile, size_t n) {
28 p = getpwuid(getuid());
29 if (p == NULL) strcpy(pwfile, "");
30 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
35 * Check the password file for a host/port match; if found, stuff the user
36 * name and password into the user/pass buffers
38 void get_stored_password(
44 char pwfile[PATH_MAX];
48 char hostbuf[SIZ], portbuf[SIZ], ubuf[SIZ], pbuf[SIZ];
53 determine_pwfilename(pwfile, sizeof pwfile);
54 if (strlen(pwfile)==0) return;
56 fp = fopen(pwfile, "r");
57 if (fp == NULL) return;
58 while (fgets(buf64, sizeof buf64, fp) != NULL) {
59 decode_base64(buf, buf64, sizeof(buf64));
60 extract(hostbuf, buf, 0);
61 extract(portbuf, buf, 1);
62 extract(ubuf, buf, 2);
63 extract(pbuf, buf, 3);
65 if (!strcasecmp(hostbuf, host)) {
66 if (!strcasecmp(portbuf, port)) {
67 strcpy(username, ubuf);
68 strcpy(password, pbuf);
77 * Set (or clear) stored passwords.
79 void set_stored_password(
85 char pwfile[PATH_MAX];
89 char hostbuf[SIZ], portbuf[SIZ], ubuf[SIZ], pbuf[SIZ];
91 determine_pwfilename(pwfile, sizeof pwfile);
92 if (strlen(pwfile)==0) return;
94 oldfp = fopen(pwfile, "r");
95 if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
97 fp = fopen(pwfile, "w");
98 if (fp == NULL) fp = fopen("/dev/null", "w");
99 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
100 decode_base64(buf, buf64, sizeof(buf64));
101 extract(hostbuf, buf, 0);
102 extract(portbuf, buf, 1);
103 extract(ubuf, buf, 2);
104 extract(pbuf, buf, 3);
106 if ( (strcasecmp(hostbuf, host))
107 || (strcasecmp(portbuf, port)) ) {
108 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
109 hostbuf, portbuf, ubuf, pbuf);
110 encode_base64(buf64, buf);
111 fprintf(fp, "%s\n", buf64);
114 if (strlen(username) > 0) {
115 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
116 host, port, username, password);
117 encode_base64(buf64, buf);
118 fprintf(fp, "%s\n", buf64);
127 * Set the password if the user wants to, clear it otherwise
129 void offer_to_remember_password(
135 if (rc_remember_passwords) {
136 if (boolprompt("Remember username/password for this site", 0)) {
137 set_stored_password(host, port, username, password);
140 set_stored_password(host, port, "", "");