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.
11 char *host
; /* Hostname corresponding to credentials */
12 char *user
; /* Username corresponding to credentials */
13 char *pass
; /* (Optional) password used by credentials */
17 init_credentials_file () {
18 if (credentials_file
== NULL
) {
19 char *cred_style
= context_find ("credentials");
21 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
22 char *hdir
= getenv ("HOME");
24 credentials_file
= concat (hdir
? hdir
: ".", "/.netrc", NULL
);
25 } else if (! strncasecmp (cred_style
, "file:", 5) ||
26 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
28 char *filename
= strchr(cred_style
, ':') + 1;
30 while (*filename
&& isspace ((unsigned char) *filename
)) ++filename
;
32 if (*filename
== '/') {
33 credentials_file
= filename
;
35 credentials_file
= m_maildir (filename
);
36 if (stat (credentials_file
, &st
) != OK
) {
38 concat (mypath
? mypath
: ".", "/", filename
, NULL
);
39 if (stat (credentials_file
, &st
) != OK
) {
40 inform("unable to find credentials file %s, continuing...",
46 if (! strncasecmp (cred_style
, "file-nopermcheck:", 17))
47 credentials_no_perm_check
= 1;
53 nmh_get_credentials (const char *host
, const char *user
)
57 char *cred_style
= context_find ("credentials");
59 init_credentials_file ();
61 creds
= mh_xmalloc(sizeof(*creds
));
63 creds
->host
= mh_xstrdup(host
);
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)) {
72 * Determine user using the first of:
74 * 2) matching host entry with login in a credentials file
76 * 3) interactively request from user (as long as the
77 * credentials file didn't have a "default" token)
79 creds
->user
= user
== NULL
? NULL
: mh_xstrdup(user
);
81 inform("unknown credentials style %s, continuing...", cred_style
);
85 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
,
86 RUSERPASS_NO_PROMPT_USER
| RUSERPASS_NO_PROMPT_PASSWORD
);
92 * Retrieve the username
96 nmh_cred_get_user(nmh_creds_t creds
)
99 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
,
100 RUSERPASS_NO_PROMPT_PASSWORD
);
107 * Retrieve the password
111 nmh_cred_get_password(nmh_creds_t creds
)
114 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
, 0);
121 * Free our credentials
125 nmh_credentials_free(nmh_creds_t creds
)
127 mh_xfree(creds
->host
);
128 mh_xfree(creds
->user
);
131 memset(creds
->pass
, 0, strlen(creds
->pass
));