1 /* exec.c - generic call-a-program code
2 * Copyright (C) 2001, 2002, 2003 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 #include <sys/types.h>
27 #ifndef EXEC_TEMPFILE_ONLY
30 #ifdef HAVE_DOSISH_SYSTEM
45 int exec_write(struct exec_info **info,const char *program,
46 const char *args_in,const char *name,int writeonly,int binary)
48 log_error(_("no remote program execution supported\n"));
49 return GPG_ERR_GENERAL;
52 int exec_read(struct exec_info *info) { return GPG_ERR_GENERAL; }
53 int exec_finish(struct exec_info *info) { return GPG_ERR_GENERAL; }
54 int set_exec_path(const char *path,int method) { return GPG_ERR_GENERAL; }
59 char *mkdtemp(char *template);
63 /* This is a nicer system() for windows that waits for programs to
64 return before returning control to the caller. I hate helpful
66 static int win_system(const char *command)
68 PROCESS_INFORMATION pi;
72 /* We must use a copy of the command as CreateProcess modifies this
74 string=xstrdup (command);
76 memset(&pi,0,sizeof(pi));
77 memset(&si,0,sizeof(si));
80 if(!CreateProcess(NULL,string,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
83 /* Wait for the child to exit */
84 WaitForSingleObject(pi.hProcess,INFINITE);
86 CloseHandle(pi.hProcess);
87 CloseHandle(pi.hThread);
94 /* method==0 to replace current $PATH, and 1 to append to current
96 int set_exec_path(const char *path,int method)
98 char *p,*curpath=NULL;
101 if(method==1 && (curpath=getenv("PATH")))
102 curlen=strlen(curpath)+1;
104 p=xmalloc (5+curlen+strlen(path)+1);
116 log_debug("set_exec_path method %d: %s\n",method,p);
118 /* Notice that path is never freed. That is intentional due to the
119 way putenv() works. This leaks a few bytes if we call
120 set_exec_path multiple times. */
123 return GPG_ERR_GENERAL;
128 /* Makes a temp directory and filenames */
129 static int make_tempdir(struct exec_info *info)
131 char *tmp=opt.temp_dir,*namein=info->name,*nameout;
134 namein=info->binary?"tempin" EXTSEP_S "bin":"tempin" EXTSEP_S "txt";
136 nameout=info->binary?"tempout" EXTSEP_S "bin":"tempout" EXTSEP_S "txt";
138 /* Make up the temp dir and files in case we need them */
144 if(GetTempPath(256,tmp)==0)
145 strcpy(tmp,"c:\\windows\\temp");
150 /* GetTempPath may return with \ on the end */
151 while(len>0 && tmp[len-1]=='\\')
157 #else /* More unixish systems */
158 tmp=getenv("TMPDIR");
165 tmp="<Wimp$ScrapDir>.GnuPG";
166 mkdir(tmp,0700); /* Error checks occur later on */
175 info->tempdir=xmalloc (strlen(tmp)+strlen(DIRSEP_S)+10+1);
177 sprintf(info->tempdir,"%s" DIRSEP_S "gpg-XXXXXX",tmp);
183 if(mkdtemp(info->tempdir)==NULL)
184 log_error(_("can't create directory `%s': %s\n"),
185 info->tempdir,strerror(errno));
190 info->tempfile_in=xmalloc (strlen(info->tempdir)+
191 strlen(DIRSEP_S)+strlen(namein)+1);
192 sprintf(info->tempfile_in,"%s" DIRSEP_S "%s",info->tempdir,namein);
196 info->tempfile_out=xmalloc (strlen(info->tempdir)+
197 strlen(DIRSEP_S)+strlen(nameout)+1);
198 sprintf(info->tempfile_out,"%s" DIRSEP_S "%s",info->tempdir,nameout);
202 return info->madedir?0:GPG_ERR_GENERAL;
205 /* Expands %i and %o in the args to the full temp files within the
207 static int expand_args(struct exec_info *info,const char *args_in)
209 const char *ch=args_in;
210 unsigned int size,len;
212 info->use_temp_files=0;
213 info->keep_temp_files=0;
216 log_debug("expanding string \"%s\"\n",args_in);
219 info->command=xmalloc (size);
221 info->command[0]='\0';
234 info->keep_temp_files=1;
240 if(make_tempdir(info))
243 append=info->tempfile_out;
244 info->use_temp_files=1;
248 info->keep_temp_files=1;
254 if(make_tempdir(info))
257 append=info->tempfile_in;
258 info->use_temp_files=1;
268 size_t applen=strlen(append);
270 if(applen+len>size-1)
276 info->command=xrealloc(info->command,size);
279 strcat(info->command,append);
285 if(len==size-1) /* leave room for the \0 */
288 info->command=xrealloc(info->command,size);
291 info->command[len++]=*ch;
292 info->command[len]='\0';
299 log_debug("args expanded to \"%s\", use %d, keep %d\n",
300 info->command,info->use_temp_files,info->keep_temp_files);
306 xfree (info->command);
309 return GPG_ERR_GENERAL;
312 /* Either handles the tempfile creation, or the fork/exec. If it
313 returns ok, then info->tochild is a FILE * that can be written to.
314 The rules are: if there are no args, then it's a fork/exec/pipe.
315 If there are args, but no tempfiles, then it's a fork/exec/pipe via
316 shell -c. If there are tempfiles, then it's a system. */
318 int exec_write(struct exec_info **info,const char *program,
319 const char *args_in,const char *name,int writeonly,int binary)
321 int ret=GPG_ERR_GENERAL;
323 if(opt.exec_disable && !opt.no_perm_warn)
325 log_info(_("external program calls are disabled due to unsafe "
326 "options file permissions\n"));
331 #if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
332 /* There should be no way to get to this spot while still carrying
333 setuid privs. Just in case, bomb out if we are. */
334 if(getuid()!=geteuid())
338 if(program==NULL && args_in==NULL)
341 *info=xcalloc (1,sizeof(struct exec_info));
344 (*info)->name=xstrdup (name);
345 (*info)->binary=binary;
346 (*info)->writeonly=writeonly;
348 /* Expand the args, if any */
349 if(args_in && expand_args(*info,args_in))
352 #ifdef EXEC_TEMPFILE_ONLY
353 if(!(*info)->use_temp_files)
355 log_error(_("this platform requires temp files when calling external "
360 #else /* !EXEC_TEMPFILE_ONLY */
362 /* If there are no args, or there are args, but no temp files, we
363 can use fork/exec/pipe */
364 if(args_in==NULL || (*info)->use_temp_files==0)
378 if(((*info)->child=fork())==-1)
387 if((*info)->child==0)
389 char *shell=getenv("SHELL");
396 /* If the program isn't going to respond back, they get to
397 keep their stdout/stderr */
398 if(!(*info)->writeonly)
400 /* implied close of STDERR */
401 if(dup2(STDOUT_FILENO,STDERR_FILENO)==-1)
404 /* implied close of STDOUT */
406 if(dup2(from[1],STDOUT_FILENO)==-1)
410 /* implied close of STDIN */
412 if(dup2(to[0],STDIN_FILENO)==-1)
418 log_debug("execlp: %s\n",program);
420 execlp(program,program,(void *)NULL);
425 log_debug("execlp: %s -c %s\n",shell,(*info)->command);
427 execlp(shell,shell,"-c",(*info)->command,(void *)NULL);
430 /* If we get this far the exec failed. Clean up and return. */
432 log_error(_("unable to execute %s \"%s\": %s\n"),
433 args_in==NULL?"program":"shell",
434 args_in==NULL?program:shell,
437 /* This mimics the POSIX sh behavior - 127 means "not found"
449 (*info)->tochild=fdopen(to[1],binary?"wb":"w");
450 if((*info)->tochild==NULL)
452 ret = gpg_error_from_errno (errno);
459 (*info)->fromchild=iobuf_fdopen(from[0],"r");
460 if((*info)->fromchild==NULL)
462 ret = gpg_error_from_errno (errno);
467 /* fd iobufs are cached?! */
468 iobuf_ioctl((*info)->fromchild,3,1,NULL);
472 #endif /* !EXEC_TEMPFILE_ONLY */
475 log_debug("using temp file `%s'\n",(*info)->tempfile_in);
477 /* It's not fork/exec/pipe, so create a temp file */
478 (*info)->tochild=fopen((*info)->tempfile_in,binary?"wb":"w");
479 if((*info)->tochild==NULL)
481 ret = gpg_error_from_errno (errno);
482 log_error(_("can't create `%s': %s\n"),
483 (*info)->tempfile_in,strerror(errno));
493 int exec_read(struct exec_info *info)
495 int ret=GPG_ERR_GENERAL;
497 fclose(info->tochild);
500 if(info->use_temp_files)
503 log_debug("system() command is %s\n",info->command);
506 info->progreturn=win_system(info->command);
508 info->progreturn=system(info->command);
511 if(info->progreturn==-1)
513 log_error(_("system error while calling external program: %s\n"),
515 info->progreturn=127;
519 #if defined(WIFEXITED) && defined(WEXITSTATUS)
520 if(WIFEXITED(info->progreturn))
521 info->progreturn=WEXITSTATUS(info->progreturn);
524 log_error(_("unnatural exit of external program\n"));
525 info->progreturn=127;
529 /* If we don't have the macros, do the best we can. */
530 info->progreturn = (info->progreturn & 0xff00) >> 8;
533 /* 127 is the magic value returned from system() to indicate
534 that the shell could not be executed, or from /bin/sh to
535 indicate that the program could not be executed. */
537 if(info->progreturn==127)
539 log_error(_("unable to execute external program\n"));
545 info->fromchild=iobuf_open(info->tempfile_out);
546 if(info->fromchild==NULL)
548 ret = gpg_error_from_errno (errno);
549 log_error(_("unable to read external program response: %s\n"),
554 /* Do not cache this iobuf on close */
555 iobuf_ioctl(info->fromchild,3,1,NULL);
565 int exec_finish(struct exec_info *info)
567 int ret=info->progreturn;
570 iobuf_close(info->fromchild);
573 fclose(info->tochild);
575 #ifndef EXEC_TEMPFILE_ONLY
578 if(waitpid(info->child,&info->progreturn,0)!=0 &&
579 WIFEXITED(info->progreturn))
580 ret=WEXITSTATUS(info->progreturn);
583 log_error(_("unnatural exit of external program\n"));
589 if(info->madedir && !info->keep_temp_files)
591 if(info->tempfile_in)
593 if(unlink(info->tempfile_in)==-1)
594 log_info(_("WARNING: unable to remove tempfile (%s) `%s': %s\n"),
595 "in",info->tempfile_in,strerror(errno));
598 if(info->tempfile_out)
600 if(unlink(info->tempfile_out)==-1)
601 log_info(_("WARNING: unable to remove tempfile (%s) `%s': %s\n"),
602 "out",info->tempfile_out,strerror(errno));
605 if(rmdir(info->tempdir)==-1)
606 log_info(_("WARNING: unable to remove temp directory `%s': %s\n"),
607 info->tempdir,strerror(errno));
610 xfree (info->command);
612 xfree (info->tempdir);
613 xfree (info->tempfile_in);
614 xfree (info->tempfile_out);
619 #endif /* ! NO_EXEC */