| 1 | /*- |
|---|
| 2 | * Copyright (c) 2011 Dag-Erling Smørgrav |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | * notice, this list of conditions and the following disclaimer |
|---|
| 10 | * in this position and unchanged. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. The name of the author may not be used to endorse or promote |
|---|
| 15 | * products derived from this software without specific prior written |
|---|
| 16 | * permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 28 | * SUCH DAMAGE. |
|---|
| 29 | * |
|---|
| 30 | * $Id$ |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | #ifdef HAVE_CONFIG_H |
|---|
| 34 | # include "config.h" |
|---|
| 35 | #endif |
|---|
| 36 | |
|---|
| 37 | #include <ctype.h> |
|---|
| 38 | #include <stdio.h> |
|---|
| 39 | #include <stdlib.h> |
|---|
| 40 | #include <string.h> |
|---|
| 41 | #include <unistd.h> |
|---|
| 42 | |
|---|
| 43 | #include <security/pam_appl.h> |
|---|
| 44 | |
|---|
| 45 | #include "openpam_impl.h" |
|---|
| 46 | |
|---|
| 47 | static char * |
|---|
| 48 | openpam_chain_name(const char *service, pam_facility_t facility) |
|---|
| 49 | { |
|---|
| 50 | char *name; |
|---|
| 51 | |
|---|
| 52 | asprintf(&name, "pam_%s_%s", service, pam_facility_name[facility]); |
|---|
| 53 | return (name); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | static char * |
|---|
| 57 | openpam_facility_index_name(pam_facility_t facility) |
|---|
| 58 | { |
|---|
| 59 | char *name, *p; |
|---|
| 60 | |
|---|
| 61 | asprintf(&name, "PAM_%s", pam_facility_name[facility]); |
|---|
| 62 | for (p = name + 4; *p; ++p) |
|---|
| 63 | *p = toupper(*p); |
|---|
| 64 | return (name); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | int |
|---|
| 68 | openpam_dump_chain(const char *name, pam_chain_t *chain) |
|---|
| 69 | { |
|---|
| 70 | char *modname, **opt, *p; |
|---|
| 71 | int i; |
|---|
| 72 | |
|---|
| 73 | for (i = 0; chain != NULL; ++i, chain = chain->next) { |
|---|
| 74 | /* declare the module's struct pam_module */ |
|---|
| 75 | modname = strrchr(chain->module->path, '/'); |
|---|
| 76 | modname = strdup(modname ? modname : chain->module->path); |
|---|
| 77 | if (modname == NULL) |
|---|
| 78 | return (PAM_BUF_ERR); |
|---|
| 79 | for (p = modname; *p && *p != '.'; ++p) |
|---|
| 80 | /* nothing */ ; |
|---|
| 81 | *p = '\0'; |
|---|
| 82 | printf("extern struct pam_module %s_pam_module;\n", modname); |
|---|
| 83 | /* module arguments */ |
|---|
| 84 | printf("static char *%s_%d_optv[] = {\n", name, i); |
|---|
| 85 | for (opt = chain->optv; *opt; ++opt) { |
|---|
| 86 | printf("\t\""); |
|---|
| 87 | for (p = *opt; *p; ++p) { |
|---|
| 88 | if (isprint((unsigned char)*p) && *p != '"') |
|---|
| 89 | printf("%c", *p); |
|---|
| 90 | else |
|---|
| 91 | printf("\\x%02x", (unsigned char)*p); |
|---|
| 92 | } |
|---|
| 93 | printf("\",\n"); |
|---|
| 94 | } |
|---|
| 95 | printf("\tNULL,\n"); |
|---|
| 96 | printf("};\n"); |
|---|
| 97 | /* next module in chain */ |
|---|
| 98 | if (chain->next != NULL) |
|---|
| 99 | printf("static pam_chain_t %s_%d;\n", name, i + 1); |
|---|
| 100 | /* chain entry */ |
|---|
| 101 | printf("static pam_chain_t %s_%d = {\n", name, i); |
|---|
| 102 | printf("\t.module = &%s_pam_module,\n", modname); |
|---|
| 103 | printf("\t.flag = 0x%08x,\n", chain->flag); |
|---|
| 104 | printf("\t.optc = %d,\n", chain->optc); |
|---|
| 105 | printf("\t.optv = %s_%d_optv,\n", name, i); |
|---|
| 106 | if (chain->next) |
|---|
| 107 | printf("\t.next = &%s_%d,\n", name, i + 1); |
|---|
| 108 | else |
|---|
| 109 | printf("\t.next = NULL,\n"); |
|---|
| 110 | printf("};\n"); |
|---|
| 111 | free(modname); |
|---|
| 112 | } |
|---|
| 113 | return (PAM_SUCCESS); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | int |
|---|
| 117 | openpam_dump_policy(const char *service) |
|---|
| 118 | { |
|---|
| 119 | pam_handle_t *pamh; |
|---|
| 120 | char *name; |
|---|
| 121 | int fclt, ret; |
|---|
| 122 | |
|---|
| 123 | if ((pamh = calloc(1, sizeof *pamh)) == NULL) |
|---|
| 124 | return (PAM_BUF_ERR); |
|---|
| 125 | if ((ret = openpam_configure(pamh, service)) != PAM_SUCCESS) |
|---|
| 126 | return (ret); |
|---|
| 127 | for (fclt = 0; fclt < PAM_NUM_FACILITIES; ++fclt) { |
|---|
| 128 | if (pamh->chains[fclt] != NULL) { |
|---|
| 129 | if ((name = openpam_chain_name(service, fclt)) == NULL) |
|---|
| 130 | return (PAM_BUF_ERR); |
|---|
| 131 | ret = openpam_dump_chain(name, pamh->chains[fclt]); |
|---|
| 132 | free(name); |
|---|
| 133 | if (ret != PAM_SUCCESS) |
|---|
| 134 | return (ret); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | printf("static pam_policy_t pam_%s_policy = {\n", service); |
|---|
| 138 | printf("\t.service = \"%s\",\n", service); |
|---|
| 139 | printf("\t.chains = {\n"); |
|---|
| 140 | for (fclt = 0; fclt < PAM_NUM_FACILITIES; ++fclt) { |
|---|
| 141 | if ((name = openpam_facility_index_name(fclt)) == NULL) |
|---|
| 142 | return (PAM_BUF_ERR); |
|---|
| 143 | printf("\t\t[%s] = ", name); |
|---|
| 144 | free(name); |
|---|
| 145 | if (pamh->chains[fclt] != NULL) { |
|---|
| 146 | if ((name = openpam_chain_name(service, fclt)) == NULL) |
|---|
| 147 | return (PAM_BUF_ERR); |
|---|
| 148 | printf("&%s_0,\n", name); |
|---|
| 149 | free(name); |
|---|
| 150 | } else { |
|---|
| 151 | printf("NULL,\n"); |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | printf("\t},\n"); |
|---|
| 155 | printf("};\n"); |
|---|
| 156 | free(pamh); |
|---|
| 157 | return (PAM_SUCCESS); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | static void |
|---|
| 161 | usage(void) |
|---|
| 162 | { |
|---|
| 163 | |
|---|
| 164 | fprintf(stderr, "usage: openpam_dump_policy [-d] policy ...\n"); |
|---|
| 165 | exit(1); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | int |
|---|
| 169 | main(int argc, char *argv[]) |
|---|
| 170 | { |
|---|
| 171 | int i, opt; |
|---|
| 172 | |
|---|
| 173 | while ((opt = getopt(argc, argv, "d")) != -1) |
|---|
| 174 | switch (opt) { |
|---|
| 175 | case 'd': |
|---|
| 176 | openpam_debug = 1; |
|---|
| 177 | break; |
|---|
| 178 | default: |
|---|
| 179 | usage(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | argc -= optind; |
|---|
| 183 | argv += optind; |
|---|
| 184 | |
|---|
| 185 | if (argc < 1) |
|---|
| 186 | usage(); |
|---|
| 187 | |
|---|
| 188 | printf("#include <security/pam_appl.h>\n"); |
|---|
| 189 | printf("#include \"openpam_impl.h\"\n"); |
|---|
| 190 | for (i = 0; i < argc; ++i) |
|---|
| 191 | openpam_dump_policy(argv[i]); |
|---|
| 192 | printf("pam_policy_t *pam_embedded_policies[] = {\n"); |
|---|
| 193 | for (i = 0; i < argc; ++i) |
|---|
| 194 | printf("\t&pam_%s_policy,\n", argv[i]); |
|---|
| 195 | printf("\tNULL,\n"); |
|---|
| 196 | printf("};\n"); |
|---|
| 197 | exit(0); |
|---|
| 198 | } |
|---|