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