]> diplodocus.org Git - nmh/blob - sbr/credentials.c
mh-sequence.man: document new '=+' and '=-' for selecting relative msgs
[nmh] / sbr / credentials.c
1 /*
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.
5 */
6
7 #include <h/mh.h>
8 #include <h/utils.h>
9 #include <h/mts.h>
10
11 void
12 init_credentials_file () {
13 if (credentials_file == NULL) {
14 char *cred_style = context_find ("credentials");
15
16 if (cred_style == NULL || ! strcmp (cred_style, "legacy")) {
17 char *hdir = getenv ("HOME");
18
19 credentials_file = concat (hdir ? hdir : ".", "/.netrc", NULL);
20 } else if (! strncasecmp (cred_style, "file:", 5)) {
21 struct stat st;
22 char *filename = cred_style + 5;
23
24 while (*filename && isspace ((int) *filename)) ++filename;
25
26 if (*filename == '/') {
27 credentials_file = filename;
28 } else {
29 credentials_file = m_maildir (filename);
30 if (stat (credentials_file, &st) != OK) {
31 credentials_file =
32 concat (mypath ? mypath : ".", "/", filename, NULL);
33 if (stat (credentials_file, &st) != OK) {
34 admonish (NULL, "unable to find credentials file %s",
35 filename);
36 }
37 }
38 }
39 }
40 }
41 }
42
43 int
44 nmh_get_credentials (char *host, char *user, int sasl, nmh_creds_t creds) {
45 char *cred_style = context_find ("credentials");
46
47 init_credentials_file ();
48 creds->host = host;
49
50 if (cred_style == NULL || ! strcmp (cred_style, "legacy")) {
51 /* This is what inc.c and msgchk.c used to contain. */
52 if (user == NULL) {
53 creds->user = getusername ();
54 }
55 if (sasl) {
56 creds->password = getusername ();
57 } else {
58 ruserpass (host, &creds->user, &creds->password);
59 }
60 } else if (! strncasecmp (cred_style, "file:", 5)) {
61 /*
62 * Determine user using the first of:
63 * 1) -user switch
64 * 2) matching host entry with login in a credentials file
65 * such as ~/.netrc
66 * 3) interactively request from user (as long as the
67 * credentials file didn't have a "default" token)
68 */
69 creds->user = user;
70 ruserpass (host, &creds->user, &creds->password);
71 }
72
73 return OK;
74 }