FOSSology  4.4.0
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 
78  /* judge if the parameters are empty */
79  if ((NULL == Source) || (!strcmp(Source, "")) || (NULL == Destination) || (!strcmp(Destination, "")))
80  return 1;
81 
82  if (!Quiet && Verbose) fprintf(stderr,"Extracting ISO: %s\n",Source);
83 
84  /* get list of directories in the ISO and make the directories */
85  if (TaintString(TempSource,FILENAME_MAX,Source,1,NULL) ||
86  TaintString(TempDestination,FILENAME_MAX,Destination,1,NULL))
87  return(-1);
88  memset(Cmd,'\0',sizeof(Cmd));
89  snprintf(Cmd,sizeof(Cmd)," (isoinfo -l -R -J -i '%s' || isoinfo -l -R -i '%s' "
90  "|| isoinfo -l -i '%s') 2>/dev/null | grep '^Directory'",
91  TempSource, TempSource, TempSource);
92 
93 
94  Fin = popen(Cmd,"r");
95  if (!Fin)
96  {
97  fprintf(stderr,"ERROR: ISO failed: %s\n",Cmd);
98  return(-1);
99  }
100  while(ReadLine(Fin,Line,sizeof(Line)-1) >= 0)
101  {
102  s=strchr(Line,'/'); /* find first slash */
103  if (s==NULL) continue;
104  snprintf(Cmd,sizeof(Cmd),"%s%s",Destination,s);
105  if (Verbose > 1) printf("ISO directory: %s\n",Cmd);
106  if (MkDir(Cmd))
107  {
108  fprintf(stderr,"ERROR: Unable to mkdir(%s) in ExtractISO\n",Cmd);
109  if (!ForceContinue) SafeExit(40);
110  }
111  }
112  pclose(Fin);
113 
114  /* Now let's extract each file */
115  snprintf(Cmd,sizeof(Cmd),"(isoinfo -f -R -J -i '%s' || isoinfo -f -R -i '%s' || isoinfo -f -i '%s') 2>/dev/null",
116  TempSource, TempSource, TempSource);
117 
118  Fin = popen(Cmd,"r");
119  if (!Fin)
120  {
121  fprintf(stderr,"ERROR: ISO failed: %s\n",Cmd);
122  return(-1);
123  }
124 
125  memset(Line,'\0',sizeof(Line));
126  strcpy(Line,Destination);
127  Len=strlen(Destination);
128  while(ReadLine(Fin,Line+Len,sizeof(Line)-1-Len) >= 0)
129  {
130  if (Line[Len] != '/') continue; /* should not happen, but... */
131  if (IsDir(Line)) continue; /* don't do directories */
132  if (Verbose > 1) printf("ISO file: %s\n",Line);
133  /* create extraction command */
134  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,Line+Len,TempSource,Line+Len,TempSource, Line+Len,Line);
135  rc = system(Cmd);
136  if (WIFSIGNALED(rc))
137  {
138  printf("ERROR: Process killed by signal (%d): %s\n",WTERMSIG(rc),Cmd);
139  SafeExit(-1);
140  }
141  rc = WEXITSTATUS(rc);
142  if (rc)
143  {
144  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
145  pclose(Fin);
146  return(rc);
147  }
148  }
149  pclose(Fin);
150 
151 
152 #if 0
153  /* save the ISO information */
154  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",
155  TempSource, TempSource, TempSource, TempDestination);
156  rc = system(Cmd);
157  if (WIFSIGNALED(rc))
158  {
159  printf("ERROR: Process killed by signal (%d): %s\n",WTERMSIG(rc),Cmd);
160  SafeExit(-1);
161  }
162  rc = WEXITSTATUS(rc);
163  if (rc)
164  {
165  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
166  return(rc);
167  }
168 #endif
169 
170 
171  /* Set the permissions on every file and directory */
172  /* Only RockRidge saves permission information! */
173  snprintf(Cmd,sizeof(Cmd),"(isoinfo -l -R -J -i '%s' || isoinfo -l -R -i '%s' || isoinfo -l -i '%s') 2>/dev/null",
174  TempSource, TempSource, TempSource);
175  Fin = popen(Cmd,"r");
176  if (Fin)
177  {
178  mode_t Mode;
179  char Dir[FILENAME_MAX];
180  memset(Dir,'\0',sizeof(Dir));
181  while((Len = ReadLine(Fin,Line,sizeof(Line)-1)) >= 0)
182  {
183  if (Len == 0) continue;
184  if (!strchr("Dd-",Line[0])) continue;
185  /* Every line is either a "Directory" or desirable chmod */
186  if (!strncmp(Line,"Directory listing of ",21))
187  {
188  strcpy(Dir,Line+22);
189  continue;
190  }
191  snprintf(Cmd,sizeof(Cmd)-1,"%s/%s%s",Destination,Dir,Line+67);
192  Mode = GetISOMode(Line);
193  chmod(Cmd,Mode);
194  }
195  pclose(Fin);
196  }
197 
198  /* All done */
199  return(0);
200 } /* ExtractISO() */
int Verbose
Verbose level.
Definition: util.c:19
Stores all extern variables used by the agent.
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:303
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:77
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:319