1 /* credentials.c -- wrap configurable access to .netrc or similar files.
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.
12 #include "m_maildir.h"
15 char *host
; /* Hostname corresponding to credentials */
16 char *user
; /* Username corresponding to credentials */
17 char *pass
; /* (Optional) password used by credentials */
21 init_credentials_file(void)
23 if (credentials_file
== NULL
) {
24 char *cred_style
= context_find ("credentials");
26 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
27 char *hdir
= getenv ("HOME");
29 credentials_file
= concat (hdir
? hdir
: ".", "/.netrc", NULL
);
30 } else if (! strncasecmp (cred_style
, "file:", 5) ||
31 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
33 char *filename
= strchr(cred_style
, ':') + 1;
35 while (isspace((unsigned char)*filename
))
38 if (*filename
== '/') {
39 credentials_file
= filename
;
41 credentials_file
= m_maildir (filename
);
42 if (stat (credentials_file
, &st
) != OK
) {
44 concat (mypath
? mypath
: ".", "/", filename
, NULL
);
45 if (stat (credentials_file
, &st
) != OK
) {
46 inform("unable to find credentials file %s, continuing...",
52 if (! strncasecmp (cred_style
, "file-nopermcheck:", 17))
53 credentials_no_perm_check
= 1;
59 nmh_get_credentials (const char *host
, const char *user
)
63 char *cred_style
= context_find ("credentials");
65 init_credentials_file ();
67 creds
= mh_xmalloc(sizeof(*creds
));
69 creds
->host
= mh_xstrdup(host
);
73 if (cred_style
== NULL
|| ! strcmp (cred_style
, "legacy")) {
74 creds
->user
= user
== NULL
? mh_xstrdup(getusername ()) : mh_xstrdup(user
);
75 } else if (! strncasecmp (cred_style
, "file:", 5) ||
76 ! strncasecmp (cred_style
, "file-nopermcheck:", 17)) {
78 * Determine user using the first of:
80 * 2) matching host entry with login in a credentials file
82 * 3) interactively request from user (as long as the
83 * credentials file didn't have a "default" token)
85 creds
->user
= user
== NULL
? NULL
: mh_xstrdup(user
);
87 inform("unknown credentials style %s, continuing...", cred_style
);
91 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
,
92 RUSERPASS_NO_PROMPT_USER
| RUSERPASS_NO_PROMPT_PASSWORD
);
98 * Retrieve the username
102 nmh_cred_get_user(nmh_creds_t creds
)
105 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
,
106 RUSERPASS_NO_PROMPT_PASSWORD
);
113 * Retrieve the password
117 nmh_cred_get_password(nmh_creds_t creds
)
120 ruserpass(creds
->host
, &creds
->user
, &creds
->pass
, 0);
127 * Free our credentials
131 nmh_credentials_free(nmh_creds_t creds
)
137 memset(creds
->pass
, 0, strlen(creds
->pass
));