2 * Functions which allow the client to remember usernames and passwords for
5 * Copyright (c) 1987-2016 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include "textclient.h"
18 #define PWFILENAME "%s/.citadel.passwords"
20 void determine_pwfilename(char *pwfile, size_t n)
24 p = getpwuid(getuid());
27 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
32 * Check the password file for a host/port match; if found, stuff the user
33 * name and password into the user/pass buffers
35 void get_stored_password(char *host, char *port, char *username, char *password)
38 char pwfile[PATH_MAX];
42 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
47 determine_pwfilename(pwfile, sizeof pwfile);
48 if (IsEmptyStr(pwfile))
51 fp = fopen(pwfile, "r");
54 while (fgets(buf64, sizeof buf64, fp) != NULL) {
55 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
56 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
57 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
58 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
59 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
61 if (!strcasecmp(hostbuf, host)) {
62 if (!strcasecmp(portbuf, port)) {
63 strcpy(username, ubuf);
64 strcpy(password, pbuf);
73 * Set (or clear) stored passwords.
75 void set_stored_password(char *host, char *port, char *username, char *password)
78 char pwfile[PATH_MAX];
82 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
84 determine_pwfilename(pwfile, sizeof pwfile);
85 if (IsEmptyStr(pwfile))
88 oldfp = fopen(pwfile, "r");
90 oldfp = fopen("/dev/null", "r");
92 fp = fopen(pwfile, "w");
94 fp = fopen("/dev/null", "w");
95 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
96 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
97 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
98 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
99 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
100 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
102 if ((strcasecmp(hostbuf, host))
103 || (strcasecmp(portbuf, port))) {
104 snprintf(buf, sizeof buf, "%s|%s|%s|%s|", hostbuf, portbuf, ubuf, pbuf);
105 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
106 fprintf(fp, "%s\n", buf64);
109 if (!IsEmptyStr(username)) {
110 snprintf(buf, sizeof buf, "%s|%s|%s|%s|", host, port, username, password);
111 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
112 fprintf(fp, "%s\n", buf64);
121 * Set the password if the user wants to, clear it otherwise
123 void offer_to_remember_password(CtdlIPC * ipc, char *host, char *port, char *username, char *password)
126 if (rc_remember_passwords) {
127 if (boolprompt("Remember username/password for this site", 0)) {
128 set_stored_password(host, port, username, password);
130 set_stored_password(host, port, "", "");