4 * Functions which allow the client to remember usernames and passwords for
15 #include <sys/types.h>
22 #define PWFILENAME "%s/.citadel.passwords"
24 void determine_pwfilename(char *pwfile) {
27 p = getpwuid(getuid());
28 if (p == NULL) strcpy(pwfile, "");
29 sprintf(pwfile, PWFILENAME, p->pw_dir);
34 * Check the password file for a host/port match; if found, stuff the user
35 * name and password into the user/pass buffers
37 void get_stored_password(
43 char pwfile[PATH_MAX];
47 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
52 determine_pwfilename(pwfile);
53 if (strlen(pwfile)==0) return;
55 fp = fopen(pwfile, "r");
56 if (fp == NULL) return;
57 while (fgets(buf64, sizeof buf64, fp) != NULL) {
58 decode_base64(buf, buf64);
59 extract(hostbuf, buf, 0);
60 extract(portbuf, buf, 1);
61 extract(ubuf, buf, 2);
62 extract(pbuf, buf, 3);
64 if (!strcasecmp(hostbuf, host)) {
65 if (!strcasecmp(portbuf, port)) {
66 strcpy(username, ubuf);
67 strcpy(password, pbuf);
76 * Set (or clear) stored passwords.
78 void set_stored_password(
84 char pwfile[PATH_MAX];
88 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
90 determine_pwfilename(pwfile);
91 if (strlen(pwfile)==0) return;
93 oldfp = fopen(pwfile, "r");
94 if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
96 fp = fopen(pwfile, "w");
97 if (fp == NULL) fp = fopen("/dev/null", "w");
98 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
99 decode_base64(buf, buf64);
100 extract(hostbuf, buf, 0);
101 extract(portbuf, buf, 1);
102 extract(ubuf, buf, 2);
103 extract(pbuf, buf, 3);
105 if ( (strcasecmp(hostbuf, host))
106 || (strcasecmp(portbuf, port)) ) {
107 sprintf(buf, "%s|%s|%s|%s|",
108 hostbuf, portbuf, ubuf, pbuf);
109 encode_base64(buf64, buf);
110 fprintf(fp, "%s\n", buf64);
113 if (strlen(username) > 0) {
114 sprintf(buf, "%s|%s|%s|%s|",
115 host, port, username, password);
116 encode_base64(buf64, buf);
117 fprintf(fp, "%s\n", buf64);
126 * Set the password if the user wants to, clear it otherwise
128 void offer_to_remember_password(
134 if (rc_remember_passwords) {
135 if (boolprompt("Remember username/password for this site", 0)) {
136 set_stored_password(host, port, username, password);
139 set_stored_password(host, port, "", "");