summaryrefslogtreecommitdiffstats
path: root/xlators/protocol/auth/login/src/login.c
blob: da10d0b5cdc22f0c363266ec44ef8b1a4005b582 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <fnmatch.h>
#include "authenticate.h"

/* Note on strict_auth
 * - Strict auth kicks in when authentication is using the username, password
 *   in the volfile to login
 * - If enabled, auth is rejected if the username and password is not matched
 *   or is not present
 * - When using SSL names, this is automatically strict, and allows only those
 *   names that are present in the allow list, IOW strict auth checking has no
 *   implication when using SSL names
*/

auth_result_t gf_auth (dict_t *input_params, dict_t *config_params)
{
        auth_result_t  result  = AUTH_DONT_CARE;
        int             ret             = 0;
        data_t          *allow_user     = NULL;
        data_t          *username_data  = NULL;
        data_t          *passwd_data    = NULL;
        data_t          *password_data  = NULL;
        char            *username       = NULL;
        char            *password       = NULL;
        char            *brick_name     = NULL;
        char            *searchstr      = NULL;
        char            *username_str   = NULL;
        char            *tmp            = NULL;
        char            *username_cpy   = NULL;
        gf_boolean_t    using_ssl       = _gf_false;
        gf_boolean_t    strict_auth     = _gf_false;

        username_data = dict_get (input_params, "ssl-name");
        if (username_data) {
                gf_log ("auth/login", GF_LOG_INFO,
                        "connecting user name: %s", username_data->data);
                using_ssl = _gf_true;
        }
        else {
                ret = dict_get_str_boolean (config_params, "strict-auth-accept",
                                            _gf_false);
                if (ret == -1)
                        strict_auth = _gf_false;
                else
                        strict_auth = ret;

                username_data = dict_get (input_params, "username");
                if (!username_data) {
                        if (strict_auth) {
                                gf_log ("auth/login", GF_LOG_DEBUG,
                                        "username not found, strict auth"
                                        " configured returning REJECT");
                                result = AUTH_REJECT;
                        } else {
                                gf_log ("auth/login", GF_LOG_DEBUG,
                                        "username not found, returning"
                                        " DONT-CARE");
                        }
                        goto out;
                }
                password_data = dict_get (input_params, "password");
                if (!password_data) {
                        if (strict_auth) {
                                gf_log ("auth/login", GF_LOG_DEBUG,
                                        "password not found, strict auth"
                                        " configured returning REJECT");
                                result = AUTH_REJECT;
                        } else {
                                gf_log ("auth/login", GF_LOG_WARNING,
                                        "password not found, returning"
                                        " DONT-CARE");
                        }
                        goto out;
                }
                password = data_to_str (password_data);
        }
        username = data_to_str (username_data);

        brick_name = data_to_str (dict_get (input_params, "remote-subvolume"));
        if (!brick_name) {
                gf_log ("auth/login", GF_LOG_ERROR,
                        "remote-subvolume not specified");
                result = AUTH_REJECT;
                goto out;
        }

        ret = gf_asprintf (&searchstr, "auth.login.%s.%s", brick_name,
                           using_ssl ? "ssl-allow" : "allow");
        if (-1 == ret) {
                gf_log ("auth/login", GF_LOG_ERROR,
                        "asprintf failed while setting search string, "
                        "returning REJECT");
                result = AUTH_REJECT;
                goto out;
        }

        allow_user = dict_get (config_params, searchstr);
        GF_FREE (searchstr);

        if (allow_user) {
                gf_log ("auth/login", GF_LOG_INFO,
                        "allowed user names: %s", allow_user->data);
                /*
                 * There's a subtle difference between SSL and non-SSL behavior
                 * if we can't match anything in the "while" loop below.
                 * Intuitively, we should AUTH_REJECT if there's no match.
                 * However, existing code depends on allowing untrusted users
                 * to connect with *no credentials at all* by falling through
                 * the loop.  They're still distinguished from trusted users
                 * who do provide a valid username and password (in fact that's
                 * pretty much the only thing we use non-SSL login auth for),
                 * but they are allowed to connect.  It's wrong, but it's not
                 * worth changing elsewhere.  Therefore, we do the sane thing
                 * only for SSL here.
                 *
                 * For SSL, if there's a list *you must be on it*.  Note that
                 * if there's no list we don't care.  In that case (and the
                 * ssl-allow=* case as well) authorization is effectively
                 * disabled, though authentication and encryption are still
                 * active.
                 *
                 * Read NOTE on strict_auth above.
                 */
                if (using_ssl || strict_auth) {
                        result = AUTH_REJECT;
                }
                username_cpy = gf_strdup (allow_user->data);
                if (!username_cpy)
                        goto out;

                username_str = strtok_r (username_cpy, " ,", &tmp);

                /*
                 * We have to match a user's *authenticated* name to one in the
                 * list.  If we're using SSL, they're already authenticated.
                 * Otherwise, they need a matching password to complete the
                 * process.
                 */
                while (username_str) {
                        if (!fnmatch (username_str, username, 0)) {
                                if (using_ssl) {
                                        result = AUTH_ACCEPT;
                                        break;
                                }
                                ret = gf_asprintf (&searchstr,
                                                   "auth.login.%s.password",
                                                   username);
                                if (-1 == ret) {
                                        gf_log ("auth/login", GF_LOG_WARNING,
                                                "asprintf failed while setting search string");
                                        goto out;
                                }
                                passwd_data = dict_get (config_params, searchstr);
                                GF_FREE (searchstr);

                                if (!passwd_data) {
                                        gf_log ("auth/login", GF_LOG_ERROR,
                                                "wrong username/password combination");
                                        result = AUTH_REJECT;
                                        goto out;
                                }

                                result = !((strcmp (data_to_str (passwd_data),
                                                    password)) ?
                                           AUTH_ACCEPT :
                                           AUTH_REJECT);
                                if (result == AUTH_REJECT)
                                        gf_log ("auth/login", GF_LOG_ERROR,
                                                "wrong password for user %s",
                                                username);

                                break;
                        }
                        username_str = strtok_r (NULL, " ,", &tmp);
                }
        }

out:
        GF_FREE (username_cpy);

        return result;
}

struct volume_options options[] = {
        { .key   = {"auth.login.*.allow"},
          .type  = GF_OPTION_TYPE_ANY
        },
        { .key   = {"auth.login.*.password"},
          .type  = GF_OPTION_TYPE_ANY
        },
        { .key = {NULL} }
};