FOSSology  4.7.1
Open Source License Compliance by Open Source Software
ununpack-iso.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2007-2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 #include "ununpack.h"
13 #include "externs.h"
14 
24 mode_t GetISOMode (char *Line)
25 {
26  mode_t Mode=0;
27  if (Line[1]=='r') Mode |= S_IRUSR;
28 #if 0
29  if (Line[2]=='w') Mode |= S_IWUSR;
30 #endif
31  if (Line[3]=='x') Mode |= S_IXUSR;
32  if (Line[3]=='s') Mode |= S_IXUSR | S_ISUID;
33  if (Line[3]=='S') Mode |= S_ISUID;
34 
35  if (Line[4]=='r') Mode |= S_IRGRP;
36 #if 0
37  if (Line[5]=='w') Mode |= S_IWGRP;
38 #endif
39  if (Line[6]=='x') Mode |= S_IXGRP;
40  if (Line[6]=='s') Mode |= S_IXGRP | S_ISGID;
41  if (Line[6]=='S') Mode |= S_ISGID;
42 
43  if (Line[7]=='r') Mode |= S_IROTH;
44 #if 0
45  if (Line[8]=='w') Mode |= S_IWOTH;
46 #endif
47  if (Line[9]=='x') Mode |= S_IXOTH;
48  if (Line[9]=='t') Mode |= S_IXOTH | S_ISVTX;
49  if (Line[9]=='T') Mode |= S_ISVTX;
50 
51  return(Mode);
52 } /* GetISOMode() */
53 
68 int ExtractISO (char *Source, char *Destination)
69 {
70  char Cmd[FILENAME_MAX*5]; /* command to run */
71  char Line[FILENAME_MAX];
72  int Len;
73  char *s; /* generic string pointer */
74  FILE *Fin;
75  int rc;
76  char TempSource[FILENAME_MAX], TempDestination[FILENAME_MAX];
77  char TempIso[FILENAME_MAX], TempOut[FILENAME_MAX];
78 
79  /* judge if the parameters are empty */
80  if ((NULL == Source) || (!strcmp(Source, "")) || (NULL == Destination) || (!strcmp(Destination, "")))
81  return 1;
82 
83  if (!Quiet && Verbose) fprintf(stderr,"Extracting ISO: %s\n",Source);
84 
85  /* get list of directories in the ISO and make the directories */
86  if (TaintString(TempSource,FILENAME_MAX,Source,1,NULL) ||
87  TaintString(TempDestination,FILENAME_MAX,Destination,1,NULL))
88  return(-1);
89  memset(Cmd,'\0',sizeof(Cmd));
90  snprintf(Cmd,sizeof(Cmd)," (isoinfo -l -R -J -i '%s' || isoinfo -l -R -i '%s' "
91  "|| isoinfo -l -i '%s') 2>/dev/null | grep '^Directory'",
92  TempSource, TempSource, TempSource);
93 
94 
95  Fin = popen(Cmd,"r");
96  if (!Fin)
97  {
98  fprintf(stderr,"ERROR: ISO failed: %s\n",Cmd);
99  return(-1);
100  }
101  while(ReadLine(Fin,Line,sizeof(Line)-1) >= 0)
102  {
103  s=strchr(Line,'/'); /* find first slash */
104  if (s==NULL) continue;
105  snprintf(Cmd,sizeof(Cmd),"%s%s",Destination,s);
106  if (Verbose > 1) printf("ISO directory: %s\n",Cmd);
107  if (MkDir(Cmd))
108  {
109  fprintf(stderr,"ERROR: Unable to mkdir(%s) in ExtractISO\n",Cmd);
110  if (!ForceContinue) SafeExit(40);
111  }
112  }
113  pclose(Fin);
114 
115  /* Now let's extract each file */
116  snprintf(Cmd,sizeof(Cmd),"(isoinfo -f -R -J -i '%s' || isoinfo -f -R -i '%s' || isoinfo -f -i '%s') 2>/dev/null",
117  TempSource, TempSource, TempSource);
118 
119  Fin = popen(Cmd,"r");
120  if (!Fin)
121  {
122  fprintf(stderr,"ERROR: ISO failed: %s\n",Cmd);
123  return(-1);
124  }
125 
126  memset(Line,'\0',sizeof(Line));
127  strcpy(Line,Destination);
128  Len=strlen(Destination);
129  while(ReadLine(Fin,Line+Len,sizeof(Line)-1-Len) >= 0)
130  {
131  if (Line[Len] != '/') continue; /* should not happen, but... */
132  if (IsDir(Line)) continue; /* don't do directories */
133  if (Verbose > 1) printf("ISO file: %s\n",Line);
134  /* The in-ISO path (Line+Len) and the output path (Line) come from the
135  archive's own directory listing and are attacker-controlled, so they
136  must be shell-escaped before being passed to system(). Without this a
137  crafted ISO containing a file whose name has a single quote can inject
138  and run arbitrary commands as the agent user. */
139  if (TaintString(TempIso,FILENAME_MAX,Line+Len,1,NULL) ||
140  TaintString(TempOut,FILENAME_MAX,Line,1,NULL))
141  continue; /* name too long to escape safely; skip this file */
142  /* create extraction command */
143  rc = snprintf(Cmd,sizeof(Cmd),"(isoinfo -R -J -i '%s' -x '%s' || isoinfo -R -i '%s' -x '%s' || isoinfo -i '%s' -x '%s') > '%s' 2>/dev/null",TempSource,TempIso,TempSource,TempIso,TempSource, TempIso,TempOut);
144  if (rc < 0 || (size_t)rc >= sizeof(Cmd))
145  {
146  /* command truncated; running it could execute a partial command, so skip */
147  fprintf(stderr,"WARNING pfile %s ISO extraction command too long, skipping '%s'\n",Pfile,Line+Len);
148  continue;
149  }
150  rc = system(Cmd);
151  if (WIFSIGNALED(rc))
152  {
153  printf("ERROR: Process killed by signal (%d): %s\n",WTERMSIG(rc),Cmd);
154  SafeExit(-1);
155  }
156  rc = WEXITSTATUS(rc);
157  if (rc)
158  {
159  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
160  pclose(Fin);
161  return(rc);
162  }
163  }
164  pclose(Fin);
165 
166 
167 #if 0
168  /* save the ISO information */
169  snprintf(Cmd,sizeof(Cmd),"(isoinfo -d -R -i '%s' || isoinfo -d -R -J -i '%s' || isoinfo -d -i '%s') > '%s/ISO_INFO' 2>/dev/null",
170  TempSource, TempSource, TempSource, TempDestination);
171  rc = system(Cmd);
172  if (WIFSIGNALED(rc))
173  {
174  printf("ERROR: Process killed by signal (%d): %s\n",WTERMSIG(rc),Cmd);
175  SafeExit(-1);
176  }
177  rc = WEXITSTATUS(rc);
178  if (rc)
179  {
180  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
181  return(rc);
182  }
183 #endif
184 
185 
186  /* Set the permissions on every file and directory */
187  /* Only RockRidge saves permission information! */
188  snprintf(Cmd,sizeof(Cmd),"(isoinfo -l -R -J -i '%s' || isoinfo -l -R -i '%s' || isoinfo -l -i '%s') 2>/dev/null",
189  TempSource, TempSource, TempSource);
190  Fin = popen(Cmd,"r");
191  if (Fin)
192  {
193  mode_t Mode;
194  char Dir[FILENAME_MAX];
195  memset(Dir,'\0',sizeof(Dir));
196  while((Len = ReadLine(Fin,Line,sizeof(Line)-1)) >= 0)
197  {
198  if (Len == 0) continue;
199  if (!strchr("Dd-",Line[0])) continue;
200  /* Every line is either a "Directory" or desirable chmod */
201  if (!strncmp(Line,"Directory listing of ",21))
202  {
203  strcpy(Dir,Line+22);
204  continue;
205  }
206  snprintf(Cmd,sizeof(Cmd)-1,"%s/%s%s",Destination,Dir,Line+67);
207  Mode = GetISOMode(Line);
208  chmod(Cmd,Mode);
209  }
210  pclose(Fin);
211  }
212 
213  /* All done */
214  return(0);
215 } /* ExtractISO() */
int Verbose
Verbose level.
Definition: util.c:19
Stores all extern variables used by the agent.
char * Pfile
Pfile name (SHA1.MD5.Size)
int Quiet
Run in quiet mode?
int ForceContinue
Force continue when unpack tool fails?
char * TaintString(char *S)
Create a string with taint quoting.
Definition: finder.c:35
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Definition: repcopyin.c:64
int ExtractISO(char *Source, char *Destination)
Given an ISO image and a directory, extract the image to the directory.
Definition: ununpack-iso.c:68
mode_t GetISOMode(char *Line)
Given a line in drwxrwxrwx format, convert it to a numeric mode.
Definition: ununpack-iso.c:24
int MkDir(char *Fname)
Smart mkdir.
Definition: utils.c:311
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:85
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:327