document exit codes in the man pages
[vlock.git] / auth-pam.c
blob2a851f886d9c4f7c581400a7832f11df8fa94560
1 /* auth-pam.c -- PAM authentification routine for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
10 * the author.
14 #include <stdio.h>
16 #include <security/pam_appl.h>
17 #include <security/pam_misc.h>
19 /* Try to authenticate the user. When the user is successfully authenticated
20 * this function returns 1. When the authentication fails for whatever reason
21 * the function returns 0.
23 int auth(const char *user) {
24 pam_handle_t *pamh;
25 int pam_status;
26 int pam_end_status;
27 struct pam_conv pamc = {
28 &misc_conv,
29 NULL
32 /* initialize pam */
33 pam_status = pam_start("vlock", user, &pamc, &pamh);
35 if (pam_status != PAM_SUCCESS) {
36 fprintf(stderr, "vlock-auth: %s\n", pam_strerror(pamh, pam_status));
37 goto end;
40 /* put the username before the password prompt */
41 fprintf(stderr, "%s's ", user); fflush(stderr);
42 /* authenticate the user */
43 pam_status = pam_authenticate(pamh, 0);
45 if (pam_status != PAM_SUCCESS) {
46 fprintf(stderr, "vlock-auth: %s\n", pam_strerror(pamh, pam_status));
49 end:
50 /* finish pam */
51 pam_end_status = pam_end(pamh, pam_status);
53 if (pam_end_status != PAM_SUCCESS) {
54 fprintf(stderr, "vlock-auth: %s\n", pam_strerror(pamh, pam_end_status));
57 return (pam_status == PAM_SUCCESS);