GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-password.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <sys/types.h>
31 
32 #include "lo-error.h"
33 #include "oct-password.h"
34 #include "pwd-wrappers.h"
35 
36 #define NOT_SUPPORTED(nm) \
37  nm ": not supported on this system"
38 
39 OCTAVE_NORETURN static
40 void
41 err_invalid ()
42 {
43  (*current_liboctave_error_handler) ("invalid password object");
44 }
45 
47 
49 
50 std::string
52 {
53  if (! ok ())
54  err_invalid ();
55 
56  return m_name;
57 }
58 
59 std::string
61 {
62  if (! ok ())
63  err_invalid ();
64 
65  return m_passwd;
66 }
67 
68 uid_t
69 password::uid () const
70 {
71  if (! ok ())
72  err_invalid ();
73 
74  return m_uid;
75 }
76 
77 gid_t
78 password::gid () const
79 {
80  if (! ok ())
81  err_invalid ();
82 
83  return m_gid;
84 }
85 
86 std::string
88 {
89  if (! ok ())
90  err_invalid ();
91 
92  return m_gecos;
93 }
94 
95 std::string
96 password::dir () const
97 {
98  if (! ok ())
99  err_invalid ();
100 
101  return m_dir;
102 }
103 
104 std::string
106 {
107  if (! ok ())
108  err_invalid ();
109 
110  return m_shell;
111 }
112 
113 password
115 {
116  std::string msg;
117  return getpwent (msg);
118 }
119 
120 password
121 password::getpwent (std::string& msg)
122 {
123 #if defined HAVE_GETPWENT
124  msg = "";
125  return password (octave_getpwent_wrapper (), msg);
126 #else
127  msg = NOT_SUPPORTED ("getpwent");
128  return password ();
129 #endif
130 }
131 
132 password
134 {
135  std::string msg;
136  return getpwuid (uid, msg);
137 }
138 
139 password
140 password::getpwuid (uid_t uid, std::string& msg)
141 {
142 #if defined (HAVE_GETPWUID)
143  msg = "";
144  return password (octave_getpwuid_wrapper (uid), msg);
145 #else
146  octave_unused_parameter (uid);
147 
148  msg = NOT_SUPPORTED ("getpwuid");
149  return password ();
150 #endif
151 }
152 
153 password
154 password::getpwnam (const std::string& nm)
155 {
156  std::string msg;
157  return getpwnam (nm, msg);
158 }
159 
160 password
161 password::getpwnam (const std::string& nm, std::string& msg)
162 {
163 #if defined (HAVE_GETPWNAM)
164  msg = "";
165  return password (octave_getpwnam_wrapper (nm.c_str ()), msg);
166 #else
167  octave_unused_parameter (nm);
168 
169  msg = NOT_SUPPORTED ("getpwnam");
170  return password ();
171 #endif
172 }
173 
174 int
176 {
177  std::string msg;
178  return setpwent (msg);
179 }
180 
181 int
182 password::setpwent (std::string& msg)
183 {
184 #if defined (HAVE_SETPWENT)
185  msg = "";
187  return 0;
188 #else
189  msg = NOT_SUPPORTED ("setpwent");
190  return -1;
191 #endif
192 }
193 
194 int
196 {
197  std::string msg;
198  return endpwent (msg);
199 }
200 
201 int
202 password::endpwent (std::string& msg)
203 {
204 #if defined (HAVE_ENDPWENT)
205  msg = "";
207  return 0;
208 #else
209  msg = NOT_SUPPORTED ("endpwent");
210  return -1;
211 #endif
212 }
213 
214 password::password (void *p, std::string& msg)
215  : m_name (), m_passwd (), m_uid (0), m_gid (0), m_gecos (),
216  m_dir (), m_shell (), m_valid (false)
217 {
218 #if defined (HAVE_PWD_H)
219  msg = "";
220 
221  if (p)
222  {
223  struct octave_passwd_wrapper pw;
224  octave_from_passwd (static_cast<struct ::passwd *> (p), &pw);
225 
226  m_name = pw.pw_name;
227  m_passwd = pw.pw_passwd;
228  m_uid = pw.pw_uid;
229  m_gid = pw.pw_gid;
230  m_gecos = pw.pw_gecos;
231  m_dir = pw.pw_dir;
232  m_shell = pw.pw_shell;
233 
234  m_valid = true;
235  }
236 #else
237  octave_unused_parameter (p);
238 
239  msg = NOT_SUPPORTED ("password functions");
240 #endif
241 }
242 
243 OCTAVE_END_NAMESPACE(sys)
244 OCTAVE_END_NAMESPACE(octave)
static password getpwnam(const std::string &nm)
static int endpwent()
static password getpwuid(uid_t uid)
std::string gecos() const
Definition: oct-password.cc:87
static int setpwent()
std::string dir() const
Definition: oct-password.cc:96
std::string name() const
Definition: oct-password.cc:51
std::string shell() const
bool ok() const
Definition: oct-password.h:89
static password getpwent()
uid_t uid() const
Definition: oct-password.cc:69
gid_t gid() const
Definition: oct-password.cc:78
std::string passwd() const
Definition: oct-password.cc:60
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define NOT_SUPPORTED(nm)
Definition: oct-password.cc:36
void octave_setpwent_wrapper(void)
Definition: pwd-wrappers.c:70
struct passwd * octave_getpwnam_wrapper(const char *nam)
Definition: pwd-wrappers.c:59
void octave_endpwent_wrapper(void)
Definition: pwd-wrappers.c:78
struct passwd * octave_getpwent_wrapper(void)
Definition: pwd-wrappers.c:39
struct passwd * octave_getpwuid_wrapper(uid_t uid)
Definition: pwd-wrappers.c:48
void octave_from_passwd(const struct passwd *pw, struct octave_passwd_wrapper *oct_pw)
Definition: pwd-wrappers.c:86