]> diplodocus.org Git - nmh/blob - sbr/credentials.c
Put parameter names in h/utils.h memory function prototypes.
[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 ! strncasecmp (cred_style, "file-nopermcheck:", 17)) {
22 struct stat st;
23 char *filename = strchr(cred_style, ':') + 1;
24
25 while (*filename && isspace ((unsigned char) *filename)) ++filename;
26
27 if (*filename == '/') {
28 credentials_file = filename;
29 } else {
30 credentials_file = m_maildir (filename);
31 if (stat (credentials_file, &st) != OK) {
32 credentials_file =
33 concat (mypath ? mypath : ".", "/", filename, NULL);
34 if (stat (credentials_file, &st) != OK) {
35 admonish (NULL, "unable to find credentials file %s",
36 filename);
37 }
38 }
39 }
40
41 if (! strncasecmp (cred_style, "file-nopermcheck:", 17))
42 credentials_no_perm_check = 1;
43 }
44 }
45 }
46
47 int
48 nmh_get_credentials (char *host, char *user, int sasl, nmh_creds_t creds) {
49 char *cred_style = context_find ("credentials");
50
51 init_credentials_file ();
52 creds->host = host;
53
54 if (cred_style == NULL || ! strcmp (cred_style, "legacy")) {
55 creds->user = user == NULL ? getusername () : user;
56 if (sasl) {
57
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 ();
61 }
62 } else if (! strncasecmp (cred_style, "file:", 5) ||
63 ! strncasecmp (cred_style, "file-nopermcheck:", 17)) {
64 /*
65 * Determine user using the first of:
66 * 1) -user switch
67 * 2) matching host entry with login in a credentials file
68 * such as ~/.netrc
69 * 3) interactively request from user (as long as the
70 * credentials file didn't have a "default" token)
71 */
72 creds->user = user;
73 } else {
74 admonish (NULL, "unknown credentials style %s", cred_style);
75 return NOTOK;
76 }
77
78 ruserpass (host, &creds->user, &creds->password);
79 return OK;
80 }