Esempio n. 1
0
static void cmd_touch( int cmdc, char *cmdv[COMMAND_WORD_NUM] )
{
  file_touch( cmdv[1]);
}
Esempio n. 2
0
int main ( int argc , char *argv[] )
{
	int now , a , b , c , d ;
	struct stat sb_dir ;
	struct stat sb_file ;
	char *filename , *ip1 , *ip2 , *ip3 , *ip4 ;
	char ipbuf[16] ;

	read_env() ;

	if ( JGREYLIST_LOG_PID )
		snprintf ( title , 31 , "jgreylist[%d]: " , getpid() ) ;

	/***************************************
	* make sure we have a command to run
	*  if we decide to allow the connection
	*/

	if ( argc < 2 )
		die ( "no command specified on command line" ) ;

	/***************************************
	* make sure we have an IP address to check
	*/

	if ( ! ( TCPREMOTEIP && *TCPREMOTEIP ) )
		die ( "TCPREMOTEIP is empty or missing" ) ;

	/***************************************
	* if JGREYLIST is set, either approve or deny automatically
	*/

	if ( JGREYLIST )
	{
		if ( *JGREYLIST )
		{
			if ( JGREYLIST_LOG )
				logit2 ( TCPREMOTEIP , ": DENY blacklisted" ) ;
			fake ( 0 , JGREYLIST ) ;
		}
		if ( JGREYLIST_LOG )
			logit2 ( TCPREMOTEIP , ": OK whitelisted" ) ;
		okay ( argc , argv ) ;
	}

	if ( JGREYLIST_NOREV && *JGREYLIST_NOREV )
		if ( ! ( TCPREMOTEHOST && *TCPREMOTEHOST ) )
		{
			if ( JGREYLIST_LOG )
				logit2 ( TCPREMOTEIP , ": DENY no reverse DNS" ) ;
			fake ( 0 , JGREYLIST_NOREV ) ;
		}

	/***************************************
	* make sure the JGREYLIST_DIR directory exists and is writable
	*/

	if ( ! ( JGREYLIST_DIR && *JGREYLIST_DIR ) )	/* defined */
		die ( "JGREYLIST_DIR is not set" ) ;

	if ( stat ( JGREYLIST_DIR , &sb_dir ) )		/* exists */
		pdie ( JGREYLIST_DIR ) ;

	if ( ! ( sb_dir.st_mode & S_IFDIR ) )		/* is a directory */
		die2 ( JGREYLIST_DIR , " is not a directory" ) ;

	if ( access ( JGREYLIST_DIR , R_OK | W_OK | X_OK ) )
		pdie ( JGREYLIST_DIR ) ;

	/***************************************
	* copy the IP, break it into four separate segments
	*/

	strncpy ( ipbuf , TCPREMOTEIP , 16 ) ;
	ipbuf[15] = '\0' ; /* just in case */
	ip1 = ip2 = ipbuf ;
	while ( *ip2 && *ip2 != '.' ) ip2++ ;
	if ( *ip2 ) *ip2++ = '\0' ;
	ip3 = ip2 ;
	while ( *ip3 && *ip3 != '.' ) ip3++ ;
	if ( *ip3 ) *ip3++ = '\0' ;
	ip4 = ip3 ;
	while ( *ip4 && *ip4 != '.' ) ip4++ ;
	if ( *ip4 ) *ip4++ = '\0' ;

	/***************************************
	* change "1.2.3.4" to "001.002.003.004"
	*/

	a = atoi ( ip1 ) ;
	b = atoi ( ip2 ) ;
	c = atoi ( ip3 ) ;
	d = atoi ( ip4 ) ;

	snprintf ( ipbuf , 16 , "%03d.%03d.%03d.%03d" , a , b , c , d ) ;

	/***************************************
	* break it into separate strings for each octet
	*/

	ipbuf[3] = ipbuf[7] = ipbuf[11] = '\0' ;
	ip2 = &(ipbuf[4]) ;
	ip3 = &(ipbuf[8]) ;
	ip4 = &(ipbuf[12]) ;

	/***************************************
	* walk into the directory structure, creating directories as we go.
	* then reassemble the portions of the IP address which will become
	* the actual filename within the directory, and build the final
	* filename whose timestamps we need.
	*/

	filename = malloc ( strlen ( JGREYLIST_DIR ) + 32 ) ;
	if ( ! filename )
		die ( "out of memory" ) ;

	strcpy ( filename , JGREYLIST_DIR ) ;
	strcat ( filename , "/" ) ;
	strcat ( filename , ip1 ) ;
	if ( mkdir ( filename , 0700 ) && errno != EEXIST )
		pdie ( filename ) ;
	strcat ( filename , "/" ) ;
	strcat ( filename , ip2 ) ;
	if ( mkdir ( filename , 0700 ) && errno != EEXIST )
		pdie ( filename ) ;
	strcat ( filename , "/" ) ;
	strcat ( filename , ip3 ) ;

	if ( JGREYLIST_BY_IP )
	{
		if ( mkdir ( filename , 0700 ) && errno != EEXIST )
			pdie ( filename ) ;
		strcat ( filename , "/" ) ;
		strcat ( filename , ip4 ) ;
	}

	/***************************************
	* try to get the file's timestamps
	* if not exist, create it and fake conversation first time
	* if other error, die
	*/

	if ( stat ( filename , &sb_file ) )
	{
		if ( errno == ENOENT )
		{
			if ( JGREYLIST_LOG )
				logit2 ( TCPREMOTEIP , ": GREY first time" ) ;
			file_create ( filename ) ;
			fake ( 1 , GREY1 ) ;
		}
		pdie ( filename ) ;
	}

	/***************************************
	* is it too soon to let them in?
	*/

	now = time ( 0 ) ;

	if ( ( sb_file.st_mtime + JGREYLIST_HOLDTIME ) > now )
	{
		if ( JGREYLIST_LOG )
			if ( JGREYLIST_LOG )
				logit2 ( TCPREMOTEIP , ": GREY too soon" ) ;
		file_touch ( filename , now ) ;
		fake ( 2 , GREY2 ) ;
	}

	/***************************************
	* they're okay
	*/

	if ( JGREYLIST_LOG )
		logit2 ( TCPREMOTEIP , ": OK known" ) ;
	file_touch ( filename , now ) ;
	okay ( argc , argv ) ;

	return 0 ;
}
Esempio n. 3
0
File: z.c Progetto: Keidan/tk
/**
 * @fn int z_compress(z_t zip, const z_file_t zname, const char* password, z_clevel_et level, _Bool append, _Bool exclude_path, fifo_t files)
 * @brief Creation of a new ZIP file.
 * @param zip The ZIP context.
 * @param zname The zip file name.
 * @param password the zip password else NULL or empty.
 * @param level The compression level.
 * @param append Append mode.
 * @param exclude_path Exclude the file path.
 * @param files The file list.
 * @retunr 0 on success else -1.
 */
int z_compress(z_t zip, const z_file_t zname, const char* password, z_clevel_et level, _Bool append, _Bool exclude_path, fifo_t files) {
  struct z_s* z = Z_CAST(zip);
  z_file_t filename_try;
  int size_buf = 0;
  void* buf = NULL;
  zipFile zf;

  size_buf = Z_WRITE_BUFFER_SIZE;
  buf = (void*)malloc(size_buf);
  if (!buf) {
    logger(LOG_ERR, "Error allocating memory\n");
    return -1;
  }
  bzero(filename_try, sizeof(z_file_t));
  strcpy(filename_try, zname);
  if(!string_indexof(filename_try, ".") == -1)
    strcat(filename_try, ".zip");

  zf = zipOpen64(filename_try, (append) ? 2 : 0);
  if (!zf) {
    free(buf);
    logger(LOG_ERR, "Error opening %s\n", filename_try);
    return -1;
  } else
    logger(LOG_DEBUG, "Creating %s\n", filename_try);
  
  while(!fifo_empty(files)) {
     const char* filenameinzip = fifo_pop(files);
     FILE * fin;
     int size_read;
     const char *savefilenameinzip;
     zip_fileinfo zi;
     unsigned long crc_file = 0;
     int zip64 = 0;
     memset(&zi, 0, sizeof(zip_fileinfo));
     if(file_is_dir(filenameinzip)) {
       ((char*)filenameinzip)[strlen(filenameinzip)] = z->dir_delimiter;
       strncat((char*)filenameinzip, ".empty", sizeof(file_name_t));
       file_touch(filenameinzip);
     }
     logger(LOG_DEBUG, "Trying to add file '%s'\n", filenameinzip);
     file_time(filenameinzip, (struct tm*)&zi.tmz_date);

     if(password != NULL && strlen(password))
       if(z_get_file_crc(filenameinzip, buf, size_buf, &crc_file) != ZIP_OK) {
	 zipClose(zf, NULL);
	 free(buf);
	 logger(LOG_ERR, "Error getting the crc for the file %s\n", filenameinzip);
	 return -1;
       }

     zip64 = file_is_large_file(filenameinzip);
     /* The path name saved, should not include a leading slash. */
     /*if it did, windows/xp and dynazip couldn't read the zip file. */
     savefilenameinzip = filenameinzip;
     while(savefilenameinzip[0] == z->dir_delimiter)
       savefilenameinzip++;

     /*should the zip file contain any path at all?*/
     if(exclude_path) {
       const char *tmpptr;
       const char *lastslash = 0;
       for(tmpptr = savefilenameinzip; *tmpptr; tmpptr++) {
	 if(*tmpptr == z->dir_delimiter)
	   lastslash = tmpptr;
       }
       if(lastslash)
	 savefilenameinzip = lastslash+1; // base filename follows last slash.
     }

     if(zipOpenNewFileInZip3_64(zf, savefilenameinzip, &zi,
				NULL, 0, NULL, 0, NULL /* comment*/,
				(level != 0) ? Z_DEFLATED : 0, level,0,
				-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
				(password != NULL && strlen(password)) ? password : NULL, crc_file, zip64) != ZIP_OK) {
	 zipClose(zf, NULL);
	 free(buf);
	 logger(LOG_ERR, "Error in opening %s in zipfile\n", filenameinzip);
	 return -1;
     }

     fin = fopen64(filenameinzip, "rb");
     if(!fin) {
       zipCloseFileInZip(zf);
       zipClose(zf, NULL);
       free(buf);
       logger(LOG_ERR, "Error in opening %s for reading\n", filenameinzip);
       return -1;
     }
     do {
       size_read = (int)fread(buf,1,size_buf,fin);
       if(size_read < size_buf)
	 if(!feof(fin)) {
	   logger(LOG_ERR, "Error in reading %s\n",filenameinzip);
	   break;
	 }

       if (size_read > 0) {
	 if(zipWriteInFileInZip(zf, buf, size_read) < 0)  {
	   logger(LOG_ERR, "Error in writing %s in the zipfile\n", filenameinzip);
	   break;
	 }
       }
     } while(size_read > 0);

     if(fin) fclose(fin);
     if(zipCloseFileInZip(zf) != ZIP_OK) {
       logger(LOG_ERR, "Error in closing %s in the zipfile\n", filenameinzip);
       break;
     }
  }
  if(zipClose(zf, NULL) != ZIP_OK)
    logger(LOG_ERR, "Error in closing %s\n",filename_try);
  free(buf);

  return 0;
}