Imported Upstream version 2.1.23
[platform/upstream/gpg2.git] / common / sysutils.c
1 /* sysutils.c -  system helpers
2  * Copyright (C) 1991-2001, 2003-2004,
3  *               2006-2008  Free Software Foundation, Inc.
4  * Copyright (C) 2013-2016 Werner Koch
5  *
6  * This file is part of GnuPG.
7  *
8  * This file is free software; you can redistribute it and/or modify
9  * it under the terms of either
10  *
11  *   - the GNU Lesser General Public License as published by the Free
12  *     Software Foundation; either version 3 of the License, or (at
13  *     your option) any later version.
14  *
15  * or
16  *
17  *   - the GNU General Public License as published by the Free
18  *     Software Foundation; either version 2 of the License, or (at
19  *     your option) any later version.
20  *
21  * or both in parallel, as here.
22  *
23  * This file is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, see <https://www.gnu.org/licenses/>.
30  */
31
32 #include <config.h>
33
34 #ifdef WITHOUT_NPTH /* Give the Makefile a chance to build without Pth.  */
35 # undef HAVE_NPTH
36 # undef USE_NPTH
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdint.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #ifdef HAVE_STAT
46 # include <sys/stat.h>
47 #endif
48 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
49 # include <asm/sysinfo.h>
50 # include <asm/unistd.h>
51 #endif
52 #include <time.h>
53 #ifdef HAVE_SETRLIMIT
54 # include <sys/time.h>
55 # include <sys/resource.h>
56 #endif
57 #ifdef HAVE_W32_SYSTEM
58 # if WINVER < 0x0500
59 #   define WINVER 0x0500  /* Required for AllowSetForegroundWindow.  */
60 # endif
61 # ifdef HAVE_WINSOCK2_H
62 #  include <winsock2.h>
63 # endif
64 # include <windows.h>
65 #else /*!HAVE_W32_SYSTEM*/
66 # include <sys/socket.h>
67 # include <sys/un.h>
68 #endif
69 #ifdef HAVE_INOTIFY_INIT
70 # include <sys/inotify.h>
71 #endif /*HAVE_INOTIFY_INIT*/
72 #ifdef HAVE_NPTH
73 # include <npth.h>
74 #endif
75 #include <fcntl.h>
76
77 #include <assuan.h>
78
79 #include "util.h"
80 #include "i18n.h"
81
82 #include "sysutils.h"
83
84 #define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
85
86 /* Flag to tell whether special file names are enabled.  See gpg.c for
87  * an explanation of these file names.  */
88 static int allow_special_filenames;
89
90
91 static GPGRT_INLINE gpg_error_t
92 my_error_from_syserror (void)
93 {
94   return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
95 }
96
97 static GPGRT_INLINE gpg_error_t
98 my_error (int e)
99 {
100   return gpg_err_make (default_errsource, (e));
101 }
102
103
104
105 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
106 #warning using trap_unaligned
107 static int
108 setsysinfo(unsigned long op, void *buffer, unsigned long size,
109                      int *start, void *arg, unsigned long flag)
110 {
111     return syscall(__NR_osf_setsysinfo, op, buffer, size, start, arg, flag);
112 }
113
114 void
115 trap_unaligned(void)
116 {
117     unsigned int buf[2];
118
119     buf[0] = SSIN_UACPROC;
120     buf[1] = UAC_SIGBUS | UAC_NOPRINT;
121     setsysinfo(SSI_NVPAIRS, buf, 1, 0, 0, 0);
122 }
123 #else
124 void
125 trap_unaligned(void)
126 {  /* dummy */
127 }
128 #endif
129
130
131 int
132 disable_core_dumps (void)
133 {
134 #ifdef HAVE_DOSISH_SYSTEM
135     return 0;
136 #else
137 # ifdef HAVE_SETRLIMIT
138     struct rlimit limit;
139
140     /* We only set the current limit unless we were not able to
141        retrieve the old value. */
142     if (getrlimit (RLIMIT_CORE, &limit))
143       limit.rlim_max = 0;
144     limit.rlim_cur = 0;
145     if( !setrlimit (RLIMIT_CORE, &limit) )
146         return 0;
147     if( errno != EINVAL && errno != ENOSYS )
148         log_fatal (_("can't disable core dumps: %s\n"), strerror(errno) );
149 #endif
150     return 1;
151 #endif
152 }
153
154 int
155 enable_core_dumps (void)
156 {
157 #ifdef HAVE_DOSISH_SYSTEM
158     return 0;
159 #else
160 # ifdef HAVE_SETRLIMIT
161     struct rlimit limit;
162
163     if (getrlimit (RLIMIT_CORE, &limit))
164       return 1;
165     limit.rlim_cur = limit.rlim_max;
166     setrlimit (RLIMIT_CORE, &limit);
167     return 1; /* We always return true because this function is
168                  merely a debugging aid. */
169 # endif
170     return 1;
171 #endif
172 }
173
174
175 /* Allow the use of special "-&nnn" style file names.  */
176 void
177 enable_special_filenames (void)
178 {
179   allow_special_filenames = 1;
180 }
181
182
183 /* Return a string which is used as a kind of process ID.  */
184 const byte *
185 get_session_marker (size_t *rlen)
186 {
187   static byte marker[SIZEOF_UNSIGNED_LONG*2];
188   static int initialized;
189
190   if (!initialized)
191     {
192       gcry_create_nonce (marker, sizeof marker);
193       initialized = 1;
194     }
195   *rlen = sizeof (marker);
196   return marker;
197 }
198
199 /* Return a random number in an unsigned int. */
200 unsigned int
201 get_uint_nonce (void)
202 {
203   unsigned int value;
204
205   gcry_create_nonce (&value, sizeof value);
206   return value;
207 }
208
209
210
211 #if 0 /* not yet needed - Note that this will require inclusion of
212          cmacros.am in Makefile.am */
213 int
214 check_permissions(const char *path,int extension,int checkonly)
215 {
216 #if defined(HAVE_STAT) && !defined(HAVE_DOSISH_SYSTEM)
217   char *tmppath;
218   struct stat statbuf;
219   int ret=1;
220   int isdir=0;
221
222   if(opt.no_perm_warn)
223     return 0;
224
225   if(extension && path[0]!=DIRSEP_C)
226     {
227       if(strchr(path,DIRSEP_C))
228         tmppath=make_filename(path,NULL);
229       else
230         tmppath=make_filename(GNUPG_LIBDIR,path,NULL);
231     }
232   else
233     tmppath=m_strdup(path);
234
235   /* It's okay if the file doesn't exist */
236   if(stat(tmppath,&statbuf)!=0)
237     {
238       ret=0;
239       goto end;
240     }
241
242   isdir=S_ISDIR(statbuf.st_mode);
243
244   /* Per-user files must be owned by the user.  Extensions must be
245      owned by the user or root. */
246   if((!extension && statbuf.st_uid != getuid()) ||
247      (extension && statbuf.st_uid!=0 && statbuf.st_uid!=getuid()))
248     {
249       if(!checkonly)
250         log_info(_("Warning: unsafe ownership on %s \"%s\"\n"),
251                  isdir?"directory":extension?"extension":"file",path);
252       goto end;
253     }
254
255   /* This works for both directories and files - basically, we don't
256      care what the owner permissions are, so long as the group and
257      other permissions are 0 for per-user files, and non-writable for
258      extensions. */
259   if((extension && (statbuf.st_mode & (S_IWGRP|S_IWOTH)) !=0) ||
260      (!extension && (statbuf.st_mode & (S_IRWXG|S_IRWXO)) != 0))
261     {
262       char *dir;
263
264       /* However, if the directory the directory/file is in is owned
265          by the user and is 700, then this is not a problem.
266          Theoretically, we could walk this test up to the root
267          directory /, but for the sake of sanity, I'm stopping at one
268          level down. */
269
270       dir= make_dirname (tmppath);
271       if(stat(dir,&statbuf)==0 && statbuf.st_uid==getuid() &&
272          S_ISDIR(statbuf.st_mode) && (statbuf.st_mode & (S_IRWXG|S_IRWXO))==0)
273         {
274           xfree (dir);
275           ret=0;
276           goto end;
277         }
278
279       m_free(dir);
280
281       if(!checkonly)
282         log_info(_("Warning: unsafe permissions on %s \"%s\"\n"),
283                  isdir?"directory":extension?"extension":"file",path);
284       goto end;
285     }
286
287   ret=0;
288
289  end:
290   m_free(tmppath);
291
292   return ret;
293
294 #endif /* HAVE_STAT && !HAVE_DOSISH_SYSTEM */
295
296   return 0;
297 }
298 #endif
299
300
301 /* Wrapper around the usual sleep function.  This one won't wake up
302    before the sleep time has really elapsed.  When build with Pth it
303    merely calls pth_sleep and thus suspends only the current
304    thread. */
305 void
306 gnupg_sleep (unsigned int seconds)
307 {
308 #ifdef USE_NPTH
309   npth_sleep (seconds);
310 #else
311   /* Fixme:  make sure that a sleep won't wake up to early.  */
312 # ifdef HAVE_W32_SYSTEM
313   Sleep (seconds*1000);
314 # else
315   sleep (seconds);
316 # endif
317 #endif
318 }
319
320
321 /* Wrapper around the platforms usleep function.  This one won't wake
322  * up before the sleep time has really elapsed.  When build with nPth
323  * it merely calls npth_usleep and thus suspends only the current
324  * thread. */
325 void
326 gnupg_usleep (unsigned int usecs)
327 {
328 #if defined(USE_NPTH)
329
330   npth_usleep (usecs);
331
332 #elif defined(HAVE_W32_SYSTEM)
333
334   Sleep ((usecs + 999) / 1000);
335
336 #elif defined(HAVE_NANOSLEEP)
337
338   if (usecs)
339     {
340       struct timespec req;
341       struct timespec rem;
342
343       req.tv_sec = 0;
344       req.tv_nsec = usecs * 1000;
345
346       while (nanosleep (&req, &rem) < 0 && errno == EINTR)
347         req = rem;
348     }
349
350 #else /*Standard Unix*/
351
352   if (usecs)
353     {
354       struct timeval tv;
355
356       tv.tv_sec  = usecs / 1000000;
357       tv.tv_usec = usecs % 1000000;
358       select (0, NULL, NULL, NULL, &tv);
359     }
360
361 #endif
362 }
363
364
365 /* This function is a NOP for POSIX systems but required under Windows
366    as the file handles as returned by OS calls (like CreateFile) are
367    different from the libc file descriptors (like open). This function
368    translates system file handles to libc file handles.  FOR_WRITE
369    gives the direction of the handle.  */
370 int
371 translate_sys2libc_fd (gnupg_fd_t fd, int for_write)
372 {
373 #if defined(HAVE_W32CE_SYSTEM)
374   (void)for_write;
375   return (int) fd;
376 #elif defined(HAVE_W32_SYSTEM)
377   int x;
378
379   if (fd == GNUPG_INVALID_FD)
380     return -1;
381
382   /* Note that _open_osfhandle is currently defined to take and return
383      a long.  */
384   x = _open_osfhandle ((long)fd, for_write ? 1 : 0);
385   if (x == -1)
386     log_error ("failed to translate osfhandle %p\n", (void *) fd);
387   return x;
388 #else /*!HAVE_W32_SYSTEM */
389   (void)for_write;
390   return fd;
391 #endif
392 }
393
394 /* This is the same as translate_sys2libc_fd but takes an integer
395    which is assumed to be such an system handle.  On WindowsCE the
396    passed FD is a rendezvous ID and the function finishes the pipe
397    creation. */
398 int
399 translate_sys2libc_fd_int (int fd, int for_write)
400 {
401 #if HAVE_W32CE_SYSTEM
402   fd = (int) _assuan_w32ce_finish_pipe (fd, for_write);
403   return translate_sys2libc_fd ((void*)fd, for_write);
404 #elif HAVE_W32_SYSTEM
405   if (fd <= 2)
406     return fd;  /* Do not do this for error, stdin, stdout, stderr. */
407
408   return translate_sys2libc_fd ((void*)fd, for_write);
409 #else
410   (void)for_write;
411   return fd;
412 #endif
413 }
414
415
416 /* Check whether FNAME has the form "-&nnnn", where N is a non-zero
417  * number.  Returns this number or -1 if it is not the case.  If the
418  * caller wants to use the file descriptor for writing FOR_WRITE shall
419  * be set to 1.  If NOTRANSLATE is set the Windows specific mapping is
420  * not done. */
421 int
422 check_special_filename (const char *fname, int for_write, int notranslate)
423 {
424   if (allow_special_filenames
425       && fname && *fname == '-' && fname[1] == '&')
426     {
427       int i;
428
429       fname += 2;
430       for (i=0; digitp (fname+i); i++ )
431         ;
432       if (!fname[i])
433         return notranslate? atoi (fname)
434           /**/            : translate_sys2libc_fd_int (atoi (fname), for_write);
435     }
436   return -1;
437 }
438
439
440 /* Replacement for tmpfile().  This is required because the tmpfile
441    function of Windows' runtime library is broken, insecure, ignores
442    TMPDIR and so on.  In addition we create a file with an inheritable
443    handle.  */
444 FILE *
445 gnupg_tmpfile (void)
446 {
447 #ifdef HAVE_W32_SYSTEM
448   int attempts, n;
449 #ifdef HAVE_W32CE_SYSTEM
450   wchar_t buffer[MAX_PATH+7+12+1];
451 # define mystrlen(a) wcslen (a)
452   wchar_t *name, *p;
453 #else
454   char buffer[MAX_PATH+7+12+1];
455 # define mystrlen(a) strlen (a)
456   char *name, *p;
457 #endif
458   HANDLE file;
459   int pid = GetCurrentProcessId ();
460   unsigned int value;
461   int i;
462   SECURITY_ATTRIBUTES sec_attr;
463
464   memset (&sec_attr, 0, sizeof sec_attr );
465   sec_attr.nLength = sizeof sec_attr;
466   sec_attr.bInheritHandle = TRUE;
467
468   n = GetTempPath (MAX_PATH+1, buffer);
469   if (!n || n > MAX_PATH || mystrlen (buffer) > MAX_PATH)
470     {
471       gpg_err_set_errno (ENOENT);
472       return NULL;
473     }
474   p = buffer + mystrlen (buffer);
475 #ifdef HAVE_W32CE_SYSTEM
476   wcscpy (p, L"_gnupg");
477   p += 7;
478 #else
479   p = stpcpy (p, "_gnupg");
480 #endif
481   /* We try to create the directory but don't care about an error as
482      it may already exist and the CreateFile would throw an error
483      anyway.  */
484   CreateDirectory (buffer, NULL);
485   *p++ = '\\';
486   name = p;
487   for (attempts=0; attempts < 10; attempts++)
488     {
489       p = name;
490       value = (GetTickCount () ^ ((pid<<16) & 0xffff0000));
491       for (i=0; i < 8; i++)
492         {
493           *p++ = tohex (((value >> 28) & 0x0f));
494           value <<= 4;
495         }
496 #ifdef HAVE_W32CE_SYSTEM
497       wcscpy (p, L".tmp");
498 #else
499       strcpy (p, ".tmp");
500 #endif
501       file = CreateFile (buffer,
502                          GENERIC_READ | GENERIC_WRITE,
503                          0,
504                          &sec_attr,
505                          CREATE_NEW,
506                          FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
507                          NULL);
508       if (file != INVALID_HANDLE_VALUE)
509         {
510           FILE *fp;
511 #ifdef HAVE_W32CE_SYSTEM
512           int fd = (int)file;
513           fp = _wfdopen (fd, L"w+b");
514 #else
515           int fd = _open_osfhandle ((long)file, 0);
516           if (fd == -1)
517             {
518               CloseHandle (file);
519               return NULL;
520             }
521           fp = fdopen (fd, "w+b");
522 #endif
523           if (!fp)
524             {
525               int save = errno;
526               close (fd);
527               gpg_err_set_errno (save);
528               return NULL;
529             }
530           return fp;
531         }
532       Sleep (1); /* One ms as this is the granularity of GetTickCount.  */
533     }
534   gpg_err_set_errno (ENOENT);
535   return NULL;
536 #undef mystrlen
537 #else /*!HAVE_W32_SYSTEM*/
538   return tmpfile ();
539 #endif /*!HAVE_W32_SYSTEM*/
540 }
541
542
543 /* Make sure that the standard file descriptors are opened. Obviously
544    some folks close them before an exec and the next file we open will
545    get one of them assigned and thus any output (i.e. diagnostics) end
546    up in that file (e.g. the trustdb).  Not actually a gpg problem as
547    this will happen with almost all utilities when called in a wrong
548    way.  However we try to minimize the damage here and raise
549    awareness of the problem.
550
551    Must be called before we open any files! */
552 void
553 gnupg_reopen_std (const char *pgmname)
554 {
555 #if defined(HAVE_STAT) && !defined(HAVE_W32_SYSTEM)
556   struct stat statbuf;
557   int did_stdin = 0;
558   int did_stdout = 0;
559   int did_stderr = 0;
560   FILE *complain;
561
562   if (fstat (STDIN_FILENO, &statbuf) == -1 && errno ==EBADF)
563     {
564       if (open ("/dev/null",O_RDONLY) == STDIN_FILENO)
565         did_stdin = 1;
566       else
567         did_stdin = 2;
568     }
569
570   if (fstat (STDOUT_FILENO, &statbuf) == -1 && errno == EBADF)
571     {
572       if (open ("/dev/null",O_WRONLY) == STDOUT_FILENO)
573         did_stdout = 1;
574       else
575         did_stdout = 2;
576     }
577
578   if (fstat (STDERR_FILENO, &statbuf)==-1 && errno==EBADF)
579     {
580       if (open ("/dev/null", O_WRONLY) == STDERR_FILENO)
581         did_stderr = 1;
582       else
583         did_stderr = 2;
584     }
585
586   /* It's hard to log this sort of thing since the filehandle we would
587      complain to may be closed... */
588   if (!did_stderr)
589     complain = stderr;
590   else if (!did_stdout)
591     complain = stdout;
592   else
593     complain = NULL;
594
595   if (complain)
596     {
597       if (did_stdin == 1)
598         fprintf (complain, "%s: WARNING: standard input reopened\n", pgmname);
599       if (did_stdout == 1)
600         fprintf (complain, "%s: WARNING: standard output reopened\n", pgmname);
601       if (did_stderr == 1)
602         fprintf (complain, "%s: WARNING: standard error reopened\n", pgmname);
603
604       if (did_stdin == 2 || did_stdout == 2 || did_stderr == 2)
605         fprintf(complain,"%s: fatal: unable to reopen standard input,"
606                 " output, or error\n", pgmname);
607     }
608
609   if (did_stdin == 2 || did_stdout == 2 || did_stderr == 2)
610     exit (3);
611 #else /* !(HAVE_STAT && !HAVE_W32_SYSTEM) */
612   (void)pgmname;
613 #endif
614 }
615
616
617 /* Hack required for Windows.  */
618 void
619 gnupg_allow_set_foregound_window (pid_t pid)
620 {
621   if (!pid)
622     log_info ("%s called with invalid pid %lu\n",
623               "gnupg_allow_set_foregound_window", (unsigned long)pid);
624 #if defined(HAVE_W32_SYSTEM) && !defined(HAVE_W32CE_SYSTEM)
625   else if (!AllowSetForegroundWindow ((pid_t)pid == (pid_t)(-1)?ASFW_ANY:pid))
626     log_info ("AllowSetForegroundWindow(%lu) failed: %s\n",
627                (unsigned long)pid, w32_strerror (-1));
628 #endif
629 }
630
631 int
632 gnupg_remove (const char *fname)
633 {
634 #ifdef HAVE_W32CE_SYSTEM
635   int rc;
636   wchar_t *wfname;
637
638   wfname = utf8_to_wchar (fname);
639   if (!wfname)
640     rc = 0;
641   else
642     {
643       rc = DeleteFile (wfname);
644       xfree (wfname);
645     }
646   if (!rc)
647     return -1; /* ERRNO is automagically provided by gpg-error.h.  */
648   return 0;
649 #else
650   return remove (fname);
651 #endif
652 }
653
654
655 /* Wrapper for rename(2) to handle Windows peculiarities.  If
656  * BLOCK_SIGNALS is not NULL and points to a variable set to true, all
657  * signals will be blocked by calling gnupg_block_all_signals; the
658  * caller needs to call gnupg_unblock_all_signals if that variable is
659  * still set to true on return. */
660 gpg_error_t
661 gnupg_rename_file (const char *oldname, const char *newname, int *block_signals)
662 {
663   gpg_error_t err = 0;
664
665   if (block_signals && *block_signals)
666     gnupg_block_all_signals ();
667
668 #ifdef HAVE_DOSISH_SYSTEM
669   {
670     int wtime = 0;
671
672     gnupg_remove (newname);
673   again:
674     if (rename (oldname, newname))
675       {
676         if (GetLastError () == ERROR_SHARING_VIOLATION)
677           {
678             /* Another process has the file open.  We do not use a
679              * lock for read but instead we wait until the other
680              * process has closed the file.  This may take long but
681              * that would also be the case with a dotlock approach for
682              * read and write.  Note that we don't need this on Unix
683              * due to the inode concept.
684              *
685              * So let's wait until the rename has worked.  The retry
686              * intervals are 50, 100, 200, 400, 800, 50ms, ...  */
687             if (!wtime || wtime >= 800)
688               wtime = 50;
689             else
690               wtime *= 2;
691
692             if (wtime >= 800)
693               log_info (_("waiting for file '%s' to become accessible ...\n"),
694                         oldname);
695
696             Sleep (wtime);
697             goto again;
698           }
699         err = my_error_from_syserror ();
700       }
701   }
702 #else /* Unix */
703   {
704 #ifdef __riscos__
705     gnupg_remove (newname);
706 #endif
707     if (rename (oldname, newname) )
708       err = my_error_from_syserror ();
709   }
710 #endif /* Unix */
711
712   if (block_signals && *block_signals && err)
713     {
714       gnupg_unblock_all_signals ();
715       *block_signals = 0;
716     }
717
718   if (err)
719     log_error (_("renaming '%s' to '%s' failed: %s\n"),
720                oldname, newname, gpg_strerror (err));
721   return err;
722 }
723
724
725 #ifndef HAVE_W32_SYSTEM
726 static mode_t
727 modestr_to_mode (const char *modestr)
728 {
729   mode_t mode = 0;
730
731   if (modestr && *modestr)
732     {
733       modestr++;
734       if (*modestr && *modestr++ == 'r')
735         mode |= S_IRUSR;
736       if (*modestr && *modestr++ == 'w')
737         mode |= S_IWUSR;
738       if (*modestr && *modestr++ == 'x')
739         mode |= S_IXUSR;
740       if (*modestr && *modestr++ == 'r')
741         mode |= S_IRGRP;
742       if (*modestr && *modestr++ == 'w')
743         mode |= S_IWGRP;
744       if (*modestr && *modestr++ == 'x')
745         mode |= S_IXGRP;
746       if (*modestr && *modestr++ == 'r')
747         mode |= S_IROTH;
748       if (*modestr && *modestr++ == 'w')
749         mode |= S_IWOTH;
750       if (*modestr && *modestr++ == 'x')
751         mode |= S_IXOTH;
752     }
753
754   return mode;
755 }
756 #endif
757
758
759 /* A wrapper around mkdir which takes a string for the mode argument.
760    This makes it easier to handle the mode argument which is not
761    defined on all systems.  The format of the modestring is
762
763       "-rwxrwxrwx"
764
765    '-' is a don't care or not set.  'r', 'w', 'x' are read allowed,
766    write allowed, execution allowed with the first group for the user,
767    the second for the group and the third for all others.  If the
768    string is shorter than above the missing mode characters are meant
769    to be not set.  */
770 int
771 gnupg_mkdir (const char *name, const char *modestr)
772 {
773 #ifdef HAVE_W32CE_SYSTEM
774   wchar_t *wname;
775   (void)modestr;
776
777   wname = utf8_to_wchar (name);
778   if (!wname)
779     return -1;
780   if (!CreateDirectoryW (wname, NULL))
781     {
782       xfree (wname);
783       return -1;  /* ERRNO is automagically provided by gpg-error.h.  */
784     }
785   xfree (wname);
786   return 0;
787 #elif MKDIR_TAKES_ONE_ARG
788   (void)modestr;
789   /* Note: In the case of W32 we better use CreateDirectory and try to
790      set appropriate permissions.  However using mkdir is easier
791      because this sets ERRNO.  */
792   return mkdir (name);
793 #else
794   return mkdir (name, modestr_to_mode (modestr));
795 #endif
796 }
797
798
799 /* A simple wrapper around chdir.  NAME is expected to be utf8
800  * encoded.  */
801 int
802 gnupg_chdir (const char *name)
803 {
804   return chdir (name);
805 }
806
807
808 /* A wrapper around chmod which takes a string for the mode argument.
809    This makes it easier to handle the mode argument which is not
810    defined on all systems.  The format of the modestring is the same
811    as for gnupg_mkdir.  */
812 int
813 gnupg_chmod (const char *name, const char *modestr)
814 {
815 #ifdef HAVE_W32_SYSTEM
816   (void)name;
817   (void)modestr;
818   return 0;
819 #else
820   return chmod (name, modestr_to_mode (modestr));
821 #endif
822 }
823
824
825 /* Our version of mkdtemp.  The API is identical to POSIX.1-2008
826    version.  We do not use a system provided mkdtemp because we have a
827    good RNG instantly available and this way we don't have diverging
828    versions.  */
829 char *
830 gnupg_mkdtemp (char *tmpl)
831 {
832   /* A lower bound on the number of temporary files to attempt to
833      generate.  The maximum total number of temporary file names that
834      can exist for a given template is 62**6 (5*36**3 for Windows).
835      It should never be necessary to try all these combinations.
836      Instead if a reasonable number of names is tried (we define
837      reasonable as 62**3 or 5*36**3) fail to give the system
838      administrator the chance to remove the problems.  */
839 #ifdef HAVE_W32_SYSTEM
840   static const char letters[] =
841     "abcdefghijklmnopqrstuvwxyz0123456789";
842 # define NUMBER_OF_LETTERS 36
843 # define ATTEMPTS_MIN (5 * 36 * 36 * 36)
844 #else
845   static const char letters[] =
846     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
847 # define NUMBER_OF_LETTERS 62
848 # define ATTEMPTS_MIN (62 * 62 * 62)
849 #endif
850   int len;
851   char *XXXXXX;
852   uint64_t value;
853   unsigned int count;
854   int save_errno = errno;
855   /* The number of times to attempt to generate a temporary file.  To
856      conform to POSIX, this must be no smaller than TMP_MAX.  */
857 #if ATTEMPTS_MIN < TMP_MAX
858   unsigned int attempts = TMP_MAX;
859 #else
860   unsigned int attempts = ATTEMPTS_MIN;
861 #endif
862
863   len = strlen (tmpl);
864   if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
865     {
866       gpg_err_set_errno (EINVAL);
867       return NULL;
868     }
869
870   /* This is where the Xs start.  */
871   XXXXXX = &tmpl[len - 6];
872
873   /* Get a random start value.  */
874   gcry_create_nonce (&value, sizeof value);
875
876   /* Loop until a directory was created.  */
877   for (count = 0; count < attempts; value += 7777, ++count)
878     {
879       uint64_t v = value;
880
881       /* Fill in the random bits.  */
882       XXXXXX[0] = letters[v % NUMBER_OF_LETTERS];
883       v /= NUMBER_OF_LETTERS;
884       XXXXXX[1] = letters[v % NUMBER_OF_LETTERS];
885       v /= NUMBER_OF_LETTERS;
886       XXXXXX[2] = letters[v % NUMBER_OF_LETTERS];
887       v /= NUMBER_OF_LETTERS;
888       XXXXXX[3] = letters[v % NUMBER_OF_LETTERS];
889       v /= NUMBER_OF_LETTERS;
890       XXXXXX[4] = letters[v % NUMBER_OF_LETTERS];
891       v /= NUMBER_OF_LETTERS;
892       XXXXXX[5] = letters[v % NUMBER_OF_LETTERS];
893
894       if (!gnupg_mkdir (tmpl, "-rwx"))
895         {
896           gpg_err_set_errno (save_errno);
897           return tmpl;
898         }
899       if (errno != EEXIST)
900         return NULL;
901     }
902
903   /* We got out of the loop because we ran out of combinations to try.  */
904   gpg_err_set_errno (EEXIST);
905   return NULL;
906 }
907
908
909 int
910 gnupg_setenv (const char *name, const char *value, int overwrite)
911 {
912 #ifdef HAVE_W32CE_SYSTEM
913   (void)name;
914   (void)value;
915   (void)overwrite;
916   return 0;
917 #else /*!W32CE*/
918 # ifdef HAVE_W32_SYSTEM
919   /*  Windows maintains (at least) two sets of environment variables.
920       One set can be accessed by GetEnvironmentVariable and
921       SetEnvironmentVariable.  This set is inherited by the children.
922       The other set is maintained in the C runtime, and is accessed
923       using getenv and putenv.  We try to keep them in sync by
924       modifying both sets.  */
925   {
926     int exists;
927     char tmpbuf[10];
928     exists = GetEnvironmentVariable (name, tmpbuf, sizeof tmpbuf);
929
930     if ((! exists || overwrite) && !SetEnvironmentVariable (name, value))
931       {
932         gpg_err_set_errno (EINVAL); /* (Might also be ENOMEM.) */
933         return -1;
934       }
935   }
936 # endif /*W32*/
937
938 # ifdef HAVE_SETENV
939   return setenv (name, value, overwrite);
940 # else /*!HAVE_SETENV*/
941   if (! getenv (name) || overwrite)
942     {
943       char *buf;
944
945       (void)overwrite;
946       if (!name || !value)
947         {
948           gpg_err_set_errno (EINVAL);
949           return -1;
950         }
951       buf = strconcat (name, "=", value, NULL);
952       if (!buf)
953         return -1;
954 # if __GNUC__
955 #  warning no setenv - using putenv but leaking memory.
956 # endif
957       return putenv (buf);
958     }
959   return 0;
960 # endif /*!HAVE_SETENV*/
961 #endif /*!W32CE*/
962 }
963
964
965 int
966 gnupg_unsetenv (const char *name)
967 {
968 #ifdef HAVE_W32CE_SYSTEM
969   (void)name;
970   return 0;
971 #else /*!W32CE*/
972 # ifdef HAVE_W32_SYSTEM
973   /*  Windows maintains (at least) two sets of environment variables.
974       One set can be accessed by GetEnvironmentVariable and
975       SetEnvironmentVariable.  This set is inherited by the children.
976       The other set is maintained in the C runtime, and is accessed
977       using getenv and putenv.  We try to keep them in sync by
978       modifying both sets.  */
979   if (!SetEnvironmentVariable (name, NULL))
980     {
981       gpg_err_set_errno (EINVAL); /* (Might also be ENOMEM.) */
982       return -1;
983     }
984 # endif /*W32*/
985
986 # ifdef HAVE_UNSETENV
987   return unsetenv (name);
988 # else /*!HAVE_UNSETENV*/
989   {
990     char *buf;
991
992     if (!name)
993       {
994         gpg_err_set_errno (EINVAL);
995         return -1;
996       }
997     buf = xtrystrdup (name);
998     if (!buf)
999       return -1;
1000 #  if __GNUC__
1001 #   warning no unsetenv - trying putenv but leaking memory.
1002 #  endif
1003     return putenv (buf);
1004   }
1005 # endif /*!HAVE_UNSETENV*/
1006 #endif /*!W32CE*/
1007 }
1008
1009
1010 /* Return the current working directory as a malloced string.  Return
1011    NULL and sets ERRNo on error.  */
1012 char *
1013 gnupg_getcwd (void)
1014 {
1015   char *buffer;
1016   size_t size = 100;
1017
1018   for (;;)
1019     {
1020       buffer = xtrymalloc (size+1);
1021       if (!buffer)
1022         return NULL;
1023 #ifdef HAVE_W32CE_SYSTEM
1024       strcpy (buffer, "/");  /* Always "/".  */
1025       return buffer;
1026 #else
1027       if (getcwd (buffer, size) == buffer)
1028         return buffer;
1029       xfree (buffer);
1030       if (errno != ERANGE)
1031         return NULL;
1032       size *= 2;
1033 #endif
1034     }
1035 }
1036
1037
1038 \f
1039 #ifdef HAVE_W32CE_SYSTEM
1040 /* There is a isatty function declaration in cegcc but it does not
1041    make sense, thus we redefine it.  */
1042 int
1043 _gnupg_isatty (int fd)
1044 {
1045   (void)fd;
1046   return 0;
1047 }
1048 #endif
1049
1050
1051 #ifdef HAVE_W32CE_SYSTEM
1052 /* Replacement for getenv which takes care of the our use of getenv.
1053    The code is not thread safe but we expect it to work in all cases
1054    because it is called for the first time early enough.  */
1055 char *
1056 _gnupg_getenv (const char *name)
1057 {
1058   static int initialized;
1059   static char *assuan_debug;
1060
1061   if (!initialized)
1062     {
1063       assuan_debug = read_w32_registry_string (NULL,
1064                                                "\\Software\\GNU\\libassuan",
1065                                                "debug");
1066       initialized = 1;
1067     }
1068
1069   if (!strcmp (name, "ASSUAN_DEBUG"))
1070     return assuan_debug;
1071   else
1072     return NULL;
1073 }
1074
1075 #endif /*HAVE_W32CE_SYSTEM*/
1076
1077
1078 #ifdef HAVE_W32_SYSTEM
1079 /* Return the user's security identifier from the current process.  */
1080 PSID
1081 w32_get_user_sid (void)
1082 {
1083   int okay = 0;
1084   HANDLE proc = NULL;
1085   HANDLE token = NULL;
1086   TOKEN_USER *user = NULL;
1087   PSID sid = NULL;
1088   DWORD tokenlen, sidlen;
1089
1090   proc = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1091   if (!proc)
1092     goto leave;
1093
1094   if (!OpenProcessToken (proc, TOKEN_QUERY, &token))
1095     goto leave;
1096
1097   if (!GetTokenInformation (token, TokenUser, NULL, 0, &tokenlen)
1098       && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1099     goto leave;
1100
1101   user = xtrymalloc (tokenlen);
1102   if (!user)
1103     goto leave;
1104
1105   if (!GetTokenInformation (token, TokenUser, user, tokenlen, &tokenlen))
1106     goto leave;
1107   if (!IsValidSid (user->User.Sid))
1108     goto leave;
1109   sidlen = GetLengthSid (user->User.Sid);
1110   sid = xtrymalloc (sidlen);
1111   if (!sid)
1112     goto leave;
1113   if (!CopySid (sidlen, sid, user->User.Sid))
1114     goto leave;
1115   okay = 1;
1116
1117  leave:
1118   xfree (user);
1119   if (token)
1120     CloseHandle (token);
1121   if (proc)
1122     CloseHandle (proc);
1123
1124   if (!okay)
1125     {
1126       xfree (sid);
1127       sid = NULL;
1128     }
1129   return sid;
1130 }
1131 #endif /*HAVE_W32_SYSTEM*/
1132
1133
1134 \f
1135 /* Support for inotify under Linux.  */
1136
1137 /* Store a new inotify file handle for FNAME at R_FD or return an
1138  * error code.  This file descriptor watch the removal of FNAME. */
1139 gpg_error_t
1140 gnupg_inotify_watch_delete_self (int *r_fd, const char *fname)
1141 {
1142 #if HAVE_INOTIFY_INIT
1143   gpg_error_t err;
1144   int fd;
1145
1146   *r_fd = -1;
1147
1148   if (!fname)
1149     return my_error (GPG_ERR_INV_VALUE);
1150
1151   fd = inotify_init ();
1152   if (fd == -1)
1153     return my_error_from_syserror ();
1154
1155   if (inotify_add_watch (fd, fname, IN_DELETE_SELF) == -1)
1156     {
1157       err = my_error_from_syserror ();
1158       close (fd);
1159       return err;
1160     }
1161
1162   *r_fd = fd;
1163   return 0;
1164 #else /*!HAVE_INOTIFY_INIT*/
1165
1166   (void)fname;
1167   *r_fd = -1;
1168   return my_error (GPG_ERR_NOT_SUPPORTED);
1169
1170 #endif /*!HAVE_INOTIFY_INIT*/
1171 }
1172
1173
1174 /* Store a new inotify file handle for SOCKET_NAME at R_FD or return
1175  * an error code. */
1176 gpg_error_t
1177 gnupg_inotify_watch_socket (int *r_fd, const char *socket_name)
1178 {
1179 #if HAVE_INOTIFY_INIT
1180   gpg_error_t err;
1181   char *fname;
1182   int fd;
1183   char *p;
1184
1185   *r_fd = -1;
1186
1187   if (!socket_name)
1188     return my_error (GPG_ERR_INV_VALUE);
1189
1190   fname = xtrystrdup (socket_name);
1191   if (!fname)
1192     return my_error_from_syserror ();
1193
1194   fd = inotify_init ();
1195   if (fd == -1)
1196     {
1197       err = my_error_from_syserror ();
1198       xfree (fname);
1199       return err;
1200     }
1201
1202   /* We need to watch the directory for the file because there won't
1203    * be an IN_DELETE_SELF for a socket file.  To handle a removal of
1204    * the directory we also watch the directory itself. */
1205   p = strrchr (fname, '/');
1206   if (p)
1207     *p = 0;
1208   if (inotify_add_watch (fd, fname,
1209                          (IN_DELETE|IN_DELETE_SELF|IN_EXCL_UNLINK)) == -1)
1210     {
1211       err = my_error_from_syserror ();
1212       close (fd);
1213       xfree (fname);
1214       return err;
1215     }
1216
1217   xfree (fname);
1218
1219   *r_fd = fd;
1220   return 0;
1221 #else /*!HAVE_INOTIFY_INIT*/
1222
1223   (void)socket_name;
1224   *r_fd = -1;
1225   return my_error (GPG_ERR_NOT_SUPPORTED);
1226
1227 #endif /*!HAVE_INOTIFY_INIT*/
1228 }
1229
1230
1231 /* Read an inotify event and return true if it matches NAME or if it
1232  * sees an IN_DELETE_SELF event for the directory of NAME.  */
1233 int
1234 gnupg_inotify_has_name (int fd, const char *name)
1235 {
1236 #if USE_NPTH && HAVE_INOTIFY_INIT
1237 #define BUFSIZE_FOR_INOTIFY (sizeof (struct inotify_event) + 255 + 1)
1238   union {
1239     struct inotify_event ev;
1240     char _buf[sizeof (struct inotify_event) + 255 + 1];
1241   } buf;
1242   struct inotify_event *evp;
1243   int n;
1244
1245   n = npth_read (fd, &buf, sizeof buf);
1246   /* log_debug ("notify read: n=%d\n", n); */
1247   evp = &buf.ev;
1248   while (n >= sizeof (struct inotify_event))
1249     {
1250       /* log_debug ("             mask=%x len=%u name=(%s)\n", */
1251       /*        evp->mask, (unsigned int)evp->len, evp->len? evp->name:""); */
1252       if ((evp->mask & IN_UNMOUNT))
1253         {
1254           /* log_debug ("             found (dir unmounted)\n"); */
1255           return 3; /* Directory was unmounted.  */
1256         }
1257       if ((evp->mask & IN_DELETE_SELF))
1258         {
1259           /* log_debug ("             found (dir removed)\n"); */
1260           return 2; /* Directory was removed.  */
1261         }
1262       if ((evp->mask & IN_DELETE))
1263         {
1264           if (evp->len >= strlen (name) && !strcmp (evp->name, name))
1265             {
1266               /* log_debug ("             found (file removed)\n"); */
1267               return 1; /* File was removed.  */
1268             }
1269         }
1270       n -= sizeof (*evp) + evp->len;
1271       evp = (struct inotify_event *)(void *)
1272         ((char *)evp + sizeof (*evp) + evp->len);
1273     }
1274
1275 #else /*!(USE_NPTH && HAVE_INOTIFY_INIT)*/
1276
1277   (void)fd;
1278   (void)name;
1279
1280 #endif  /*!(USE_NPTH && HAVE_INOTIFY_INIT)*/
1281
1282   return 0; /* Not found.  */
1283 }
1284
1285
1286 /* Return a malloc'ed string that is the path to the passed
1287  * unix-domain socket (or return NULL if this is not a valid
1288  * unix-domain socket).  We use a plain int here because it is only
1289  * used on Linux.
1290  *
1291  * FIXME: This function needs to be moved to libassuan.  */
1292 #ifndef HAVE_W32_SYSTEM
1293 char *
1294 gnupg_get_socket_name (int fd)
1295 {
1296   struct sockaddr_un un;
1297   socklen_t len = sizeof(un);
1298   char *name = NULL;
1299
1300   if (getsockname (fd, (struct sockaddr*)&un, &len) != 0)
1301     log_error ("could not getsockname(%d): %s\n", fd,
1302                gpg_strerror (my_error_from_syserror ()));
1303   else if (un.sun_family != AF_UNIX)
1304     log_error ("file descriptor %d is not a unix-domain socket\n", fd);
1305   else if (len <= offsetof (struct sockaddr_un, sun_path))
1306     log_error ("socket name not present for file descriptor %d\n", fd);
1307   else if (len > sizeof(un))
1308     log_error ("socket name for file descriptor %d was truncated "
1309                "(passed %zu bytes, wanted %u)\n", fd, sizeof(un), len);
1310   else
1311     {
1312       size_t namelen = len - offsetof (struct sockaddr_un, sun_path);
1313
1314       /* log_debug ("file descriptor %d has path %s (%zu octets)\n", fd, */
1315       /*            un.sun_path, namelen); */
1316       name = xtrymalloc (namelen + 1);
1317       if (!name)
1318         log_error ("failed to allocate memory for name of fd %d: %s\n",
1319                    fd, gpg_strerror (my_error_from_syserror ()));
1320       else
1321         {
1322           memcpy (name, un.sun_path, namelen);
1323           name[namelen] = 0;
1324         }
1325     }
1326
1327   return name;
1328 }
1329 #endif /*!HAVE_W32_SYSTEM*/
1330
1331 /* Check whether FD is valid.  */
1332 int
1333 gnupg_fd_valid (int fd)
1334 {
1335   int d = dup (fd);
1336   if (d < 0)
1337     return 0;
1338   close (d);
1339   return 1;
1340 }