blob: 854874d0473b10c97dfac6d25f0060f66eb3406b [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>
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08008#include <phosphor-logging/lg2.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06009#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 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +080039 lg2::error("Failed to lock shadow file");
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053040 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 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +080047 lg2::error("Failed to unlock shadow file");
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053048 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