2de25792eb20a96f5030f0ae59c7068c2595ae81
[citadel.git] / textclient / src / client_passwords.c
1 /*
2  * Functions which allow the client to remember usernames and passwords for
3  * various sites.
4  *
5  * Copyright (c) 1987-2016 by the citadel.org team
6  *
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.
9  *
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.
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <pwd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <libcitadel.h>
27 #include "citadel_ipc.h"
28 #include "commands.h"
29 #include "client_passwords.h"
30
31 #define PWFILENAME "%s/.citadel.passwords"
32
33 void determine_pwfilename(char *pwfile, size_t n) {
34         struct passwd *p;
35
36         p = getpwuid(getuid());
37         if (p == NULL) strcpy(pwfile, "");
38         snprintf(pwfile, n, PWFILENAME, p->pw_dir);
39 }
40
41
42 /*
43  * Check the password file for a host/port match; if found, stuff the user
44  * name and password into the user/pass buffers
45  */
46 void get_stored_password(
47                 char *host,
48                 char *port,
49                 char *username,
50                 char *password) {
51
52         char pwfile[PATH_MAX];
53         FILE *fp;
54         char buf[SIZ];
55         char buf64[SIZ];
56         char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
57
58         strcpy(username, "");
59         strcpy(password, "");
60
61         determine_pwfilename(pwfile, sizeof pwfile);
62         if (IsEmptyStr(pwfile)) return;
63
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);
72
73                 if (!strcasecmp(hostbuf, host)) {
74                         if (!strcasecmp(portbuf, port)) {
75                                 strcpy(username, ubuf);
76                                 strcpy(password, pbuf);
77                         }
78                 }
79         }
80         fclose(fp);
81 }
82
83
84 /*
85  * Set (or clear) stored passwords.
86  */
87 void set_stored_password(
88                 char *host,
89                 char *port,
90                 char *username,
91                 char *password) {
92
93         char pwfile[PATH_MAX];
94         FILE *fp, *oldfp;
95         char buf[SIZ];
96         char buf64[SIZ];
97         char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
98
99         determine_pwfilename(pwfile, sizeof pwfile);
100         if (IsEmptyStr(pwfile)) return;
101
102         oldfp = fopen(pwfile, "r");
103         if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
104         unlink(pwfile);
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);
113
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);
120                 }
121         }
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);
127         }
128         fclose(oldfp);
129         fclose(fp);
130         chmod(pwfile, 0600);
131 }
132
133
134 /*
135  * Set the password if the user wants to, clear it otherwise 
136  */
137 void offer_to_remember_password(CtdlIPC *ipc,
138                 char *host,
139                 char *port,
140                 char *username,
141                 char *password) {
142
143         if (rc_remember_passwords) {
144                 if (boolprompt("Remember username/password for this site", 0)) {
145                         set_stored_password(host, port, username, password);
146                 }
147                 else {
148                         set_stored_password(host, port, "", "");
149                 }
150         }
151 }