~ubuntu-branches/ubuntu/intrepid/schroot/intrepid

« back to all changes in this revision

Viewing changes to schroot/sbuild-auth.h

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-07-08 18:33:28 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060708183328-rlo4mpldmyoda55q
Tags: 0.99.2-2ubuntu1
* remerge ubuntu changes:
  + debian/control: libpam-dev (>> 0.79-3ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
2
 
 *
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.
7
 
 *
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.
12
 
 *
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,
16
 
 * MA  02111-1307  USA
17
 
 *
18
 
 *********************************************************************/
19
 
 
20
 
#ifndef SBUILD_AUTH_H
21
 
#define SBUILD_AUTH_H
22
 
 
23
 
#include "sbuild-config.h"
24
 
 
25
 
#include <string>
26
 
#include <vector>
27
 
 
28
 
#ifdef HAVE_TR1_MEMORY
29
 
#include <tr1/memory>
30
 
#elif HAVE_BOOST_SHARED_PTR_HPP
31
 
#include <boost/shared_ptr.hpp>
32
 
namespace std { namespace tr1 { using boost::shared_ptr; } }
33
 
#else
34
 
#error A shared_ptr implementation is not available
35
 
#endif
36
 
 
37
 
#include <sys/types.h>
38
 
#include <sys/wait.h>
39
 
#include <grp.h>
40
 
#include <pwd.h>
41
 
#include <unistd.h>
42
 
 
43
 
#include <security/pam_appl.h>
44
 
 
45
 
#include "sbuild-auth-conv.h"
46
 
#include "sbuild-environment.h"
47
 
#include "sbuild-error.h"
48
 
#include "sbuild-types.h"
49
 
 
50
 
namespace sbuild
51
 
{
52
 
 
53
 
  /**
54
 
   * @brief Authentication handler.
55
 
   *
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.
59
 
   *
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.
64
 
   *
65
 
   * The correct order is
66
 
   * - start
67
 
   * - authenticate
68
 
   * - setupenv
69
 
   * - account
70
 
   * - cred_establish
71
 
   * - open_session
72
 
   *
73
 
   * After the session has finished, or if an error occured, the
74
 
   * corresponding cleanup methods should be called
75
 
   * - close_session
76
 
   * - sbuild
77
 
   * - cred_delete
78
 
   * - stop
79
 
   *
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
83
 
   * must still be used.
84
 
   */
85
 
  class auth
86
 
  {
87
 
  public:
88
 
    /// Authentication status
89
 
    enum status
90
 
      {
91
 
        STATUS_NONE, ///< Authentication is not required.
92
 
        STATUS_USER, ///< Authentication is required by the user.
93
 
        STATUS_FAIL  ///< Authentication has failed.
94
 
      };
95
 
 
96
 
    /// Message verbosity
97
 
    enum verbosity
98
 
      {
99
 
        VERBOSITY_QUIET,  ///< Only print essential messages.
100
 
        VERBOSITY_NORMAL, ///< Print messages (the default).
101
 
        VERBOSITY_VERBOSE ///< Print all messages.
102
 
      };
103
 
 
104
 
    /// Exception type.
105
 
    typedef runtime_error_custom<auth> error;
106
 
 
107
 
    /// A shared_ptr to an auth_conv object.
108
 
    typedef std::tr1::shared_ptr<auth_conv> conv_ptr;
109
 
 
110
 
    /**
111
 
     * The constructor.
112
 
     *
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.
117
 
     */
118
 
    auth (std::string const& service_name);
119
 
 
120
 
    /**
121
 
     * The destructor.
122
 
     */
123
 
    virtual ~auth ();
124
 
 
125
 
    /**
126
 
     * Get the PAM service name.
127
 
     *
128
 
     * @returns the service name.
129
 
     */
130
 
    std::string const&
131
 
    get_service () const;
132
 
 
133
 
    /**
134
 
     * Get the uid of the user.  This is the uid to run as in the *
135
 
     * session.
136
 
     *
137
 
     * @returns a uid.  This will be 0 if no user was set, or the user
138
 
     * is uid 0.
139
 
     */
140
 
    uid_t
141
 
    get_uid () const;
142
 
 
143
 
    /**
144
 
     * Get the gid of the user.  This is the gid to run as in the
145
 
     * session.
146
 
     *
147
 
     * @returns a gid.  This will be 0 if no user was set, or the user
148
 
     * is gid 0.
149
 
     */
150
 
    gid_t
151
 
    get_gid () const;
152
 
 
153
 
    /**
154
 
     * Get the name of the user.  This is the user to run as in the
155
 
     * session.
156
 
     *
157
 
     * @returns the user's name.
158
 
     */
159
 
    std::string const&
160
 
    get_user () const;
161
 
 
162
 
    /**
163
 
     * Set the name of the user.  This is the user to run as in the
164
 
     * session.
165
 
     *
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.
169
 
     *
170
 
     * @param user the name to set.
171
 
     */
172
 
    void
173
 
    set_user (std::string const& user);
174
 
 
175
 
    /**
176
 
     * Get the command to run in the session.
177
 
     *
178
 
     * @returns the command as string list, each item being a separate
179
 
     * argument.  If no command has been specified, the list will be
180
 
     * empty.
181
 
     */
182
 
    string_list const&
183
 
    get_command () const;
184
 
 
185
 
    /**
186
 
     * Set the command to run in the session.
187
 
     *
188
 
     * @param command the command to run.  This is a string list, each
189
 
     * item being a separate argument.
190
 
     */
191
 
    void
192
 
    set_command (string_list const& command);
193
 
 
194
 
    /**
195
 
     * Get the home directory.  This is the $HOME to set in the session,
196
 
     * if the user environment is not being preserved.
197
 
     *
198
 
     * @returns the home directory.
199
 
     */
200
 
    std::string const&
201
 
    get_home () const;
202
 
 
203
 
    /**
204
 
     * Get the name of the shell.  This is the shell to run in the
205
 
     * session.
206
 
     *
207
 
     * @returns the shell.  This is typically a full pathname, though
208
 
     * the executable name only should also work (the caller will have
209
 
     * to search for it).
210
 
     */
211
 
    std::string const&
212
 
    get_shell () const;
213
 
 
214
 
    /**
215
 
     * Get the environment to use in the session.
216
 
     *
217
 
     * @returns an environment list (a list of key-value pairs).
218
 
     */
219
 
    environment const&
220
 
    get_environment () const;
221
 
 
222
 
    /**
223
 
     * Set the environment to use in the session.
224
 
     *
225
 
     * @param environment an environ- or envp-like string vector
226
 
     * containing key=value pairs.
227
 
     */
228
 
    void
229
 
    set_environment (char **environment);
230
 
 
231
 
    /**
232
 
     * Set the environment to use in the session.
233
 
     *
234
 
     * @param environment an environment list.
235
 
     */
236
 
    void
237
 
    set_environment (environment const& environment);
238
 
 
239
 
    /**
240
 
     * Get the PAM environment.  This is the environment as set by PAM
241
 
     * modules.
242
 
     *
243
 
     * @returns an environment list.
244
 
     */
245
 
    environment
246
 
    get_pam_environment () const;
247
 
 
248
 
    /**
249
 
     * Get the "remote uid" of the user.  This is the uid which is
250
 
     * requesting authentication.
251
 
     *
252
 
     * @returns a uid.
253
 
     */
254
 
    uid_t
255
 
    get_ruid () const;
256
 
 
257
 
    /**
258
 
     * Get the "remote" name of the user.  This is the user which is
259
 
     * requesting authentication.
260
 
     *
261
 
     * @returns a user name.
262
 
     */
263
 
    std::string const&
264
 
    get_ruser () const;
265
 
 
266
 
    /**
267
 
     * Get the message verbosity.
268
 
     *
269
 
     * Returns the verbosity level.
270
 
     */
271
 
    verbosity
272
 
    get_verbosity () const;
273
 
 
274
 
    /**
275
 
     * Set the message verbosity.
276
 
     *
277
 
     * @param verbosity the verbosity level.
278
 
     */
279
 
    void
280
 
    set_verbosity (verbosity verbosity);
281
 
 
282
 
    /**
283
 
     * Get the conversation handler.
284
 
     *
285
 
     * @returns a shared_ptr to the handler.
286
 
     */
287
 
    conv_ptr&
288
 
    get_conv ();
289
 
 
290
 
    /**
291
 
     * Set the conversation handler.
292
 
     *
293
 
     * @param conv a shared_ptr to the handler.
294
 
     */
295
 
    void
296
 
    set_conv (conv_ptr& conv);
297
 
 
298
 
    /**
299
 
     * Run a session.  The user will be asked for authentication if
300
 
     * required, and then the run_impl virtual method will be called.
301
 
     *
302
 
     * An error will be thrown on failure.
303
 
     */
304
 
    void
305
 
    run ();
306
 
 
307
 
    /**
308
 
     * Start the PAM system.  No other PAM functions may be called before
309
 
     * calling this function.
310
 
     *
311
 
     * An error will be thrown on failure.
312
 
     */
313
 
    void
314
 
    start ();
315
 
 
316
 
    /**
317
 
     * Stop the PAM system.  No other PAM functions may be used after
318
 
     * calling this function.
319
 
     *
320
 
     * An error will be thrown on failure.
321
 
     */
322
 
    void
323
 
    stop ();
324
 
 
325
 
    /**
326
 
     * Perform PAM authentication.  If required, the user will be
327
 
     * prompted to authenticate themselves.
328
 
     *
329
 
     * An error will be thrown on failure.
330
 
     */
331
 
    void
332
 
    authenticate ();
333
 
 
334
 
    /**
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.
338
 
     *
339
 
     * An error will be thrown on failure.
340
 
     */
341
 
    void
342
 
    setupenv ();
343
 
 
344
 
    /**
345
 
     * Do PAM account management (authorisation).
346
 
     *
347
 
     * An error will be thrown on failure.
348
 
     */
349
 
    void
350
 
    account ();
351
 
 
352
 
    /**
353
 
     * Use PAM to establish credentials.
354
 
     *
355
 
     * An error will be thrown on failure.
356
 
     */
357
 
    void
358
 
    cred_establish ();
359
 
 
360
 
    /**
361
 
     * Use PAM to delete credentials.
362
 
     *
363
 
     * An error will be thrown on failure.
364
 
     */
365
 
    void
366
 
    cred_delete ();
367
 
 
368
 
    /**
369
 
     * Open a PAM session.
370
 
     *
371
 
     * An error will be thrown on failure.
372
 
     */
373
 
    void
374
 
    open_session ();
375
 
 
376
 
    /**
377
 
     * Close a PAM session.
378
 
     *
379
 
     * An error will be thrown on failure.
380
 
     */
381
 
    void
382
 
    close_session ();
383
 
 
384
 
protected:
385
 
    /**
386
 
     * Check if authentication is required.  This default
387
 
     * implementation always requires authentication.
388
 
     */
389
 
    virtual status
390
 
    get_auth_status () const;
391
 
 
392
 
    /**
393
 
     * Run session.  The code to run when authentication and
394
 
     * authorisation have been completed.
395
 
     */
396
 
    virtual void
397
 
    run_impl () = 0;
398
 
 
399
 
  public:
400
 
    /**
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).
404
 
     *
405
 
     * @param oldauth the current authentication status.
406
 
     * @param newauth the new authentication status.
407
 
     * @returns the new authentication status.
408
 
     */
409
 
    status
410
 
    change_auth (status oldauth,
411
 
                 status newauth) const
412
 
    {
413
 
      /* Ensure auth level always escalates. */
414
 
      if (newauth > oldauth)
415
 
        return newauth;
416
 
      else
417
 
        return oldauth;
418
 
    }
419
 
 
420
 
  protected:
421
 
    /// The PAM handle.
422
 
    pam_handle_t      *pam;
423
 
 
424
 
  private:
425
 
    /// The PAM service name.
426
 
    const std::string  service;
427
 
    /// The uid to run as.
428
 
    uid_t              uid;
429
 
    /// The gid to run as.
430
 
    gid_t              gid;
431
 
    /// The user name to run as.
432
 
    std::string        user;
433
 
    /// The command to run.
434
 
    string_list        command;
435
 
    /// The home directory to run in.
436
 
    std::string        home;
437
 
    /// The user shell to run.
438
 
    std::string        shell;
439
 
    /// The user environment to set.
440
 
    environment        user_environment;
441
 
    /// The uid requesting authentication.
442
 
    uid_t              ruid;
443
 
    /// The user name requesting authentication.
444
 
    std::string        ruser;
445
 
    /// The PAM conversation handler.
446
 
    conv_ptr           conv;
447
 
    /// The message verbosity.
448
 
    verbosity          message_verbosity;
449
 
  };
450
 
 
451
 
}
452
 
 
453
 
#endif /* SBUILD_AUTH_H */
454
 
 
455
 
/*
456
 
 * Local Variables:
457
 
 * mode:C++
458
 
 * End:
459
 
 */