blob: f2749669c449d9a750d4564c93220ffed588992a [file] [log] [blame]
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05301#pragma once
2
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05303#include <shadow.h>
Patrick Williams9638afb2021-02-22 17:16:24 -06004#include <stdio.h>
5
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05306#include <phosphor-logging/elog-errors.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06007#include <phosphor-logging/elog.hpp>
8#include <phosphor-logging/log.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10
11#include <cassert>
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053012namespace phosphor
13{
14namespace user
15{
16namespace shadow
17{
18
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053019using InternalFailure =
20 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053021using namespace phosphor::logging;
22
23/** @class Lock
24 * @brief Responsible for locking and unlocking /etc/shadow
25 */
26class Lock
27{
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053028 public:
29 Lock(const Lock&) = delete;
30 Lock& operator=(const Lock&) = delete;
31 Lock(Lock&&) = delete;
32 Lock& operator=(Lock&&) = delete;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053033
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053034 /** @brief Default constructor that just locks the shadow file */
35 Lock()
36 {
37 if (!lckpwdf())
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053038 {
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053039 log<level::ERR>("Locking Shadow failed");
40 elog<InternalFailure>();
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053041 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053042 }
43 ~Lock()
44 {
45 if (!ulckpwdf())
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053046 {
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053047 log<level::ERR>("Un-Locking Shadow failed");
48 elog<InternalFailure>();
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053049 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053050 }
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053051};
52
53} // namespace shadow
54} // namespace user
55} // namespace phosphor