| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 |  | 
|---|
| 4 | manipulate nbt name structures | 
|---|
| 5 |  | 
|---|
| 6 | Copyright (C) Andrew Tridgell 1994-1998 | 
|---|
| 7 | Copyright (C) Jeremy Allison 2007 | 
|---|
| 8 | Copyright (C) Andrew Bartlett 2009. | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify | 
|---|
| 11 | it under the terms of the GNU General Public License as published by | 
|---|
| 12 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 13 | (at your option) any later version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, | 
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | GNU General Public License for more details. | 
|---|
| 19 |  | 
|---|
| 20 | You should have received a copy of the GNU General Public License | 
|---|
| 21 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #include "includes.h" | 
|---|
| 25 | #include "lib/util/xfile.h" | 
|---|
| 26 | #include "lib/util/util_net.h" | 
|---|
| 27 | #include "system/filesys.h" | 
|---|
| 28 | #include "system/network.h" | 
|---|
| 29 |  | 
|---|
| 30 | /******************************************************** | 
|---|
| 31 | Start parsing the lmhosts file. | 
|---|
| 32 | *********************************************************/ | 
|---|
| 33 |  | 
|---|
| 34 | XFILE *startlmhosts(const char *fname) | 
|---|
| 35 | { | 
|---|
| 36 | XFILE *fp = x_fopen(fname,O_RDONLY, 0); | 
|---|
| 37 | if (!fp) { | 
|---|
| 38 | DEBUG(4,("startlmhosts: Can't open lmhosts file %s. " | 
|---|
| 39 | "Error was %s\n", | 
|---|
| 40 | fname, strerror(errno))); | 
|---|
| 41 | return NULL; | 
|---|
| 42 | } | 
|---|
| 43 | return fp; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | /******************************************************** | 
|---|
| 47 | Parse the next line in the lmhosts file. | 
|---|
| 48 | *********************************************************/ | 
|---|
| 49 |  | 
|---|
| 50 | bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type, | 
|---|
| 51 | struct sockaddr_storage *pss) | 
|---|
| 52 | { | 
|---|
| 53 | char line[1024]; | 
|---|
| 54 |  | 
|---|
| 55 | *pp_name = NULL; | 
|---|
| 56 |  | 
|---|
| 57 | while(!x_feof(fp) && !x_ferror(fp)) { | 
|---|
| 58 | char *ip = NULL; | 
|---|
| 59 | char *flags = NULL; | 
|---|
| 60 | char *extra = NULL; | 
|---|
| 61 | char *name = NULL; | 
|---|
| 62 | const char *ptr; | 
|---|
| 63 | char *ptr1 = NULL; | 
|---|
| 64 | int count = 0; | 
|---|
| 65 |  | 
|---|
| 66 | *name_type = -1; | 
|---|
| 67 |  | 
|---|
| 68 | if (!fgets_slash(line,sizeof(line),fp)) { | 
|---|
| 69 | continue; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | if (*line == '#') { | 
|---|
| 73 | continue; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | ptr = line; | 
|---|
| 77 |  | 
|---|
| 78 | if (next_token_talloc(ctx, &ptr, &ip, NULL)) | 
|---|
| 79 | ++count; | 
|---|
| 80 | if (next_token_talloc(ctx, &ptr, &name, NULL)) | 
|---|
| 81 | ++count; | 
|---|
| 82 | if (next_token_talloc(ctx, &ptr, &flags, NULL)) | 
|---|
| 83 | ++count; | 
|---|
| 84 | if (next_token_talloc(ctx, &ptr, &extra, NULL)) | 
|---|
| 85 | ++count; | 
|---|
| 86 |  | 
|---|
| 87 | if (count <= 0) | 
|---|
| 88 | continue; | 
|---|
| 89 |  | 
|---|
| 90 | if (count > 0 && count < 2) { | 
|---|
| 91 | DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n", | 
|---|
| 92 | line)); | 
|---|
| 93 | continue; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | if (count >= 4) { | 
|---|
| 97 | DEBUG(0,("getlmhostsent: too many columns " | 
|---|
| 98 | "in lmhosts file (obsolete syntax)\n")); | 
|---|
| 99 | continue; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | if (!flags) { | 
|---|
| 103 | flags = talloc_strdup(ctx, ""); | 
|---|
| 104 | if (!flags) { | 
|---|
| 105 | continue; | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", | 
|---|
| 110 | ip, name, flags)); | 
|---|
| 111 |  | 
|---|
| 112 | if (strchr_m(flags,'G') || strchr_m(flags,'S')) { | 
|---|
| 113 | DEBUG(0,("getlmhostsent: group flag " | 
|---|
| 114 | "in lmhosts ignored (obsolete)\n")); | 
|---|
| 115 | continue; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) { | 
|---|
| 119 | DEBUG(0,("getlmhostsent: invalid address " | 
|---|
| 120 | "%s.\n", ip)); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /* Extra feature. If the name ends in '#XX', | 
|---|
| 124 | * where XX is a hex number, then only add that name type. */ | 
|---|
| 125 | if((ptr1 = strchr_m(name, '#')) != NULL) { | 
|---|
| 126 | char *endptr; | 
|---|
| 127 | ptr1++; | 
|---|
| 128 |  | 
|---|
| 129 | *name_type = (int)strtol(ptr1, &endptr, 16); | 
|---|
| 130 | if(!*ptr1 || (endptr == ptr1)) { | 
|---|
| 131 | DEBUG(0,("getlmhostsent: invalid name " | 
|---|
| 132 | "%s containing '#'.\n", name)); | 
|---|
| 133 | continue; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | *(--ptr1) = '\0'; /* Truncate at the '#' */ | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | *pp_name = talloc_strdup(ctx, name); | 
|---|
| 140 | if (!*pp_name) { | 
|---|
| 141 | return false; | 
|---|
| 142 | } | 
|---|
| 143 | return true; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | return false; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /******************************************************** | 
|---|
| 150 | Finish parsing the lmhosts file. | 
|---|
| 151 | *********************************************************/ | 
|---|
| 152 |  | 
|---|
| 153 | void endlmhosts(XFILE *fp) | 
|---|
| 154 | { | 
|---|
| 155 | x_fclose(fp); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|