2 * Functions which allow the client to remember usernames and passwords for
5 * Copyright (c) 1987-2009 by the citadel.org team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
32 #include <libcitadel.h>
34 #include "citadel_ipc.h"
36 #include "client_passwords.h"
38 #define PWFILENAME "%s/.citadel.passwords"
40 void determine_pwfilename(char *pwfile, size_t n) {
43 p = getpwuid(getuid());
44 if (p == NULL) strcpy(pwfile, "");
45 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
50 * Check the password file for a host/port match; if found, stuff the user
51 * name and password into the user/pass buffers
53 void get_stored_password(
59 char pwfile[PATH_MAX];
63 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
68 determine_pwfilename(pwfile, sizeof pwfile);
69 if (IsEmptyStr(pwfile)) return;
71 fp = fopen(pwfile, "r");
72 if (fp == NULL) return;
73 while (fgets(buf64, sizeof buf64, fp) != NULL) {
74 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
75 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
76 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
77 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
78 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
80 if (!strcasecmp(hostbuf, host)) {
81 if (!strcasecmp(portbuf, port)) {
82 strcpy(username, ubuf);
83 strcpy(password, pbuf);
92 * Set (or clear) stored passwords.
94 void set_stored_password(
100 char pwfile[PATH_MAX];
104 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
106 determine_pwfilename(pwfile, sizeof pwfile);
107 if (IsEmptyStr(pwfile)) return;
109 oldfp = fopen(pwfile, "r");
110 if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
112 fp = fopen(pwfile, "w");
113 if (fp == NULL) fp = fopen("/dev/null", "w");
114 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
115 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
116 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
117 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
118 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
119 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
121 if ( (strcasecmp(hostbuf, host))
122 || (strcasecmp(portbuf, port)) ) {
123 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
124 hostbuf, portbuf, ubuf, pbuf);
125 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
126 fprintf(fp, "%s\n", buf64);
129 if (!IsEmptyStr(username)) {
130 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
131 host, port, username, password);
132 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
133 fprintf(fp, "%s\n", buf64);
142 * Set the password if the user wants to, clear it otherwise
144 void offer_to_remember_password(CtdlIPC *ipc,
150 if (rc_remember_passwords) {
151 if (boolprompt("Remember username/password for this site", 0)) {
152 set_stored_password(host, port, username, password);
155 set_stored_password(host, port, "", "");