1 /* sysutils.c - system helpers
2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #ifdef WITHOUT_GNU_PTH /* Give the Makefile a chance to build without Pth. */
33 # include <sys/stat.h>
35 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
36 # include <asm/sysinfo.h>
37 # include <asm/unistd.h>
41 # include <sys/time.h>
42 # include <sys/resource.h>
44 #ifdef HAVE_W32_SYSTEM
56 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
57 #warning using trap_unaligned
59 setsysinfo(unsigned long op
, void *buffer
, unsigned long size
,
60 int *start
, void *arg
, unsigned long flag
)
62 return syscall(__NR_osf_setsysinfo
, op
, buffer
, size
, start
, arg
, flag
);
70 buf
[0] = SSIN_UACPROC
;
71 buf
[1] = UAC_SIGBUS
| UAC_NOPRINT
;
72 setsysinfo(SSI_NVPAIRS
, buf
, 1, 0, 0, 0);
83 disable_core_dumps (void)
85 #ifdef HAVE_DOSISH_SYSTEM
88 # ifdef HAVE_SETRLIMIT
91 /* We only set the current limit unless we were not able to
92 retrieve the old value. */
93 if (getrlimit (RLIMIT_CORE
, &limit
))
96 if( !setrlimit (RLIMIT_CORE
, &limit
) )
98 if( errno
!= EINVAL
&& errno
!= ENOSYS
)
99 log_fatal (_("can't disable core dumps: %s\n"), strerror(errno
) );
106 enable_core_dumps (void)
108 #ifdef HAVE_DOSISH_SYSTEM
111 # ifdef HAVE_SETRLIMIT
114 if (getrlimit (RLIMIT_CORE
, &limit
))
116 limit
.rlim_cur
= limit
.rlim_max
;
117 setrlimit (RLIMIT_CORE
, &limit
);
118 return 1; /* We always return true because trhis function is
119 merely a debugging aid. */
127 /* Return a string which is used as a kind of process ID */
129 get_session_marker( size_t *rlen
)
131 static byte marker
[SIZEOF_UNSIGNED_LONG
*2];
132 static int initialized
;
134 if ( !initialized
) {
135 volatile ulong aa
, bb
; /* we really want the uninitialized value */
139 /* Although this marker is guessable it is not easy to use
140 * for a faked control packet because an attacker does not
141 * have enough control about the time the verification does
142 * take place. Of course, we can add just more random but
143 * than we need the random generator even for verification
144 * tasks - which does not make sense. */
145 a
= aa
^ (ulong
)getpid();
146 b
= bb
^ (ulong
)time(NULL
);
147 memcpy( marker
, &a
, SIZEOF_UNSIGNED_LONG
);
148 memcpy( marker
+SIZEOF_UNSIGNED_LONG
, &b
, SIZEOF_UNSIGNED_LONG
);
150 *rlen
= sizeof(marker
);
155 #if 0 /* not yet needed - Note that this will require inclusion of
156 cmacros.am in Makefile.am */
158 check_permissions(const char *path
,int extension
,int checkonly
)
160 #if defined(HAVE_STAT) && !defined(HAVE_DOSISH_SYSTEM)
169 if(extension
&& path
[0]!=DIRSEP_C
)
171 if(strchr(path
,DIRSEP_C
))
172 tmppath
=make_filename(path
,NULL
);
174 tmppath
=make_filename(GNUPG_LIBDIR
,path
,NULL
);
177 tmppath
=m_strdup(path
);
179 /* It's okay if the file doesn't exist */
180 if(stat(tmppath
,&statbuf
)!=0)
186 isdir
=S_ISDIR(statbuf
.st_mode
);
188 /* Per-user files must be owned by the user. Extensions must be
189 owned by the user or root. */
190 if((!extension
&& statbuf
.st_uid
!= getuid()) ||
191 (extension
&& statbuf
.st_uid
!=0 && statbuf
.st_uid
!=getuid()))
194 log_info(_("Warning: unsafe ownership on %s \"%s\"\n"),
195 isdir
?"directory":extension
?"extension":"file",path
);
199 /* This works for both directories and files - basically, we don't
200 care what the owner permissions are, so long as the group and
201 other permissions are 0 for per-user files, and non-writable for
203 if((extension
&& (statbuf
.st_mode
& (S_IWGRP
|S_IWOTH
)) !=0) ||
204 (!extension
&& (statbuf
.st_mode
& (S_IRWXG
|S_IRWXO
)) != 0))
208 /* However, if the directory the directory/file is in is owned
209 by the user and is 700, then this is not a problem.
210 Theoretically, we could walk this test up to the root
211 directory /, but for the sake of sanity, I'm stopping at one
214 dir
= make_dirname (tmppath
);
215 if(stat(dir
,&statbuf
)==0 && statbuf
.st_uid
==getuid() &&
216 S_ISDIR(statbuf
.st_mode
) && (statbuf
.st_mode
& (S_IRWXG
|S_IRWXO
))==0)
226 log_info(_("Warning: unsafe permissions on %s \"%s\"\n"),
227 isdir
?"directory":extension
?"extension":"file",path
);
238 #endif /* HAVE_STAT && !HAVE_DOSISH_SYSTEM */
245 /* Wrapper around the usual sleep fucntion. This one won't wake up
246 before the sleep time has really elapsed. When build with Pth it
247 merely calls pth_sleep and thus suspends only the current
250 gnupg_sleep (unsigned int seconds
)
253 /* With Pth we force a regular sleep for seconds == 0 so that also
254 the process will give up its timeslot. */
257 # ifdef HAVE_W32_SYSTEM
265 /* Fixme: make sure that a sleep won't wake up to early. */
266 # ifdef HAVE_W32_SYSTEM
267 Sleep (seconds
*1000);
275 /* This function is a NOP for POSIX systems but required under Windows
276 as the file handles as returned by OS calls (like CreateFile) are
277 different from the libc file descriptors (like open). This function
278 translates system file handles to libc file handles. FOR_WRITE
279 gives the direction of the handle. */
281 translate_sys2libc_fd (int fd
, int for_write
)
283 #ifdef HAVE_W32_SYSTEM
287 return fd
; /* Do not do this for error, stdin, stdout, stderr.
288 (This also ignores an fd of -1.) */
290 x
= _open_osfhandle (fd
, for_write
? 1 : 0);
292 log_error ("failed to translate osfhandle %p\n", (void *) fd
);
295 /* log_info ("_open_osfhandle %p yields %d%s\n", */
296 /* (void*)fd, x, for_write? " for writing":"" ); */
299 #endif /* HAVE_W32_SYSTEM */