* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / client_passwords.c
1 /*
2  * $Id$
3  *
4  * Functions which allow the client to remember usernames and passwords for
5  * various sites.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <pwd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <stdio.h>
19 #include "tools.h"
20 #include "commands.h"
21
22 #define PWFILENAME "%s/.citadel.passwords"
23
24 void determine_pwfilename(char *pwfile) {
25         struct passwd *p;
26
27         p = getpwuid(getuid());
28         if (p == NULL) strcpy(pwfile, "");
29         sprintf(pwfile, PWFILENAME, p->pw_dir);
30 }
31
32
33 /*
34  * Check the password file for a host/port match; if found, stuff the user
35  * name and password into the user/pass buffers
36  */
37 void get_stored_password(
38                 char *host,
39                 char *port,
40                 char *username,
41                 char *password) {
42
43         char pwfile[PATH_MAX];
44         FILE *fp;
45         char buf[256];
46         char buf64[256];
47         char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
48
49         strcpy(username, "");
50         strcpy(password, "");
51
52         determine_pwfilename(pwfile);
53         if (strlen(pwfile)==0) return;
54
55         fp = fopen(pwfile, "r");
56         if (fp == NULL) return;
57         while (fgets(buf64, sizeof buf64, fp) != NULL) {
58                 decode_base64(buf, buf64);
59                 extract(hostbuf, buf, 0);
60                 extract(portbuf, buf, 1);
61                 extract(ubuf, buf, 2);
62                 extract(pbuf, buf, 3);
63
64                 if (!strcasecmp(hostbuf, host)) {
65                         if (!strcasecmp(portbuf, port)) {
66                                 strcpy(username, ubuf);
67                                 strcpy(password, pbuf);
68                         }
69                 }
70         }
71         fclose(fp);
72 }
73
74
75 /*
76  * Set (or clear) stored passwords.
77  */
78 void set_stored_password(
79                 char *host,
80                 char *port,
81                 char *username,
82                 char *password) {
83
84         char pwfile[PATH_MAX];
85         FILE *fp, *oldfp;
86         char buf[256];
87         char buf64[256];
88         char hostbuf[256], portbuf[256], ubuf[256], pbuf[256];
89
90         determine_pwfilename(pwfile);
91         if (strlen(pwfile)==0) return;
92
93         oldfp = fopen(pwfile, "r");
94         if (oldfp == NULL) oldfp = fopen("/dev/null", "r");
95         unlink(pwfile);
96         fp = fopen(pwfile, "w");
97         if (fp == NULL) fp = fopen("/dev/null", "w");
98         while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
99                 decode_base64(buf, buf64);
100                 extract(hostbuf, buf, 0);
101                 extract(portbuf, buf, 1);
102                 extract(ubuf, buf, 2);
103                 extract(pbuf, buf, 3);
104
105                 if ( (strcasecmp(hostbuf, host)) 
106                    || (strcasecmp(portbuf, port)) ) {
107                         sprintf(buf, "%s|%s|%s|%s|",
108                                 hostbuf, portbuf, ubuf, pbuf);
109                         encode_base64(buf64, buf);
110                         fprintf(fp, "%s\n", buf64);
111                 }
112         }
113         if (strlen(username) > 0) {
114                 sprintf(buf, "%s|%s|%s|%s|",
115                         host, port, username, password);
116                 encode_base64(buf64, buf);
117                 fprintf(fp, "%s\n", buf64);
118         }
119         fclose(oldfp);
120         fclose(fp);
121         chmod(pwfile, 0600);
122 }
123
124
125 /*
126  * Set the password if the user wants to, clear it otherwise 
127  */
128 void offer_to_remember_password(
129                 char *host,
130                 char *port,
131                 char *username,
132                 char *password) {
133
134         if (rc_remember_passwords) {
135                 if (boolprompt("Remember username/password for this site", 0)) {
136                         set_stored_password(host, port, username, password);
137                 }
138                 else {
139                         set_stored_password(host, port, "", "");
140                 }
141         }
142 }