summaryrefslogtreecommitdiffstats
path: root/remoting/host/user_authenticator_mac.cc
blob: 85685cf2ece0dedc9c379aab8aecee7cfa8fc8d4 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/user_authenticator.h"

#include <Security/Security.h>

#include <string>

#include "base/basictypes.h"
#include "base/mac/mac_logging.h"

namespace remoting {

namespace {

class UserAuthenticatorMac : public UserAuthenticator {
 public:
  UserAuthenticatorMac() {}
  virtual ~UserAuthenticatorMac() {}
  virtual bool Authenticate(const std::string& username,
                            const std::string& password);

 private:
  DISALLOW_COPY_AND_ASSIGN(UserAuthenticatorMac);
};

const char kAuthorizationRightName[] = "system.login.tty";

bool UserAuthenticatorMac::Authenticate(const std::string& username,
                                        const std::string& password) {
  // The authorization right being requested.  This particular right allows
  // testing of a username/password, as if the user were logging on to the
  // system locally.
  AuthorizationItem right;
  right.name = kAuthorizationRightName;
  right.valueLength = 0;
  right.value = NULL;
  right.flags = 0;
  AuthorizationRights rights;
  rights.count = 1;
  rights.items = &right;

  // Passing the username/password as an "environment" parameter causes these
  // to be submitted to the Security Framework, instead of the interactive
  // password prompt appearing on the host system.  Valid on OS X 10.4 and
  // later versions.
  AuthorizationItem environment_items[2];
  environment_items[0].name = kAuthorizationEnvironmentUsername;
  environment_items[0].valueLength = username.size();
  environment_items[0].value = const_cast<char*>(username.data());
  environment_items[0].flags = 0;
  environment_items[1].name = kAuthorizationEnvironmentPassword;
  environment_items[1].valueLength = password.size();
  environment_items[1].value = const_cast<char*>(password.data());
  environment_items[1].flags = 0;
  AuthorizationEnvironment environment;
  environment.count = 2;
  environment.items = environment_items;

  OSStatus status = AuthorizationCreate(&rights, &environment,
                                        kAuthorizationFlagExtendRights,
                                        NULL);
  switch (status) {
    case errAuthorizationSuccess:
      return true;

    case errAuthorizationDenied:
      return false;

    default:
      OSSTATUS_LOG(ERROR, status) << "AuthorizationCreate";
      return false;
  }
}

}  // namespace

// static
UserAuthenticator* UserAuthenticator::Create() {
  return new UserAuthenticatorMac();
}

}  // namespace remoting