]> diplodocus.org Git - nmh/blob - sbr/credentials.c
Another pass at cleaning up (some of) the manpages.
[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 ((unsigned char) *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 creds->user = user == NULL ? getusername () : user;
52 if (sasl) {
53
54 /* This is what inc.c and msgchk.c used to contain. */
55 /* Only inc.c and msgchk.c do this. smtp.c doesn't. */
56 creds->password = getusername ();
57 }
58 } else if (! strncasecmp (cred_style, "file:", 5)) {
59 /*
60 * Determine user using the first of:
61 * 1) -user switch
62 * 2) matching host entry with login in a credentials file
63 * such as ~/.netrc
64 * 3) interactively request from user (as long as the
65 * credentials file didn't have a "default" token)
66 */
67 creds->user = user;
68 } else {
69 admonish (NULL, "unknown credentials style %s", cred_style);
70 return NOTOK;
71 }
72
73 ruserpass (host, &creds->user, &creds->password);
74 return OK;
75 }