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