opkg: Fix handling of sha256sums for conffiles
[openwrt.git] / package / system / opkg / patches / 230-drop_md5_support.patch
1 --- a/libopkg/conffile.c
2 +++ b/libopkg/conffile.c
3 @@ -36,7 +36,7 @@ void conffile_deinit(conffile_t *conffil
4  
5  int conffile_has_been_modified(conffile_t *conffile)
6  {
7 -    char *md5sum;
8 +    char *chksum;
9      char *filename = conffile->name;
10      char *root_filename;
11      int ret = 1;
12 @@ -48,16 +48,23 @@ int conffile_has_been_modified(conffile_
13  
14      root_filename = root_filename_alloc(filename);
15  
16 -    md5sum = file_md5sum_alloc(root_filename);
17 -
18 -    if (md5sum && (ret = strcmp(md5sum, conffile->value))) {
19 -        opkg_msg(INFO, "Conffile %s:\n\told md5=%s\n\tnew md5=%s\n",
20 -               conffile->name, md5sum, conffile->value);
21 +#ifdef HAVE_MD5
22 +    if(conffile->value && strlen(conffile->value) > 33) {
23 +        chksum = file_sha256sum_alloc(root_filename);
24 +    } else {
25 +        chksum = file_md5sum_alloc(root_filename);
26 +    }
27 +#else
28 +    chksum = file_sha256sum_alloc(root_filename);
29 +#endif
30 +    if (chksum && (ret = strcmp(chksum, conffile->value))) {
31 +        opkg_msg(INFO, "Conffile %s:\n\told chk=%s\n\tnew chk=%s\n",
32 +               conffile->name, chksum, conffile->value);
33      }
34  
35      free(root_filename);
36 -    if (md5sum)
37 -        free(md5sum);
38 +    if (chksum)
39 +        free(chksum);
40  
41      return ret;
42  }
43 --- a/libopkg/file_util.c
44 +++ b/libopkg/file_util.c
45 @@ -26,7 +26,9 @@
46  
47  #include "sprintf_alloc.h"
48  #include "file_util.h"
49 +#ifdef HAVE_MD5
50  #include "md5.h"
51 +#endif
52  #include "libbb/libbb.h"
53  
54  #if defined HAVE_SHA256
55 @@ -135,6 +137,7 @@ file_mkdir_hier(const char *path, long m
56         return make_directory(path, mode, FILEUTILS_RECUR);
57  }
58  
59 +#ifdef HAVE_MD5
60  char *file_md5sum_alloc(const char *file_name)
61  {
62      static const int md5sum_bin_len = 16;
63 @@ -180,6 +183,7 @@ char *file_md5sum_alloc(const char *file
64  
65      return md5sum_hex;
66  }
67 +#endif
68  
69  #ifdef HAVE_SHA256
70  char *file_sha256sum_alloc(const char *file_name)
71 --- a/libopkg/opkg_install.c
72 +++ b/libopkg/opkg_install.c
73 @@ -1082,7 +1082,7 @@ resolve_conffiles(pkg_t *pkg)
74       conffile_list_elt_t *iter;
75       conffile_t *cf;
76       char *cf_backup;
77 -     char *md5sum;
78 +     char *chksum;
79  
80       if (conf->noaction) return 0;
81  
82 @@ -1093,7 +1093,7 @@ resolve_conffiles(pkg_t *pkg)
83  
84           /* Might need to initialize the md5sum for each conffile */
85           if (cf->value == NULL) {
86 -              cf->value = file_md5sum_alloc(root_filename);
87 +              cf->value = file_sha256sum_alloc(root_filename);
88           }
89  
90           if (!file_exists(root_filename)) {
91 @@ -1105,8 +1105,16 @@ resolve_conffiles(pkg_t *pkg)
92  
93            if (file_exists(cf_backup)) {
94                /* Let's compute md5 to test if files are changed */
95 -              md5sum = file_md5sum_alloc(cf_backup);
96 -              if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
97 +#ifdef HAVE_MD5
98 +              if(cf->value && strlen(cf->value) > 33) {
99 +                  chksum = file_sha256sum_alloc(cf_backup);
100 +              } else {
101 +                  chksum = file_md5sum_alloc(cf_backup);
102 +              }
103 +#else
104 +              chksum = file_sha256sum_alloc(cf_backup);
105 +#endif
106 +              if (chksum && cf->value && strcmp(cf->value,chksum) != 0 ) {
107                    if (conf->force_maintainer) {
108                        opkg_msg(NOTICE, "Conffile %s using maintainer's setting.\n",
109                                       cf_backup);
110 @@ -1123,8 +1131,8 @@ resolve_conffiles(pkg_t *pkg)
111                   }
112                }
113                unlink(cf_backup);
114 -             if (md5sum)
115 -                  free(md5sum);
116 +             if (chksum)
117 +                  free(chksum);
118            }
119  
120           free(cf_backup);
121 @@ -1323,6 +1331,7 @@ opkg_install_pkg(pkg_t *pkg, int from_up
122       }
123       #endif
124  
125 +#ifdef HAVE_MD5
126       /* Check for md5 values */
127       if (pkg->md5sum)
128       {
129 @@ -1346,6 +1355,7 @@ opkg_install_pkg(pkg_t *pkg, int from_up
130          if (file_md5)
131                free(file_md5);
132       }
133 +#endif
134  
135  #ifdef HAVE_SHA256
136       /* Check for sha256 value */
137 --- a/libopkg/Makefile.am
138 +++ b/libopkg/Makefile.am
139 @@ -25,13 +25,16 @@ opkg_list_sources = conffile.c conffile.
140                     pkg_src.c pkg_src.h pkg_src_list.c pkg_src_list.h \
141                     str_list.c str_list.h void_list.c void_list.h \
142                     active_list.c active_list.h list.h 
143 -opkg_util_sources = file_util.c file_util.h opkg_message.h opkg_message.c md5.c md5.h \
144 +opkg_util_sources = file_util.c file_util.h opkg_message.h opkg_message.c \
145                     parse_util.c parse_util.h \
146                     sprintf_alloc.c sprintf_alloc.h \
147                     xregex.c xregex.h xsystem.c xsystem.h
148  if HAVE_PATHFINDER
149  opkg_util_sources += opkg_pathfinder.c opkg_pathfinder.h
150  endif
151 +if HAVE_MD5
152 +opkg_util_sources += md5.c md5.h
153 +endif
154  if HAVE_SHA256
155  opkg_util_sources += sha256.c sha256.h
156  endif
157 --- a/configure.ac
158 +++ b/configure.ac
159 @@ -68,10 +68,19 @@ AC_ARG_ENABLE(sha256,
160        (sha256.{c,h} are GPLv3 licensed) [[default=no]] ]),
161      [want_sha256="$enableval"], [want_sha256="no"])
162  
163 +AC_ARG_ENABLE(md5,
164 +              AC_HELP_STRING([--enable-md5], [Enable md5sum check
165 +      (md5.{c,h} are GPLv3 licensed) [[default=no]] ]),
166 +    [want_md5="$enableval"], [want_md5="yes"])
167 +
168  if test "x$want_sha256" = "xyes"; then
169    AC_DEFINE(HAVE_SHA256, 1, [Define if you want sha256 support])
170  fi
171 +if test "x$want_md5" = "xyes"; then
172 +  AC_DEFINE(HAVE_MD5, 1, [Define if you want md5 support])
173 +fi
174  AM_CONDITIONAL(HAVE_SHA256, test "x$want_sha256" = "xyes")
175 +AM_CONDITIONAL(HAVE_MD5, test "x$want_md5" = "xyes")
176  
177  # check for openssl
178  AC_ARG_ENABLE(openssl,
179 --- a/libopkg/pkg_parse.c
180 +++ b/libopkg/pkg_parse.c
181 @@ -49,9 +49,9 @@ parse_status(pkg_t *pkg, const char *sst
182  static void
183  parse_conffiles(pkg_t *pkg, const char *cstr)
184  {
185 -       char file_name[1024], md5sum[35];
186 +       char file_name[1024], md5sum[85];
187  
188 -       if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
189 +       if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
190                 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
191                                 pkg->name);
192                 return;