| 1 | /* pam-test.c - simple PAM authentication test program |
| 2 | Copyright (C) 2007, 2009 g10 Code GmbH |
| 3 | |
| 4 | This file is part of Poldi. |
| 5 | |
| 6 | Poldi is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | Poldi is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <security/pam_appl.h> |
| 23 | #include <security/pam_misc.h> |
| 24 | #include <ctype.h> |
| 25 | #include <unistd.h> |
| 26 | #include <getopt.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | |
| 31 | |
| 32 | #define PROGRAM_NAMEcxx . . $ `tests/pam-test.c:32:9`! "pam-test" |
| 33 | #define PROGRAM_VERSIONcxx . . $ `tests/pam-test.c:33:9`! "0.2" |
| 34 | |
| 35 | /* Use the standard conversation function from libpam-misc. */ |
| 36 | static struct pam_convcxx . . $ pam_conv# convcxx . . $ conv. = |
| 37 | { |
| 38 | misc_convcxx . . $ misc_conv(a728ce2473c45339)., |
| 39 | NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`! |
| 40 | }; |
| 41 | |
| 42 | static void |
| 43 | print_helpcxx . . $ print_help(49f6e7a06ebc5aa8). (void) |
| 44 | { |
| 45 | printfcxx . . $ printf(581fddd72a8db8fc). ("\ |
| 46 | Usage: %s [options] <PAM service name>\n\ |
| 47 | Test PAM authentication.\n\ |
| 48 | \n\ |
| 49 | Options:\n\ |
| 50 | -h, --help print help information\n\ |
| 51 | -v, --version print version information\n\ |
| 52 | -u, --username specify username for authentication\n\ |
| 53 | \n\ |
| 54 | Report bugs to <[email protected]>.\n", PROGRAM_NAMEcxx . . $ `tests/pam-test.c:32:9`!); |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | print_versioncxx . . $ print_version(49f6e7a06ebc5aa8). (void) |
| 59 | { |
| 60 | printfcxx . . $ printf(581fddd72a8db8fc). (PROGRAM_NAMEcxx . . $ `tests/pam-test.c:32:9`! " " PROGRAM_VERSIONcxx . . $ `tests/pam-test.c:33:9`! "\n"); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | test_authcxx . . $ test_auth(2fa786934fe80e24). (const char *servicename, const char *username) |
| 65 | { |
| 66 | const void *user_opaque; |
| 67 | const char *user; |
| 68 | pam_handle_tcxx . . $ pam_handle_t# *handle; |
| 69 | int rc; |
| 70 | |
| 71 | /* Connect to PAM. */ |
| 72 | rc = pam_startcxx . . $ pam_start(9115a7afd94c87f3). (servicename, username, &convcxx . . $ conv., &handle); |
| 73 | if (rc != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!) |
| 74 | { |
| 75 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: %s\n", pam_strerrorcxx . . $ pam_strerror(b465d44ed4c5fb1c). (handle, rc)); |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | /* Try authentication. */ |
| 80 | rc = pam_authenticatecxx . . $ pam_authenticate(c56fd5b047f94224). (handle, 0); |
| 81 | if (rc != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!) |
| 82 | { |
| 83 | printfcxx . . $ printf(581fddd72a8db8fc). ("Authentication failed\n"); |
| 84 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: %s\n", pam_strerrorcxx . . $ pam_strerror(b465d44ed4c5fb1c). (handle, rc)); |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | printfcxx . . $ printf(581fddd72a8db8fc). ("Authentication succeeded\n"); |
| 89 | |
| 90 | /* Retrieve name of authenticated identity. */ |
| 91 | rc = pam_get_itemcxx . . $ pam_get_item(e1131d0411280f41). (handle, PAM_USERcxx . . $ `<external>/1475977469199658974/_pam_types.h:133:9`!, &user_opaque); |
| 92 | if (rc != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!) |
| 93 | { |
| 94 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: %s\n", pam_strerrorcxx . . $ pam_strerror(b465d44ed4c5fb1c). (handle, rc)); |
| 95 | goto out; |
| 96 | } |
| 97 | user = user_opaque; |
| 98 | printfcxx . . $ printf(581fddd72a8db8fc). ("Authenticated as user `%s'\n", user); |
| 99 | |
| 100 | /* Disconnect from PAM. */ |
| 101 | rc = pam_endcxx . . $ pam_end(c56fd5b047f94224). (handle, rc); |
| 102 | if (rc != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!) |
| 103 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: failed to release PAM handle\n"); |
| 104 | |
| 105 | out: |
| 106 | |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | /* This is a simple test program for PAM authentication. */ |
| 111 | int |
| 112 | maincxx . . $ main(afcd3168fc1188a4). (int argc, char **argv) |
| 113 | { |
| 114 | const char *servicename; |
| 115 | const char *username; |
| 116 | int c; |
| 117 | |
| 118 | servicename = username = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 119 | |
| 120 | while (1) |
| 121 | { |
| 122 | static struct optioncxx . . $ option# long_options[] = |
| 123 | { |
| 124 | { "version", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'v' }, |
| 125 | { "help", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'h' }, |
| 126 | { "user", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'u' }, |
| 127 | { 0, 0, 0, 0 } |
| 128 | }; |
| 129 | int option_index = 0; |
| 130 | |
| 131 | c = getopt_longcxx . . $ getopt_long(41f665c090816971). (argc, argv, "vhu:", |
| 132 | long_options, &option_index); |
| 133 | |
| 134 | /* Detect the end of the options. */ |
| 135 | if (c == -1) |
| 136 | break; |
| 137 | |
| 138 | switch (c) |
| 139 | { |
| 140 | case 'u': |
| 141 | username = strdupcxx . . $ strdup(a8b8e32347182212). (optargcxx . . $ optarg.); |
| 142 | if (!username) |
| 143 | { |
| 144 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "failed to duplicate username: %s", strerrorcxx . . $ strerror(ff224f080444ee45). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 145 | exitcxx . . $ exit(3ca292c338df9bb3). (1); |
| 146 | } |
| 147 | break; |
| 148 | |
| 149 | case 'h': |
| 150 | print_helpcxx . . $ print_help(49f6e7a06ebc5aa8). (); |
| 151 | exitcxx . . $ exit(3ca292c338df9bb3). (0); |
| 152 | break; |
| 153 | |
| 154 | case 'v': |
| 155 | print_versioncxx . . $ print_version(49f6e7a06ebc5aa8). (); |
| 156 | exitcxx . . $ exit(3ca292c338df9bb3). (0); |
| 157 | break; |
| 158 | |
| 159 | case '?': |
| 160 | /* `getopt_long' already printed an error message. */ |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | abortcxx . . $ abort(8df32a62017fbbf6). (); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (argc - optindcxx . . $ optind. != 1) |
| 169 | { |
| 170 | print_helpcxx . . $ print_help(49f6e7a06ebc5aa8). (); |
| 171 | exitcxx . . $ exit(3ca292c338df9bb3). (1); |
| 172 | } |
| 173 | |
| 174 | servicename = argv[optindcxx . . $ optind.]; |
| 175 | test_authcxx . . $ test_auth(2fa786934fe80e24). (servicename, username); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* end */ |