| 1 | /*
|
|---|
| 2 | * mapfile.c
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) Gerald Carter <jerry@samba.org>
|
|---|
| 5 | *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | * it 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 | * This program 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
|
|---|
| 14 | * GNU 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 <http://www.gnu.org/licenses/>.
|
|---|
| 18 | *
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 | #include "winbindd/winbindd.h"
|
|---|
| 23 | #include "idmap_hash.h"
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 |
|
|---|
| 26 | XFILE *lw_map_file = NULL;
|
|---|
| 27 |
|
|---|
| 28 | /*********************************************************************
|
|---|
| 29 | ********************************************************************/
|
|---|
| 30 |
|
|---|
| 31 | static bool mapfile_open(void)
|
|---|
| 32 | {
|
|---|
| 33 | const char *mapfile_name = NULL;
|
|---|
| 34 |
|
|---|
| 35 | /* If we have an open handle, just reset it */
|
|---|
| 36 |
|
|---|
| 37 | if (lw_map_file) {
|
|---|
| 38 | return (x_tseek(lw_map_file, 0, SEEK_SET) == 0);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | mapfile_name = lp_parm_const_string(-1, "idmap_hash", "name_map", NULL);
|
|---|
| 42 | if (!mapfile_name) {
|
|---|
| 43 | return false;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | lw_map_file = x_fopen(mapfile_name, O_RDONLY, 0);
|
|---|
| 47 | if (!lw_map_file) {
|
|---|
| 48 | DEBUG(0,("can't open idmap_hash:name_map (%s). Error %s\n",
|
|---|
| 49 | mapfile_name, strerror(errno) ));
|
|---|
| 50 | return false;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return true;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /*********************************************************************
|
|---|
| 57 | ********************************************************************/
|
|---|
| 58 |
|
|---|
| 59 | static bool mapfile_read_line(fstring key, fstring value)
|
|---|
| 60 | {
|
|---|
| 61 | char buffer[1024];
|
|---|
| 62 | char *p;
|
|---|
| 63 | int len;
|
|---|
| 64 |
|
|---|
| 65 | if (!lw_map_file)
|
|---|
| 66 | return false;
|
|---|
| 67 |
|
|---|
| 68 | if ((p = x_fgets(buffer, sizeof(buffer)-1, lw_map_file)) == NULL) {
|
|---|
| 69 | return false;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /* Strip newlines and carriage returns */
|
|---|
| 73 |
|
|---|
| 74 | len = strlen_m(buffer) - 1;
|
|---|
| 75 | while ((buffer[len] == '\n') || (buffer[len] == '\r')) {
|
|---|
| 76 | buffer[len--] = '\0';
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | if ((p = strchr_m(buffer, '=')) == NULL ) {
|
|---|
| 81 | DEBUG(0,("idmap_hash: Bad line in name_map (%s)\n", buffer));
|
|---|
| 82 | return false;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | *p = '\0';
|
|---|
| 86 | p++;
|
|---|
| 87 |
|
|---|
| 88 | fstrcpy(key, buffer);
|
|---|
| 89 | fstrcpy(value, p);
|
|---|
| 90 |
|
|---|
| 91 | /* Eat whitespace */
|
|---|
| 92 |
|
|---|
| 93 | if (!trim_char(key, ' ', ' '))
|
|---|
| 94 | return false;
|
|---|
| 95 |
|
|---|
| 96 | if (!trim_char(value, ' ', ' '))
|
|---|
| 97 | return false;
|
|---|
| 98 |
|
|---|
| 99 | return true;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /*********************************************************************
|
|---|
| 103 | ********************************************************************/
|
|---|
| 104 |
|
|---|
| 105 | static bool mapfile_close(void)
|
|---|
| 106 | {
|
|---|
| 107 | int ret = 0;
|
|---|
| 108 | if (lw_map_file) {
|
|---|
| 109 | ret = x_fclose(lw_map_file);
|
|---|
| 110 | lw_map_file = NULL;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | return (ret == 0);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | /*********************************************************************
|
|---|
| 118 | ********************************************************************/
|
|---|
| 119 |
|
|---|
| 120 | NTSTATUS mapfile_lookup_key(TALLOC_CTX *ctx, const char *value, char **key)
|
|---|
| 121 | {
|
|---|
| 122 | fstring r_key, r_value;
|
|---|
| 123 | NTSTATUS ret = NT_STATUS_NOT_FOUND;
|
|---|
| 124 |
|
|---|
| 125 | if (!mapfile_open())
|
|---|
| 126 | return NT_STATUS_OBJECT_PATH_NOT_FOUND;
|
|---|
| 127 |
|
|---|
| 128 | while (mapfile_read_line(r_key, r_value))
|
|---|
| 129 | {
|
|---|
| 130 | if (strequal(r_value, value)) {
|
|---|
| 131 | ret = NT_STATUS_OK;
|
|---|
| 132 |
|
|---|
| 133 | /* We're done once finishing this block */
|
|---|
| 134 | *key = talloc_strdup(ctx, r_key);
|
|---|
| 135 | if (!*key) {
|
|---|
| 136 | ret = NT_STATUS_NO_MEMORY;
|
|---|
| 137 | }
|
|---|
| 138 | break;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | mapfile_close();
|
|---|
| 143 |
|
|---|
| 144 | return ret;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /*********************************************************************
|
|---|
| 148 | ********************************************************************/
|
|---|
| 149 |
|
|---|
| 150 | NTSTATUS mapfile_lookup_value(TALLOC_CTX *ctx, const char *key, char **value)
|
|---|
| 151 | {
|
|---|
| 152 | fstring r_key, r_value;
|
|---|
| 153 | NTSTATUS ret = NT_STATUS_NOT_FOUND;
|
|---|
| 154 |
|
|---|
| 155 | if (!mapfile_open())
|
|---|
| 156 | return NT_STATUS_OBJECT_PATH_NOT_FOUND;
|
|---|
| 157 |
|
|---|
| 158 | while (mapfile_read_line(r_key, r_value))
|
|---|
| 159 | {
|
|---|
| 160 | if (strequal(r_key, key)) {
|
|---|
| 161 | ret = NT_STATUS_OK;
|
|---|
| 162 |
|
|---|
| 163 | /* We're done once finishing this block */
|
|---|
| 164 | *value = talloc_strdup(ctx, r_value);
|
|---|
| 165 | if (!*key) {
|
|---|
| 166 | ret = NT_STATUS_NO_MEMORY;
|
|---|
| 167 | }
|
|---|
| 168 | break;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | mapfile_close();
|
|---|
| 173 |
|
|---|
| 174 | return ret;
|
|---|
| 175 | }
|
|---|