blob: 81903df9370b652d44895b1db3fab5c0ae35b375 [file] [log] [blame]
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05301#pragma once
2
3#include <stdio.h>
4#include <cassert>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05305#include <shadow.h>
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05306#include <phosphor-logging/log.hpp>
7#include <phosphor-logging/elog.hpp>
8#include <xyz/openbmc_project/Common/error.hpp>
9#include <phosphor-logging/elog-errors.hpp>
10namespace phosphor
11{
12namespace user
13{
14namespace shadow
15{
16
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053017using InternalFailure =
18 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053019using namespace phosphor::logging;
20
21/** @class Lock
22 * @brief Responsible for locking and unlocking /etc/shadow
23 */
24class Lock
25{
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053026 public:
27 Lock(const Lock&) = delete;
28 Lock& operator=(const Lock&) = delete;
29 Lock(Lock&&) = delete;
30 Lock& operator=(Lock&&) = delete;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053031
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053032 /** @brief Default constructor that just locks the shadow file */
33 Lock()
34 {
35 if (!lckpwdf())
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053036 {
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053037 log<level::ERR>("Locking Shadow failed");
38 elog<InternalFailure>();
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053039 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053040 }
41 ~Lock()
42 {
43 if (!ulckpwdf())
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053044 {
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053045 log<level::ERR>("Un-Locking Shadow failed");
46 elog<InternalFailure>();
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053047 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053048 }
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053049};
50
51} // namespace shadow
52} // namespace user
53} // namespace phosphor