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