| 1 | /* |
| 2 | * Copyright (C) 2000 Rob Crittenden ([email protected]) |
| 3 | * Copyright (C) 2002 Gianluigi Tiesi ([email protected]) |
| 4 | * Copyright (C) 2004 CreepLord ([email protected]) |
| 5 | * Copyright (C) 2008 Pelish ([email protected]) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | #include "common/setup_before.h" |
| 22 | #define AUTOUPDATE_INTERNAL_ACCESS |
| 23 | #include "autoupdate.h" |
| 24 | |
| 25 | #include <cerrno> |
| 26 | #include <cstring> |
| 27 | |
| 28 | #include "common/eventlog.h" |
| 29 | #include "common/list.h" |
| 30 | #include "common/util.h" |
| 31 | #include "common/proginfo.h" |
| 32 | #include "common/tag.h" |
| 33 | #include "common/xalloc.h" |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | namespace bnetd |
| 40 | { |
| 41 | |
| 42 | static t_list * autoupdate_head = NULL; |
| 43 | static std::FILE * fp = NULL; |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * Open the autoupdate configuration file, create a linked list of the |
| 48 | * clienttag and the update file for it. The format of the file is: |
| 49 | * archtag<tab>clienttag<tab>versiontag<tab>update file |
| 50 | * |
| 51 | * Comments begin with # and are ignored. |
| 52 | * |
| 53 | * The server assumes that the update file is in the "files" directory |
| 54 | * so do not include "/" in the filename - it won't be sent |
| 55 | * (because it is a security risk). |
| 56 | */ |
| 57 | |
| 58 | extern int autoupdate_load(char const * filename) |
| 59 | { |
| 60 | unsigned int line; |
| 61 | unsigned int pos; |
| 62 | char * buff; |
| 63 | char * temp; |
| 64 | char const * archtag; |
| 65 | char const * clienttag; |
| 66 | char const * updatefile; |
| 67 | char const * versiontag; |
| 68 | char const * path; |
| 69 | t_autoupdate * entry; |
| 70 | |
| 71 | if (!filename) { |
| 72 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | if (!(fp = std::fopen(filename, "r"))) { |
| 77 | eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno)); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | autoupdate_head = list_create(); |
| 82 | |
| 83 | for (line = 1; (buff = file_get_line(fp)); line++) { |
| 84 | for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++); |
| 85 | |
| 86 | if (buff[pos] == '\0' || buff[pos] == '#') { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if ((temp = std::strrchr(buff, '#'))) { |
| 91 | unsigned int len; |
| 92 | unsigned int endpos; |
| 93 | |
| 94 | *temp = '\0'; |
| 95 | len = std::strlen(buff) + 1; |
| 96 | for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--); |
| 97 | buff[endpos + 1] = '\0'; |
| 98 | } |
| 99 | |
| 100 | /* FIXME: use next_token instead of std::strtok */ |
| 101 | if (!(archtag = std::strtok(buff, " \t"))) { /* std::strtok modifies the string it is passed */ |
| 102 | eventlog(eventlog_level_error, __FUNCTION__, "missing archtag on line {} of file \"{}\"", line, filename); |
| 103 | continue; |
| 104 | } |
| 105 | if (!(clienttag = std::strtok(NULL, " \t"))) { |
| 106 | eventlog(eventlog_level_error, __FUNCTION__, "missing clienttag on line {} of file \"{}\"", line, filename); |
| 107 | continue; |
| 108 | } |
| 109 | if (!(versiontag = std::strtok(NULL, " \t"))) { |
| 110 | eventlog(eventlog_level_error, __FUNCTION__, "missing versiontag on line {} of file \"{}\"", line, filename); |
| 111 | continue; |
| 112 | } |
| 113 | if (!(updatefile = std::strtok(NULL, " \t"))) { |
| 114 | eventlog(eventlog_level_error, __FUNCTION__, "missing updatefile on line {} of file \"{}\"", line, filename); |
| 115 | continue; |
| 116 | } |
| 117 | if ((!(path = std::strtok(NULL, " \t"))) && tag_check_wolv1(tag_str_to_uint(clienttag)) && tag_check_wolv2(tag_str_to_uint(clienttag))) { /* Only in WOL is needed to have path */ |
| 118 | eventlog(eventlog_level_error, __FUNCTION__, "missing path on line {} of file \"{}\"", line, filename); |
| 119 | } |
| 120 | |
| 121 | entry = (t_autoupdate*)xmalloc(sizeof(t_autoupdate)); |
| 122 | |
| 123 | if (!tag_check_arch((entry->archtag = tag_str_to_uint(archtag)))) { |
| 124 | eventlog(eventlog_level_error, __FUNCTION__, "got unknown archtag"); |
| 125 | xfree(entry); |
| 126 | continue; |
| 127 | } |
| 128 | if (!tag_check_client((entry->clienttag = tag_str_to_uint(clienttag)))) { |
| 129 | eventlog(eventlog_level_error, __FUNCTION__, "got unknown clienttag"); |
| 130 | xfree(entry); |
| 131 | continue; |
| 132 | } |
| 133 | entry->versiontag = xstrdup(versiontag); |
| 134 | entry->updatefile = xstrdup(updatefile); |
| 135 | if (path) |
| 136 | entry->path = xstrdup(path); |
| 137 | else |
| 138 | entry->path = NULL; |
| 139 | |
| 140 | eventlog(eventlog_level_debug, __FUNCTION__, "update '{}' version '{}' with file {}", clienttag, versiontag, updatefile); |
| 141 | |
| 142 | list_append_data(autoupdate_head, entry); |
| 143 | } |
| 144 | file_get_line(NULL); // clear file_get_line buffer |
| 145 | std::fclose(fp); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Free up all of the elements in the linked list |
| 151 | */ |
| 152 | |
| 153 | extern int autoupdate_unload(void) |
| 154 | { |
| 155 | if (autoupdate_head) { |
| 156 | t_elem * curr; |
| 157 | t_autoupdate * entry; |
| 158 | LIST_TRAVERSE(autoupdate_head, curr) |
| 159 | { |
| 160 | if (!(entry = (t_autoupdate*)elem_get_data(curr))) |
| 161 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 162 | else { |
| 163 | xfree((void *)entry->versiontag); /* avoid warning */ |
| 164 | xfree((void *)entry->updatefile); /* avoid warning */ |
| 165 | if (entry->path) |
| 166 | xfree((void *)entry->path); /* avoid warning */ |
| 167 | xfree(entry); |
| 168 | } |
| 169 | list_remove_elem(autoupdate_head, &curr); |
| 170 | } |
| 171 | |
| 172 | if (list_destroy(autoupdate_head) < 0) return -1; |
| 173 | autoupdate_head = NULL; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Check to see if an update exists for the clients version |
| 180 | * return file name if there is one |
| 181 | * retrun NULL if no update exists |
| 182 | */ |
| 183 | |
| 184 | extern char * autoupdate_check(t_tag archtag, t_tag clienttag, t_tag gamelang, char const * versiontag, char const * sku) |
| 185 | { |
| 186 | if (autoupdate_head) { |
| 187 | t_elem const * curr; |
| 188 | t_autoupdate * entry; |
| 189 | char * temp; |
| 190 | |
| 191 | LIST_TRAVERSE_CONST(autoupdate_head, curr) |
| 192 | { |
| 193 | if (!(entry = (t_autoupdate*)elem_get_data(curr))) { |
| 194 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list"); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | if (entry->archtag != archtag) |
| 199 | continue; |
| 200 | if (entry->clienttag != clienttag) |
| 201 | continue; |
| 202 | if (std::strcmp(entry->versiontag, versiontag) != 0) |
| 203 | continue; |
| 204 | |
| 205 | /* if we have a gamelang or SKU then add it to the update file name */ |
| 206 | // so far only WAR3 uses gamelang specific MPQs! |
| 207 | if (((gamelang) && ((clienttag == CLIENTTAG_WARCRAFT3_UINT) || (clienttag == CLIENTTAG_WAR3XP_UINT))) |
| 208 | || ((sku) && (tag_check_wolv2(clienttag)))) { |
| 209 | char gltag[5]; |
| 210 | char * tempmpq; |
| 211 | char * extention; |
| 212 | char const * path = entry->path; |
| 213 | |
| 214 | tempmpq = xstrdup(entry->updatefile); |
| 215 | |
| 216 | extention = std::strrchr(tempmpq, '.'); |
| 217 | *extention = '\0'; |
| 218 | extention++; |
| 219 | |
| 220 | if ((clienttag == CLIENTTAG_WARCRAFT3_UINT) || (clienttag == CLIENTTAG_WAR3XP_UINT)) { |
| 221 | tag_uint_to_str(gltag, gamelang); |
| 222 | |
| 223 | temp = (char*)xmalloc(std::strlen(entry->updatefile) + 6); |
| 224 | std::sprintf(temp, "%s_%s.%s", tempmpq, gltag, extention); |
| 225 | } |
| 226 | else { |
| 227 | temp = (char*)xmalloc(std::strlen(path) + std::strlen(entry->updatefile) + std::strlen(sku) + 3); |
| 228 | std::sprintf(temp, "%s %s_%s.%s", path, tempmpq, sku, extention); |
| 229 | } |
| 230 | |
| 231 | xfree((void *)tempmpq); |
| 232 | return temp; |
| 233 | } |
| 234 | temp = xstrdup(entry->updatefile); |
| 235 | return temp; |
| 236 | } |
| 237 | } |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | } |