File: item-file.c

package info (click to toggle)
cfengine2 2.2.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 10,184 kB
  • ctags: 5,650
  • sloc: ansic: 63,372; sh: 9,932; makefile: 218; lisp: 182; yacc: 100; perl: 72
file content (353 lines) | stat: -rw-r--r-- 8,352 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* cfengine for GNU
 
        Copyright (C) 1995
        Free Software Foundation, Inc.
 
   This file is part of GNU cfengine - written and maintained 
   by Mark Burgess, Dept of Computing and Engineering, Oslo College,
   Dept. of Theoretical physics, University of Oslo
 
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

*/


/*********************************************************************/
/*                                                                   */
/*  TOOLKIT: the "item file extension" object for cfengine           */
/*                                                                   */
/*********************************************************************/

#include "cf.defs.h"
#include "cf.extern.h"

/*********************************************************************/

int LoadItemList(struct Item **liststart,char *file)

{ FILE *fp;
  struct stat statbuf;

if (stat(file,&statbuf) == -1)
   {
   if (DONTDO)
      {
      snprintf(OUTPUT,CF_BUFSIZE*2,"Currently no file %s to edit\n",file);
      CfLog(cfverbose,OUTPUT,"");
      return false;
      }
   else
      {
      snprintf(OUTPUT,CF_BUFSIZE*2,"Couldn't stat %s - no file to edit\n",file);
      CfLog(cfverbose,OUTPUT,"stat");
      return false;
      }
   }

if ((EDITFILESIZE != 0) &&(statbuf.st_size > EDITFILESIZE))
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"File %s is bigger than the limit <editfilesize>\n",file);
   CfLog(cfinform,OUTPUT,"");
   return(false);
   }

if (! S_ISREG(statbuf.st_mode))
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"%s is not a plain file\n",file);
   CfLog(cfinform,OUTPUT,"");
   return false;
   }

if ((fp = fopen(file,"r")) == NULL)
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"Couldn't read file %s for editing\n",file);
   CfLog(cfinform,OUTPUT,"fopen");
   return false;
   }


memset(VBUFF,0,CF_BUFSIZE); 

while(!feof(fp))
   {
   ReadLine(VBUFF,CF_BUFSIZE,fp);

   if (!feof(fp) || (strlen(VBUFF) != 0))
      {
      AppendItem(liststart,VBUFF,NULL);
      }
   VBUFF[0] = '\0';
   }

fclose(fp);
return (true); 
}

/*********************************************************************/

int SaveItemList(struct Item *liststart,char *file,char *repository)

{ struct Item *ip;
  struct stat statbuf;
  char new[CF_BUFSIZE],backup[CF_BUFSIZE];
  FILE *fp;
  mode_t mask;
  char stamp[CF_BUFSIZE]; 
  time_t STAMPNOW;
  STAMPNOW = time((time_t *)NULL);
#ifdef WITH_SELINUX
  int selinux_enabled=0;
  security_context_t scontext=NULL;

  selinux_enabled = (is_selinux_enabled()>0);
  if(selinux_enabled)
      {
      /* get current security context */
      getfilecon(file, &scontext);
      }
#endif

if (stat(file,&statbuf) == -1)
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"Couldn't stat %s, which needed editing!\n",file);
   CfLog(cferror,OUTPUT,"");
   CfLog(cferror,"Check definition in program - is file NFS mounted?\n\n","");
   return false;
   }

strcpy(new,file);
strcat(new,CF_EDITED);

strcpy(backup,file);

sprintf(stamp, "_%d_%s", CFSTARTTIME, CanonifyName(ctime(&STAMPNOW)));

if (IMAGEBACKUP == 's')
   {
   strcat(backup,stamp);
   }
 
strcat(backup,CF_SAVED);
 
unlink(new); /* Just in case of races */ 
 
if ((fp = fopen(new,"w")) == NULL)
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"Couldn't write file %s after editing\n",new);
   CfLog(cferror,OUTPUT,"");
   return false;
   }

for (ip = liststart; ip != NULL; ip=ip->next)
   {
   fprintf(fp,"%s\n",ip->name);
   }

if (fclose(fp) == -1)
   {
   CfLog(cferror,"Unable to close file while writing","fclose");
   return false;
   }
 
if (ISCFENGINE && (ACTION == editfiles))
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"Edited file %s \n",file);
   CfLog(cfinform,OUTPUT,""); 
   }

if (IMAGEBACKUP != 'n')
   {
   if (! IsItemIn(VREPOSLIST,new))
      {
      if (rename(file,backup) == -1)
         {
         snprintf(OUTPUT,CF_BUFSIZE*2,"Error while renaming backup %s\n",file);
         CfLog(cfverbose,OUTPUT,"rename ");
         unlink(new);
         return false;
         }
      else if (Repository(backup,repository))
         {
         unlink(backup);
         }
      }
   }

if (rename(new,file) == -1)
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"Error while renaming %s\n",file);
   CfLog(cfverbose,OUTPUT,"rename");
   return false;
   }       

mask = umask(0); 
chmod(file,statbuf.st_mode);                    /* Restore file permissions etc */
chown(file,statbuf.st_uid,statbuf.st_gid);
umask(mask); 
#ifdef WITH_SELINUX
if(selinux_enabled)
    {
    /* restore file context */
    setfilecon(file,scontext);
    }
#endif
return true;
}

/*********************************************************************/

int CompareToFile(struct Item *liststart,char *file)

/* returns true if file on disk is identical to file in memory */

{ FILE *fp;
  struct stat statbuf;
  struct Item *ip = liststart;
  unsigned char *finmem = NULL, fdata;
  unsigned long fip = 0, tmplen, idx;

Debug("CompareToFile(%s)\n",file);

if (stat(file,&statbuf) == -1)
   {
   return false;
   }

if (liststart == NULL)
   {
   return false;
   }

for (ip = liststart; ip != NULL; ip=ip->next)
   {
   tmplen = strlen(ip->name);
   if ((finmem = realloc(finmem, fip+tmplen+1)) == NULL)
	{
	Debug("CompareToFile(%s): can't realloc() memory\n",file);
	free(finmem);
	return false;
	}
   memcpy(finmem+fip, ip->name, tmplen);
   fip += tmplen;
   *(finmem+fip++) = '\n';
   }

if (statbuf.st_size != fip)
   {
   Debug("CompareToFile(%s): sizes are different: MEM:(%u) FILE:(%u)\n",file, fip, statbuf.st_size);
   free(finmem);
   return false;
   }
 
if ((fp = fopen(file,"r")) == NULL)
   {
   snprintf(OUTPUT,CF_BUFSIZE*2,"Couldn't read file %s for editing\n",file);
   CfLog(cferror,OUTPUT,"fopen");
   free(finmem);
   return false;
   }

for (idx = 0; idx < fip; idx++)
   {
   if (fread(&fdata, 1, 1, fp) != 1)
	{
	Debug("CompareToFile(%s): non-zero fread() before file-in-mem finished at %u-th byte MEM:(0x%x/%c)\n",file, idx, *(finmem+idx), *(finmem+idx));
	free(finmem);
	fclose(fp);
	return false;
	}
   if (fdata != *(finmem+idx))
	{
	Debug("CompareToFile(%s): difference found at %u-th byte MEM:(0x%x/%c) != FILE:(0x%x/%c)\n",file, idx, *(finmem+idx), *(finmem+idx), fdata, fdata);
	free(finmem);
	fclose(fp);
	return false;
	}
   }

free(finmem);
fclose(fp);
return (true);
}

/*********************************************************************/

void InsertFileAfter (struct Item **filestart,struct Item *ptr,char *string)

{ struct Item *ip;
  char *sp;
  FILE *fp;
  char linebuf[CF_BUFSIZE];

EditVerbose("Edit: Inserting file %s \n",string);

if ((fp=fopen(string,"r")) == NULL)
   {
   Verbose("Could not open file %s\n",string);
   return;
   }

while(!feof(fp) && ReadLine(linebuf,CF_BUFSIZE,fp))
   {
   if ((ip = (struct Item *)malloc(sizeof(struct Item))) == NULL)
      {
      CfLog(cferror,"","Can't allocate memory in InsertItemAfter()");
      FatalError("");
      }
   
   if ((sp = malloc(strlen(linebuf)+1)) == NULL)
      {
      CfLog(cferror,"","Can't allocate memory in InsertItemAfter()");
      FatalError("");
      }

   if (CURRENTLINEPTR == NULL)
      {
      if (*filestart == NULL)
         {
         *filestart = ip;
         ip->next = NULL;
         }
      else
         {
         ip->next = (*filestart)->next;
         (*filestart)->next = ip;     
         }
      
      strcpy(sp,linebuf);
      ip->name = sp;
      ip->classes = NULL;
      CURRENTLINEPTR = ip;
      CURRENTLINENUMBER = 1;
      } 
   else
      {
      ip->next = CURRENTLINEPTR->next;
      CURRENTLINEPTR->next = ip;
      CURRENTLINEPTR=ip;
      CURRENTLINENUMBER++;
      strcpy(sp,linebuf);
      ip->name = sp;
      ip->classes = NULL;
      }
   }

NUMBEROFEDITS++;

fclose(fp);
 
return;
}