Subversion Repositories SvarDOS

Rev

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