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.
22 #include <sys/types.h>
26 #include <libcitadel.h>
27 #include "citadel_ipc.h"
29 #include "client_passwords.h"
31 #define PWFILENAME "%s/.citadel.passwords"
33 void determine_pwfilename(char *pwfile, size_t n) {
36 p = getpwuid(getuid());
37 if (p == NULL) strcpy(pwfile, "");
38 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
43 * Check the password file for a host/port match; if found, stuff the user
44 * name and password into the user/pass buffers
46 void get_stored_password(
52 char pwfile[PATH_MAX];
56 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
61 determine_pwfilename(pwfile, sizeof pwfile);
62 if (IsEmptyStr(pwfile)) return;
64 fp = fopen(pwfile, "r");
65 if (fp == NULL) return;
66 while (fgets(buf64, sizeof buf64, fp) != NULL) {
67 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
68 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
69 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
70 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
71 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
73 if (!strcasecmp(hostbuf, host)) {
74 if (!strcasecmp(portbuf, port)) {
75 strcpy(username, ubuf);
76 strcpy(password, pbuf);
85 * Set (or clear) stored passwords.
87 void set_stored_password(
93 char pwfile[PATH_MAX];
97 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
99 determine_pwfilename(pwfile, sizeof pwfile);
100 if (IsEmptyStr(pwfile)) return;
102 oldfp = fopen(pwfile, "r");
103 if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
105 fp = fopen(pwfile, "w");
106 if (fp == NULL) fp = fopen("/dev/null", "w");
107 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
108 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
109 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
110 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
111 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
112 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
114 if ( (strcasecmp(hostbuf, host))
115 || (strcasecmp(portbuf, port)) ) {
116 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
117 hostbuf, portbuf, ubuf, pbuf);
118 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
119 fprintf(fp, "%s\n", buf64);
122 if (!IsEmptyStr(username)) {
123 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
124 host, port, username, password);
125 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
126 fprintf(fp, "%s\n", buf64);
135 * Set the password if the user wants to, clear it otherwise
137 void offer_to_remember_password(CtdlIPC *ipc,
143 if (rc_remember_passwords) {
144 if (boolprompt("Remember username/password for this site", 0)) {
145 set_stored_password(host, port, username, password);
148 set_stored_password(host, port, "", "");