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 init_credentials_file () {
13 if (credentials_file
== NULL
) {
14 char *cred_style
= context_find ("credentials");
16 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
17 char *hdir
= getenv ("HOME");
19 credentials_file
= concat (hdir
? hdir
: ".", "/.netrc", NULL
);
20 } else if (! strncasecmp (cred_style
, "file:", 5) ||
21 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
23 char *filename
= strchr(cred_style
, ':') + 1;
25 while (*filename
&& isspace ((unsigned char) *filename
)) ++filename
;
27 if (*filename
== '/') {
28 credentials_file
= filename
;
30 credentials_file
= m_maildir (filename
);
31 if (stat (credentials_file
, &st
) != OK
) {
33 concat (mypath
? mypath
: ".", "/", filename
, NULL
);
34 if (stat (credentials_file
, &st
) != OK
) {
35 admonish (NULL
, "unable to find credentials file %s",
41 if (! strncasecmp (cred_style
, "file-nopermcheck:", 17))
42 credentials_no_perm_check
= 1;
48 nmh_get_credentials (char *host
, char *user
, int sasl
, nmh_creds_t creds
) {
49 char *cred_style
= context_find ("credentials");
51 init_credentials_file ();
54 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
55 creds
->user
= user
== NULL
? getusername () : user
;
58 /* This is what inc.c and msgchk.c used to contain. */
59 /* Only inc.c and msgchk.c do this. smtp.c doesn't. */
60 creds
->password
= getusername ();
62 } else if (! strncasecmp (cred_style
, "file:", 5) ||
63 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
65 * Determine user using the first of:
67 * 2) matching host entry with login in a credentials file
69 * 3) interactively request from user (as long as the
70 * credentials file didn't have a "default" token)
74 admonish (NULL
, "unknown credentials style %s", cred_style
);
78 ruserpass (host
, &creds
->user
, &creds
->password
);