2 * This code is Copyright (c) 2013, by the authors of nmh. See the
3 * COPYRIGHT file in the root directory of the nmh distribution for
4 * complete copyright information.
12 char *host
; /* Hostname corresponding to credentials */
13 char *user
; /* Username corresponding to credentials */
14 char *pass
; /* (Optional) password used by credentials */
18 init_credentials_file () {
19 if (credentials_file
== NULL
) {
20 char *cred_style
= context_find ("credentials");
22 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
23 char *hdir
= getenv ("HOME");
25 credentials_file
= concat (hdir
? hdir
: ".", "/.netrc", NULL
);
26 } else if (! strncasecmp (cred_style
, "file:", 5) ||
27 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
29 char *filename
= strchr(cred_style
, ':') + 1;
31 while (*filename
&& isspace ((unsigned char) *filename
)) ++filename
;
33 if (*filename
== '/') {
34 credentials_file
= filename
;
36 credentials_file
= m_maildir (filename
);
37 if (stat (credentials_file
, &st
) != OK
) {
39 concat (mypath
? mypath
: ".", "/", filename
, NULL
);
40 if (stat (credentials_file
, &st
) != OK
) {
41 admonish (NULL
, "unable to find credentials file %s",
47 if (! strncasecmp (cred_style
, "file-nopermcheck:", 17))
48 credentials_no_perm_check
= 1;
54 nmh_get_credentials (const char *host
, const char *user
)
58 char *cred_style
= context_find ("credentials");
60 init_credentials_file ();
62 creds
= mh_xmalloc(sizeof(*creds
));
64 creds
->host
= mh_xstrdup(host
);
68 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
69 creds
->user
= user
== NULL
? mh_xstrdup(getusername ()) : mh_xstrdup(user
);
70 } else if (! strncasecmp (cred_style
, "file:", 5) ||
71 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
73 * Determine user using the first of:
75 * 2) matching host entry with login in a credentials file
77 * 3) interactively request from user (as long as the
78 * credentials file didn't have a "default" token)
80 creds
->user
= user
== NULL
? NULL
: mh_xstrdup(user
);
82 admonish (NULL
, "unknown credentials style %s", cred_style
);
86 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
,
87 RUSERPASS_NO_PROMPT_USER
| RUSERPASS_NO_PROMPT_PASSWORD
);
93 * Retrieve the username
97 nmh_cred_get_user(nmh_creds_t creds
)
100 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
,
101 RUSERPASS_NO_PROMPT_PASSWORD
);
108 * Retrieve the password
112 nmh_cred_get_password(nmh_creds_t creds
)
115 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
, 0);
122 * Free our credentials
126 nmh_credentials_free(nmh_creds_t creds
)
128 mh_xfree(creds
->host
);
129 mh_xfree(creds
->user
);
132 memset(creds
->pass
, 0, strlen(creds
->pass
));