2014-10-09 19:50:34 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
#include "osquery/core.h"
|
|
|
|
#include "osquery/database.h"
|
|
|
|
|
2014-10-10 01:08:18 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
|
2014-10-09 19:50:34 +00:00
|
|
|
namespace osquery {
|
|
|
|
namespace tables {
|
|
|
|
|
2014-10-10 22:09:14 +00:00
|
|
|
std::mutex pwdEnumerationMutex;
|
|
|
|
|
2014-10-09 19:50:34 +00:00
|
|
|
QueryData genUsers() {
|
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;
|
|
|
|
|
2014-10-09 19:50:34 +00:00
|
|
|
while ((pwd = getpwent()) != NULL) {
|
2014-10-10 01:55:25 +00:00
|
|
|
if (std::find(users_in.begin(), users_in.end(), pwd->pw_uid) == users_in.end()) {
|
|
|
|
Row r;
|
|
|
|
r["uid"] = boost::lexical_cast<std::string>(pwd->pw_uid);
|
|
|
|
r["gid"] = boost::lexical_cast<std::string>(pwd->pw_gid);
|
|
|
|
r["username"] = std::string(pwd->pw_name);
|
|
|
|
r["description"] = std::string(pwd->pw_gecos);
|
|
|
|
r["directory"] = std::string(pwd->pw_dir);
|
|
|
|
r["shell"] = std::string(pwd->pw_shell);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|