2 * QEMU PAM authorization driver
4 * Copyright (c) 2018 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "authz/pamacct.h"
24 #include "qemu/module.h"
25 #include "qom/object_interfaces.h"
27 #include <security/pam_appl.h>
30 static bool qauthz_pam_is_allowed(QAuthZ
*authz
,
34 QAuthZPAM
*pauthz
= QAUTHZ_PAM(authz
);
35 const struct pam_conv pam_conversation
= { 0 };
36 pam_handle_t
*pamh
= NULL
;
39 trace_qauthz_pam_check(authz
, identity
, pauthz
->service
);
40 ret
= pam_start(pauthz
->service
,
44 if (ret
!= PAM_SUCCESS
) {
45 error_setg(errp
, "Unable to start PAM transaction: %s",
46 pam_strerror(NULL
, ret
));
50 ret
= pam_acct_mgmt(pamh
, PAM_SILENT
);
52 if (ret
!= PAM_SUCCESS
) {
53 error_setg(errp
, "Unable to authorize user '%s': %s",
54 identity
, pam_strerror(pamh
, ret
));
63 qauthz_pam_prop_set_service(Object
*obj
,
65 Error
**errp G_GNUC_UNUSED
)
67 QAuthZPAM
*pauthz
= QAUTHZ_PAM(obj
);
69 g_free(pauthz
->service
);
70 pauthz
->service
= g_strdup(service
);
75 qauthz_pam_prop_get_service(Object
*obj
,
76 Error
**errp G_GNUC_UNUSED
)
78 QAuthZPAM
*pauthz
= QAUTHZ_PAM(obj
);
80 return g_strdup(pauthz
->service
);
85 qauthz_pam_complete(UserCreatable
*uc
, Error
**errp
)
91 qauthz_pam_finalize(Object
*obj
)
93 QAuthZPAM
*pauthz
= QAUTHZ_PAM(obj
);
95 g_free(pauthz
->service
);
100 qauthz_pam_class_init(ObjectClass
*oc
, void *data
)
102 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
103 QAuthZClass
*authz
= QAUTHZ_CLASS(oc
);
105 ucc
->complete
= qauthz_pam_complete
;
106 authz
->is_allowed
= qauthz_pam_is_allowed
;
108 object_class_property_add_str(oc
, "service",
109 qauthz_pam_prop_get_service
,
110 qauthz_pam_prop_set_service
,
115 QAuthZPAM
*qauthz_pam_new(const char *id
,
120 object_new_with_props(TYPE_QAUTHZ_PAM
,
121 object_get_objects_root(),
128 static const TypeInfo qauthz_pam_info
= {
129 .parent
= TYPE_QAUTHZ
,
130 .name
= TYPE_QAUTHZ_PAM
,
131 .instance_size
= sizeof(QAuthZPAM
),
132 .instance_finalize
= qauthz_pam_finalize
,
133 .class_size
= sizeof(QAuthZPAMClass
),
134 .class_init
= qauthz_pam_class_init
,
135 .interfaces
= (InterfaceInfo
[]) {
136 { TYPE_USER_CREATABLE
},
143 qauthz_pam_register_types(void)
145 type_register_static(&qauthz_pam_info
);
149 type_init(qauthz_pam_register_types
);