2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-10-09 19:50:34 +00:00
|
|
|
|
2014-10-10 01:55:25 +00:00
|
|
|
#include <set>
|
2014-10-10 22:09:14 +00:00
|
|
|
#include <mutex>
|
2014-10-09 19:50:34 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
2014-11-26 00:28:10 +00:00
|
|
|
#include <pwd.h>
|
2014-10-09 19:50:34 +00:00
|
|
|
|
2014-12-03 23:14:02 +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 {
|
|
|
|
|
2014-10-10 22:09:14 +00:00
|
|
|
std::mutex pwdEnumerationMutex;
|
|
|
|
|
2014-11-26 00:28:10 +00:00
|
|
|
QueryData genUsers(QueryContext& context) {
|
2014-10-10 22:09:14 +00:00
|
|
|
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;
|
2014-10-10 01:55:25 +00:00
|
|
|
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()) {
|
2014-10-10 01:55:25 +00:00
|
|
|
Row r;
|
2014-11-26 00:28:10 +00:00
|
|
|
r["uid"] = BIGINT(pwd->pw_uid);
|
|
|
|
r["gid"] = BIGINT(pwd->pw_gid);
|
2014-12-01 16:52:13 +00:00
|
|
|
r["uid_signed"] = BIGINT((int32_t) pwd->pw_uid);
|
|
|
|
r["gid_signed"] = BIGINT((int32_t) pwd->pw_gid);
|
2014-11-26 00:28:10 +00:00
|
|
|
r["username"] = TEXT(pwd->pw_name);
|
|
|
|
r["description"] = TEXT(pwd->pw_gecos);
|
|
|
|
r["directory"] = TEXT(pwd->pw_dir);
|
|
|
|
r["shell"] = TEXT(pwd->pw_shell);
|
2014-10-10 01:55:25 +00:00
|
|
|
results.push_back(r);
|
|
|
|
users_in.insert(pwd->pw_uid);
|
|
|
|
}
|
2014-10-09 19:50:34 +00:00
|
|
|
}
|
|
|
|
endpwent();
|
2014-10-10 01:55:25 +00:00
|
|
|
users_in.clear();
|
2014-10-09 19:50:34 +00:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|