]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
6bfa5ca9285ffd7ff2e572c682930531efed16ca
[apt.git] / apt-pkg / contrib / fileutl.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 File Utilities
6
7 CopyFile - Buffered copy of a single file
8 GetLock - dpkg compatible lock file manipulation (fcntl)
9
10 Most of this source is placed in the Public Domain, do with it what
11 you will
12 It was originally written by Jason Gunthorpe <jgg@debian.org>.
13 FileFd gzip support added by Martin Pitt <martin.pitt@canonical.com>
14
15 The exception is RunScripts() it is under the GPLv2
16
17 ##################################################################### */
18 /*}}}*/
19 // Include Files /*{{{*/
20 #include <config.h>
21
22 #include <apt-pkg/fileutl.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/error.h>
25 #include <apt-pkg/sptr.h>
26 #include <apt-pkg/aptconfiguration.h>
27 #include <apt-pkg/configuration.h>
28 #include <apt-pkg/macros.h>
29
30 #include <ctype.h>
31 #include <stdarg.h>
32 #include <stddef.h>
33 #include <sys/select.h>
34 #include <time.h>
35 #include <string>
36 #include <vector>
37 #include <cstdlib>
38 #include <cstring>
39 #include <cstdio>
40 #include <iostream>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <sys/wait.h>
46 #include <dirent.h>
47 #include <signal.h>
48 #include <errno.h>
49 #include <glob.h>
50 #include <pwd.h>
51 #include <grp.h>
52
53 #include <set>
54 #include <algorithm>
55 #include <memory>
56
57 #ifdef HAVE_ZLIB
58 #include <zlib.h>
59 #endif
60 #ifdef HAVE_BZ2
61 #include <bzlib.h>
62 #endif
63 #ifdef HAVE_LZMA
64 #include <lzma.h>
65 #endif
66 #ifdef HAVE_LZ4
67 #include <lz4frame.h>
68 #endif
69 #include <endian.h>
70 #include <stdint.h>
71
72 #if __gnu_linux__
73 #include <sys/prctl.h>
74 #endif
75
76 #include <apti18n.h>
77 /*}}}*/
78
79 using namespace std;
80
81 /* Should be a multiple of the common page size (4096) */
82 static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024;
83
84 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
85 // ---------------------------------------------------------------------
86 /* */
87 bool RunScripts(const char *Cnf)
88 {
89 Configuration::Item const *Opts = _config->Tree(Cnf);
90 if (Opts == 0 || Opts->Child == 0)
91 return true;
92 Opts = Opts->Child;
93
94 // Fork for running the system calls
95 pid_t Child = ExecFork();
96
97 // This is the child
98 if (Child == 0)
99 {
100 if (_config->FindDir("DPkg::Chroot-Directory","/") != "/")
101 {
102 std::cerr << "Chrooting into "
103 << _config->FindDir("DPkg::Chroot-Directory")
104 << std::endl;
105 if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
106 _exit(100);
107 }
108
109 if (chdir("/tmp/") != 0)
110 _exit(100);
111
112 unsigned int Count = 1;
113 for (; Opts != 0; Opts = Opts->Next, Count++)
114 {
115 if (Opts->Value.empty() == true)
116 continue;
117
118 if(_config->FindB("Debug::RunScripts", false) == true)
119 std::clog << "Running external script: '"
120 << Opts->Value << "'" << std::endl;
121
122 if (system(Opts->Value.c_str()) != 0)
123 _exit(100+Count);
124 }
125 _exit(0);
126 }
127
128 // Wait for the child
129 int Status = 0;
130 while (waitpid(Child,&Status,0) != Child)
131 {
132 if (errno == EINTR)
133 continue;
134 return _error->Errno("waitpid","Couldn't wait for subprocess");
135 }
136
137 // Restore sig int/quit
138 signal(SIGQUIT,SIG_DFL);
139 signal(SIGINT,SIG_DFL);
140
141 // Check for an error code.
142 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
143 {
144 unsigned int Count = WEXITSTATUS(Status);
145 if (Count > 100)
146 {
147 Count -= 100;
148 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
149 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
150 }
151
152 return _error->Error("Sub-process returned an error code");
153 }
154
155 return true;
156 }
157 /*}}}*/
158
159 // CopyFile - Buffered copy of a file /*{{{*/
160 // ---------------------------------------------------------------------
161 /* The caller is expected to set things so that failure causes erasure */
162 bool CopyFile(FileFd &From,FileFd &To)
163 {
164 if (From.IsOpen() == false || To.IsOpen() == false ||
165 From.Failed() == true || To.Failed() == true)
166 return false;
167
168 // Buffered copy between fds
169 constexpr size_t BufSize = APT_BUFFER_SIZE;
170 std::unique_ptr<unsigned char[]> Buf(new unsigned char[BufSize]);
171 unsigned long long ToRead = 0;
172 do {
173 if (From.Read(Buf.get(),BufSize, &ToRead) == false ||
174 To.Write(Buf.get(),ToRead) == false)
175 return false;
176 } while (ToRead != 0);
177
178 return true;
179 }
180 /*}}}*/
181 bool RemoveFile(char const * const Function, std::string const &FileName)/*{{{*/
182 {
183 if (FileName == "/dev/null")
184 return true;
185 errno = 0;
186 if (unlink(FileName.c_str()) != 0)
187 {
188 if (errno == ENOENT)
189 return true;
190
191 return _error->WarningE(Function,_("Problem unlinking the file %s"), FileName.c_str());
192 }
193 return true;
194 }
195 /*}}}*/
196 // GetLock - Gets a lock file /*{{{*/
197 // ---------------------------------------------------------------------
198 /* This will create an empty file of the given name and lock it. Once this
199 is done all other calls to GetLock in any other process will fail with
200 -1. The return result is the fd of the file, the call should call
201 close at some time. */
202 int GetLock(string File,bool Errors)
203 {
204 // GetLock() is used in aptitude on directories with public-write access
205 // Use O_NOFOLLOW here to prevent symlink traversal attacks
206 int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640);
207 if (FD < 0)
208 {
209 // Read only .. can't have locking problems there.
210 if (errno == EROFS)
211 {
212 _error->Warning(_("Not using locking for read only lock file %s"),File.c_str());
213 return dup(0); // Need something for the caller to close
214 }
215
216 if (Errors == true)
217 _error->Errno("open",_("Could not open lock file %s"),File.c_str());
218
219 // Feh.. We do this to distinguish the lock vs open case..
220 errno = EPERM;
221 return -1;
222 }
223 SetCloseExec(FD,true);
224
225 // Acquire a write lock
226 struct flock fl;
227 fl.l_type = F_WRLCK;
228 fl.l_whence = SEEK_SET;
229 fl.l_start = 0;
230 fl.l_len = 0;
231 if (fcntl(FD,F_SETLK,&fl) == -1)
232 {
233 // always close to not leak resources
234 int Tmp = errno;
235 close(FD);
236 errno = Tmp;
237
238 if (errno == ENOLCK)
239 {
240 _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str());
241 return dup(0); // Need something for the caller to close
242 }
243
244 if (Errors == true)
245 _error->Errno("open",_("Could not get lock %s"),File.c_str());
246
247 return -1;
248 }
249
250 return FD;
251 }
252 /*}}}*/
253 // FileExists - Check if a file exists /*{{{*/
254 // ---------------------------------------------------------------------
255 /* Beware: Directories are also files! */
256 bool FileExists(string File)
257 {
258 struct stat Buf;
259 if (stat(File.c_str(),&Buf) != 0)
260 return false;
261 return true;
262 }
263 /*}}}*/
264 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
265 // ---------------------------------------------------------------------
266 /* */
267 bool RealFileExists(string File)
268 {
269 struct stat Buf;
270 if (stat(File.c_str(),&Buf) != 0)
271 return false;
272 return ((Buf.st_mode & S_IFREG) != 0);
273 }
274 /*}}}*/
275 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
276 // ---------------------------------------------------------------------
277 /* */
278 bool DirectoryExists(string const &Path)
279 {
280 struct stat Buf;
281 if (stat(Path.c_str(),&Buf) != 0)
282 return false;
283 return ((Buf.st_mode & S_IFDIR) != 0);
284 }
285 /*}}}*/
286 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
287 // ---------------------------------------------------------------------
288 /* This method will create all directories needed for path in good old
289 mkdir -p style but refuses to do this if Parent is not a prefix of
290 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
291 so it will create apt/archives if /var/cache exists - on the other
292 hand if the parent is /var/lib the creation will fail as this path
293 is not a parent of the path to be generated. */
294 bool CreateDirectory(string const &Parent, string const &Path)
295 {
296 if (Parent.empty() == true || Path.empty() == true)
297 return false;
298
299 if (DirectoryExists(Path) == true)
300 return true;
301
302 if (DirectoryExists(Parent) == false)
303 return false;
304
305 // we are not going to create directories "into the blue"
306 if (Path.compare(0, Parent.length(), Parent) != 0)
307 return false;
308
309 vector<string> const dirs = VectorizeString(Path.substr(Parent.size()), '/');
310 string progress = Parent;
311 for (vector<string>::const_iterator d = dirs.begin(); d != dirs.end(); ++d)
312 {
313 if (d->empty() == true)
314 continue;
315
316 progress.append("/").append(*d);
317 if (DirectoryExists(progress) == true)
318 continue;
319
320 if (mkdir(progress.c_str(), 0755) != 0)
321 return false;
322 }
323 return true;
324 }
325 /*}}}*/
326 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
327 // ---------------------------------------------------------------------
328 /* a small wrapper around CreateDirectory to check if it exists and to
329 remove the trailing "/apt/" from the parent directory if needed */
330 bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path)
331 {
332 if (DirectoryExists(Path) == true)
333 return true;
334
335 size_t const len = Parent.size();
336 if (len > 5 && Parent.find("/apt/", len - 6, 5) == len - 5)
337 {
338 if (CreateDirectory(Parent.substr(0,len-5), Path) == true)
339 return true;
340 }
341 else if (CreateDirectory(Parent, Path) == true)
342 return true;
343
344 return false;
345 }
346 /*}}}*/
347 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
348 // ---------------------------------------------------------------------
349 /* If an extension is given only files with this extension are included
350 in the returned vector, otherwise every "normal" file is included. */
351 std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
352 bool const &SortList, bool const &AllowNoExt)
353 {
354 std::vector<string> ext;
355 ext.reserve(2);
356 if (Ext.empty() == false)
357 ext.push_back(Ext);
358 if (AllowNoExt == true && ext.empty() == false)
359 ext.push_back("");
360 return GetListOfFilesInDir(Dir, ext, SortList);
361 }
362 std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
363 bool const &SortList)
364 {
365 // Attention debuggers: need to be set with the environment config file!
366 bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false);
367 if (Debug == true)
368 {
369 std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl;
370 if (Ext.empty() == true)
371 std::clog << "\tNO extension" << std::endl;
372 else
373 for (std::vector<string>::const_iterator e = Ext.begin();
374 e != Ext.end(); ++e)
375 std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl;
376 }
377
378 std::vector<string> List;
379
380 if (DirectoryExists(Dir) == false)
381 {
382 _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
383 return List;
384 }
385
386 Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently");
387 DIR *D = opendir(Dir.c_str());
388 if (D == 0)
389 {
390 _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
391 return List;
392 }
393
394 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
395 {
396 // skip "hidden" files
397 if (Ent->d_name[0] == '.')
398 continue;
399
400 // Make sure it is a file and not something else
401 string const File = flCombine(Dir,Ent->d_name);
402 #ifdef _DIRENT_HAVE_D_TYPE
403 if (Ent->d_type != DT_REG)
404 #endif
405 {
406 if (RealFileExists(File) == false)
407 {
408 // do not show ignoration warnings for directories
409 if (
410 #ifdef _DIRENT_HAVE_D_TYPE
411 Ent->d_type == DT_DIR ||
412 #endif
413 DirectoryExists(File) == true)
414 continue;
415 if (SilentIgnore.Match(Ent->d_name) == false)
416 _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str());
417 continue;
418 }
419 }
420
421 // check for accepted extension:
422 // no extension given -> periods are bad as hell!
423 // extensions given -> "" extension allows no extension
424 if (Ext.empty() == false)
425 {
426 string d_ext = flExtension(Ent->d_name);
427 if (d_ext == Ent->d_name) // no extension
428 {
429 if (std::find(Ext.begin(), Ext.end(), "") == Ext.end())
430 {
431 if (Debug == true)
432 std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl;
433 if (SilentIgnore.Match(Ent->d_name) == false)
434 _error->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent->d_name, Dir.c_str());
435 continue;
436 }
437 }
438 else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end())
439 {
440 if (Debug == true)
441 std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl;
442 if (SilentIgnore.Match(Ent->d_name) == false)
443 _error->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent->d_name, Dir.c_str());
444 continue;
445 }
446 }
447
448 // Skip bad filenames ala run-parts
449 const char *C = Ent->d_name;
450 for (; *C != 0; ++C)
451 if (isalpha(*C) == 0 && isdigit(*C) == 0
452 && *C != '_' && *C != '-' && *C != ':') {
453 // no required extension -> dot is a bad character
454 if (*C == '.' && Ext.empty() == false)
455 continue;
456 break;
457 }
458
459 // we don't reach the end of the name -> bad character included
460 if (*C != 0)
461 {
462 if (Debug == true)
463 std::clog << "Bad file: " << Ent->d_name << " → bad character »"
464 << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl;
465 continue;
466 }
467
468 // skip filenames which end with a period. These are never valid
469 if (*(C - 1) == '.')
470 {
471 if (Debug == true)
472 std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl;
473 continue;
474 }
475
476 if (Debug == true)
477 std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl;
478 List.push_back(File);
479 }
480 closedir(D);
481
482 if (SortList == true)
483 std::sort(List.begin(),List.end());
484 return List;
485 }
486 std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList)
487 {
488 bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false);
489 if (Debug == true)
490 std::clog << "Accept in " << Dir << " all regular files" << std::endl;
491
492 std::vector<string> List;
493
494 if (DirectoryExists(Dir) == false)
495 {
496 _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
497 return List;
498 }
499
500 DIR *D = opendir(Dir.c_str());
501 if (D == 0)
502 {
503 _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
504 return List;
505 }
506
507 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
508 {
509 // skip "hidden" files
510 if (Ent->d_name[0] == '.')
511 continue;
512
513 // Make sure it is a file and not something else
514 string const File = flCombine(Dir,Ent->d_name);
515 #ifdef _DIRENT_HAVE_D_TYPE
516 if (Ent->d_type != DT_REG)
517 #endif
518 {
519 if (RealFileExists(File) == false)
520 {
521 if (Debug == true)
522 std::clog << "Bad file: " << Ent->d_name << " → it is not a real file" << std::endl;
523 continue;
524 }
525 }
526
527 // Skip bad filenames ala run-parts
528 const char *C = Ent->d_name;
529 for (; *C != 0; ++C)
530 if (isalpha(*C) == 0 && isdigit(*C) == 0
531 && *C != '_' && *C != '-' && *C != '.')
532 break;
533
534 // we don't reach the end of the name -> bad character included
535 if (*C != 0)
536 {
537 if (Debug == true)
538 std::clog << "Bad file: " << Ent->d_name << " → bad character »" << *C << "« in filename" << std::endl;
539 continue;
540 }
541
542 // skip filenames which end with a period. These are never valid
543 if (*(C - 1) == '.')
544 {
545 if (Debug == true)
546 std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl;
547 continue;
548 }
549
550 if (Debug == true)
551 std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl;
552 List.push_back(File);
553 }
554 closedir(D);
555
556 if (SortList == true)
557 std::sort(List.begin(),List.end());
558 return List;
559 }
560 /*}}}*/
561 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
562 // ---------------------------------------------------------------------
563 /* We return / on failure. */
564 string SafeGetCWD()
565 {
566 // Stash the current dir.
567 char S[300];
568 S[0] = 0;
569 if (getcwd(S,sizeof(S)-2) == 0)
570 return "/";
571 unsigned int Len = strlen(S);
572 S[Len] = '/';
573 S[Len+1] = 0;
574 return S;
575 }
576 /*}}}*/
577 // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
578 // ---------------------------------------------------------------------
579 /* We return / on failure. */
580 time_t GetModificationTime(string const &Path)
581 {
582 struct stat St;
583 if (stat(Path.c_str(), &St) < 0)
584 return -1;
585 return St.st_mtime;
586 }
587 /*}}}*/
588 // flNotDir - Strip the directory from the filename /*{{{*/
589 // ---------------------------------------------------------------------
590 /* */
591 string flNotDir(string File)
592 {
593 string::size_type Res = File.rfind('/');
594 if (Res == string::npos)
595 return File;
596 Res++;
597 return string(File,Res,Res - File.length());
598 }
599 /*}}}*/
600 // flNotFile - Strip the file from the directory name /*{{{*/
601 // ---------------------------------------------------------------------
602 /* Result ends in a / */
603 string flNotFile(string File)
604 {
605 string::size_type Res = File.rfind('/');
606 if (Res == string::npos)
607 return "./";
608 Res++;
609 return string(File,0,Res);
610 }
611 /*}}}*/
612 // flExtension - Return the extension for the file /*{{{*/
613 // ---------------------------------------------------------------------
614 /* */
615 string flExtension(string File)
616 {
617 string::size_type Res = File.rfind('.');
618 if (Res == string::npos)
619 return File;
620 Res++;
621 return string(File,Res,Res - File.length());
622 }
623 /*}}}*/
624 // flNoLink - If file is a symlink then deref it /*{{{*/
625 // ---------------------------------------------------------------------
626 /* If the name is not a link then the returned path is the input. */
627 string flNoLink(string File)
628 {
629 struct stat St;
630 if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0)
631 return File;
632 if (stat(File.c_str(),&St) != 0)
633 return File;
634
635 /* Loop resolving the link. There is no need to limit the number of
636 loops because the stat call above ensures that the symlink is not
637 circular */
638 char Buffer[1024];
639 string NFile = File;
640 while (1)
641 {
642 // Read the link
643 ssize_t Res;
644 if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 ||
645 (size_t)Res >= sizeof(Buffer))
646 return File;
647
648 // Append or replace the previous path
649 Buffer[Res] = 0;
650 if (Buffer[0] == '/')
651 NFile = Buffer;
652 else
653 NFile = flNotFile(NFile) + Buffer;
654
655 // See if we are done
656 if (lstat(NFile.c_str(),&St) != 0)
657 return File;
658 if (S_ISLNK(St.st_mode) == 0)
659 return NFile;
660 }
661 }
662 /*}}}*/
663 // flCombine - Combine a file and a directory /*{{{*/
664 // ---------------------------------------------------------------------
665 /* If the file is an absolute path then it is just returned, otherwise
666 the directory is pre-pended to it. */
667 string flCombine(string Dir,string File)
668 {
669 if (File.empty() == true)
670 return string();
671
672 if (File[0] == '/' || Dir.empty() == true)
673 return File;
674 if (File.length() >= 2 && File[0] == '.' && File[1] == '/')
675 return File;
676 if (Dir[Dir.length()-1] == '/')
677 return Dir + File;
678 return Dir + '/' + File;
679 }
680 /*}}}*/
681 // flAbsPath - Return the absolute path of the filename /*{{{*/
682 // ---------------------------------------------------------------------
683 /* */
684 string flAbsPath(string File)
685 {
686 char *p = realpath(File.c_str(), NULL);
687 if (p == NULL)
688 {
689 _error->Errno("realpath", "flAbsPath on %s failed", File.c_str());
690 return "";
691 }
692 std::string AbsPath(p);
693 free(p);
694 return AbsPath;
695 }
696 /*}}}*/
697 // SetCloseExec - Set the close on exec flag /*{{{*/
698 // ---------------------------------------------------------------------
699 /* */
700 void SetCloseExec(int Fd,bool Close)
701 {
702 if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0)
703 {
704 cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl;
705 exit(100);
706 }
707 }
708 /*}}}*/
709 // SetNonBlock - Set the nonblocking flag /*{{{*/
710 // ---------------------------------------------------------------------
711 /* */
712 void SetNonBlock(int Fd,bool Block)
713 {
714 int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK);
715 if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0)
716 {
717 cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl;
718 exit(100);
719 }
720 }
721 /*}}}*/
722 // WaitFd - Wait for a FD to become readable /*{{{*/
723 // ---------------------------------------------------------------------
724 /* This waits for a FD to become readable using select. It is useful for
725 applications making use of non-blocking sockets. The timeout is
726 in seconds. */
727 bool WaitFd(int Fd,bool write,unsigned long timeout)
728 {
729 fd_set Set;
730 struct timeval tv;
731 FD_ZERO(&Set);
732 FD_SET(Fd,&Set);
733 tv.tv_sec = timeout;
734 tv.tv_usec = 0;
735 if (write == true)
736 {
737 int Res;
738 do
739 {
740 Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0));
741 }
742 while (Res < 0 && errno == EINTR);
743
744 if (Res <= 0)
745 return false;
746 }
747 else
748 {
749 int Res;
750 do
751 {
752 Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0));
753 }
754 while (Res < 0 && errno == EINTR);
755
756 if (Res <= 0)
757 return false;
758 }
759
760 return true;
761 }
762 /*}}}*/
763 // MergeKeepFdsFromConfiguration - Merge APT::Keep-Fds configuration /*{{{*/
764 // ---------------------------------------------------------------------
765 /* This is used to merge the APT::Keep-Fds with the provided KeepFDs
766 * set.
767 */
768 void MergeKeepFdsFromConfiguration(std::set<int> &KeepFDs)
769 {
770 Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds");
771 if (Opts != 0 && Opts->Child != 0)
772 {
773 Opts = Opts->Child;
774 for (; Opts != 0; Opts = Opts->Next)
775 {
776 if (Opts->Value.empty() == true)
777 continue;
778 int fd = atoi(Opts->Value.c_str());
779 KeepFDs.insert(fd);
780 }
781 }
782 }
783 /*}}}*/
784 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
785 // ---------------------------------------------------------------------
786 /* This is used if you want to cleanse the environment for the forked
787 child, it fixes up the important signals and nukes all of the fds,
788 otherwise acts like normal fork. */
789 pid_t ExecFork()
790 {
791 set<int> KeepFDs;
792 // we need to merge the Keep-Fds as external tools like
793 // debconf-apt-progress use it
794 MergeKeepFdsFromConfiguration(KeepFDs);
795 return ExecFork(KeepFDs);
796 }
797
798 pid_t ExecFork(std::set<int> KeepFDs)
799 {
800 // Fork off the process
801 pid_t Process = fork();
802 if (Process < 0)
803 {
804 cerr << "FATAL -> Failed to fork." << endl;
805 exit(100);
806 }
807
808 // Spawn the subprocess
809 if (Process == 0)
810 {
811 // Setup the signals
812 signal(SIGPIPE,SIG_DFL);
813 signal(SIGQUIT,SIG_DFL);
814 signal(SIGINT,SIG_DFL);
815 signal(SIGWINCH,SIG_DFL);
816 signal(SIGCONT,SIG_DFL);
817 signal(SIGTSTP,SIG_DFL);
818
819 DIR *dir = opendir("/proc/self/fd");
820 if (dir != NULL)
821 {
822 struct dirent *ent;
823 while ((ent = readdir(dir)))
824 {
825 int fd = atoi(ent->d_name);
826 // If fd > 0, it was a fd number and not . or ..
827 if (fd >= 3 && KeepFDs.find(fd) == KeepFDs.end())
828 fcntl(fd,F_SETFD,FD_CLOEXEC);
829 }
830 closedir(dir);
831 } else {
832 long ScOpenMax = sysconf(_SC_OPEN_MAX);
833 // Close all of our FDs - just in case
834 for (int K = 3; K != ScOpenMax; K++)
835 {
836 if(KeepFDs.find(K) == KeepFDs.end())
837 fcntl(K,F_SETFD,FD_CLOEXEC);
838 }
839 }
840 }
841
842 return Process;
843 }
844 /*}}}*/
845 // ExecWait - Fancy waitpid /*{{{*/
846 // ---------------------------------------------------------------------
847 /* Waits for the given sub process. If Reap is set then no errors are
848 generated. Otherwise a failed subprocess will generate a proper descriptive
849 message */
850 bool ExecWait(pid_t Pid,const char *Name,bool Reap)
851 {
852 if (Pid <= 1)
853 return true;
854
855 // Wait and collect the error code
856 int Status;
857 while (waitpid(Pid,&Status,0) != Pid)
858 {
859 if (errno == EINTR)
860 continue;
861
862 if (Reap == true)
863 return false;
864
865 return _error->Error(_("Waited for %s but it wasn't there"),Name);
866 }
867
868
869 // Check for an error code.
870 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
871 {
872 if (Reap == true)
873 return false;
874 if (WIFSIGNALED(Status) != 0)
875 {
876 if( WTERMSIG(Status) == SIGSEGV)
877 return _error->Error(_("Sub-process %s received a segmentation fault."),Name);
878 else
879 return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status));
880 }
881
882 if (WIFEXITED(Status) != 0)
883 return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status));
884
885 return _error->Error(_("Sub-process %s exited unexpectedly"),Name);
886 }
887
888 return true;
889 }
890 /*}}}*/
891 // StartsWithGPGClearTextSignature - Check if a file is Pgp/GPG clearsigned /*{{{*/
892 bool StartsWithGPGClearTextSignature(string const &FileName)
893 {
894 static const char* SIGMSG = "-----BEGIN PGP SIGNED MESSAGE-----\n";
895 char buffer[strlen(SIGMSG)+1];
896 FILE* gpg = fopen(FileName.c_str(), "r");
897 if (gpg == NULL)
898 return false;
899
900 char const * const test = fgets(buffer, sizeof(buffer), gpg);
901 fclose(gpg);
902 if (test == NULL || strcmp(buffer, SIGMSG) != 0)
903 return false;
904
905 return true;
906 }
907 /*}}}*/
908 // ChangeOwnerAndPermissionOfFile - set file attributes to requested values /*{{{*/
909 bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode)
910 {
911 if (strcmp(file, "/dev/null") == 0)
912 return true;
913 bool Res = true;
914 if (getuid() == 0 && strlen(user) != 0 && strlen(group) != 0) // if we aren't root, we can't chown, so don't try it
915 {
916 // ensure the file is owned by root and has good permissions
917 struct passwd const * const pw = getpwnam(user);
918 struct group const * const gr = getgrnam(group);
919 if (pw != NULL && gr != NULL && chown(file, pw->pw_uid, gr->gr_gid) != 0)
920 Res &= _error->WarningE(requester, "chown to %s:%s of file %s failed", user, group, file);
921 }
922 if (chmod(file, mode) != 0)
923 Res &= _error->WarningE(requester, "chmod 0%o of file %s failed", mode, file);
924 return Res;
925 }
926 /*}}}*/
927
928 struct APT_HIDDEN simple_buffer { /*{{{*/
929 size_t buffersize_max = 0;
930 unsigned long long bufferstart = 0;
931 unsigned long long bufferend = 0;
932 char *buffer = nullptr;
933
934 simple_buffer() {
935 reset(4096);
936 }
937 ~simple_buffer() {
938 delete[] buffer;
939 }
940
941 const char *get() const { return buffer + bufferstart; }
942 char *get() { return buffer + bufferstart; }
943 const char *getend() const { return buffer + bufferend; }
944 char *getend() { return buffer + bufferend; }
945 bool empty() const { return bufferend <= bufferstart; }
946 bool full() const { return bufferend == buffersize_max; }
947 unsigned long long free() const { return buffersize_max - bufferend; }
948 unsigned long long size() const { return bufferend-bufferstart; }
949 void reset(size_t size)
950 {
951 if (size > buffersize_max) {
952 delete[] buffer;
953 buffersize_max = size;
954 buffer = new char[size];
955 }
956 reset();
957 }
958 void reset() { bufferend = bufferstart = 0; }
959 ssize_t read(void *to, unsigned long long requested_size) APT_MUSTCHECK
960 {
961 if (size() < requested_size)
962 requested_size = size();
963 memcpy(to, buffer + bufferstart, requested_size);
964 bufferstart += requested_size;
965 if (bufferstart == bufferend)
966 bufferstart = bufferend = 0;
967 return requested_size;
968 }
969 ssize_t write(const void *from, unsigned long long requested_size) APT_MUSTCHECK
970 {
971 if (free() < requested_size)
972 requested_size = free();
973 memcpy(getend(), from, requested_size);
974 bufferend += requested_size;
975 if (bufferstart == bufferend)
976 bufferstart = bufferend = 0;
977 return requested_size;
978 }
979 };
980 /*}}}*/
981
982 class APT_HIDDEN FileFdPrivate { /*{{{*/
983 friend class BufferedWriteFileFdPrivate;
984 protected:
985 FileFd * const filefd;
986 simple_buffer buffer;
987 int compressed_fd;
988 pid_t compressor_pid;
989 bool is_pipe;
990 APT::Configuration::Compressor compressor;
991 unsigned int openmode;
992 unsigned long long seekpos;
993 public:
994
995 explicit FileFdPrivate(FileFd * const pfilefd) : filefd(pfilefd),
996 compressed_fd(-1), compressor_pid(-1), is_pipe(false),
997 openmode(0), seekpos(0) {};
998 virtual APT::Configuration::Compressor get_compressor() const
999 {
1000 return compressor;
1001 }
1002 virtual void set_compressor(APT::Configuration::Compressor const &compressor)
1003 {
1004 this->compressor = compressor;
1005 }
1006 virtual unsigned int get_openmode() const
1007 {
1008 return openmode;
1009 }
1010 virtual void set_openmode(unsigned int openmode)
1011 {
1012 this->openmode = openmode;
1013 }
1014 virtual bool get_is_pipe() const
1015 {
1016 return is_pipe;
1017 }
1018 virtual void set_is_pipe(bool is_pipe)
1019 {
1020 this->is_pipe = is_pipe;
1021 }
1022 virtual unsigned long long get_seekpos() const
1023 {
1024 return seekpos;
1025 }
1026 virtual void set_seekpos(unsigned long long seekpos)
1027 {
1028 this->seekpos = seekpos;
1029 }
1030
1031 virtual bool InternalOpen(int const iFd, unsigned int const Mode) = 0;
1032 ssize_t InternalRead(void * To, unsigned long long Size)
1033 {
1034 // Drain the buffer if needed.
1035 if (buffer.empty() == false)
1036 {
1037 return buffer.read(To, Size);
1038 }
1039 return InternalUnbufferedRead(To, Size);
1040 }
1041 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) = 0;
1042 virtual bool InternalReadError() { return filefd->FileFdErrno("read",_("Read error")); }
1043 virtual char * InternalReadLine(char * To, unsigned long long Size)
1044 {
1045 if (unlikely(Size == 0))
1046 return nullptr;
1047 // Read one byte less than buffer size to have space for trailing 0.
1048 --Size;
1049
1050 char * const InitialTo = To;
1051
1052 while (Size > 0) {
1053 if (buffer.empty() == true)
1054 {
1055 buffer.reset();
1056 unsigned long long actualread = 0;
1057 if (filefd->Read(buffer.getend(), buffer.free(), &actualread) == false)
1058 return nullptr;
1059 buffer.bufferend = actualread;
1060 if (buffer.size() == 0)
1061 {
1062 if (To == InitialTo)
1063 return nullptr;
1064 break;
1065 }
1066 filefd->Flags &= ~FileFd::HitEof;
1067 }
1068
1069 unsigned long long const OutputSize = std::min(Size, buffer.size());
1070 char const * const newline = static_cast<char const * const>(memchr(buffer.get(), '\n', OutputSize));
1071 // Read until end of line or up to Size bytes from the buffer.
1072 unsigned long long actualread = buffer.read(To,
1073 (newline != nullptr)
1074 ? (newline - buffer.get()) + 1
1075 : OutputSize);
1076 To += actualread;
1077 Size -= actualread;
1078 if (newline != nullptr)
1079 break;
1080 }
1081 *To = '\0';
1082 return InitialTo;
1083 }
1084 virtual bool InternalFlush()
1085 {
1086 return true;
1087 }
1088 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) = 0;
1089 virtual bool InternalWriteError() { return filefd->FileFdErrno("write",_("Write error")); }
1090 virtual bool InternalSeek(unsigned long long const To)
1091 {
1092 // Our poor man seeking is costly, so try to avoid it
1093 unsigned long long const iseekpos = filefd->Tell();
1094 if (iseekpos == To)
1095 return true;
1096 else if (iseekpos < To)
1097 return filefd->Skip(To - iseekpos);
1098
1099 if ((openmode & FileFd::ReadOnly) != FileFd::ReadOnly)
1100 return filefd->FileFdError("Reopen is only implemented for read-only files!");
1101 InternalClose(filefd->FileName);
1102 if (filefd->iFd != -1)
1103 close(filefd->iFd);
1104 filefd->iFd = -1;
1105 if (filefd->TemporaryFileName.empty() == false)
1106 filefd->iFd = open(filefd->TemporaryFileName.c_str(), O_RDONLY);
1107 else if (filefd->FileName.empty() == false)
1108 filefd->iFd = open(filefd->FileName.c_str(), O_RDONLY);
1109 else
1110 {
1111 if (compressed_fd > 0)
1112 if (lseek(compressed_fd, 0, SEEK_SET) != 0)
1113 filefd->iFd = compressed_fd;
1114 if (filefd->iFd < 0)
1115 return filefd->FileFdError("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!");
1116 }
1117
1118 if (filefd->OpenInternDescriptor(openmode, compressor) == false)
1119 return filefd->FileFdError("Seek on file %s because it couldn't be reopened", filefd->FileName.c_str());
1120
1121 buffer.reset();
1122 set_seekpos(0);
1123 if (To != 0)
1124 return filefd->Skip(To);
1125
1126 seekpos = To;
1127 return true;
1128 }
1129 virtual bool InternalSkip(unsigned long long Over)
1130 {
1131 unsigned long long constexpr buffersize = 1024;
1132 char buffer[buffersize];
1133 while (Over != 0)
1134 {
1135 unsigned long long toread = std::min(buffersize, Over);
1136 if (filefd->Read(buffer, toread) == false)
1137 return filefd->FileFdError("Unable to seek ahead %llu",Over);
1138 Over -= toread;
1139 }
1140 return true;
1141 }
1142 virtual bool InternalTruncate(unsigned long long const)
1143 {
1144 return filefd->FileFdError("Truncating compressed files is not implemented (%s)", filefd->FileName.c_str());
1145 }
1146 virtual unsigned long long InternalTell()
1147 {
1148 // In theory, we could just return seekpos here always instead of
1149 // seeking around, but not all users of FileFd use always Seek() and co
1150 // so d->seekpos isn't always true and we can just use it as a hint if
1151 // we have nothing else, but not always as an authority…
1152 return seekpos - buffer.size();
1153 }
1154 virtual unsigned long long InternalSize()
1155 {
1156 unsigned long long size = 0;
1157 unsigned long long const oldSeek = filefd->Tell();
1158 unsigned long long constexpr ignoresize = 1024;
1159 char ignore[ignoresize];
1160 unsigned long long read = 0;
1161 do {
1162 if (filefd->Read(ignore, ignoresize, &read) == false)
1163 {
1164 filefd->Seek(oldSeek);
1165 return 0;
1166 }
1167 } while(read != 0);
1168 size = filefd->Tell();
1169 filefd->Seek(oldSeek);
1170 return size;
1171 }
1172 virtual bool InternalClose(std::string const &FileName) = 0;
1173 virtual bool InternalStream() const { return false; }
1174 virtual bool InternalAlwaysAutoClose() const { return true; }
1175
1176 virtual ~FileFdPrivate() {}
1177 };
1178 /*}}}*/
1179 class APT_HIDDEN BufferedWriteFileFdPrivate : public FileFdPrivate { /*{{{*/
1180 protected:
1181 FileFdPrivate *wrapped;
1182 simple_buffer writebuffer;
1183
1184 public:
1185
1186 explicit BufferedWriteFileFdPrivate(FileFdPrivate *Priv) :
1187 FileFdPrivate(Priv->filefd), wrapped(Priv) {};
1188
1189 virtual APT::Configuration::Compressor get_compressor() const APT_OVERRIDE
1190 {
1191 return wrapped->get_compressor();
1192 }
1193 virtual void set_compressor(APT::Configuration::Compressor const &compressor) APT_OVERRIDE
1194 {
1195 return wrapped->set_compressor(compressor);
1196 }
1197 virtual unsigned int get_openmode() const APT_OVERRIDE
1198 {
1199 return wrapped->get_openmode();
1200 }
1201 virtual void set_openmode(unsigned int openmode) APT_OVERRIDE
1202 {
1203 return wrapped->set_openmode(openmode);
1204 }
1205 virtual bool get_is_pipe() const APT_OVERRIDE
1206 {
1207 return wrapped->get_is_pipe();
1208 }
1209 virtual void set_is_pipe(bool is_pipe) APT_OVERRIDE
1210 {
1211 FileFdPrivate::set_is_pipe(is_pipe);
1212 wrapped->set_is_pipe(is_pipe);
1213 }
1214 virtual unsigned long long get_seekpos() const APT_OVERRIDE
1215 {
1216 return wrapped->get_seekpos();
1217 }
1218 virtual void set_seekpos(unsigned long long seekpos) APT_OVERRIDE
1219 {
1220 return wrapped->set_seekpos(seekpos);
1221 }
1222 virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE
1223 {
1224 if (InternalFlush() == false)
1225 return false;
1226 return wrapped->InternalOpen(iFd, Mode);
1227 }
1228 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
1229 {
1230 if (InternalFlush() == false)
1231 return -1;
1232 return wrapped->InternalUnbufferedRead(To, Size);
1233
1234 }
1235 virtual bool InternalReadError() APT_OVERRIDE
1236 {
1237 return wrapped->InternalReadError();
1238 }
1239 virtual char * InternalReadLine(char * To, unsigned long long Size) APT_OVERRIDE
1240 {
1241 if (InternalFlush() == false)
1242 return nullptr;
1243 return wrapped->InternalReadLine(To, Size);
1244 }
1245 virtual bool InternalFlush() APT_OVERRIDE
1246 {
1247 while (writebuffer.empty() == false) {
1248 auto written = wrapped->InternalWrite(writebuffer.get(),
1249 writebuffer.size());
1250 // Ignore interrupted syscalls
1251 if (written < 0 && errno == EINTR)
1252 continue;
1253 if (written < 0)
1254 return wrapped->InternalWriteError();
1255
1256 writebuffer.bufferstart += written;
1257 }
1258
1259 writebuffer.reset();
1260 return true;
1261 }
1262 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
1263 {
1264 // Optimisation: If the buffer is empty and we have more to write than
1265 // would fit in the buffer (or equal number of bytes), write directly.
1266 if (writebuffer.empty() == true && Size >= writebuffer.free())
1267 return wrapped->InternalWrite(From, Size);
1268
1269 // Write as much into the buffer as possible and then flush if needed
1270 auto written = writebuffer.write(From, Size);
1271
1272 if (writebuffer.full() && InternalFlush() == false)
1273 return -1;
1274
1275 return written;
1276 }
1277 virtual bool InternalWriteError() APT_OVERRIDE
1278 {
1279 return wrapped->InternalWriteError();
1280 }
1281 virtual bool InternalSeek(unsigned long long const To) APT_OVERRIDE
1282 {
1283 if (InternalFlush() == false)
1284 return false;
1285 return wrapped->InternalSeek(To);
1286 }
1287 virtual bool InternalSkip(unsigned long long Over) APT_OVERRIDE
1288 {
1289 if (InternalFlush() == false)
1290 return false;
1291 return wrapped->InternalSkip(Over);
1292 }
1293 virtual bool InternalTruncate(unsigned long long const Size) APT_OVERRIDE
1294 {
1295 if (InternalFlush() == false)
1296 return false;
1297 return wrapped->InternalTruncate(Size);
1298 }
1299 virtual unsigned long long InternalTell() APT_OVERRIDE
1300 {
1301 if (InternalFlush() == false)
1302 return -1;
1303 return wrapped->InternalTell();
1304 }
1305 virtual unsigned long long InternalSize() APT_OVERRIDE
1306 {
1307 if (InternalFlush() == false)
1308 return -1;
1309 return wrapped->InternalSize();
1310 }
1311 virtual bool InternalClose(std::string const &FileName) APT_OVERRIDE
1312 {
1313 return wrapped->InternalClose(FileName);
1314 }
1315 virtual bool InternalAlwaysAutoClose() const APT_OVERRIDE
1316 {
1317 return wrapped->InternalAlwaysAutoClose();
1318 }
1319 virtual ~BufferedWriteFileFdPrivate()
1320 {
1321 delete wrapped;
1322 }
1323 };
1324 /*}}}*/
1325 class APT_HIDDEN GzipFileFdPrivate: public FileFdPrivate { /*{{{*/
1326 #ifdef HAVE_ZLIB
1327 public:
1328 gzFile gz;
1329 virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE
1330 {
1331 if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite)
1332 gz = gzdopen(iFd, "r+");
1333 else if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly)
1334 gz = gzdopen(iFd, "w");
1335 else
1336 gz = gzdopen(iFd, "r");
1337 filefd->Flags |= FileFd::Compressed;
1338 return gz != nullptr;
1339 }
1340 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
1341 {
1342 return gzread(gz, To, Size);
1343 }
1344 virtual bool InternalReadError() APT_OVERRIDE
1345 {
1346 int err;
1347 char const * const errmsg = gzerror(gz, &err);
1348 if (err != Z_ERRNO)
1349 return filefd->FileFdError("gzread: %s (%d: %s)", _("Read error"), err, errmsg);
1350 return FileFdPrivate::InternalReadError();
1351 }
1352 virtual char * InternalReadLine(char * To, unsigned long long Size) APT_OVERRIDE
1353 {
1354 return gzgets(gz, To, Size);
1355 }
1356 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
1357 {
1358 return gzwrite(gz,From,Size);
1359 }
1360 virtual bool InternalWriteError() APT_OVERRIDE
1361 {
1362 int err;
1363 char const * const errmsg = gzerror(gz, &err);
1364 if (err != Z_ERRNO)
1365 return filefd->FileFdError("gzwrite: %s (%d: %s)", _("Write error"), err, errmsg);
1366 return FileFdPrivate::InternalWriteError();
1367 }
1368 virtual bool InternalSeek(unsigned long long const To) APT_OVERRIDE
1369 {
1370 off_t const res = gzseek(gz, To, SEEK_SET);
1371 if (res != (off_t)To)
1372 return filefd->FileFdError("Unable to seek to %llu", To);
1373 seekpos = To;
1374 buffer.reset();
1375 return true;
1376 }
1377 virtual bool InternalSkip(unsigned long long Over) APT_OVERRIDE
1378 {
1379 if (Over >= buffer.size())
1380 {
1381 Over -= buffer.size();
1382 buffer.reset();
1383 }
1384 else
1385 {
1386 buffer.bufferstart += Over;
1387 return true;
1388 }
1389 if (Over == 0)
1390 return true;
1391 off_t const res = gzseek(gz, Over, SEEK_CUR);
1392 if (res < 0)
1393 return filefd->FileFdError("Unable to seek ahead %llu",Over);
1394 seekpos = res;
1395 return true;
1396 }
1397 virtual unsigned long long InternalTell() APT_OVERRIDE
1398 {
1399 return gztell(gz) - buffer.size();
1400 }
1401 virtual unsigned long long InternalSize() APT_OVERRIDE
1402 {
1403 unsigned long long filesize = FileFdPrivate::InternalSize();
1404 // only check gzsize if we are actually a gzip file, just checking for
1405 // "gz" is not sufficient as uncompressed files could be opened with
1406 // gzopen in "direct" mode as well
1407 if (filesize == 0 || gzdirect(gz))
1408 return filesize;
1409
1410 off_t const oldPos = lseek(filefd->iFd, 0, SEEK_CUR);
1411 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
1412 * this ourselves; the original (uncompressed) file size is the last 32
1413 * bits of the file */
1414 // FIXME: Size for gz-files is limited by 32bit… no largefile support
1415 if (lseek(filefd->iFd, -4, SEEK_END) < 0)
1416 {
1417 filefd->FileFdErrno("lseek","Unable to seek to end of gzipped file");
1418 return 0;
1419 }
1420 uint32_t size = 0;
1421 if (read(filefd->iFd, &size, 4) != 4)
1422 {
1423 filefd->FileFdErrno("read","Unable to read original size of gzipped file");
1424 return 0;
1425 }
1426 size = le32toh(size);
1427
1428 if (lseek(filefd->iFd, oldPos, SEEK_SET) < 0)
1429 {
1430 filefd->FileFdErrno("lseek","Unable to seek in gzipped file");
1431 return 0;
1432 }
1433 return size;
1434 }
1435 virtual bool InternalClose(std::string const &FileName) APT_OVERRIDE
1436 {
1437 if (gz == nullptr)
1438 return true;
1439 int const e = gzclose(gz);
1440 gz = nullptr;
1441 // gzdclose() on empty files always fails with "buffer error" here, ignore that
1442 if (e != 0 && e != Z_BUF_ERROR)
1443 return _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str());
1444 return true;
1445 }
1446
1447 explicit GzipFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), gz(nullptr) {}
1448 virtual ~GzipFileFdPrivate() { InternalClose(""); }
1449 #endif
1450 };
1451 /*}}}*/
1452 class APT_HIDDEN Bz2FileFdPrivate: public FileFdPrivate { /*{{{*/
1453 #ifdef HAVE_BZ2
1454 BZFILE* bz2;
1455 public:
1456 virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE
1457 {
1458 if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite)
1459 bz2 = BZ2_bzdopen(iFd, "r+");
1460 else if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly)
1461 bz2 = BZ2_bzdopen(iFd, "w");
1462 else
1463 bz2 = BZ2_bzdopen(iFd, "r");
1464 filefd->Flags |= FileFd::Compressed;
1465 return bz2 != nullptr;
1466 }
1467 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
1468 {
1469 return BZ2_bzread(bz2, To, Size);
1470 }
1471 virtual bool InternalReadError() APT_OVERRIDE
1472 {
1473 int err;
1474 char const * const errmsg = BZ2_bzerror(bz2, &err);
1475 if (err != BZ_IO_ERROR)
1476 return filefd->FileFdError("BZ2_bzread: %s %s (%d: %s)", filefd->FileName.c_str(), _("Read error"), err, errmsg);
1477 return FileFdPrivate::InternalReadError();
1478 }
1479 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
1480 {
1481 return BZ2_bzwrite(bz2, (void*)From, Size);
1482 }
1483 virtual bool InternalWriteError() APT_OVERRIDE
1484 {
1485 int err;
1486 char const * const errmsg = BZ2_bzerror(bz2, &err);
1487 if (err != BZ_IO_ERROR)
1488 return filefd->FileFdError("BZ2_bzwrite: %s %s (%d: %s)", filefd->FileName.c_str(), _("Write error"), err, errmsg);
1489 return FileFdPrivate::InternalWriteError();
1490 }
1491 virtual bool InternalStream() const APT_OVERRIDE { return true; }
1492 virtual bool InternalClose(std::string const &) APT_OVERRIDE
1493 {
1494 if (bz2 == nullptr)
1495 return true;
1496 BZ2_bzclose(bz2);
1497 bz2 = nullptr;
1498 return true;
1499 }
1500
1501 explicit Bz2FileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), bz2(nullptr) {}
1502 virtual ~Bz2FileFdPrivate() { InternalClose(""); }
1503 #endif
1504 };
1505 /*}}}*/
1506 class APT_HIDDEN Lz4FileFdPrivate: public FileFdPrivate { /*{{{*/
1507 static constexpr unsigned long long LZ4_HEADER_SIZE = 19;
1508 static constexpr unsigned long long LZ4_FOOTER_SIZE = 4;
1509 #ifdef HAVE_LZ4
1510 LZ4F_decompressionContext_t dctx;
1511 LZ4F_compressionContext_t cctx;
1512 LZ4F_errorCode_t res;
1513 FileFd backend;
1514 simple_buffer lz4_buffer;
1515 // Count of bytes that the decompressor expects to read next, or buffer size.
1516 size_t next_to_load = APT_BUFFER_SIZE;
1517 public:
1518 virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE
1519 {
1520 if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite)
1521 return _error->Error("lz4 only supports write or read mode");
1522
1523 if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) {
1524 res = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION);
1525 lz4_buffer.reset(LZ4F_compressBound(APT_BUFFER_SIZE, nullptr)
1526 + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE);
1527 } else {
1528 res = LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
1529 lz4_buffer.reset(APT_BUFFER_SIZE);
1530 }
1531
1532 filefd->Flags |= FileFd::Compressed;
1533
1534 if (LZ4F_isError(res))
1535 return false;
1536
1537 unsigned int flags = (Mode & (FileFd::WriteOnly|FileFd::ReadOnly));
1538 if (backend.OpenDescriptor(iFd, flags) == false)
1539 return false;
1540
1541 // Write the file header
1542 if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly)
1543 {
1544 res = LZ4F_compressBegin(cctx, lz4_buffer.buffer, lz4_buffer.buffersize_max, nullptr);
1545 if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false)
1546 return false;
1547 }
1548
1549 return true;
1550 }
1551 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
1552 {
1553 /* Keep reading as long as the compressor still wants to read */
1554 while (next_to_load) {
1555 // Fill compressed buffer;
1556 if (lz4_buffer.empty()) {
1557 unsigned long long read;
1558 /* Reset - if LZ4 decompressor wants to read more, allocate more */
1559 lz4_buffer.reset(next_to_load);
1560 if (backend.Read(lz4_buffer.getend(), lz4_buffer.free(), &read) == false)
1561 return -1;
1562 lz4_buffer.bufferend += read;
1563
1564 /* Expected EOF */
1565 if (read == 0) {
1566 res = -1;
1567 return filefd->FileFdError("LZ4F: %s %s",
1568 filefd->FileName.c_str(),
1569 _("Unexpected end of file")), -1;
1570 }
1571 }
1572 // Drain compressed buffer as far as possible.
1573 size_t in = lz4_buffer.size();
1574 size_t out = Size;
1575
1576 res = LZ4F_decompress(dctx, To, &out, lz4_buffer.get(), &in, nullptr);
1577 if (LZ4F_isError(res))
1578 return -1;
1579
1580 next_to_load = res;
1581 lz4_buffer.bufferstart += in;
1582
1583 if (out != 0)
1584 return out;
1585 }
1586
1587 return 0;
1588 }
1589 virtual bool InternalReadError() APT_OVERRIDE
1590 {
1591 char const * const errmsg = LZ4F_getErrorName(res);
1592
1593 return filefd->FileFdError("LZ4F: %s %s (%zu: %s)", filefd->FileName.c_str(), _("Read error"), res, errmsg);
1594 }
1595 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
1596 {
1597 unsigned long long const towrite = std::min(APT_BUFFER_SIZE, Size);
1598
1599 res = LZ4F_compressUpdate(cctx,
1600 lz4_buffer.buffer, lz4_buffer.buffersize_max,
1601 From, towrite, nullptr);
1602
1603 if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false)
1604 return -1;
1605
1606 return towrite;
1607 }
1608 virtual bool InternalWriteError() APT_OVERRIDE
1609 {
1610 char const * const errmsg = LZ4F_getErrorName(res);
1611
1612 return filefd->FileFdError("LZ4F: %s %s (%zu: %s)", filefd->FileName.c_str(), _("Write error"), res, errmsg);
1613 }
1614 virtual bool InternalStream() const APT_OVERRIDE { return true; }
1615
1616 virtual bool InternalFlush() APT_OVERRIDE
1617 {
1618 return backend.Flush();
1619 }
1620
1621 virtual bool InternalClose(std::string const &) APT_OVERRIDE
1622 {
1623 /* Reset variables */
1624 res = 0;
1625 next_to_load = APT_BUFFER_SIZE;
1626
1627 if (cctx != nullptr)
1628 {
1629 if (filefd->Failed() == false)
1630 {
1631 res = LZ4F_compressEnd(cctx, lz4_buffer.buffer, lz4_buffer.buffersize_max, nullptr);
1632 if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false)
1633 return false;
1634 if (!backend.Flush())
1635 return false;
1636 }
1637 if (!backend.Close())
1638 return false;
1639
1640 res = LZ4F_freeCompressionContext(cctx);
1641 cctx = nullptr;
1642 }
1643
1644 if (dctx != nullptr)
1645 {
1646 res = LZ4F_freeDecompressionContext(dctx);
1647 dctx = nullptr;
1648 }
1649
1650 return LZ4F_isError(res) == false;
1651 }
1652
1653 explicit Lz4FileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), dctx(nullptr), cctx(nullptr) {}
1654 virtual ~Lz4FileFdPrivate() {
1655 InternalClose("");
1656 }
1657 #endif
1658 };
1659 /*}}}*/
1660 class APT_HIDDEN LzmaFileFdPrivate: public FileFdPrivate { /*{{{*/
1661 #ifdef HAVE_LZMA
1662 struct LZMAFILE {
1663 FILE* file;
1664 FileFd * const filefd;
1665 uint8_t buffer[4096];
1666 lzma_stream stream;
1667 lzma_ret err;
1668 bool eof;
1669 bool compressing;
1670
1671 LZMAFILE(FileFd * const fd) : file(nullptr), filefd(fd), eof(false), compressing(false) { buffer[0] = '\0'; }
1672 ~LZMAFILE()
1673 {
1674 if (compressing == true && filefd->Failed() == false)
1675 {
1676 size_t constexpr buffersize = sizeof(buffer)/sizeof(buffer[0]);
1677 while(true)
1678 {
1679 stream.avail_out = buffersize;
1680 stream.next_out = buffer;
1681 err = lzma_code(&stream, LZMA_FINISH);
1682 if (err != LZMA_OK && err != LZMA_STREAM_END)
1683 {
1684 _error->Error("~LZMAFILE: Compress finalisation failed");
1685 break;
1686 }
1687 size_t const n = buffersize - stream.avail_out;
1688 if (n && fwrite(buffer, 1, n, file) != n)
1689 {
1690 _error->Errno("~LZMAFILE",_("Write error"));
1691 break;
1692 }
1693 if (err == LZMA_STREAM_END)
1694 break;
1695 }
1696 }
1697 lzma_end(&stream);
1698 fclose(file);
1699 }
1700 };
1701 LZMAFILE* lzma;
1702 static uint32_t findXZlevel(std::vector<std::string> const &Args)
1703 {
1704 for (auto a = Args.rbegin(); a != Args.rend(); ++a)
1705 if (a->empty() == false && (*a)[0] == '-' && (*a)[1] != '-')
1706 {
1707 auto const number = a->find_last_of("0123456789");
1708 if (number == std::string::npos)
1709 continue;
1710 auto const extreme = a->find("e", number);
1711 uint32_t level = (extreme != std::string::npos) ? LZMA_PRESET_EXTREME : 0;
1712 switch ((*a)[number])
1713 {
1714 case '0': return level | 0;
1715 case '1': return level | 1;
1716 case '2': return level | 2;
1717 case '3': return level | 3;
1718 case '4': return level | 4;
1719 case '5': return level | 5;
1720 case '6': return level | 6;
1721 case '7': return level | 7;
1722 case '8': return level | 8;
1723 case '9': return level | 9;
1724 }
1725 }
1726 return 6;
1727 }
1728 public:
1729 virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE
1730 {
1731 if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite)
1732 return filefd->FileFdError("ReadWrite mode is not supported for lzma/xz files %s", filefd->FileName.c_str());
1733
1734 if (lzma == nullptr)
1735 lzma = new LzmaFileFdPrivate::LZMAFILE(filefd);
1736 if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly)
1737 lzma->file = fdopen(iFd, "w");
1738 else
1739 lzma->file = fdopen(iFd, "r");
1740 filefd->Flags |= FileFd::Compressed;
1741 if (lzma->file == nullptr)
1742 return false;
1743
1744 lzma_stream tmp_stream = LZMA_STREAM_INIT;
1745 lzma->stream = tmp_stream;
1746
1747 if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly)
1748 {
1749 uint32_t const xzlevel = findXZlevel(compressor.CompressArgs);
1750 if (compressor.Name == "xz")
1751 {
1752 if (lzma_easy_encoder(&lzma->stream, xzlevel, LZMA_CHECK_CRC64) != LZMA_OK)
1753 return false;
1754 }
1755 else
1756 {
1757 lzma_options_lzma options;
1758 lzma_lzma_preset(&options, xzlevel);
1759 if (lzma_alone_encoder(&lzma->stream, &options) != LZMA_OK)
1760 return false;
1761 }
1762 lzma->compressing = true;
1763 }
1764 else
1765 {
1766 uint64_t const memlimit = UINT64_MAX;
1767 if (compressor.Name == "xz")
1768 {
1769 if (lzma_auto_decoder(&lzma->stream, memlimit, 0) != LZMA_OK)
1770 return false;
1771 }
1772 else
1773 {
1774 if (lzma_alone_decoder(&lzma->stream, memlimit) != LZMA_OK)
1775 return false;
1776 }
1777 lzma->compressing = false;
1778 }
1779 return true;
1780 }
1781 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
1782 {
1783 ssize_t Res;
1784 if (lzma->eof == true)
1785 return 0;
1786
1787 lzma->stream.next_out = (uint8_t *) To;
1788 lzma->stream.avail_out = Size;
1789 if (lzma->stream.avail_in == 0)
1790 {
1791 lzma->stream.next_in = lzma->buffer;
1792 lzma->stream.avail_in = fread(lzma->buffer, 1, sizeof(lzma->buffer)/sizeof(lzma->buffer[0]), lzma->file);
1793 }
1794 lzma->err = lzma_code(&lzma->stream, LZMA_RUN);
1795 if (lzma->err == LZMA_STREAM_END)
1796 {
1797 lzma->eof = true;
1798 Res = Size - lzma->stream.avail_out;
1799 }
1800 else if (lzma->err != LZMA_OK)
1801 {
1802 Res = -1;
1803 errno = 0;
1804 }
1805 else
1806 {
1807 Res = Size - lzma->stream.avail_out;
1808 if (Res == 0)
1809 {
1810 // lzma run was okay, but produced no output…
1811 Res = -1;
1812 errno = EINTR;
1813 }
1814 }
1815 return Res;
1816 }
1817 virtual bool InternalReadError() APT_OVERRIDE
1818 {
1819 return filefd->FileFdError("lzma_read: %s (%d)", _("Read error"), lzma->err);
1820 }
1821 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
1822 {
1823 ssize_t Res;
1824 lzma->stream.next_in = (uint8_t *)From;
1825 lzma->stream.avail_in = Size;
1826 lzma->stream.next_out = lzma->buffer;
1827 lzma->stream.avail_out = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]);
1828 lzma->err = lzma_code(&lzma->stream, LZMA_RUN);
1829 if (lzma->err != LZMA_OK)
1830 return -1;
1831 size_t const n = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]) - lzma->stream.avail_out;
1832 size_t const m = (n == 0) ? 0 : fwrite(lzma->buffer, 1, n, lzma->file);
1833 if (m != n)
1834 {
1835 Res = -1;
1836 errno = 0;
1837 }
1838 else
1839 {
1840 Res = Size - lzma->stream.avail_in;
1841 if (Res == 0)
1842 {
1843 // lzma run was okay, but produced no output…
1844 Res = -1;
1845 errno = EINTR;
1846 }
1847 }
1848 return Res;
1849 }
1850 virtual bool InternalWriteError() APT_OVERRIDE
1851 {
1852 return filefd->FileFdError("lzma_write: %s (%d)", _("Write error"), lzma->err);
1853 }
1854 virtual bool InternalStream() const APT_OVERRIDE { return true; }
1855 virtual bool InternalClose(std::string const &) APT_OVERRIDE
1856 {
1857 delete lzma;
1858 lzma = nullptr;
1859 return true;
1860 }
1861
1862 explicit LzmaFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), lzma(nullptr) {}
1863 virtual ~LzmaFileFdPrivate() { InternalClose(""); }
1864 #endif
1865 };
1866 /*}}}*/
1867 class APT_HIDDEN PipedFileFdPrivate: public FileFdPrivate /*{{{*/
1868 /* if we don't have a specific class dealing with library calls, we (un)compress
1869 by executing a specified binary and pipe in/out what we need */
1870 {
1871 public:
1872 virtual bool InternalOpen(int const, unsigned int const Mode) APT_OVERRIDE
1873 {
1874 // collect zombies here in case we reopen
1875 if (compressor_pid > 0)
1876 ExecWait(compressor_pid, "FileFdCompressor", true);
1877
1878 if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite)
1879 return filefd->FileFdError("ReadWrite mode is not supported for file %s", filefd->FileName.c_str());
1880
1881 bool const Comp = (Mode & FileFd::WriteOnly) == FileFd::WriteOnly;
1882 if (Comp == false)
1883 {
1884 // Handle 'decompression' of empty files
1885 struct stat Buf;
1886 fstat(filefd->iFd, &Buf);
1887 if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false)
1888 return true;
1889
1890 // We don't need the file open - instead let the compressor open it
1891 // as he properly knows better how to efficiently read from 'his' file
1892 if (filefd->FileName.empty() == false)
1893 {
1894 close(filefd->iFd);
1895 filefd->iFd = -1;
1896 }
1897 }
1898
1899 // Create a data pipe
1900 int Pipe[2] = {-1,-1};
1901 if (pipe(Pipe) != 0)
1902 return filefd->FileFdErrno("pipe",_("Failed to create subprocess IPC"));
1903 for (int J = 0; J != 2; J++)
1904 SetCloseExec(Pipe[J],true);
1905
1906 compressed_fd = filefd->iFd;
1907 set_is_pipe(true);
1908
1909 if (Comp == true)
1910 filefd->iFd = Pipe[1];
1911 else
1912 filefd->iFd = Pipe[0];
1913
1914 // The child..
1915 compressor_pid = ExecFork();
1916 if (compressor_pid == 0)
1917 {
1918 if (Comp == true)
1919 {
1920 dup2(compressed_fd,STDOUT_FILENO);
1921 dup2(Pipe[0],STDIN_FILENO);
1922 }
1923 else
1924 {
1925 if (compressed_fd != -1)
1926 dup2(compressed_fd,STDIN_FILENO);
1927 dup2(Pipe[1],STDOUT_FILENO);
1928 }
1929 int const nullfd = open("/dev/null", O_WRONLY);
1930 if (nullfd != -1)
1931 {
1932 dup2(nullfd,STDERR_FILENO);
1933 close(nullfd);
1934 }
1935
1936 SetCloseExec(STDOUT_FILENO,false);
1937 SetCloseExec(STDIN_FILENO,false);
1938
1939 std::vector<char const*> Args;
1940 Args.push_back(compressor.Binary.c_str());
1941 std::vector<std::string> const * const addArgs =
1942 (Comp == true) ? &(compressor.CompressArgs) : &(compressor.UncompressArgs);
1943 for (std::vector<std::string>::const_iterator a = addArgs->begin();
1944 a != addArgs->end(); ++a)
1945 Args.push_back(a->c_str());
1946 if (Comp == false && filefd->FileName.empty() == false)
1947 {
1948 // commands not needing arguments, do not need to be told about using standard output
1949 // in reality, only testcases with tools like cat, rev, rot13, … are able to trigger this
1950 if (compressor.CompressArgs.empty() == false && compressor.UncompressArgs.empty() == false)
1951 Args.push_back("--stdout");
1952 if (filefd->TemporaryFileName.empty() == false)
1953 Args.push_back(filefd->TemporaryFileName.c_str());
1954 else
1955 Args.push_back(filefd->FileName.c_str());
1956 }
1957 Args.push_back(NULL);
1958
1959 execvp(Args[0],(char **)&Args[0]);
1960 cerr << _("Failed to exec compressor ") << Args[0] << endl;
1961 _exit(100);
1962 }
1963 if (Comp == true)
1964 close(Pipe[0]);
1965 else
1966 close(Pipe[1]);
1967
1968 return true;
1969 }
1970 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
1971 {
1972 return read(filefd->iFd, To, Size);
1973 }
1974 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
1975 {
1976 return write(filefd->iFd, From, Size);
1977 }
1978 virtual bool InternalClose(std::string const &) APT_OVERRIDE
1979 {
1980 bool Ret = true;
1981 if (filefd->iFd != -1)
1982 {
1983 close(filefd->iFd);
1984 filefd->iFd = -1;
1985 }
1986 if (compressor_pid > 0)
1987 Ret &= ExecWait(compressor_pid, "FileFdCompressor", true);
1988 compressor_pid = -1;
1989 return Ret;
1990 }
1991 explicit PipedFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd) {}
1992 virtual ~PipedFileFdPrivate() { InternalClose(""); }
1993 };
1994 /*}}}*/
1995 class APT_HIDDEN DirectFileFdPrivate: public FileFdPrivate /*{{{*/
1996 {
1997 public:
1998 virtual bool InternalOpen(int const, unsigned int const) APT_OVERRIDE { return true; }
1999 virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE
2000 {
2001 return read(filefd->iFd, To, Size);
2002 }
2003 virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE
2004 {
2005 // files opened read+write are strange and only really "supported" for direct files
2006 if (buffer.size() != 0)
2007 {
2008 lseek(filefd->iFd, -buffer.size(), SEEK_CUR);
2009 buffer.reset();
2010 }
2011 return write(filefd->iFd, From, Size);
2012 }
2013 virtual bool InternalSeek(unsigned long long const To) APT_OVERRIDE
2014 {
2015 off_t const res = lseek(filefd->iFd, To, SEEK_SET);
2016 if (res != (off_t)To)
2017 return filefd->FileFdError("Unable to seek to %llu", To);
2018 seekpos = To;
2019 buffer.reset();
2020 return true;
2021 }
2022 virtual bool InternalSkip(unsigned long long Over) APT_OVERRIDE
2023 {
2024 if (Over >= buffer.size())
2025 {
2026 Over -= buffer.size();
2027 buffer.reset();
2028 }
2029 else
2030 {
2031 buffer.bufferstart += Over;
2032 return true;
2033 }
2034 if (Over == 0)
2035 return true;
2036 off_t const res = lseek(filefd->iFd, Over, SEEK_CUR);
2037 if (res < 0)
2038 return filefd->FileFdError("Unable to seek ahead %llu",Over);
2039 seekpos = res;
2040 return true;
2041 }
2042 virtual bool InternalTruncate(unsigned long long const To) APT_OVERRIDE
2043 {
2044 if (buffer.size() != 0)
2045 {
2046 unsigned long long const seekpos = lseek(filefd->iFd, 0, SEEK_CUR);
2047 if ((seekpos - buffer.size()) >= To)
2048 buffer.reset();
2049 else if (seekpos >= To)
2050 buffer.bufferend = (To - seekpos) + buffer.bufferstart;
2051 else
2052 buffer.reset();
2053 }
2054 if (ftruncate(filefd->iFd, To) != 0)
2055 return filefd->FileFdError("Unable to truncate to %llu",To);
2056 return true;
2057 }
2058 virtual unsigned long long InternalTell() APT_OVERRIDE
2059 {
2060 return lseek(filefd->iFd,0,SEEK_CUR) - buffer.size();
2061 }
2062 virtual unsigned long long InternalSize() APT_OVERRIDE
2063 {
2064 return filefd->FileSize();
2065 }
2066 virtual bool InternalClose(std::string const &) APT_OVERRIDE { return true; }
2067 virtual bool InternalAlwaysAutoClose() const APT_OVERRIDE { return false; }
2068
2069 explicit DirectFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd) {}
2070 virtual ~DirectFileFdPrivate() { InternalClose(""); }
2071 };
2072 /*}}}*/
2073 // FileFd Constructors /*{{{*/
2074 FileFd::FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode) : iFd(-1), Flags(0), d(NULL)
2075 {
2076 Open(FileName,Mode, None, AccessMode);
2077 }
2078 FileFd::FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode) : iFd(-1), Flags(0), d(NULL)
2079 {
2080 Open(FileName,Mode, Compress, AccessMode);
2081 }
2082 FileFd::FileFd() : iFd(-1), Flags(AutoClose), d(NULL) {}
2083 FileFd::FileFd(int const Fd, unsigned int const Mode, CompressMode Compress) : iFd(-1), Flags(0), d(NULL)
2084 {
2085 OpenDescriptor(Fd, Mode, Compress);
2086 }
2087 FileFd::FileFd(int const Fd, bool const AutoClose) : iFd(-1), Flags(0), d(NULL)
2088 {
2089 OpenDescriptor(Fd, ReadWrite, None, AutoClose);
2090 }
2091 /*}}}*/
2092 // FileFd::Open - Open a file /*{{{*/
2093 // ---------------------------------------------------------------------
2094 /* The most commonly used open mode combinations are given with Mode */
2095 bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, unsigned long const AccessMode)
2096 {
2097 if (Mode == ReadOnlyGzip)
2098 return Open(FileName, ReadOnly, Gzip, AccessMode);
2099
2100 if (Compress == Auto && (Mode & WriteOnly) == WriteOnly)
2101 return FileFdError("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str());
2102
2103 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
2104 std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
2105 if (Compress == Auto)
2106 {
2107 for (; compressor != compressors.end(); ++compressor)
2108 {
2109 std::string file = FileName + compressor->Extension;
2110 if (FileExists(file) == false)
2111 continue;
2112 FileName = file;
2113 break;
2114 }
2115 }
2116 else if (Compress == Extension)
2117 {
2118 std::string::size_type const found = FileName.find_last_of('.');
2119 std::string ext;
2120 if (found != std::string::npos)
2121 {
2122 ext = FileName.substr(found);
2123 if (ext == ".new" || ext == ".bak")
2124 {
2125 std::string::size_type const found2 = FileName.find_last_of('.', found - 1);
2126 if (found2 != std::string::npos)
2127 ext = FileName.substr(found2, found - found2);
2128 else
2129 ext.clear();
2130 }
2131 }
2132 for (; compressor != compressors.end(); ++compressor)
2133 if (ext == compressor->Extension)
2134 break;
2135 // no matching extension - assume uncompressed (imagine files like 'example.org_Packages')
2136 if (compressor == compressors.end())
2137 for (compressor = compressors.begin(); compressor != compressors.end(); ++compressor)
2138 if (compressor->Name == ".")
2139 break;
2140 }
2141 else
2142 {
2143 std::string name;
2144 switch (Compress)
2145 {
2146 case None: name = "."; break;
2147 case Gzip: name = "gzip"; break;
2148 case Bzip2: name = "bzip2"; break;
2149 case Lzma: name = "lzma"; break;
2150 case Xz: name = "xz"; break;
2151 case Lz4: name = "lz4"; break;
2152 case Auto:
2153 case Extension:
2154 // Unreachable
2155 return FileFdError("Opening File %s in None, Auto or Extension should be already handled?!?", FileName.c_str());
2156 }
2157 for (; compressor != compressors.end(); ++compressor)
2158 if (compressor->Name == name)
2159 break;
2160 if (compressor == compressors.end())
2161 return FileFdError("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str());
2162 }
2163
2164 if (compressor == compressors.end())
2165 return FileFdError("Can't find a match for specified compressor mode for file %s", FileName.c_str());
2166 return Open(FileName, Mode, *compressor, AccessMode);
2167 }
2168 bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const AccessMode)
2169 {
2170 Close();
2171 Flags = AutoClose;
2172
2173 if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0)
2174 return FileFdError("ReadOnly mode for %s doesn't accept additional flags!", FileName.c_str());
2175 if ((Mode & ReadWrite) == 0)
2176 return FileFdError("No openmode provided in FileFd::Open for %s", FileName.c_str());
2177
2178 unsigned int OpenMode = Mode;
2179 if (FileName == "/dev/null")
2180 OpenMode = OpenMode & ~(Atomic | Exclusive | Create | Empty);
2181
2182 if ((OpenMode & Atomic) == Atomic)
2183 {
2184 Flags |= Replace;
2185 }
2186 else if ((OpenMode & (Exclusive | Create)) == (Exclusive | Create))
2187 {
2188 // for atomic, this will be done by rename in Close()
2189 RemoveFile("FileFd::Open", FileName);
2190 }
2191 if ((OpenMode & Empty) == Empty)
2192 {
2193 struct stat Buf;
2194 if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
2195 RemoveFile("FileFd::Open", FileName);
2196 }
2197
2198 int fileflags = 0;
2199 #define if_FLAGGED_SET(FLAG, MODE) if ((OpenMode & FLAG) == FLAG) fileflags |= MODE
2200 if_FLAGGED_SET(ReadWrite, O_RDWR);
2201 else if_FLAGGED_SET(ReadOnly, O_RDONLY);
2202 else if_FLAGGED_SET(WriteOnly, O_WRONLY);
2203
2204 if_FLAGGED_SET(Create, O_CREAT);
2205 if_FLAGGED_SET(Empty, O_TRUNC);
2206 if_FLAGGED_SET(Exclusive, O_EXCL);
2207 #undef if_FLAGGED_SET
2208
2209 if ((OpenMode & Atomic) == Atomic)
2210 {
2211 char *name = strdup((FileName + ".XXXXXX").c_str());
2212
2213 if((iFd = mkstemp(name)) == -1)
2214 {
2215 free(name);
2216 return FileFdErrno("mkstemp", "Could not create temporary file for %s", FileName.c_str());
2217 }
2218
2219 TemporaryFileName = string(name);
2220 free(name);
2221
2222 // umask() will always set the umask and return the previous value, so
2223 // we first set the umask and then reset it to the old value
2224 mode_t const CurrentUmask = umask(0);
2225 umask(CurrentUmask);
2226 // calculate the actual file permissions (just like open/creat)
2227 mode_t const FilePermissions = (AccessMode & ~CurrentUmask);
2228
2229 if(fchmod(iFd, FilePermissions) == -1)
2230 return FileFdErrno("fchmod", "Could not change permissions for temporary file %s", TemporaryFileName.c_str());
2231 }
2232 else
2233 iFd = open(FileName.c_str(), fileflags, AccessMode);
2234
2235 this->FileName = FileName;
2236 if (iFd == -1 || OpenInternDescriptor(OpenMode, compressor) == false)
2237 {
2238 if (iFd != -1)
2239 {
2240 close (iFd);
2241 iFd = -1;
2242 }
2243 return FileFdErrno("open",_("Could not open file %s"), FileName.c_str());
2244 }
2245
2246 SetCloseExec(iFd,true);
2247 return true;
2248 }
2249 /*}}}*/
2250 // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/
2251 bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose)
2252 {
2253 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
2254 std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
2255 std::string name;
2256
2257 // compat with the old API
2258 if (Mode == ReadOnlyGzip && Compress == None)
2259 Compress = Gzip;
2260
2261 switch (Compress)
2262 {
2263 case None: name = "."; break;
2264 case Gzip: name = "gzip"; break;
2265 case Bzip2: name = "bzip2"; break;
2266 case Lzma: name = "lzma"; break;
2267 case Xz: name = "xz"; break;
2268 case Lz4: name = "lz4"; break;
2269 case Auto:
2270 case Extension:
2271 if (AutoClose == true && Fd != -1)
2272 close(Fd);
2273 return FileFdError("Opening Fd %d in Auto or Extension compression mode is not supported", Fd);
2274 }
2275 for (; compressor != compressors.end(); ++compressor)
2276 if (compressor->Name == name)
2277 break;
2278 if (compressor == compressors.end())
2279 {
2280 if (AutoClose == true && Fd != -1)
2281 close(Fd);
2282 return FileFdError("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str());
2283 }
2284 return OpenDescriptor(Fd, Mode, *compressor, AutoClose);
2285 }
2286 bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose)
2287 {
2288 Close();
2289 Flags = (AutoClose) ? FileFd::AutoClose : 0;
2290 iFd = Fd;
2291 this->FileName = "";
2292 if (OpenInternDescriptor(Mode, compressor) == false)
2293 {
2294 if (iFd != -1 && (
2295 (Flags & Compressed) == Compressed ||
2296 AutoClose == true))
2297 {
2298 close (iFd);
2299 iFd = -1;
2300 }
2301 return FileFdError(_("Could not open file descriptor %d"), Fd);
2302 }
2303 return true;
2304 }
2305 bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor)
2306 {
2307 if (iFd == -1)
2308 return false;
2309
2310 if (d != nullptr)
2311 d->InternalClose(FileName);
2312
2313 if (d == nullptr)
2314 {
2315 if (false)
2316 /* dummy so that the rest can be 'else if's */;
2317 #define APT_COMPRESS_INIT(NAME, CONSTRUCTOR) \
2318 else if (compressor.Name == NAME) \
2319 d = new CONSTRUCTOR(this)
2320 #ifdef HAVE_ZLIB
2321 APT_COMPRESS_INIT("gzip", GzipFileFdPrivate);
2322 #endif
2323 #ifdef HAVE_BZ2
2324 APT_COMPRESS_INIT("bzip2", Bz2FileFdPrivate);
2325 #endif
2326 #ifdef HAVE_LZMA
2327 APT_COMPRESS_INIT("xz", LzmaFileFdPrivate);
2328 APT_COMPRESS_INIT("lzma", LzmaFileFdPrivate);
2329 #endif
2330 #ifdef HAVE_LZ4
2331 APT_COMPRESS_INIT("lz4", Lz4FileFdPrivate);
2332 #endif
2333 #undef APT_COMPRESS_INIT
2334 else if (compressor.Name == "." || compressor.Binary.empty() == true)
2335 d = new DirectFileFdPrivate(this);
2336 else
2337 d = new PipedFileFdPrivate(this);
2338
2339 if (Mode & BufferedWrite)
2340 d = new BufferedWriteFileFdPrivate(d);
2341
2342 d->set_openmode(Mode);
2343 d->set_compressor(compressor);
2344 if ((Flags & AutoClose) != AutoClose && d->InternalAlwaysAutoClose())
2345 {
2346 // Need to duplicate fd here or gz/bz2 close for cleanup will close the fd as well
2347 int const internFd = dup(iFd);
2348 if (internFd == -1)
2349 return FileFdErrno("OpenInternDescriptor", _("Could not open file descriptor %d"), iFd);
2350 iFd = internFd;
2351 }
2352 }
2353 return d->InternalOpen(iFd, Mode);
2354 }
2355 /*}}}*/
2356 // FileFd::~File - Closes the file /*{{{*/
2357 // ---------------------------------------------------------------------
2358 /* If the proper modes are selected then we close the Fd and possibly
2359 unlink the file on error. */
2360 FileFd::~FileFd()
2361 {
2362 Close();
2363 if (d != NULL)
2364 d->InternalClose(FileName);
2365 delete d;
2366 d = NULL;
2367 }
2368 /*}}}*/
2369 // FileFd::Read - Read a bit of the file /*{{{*/
2370 // ---------------------------------------------------------------------
2371 /* We are careful to handle interruption by a signal while reading
2372 gracefully. */
2373 bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
2374 {
2375 if (d == nullptr)
2376 return false;
2377 ssize_t Res = 1;
2378 errno = 0;
2379 if (Actual != 0)
2380 *Actual = 0;
2381 *((char *)To) = '\0';
2382 while (Res > 0 && Size > 0)
2383 {
2384 Res = d->InternalRead(To, Size);
2385
2386 if (Res < 0)
2387 {
2388 if (errno == EINTR)
2389 {
2390 // trick the while-loop into running again
2391 Res = 1;
2392 errno = 0;
2393 continue;
2394 }
2395 return d->InternalReadError();
2396 }
2397
2398 To = (char *)To + Res;
2399 Size -= Res;
2400 if (d != NULL)
2401 d->set_seekpos(d->get_seekpos() + Res);
2402 if (Actual != 0)
2403 *Actual += Res;
2404 }
2405
2406 if (Size == 0)
2407 return true;
2408
2409 // Eof handling
2410 if (Actual != 0)
2411 {
2412 Flags |= HitEof;
2413 return true;
2414 }
2415
2416 return FileFdError(_("read, still have %llu to read but none left"), Size);
2417 }
2418 /*}}}*/
2419 // FileFd::ReadLine - Read a complete line from the file /*{{{*/
2420 // ---------------------------------------------------------------------
2421 /* Beware: This method can be quite slow for big buffers on UNcompressed
2422 files because of the naive implementation! */
2423 char* FileFd::ReadLine(char *To, unsigned long long const Size)
2424 {
2425 *To = '\0';
2426 if (d == nullptr)
2427 return nullptr;
2428 return d->InternalReadLine(To, Size);
2429 }
2430 /*}}}*/
2431 // FileFd::Flush - Flush the file /*{{{*/
2432 bool FileFd::Flush()
2433 {
2434 if (d == nullptr)
2435 return true;
2436
2437 return d->InternalFlush();
2438 }
2439 /*}}}*/
2440 // FileFd::Write - Write to the file /*{{{*/
2441 bool FileFd::Write(const void *From,unsigned long long Size)
2442 {
2443 if (d == nullptr)
2444 return false;
2445 ssize_t Res = 1;
2446 errno = 0;
2447 while (Res > 0 && Size > 0)
2448 {
2449 Res = d->InternalWrite(From, Size);
2450
2451 if (Res < 0)
2452 {
2453 if (errno == EINTR)
2454 {
2455 // trick the while-loop into running again
2456 Res = 1;
2457 errno = 0;
2458 continue;
2459 }
2460 return d->InternalWriteError();
2461 }
2462
2463 From = (char const *)From + Res;
2464 Size -= Res;
2465 if (d != NULL)
2466 d->set_seekpos(d->get_seekpos() + Res);
2467 }
2468
2469 if (Size == 0)
2470 return true;
2471
2472 return FileFdError(_("write, still have %llu to write but couldn't"), Size);
2473 }
2474 bool FileFd::Write(int Fd, const void *From, unsigned long long Size)
2475 {
2476 ssize_t Res = 1;
2477 errno = 0;
2478 while (Res > 0 && Size > 0)
2479 {
2480 Res = write(Fd,From,Size);
2481 if (Res < 0 && errno == EINTR)
2482 continue;
2483 if (Res < 0)
2484 return _error->Errno("write",_("Write error"));
2485
2486 From = (char const *)From + Res;
2487 Size -= Res;
2488 }
2489
2490 if (Size == 0)
2491 return true;
2492
2493 return _error->Error(_("write, still have %llu to write but couldn't"), Size);
2494 }
2495 /*}}}*/
2496 // FileFd::Seek - Seek in the file /*{{{*/
2497 bool FileFd::Seek(unsigned long long To)
2498 {
2499 if (d == nullptr)
2500 return false;
2501 Flags &= ~HitEof;
2502 return d->InternalSeek(To);
2503 }
2504 /*}}}*/
2505 // FileFd::Skip - Skip over data in the file /*{{{*/
2506 bool FileFd::Skip(unsigned long long Over)
2507 {
2508 if (d == nullptr)
2509 return false;
2510 return d->InternalSkip(Over);
2511 }
2512 /*}}}*/
2513 // FileFd::Truncate - Truncate the file /*{{{*/
2514 bool FileFd::Truncate(unsigned long long To)
2515 {
2516 if (d == nullptr)
2517 return false;
2518 // truncating /dev/null is always successful - as we get an error otherwise
2519 if (To == 0 && FileName == "/dev/null")
2520 return true;
2521 return d->InternalTruncate(To);
2522 }
2523 /*}}}*/
2524 // FileFd::Tell - Current seek position /*{{{*/
2525 // ---------------------------------------------------------------------
2526 /* */
2527 unsigned long long FileFd::Tell()
2528 {
2529 if (d == nullptr)
2530 return false;
2531 off_t const Res = d->InternalTell();
2532 if (Res == (off_t)-1)
2533 FileFdErrno("lseek","Failed to determine the current file position");
2534 d->set_seekpos(Res);
2535 return Res;
2536 }
2537 /*}}}*/
2538 static bool StatFileFd(char const * const msg, int const iFd, std::string const &FileName, struct stat &Buf, FileFdPrivate * const d) /*{{{*/
2539 {
2540 bool ispipe = (d != NULL && d->get_is_pipe() == true);
2541 if (ispipe == false)
2542 {
2543 if (fstat(iFd,&Buf) != 0)
2544 // higher-level code will generate more meaningful messages,
2545 // even translated this would be meaningless for users
2546 return _error->Errno("fstat", "Unable to determine %s for fd %i", msg, iFd);
2547 if (FileName.empty() == false)
2548 ispipe = S_ISFIFO(Buf.st_mode);
2549 }
2550
2551 // for compressor pipes st_size is undefined and at 'best' zero
2552 if (ispipe == true)
2553 {
2554 // we set it here, too, as we get the info here for free
2555 // in theory the Open-methods should take care of it already
2556 if (d != NULL)
2557 d->set_is_pipe(true);
2558 if (stat(FileName.c_str(), &Buf) != 0)
2559 return _error->Errno("fstat", "Unable to determine %s for file %s", msg, FileName.c_str());
2560 }
2561 return true;
2562 }
2563 /*}}}*/
2564 // FileFd::FileSize - Return the size of the file /*{{{*/
2565 unsigned long long FileFd::FileSize()
2566 {
2567 struct stat Buf;
2568 if (StatFileFd("file size", iFd, FileName, Buf, d) == false)
2569 {
2570 Flags |= Fail;
2571 return 0;
2572 }
2573 return Buf.st_size;
2574 }
2575 /*}}}*/
2576 // FileFd::ModificationTime - Return the time of last touch /*{{{*/
2577 time_t FileFd::ModificationTime()
2578 {
2579 struct stat Buf;
2580 if (StatFileFd("modification time", iFd, FileName, Buf, d) == false)
2581 {
2582 Flags |= Fail;
2583 return 0;
2584 }
2585 return Buf.st_mtime;
2586 }
2587 /*}}}*/
2588 // FileFd::Size - Return the size of the content in the file /*{{{*/
2589 unsigned long long FileFd::Size()
2590 {
2591 if (d == nullptr)
2592 return false;
2593 return d->InternalSize();
2594 }
2595 /*}}}*/
2596 // FileFd::Close - Close the file if the close flag is set /*{{{*/
2597 // ---------------------------------------------------------------------
2598 /* */
2599 bool FileFd::Close()
2600 {
2601 if (Failed() == false && Flush() == false)
2602 return false;
2603 if (iFd == -1)
2604 return true;
2605
2606 bool Res = true;
2607 if ((Flags & AutoClose) == AutoClose)
2608 {
2609 if ((Flags & Compressed) != Compressed && iFd > 0 && close(iFd) != 0)
2610 Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str());
2611 }
2612
2613 if (d != NULL)
2614 {
2615 Res &= d->InternalClose(FileName);
2616 delete d;
2617 d = NULL;
2618 }
2619
2620 if ((Flags & Replace) == Replace) {
2621 if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
2622 Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str());
2623
2624 FileName = TemporaryFileName; // for the unlink() below.
2625 TemporaryFileName.clear();
2626 }
2627
2628 iFd = -1;
2629
2630 if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
2631 FileName.empty() == false)
2632 Res &= RemoveFile("FileFd::Close", FileName);
2633
2634 if (Res == false)
2635 Flags |= Fail;
2636 return Res;
2637 }
2638 /*}}}*/
2639 // FileFd::Sync - Sync the file /*{{{*/
2640 // ---------------------------------------------------------------------
2641 /* */
2642 bool FileFd::Sync()
2643 {
2644 if (fsync(iFd) != 0)
2645 return FileFdErrno("sync",_("Problem syncing the file"));
2646 return true;
2647 }
2648 /*}}}*/
2649 // FileFd::FileFdErrno - set Fail and call _error->Errno *{{{*/
2650 bool FileFd::FileFdErrno(const char *Function, const char *Description,...)
2651 {
2652 Flags |= Fail;
2653 va_list args;
2654 size_t msgSize = 400;
2655 int const errsv = errno;
2656 while (true)
2657 {
2658 va_start(args,Description);
2659 if (_error->InsertErrno(GlobalError::ERROR, Function, Description, args, errsv, msgSize) == false)
2660 break;
2661 va_end(args);
2662 }
2663 return false;
2664 }
2665 /*}}}*/
2666 // FileFd::FileFdError - set Fail and call _error->Error *{{{*/
2667 bool FileFd::FileFdError(const char *Description,...) {
2668 Flags |= Fail;
2669 va_list args;
2670 size_t msgSize = 400;
2671 while (true)
2672 {
2673 va_start(args,Description);
2674 if (_error->Insert(GlobalError::ERROR, Description, args, msgSize) == false)
2675 break;
2676 va_end(args);
2677 }
2678 return false;
2679 }
2680 /*}}}*/
2681 gzFile FileFd::gzFd() { /*{{{*/
2682 #ifdef HAVE_ZLIB
2683 GzipFileFdPrivate * const gzipd = dynamic_cast<GzipFileFdPrivate*>(d);
2684 if (gzipd == nullptr)
2685 return nullptr;
2686 else
2687 return gzipd->gz;
2688 #else
2689 return nullptr;
2690 #endif
2691 }
2692 /*}}}*/
2693
2694 // Glob - wrapper around "glob()" /*{{{*/
2695 std::vector<std::string> Glob(std::string const &pattern, int flags)
2696 {
2697 std::vector<std::string> result;
2698 glob_t globbuf;
2699 int glob_res;
2700 unsigned int i;
2701
2702 glob_res = glob(pattern.c_str(), flags, NULL, &globbuf);
2703
2704 if (glob_res != 0)
2705 {
2706 if(glob_res != GLOB_NOMATCH) {
2707 _error->Errno("glob", "Problem with glob");
2708 return result;
2709 }
2710 }
2711
2712 // append results
2713 for(i=0;i<globbuf.gl_pathc;i++)
2714 result.push_back(string(globbuf.gl_pathv[i]));
2715
2716 globfree(&globbuf);
2717 return result;
2718 }
2719 /*}}}*/
2720 std::string GetTempDir() /*{{{*/
2721 {
2722 const char *tmpdir = getenv("TMPDIR");
2723
2724 #ifdef P_tmpdir
2725 if (!tmpdir)
2726 tmpdir = P_tmpdir;
2727 #endif
2728
2729 struct stat st;
2730 if (!tmpdir || strlen(tmpdir) == 0 || // tmpdir is set
2731 stat(tmpdir, &st) != 0 || (st.st_mode & S_IFDIR) == 0) // exists and is directory
2732 tmpdir = "/tmp";
2733 else if (geteuid() != 0 && // root can do everything anyway
2734 faccessat(-1, tmpdir, R_OK | W_OK | X_OK, AT_EACCESS | AT_SYMLINK_NOFOLLOW) != 0) // current user has rwx access to directory
2735 tmpdir = "/tmp";
2736
2737 return string(tmpdir);
2738 }
2739 std::string GetTempDir(std::string const &User)
2740 {
2741 // no need/possibility to drop privs
2742 if(getuid() != 0 || User.empty() || User == "root")
2743 return GetTempDir();
2744
2745 struct passwd const * const pw = getpwnam(User.c_str());
2746 if (pw == NULL)
2747 return GetTempDir();
2748
2749 gid_t const old_euid = geteuid();
2750 gid_t const old_egid = getegid();
2751 if (setegid(pw->pw_gid) != 0)
2752 _error->Errno("setegid", "setegid %u failed", pw->pw_gid);
2753 if (seteuid(pw->pw_uid) != 0)
2754 _error->Errno("seteuid", "seteuid %u failed", pw->pw_uid);
2755
2756 std::string const tmp = GetTempDir();
2757
2758 if (seteuid(old_euid) != 0)
2759 _error->Errno("seteuid", "seteuid %u failed", old_euid);
2760 if (setegid(old_egid) != 0)
2761 _error->Errno("setegid", "setegid %u failed", old_egid);
2762
2763 return tmp;
2764 }
2765 /*}}}*/
2766 FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * const TmpFd) /*{{{*/
2767 {
2768 char fn[512];
2769 FileFd * const Fd = TmpFd == NULL ? new FileFd() : TmpFd;
2770
2771 std::string const tempdir = GetTempDir();
2772 snprintf(fn, sizeof(fn), "%s/%s.XXXXXX",
2773 tempdir.c_str(), Prefix.c_str());
2774 int const fd = mkstemp(fn);
2775 if(ImmediateUnlink)
2776 unlink(fn);
2777 if (fd < 0)
2778 {
2779 _error->Errno("GetTempFile",_("Unable to mkstemp %s"), fn);
2780 return NULL;
2781 }
2782 if (!Fd->OpenDescriptor(fd, FileFd::ReadWrite, FileFd::None, true))
2783 {
2784 _error->Errno("GetTempFile",_("Unable to write to %s"),fn);
2785 return NULL;
2786 }
2787 return Fd;
2788 }
2789 /*}}}*/
2790 bool Rename(std::string From, std::string To) /*{{{*/
2791 {
2792 if (rename(From.c_str(),To.c_str()) != 0)
2793 {
2794 _error->Error(_("rename failed, %s (%s -> %s)."),strerror(errno),
2795 From.c_str(),To.c_str());
2796 return false;
2797 }
2798 return true;
2799 }
2800 /*}}}*/
2801 bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode)/*{{{*/
2802 {
2803 int fd;
2804 if (Mode != FileFd::ReadOnly && Mode != FileFd::WriteOnly)
2805 return _error->Error("Popen supports ReadOnly (x)or WriteOnly mode only");
2806
2807 int Pipe[2] = {-1, -1};
2808 if(pipe(Pipe) != 0)
2809 return _error->Errno("pipe", _("Failed to create subprocess IPC"));
2810
2811 std::set<int> keep_fds;
2812 keep_fds.insert(Pipe[0]);
2813 keep_fds.insert(Pipe[1]);
2814 Child = ExecFork(keep_fds);
2815 if(Child < 0)
2816 return _error->Errno("fork", "Failed to fork");
2817 if(Child == 0)
2818 {
2819 if(Mode == FileFd::ReadOnly)
2820 {
2821 close(Pipe[0]);
2822 fd = Pipe[1];
2823 }
2824 else if(Mode == FileFd::WriteOnly)
2825 {
2826 close(Pipe[1]);
2827 fd = Pipe[0];
2828 }
2829
2830 if(Mode == FileFd::ReadOnly)
2831 {
2832 dup2(fd, 1);
2833 dup2(fd, 2);
2834 } else if(Mode == FileFd::WriteOnly)
2835 dup2(fd, 0);
2836
2837 execv(Args[0], (char**)Args);
2838 _exit(100);
2839 }
2840 if(Mode == FileFd::ReadOnly)
2841 {
2842 close(Pipe[1]);
2843 fd = Pipe[0];
2844 }
2845 else if(Mode == FileFd::WriteOnly)
2846 {
2847 close(Pipe[0]);
2848 fd = Pipe[1];
2849 }
2850 else
2851 return _error->Error("Popen supports ReadOnly (x)or WriteOnly mode only");
2852 Fd.OpenDescriptor(fd, Mode, FileFd::None, true);
2853
2854 return true;
2855 }
2856 /*}}}*/
2857 bool DropPrivileges() /*{{{*/
2858 {
2859 if(_config->FindB("Debug::NoDropPrivs", false) == true)
2860 return true;
2861
2862 #if __gnu_linux__
2863 #if defined(PR_SET_NO_NEW_PRIVS) && ( PR_SET_NO_NEW_PRIVS != 38 )
2864 #error "PR_SET_NO_NEW_PRIVS is defined, but with a different value than expected!"
2865 #endif
2866 // see prctl(2), needs linux3.5 at runtime - magic constant to avoid it at buildtime
2867 int ret = prctl(38, 1, 0, 0, 0);
2868 // ignore EINVAL - kernel is too old to understand the option
2869 if(ret < 0 && errno != EINVAL)
2870 _error->Warning("PR_SET_NO_NEW_PRIVS failed with %i", ret);
2871 #endif
2872
2873 // empty setting disables privilege dropping - this also ensures
2874 // backward compatibility, see bug #764506
2875 const std::string toUser = _config->Find("APT::Sandbox::User");
2876 if (toUser.empty() || toUser == "root")
2877 return true;
2878
2879 // a lot can go wrong trying to drop privileges completely,
2880 // so ideally we would like to verify that we have done it –
2881 // but the verify asks for too much in case of fakeroot (and alike)
2882 // [Specific checks can be overridden with dedicated options]
2883 bool const VerifySandboxing = _config->FindB("APT::Sandbox::Verify", false);
2884
2885 // uid will be 0 in the end, but gid might be different anyway
2886 uid_t const old_uid = getuid();
2887 gid_t const old_gid = getgid();
2888
2889 if (old_uid != 0)
2890 return true;
2891
2892 struct passwd *pw = getpwnam(toUser.c_str());
2893 if (pw == NULL)
2894 return _error->Error("No user %s, can not drop rights", toUser.c_str());
2895
2896 // Do not change the order here, it might break things
2897 // Get rid of all our supplementary groups first
2898 if (setgroups(1, &pw->pw_gid))
2899 return _error->Errno("setgroups", "Failed to setgroups");
2900
2901 // Now change the group ids to the new user
2902 #ifdef HAVE_SETRESGID
2903 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
2904 return _error->Errno("setresgid", "Failed to set new group ids");
2905 #else
2906 if (setegid(pw->pw_gid) != 0)
2907 return _error->Errno("setegid", "Failed to setegid");
2908
2909 if (setgid(pw->pw_gid) != 0)
2910 return _error->Errno("setgid", "Failed to setgid");
2911 #endif
2912
2913 // Change the user ids to the new user
2914 #ifdef HAVE_SETRESUID
2915 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0)
2916 return _error->Errno("setresuid", "Failed to set new user ids");
2917 #else
2918 if (setuid(pw->pw_uid) != 0)
2919 return _error->Errno("setuid", "Failed to setuid");
2920 if (seteuid(pw->pw_uid) != 0)
2921 return _error->Errno("seteuid", "Failed to seteuid");
2922 #endif
2923
2924 // disabled by default as fakeroot doesn't implement getgroups currently (#806521)
2925 if (VerifySandboxing == true || _config->FindB("APT::Sandbox::Verify::Groups", false) == true)
2926 {
2927 // Verify that the user isn't still in any supplementary groups
2928 long const ngroups_max = sysconf(_SC_NGROUPS_MAX);
2929 std::unique_ptr<gid_t[]> gidlist(new gid_t[ngroups_max]);
2930 if (unlikely(gidlist == NULL))
2931 return _error->Error("Allocation of a list of size %lu for getgroups failed", ngroups_max);
2932 ssize_t gidlist_nr;
2933 if ((gidlist_nr = getgroups(ngroups_max, gidlist.get())) < 0)
2934 return _error->Errno("getgroups", "Could not get new groups (%lu)", ngroups_max);
2935 for (ssize_t i = 0; i < gidlist_nr; ++i)
2936 if (gidlist[i] != pw->pw_gid)
2937 return _error->Error("Could not switch group, user %s is still in group %d", toUser.c_str(), gidlist[i]);
2938 }
2939
2940 // enabled by default as all fakeroot-lookalikes should fake that accordingly
2941 if (VerifySandboxing == true || _config->FindB("APT::Sandbox::Verify::IDs", true) == true)
2942 {
2943 // Verify that gid, egid, uid, and euid changed
2944 if (getgid() != pw->pw_gid)
2945 return _error->Error("Could not switch group");
2946 if (getegid() != pw->pw_gid)
2947 return _error->Error("Could not switch effective group");
2948 if (getuid() != pw->pw_uid)
2949 return _error->Error("Could not switch user");
2950 if (geteuid() != pw->pw_uid)
2951 return _error->Error("Could not switch effective user");
2952
2953 #ifdef HAVE_GETRESUID
2954 // verify that the saved set-user-id was changed as well
2955 uid_t ruid = 0;
2956 uid_t euid = 0;
2957 uid_t suid = 0;
2958 if (getresuid(&ruid, &euid, &suid))
2959 return _error->Errno("getresuid", "Could not get saved set-user-ID");
2960 if (suid != pw->pw_uid)
2961 return _error->Error("Could not switch saved set-user-ID");
2962 #endif
2963
2964 #ifdef HAVE_GETRESGID
2965 // verify that the saved set-group-id was changed as well
2966 gid_t rgid = 0;
2967 gid_t egid = 0;
2968 gid_t sgid = 0;
2969 if (getresgid(&rgid, &egid, &sgid))
2970 return _error->Errno("getresuid", "Could not get saved set-group-ID");
2971 if (sgid != pw->pw_gid)
2972 return _error->Error("Could not switch saved set-group-ID");
2973 #endif
2974 }
2975
2976 // disabled as fakeroot doesn't forbid (by design) (re)gaining root from unprivileged
2977 if (VerifySandboxing == true || _config->FindB("APT::Sandbox::Verify::Regain", false) == true)
2978 {
2979 // Check that uid and gid changes do not work anymore
2980 if (pw->pw_gid != old_gid && (setgid(old_gid) != -1 || setegid(old_gid) != -1))
2981 return _error->Error("Could restore a gid to root, privilege dropping did not work");
2982
2983 if (pw->pw_uid != old_uid && (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
2984 return _error->Error("Could restore a uid to root, privilege dropping did not work");
2985 }
2986
2987 return true;
2988 }
2989 /*}}}*/