xref: /freebsd/contrib/pam-krb5/pam-util/args.c (revision bf6873c5786e333d679a7838d28812febf479a8a)
1*bf6873c5SCy Schubert /*
2*bf6873c5SCy Schubert  * Constructor and destructor for PAM data.
3*bf6873c5SCy Schubert  *
4*bf6873c5SCy Schubert  * The PAM utility functions often need an initial argument that encapsulates
5*bf6873c5SCy Schubert  * the PAM handle, some configuration information, and possibly a Kerberos
6*bf6873c5SCy Schubert  * context.  This implements a constructor and destructor for that data
7*bf6873c5SCy Schubert  * structure.
8*bf6873c5SCy Schubert  *
9*bf6873c5SCy Schubert  * The individual PAM modules should provide a definition of the pam_config
10*bf6873c5SCy Schubert  * struct appropriate to that module.  None of the PAM utility functions need
11*bf6873c5SCy Schubert  * to know what that configuration struct looks like, and it must be freed
12*bf6873c5SCy Schubert  * before calling putil_args_free().
13*bf6873c5SCy Schubert  *
14*bf6873c5SCy Schubert  * The canonical version of this file is maintained in the rra-c-util package,
15*bf6873c5SCy Schubert  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
16*bf6873c5SCy Schubert  *
17*bf6873c5SCy Schubert  * Written by Russ Allbery <eagle@eyrie.org>
18*bf6873c5SCy Schubert  * Copyright 2010, 2012-2014
19*bf6873c5SCy Schubert  *     The Board of Trustees of the Leland Stanford Junior University
20*bf6873c5SCy Schubert  *
21*bf6873c5SCy Schubert  * Permission is hereby granted, free of charge, to any person obtaining a
22*bf6873c5SCy Schubert  * copy of this software and associated documentation files (the "Software"),
23*bf6873c5SCy Schubert  * to deal in the Software without restriction, including without limitation
24*bf6873c5SCy Schubert  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25*bf6873c5SCy Schubert  * and/or sell copies of the Software, and to permit persons to whom the
26*bf6873c5SCy Schubert  * Software is furnished to do so, subject to the following conditions:
27*bf6873c5SCy Schubert  *
28*bf6873c5SCy Schubert  * The above copyright notice and this permission notice shall be included in
29*bf6873c5SCy Schubert  * all copies or substantial portions of the Software.
30*bf6873c5SCy Schubert  *
31*bf6873c5SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32*bf6873c5SCy Schubert  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33*bf6873c5SCy Schubert  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
34*bf6873c5SCy Schubert  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35*bf6873c5SCy Schubert  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
36*bf6873c5SCy Schubert  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37*bf6873c5SCy Schubert  * DEALINGS IN THE SOFTWARE.
38*bf6873c5SCy Schubert  *
39*bf6873c5SCy Schubert  * SPDX-License-Identifier: MIT
40*bf6873c5SCy Schubert  */
41*bf6873c5SCy Schubert 
42*bf6873c5SCy Schubert #include <config.h>
43*bf6873c5SCy Schubert #ifdef HAVE_KRB5
44*bf6873c5SCy Schubert #    include <portable/krb5.h>
45*bf6873c5SCy Schubert #endif
46*bf6873c5SCy Schubert #include <portable/pam.h>
47*bf6873c5SCy Schubert #include <portable/system.h>
48*bf6873c5SCy Schubert 
49*bf6873c5SCy Schubert #include <errno.h>
50*bf6873c5SCy Schubert 
51*bf6873c5SCy Schubert #include <pam-util/args.h>
52*bf6873c5SCy Schubert #include <pam-util/logging.h>
53*bf6873c5SCy Schubert 
54*bf6873c5SCy Schubert 
55*bf6873c5SCy Schubert /*
56*bf6873c5SCy Schubert  * Allocate a new pam_args struct and return it, or NULL on memory allocation
57*bf6873c5SCy Schubert  * or Kerberos initialization failure.  If HAVE_KRB5 is defined, we also
58*bf6873c5SCy Schubert  * allocate a Kerberos context.
59*bf6873c5SCy Schubert  */
60*bf6873c5SCy Schubert struct pam_args *
putil_args_new(pam_handle_t * pamh,int flags)61*bf6873c5SCy Schubert putil_args_new(pam_handle_t *pamh, int flags)
62*bf6873c5SCy Schubert {
63*bf6873c5SCy Schubert     struct pam_args *args;
64*bf6873c5SCy Schubert #ifdef HAVE_KRB5
65*bf6873c5SCy Schubert     krb5_error_code status;
66*bf6873c5SCy Schubert #endif
67*bf6873c5SCy Schubert 
68*bf6873c5SCy Schubert     args = calloc(1, sizeof(struct pam_args));
69*bf6873c5SCy Schubert     if (args == NULL) {
70*bf6873c5SCy Schubert         putil_crit(NULL, "cannot allocate memory: %s", strerror(errno));
71*bf6873c5SCy Schubert         return NULL;
72*bf6873c5SCy Schubert     }
73*bf6873c5SCy Schubert     args->pamh = pamh;
74*bf6873c5SCy Schubert     args->silent = ((flags & PAM_SILENT) == PAM_SILENT);
75*bf6873c5SCy Schubert 
76*bf6873c5SCy Schubert #ifdef HAVE_KRB5
77*bf6873c5SCy Schubert     if (issetugid())
78*bf6873c5SCy Schubert         status = krb5_init_secure_context(&args->ctx);
79*bf6873c5SCy Schubert     else
80*bf6873c5SCy Schubert         status = krb5_init_context(&args->ctx);
81*bf6873c5SCy Schubert     if (status != 0) {
82*bf6873c5SCy Schubert         putil_err_krb5(args, status, "cannot create Kerberos context");
83*bf6873c5SCy Schubert         free(args);
84*bf6873c5SCy Schubert         return NULL;
85*bf6873c5SCy Schubert     }
86*bf6873c5SCy Schubert #endif /* HAVE_KRB5 */
87*bf6873c5SCy Schubert     return args;
88*bf6873c5SCy Schubert }
89*bf6873c5SCy Schubert 
90*bf6873c5SCy Schubert 
91*bf6873c5SCy Schubert /*
92*bf6873c5SCy Schubert  * Free a pam_args struct.  The config member must be freed separately.
93*bf6873c5SCy Schubert  */
94*bf6873c5SCy Schubert void
putil_args_free(struct pam_args * args)95*bf6873c5SCy Schubert putil_args_free(struct pam_args *args)
96*bf6873c5SCy Schubert {
97*bf6873c5SCy Schubert     if (args == NULL)
98*bf6873c5SCy Schubert         return;
99*bf6873c5SCy Schubert #ifdef HAVE_KRB5
100*bf6873c5SCy Schubert     free(args->realm);
101*bf6873c5SCy Schubert     if (args->ctx != NULL)
102*bf6873c5SCy Schubert         krb5_free_context(args->ctx);
103*bf6873c5SCy Schubert #endif
104*bf6873c5SCy Schubert     free(args);
105*bf6873c5SCy Schubert }
106