1 // Functions which allow the client to remember usernames and passwords for
4 // Copyright (c) 1987-2016 by the citadel.org team
6 // This program is open source software. Use, duplication, and/or
7 // disclosure are subject to the GNU General Purpose License version 3.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 #include "textclient.h"
16 #define PWFILENAME "%s/.citadel.passwords"
18 void determine_pwfilename(char *pwfile, size_t n)
22 p = getpwuid(getuid());
25 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
30 * Check the password file for a host/port match; if found, stuff the user
31 * name and password into the user/pass buffers
33 void get_stored_password(char *host, char *port, char *username, char *password)
36 char pwfile[PATH_MAX];
40 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
45 determine_pwfilename(pwfile, sizeof pwfile);
46 if (IsEmptyStr(pwfile))
49 fp = fopen(pwfile, "r");
52 while (fgets(buf64, sizeof buf64, fp) != NULL) {
53 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
54 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
55 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
56 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
57 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
59 if (!strcasecmp(hostbuf, host)) {
60 if (!strcasecmp(portbuf, port)) {
61 strcpy(username, ubuf);
62 strcpy(password, pbuf);
71 * Set (or clear) stored passwords.
73 void set_stored_password(char *host, char *port, char *username, char *password)
76 char pwfile[PATH_MAX];
80 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
82 determine_pwfilename(pwfile, sizeof pwfile);
83 if (IsEmptyStr(pwfile))
86 oldfp = fopen(pwfile, "r");
88 oldfp = fopen("/dev/null", "r");
90 fp = fopen(pwfile, "w");
92 fp = fopen("/dev/null", "w");
93 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
94 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
95 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
96 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
97 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
98 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
100 if ((strcasecmp(hostbuf, host))
101 || (strcasecmp(portbuf, port))) {
102 snprintf(buf, sizeof buf, "%s|%s|%s|%s|", hostbuf, portbuf, ubuf, pbuf);
103 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
104 fprintf(fp, "%s\n", buf64);
107 if (!IsEmptyStr(username)) {
108 snprintf(buf, sizeof buf, "%s|%s|%s|%s|", host, port, username, password);
109 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
110 fprintf(fp, "%s\n", buf64);
119 * Set the password if the user wants to, clear it otherwise
121 void offer_to_remember_password(CtdlIPC * ipc, char *host, char *port, char *username, char *password)
124 if (rc_remember_passwords) {
125 if (boolprompt("Remember username/password for this site", 0)) {
126 set_stored_password(host, port, username, password);
128 set_stored_password(host, port, "", "");