GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-password.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
39OCTAVE_NORETURN static
40void
41err_invalid ()
42{
43 (*current_liboctave_error_handler) ("invalid password object");
44}
45
47
49
50std::string
52{
53 if (! ok ())
54 err_invalid ();
55
56 return m_name;
57}
58
59std::string
61{
62 if (! ok ())
63 err_invalid ();
64
65 return m_passwd;
66}
67
68uid_t
70{
71 if (! ok ())
72 err_invalid ();
73
74 return m_uid;
75}
76
77gid_t
79{
80 if (! ok ())
81 err_invalid ();
82
83 return m_gid;
84}
85
86std::string
88{
89 if (! ok ())
90 err_invalid ();
91
92 return m_gecos;
93}
94
95std::string
97{
98 if (! ok ())
99 err_invalid ();
100
101 return m_dir;
102}
103
104std::string
106{
107 if (! ok ())
108 err_invalid ();
109
110 return m_shell;
111}
112
115{
116 std::string msg;
117 return getpwent (msg);
118}
119
121password::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
134{
135 std::string msg;
136 return getpwuid (uid, msg);
137}
138
140password::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
154password::getpwnam (const std::string& nm)
155{
156 std::string msg;
157 return getpwnam (nm, msg);
158}
159
161password::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
174int
176{
177 std::string msg;
178 return setpwent (msg);
179}
180
181int
182password::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
194int
196{
197 std::string msg;
198 return endpwent (msg);
199}
200
201int
202password::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
214password::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
243OCTAVE_END_NAMESPACE(sys)
244OCTAVE_END_NAMESPACE(octave)
static password getpwnam(const std::string &nm)
static int endpwent()
static password getpwuid(uid_t uid)
std::string gecos() const
static int setpwent()
std::string dir() const
std::string name() const
std::string shell() const
bool ok() const
static password getpwent()
uid_t uid() const
gid_t gid() const
std::string passwd() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define NOT_SUPPORTED(nm)
Definition oct-group.cc:40
void octave_setpwent_wrapper(void)
void octave_endpwent_wrapper(void)
struct passwd * octave_getpwent_wrapper(void)
struct passwd * octave_getpwnam_wrapper(const char *nam)
struct passwd * octave_getpwuid_wrapper(uid_t uid)
void octave_from_passwd(const struct passwd *pw, struct octave_passwd_wrapper *oct_pw)