1
/* Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
3
* schroot is free software; you can redistribute it and/or modify it
4
* under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* schroot is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
23
#include "sbuild-config.h"
28
#ifdef HAVE_TR1_MEMORY
30
#elif HAVE_BOOST_SHARED_PTR_HPP
31
#include <boost/shared_ptr.hpp>
32
namespace std { namespace tr1 { using boost::shared_ptr; } }
34
#error A shared_ptr implementation is not available
37
#include <sys/types.h>
43
#include <security/pam_appl.h>
45
#include "sbuild-auth-conv.h"
46
#include "sbuild-environment.h"
47
#include "sbuild-error.h"
48
#include "sbuild-types.h"
54
* @brief Authentication handler.
56
* auth handles user authentication, authorisation and session
57
* management using the Pluggable authentication Modules (PAM)
58
* library. It is essentially an object-oriented wrapper around PAM.
60
* In order to use PAM correctly, it is important to call several of
61
* the methods in the correct order. For example, it is not possible
62
* to authorise a user before authenticating a user, and a session may
63
* not be started before either of these have occured.
65
* The correct order is
73
* After the session has finished, or if an error occured, the
74
* corresponding cleanup methods should be called
80
* The run method will handle all this. The run_impl virtual
81
* function should be used to provide a session handler to open and
82
* close the session for the user. open_session and close_session
88
/// Authentication status
91
STATUS_NONE, ///< Authentication is not required.
92
STATUS_USER, ///< Authentication is required by the user.
93
STATUS_FAIL ///< Authentication has failed.
99
VERBOSITY_QUIET, ///< Only print essential messages.
100
VERBOSITY_NORMAL, ///< Print messages (the default).
101
VERBOSITY_VERBOSE ///< Print all messages.
105
typedef runtime_error_custom<auth> error;
107
/// A shared_ptr to an auth_conv object.
108
typedef std::tr1::shared_ptr<auth_conv> conv_ptr;
113
* @param service_name the PAM service name. This should be a
114
* hard-coded constant string literal for safety and security.
115
* This is passed to pam_start() when initialising PAM, and is
116
* used to load the correct configuration file from /etc/pam.d.
118
auth (std::string const& service_name);
126
* Get the PAM service name.
128
* @returns the service name.
131
get_service () const;
134
* Get the uid of the user. This is the uid to run as in the *
137
* @returns a uid. This will be 0 if no user was set, or the user
144
* Get the gid of the user. This is the gid to run as in the
147
* @returns a gid. This will be 0 if no user was set, or the user
154
* Get the name of the user. This is the user to run as in the
157
* @returns the user's name.
163
* Set the name of the user. This is the user to run as in the
166
* As a side effect, the uid, gid, home and shell member variables
167
* will also be set, so calling the corresponding get methods will
168
* now return meaningful values.
170
* @param user the name to set.
173
set_user (std::string const& user);
176
* Get the command to run in the session.
178
* @returns the command as string list, each item being a separate
179
* argument. If no command has been specified, the list will be
183
get_command () const;
186
* Set the command to run in the session.
188
* @param command the command to run. This is a string list, each
189
* item being a separate argument.
192
set_command (string_list const& command);
195
* Get the home directory. This is the $HOME to set in the session,
196
* if the user environment is not being preserved.
198
* @returns the home directory.
204
* Get the name of the shell. This is the shell to run in the
207
* @returns the shell. This is typically a full pathname, though
208
* the executable name only should also work (the caller will have
215
* Get the environment to use in the session.
217
* @returns an environment list (a list of key-value pairs).
220
get_environment () const;
223
* Set the environment to use in the session.
225
* @param environment an environ- or envp-like string vector
226
* containing key=value pairs.
229
set_environment (char **environment);
232
* Set the environment to use in the session.
234
* @param environment an environment list.
237
set_environment (environment const& environment);
240
* Get the PAM environment. This is the environment as set by PAM
243
* @returns an environment list.
246
get_pam_environment () const;
249
* Get the "remote uid" of the user. This is the uid which is
250
* requesting authentication.
258
* Get the "remote" name of the user. This is the user which is
259
* requesting authentication.
261
* @returns a user name.
267
* Get the message verbosity.
269
* Returns the verbosity level.
272
get_verbosity () const;
275
* Set the message verbosity.
277
* @param verbosity the verbosity level.
280
set_verbosity (verbosity verbosity);
283
* Get the conversation handler.
285
* @returns a shared_ptr to the handler.
291
* Set the conversation handler.
293
* @param conv a shared_ptr to the handler.
296
set_conv (conv_ptr& conv);
299
* Run a session. The user will be asked for authentication if
300
* required, and then the run_impl virtual method will be called.
302
* An error will be thrown on failure.
308
* Start the PAM system. No other PAM functions may be called before
309
* calling this function.
311
* An error will be thrown on failure.
317
* Stop the PAM system. No other PAM functions may be used after
318
* calling this function.
320
* An error will be thrown on failure.
326
* Perform PAM authentication. If required, the user will be
327
* prompted to authenticate themselves.
329
* An error will be thrown on failure.
335
* Import the user environment into PAM. If no environment was
336
* specified with set_environment, a minimal environment will be
337
* created containing HOME, LOGNAME, PATH, TERM and LOGNAME.
339
* An error will be thrown on failure.
345
* Do PAM account management (authorisation).
347
* An error will be thrown on failure.
353
* Use PAM to establish credentials.
355
* An error will be thrown on failure.
361
* Use PAM to delete credentials.
363
* An error will be thrown on failure.
369
* Open a PAM session.
371
* An error will be thrown on failure.
377
* Close a PAM session.
379
* An error will be thrown on failure.
386
* Check if authentication is required. This default
387
* implementation always requires authentication.
390
get_auth_status () const;
393
* Run session. The code to run when authentication and
394
* authorisation have been completed.
401
* Set new authentication status. If newauth > oldauth, newauth
402
* is returned, otherwise oldauth is returned. This is to ensure
403
* the authentication status can never be decreased (relaxed).
405
* @param oldauth the current authentication status.
406
* @param newauth the new authentication status.
407
* @returns the new authentication status.
410
change_auth (status oldauth,
411
status newauth) const
413
/* Ensure auth level always escalates. */
414
if (newauth > oldauth)
425
/// The PAM service name.
426
const std::string service;
427
/// The uid to run as.
429
/// The gid to run as.
431
/// The user name to run as.
433
/// The command to run.
435
/// The home directory to run in.
437
/// The user shell to run.
439
/// The user environment to set.
440
environment user_environment;
441
/// The uid requesting authentication.
443
/// The user name requesting authentication.
445
/// The PAM conversation handler.
447
/// The message verbosity.
448
verbosity message_verbosity;
453
#endif /* SBUILD_AUTH_H */