FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ununpack-iso.c
Go to the documentation of this file.
1 /*******************************************************************
2  Copyright (C) 2007-2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *******************************************************************/
17 
23 #include "ununpack.h"
24 #include "externs.h"
25 
35 mode_t GetISOMode (char *Line)
36 {
37  mode_t Mode=0;
38  if (Line[1]=='r') Mode |= S_IRUSR;
39 #if 0
40  if (Line[2]=='w') Mode |= S_IWUSR;
41 #endif
42  if (Line[3]=='x') Mode |= S_IXUSR;
43  if (Line[3]=='s') Mode |= S_IXUSR | S_ISUID;
44  if (Line[3]=='S') Mode |= S_ISUID;
45 
46  if (Line[4]=='r') Mode |= S_IRGRP;
47 #if 0
48  if (Line[5]=='w') Mode |= S_IWGRP;
49 #endif
50  if (Line[6]=='x') Mode |= S_IXGRP;
51  if (Line[6]=='s') Mode |= S_IXGRP | S_ISGID;
52  if (Line[6]=='S') Mode |= S_ISGID;
53 
54  if (Line[7]=='r') Mode |= S_IROTH;
55 #if 0
56  if (Line[8]=='w') Mode |= S_IWOTH;
57 #endif
58  if (Line[9]=='x') Mode |= S_IXOTH;
59  if (Line[9]=='t') Mode |= S_IXOTH | S_ISVTX;
60  if (Line[9]=='T') Mode |= S_ISVTX;
61 
62  return(Mode);
63 } /* GetISOMode() */
64 
79 int ExtractISO (char *Source, char *Destination)
80 {
81  char Cmd[FILENAME_MAX*5]; /* command to run */
82  char Line[FILENAME_MAX];
83  int Len;
84  char *s; /* generic string pointer */
85  FILE *Fin;
86  int rc;
87  char TempSource[FILENAME_MAX], TempDestination[FILENAME_MAX];
88 
89  /* judge if the parameters are empty */
90  if ((NULL == Source) || (!strcmp(Source, "")) || (NULL == Destination) || (!strcmp(Destination, "")))
91  return 1;
92 
93  if (!Quiet && Verbose) fprintf(stderr,"Extracting ISO: %s\n",Source);
94 
95  /* get list of directories in the ISO and make the directories */
96  if (TaintString(TempSource,FILENAME_MAX,Source,1,NULL) ||
97  TaintString(TempDestination,FILENAME_MAX,Destination,1,NULL))
98  return(-1);
99  memset(Cmd,'\0',sizeof(Cmd));
100  snprintf(Cmd,sizeof(Cmd)," (isoinfo -l -R -J -i '%s' || isoinfo -l -R -i '%s' "
101  "|| isoinfo -l -i '%s') 2>/dev/null | grep '^Directory'",
102  TempSource, TempSource, TempSource);
103 
104 
105  Fin = popen(Cmd,"r");
106  if (!Fin)
107  {
108  fprintf(stderr,"ERROR: ISO failed: %s\n",Cmd);
109  return(-1);
110  }
111  while(ReadLine(Fin,Line,sizeof(Line)-1) >= 0)
112  {
113  s=strchr(Line,'/'); /* find first slash */
114  if (s==NULL) continue;
115  snprintf(Cmd,sizeof(Cmd),"%s%s",Destination,s);
116  if (Verbose > 1) printf("ISO directory: %s\n",Cmd);
117  if (MkDir(Cmd))
118  {
119  fprintf(stderr,"ERROR: Unable to mkdir(%s) in ExtractISO\n",Cmd);
120  if (!ForceContinue) SafeExit(40);
121  }
122  }
123  pclose(Fin);
124 
125  /* Now let's extract each file */
126  snprintf(Cmd,sizeof(Cmd),"(isoinfo -f -R -J -i '%s' || isoinfo -f -R -i '%s' || isoinfo -f -i '%s') 2>/dev/null",
127  TempSource, TempSource, TempSource);
128 
129  Fin = popen(Cmd,"r");
130  if (!Fin)
131  {
132  fprintf(stderr,"ERROR: ISO failed: %s\n",Cmd);
133  return(-1);
134  }
135 
136  memset(Line,'\0',sizeof(Line));
137  strcpy(Line,Destination);
138  Len=strlen(Destination);
139  while(ReadLine(Fin,Line+Len,sizeof(Line)-1-Len) >= 0)
140  {
141  if (Line[Len] != '/') continue; /* should not happen, but... */
142  if (IsDir(Line)) continue; /* don't do directories */
143  if (Verbose > 1) printf("ISO file: %s\n",Line);
144  /* create extraction command */
145  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);
146  rc = system(Cmd);
147  if (WIFSIGNALED(rc))
148  {
149  printf("ERROR: Process killed by signal (%d): %s\n",WTERMSIG(rc),Cmd);
150  SafeExit(-1);
151  }
152  rc = WEXITSTATUS(rc);
153  if (rc)
154  {
155  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
156  pclose(Fin);
157  return(rc);
158  }
159  }
160  pclose(Fin);
161 
162 
163 #if 0
164  /* save the ISO information */
165  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",
166  TempSource, TempSource, TempSource, TempDestination);
167  rc = system(Cmd);
168  if (WIFSIGNALED(rc))
169  {
170  printf("ERROR: Process killed by signal (%d): %s\n",WTERMSIG(rc),Cmd);
171  SafeExit(-1);
172  }
173  rc = WEXITSTATUS(rc);
174  if (rc)
175  {
176  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
177  return(rc);
178  }
179 #endif
180 
181 
182  /* Set the permissions on every file and directory */
183  /* Only RockRidge saves permission information! */
184  snprintf(Cmd,sizeof(Cmd),"(isoinfo -l -R -J -i '%s' || isoinfo -l -R -i '%s' || isoinfo -l -i '%s') 2>/dev/null",
185  TempSource, TempSource, TempSource);
186  Fin = popen(Cmd,"r");
187  if (Fin)
188  {
189  mode_t Mode;
190  char Dir[FILENAME_MAX];
191  memset(Dir,'\0',sizeof(Dir));
192  while((Len = ReadLine(Fin,Line,sizeof(Line)-1)) >= 0)
193  {
194  if (Len == 0) continue;
195  if (!strchr("Dd-",Line[0])) continue;
196  /* Every line is either a "Directory" or desirable chmod */
197  if (!strncmp(Line,"Directory listing of ",21))
198  {
199  strcpy(Dir,Line+22);
200  continue;
201  }
202  snprintf(Cmd,sizeof(Cmd)-1,"%s/%s%s",Destination,Dir,Line+67);
203  Mode = GetISOMode(Line);
204  chmod(Cmd,Mode);
205  }
206  pclose(Fin);
207  }
208 
209  /* All done */
210  return(0);
211 } /* ExtractISO() */
int Quiet
Run in quiet mode?
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:330
int Verbose
Verbose level.
Definition: util.c:28
int ExtractISO(char *Source, char *Destination)
Given an ISO image and a directory, extract the image to the directory.
Definition: ununpack-iso.c:79
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
mode_t GetISOMode(char *Line)
Given a line in drwxrwxrwx format, convert it to a numeric mode.
Definition: ununpack-iso.c:35
int MkDir(char *Fname)
Smart mkdir.
Definition: utils.c:314
Stores all extern variables used by the agent.
char * TaintString(char *S)
Create a string with taint quoting.
Definition: finder.c:46
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Definition: repcopyin.c:77
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:88
int ForceContinue
Force continue when unpack tool fails?