File: credentials.c

package info (click to toggle)
nmh 1.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,204 kB
  • ctags: 3,851
  • sloc: ansic: 48,922; sh: 16,422; makefile: 559; perl: 509; lex: 402; awk: 74
file content (75 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * This code is Copyright (c) 2013, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/utils.h>
#include <h/mts.h>

void
init_credentials_file () {
    if (credentials_file == NULL) {
        char *cred_style = context_find ("credentials");

        if (cred_style == NULL  ||  ! strcmp (cred_style, "legacy")) {
            char *hdir = getenv ("HOME");

            credentials_file = concat (hdir ? hdir : ".", "/.netrc", NULL);
        } else if (! strncasecmp (cred_style, "file:", 5)) {
            struct stat st;
            char *filename = cred_style + 5;

            while (*filename && isspace ((unsigned char) *filename)) ++filename;

            if (*filename == '/') {
                credentials_file = filename;
            } else {
                credentials_file = m_maildir (filename);
                if (stat (credentials_file, &st) != OK) {
                    credentials_file =
                        concat (mypath ? mypath : ".", "/", filename, NULL);
                    if (stat (credentials_file, &st) != OK) {
                        admonish (NULL, "unable to find credentials file %s",
                                  filename);
                    }
                }
            }
        }
    }
}

int
nmh_get_credentials (char *host, char *user, int sasl, nmh_creds_t creds) {
    char *cred_style = context_find ("credentials");

    init_credentials_file ();
    creds->host = host;

    if (cred_style == NULL  ||  ! strcmp (cred_style, "legacy")) {
	creds->user = user == NULL  ?  getusername ()  :  user;
        if (sasl) {

            /* This is what inc.c and msgchk.c used to contain. */
            /* Only inc.c and msgchk.c do this.  smtp.c doesn't. */
            creds->password = getusername ();
        }
    } else if (! strncasecmp (cred_style, "file:", 5)) {
        /*
         * Determine user using the first of:
         * 1) -user switch
         * 2) matching host entry with login in a credentials file
         *    such as ~/.netrc
         * 3) interactively request from user (as long as the
         *    credentials file didn't have a "default" token)
         */
        creds->user = user;
    } else {
        admonish (NULL, "unknown credentials style %s", cred_style);
        return NOTOK;
    }

    ruserpass (host, &creds->user, &creds->password);
    return OK;
}