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