summaryrefslogtreecommitdiff
path: root/src/help.c
blob: 3e83493a8d18e7e5068165b61dcd087d465a7c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * dpkg - main program for package management
 * help.c - various helper routines
 *
 * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
 * Copyright © 2007-2014 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/path.h>
#include <dpkg/subproc.h>

#include "filesdb.h"
#include "main.h"

const char *const statusstrings[]= {
  [PKG_STAT_NOTINSTALLED]    = N_("not installed"),
  [PKG_STAT_CONFIGFILES]     = N_("not installed but configs remain"),
  [PKG_STAT_HALFINSTALLED]   = N_("broken due to failed removal or installation"),
  [PKG_STAT_UNPACKED]        = N_("unpacked but not configured"),
  [PKG_STAT_HALFCONFIGURED]  = N_("broken due to postinst failure"),
  [PKG_STAT_TRIGGERSAWAITED] = N_("awaiting trigger processing by another package"),
  [PKG_STAT_TRIGGERSPENDING] = N_("triggered"),
  [PKG_STAT_INSTALLED]       = N_("installed")
};

struct filenamenode *
namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg,
              struct pkgbin *pkgbin)
{
  struct filenamenode *r;

  if (!namenode->divert) {
    r = namenode;
    return r;
  }

  debug(dbg_eachfile, "namenodetouse namenode='%s' pkg=%s",
        namenode->name, pkgbin_name(pkg, pkgbin, pnaw_always));

  r=
    (namenode->divert->useinstead && namenode->divert->pkgset != pkg->set)
      ? namenode->divert->useinstead : namenode;

  debug(dbg_eachfile,
        "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
        namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
        namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
        namenode->divert->pkgset ? namenode->divert->pkgset->name : "<none>",
        r->name);

  return r;
}

/**
 * Verify that some programs can be found in the PATH.
 */
void checkpath(void) {
  static const char *const prog_list[] = {
    DEFAULTSHELL,
    RM,
    TAR,
    FIND,
    BACKEND,
    /* Mac OS X uses dyld (Mach-O) instead of ld.so (ELF), and does not have
     * an ldconfig. */
#if defined(__APPLE__) && defined(__MACH__)
    "update_dyld_shared_cache",
#else
    "ldconfig",
#endif
#if BUILD_START_STOP_DAEMON
    "start-stop-daemon",
#endif
    NULL
  };

  const char *const *prog;
  const char *path_list;
  struct varbuf filename = VARBUF_INIT;
  int warned= 0;

  path_list = getenv("PATH");
  if (!path_list)
    ohshit(_("PATH is not set"));

  for (prog = prog_list; *prog; prog++) {
    struct stat stab;
    const char *path, *path_end;
    size_t path_len;

    for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
      path_end = strchr(path, ':');
      path_len = path_end ? (size_t)(path_end - path) : strlen(path);

      varbuf_reset(&filename);
      varbuf_add_buf(&filename, path, path_len);
      if (path_len)
        varbuf_add_char(&filename, '/');
      varbuf_add_str(&filename, *prog);
      varbuf_end_str(&filename);

      if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
        break;
    }
    if (!path) {
      warning(_("'%s' not found in PATH or not executable"), *prog);
      warned++;
    }
  }

  varbuf_destroy(&filename);

  if (warned)
    forcibleerr(fc_badpath,
                P_("%d expected program not found in PATH or not executable\n%s",
                   "%d expected programs not found in PATH or not executable\n%s",
                   warned),
                warned, _("Note: root's PATH should usually contain "
                          "/usr/local/sbin, /usr/sbin and /sbin"));
}

bool
ignore_depends(struct pkginfo *pkg)
{
  struct pkg_list *id;
  for (id= ignoredependss; id; id= id->next)
    if (id->pkg == pkg)
      return true;
  return false;
}

static bool
ignore_depends_possi(struct deppossi *possi)
{
  struct deppossi_pkg_iterator *possi_iter;
  struct pkginfo *pkg;

  possi_iter = deppossi_pkg_iter_new(possi, wpb_installed);
  while ((pkg = deppossi_pkg_iter_next(possi_iter))) {
    if (ignore_depends(pkg)) {
      deppossi_pkg_iter_free(possi_iter);
      return true;
    }
  }
  deppossi_pkg_iter_free(possi_iter);

  return false;
}

bool
force_depends(struct deppossi *possi)
{
  return fc_depends ||
         ignore_depends_possi(possi) ||
         ignore_depends(possi->up->up);
}

bool
force_breaks(struct deppossi *possi)
{
  return fc_breaks ||
         ignore_depends_possi(possi) ||
         ignore_depends(possi->up->up);
}

bool
force_conflicts(struct deppossi *possi)
{
  return fc_conflicts;
}

void clear_istobes(void) {
  struct pkgiterator *it;
  struct pkginfo *pkg;

  it = pkg_db_iter_new();
  while ((pkg = pkg_db_iter_next_pkg(it)) != NULL) {
    ensure_package_clientdata(pkg);
    pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
    pkg->clientdata->replacingfilesandsaid= 0;
  }
  pkg_db_iter_free(it);
}

/*
 * Returns true if the directory contains conffiles belonging to pkg,
 * false otherwise.
 */
bool
dir_has_conffiles(struct filenamenode *file, struct pkginfo *pkg)
{
  struct conffile *conff;
  size_t namelen;

  debug(dbg_veryverbose, "dir_has_conffiles '%s' (from %s)", file->name,
        pkg_name(pkg, pnaw_always));
  namelen = strlen(file->name);
  for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
      if (conff->obsolete)
        continue;
      if (strncmp(file->name, conff->name, namelen) == 0 &&
          conff->name[namelen] == '/') {
	debug(dbg_veryverbose, "directory %s has conffile %s from %s",
	      file->name, conff->name, pkg_name(pkg, pnaw_always));
	return true;
      }
  }
  debug(dbg_veryverbose, "dir_has_conffiles no");
  return false;
}

/*
 * Returns true if the file is used by packages other than pkg,
 * false otherwise.
 */
bool
dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
{
  struct filepackages_iterator *iter;
  struct pkginfo *other_pkg;

  debug(dbg_veryverbose, "dir_is_used_by_others '%s' (except %s)", file->name,
        pkg ? pkg_name(pkg, pnaw_always) : "<none>");

  iter = filepackages_iter_new(file);
  while ((other_pkg = filepackages_iter_next(iter))) {
    debug(dbg_veryverbose, "dir_is_used_by_others considering %s ...",
          pkg_name(other_pkg, pnaw_always));
    if (other_pkg == pkg)
      continue;

    filepackages_iter_free(iter);
    debug(dbg_veryverbose, "dir_is_used_by_others yes");
    return true;
  }
  filepackages_iter_free(iter);

  debug(dbg_veryverbose, "dir_is_used_by_others no");
  return false;
}

/*
 * Returns true if the file is used by pkg, false otherwise.
 */
bool
dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
                   struct fileinlist *list)
{
  struct fileinlist *node;
  size_t namelen;

  debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
        file->name, pkg ? pkg_name(pkg, pnaw_always) : "<none>");

  namelen = strlen(file->name);

  for (node = list; node; node = node->next) {
    debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
          node->namenode->name);

    if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
        node->namenode->name[namelen] == '/') {
      debug(dbg_veryverbose, "dir_is_used_by_pkg yes");
      return true;
    }
  }

  debug(dbg_veryverbose, "dir_is_used_by_pkg no");

  return false;
}

/**
 * Mark a conffile as obsolete.
 *
 * @param pkg		The package owning the conffile.
 * @param namenode	The namenode for the obsolete conffile.
 */
void
conffile_mark_obsolete(struct pkginfo *pkg, struct filenamenode *namenode)
{
  struct conffile *conff;

  for (conff = pkg->installed.conffiles; conff; conff = conff->next) {
    if (strcmp(conff->name, namenode->name) == 0) {
      debug(dbg_conff, "marking %s conffile %s as obsolete",
            pkg_name(pkg, pnaw_always), conff->name);
      conff->obsolete = true;
      return;
    }
  }
}

void oldconffsetflags(const struct conffile *searchconff) {
  struct filenamenode *namenode;

  while (searchconff) {
    namenode= findnamenode(searchconff->name, 0); /* XXX */
    namenode->flags |= fnnf_old_conff;
    if (!namenode->oldhash)
      namenode->oldhash= searchconff->hash;
    debug(dbg_conffdetail, "oldconffsetflags '%s' namenode '%s' flags %o",
          searchconff->name, namenode->name, namenode->flags);
    searchconff= searchconff->next;
  }
}

/*
 * If the pathname to remove is:
 *
 * 1. a sticky or set-id file, or
 * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
 *
 * we change its mode so that a malicious user cannot use it, even if it's
 * linked to another file.
 */
int
secure_unlink(const char *pathname)
{
  struct stat stab;

  if (lstat(pathname,&stab)) return -1;

  return secure_unlink_statted(pathname, &stab);
}

int
secure_unlink_statted(const char *pathname, const struct stat *stab)
{
  if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
      !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
	S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
    if (chmod(pathname, 0600))
      return -1;
  }
  if (unlink(pathname)) return -1;
  return 0;
}

void ensure_pathname_nonexisting(const char *pathname) {
  pid_t pid;
  const char *u;

  u = path_skip_slash_dotslash(pathname);
  assert(*u);

  debug(dbg_eachfile, "ensure_pathname_nonexisting '%s'", pathname);
  if (!rmdir(pathname))
    return; /* Deleted it OK, it was a directory. */
  if (errno == ENOENT || errno == ELOOP) return;
  if (errno == ENOTDIR) {
    /* Either it's a file, or one of the path components is. If one
     * of the path components is this will fail again ... */
    if (secure_unlink(pathname) == 0)
      return; /* OK, it was. */
    if (errno == ENOTDIR) return;
  }
  if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh? */
    ohshite(_("unable to securely remove '%.255s'"), pathname);
  }
  pid = subproc_fork();
  if (pid == 0) {
    execlp(RM, "rm", "-rf", "--", pathname, NULL);
    ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  }
  debug(dbg_eachfile, "ensure_pathname_nonexisting running rm -rf '%s'",
        pathname);
  subproc_reap(pid, _("rm command for cleanup"), 0);
}

void
log_action(const char *action, struct pkginfo *pkg, struct pkgbin *pkgbin)
{
  log_message("%s %s %s %s", action, pkgbin_name(pkg, pkgbin, pnaw_always),
	      versiondescribe(&pkg->installed.version, vdew_nonambig),
	      versiondescribe(&pkg->available.version, vdew_nonambig));
  statusfd_send("processing: %s: %s", action,
                pkgbin_name(pkg, pkgbin, pnaw_nonambig));
}