osquery-1/osquery/tables/system/linux/users.cpp

55 lines
1.3 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
2015-05-12 06:31:13 +00:00
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2014-10-09 19:50:34 +00:00
#include <set>
#include <mutex>
2014-10-09 19:50:34 +00:00
#include <vector>
#include <string>
#include <pwd.h>
2014-10-09 19:50:34 +00:00
#include <osquery/core.h>
#include <osquery/tables.h>
2014-10-10 01:08:18 +00:00
2014-10-09 19:50:34 +00:00
namespace osquery {
namespace tables {
std::mutex pwdEnumerationMutex;
QueryData genUsers(QueryContext& context) {
std::lock_guard<std::mutex> lock(pwdEnumerationMutex);
2014-10-09 19:50:34 +00:00
QueryData results;
2014-10-10 01:08:18 +00:00
struct passwd *pwd = nullptr;
std::set<long> users_in;
2015-04-17 00:40:19 +00:00
while ((pwd = getpwent()) != nullptr) {
2014-10-28 00:37:36 +00:00
if (std::find(users_in.begin(), users_in.end(), pwd->pw_uid) ==
users_in.end()) {
Row r;
r["uid"] = BIGINT(pwd->pw_uid);
r["gid"] = BIGINT(pwd->pw_gid);
r["uid_signed"] = BIGINT((int32_t) pwd->pw_uid);
r["gid_signed"] = BIGINT((int32_t) pwd->pw_gid);
r["username"] = TEXT(pwd->pw_name);
r["description"] = TEXT(pwd->pw_gecos);
r["directory"] = TEXT(pwd->pw_dir);
r["shell"] = TEXT(pwd->pw_shell);
results.push_back(r);
users_in.insert(pwd->pw_uid);
}
2014-10-09 19:50:34 +00:00
}
endpwent();
users_in.clear();
2014-10-09 19:50:34 +00:00
return results;
}
}
}