1 /* exec.c - generic call-a-program code
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005 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 <https://www.gnu.org/licenses/>.
21 FIXME: We should replace most code in this module by our
22 spawn implementation from common/exechelp.c.
31 #include <sys/types.h>
32 #ifndef EXEC_TEMPFILE_ONLY
35 #ifdef HAVE_DOSISH_SYSTEM
36 # ifdef HAVE_WINSOCK2_H
37 # include <winsock2.h>
57 exec_write(struct exec_info **info,const char *program,
58 const char *args_in,const char *name,int writeonly,int binary)
60 log_error(_("no remote program execution supported\n"));
61 return GPG_ERR_GENERAL;
65 exec_read(struct exec_info *info) { return GPG_ERR_GENERAL; }
67 exec_finish(struct exec_info *info) { return GPG_ERR_GENERAL; }
69 set_exec_path(const char *path) { return GPG_ERR_GENERAL; }
74 /* This is a nicer system() for windows that waits for programs to
75 return before returning control to the caller. I hate helpful
78 w32_system(const char *command)
80 #ifdef HAVE_W32CE_SYSTEM
81 #warning Change this code to use common/exechelp.c
83 PROCESS_INFORMATION pi;
87 /* We must use a copy of the command as CreateProcess modifies this
89 string=xstrdup(command);
91 memset(&pi,0,sizeof(pi));
92 memset(&si,0,sizeof(si));
95 if(!CreateProcess(NULL,string,NULL,NULL,FALSE,
100 /* Wait for the child to exit */
101 WaitForSingleObject(pi.hProcess,INFINITE);
103 CloseHandle(pi.hProcess);
104 CloseHandle(pi.hThread);
112 /* Replaces current $PATH */
114 set_exec_path(const char *path)
116 #ifdef HAVE_W32CE_SYSTEM
117 #warning Change this code to use common/exechelp.c
121 p=xmalloc(5+strlen(path)+1);
126 log_debug("set_exec_path: %s\n",p);
128 /* Notice that path is never freed. That is intentional due to the
129 way putenv() works. This leaks a few bytes if we call
130 set_exec_path multiple times. */
133 return GPG_ERR_GENERAL;
139 /* Makes a temp directory and filenames */
141 make_tempdir(struct exec_info *info)
143 char *tmp=opt.temp_dir,*namein=info->name,*nameout;
146 namein=info->flags.binary?"tempin" EXTSEP_S "bin":"tempin" EXTSEP_S "txt";
148 nameout=info->flags.binary?"tempout" EXTSEP_S "bin":"tempout" EXTSEP_S "txt";
150 /* Make up the temp dir and files in case we need them */
157 tmp=xmalloc(MAX_PATH+2);
158 err=GetTempPath(MAX_PATH+1,tmp);
159 if(err==0 || err>MAX_PATH+1)
160 strcpy(tmp,"c:\\windows\\temp");
165 /* GetTempPath may return with \ on the end */
166 while(len>0 && tmp[len-1]=='\\')
172 #else /* More unixish systems */
173 tmp=getenv("TMPDIR");
180 tmp="<Wimp$ScrapDir>.GnuPG";
181 mkdir(tmp,0700); /* Error checks occur later on */
190 info->tempdir=xmalloc(strlen(tmp)+strlen(DIRSEP_S)+10+1);
192 sprintf(info->tempdir,"%s" DIRSEP_S "gpg-XXXXXX",tmp);
198 if (!gnupg_mkdtemp(info->tempdir))
199 log_error(_("can't create directory '%s': %s\n"),
200 info->tempdir,strerror(errno));
203 info->flags.madedir=1;
205 info->tempfile_in=xmalloc(strlen(info->tempdir)+
206 strlen(DIRSEP_S)+strlen(namein)+1);
207 sprintf(info->tempfile_in,"%s" DIRSEP_S "%s",info->tempdir,namein);
209 if(!info->flags.writeonly)
211 info->tempfile_out=xmalloc(strlen(info->tempdir)+
212 strlen(DIRSEP_S)+strlen(nameout)+1);
213 sprintf(info->tempfile_out,"%s" DIRSEP_S "%s",info->tempdir,nameout);
217 return info->flags.madedir? 0 : GPG_ERR_GENERAL;
220 /* Expands %i and %o in the args to the full temp files within the
223 expand_args(struct exec_info *info,const char *args_in)
225 const char *ch = args_in;
228 info->flags.use_temp_files=0;
229 info->flags.keep_temp_files=0;
232 log_debug("expanding string \"%s\"\n",args_in);
234 init_membuf (&command, 100);
247 info->flags.keep_temp_files=1;
251 if(!info->flags.madedir)
253 if(make_tempdir(info))
256 append=info->tempfile_out;
257 info->flags.use_temp_files=1;
261 info->flags.keep_temp_files=1;
265 if(!info->flags.madedir)
267 if(make_tempdir(info))
270 append=info->tempfile_in;
271 info->flags.use_temp_files=1;
280 put_membuf_str (&command, append);
283 put_membuf (&command, ch, 1);
288 put_membuf (&command, "", 1); /* Terminate string. */
290 info->command = get_membuf (&command, NULL);
292 return gpg_error_from_syserror ();
295 log_debug("args expanded to \"%s\", use %u, keep %u\n",info->command,
296 info->flags.use_temp_files,info->flags.keep_temp_files);
301 xfree (get_membuf (&command, NULL));
302 return GPG_ERR_GENERAL;
305 /* Either handles the tempfile creation, or the fork/exec. If it
306 returns ok, then info->tochild is a FILE * that can be written to.
307 The rules are: if there are no args, then it's a fork/exec/pipe.
308 If there are args, but no tempfiles, then it's a fork/exec/pipe via
309 shell -c. If there are tempfiles, then it's a system. */
312 exec_write(struct exec_info **info,const char *program,
313 const char *args_in,const char *name,int writeonly,int binary)
315 int ret = GPG_ERR_GENERAL;
317 if(opt.exec_disable && !opt.no_perm_warn)
319 log_info(_("external program calls are disabled due to unsafe "
320 "options file permissions\n"));
325 #if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
326 /* There should be no way to get to this spot while still carrying
327 setuid privs. Just in case, bomb out if we are. */
328 if ( getuid () != geteuid ())
332 if(program==NULL && args_in==NULL)
335 *info=xmalloc_clear(sizeof(struct exec_info));
338 (*info)->name=xstrdup(name);
339 (*info)->flags.binary=binary;
340 (*info)->flags.writeonly=writeonly;
342 /* Expand the args, if any */
343 if(args_in && expand_args(*info,args_in))
346 #ifdef EXEC_TEMPFILE_ONLY
347 if(!(*info)->flags.use_temp_files)
349 log_error(_("this platform requires temporary files when calling"
350 " external programs\n"));
354 #else /* !EXEC_TEMPFILE_ONLY */
356 /* If there are no args, or there are args, but no temp files, we
357 can use fork/exec/pipe */
358 if(args_in==NULL || (*info)->flags.use_temp_files==0)
372 if(((*info)->child=fork())==-1)
381 if((*info)->child==0)
383 char *shell=getenv("SHELL");
390 /* If the program isn't going to respond back, they get to
391 keep their stdout/stderr */
392 if(!(*info)->flags.writeonly)
394 /* implied close of STDERR */
395 if(dup2(STDOUT_FILENO,STDERR_FILENO)==-1)
398 /* implied close of STDOUT */
400 if(dup2(from[1],STDOUT_FILENO)==-1)
404 /* implied close of STDIN */
406 if(dup2(to[0],STDIN_FILENO)==-1)
412 log_debug("execlp: %s\n",program);
414 execlp(program,program,(void *)NULL);
419 log_debug("execlp: %s -c %s\n",shell,(*info)->command);
421 execlp(shell,shell,"-c",(*info)->command,(void *)NULL);
424 /* If we get this far the exec failed. Clean up and return. */
427 log_error(_("unable to execute program '%s': %s\n"),
428 program,strerror(errno));
430 log_error(_("unable to execute shell '%s': %s\n"),
431 shell,strerror(errno));
433 /* This mimics the POSIX sh behavior - 127 means "not found"
445 (*info)->tochild=fdopen(to[1],binary?"wb":"w");
446 if((*info)->tochild==NULL)
448 ret = gpg_error_from_syserror ();
455 (*info)->fromchild=iobuf_fdopen(from[0],"r");
456 if((*info)->fromchild==NULL)
458 ret = gpg_error_from_syserror ();
463 /* fd iobufs are cached! */
464 iobuf_ioctl((*info)->fromchild, IOBUF_IOCTL_NO_CACHE, 1, NULL);
468 #endif /* !EXEC_TEMPFILE_ONLY */
471 log_debug("using temp file '%s'\n",(*info)->tempfile_in);
473 /* It's not fork/exec/pipe, so create a temp file */
474 if( is_secured_filename ((*info)->tempfile_in) )
476 (*info)->tochild = NULL;
477 gpg_err_set_errno (EPERM);
480 (*info)->tochild=fopen((*info)->tempfile_in,binary?"wb":"w");
481 if((*info)->tochild==NULL)
483 ret = gpg_error_from_syserror ();
484 log_error(_("can't create '%s': %s\n"),
485 (*info)->tempfile_in,strerror(errno));
501 exec_read(struct exec_info *info)
503 int ret = GPG_ERR_GENERAL;
505 fclose(info->tochild);
508 if(info->flags.use_temp_files)
511 log_debug("system() command is %s\n",info->command);
514 info->progreturn=w32_system(info->command);
516 info->progreturn=system(info->command);
519 if(info->progreturn==-1)
521 log_error(_("system error while calling external program: %s\n"),
523 info->progreturn=127;
527 #if defined(WIFEXITED) && defined(WEXITSTATUS)
528 if(WIFEXITED(info->progreturn))
529 info->progreturn=WEXITSTATUS(info->progreturn);
532 log_error(_("unnatural exit of external program\n"));
533 info->progreturn=127;
537 /* If we don't have the macros, do the best we can. */
538 info->progreturn = (info->progreturn & 0xff00) >> 8;
541 /* 127 is the magic value returned from system() to indicate
542 that the shell could not be executed, or from /bin/sh to
543 indicate that the program could not be executed. */
545 if(info->progreturn==127)
547 log_error(_("unable to execute external program\n"));
551 if(!info->flags.writeonly)
553 info->fromchild=iobuf_open(info->tempfile_out);
555 && is_secured_file (iobuf_get_fd (info->fromchild)))
557 iobuf_close (info->fromchild);
558 info->fromchild = NULL;
559 gpg_err_set_errno (EPERM);
561 if(info->fromchild==NULL)
563 ret = gpg_error_from_syserror ();
564 log_error(_("unable to read external program response: %s\n"),
569 /* Do not cache this iobuf on close */
570 iobuf_ioctl(info->fromchild, IOBUF_IOCTL_NO_CACHE, 1, NULL);
581 exec_finish(struct exec_info *info)
583 int ret=info->progreturn;
586 iobuf_close(info->fromchild);
589 fclose(info->tochild);
591 #ifndef EXEC_TEMPFILE_ONLY
594 if(waitpid(info->child,&info->progreturn,0)!=0 &&
595 WIFEXITED(info->progreturn))
596 ret=WEXITSTATUS(info->progreturn);
599 log_error(_("unnatural exit of external program\n"));
605 if(info->flags.madedir && !info->flags.keep_temp_files)
607 if(info->tempfile_in)
609 if(unlink(info->tempfile_in)==-1)
610 log_info(_("WARNING: unable to remove tempfile (%s) '%s': %s\n"),
611 "in",info->tempfile_in,strerror(errno));
614 if(info->tempfile_out)
616 if(unlink(info->tempfile_out)==-1)
617 log_info(_("WARNING: unable to remove tempfile (%s) '%s': %s\n"),
618 "out",info->tempfile_out,strerror(errno));
621 if(rmdir(info->tempdir)==-1)
622 log_info(_("WARNING: unable to remove temp directory '%s': %s\n"),
623 info->tempdir,strerror(errno));
626 xfree(info->command);
628 xfree(info->tempdir);
629 xfree(info->tempfile_in);
630 xfree(info->tempfile_out);
635 #endif /* ! NO_EXEC */