2 * Functions which allow the client to remember usernames and passwords for
5 * Copyright (c) 1987-2012 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>
28 #include "citadel_ipc.h"
30 #include "client_passwords.h"
32 #define PWFILENAME "%s/.citadel.passwords"
34 void determine_pwfilename(char *pwfile, size_t n) {
37 p = getpwuid(getuid());
38 if (p == NULL) strcpy(pwfile, "");
39 snprintf(pwfile, n, PWFILENAME, p->pw_dir);
44 * Check the password file for a host/port match; if found, stuff the user
45 * name and password into the user/pass buffers
47 void get_stored_password(
53 char pwfile[PATH_MAX];
57 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
62 determine_pwfilename(pwfile, sizeof pwfile);
63 if (IsEmptyStr(pwfile)) return;
65 fp = fopen(pwfile, "r");
66 if (fp == NULL) return;
67 while (fgets(buf64, sizeof buf64, fp) != NULL) {
68 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
69 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
70 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
71 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
72 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
74 if (!strcasecmp(hostbuf, host)) {
75 if (!strcasecmp(portbuf, port)) {
76 strcpy(username, ubuf);
77 strcpy(password, pbuf);
86 * Set (or clear) stored passwords.
88 void set_stored_password(
94 char pwfile[PATH_MAX];
98 char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
100 determine_pwfilename(pwfile, sizeof pwfile);
101 if (IsEmptyStr(pwfile)) return;
103 oldfp = fopen(pwfile, "r");
104 if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
106 fp = fopen(pwfile, "w");
107 if (fp == NULL) fp = fopen("/dev/null", "w");
108 while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
109 CtdlDecodeBase64(buf, buf64, sizeof(buf64));
110 extract_token(hostbuf, buf, 0, '|', sizeof hostbuf);
111 extract_token(portbuf, buf, 1, '|', sizeof portbuf);
112 extract_token(ubuf, buf, 2, '|', sizeof ubuf);
113 extract_token(pbuf, buf, 3, '|', sizeof pbuf);
115 if ( (strcasecmp(hostbuf, host))
116 || (strcasecmp(portbuf, port)) ) {
117 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
118 hostbuf, portbuf, ubuf, pbuf);
119 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
120 fprintf(fp, "%s\n", buf64);
123 if (!IsEmptyStr(username)) {
124 snprintf(buf, sizeof buf, "%s|%s|%s|%s|",
125 host, port, username, password);
126 CtdlEncodeBase64(buf64, buf, strlen(buf), 0);
127 fprintf(fp, "%s\n", buf64);
136 * Set the password if the user wants to, clear it otherwise
138 void offer_to_remember_password(CtdlIPC *ipc,
144 if (rc_remember_passwords) {
145 if (boolprompt("Remember username/password for this site", 0)) {
146 set_stored_password(host, port, username, password);
149 set_stored_password(host, port, "", "");