Welcome to mirror list, hosted at ThFree Co, Russian Federation.

authenticate.cc « filed « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cf0b3a7deba45127a28f509ec1d3a3d9a402a2a (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
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2012 Planets Communications B.V.
   Copyright (C) 2013-2022 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
// Kern Sibbald, October 2000
/**
 * @file
 * Authenticate Director who is attempting to connect.
 */

#include "include/bareos.h"
#include "filed/filed.h"
#include "filed/filed_globals.h"
#include "filed/jcr_private.h"
#include "filed/restore.h"
#include "lib/bnet.h"
#include "lib/bsock.h"
#include "lib/parse_conf.h"
#include "lib/util.h"

namespace filedaemon {

const int debuglevel = 50;

/* Version at end of Hello
 *   prior to 10Mar08 no version
 *   1 10Mar08
 *   2 13Mar09 - Added the ability to restore from multiple storages
 *   3 03Sep10 - Added the restore object command for vss plugin 4.0
 *   4 25Nov10 - Added bandwidth command 5.1
 *   5 24Nov11 - Added new restore object command format (pluginname) 6.0
 *
 *  51 21Mar13 - Added reverse datachannel initialization
 *  52 13Jul13 - Added plugin options
 *  53 02Apr15 - Added setdebug timestamp
 *  54 29Oct15 - Added getSecureEraseCmd
 */
static char OK_hello_compat[] = "2000 OK Hello 5\n";
static char OK_hello[] = "2000 OK Hello 54\n";

static char Dir_sorry[] = "2999 Authentication failed.\n";

/**
 * To prevent DOS attacks,
 * wait a bit in case of an
 * authentication failure of a (remotely) initiated connection.
 */
static inline void delay()
{
  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

  // Single thread all failures to avoid DOS
  lock_mutex(mutex);
  Bmicrosleep(6, 0);
  unlock_mutex(mutex);
}

static inline void AuthenticateFailed(JobControlRecord* jcr, PoolMem& message)
{
  Dmsg0(debuglevel, message.c_str());
  Jmsg0(jcr, M_FATAL, 0, message.c_str());
  delay();
}

/**
 * Initiate the communications with the Director.
 * He has made a connection to our server.
 *
 * Basic tasks done here:
 * We read Director's initial message and authorize him.
 */
bool AuthenticateDirector(JobControlRecord* jcr)
{
  BareosSocket* dir = jcr->dir_bsock;

  PoolMem errormsg(PM_MESSAGE);
  PoolMem dirname(PM_MESSAGE);
  DirectorResource* director = NULL;

  if (dir->message_length < 25 || dir->message_length > 500) {
    char addr[64];
    char* who = BnetGetPeer(dir, addr, sizeof(addr)) ? dir->who() : addr;
    errormsg.bsprintf(_("Bad Hello command from Director at %s. Len=%d.\n"),
                      who, dir->message_length);
    AuthenticateFailed(jcr, errormsg);
    return false;
  }

  if (sscanf(dir->msg, "Hello Director %s calling",
             dirname.check_size(dir->message_length))
      != 1) {
    char addr[64];
    char* who = BnetGetPeer(dir, addr, sizeof(addr)) ? dir->who() : addr;
    dir->msg[100] = 0;
    errormsg.bsprintf(_("Bad Hello command from Director at %s: %s\n"), who,
                      dir->msg);
    AuthenticateFailed(jcr, errormsg);
    return false;
  }

  UnbashSpaces(dirname.c_str());
  director = (DirectorResource*)my_config->GetResWithName(R_DIRECTOR,
                                                          dirname.c_str());

  if (!director) {
    char addr[64];
    char* who = BnetGetPeer(dir, addr, sizeof(addr)) ? dir->who() : addr;
    errormsg.bsprintf(
        _("Connection from unknown Director %s at %s rejected.\n"),
        dirname.c_str(), who);
    AuthenticateFailed(jcr, errormsg);
    return false;
  }

  if (!director->conn_from_dir_to_fd) {
    errormsg.bsprintf(_("Connection from Director %s rejected.\n"),
                      dirname.c_str());
    AuthenticateFailed(jcr, errormsg);
    return false;
  }

  if (!dir->AuthenticateInboundConnection(jcr, my_config, dirname.c_str(),
                                          director->password_, director)) {
    dir->fsend("%s", Dir_sorry);
    errormsg.bsprintf(_("Unable to authenticate Director %s.\n"),
                      dirname.c_str());
    AuthenticateFailed(jcr, errormsg);
    return false;
  }

  jcr->impl->director = director;

  return dir->fsend("%s", (me->compatible) ? OK_hello_compat : OK_hello);
}

// Authenticate with a remote director.
bool AuthenticateWithDirector(JobControlRecord* jcr, DirectorResource* director)
{
  return jcr->dir_bsock->AuthenticateOutboundConnection(
      jcr, my_config->CreateOwnQualifiedNameForNetworkDump(),
      me->resource_name_, director->password_, director);
}

// Authenticate a remote storage daemon.
bool AuthenticateStoragedaemon(JobControlRecord* jcr)
{
  bool result = false;
  BareosSocket* sd = jcr->store_bsock;
  s_password password;

  password.encoding = p_encoding_md5;
  password.value = jcr->sd_auth_key;
  result = sd->AuthenticateInboundConnection(jcr, my_config, jcr->client_name,
                                             password, me);

  // Destroy session key
  memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
  if (!result) { delay(); }

  return result;
}

// Authenticate with a remote storage daemon.
bool AuthenticateWithStoragedaemon(JobControlRecord* jcr)
{
  bool result = false;
  BareosSocket* sd = jcr->store_bsock;
  s_password password;

  password.encoding = p_encoding_md5;
  password.value = jcr->sd_auth_key;
  result = sd->AuthenticateOutboundConnection(
      jcr, my_config->CreateOwnQualifiedNameForNetworkDump(),
      (char*)jcr->client_name, password, me);

  // Destroy session key
  memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));

  return result;
}
} /* namespace filedaemon */