aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Woodward2003-09-29 00:10:27 +0000
committerJason Woodward2003-09-29 00:10:27 +0000
commit53de9c49a29e4c84e22152a58d62195af314a92b (patch)
treedb1719e6bf91ae0b0ba6469b4444e3303558d328
parentd4f7267bfe72c6fbc81c22ec9d9c1e38b6c19cf9 (diff)
downloadslapt-get-53de9c49a29e4c84e22152a58d62195af314a92b.tar.gz
added md5 checking
-rw-r--r--ChangeLog6
-rw-r--r--Makefile4
-rw-r--r--TODO2
-rw-r--r--src/action.c9
-rw-r--r--src/configuration.c41
-rw-r--r--src/configuration.h1
-rw-r--r--src/curl.c78
-rw-r--r--src/curl.h1
-rw-r--r--src/main.h2
-rw-r--r--src/package.c75
-rw-r--r--src/package.h4
11 files changed, 211 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index 65b860c..3f5c4c8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,10 @@
+0.9.4 Sep 29, 2003
+ * downloads CHECKSUMS.md5 from mirror
+ * md5 sum's each package after downloading (from CHECKSUMS.md5 data)
+ utilizing openssl/evp.h
+ * pre-download check... won't download twice if md5sum is good
+
0.9.3 Sep 24, 2003
* default make target doesn't build slack pkg
* --dist-upgrade becomes target instead of option
diff --git a/Makefile b/Makefile
index 983ef88..d425a36 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
PROGRAM_NAME=slapt-get
-VERSION=0.9.3
+VERSION=0.9.4
CC=gcc
CURLFLAGS=`curl-config --libs`
OBJS=src/configuration.o src/package.o src/curl.o src/action.o src/main.o
@@ -7,7 +7,7 @@ RCDEST=/etc/slapt-getrc
RCSOURCE=example.slapt-getrc
SBINDIR=/sbin/
DEFINES=-DPROGRAM_NAME="\"$(PROGRAM_NAME)\"" -DVERSION="\"$(VERSION)\"" -DRC_LOCATION="\"$(RCDEST)\""
-CFLAGS=-W -Werror -Wall -O2 -ansi -pedantic -Iinclude $(DEFINES)
+CFLAGS=-W -Werror -Wall -g -ansi -pedantic -Iinclude $(DEFINES)
default: $(PROGRAM_NAME)
diff --git a/TODO b/TODO
index fc0c3e4..ec81177 100644
--- a/TODO
+++ b/TODO
@@ -4,8 +4,8 @@ Future enhancements, fixes, or features:
* fix broken pkg description (someone who knows what they are doing help)
* find memory leaks
- * file check before downloading pkg (don't download twice, incorp md5sum checking maybe)
* source pkg handling (maybe)
* dependancy tracking (ideally that will happen in PACKAGES.TXT)
* multiple sources/mirrors (merge package data into one local PACKAGES.TXT)
+ * optimize
diff --git a/src/action.c b/src/action.c
index 055f131..643bfde 100644
--- a/src/action.c
+++ b/src/action.c
@@ -186,6 +186,7 @@ void pkg_action_show(const char *pkg_name){
void pkg_action_update(const rc_config *global_config){
FILE *pkg_list_fh;
FILE *patches_list_fh;
+ FILE *checksum_list_fh;
/* download our PKG_LIST */
pkg_list_fh = download_pkg_list(global_config);
@@ -195,6 +196,10 @@ void pkg_action_update(const rc_config *global_config){
patches_list_fh = download_patches_list(global_config);
fclose(patches_list_fh);
+ /* download */
+ checksum_list_fh = download_checksum_list(global_config);
+ fclose(checksum_list_fh);
+
/* source listing to go here */
return;
@@ -217,7 +222,6 @@ void pkg_action_upgrade(const rc_config *global_config,pkg_info_t *installed_pkg
if( cmp_result < 0 ){ /* update_pkg is newer than installed_pkg */
if( (upgrade_pkg(global_config,update_pkg)) == -1 ){
fprintf(stderr,"Failed to update %s.\n",installed_pkg->name);
- exit(1);
}
}else{
if( cmp_result > 0 ){
@@ -230,7 +234,6 @@ void pkg_action_upgrade(const rc_config *global_config,pkg_info_t *installed_pkg
if( strcmp(installed_pkg->version,available_pkg->version) < 0 ){
if( (upgrade_pkg(global_config,available_pkg)) == -1 ){
fprintf(stderr,"Failed to update %s.\n",installed_pkg->name);
- exit(1);
}
}else{
printf("%s is already the newest version.\n",installed_pkg->name);
@@ -274,7 +277,6 @@ void pkg_action_upgrade_all(const rc_config *global_config){
/* attempt to upgrade */
if( (upgrade_pkg(global_config,update_pkg)) == -1 ){
fprintf(stderr,"Failed to update %s.\n",installed_pkgs->pkgs[iterator]->name);
- exit(1);
}/* end upgrade attempt */
}else{
@@ -301,7 +303,6 @@ void pkg_action_upgrade_all(const rc_config *global_config){
"Failed to update %s.\n",
installed_pkgs->pkgs[iterator]->name
);
- exit(1);
}/* end upgrade attempt */
}/* end if strcmp */
}/* end if current_pkg */
diff --git a/src/configuration.c b/src/configuration.c
index 6d8007d..7d29af8 100644
--- a/src/configuration.c
+++ b/src/configuration.c
@@ -324,3 +324,44 @@ void create_dir_structure(const char *dir_name){
free(cwd);
}
+void gen_md5_sum_of_file(FILE *f,char *result_sum){
+ EVP_MD_CTX mdctx;
+ const EVP_MD *md;
+ unsigned char md_value[EVP_MAX_MD_SIZE];
+ int md_len, i;
+ ssize_t getline_read;
+ size_t getline_size;
+ char *result_sum_tmp = NULL;
+ char *getline_buffer = NULL;
+
+ md = EVP_md5();
+
+ EVP_MD_CTX_init(&mdctx);
+ EVP_DigestInit_ex(&mdctx, md, NULL);
+
+ rewind(f);
+
+ while( (getline_read = getline(&getline_buffer, &getline_size, f)) != EOF )
+ EVP_DigestUpdate(&mdctx, getline_buffer, getline_read);
+
+ free(getline_buffer);
+
+ EVP_DigestFinal_ex(&mdctx, md_value, (unsigned int*)&md_len);
+ EVP_MD_CTX_cleanup(&mdctx);
+
+ result_sum[0] = '\0';
+
+ for(i = 0; i < md_len; i++){
+ char *p = malloc( 3 );
+
+ if( snprintf(p,3,"%02x",md_value[i]) > 0 ){
+
+ if( (result_sum_tmp = strncat(result_sum,p,3)) != NULL )
+ result_sum = result_sum_tmp;
+
+ }
+
+ free(p);
+ }
+
+}
diff --git a/src/configuration.h b/src/configuration.h
index 7f8355f..cf30dd6 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -45,3 +45,4 @@ char spinner(void);
void clean_pkg_dir(const char *);
struct exclude_list *parse_exclude(char *);
void create_dir_structure(const char *);
+void gen_md5_sum_of_file(FILE *,char *);
diff --git a/src/curl.c b/src/curl.c
index df05181..733eeea 100644
--- a/src/curl.c
+++ b/src/curl.c
@@ -108,10 +108,49 @@ FILE *download_patches_list(const rc_config *global_config){
return fh;
}
+FILE *download_checksum_list(const rc_config *global_config){
+ FILE *fh = NULL;
+ char *url = NULL;
+
+ fh = open_file(CHECKSUM_FILE,"w+");
+
+#if USE_CURL_PROGRESS == 0
+ printf("Retrieving checksum list...");
+#else
+ printf("Retrieving checksum list...\n");
+#endif
+
+ url = calloc(
+ strlen(global_config->mirror_url) + strlen(CHECKSUM_FILE) + 1 , sizeof *url
+ );
+ if( url == NULL ){
+ fprintf(stderr,"Failed to calloc url\n");
+ exit(1);
+ }
+
+ strncpy(url,global_config->mirror_url,strlen(global_config->mirror_url) );
+ strncat(url,CHECKSUM_FILE,strlen(CHECKSUM_FILE) );
+ download_data(fh,url);
+#if USE_CURL_PROGRESS == 0
+ printf("Done\n");
+#endif
+
+ free(url);
+ rewind(fh); /* make sure we are back at the front of the file */
+ return fh;
+}
+
char *download_pkg(const rc_config *global_config,pkg_info_t *pkg){
FILE *fh = NULL;
+ FILE *fh_test = NULL;
char *file_name = NULL;
char *url = NULL;
+ char *md5_sum = NULL;
+ char *md5_sum_of_file = NULL;
+
+ md5_sum = malloc(34);
+ md5_sum_of_file = malloc(34);
+ get_md5sum(global_config,pkg,md5_sum);
/* build the file name */
file_name = calloc(
@@ -127,13 +166,21 @@ char *download_pkg(const rc_config *global_config,pkg_info_t *pkg){
file_name = strncat(file_name,pkg->version,strlen(pkg->version));
file_name = strncat(file_name,".tgz",strlen(".tgz"));
- fh = open_file(file_name,"wb");
-
/*
* TODO: here we will use the md5sum (or some other means)
* to see if the file is already present and valid
*/
-
+ if( ( fh_test = fopen(file_name,"r") ) != NULL){
+ /* check to see if the md5sum is correct */
+ gen_md5_sum_of_file(fh_test,md5_sum_of_file);
+ fclose(fh_test);
+ if( strcmp(md5_sum_of_file,md5_sum) == 0 ){
+ printf("Using cached copy of %s\n",pkg->name);
+ free(md5_sum);
+ free(md5_sum_of_file);
+ return file_name;
+ }
+ }
/* build the url */
url = calloc(
@@ -154,15 +201,38 @@ char *download_pkg(const rc_config *global_config,pkg_info_t *pkg){
#else
printf("Downloading %s...\n",pkg->name);
#endif
+
+ fh = open_file(file_name,"wb");
download_data(fh,url);
+
#if USE_CURL_PROGRESS == 0
printf("Done\n");
#endif
+ fclose(fh);
+
+ /* check to see if the md5sum is correct */
+ fh = open_file(file_name,"r");
+ gen_md5_sum_of_file(fh,md5_sum_of_file);
+ fclose(fh);
+ printf("verifying %s md5 sum...",pkg->name);
+ if( strcmp(md5_sum_of_file,md5_sum) != 0 ){
+ fprintf(stderr,"md5 sum for %s is not correct!\n",pkg->name);
+#if DEBUG == 1
+ fprintf(stderr,"MD5 found: [%s]\n",md5_sum_of_file);
+ fprintf(stderr,"MD5 expected: [%s]\n",md5_sum);
+ fprintf(stderr,"File: %s/%s\n",global_config->working_dir,file_name);
+#endif
+ return NULL;
+ }else{
+ printf("Done\n");
+ }
+ free(md5_sum);
+ free(md5_sum_of_file);
+
if( url != NULL ){
free(url);
}
- fclose(fh);
return file_name;
}
diff --git a/src/curl.h b/src/curl.h
index aa249f1..1a986f9 100644
--- a/src/curl.h
+++ b/src/curl.h
@@ -23,5 +23,6 @@ void download_data(FILE *,const char *);
FILE *download_pkg_list(const rc_config *);
FILE *download_file_list(const rc_config *);
FILE *download_patches_list(const rc_config *);
+FILE *download_checksum_list(const rc_config *global_config);
char *download_pkg(const rc_config *,pkg_info_t *);
int progress_callback(void *,double,double,double,double);
diff --git a/src/main.h b/src/main.h
index 2ab54f5..f9be1af 100644
--- a/src/main.h
+++ b/src/main.h
@@ -32,6 +32,7 @@
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
+#include <openssl/evp.h>
/* */
/* local includes */
@@ -58,4 +59,5 @@ void version_info(void);
#define REMOVE_CMD "/sbin/removepkg "
#define INSTALL_CMD "/sbin/installpkg "
#define UPGRADE_CMD "/sbin/upgradepkg "
+#define CHECKSUM_FILE "CHECKSUMS.md5"
diff --git a/src/package.c b/src/package.c
index 0c69655..35bcc53 100644
--- a/src/package.c
+++ b/src/package.c
@@ -440,6 +440,10 @@ int install_pkg(const rc_config *global_config,pkg_info_t *pkg){
chdir(pkg->location);
pkg_file_name = download_pkg(global_config,pkg);
+ if( pkg_file_name == NULL ){
+ fprintf(stderr,"Skipping %s\n",pkg->name);
+ return -1;
+ }
/* build and execute our command */
command = calloc( strlen(INSTALL_CMD) + strlen(pkg_file_name) + 1 , sizeof *command );
@@ -486,6 +490,10 @@ int upgrade_pkg(const rc_config *global_config,pkg_info_t *pkg){
/* download it */
pkg_file_name = download_pkg(global_config,pkg);
+ if( pkg_file_name == NULL ){
+ fprintf(stderr,"Skipping %s\n",pkg->name);
+ return -1;
+ }
/* build and execute our command */
command = calloc( strlen(UPGRADE_CMD) + strlen(pkg_file_name) + 1 , sizeof *command );
@@ -566,3 +574,70 @@ int is_excluded(const rc_config *global_config,const char *pkg_name){
return 0;
}
+void get_md5sum(const rc_config *global_config,pkg_info_t *pkg,char *md5_sum){
+ FILE *checksum_file;
+ regex_t md5sum_regex;
+ size_t nmatch = 10;
+ regmatch_t pmatch[10];
+ int regexec_return;
+ ssize_t getline_read;
+ size_t getline_len = 0;
+ char *getline_buffer = NULL;
+ char *cwd = NULL;
+
+ cwd = getcwd(NULL,0);
+
+ chdir(global_config->working_dir);
+
+ checksum_file = open_file(CHECKSUM_FILE,"r");
+
+ regcomp(&md5sum_regex,MD5SUM_REGEX, REG_EXTENDED|REG_NEWLINE);
+
+ while( (getline_read = getline(&getline_buffer,&getline_len,checksum_file) ) != EOF ){
+
+ if( strstr(getline_buffer,".tgz") == NULL) continue;
+ if((strstr(getline_buffer,"slackware")==NULL) && (strstr(getline_buffer,"patch")==NULL)){
+ continue;
+ }
+
+ regexec_return = regexec( &md5sum_regex, getline_buffer, nmatch, pmatch, 0);
+ if( regexec_return == 0 ){
+ char name[50];
+ char version[50];
+ char sum[34];
+
+ strncpy(
+ name,
+ getline_buffer + pmatch[3].rm_so,
+ pmatch[3].rm_eo - pmatch[3].rm_so
+ );
+ name[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';
+
+ strncpy(
+ version,
+ getline_buffer + pmatch[4].rm_so,
+ pmatch[4].rm_eo - pmatch[4].rm_so
+ );
+ version[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';
+
+ strncpy(
+ sum,
+ getline_buffer + pmatch[1].rm_so,
+ pmatch[1].rm_eo - pmatch[1].rm_so
+ );
+ sum[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';
+
+ if( (strcmp(pkg->name,name) == 0) && (strcmp(pkg->version,version) == 0) ){
+ memcpy(md5_sum,sum,pmatch[1].rm_eo - pmatch[1].rm_so + 1);
+ break;
+ }
+
+ }
+ }
+ fclose(checksum_file);
+ chdir(cwd);
+ free(cwd);
+
+ return;
+}
+
diff --git a/src/package.h b/src/package.h
index daf6b1c..175359a 100644
--- a/src/package.h
+++ b/src/package.h
@@ -26,6 +26,7 @@
#define PKG_SIZEU_PATTERN "^PACKAGE SIZE [(]+uncompressed[)]+: ([0-9]+) K$"
#define PKG_LOG_DIR "/var/log/packages"
#define PKG_LOG_PATTERN "^([a-zA-Z0-9-+_]+)-([a-zA-Z0-9-._]+)$"
+#define MD5SUM_REGEX "([a-zA-Z0-9]+)[ ]+([a-zA-Z0-9-._/]+/)([a-zA-Z0-9-+_]+)-([a-zA-Z0-9-._]+).tgz$"
/*
* VARIABLE DEFINITIONS
@@ -67,4 +68,5 @@ int remove_pkg(pkg_info_t *);
void free_pkg_list(struct pkg_list *);
/* exclude pkg based on pkg name */
int is_excluded(const rc_config *,const char *);
-
+/* lookup md5sum of file */
+void get_md5sum(const rc_config *,pkg_info_t *,char *);