Subversion Repositories SvarDOS

Rev

Rev 236 | Rev 260 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
230 mateuszvis 2
 * This file is part of pkginst (SvarDOS)
219 mateuszvis 3
 * Copyright (C) 2012-2021 Mateusz Viste
4
 * Changes by TK Chia
5
 */
6
 
7
#include <ctype.h>     /* toupper() */
8
#include <stdio.h>
9
#include <stdlib.h>    /* system() */
10
#include <string.h>    /* strcpy() */
11
#include <unistd.h>    /* read() */
12
#include <sys/types.h> /* struct utimbuf */
13
 
14
#include "crc32.h"     /* all CRC32 related stuff */
15
#include "helpers.h"   /* slash2backslash(), strtolower() */
16
#include "fileexst.h"
17
#include "kprintf.h"
18
#include "libunzip.h"  /* zip_listfiles()... */
19
#include "showinst.h"  /* pkg_loadflist() */
20
#include "pkginst.h"   /* include self for control */
21
#include "version.h"
22
 
23
 
24
/* return 1 if fname looks like a link filename, 0 otherwise */
236 mateuszvis 25
static int islinkfile(const char *fname) {
219 mateuszvis 26
  char *link1 = "LINKS\\";
27
  char *link2 = "links\\";
28
  int x;
29
  for (x = 0; ; x++) {
30
    if (link1[x] == 0) return(1);
31
    if ((fname[x] != link1[x]) && (fname[x] != link2[x])) return(0);
32
  }
33
}
34
 
35
 
36
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
37
 * nonzero otherwise. */
236 mateuszvis 38
static int validfilename(const char *fname) {
219 mateuszvis 39
  int i, i2;
40
  char *validchars = "!#$%&'()-@^_`{}~";
41
  int elemlen = 0;
42
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
43
                         a filename or an extension */
44
  /* look for invalid chars in the entire string, and check the length of the
45
   * element at the same time */
46
  for (i = 0; fname[i] != 0; i++) {
47
    /* director separators are threated specially */
48
    if (fname[i] == '\\') {
49
      elemlen = 0;
50
      elemmaxlen = 8;
51
      continue;
52
    }
53
    /* '.' switches the check into extension mode */
54
    if (fname[i] == '.') {
55
      if (elemlen == 0) return(-1); /* a file must have a base name */
56
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
57
      elemlen = 0;
58
      elemmaxlen = 3;
59
      continue;
60
    }
61
    /* check that the element is not too long */
62
    if (++elemlen > elemmaxlen) return(-3);
63
    /* look for valid characters */
64
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
65
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
66
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
67
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
68
    /* look for other valid characters */
69
    for (i2 = 0; validchars[i2] != 0; i2++) {
70
      if (fname[i] == validchars[i2]) break;
71
    }
72
    if (validchars[i2] != 0) continue;
73
    /* if we are here, then the character is invalid */
74
    return(-4);
75
  }
76
  /* all checks passed */
77
  return(0);
78
}
79
 
80
 
81
/* processes a link file - that is, reads the target inside, and overwrite
82
 * the file with new content */
236 mateuszvis 83
static void processlinkfile(const char *linkfile, const char *dosdir, const struct customdirs *dirlist, char *buff) {
219 mateuszvis 84
  char origtarget[512];
85
  int x;
86
  char *shortfile;
87
  unsigned char comstub[] = { /* machine code of a COM stub launcher */
88
    0xBC,0x00,0x00,0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0xBB,0x2A,0x01,0x8C,0x5F,0x04,
89
    0x8C,0x5F,0x08,0x8C,0x5F,0x0C,0xB8,0x00,0x4B,0xBA,0x38,0x01,0xCD,0x21,0xB0,0x7F,
90
    0x72,0x04,0xB4,0x4D,0xCD,0x21,0xB4,0x4C,0xCD,0x21,0x00,0x00,0x80,0x00,0x00,0x00,
91
    0x5C,0x00,0x00,0x00,0x6C,0x00,0x00,0x00}; /* read comlink.asm for details */
92
  const unsigned stack_paras = 12;
93
  unsigned alloc_paras, alloc_bytes;
94
  FILE *fd;
95
  /* open the link file and read the original target */
96
  fd = fopen(linkfile, "r");
97
  if (fd == NULL) {
98
    kitten_printf(3, 21, "Error: Failed to open link file '%s' for read access.", linkfile);
99
    puts("");
100
    return;
101
  }
102
  x = fread(origtarget, 1, sizeof(origtarget) - 1, fd);
103
  origtarget[x] = 0;
104
  fclose(fd);
105
  /* validate the original target (ltrim to first \r or \n) */
106
  for (x = 0; origtarget[x] != 0; x++) {
107
    if ((origtarget[x] == '\r') || (origtarget[x] == '\n')) {
108
      origtarget[x] = 0;
109
      break;
110
    }
111
  }
112
  /* translate the original target to a local path */
113
  shortfile = computelocalpath(origtarget, buff, dosdir, dirlist);
114
  /* compute the amount of memory the stub should leave for itself:
115
	- 16 paragraphs needed for the PSP
116
	- sizeof(comstub) bytes needed for code and data
117
	- strlen(shortfile) + 1 for the command line
118
	- stack_paras paragraphs for the stack */
119
  alloc_paras = 16 + (sizeof(comstub) + strlen(shortfile) + 16) / 16 + stack_paras;
120
  alloc_bytes = 16 * alloc_paras;
121
  comstub[1] = alloc_bytes & 0xff;
122
  comstub[2] = alloc_bytes >> 8;
123
  comstub[4] = alloc_paras & 0xff;
124
  comstub[5] = alloc_paras >> 8;
125
  /* write new content into the link file */
126
  fd = fopen(linkfile, "wb");
127
  if (fd == NULL) {
128
    kitten_printf(3, 22, "Error: Failed to open link file '%s' for write access.", linkfile);
129
    puts("");
130
    return;
131
  }
132
  fwrite(comstub, 1, sizeof(comstub), fd);
133
  fprintf(fd, "%s%s%c", buff, shortfile, 0);
134
  fclose(fd);
135
}
136
 
137
 
138
/* returns 0 if pkgname is not installed, non-zero otherwise */
230 mateuszvis 139
int is_package_installed(const char *pkgname, const char *dosdir) {
219 mateuszvis 140
  char fname[512];
141
  sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
230 mateuszvis 142
  return(fileexists(fname)); /* file exists -> package is installed */
219 mateuszvis 143
}
144
 
145
 
146
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
230 mateuszvis 147
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
225 mateuszvis 148
  if (is_package_installed(pkgname, dosdir) != 0) {
219 mateuszvis 149
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
150
    puts("");
151
    return(-1);
152
  }
153
  return(0);
154
}
155
 
156
 
157
/* find a filename in a flist linked list, and returns a pointer to it */
236 mateuszvis 158
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
219 mateuszvis 159
  while (flist != NULL) {
160
    if (strcmp(flist->fname, fname) == 0) return(flist);
161
    flist = flist->next;
162
  }
163
  return(NULL);
164
}
165
 
166
 
167
/* prepare a package for installation. this is mandatory before actually installing it!
230 mateuszvis 168
 * returns a pointer to the zip file's index on success, NULL on failure. the **zipfd pointer is updated with a file descriptor to the open zip file to install. */
236 mateuszvis 169
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
170
  char fname[256];
171
  char appinfofile[256];
219 mateuszvis 172
  int appinfopresence;
173
  char *shortfile;
230 mateuszvis 174
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
175
  struct flist_t *flist = NULL;
219 mateuszvis 176
 
177
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
178
 
179
  /* check if not already installed, if already here, print a message "you might want to use update instead"
180
   * of course this must not be done if we are in the process of upgrading said package */
225 mateuszvis 181
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
219 mateuszvis 182
    return(NULL);
183
  }
184
 
185
  /* Now let's check the content of the zip file */
186
 
187
  *zipfd = fopen(zipfile, "rb");
188
  if (*zipfd == NULL) {
189
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
230 mateuszvis 190
    goto RAII;
219 mateuszvis 191
  }
192
  ziplinkedlist = zip_listfiles(*zipfd);
193
  if (ziplinkedlist == NULL) {
194
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
230 mateuszvis 195
    goto RAII;
219 mateuszvis 196
  }
197
  /* if updating, load the list of files belonging to the current package */
198
  if ((flags & PKGINST_UPDATE) != 0) {
199
    flist = pkg_loadflist(pkgname, dosdir);
200
  }
201
  /* Verify that there's no collision with existing local files, look for the appinfo presence, get rid of sources if required, and rename BAT links into COM files */
202
  appinfopresence = 0;
203
  prevzipnode = NULL;
204
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
205
    /* change all slashes to backslashes, and switch into all-lowercase */
206
    slash2backslash(curzipnode->filename);
207
    strtolower(curzipnode->filename);
208
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
209
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
210
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
211
    }
212
    /* is it a "link file"? */
213
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
214
      /* skip links, if that's what the user wants */
215
      if ((flags & PKGINST_SKIPLINKS) != 0) {
216
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see later below */
217
      } else {
218
        /* if it's a *.BAT link, then rename it to *.COM */
219
        char *ext = getfext(curzipnode->filename);
220
        if (strcasecmp(ext, "bat") == 0) sprintf(ext, "com");
221
      }
222
    }
223
 
224
    if (curzipnode->filename[0] == 0) { /* ignore empty filenames (maybe it was empty originally, or has been emptied because it's a dropped source or link) */
225
      if (prevzipnode == NULL) {  /* take the item out of the list */
226
        ziplinkedlist = curzipnode->nextfile;
227
        free(curzipnode); /* free the item */
228
        curzipnode = ziplinkedlist;
229
      } else {
230
        prevzipnode->nextfile = curzipnode->nextfile;
231
        free(curzipnode); /* free the item */
232
        curzipnode = prevzipnode->nextfile;
233
      }
234
      continue; /* go to the next item */
235
    }
236
    /* validate that the file has a valid filename (8+3, no shady chars...) */
237
    if (validfilename(curzipnode->filename) != 0) {
238
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
239
      printf(" %s\n", curzipnode->filename);
230 mateuszvis 240
      goto RAII_ERR;
219 mateuszvis 241
    }
242
    /* look out for collisions with already existing files (unless we are
243
     * updating the package and the local file belongs to it */
244
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
245
    strcat(fname, shortfile);
246
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
247
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
248
      printf(" %s\n", fname);
230 mateuszvis 249
      goto RAII_ERR;
219 mateuszvis 250
    }
251
    /* abort if any entry is encrypted */
252
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
253
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
254
      puts("");
255
      printf(" %s\n", curzipnode->filename);
230 mateuszvis 256
      goto RAII_ERR;
219 mateuszvis 257
    }
258
    /* abort if any file is compressed with an unsupported method */
223 mateuszvis 259
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
219 mateuszvis 260
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
261
      puts("");
262
      printf(" %s\n", curzipnode->filename);
230 mateuszvis 263
      goto RAII_ERR;
219 mateuszvis 264
    }
265
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
266
    prevzipnode = curzipnode;
267
    curzipnode = curzipnode->nextfile;
268
  }
269
  /* if appinfo file not found, this is not a real FreeDOS package */
270
  if (appinfopresence != 1) {
271
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
272
    puts("");
230 mateuszvis 273
    goto RAII_ERR;
274
  }
275
 
276
  goto RAII;
277
 
278
  RAII_ERR:
279
  zip_freelist(&ziplinkedlist);
280
  ziplinkedlist = NULL;
281
  if ((zipfd != NULL) && (*zipfd != NULL)) {
219 mateuszvis 282
    fclose(*zipfd);
230 mateuszvis 283
    *zipfd = NULL;
219 mateuszvis 284
  }
285
 
230 mateuszvis 286
  RAII:
287
  pkg_freeflist(flist);
219 mateuszvis 288
  return(ziplinkedlist);
289
}
290
 
291
 
292
/* install a package that has been prepared already. returns 0 on success,
293
 * or a negative value on error, or a positive value on warning */
236 mateuszvis 294
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
219 mateuszvis 295
  char *buff;
296
  char *fulldestfilename;
297
  char packageslst[64];
298
  char *shortfile;
299
  long filesextractedsuccess = 0, filesextractedfailure = 0;
300
  struct ziplist *curzipnode;
301
  FILE *lstfd;
302
 
303
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
304
 
305
  buff = malloc(512);
306
  fulldestfilename = malloc(1024);
307
  if ((buff == NULL) || (fulldestfilename == NULL)) {
308
    kitten_puts(8, 0, "Out of memory!");
309
    zip_freelist(&ziplinkedlist);
310
    free(buff);
311
    free(fulldestfilename);
312
    return(-1);
313
  }
314
 
315
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
316
  sprintf(buff, "%s\\packages\\", dosdir);
317
  mkpath(buff);
318
 
319
  /* open the lst file */
320
  sprintf(buff, "%s\\%s", dosdir, packageslst);
321
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
322
  if (lstfd == NULL) {
323
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
324
    puts("");
325
    zip_freelist(&ziplinkedlist);
326
    free(buff);
327
    free(fulldestfilename);
328
    return(-2);
329
  }
330
 
331
  /* write list of files in zip into the lst, and create the directories structure */
332
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
333
    int unzip_result;
334
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
335
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
336
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
337
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
338
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
339
    /* create the path, just in case it doesn't exist yet */
340
    mkpath(buff);
341
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
342
    /* Now unzip the file */
343
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
344
    if (unzip_result != 0) {
345
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
346
      printf(" [%d]\n", unzip_result);
347
      filesextractedfailure += 1;
348
    } else {
349
      printf(" %s -> %s\n", curzipnode->filename, buff);
350
      filesextractedsuccess += 1;
351
    }
352
    /* if it's a LINK file, recompute a new content */
353
    if (islinkfile(curzipnode->filename) != 0) {
354
      processlinkfile(fulldestfilename, dosdir, dirlist, buff);
355
    }
356
  }
357
  fclose(lstfd);
358
 
359
  /* free the ziplist and close file descriptor */
360
  zip_freelist(&ziplinkedlist);
361
  free(buff);
362
  free(fulldestfilename);
363
 
364
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
365
  puts("");
366
  return(filesextractedfailure);
367
}