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) {
21 p = getpwuid(getuid());
24 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
29 * Check the password file for a host/port match; if found, stuff the user
30 * name and password into the user/pass buffers
32 void get_stored_password(char *host, char *port, char *username, char *password) {
34 char pwfile[PATH_MAX];
38 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
43 determine_pwfilename(pwfile, sizeof pwfile);
44 if (IsEmptyStr(pwfile))
47 fp = fopen(pwfile, "r");
50 while (fgets(buf64, sizeof buf64, fp) != NULL) {
51 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
52 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
53 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
54 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
55 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
57 if (!strcasecmp(hostbuf, host)) {
58 if (!strcasecmp(portbuf, port)) {
59 strcpy(username, ubuf);
60 strcpy(password, pbuf);
69 * Set (or clear) stored passwords.
71 void set_stored_password(char *host, char *port, char *username, char *password) {
73 char pwfile[PATH_MAX];
77 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
79 determine_pwfilename(pwfile, sizeof pwfile);
80 if (IsEmptyStr(pwfile))
83 oldfp = fopen(pwfile, "r");
85 oldfp = fopen("/dev/null", "r");
87 fp = fopen(pwfile, "w");
89 fp = fopen("/dev/null", "w");
90 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
91 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
92 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
93 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
94 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
95 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
97 if ((strcasecmp(hostbuf, host))
98 || (strcasecmp(portbuf, port))) {
99 snprintf(buf, sizeof buf, "%s|%s|%s|%s|", hostbuf, portbuf, ubuf, pbuf);
100 CtdlEncodeBase64(buf64, buf, strlen(buf), BASE64_NO_LINEBREAKS);
101 fprintf(fp, "%s\n", buf64);
104 if (!IsEmptyStr(username)) {
105 snprintf(buf, sizeof buf, "%s|%s|%s|%s|", host, port, username, password);
106 CtdlEncodeBase64(buf64, buf, strlen(buf), BASE64_NO_LINEBREAKS);
107 fprintf(fp, "%s\n", buf64);
116 * Set the password if the user wants to, clear it otherwise
118 void offer_to_remember_password(CtdlIPC * ipc, char *host, char *port, char *username, char *password) {
120 if (rc_remember_passwords) {
121 if (boolprompt("Remember username/password for this site", 0)) {
122 set_stored_password(host, port, username, password);
125 set_stored_password(host, port, "", "");