summaryrefslogtreecommitdiffstats
path: root/xlators/protocol/server/src/authenticate.c
blob: 5163d465261531ce4cc9c7f4699c689b590b37e4 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
  Copyright (c) 2007-2013 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.
*/



#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <dlfcn.h>
#include <errno.h>
#include "authenticate.h"
#include "server-messages.h"

static int
init (dict_t *this, char *key, data_t *value, void *data)
{
        void          *handle       = NULL;
        char          *auth_file    = NULL;
        auth_handle_t *auth_handle  = NULL;
        auth_fn_t      authenticate = NULL;
        int           *error        = NULL;
        int            ret          = 0;

        /* It gets over written */
        error = data;

        if (!strncasecmp (key, "ip", strlen ("ip"))) {
                gf_msg ("authenticate", GF_LOG_ERROR, 0,
                        PS_MSG_AUTHENTICATE_ERROR, "AUTHENTICATION MODULE "
                        "\"IP\" HAS BEEN REPLACED BY \"ADDR\"");
                dict_set (this, key, data_from_dynptr (NULL, 0));
                /* TODO: 1.3.x backward compatibility */
                // *error = -1;
                // return;
                key = "addr";
        }

        ret = gf_asprintf (&auth_file, "%s/%s.so", LIBDIR, key);
        if (-1 == ret) {
                dict_set (this, key, data_from_dynptr (NULL, 0));
                *error = -1;
                return -1;
        }

        handle = dlopen (auth_file, RTLD_LAZY);
        if (!handle) {
                gf_msg ("authenticate", GF_LOG_ERROR, 0,
                        PS_MSG_AUTHENTICATE_ERROR, "dlopen(%s): %s\n",
                        auth_file, dlerror ());
                dict_set (this, key, data_from_dynptr (NULL, 0));
                GF_FREE (auth_file);
                *error = -1;
                return -1;
        }
        GF_FREE (auth_file);

        authenticate = dlsym (handle, "gf_auth");
        if (!authenticate) {
                gf_msg ("authenticate", GF_LOG_ERROR, 0,
                        PS_MSG_AUTHENTICATE_ERROR, "dlsym(gf_auth) on %s\n",
                        dlerror ());
                dict_set (this, key, data_from_dynptr (NULL, 0));
                dlclose (handle);
                *error = -1;
                return -1;
        }

        auth_handle = GF_CALLOC (1, sizeof (*auth_handle),
                                 gf_common_mt_auth_handle_t);
        if (!auth_handle) {
                dict_set (this, key, data_from_dynptr (NULL, 0));
                *error = -1;
                dlclose (handle);
                return -1;
        }
        auth_handle->vol_opt = GF_CALLOC (1, sizeof (volume_opt_list_t),
                                          gf_common_mt_volume_opt_list_t);
        if (!auth_handle->vol_opt) {
                dict_set (this, key, data_from_dynptr (NULL, 0));
                *error = -1;
                GF_FREE (auth_handle);
                dlclose (handle);
                return -1;
        }
        auth_handle->vol_opt->given_opt = dlsym (handle, "options");
        if (auth_handle->vol_opt->given_opt == NULL) {
                gf_msg_debug ("authenticate", 0, "volume option validation "
                              "not specified");
        }

        auth_handle->authenticate = authenticate;
        auth_handle->handle = handle;

        dict_set (this, key,
                  data_from_dynptr (auth_handle, sizeof (*auth_handle)));
        return 0;
}

static int
fini (dict_t *this, char *key, data_t *value, void *data)
{
        auth_handle_t *handle = data_to_ptr (value);
        if (handle) {
                dlclose (handle->handle);
        }
        return 0;
}

static int
_gf_auth_option_validate (dict_t *d, char *k, data_t *v, void *tmp)
{
        auth_handle_t *handle = NULL;
        xlator_t      *xl = NULL;
        int ret = 0;

        xl = tmp;

        handle = data_to_ptr (v);
        if (!handle)
                return 0;

        list_add_tail (&(handle->vol_opt->list), &(xl->volume_options));

        ret = xlator_options_validate_list (xl, xl->options,
                                            handle->vol_opt, NULL);
        if (ret) {
                gf_msg ("authenticate", GF_LOG_ERROR, 0,
                        PS_MSG_VOL_VALIDATE_FAILED, "volume option validation "
                        "failed");
                return -1;
        }
        return 0;
}

int32_t
gf_auth_init (xlator_t *xl, dict_t *auth_modules)
{
        int ret = 0;

        dict_foreach (auth_modules, init, &ret);
        if (ret)
                goto out;

        ret = dict_foreach (auth_modules, _gf_auth_option_validate, xl);

out:
        if (ret) {
                gf_msg (xl->name, GF_LOG_ERROR, 0, PS_MSG_AUTH_INIT_FAILED,
                        "authentication init failed");
                dict_foreach (auth_modules, fini, &ret);
                ret = -1;
        }
        return ret;
}

typedef struct {
        dict_t  *iparams;
        dict_t  *cparams;
        int64_t result;
} gf_auth_args_t;

static int
gf_auth_one_method (dict_t *this, char *key, data_t *value, void *data)
{
        gf_auth_args_t  *args   = data;
        auth_handle_t   *handle = NULL;

        if (!value) {
                return 0;
        }

        handle = data_to_ptr (value);
        if (!handle || !handle->authenticate) {
                return 0;
        }

        switch (handle->authenticate (args->iparams, args->cparams)) {
        case AUTH_ACCEPT:
                if (args->result != AUTH_REJECT) {
                        args->result = AUTH_ACCEPT;
                }
                /* FALLTHROUGH */
        default:
                return 0;
        case AUTH_REJECT:
                args->result = AUTH_REJECT;
                return -1;
        }
}

auth_result_t
gf_authenticate (dict_t *input_params,
                 dict_t *config_params,
                 dict_t *auth_modules)
{
        char *name = NULL;
        data_t *peerinfo_data = NULL;
        gf_auth_args_t  args;

        args.iparams = input_params;
        args.cparams = config_params;
        args.result = AUTH_DONT_CARE;

        dict_foreach (auth_modules, gf_auth_one_method, &args);

        if (AUTH_DONT_CARE == args.result) {
                peerinfo_data = dict_get (input_params, "peer-info-name");

                if (peerinfo_data) {
                        name = peerinfo_data->data;
                }

                gf_msg ("auth", GF_LOG_ERROR, 0, PS_MSG_REMOTE_CLIENT_REFUSED,
                        "no authentication module is interested in "
                        "accepting remote-client %s", name);
                args.result = AUTH_REJECT;
        }

        return args.result;
}

void
gf_auth_fini (dict_t *auth_modules)
{
        int32_t dummy;

        dict_foreach (auth_modules, fini, &dummy);
}