]> diplodocus.org Git - nmh/blob - sbr/credentials.c
b7eda96a0073b6d137bf4605e8dc0b4e564cb7b3
[nmh] / sbr / credentials.c
1 /* credentials.c -- wrap configurable access to .netrc or similar files.
2 *
3 * This code is Copyright (c) 2013, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "ruserpass.h"
10 #include "credentials.h"
11 #include "context_find.h"
12 #include "error.h"
13 #include "h/utils.h"
14 #include "h/mts.h"
15 #include "m_maildir.h"
16
17 struct nmh_creds {
18 char *host; /* Hostname corresponding to credentials */
19 char *user; /* Username corresponding to credentials */
20 char *pass; /* (Optional) password used by credentials */
21 };
22
23 void
24 init_credentials_file(void)
25 {
26 if (credentials_file == NULL) {
27 char *cred_style = context_find ("credentials");
28
29 if (cred_style == NULL || ! strcmp (cred_style, "legacy")) {
30 char *hdir = getenv ("HOME");
31
32 credentials_file = concat (hdir ? hdir : ".", "/.netrc", NULL);
33 } else if (! strncasecmp (cred_style, "file:", 5) ||
34 ! strncasecmp (cred_style, "file-nopermcheck:", 17)) {
35 struct stat st;
36 char *filename = strchr(cred_style, ':') + 1;
37
38 while (isspace((unsigned char)*filename))
39 filename++;
40
41 if (*filename == '/') {
42 credentials_file = filename;
43 } else {
44 credentials_file = m_maildir (filename);
45 if (stat (credentials_file, &st) != OK) {
46 credentials_file =
47 concat (mypath ? mypath : ".", "/", filename, NULL);
48 if (stat (credentials_file, &st) != OK) {
49 inform("unable to find credentials file %s, continuing...",
50 filename);
51 }
52 }
53 }
54
55 if (! strncasecmp (cred_style, "file-nopermcheck:", 17))
56 credentials_no_perm_check = 1;
57 }
58 }
59 }
60
61 nmh_creds_t
62 nmh_get_credentials (const char *host, const char *user)
63 {
64 nmh_creds_t creds;
65
66 char *cred_style = context_find ("credentials");
67
68 init_credentials_file ();
69
70 creds = mh_xmalloc(sizeof(*creds));
71
72 creds->host = mh_xstrdup(host);
73 creds->user = NULL;
74 creds->pass = NULL;
75
76 if (cred_style == NULL || ! strcmp (cred_style, "legacy")) {
77 creds->user = user == NULL ? mh_xstrdup(getusername ()) : mh_xstrdup(user);
78 } else if (! strncasecmp (cred_style, "file:", 5) ||
79 ! strncasecmp (cred_style, "file-nopermcheck:", 17)) {
80 /*
81 * Determine user using the first of:
82 * 1) -user switch
83 * 2) matching host entry with login in a credentials file
84 * such as ~/.netrc
85 * 3) interactively request from user (as long as the
86 * credentials file didn't have a "default" token)
87 */
88 creds->user = user == NULL ? NULL : mh_xstrdup(user);
89 } else {
90 inform("unknown credentials style %s, continuing...", cred_style);
91 return NULL;
92 }
93
94 ruserpass(creds->host, &creds->user, &creds->pass,
95 RUSERPASS_NO_PROMPT_USER | RUSERPASS_NO_PROMPT_PASSWORD);
96
97 return creds;
98 }
99
100 /*
101 * Retrieve the username
102 */
103
104 const char *
105 nmh_cred_get_user(nmh_creds_t creds)
106 {
107 if (! creds->user) {
108 ruserpass(creds->host, &creds->user, &creds->pass,
109 RUSERPASS_NO_PROMPT_PASSWORD);
110 }
111
112 return creds->user;
113 }
114
115 /*
116 * Retrieve the password
117 */
118
119 const char *
120 nmh_cred_get_password(nmh_creds_t creds)
121 {
122 if (! creds->pass) {
123 ruserpass(creds->host, &creds->user, &creds->pass, 0);
124 }
125
126 return creds->pass;
127 }
128
129 /*
130 * Free our credentials
131 */
132
133 void
134 nmh_credentials_free(nmh_creds_t creds)
135 {
136 free(creds->host);
137 free(creds->user);
138
139 if (creds->pass) {
140 memset(creds->pass, 0, strlen(creds->pass));
141 free(creds->pass);
142 }
143
144 free(creds);
145 }