1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#include "UserInfo.h"
29
30#include <pwd.h>
31#include <sys/types.h>
32#include <unistd.h>
33
34#include <vector>
35
36#include "Exception.h"
37#include "ExceptionInternal.h"
38
39namespace Hdfs {
40namespace Internal {
41
42UserInfo UserInfo::LocalUser() {
43 UserInfo retval;
44 uid_t uid, euid;
45 int bufsize;
46 struct passwd pwd, epwd, *result = NULL;
47 euid = geteuid();
48 uid = getuid();
49
50 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
51 THROW(InvalidParameter,
52 "Invalid input: \"sysconf\" function failed to get the configure with key \"_SC_GETPW_R_SIZE_MAX\".");
53 }
54
55 std::vector<char> buffer(bufsize);
56
57 if (getpwuid_r(euid, &epwd, &buffer[0], bufsize, &result) != 0 || !result) {
58 THROW(InvalidParameter,
59 "Invalid input: effective user name cannot be found with UID %u.",
60 euid);
61 }
62
63 retval.setEffectiveUser(epwd.pw_name);
64
65 if (getpwuid_r(uid, &pwd, &buffer[0], bufsize, &result) != 0 || !result) {
66 THROW(InvalidParameter,
67 "Invalid input: real user name cannot be found with UID %u.",
68 uid);
69 }
70
71 retval.setRealUser(pwd.pw_name);
72 return retval;
73}
74
75size_t UserInfo::hash_value() const {
76 size_t values[] = { StringHasher(realUser), effectiveUser.hash_value() };
77 return CombineHasher(values, sizeof(values) / sizeof(values[0]));
78}
79
80}
81}
82