-
Notifications
You must be signed in to change notification settings - Fork 10
/
hplip-printer-app.c
1854 lines (1634 loc) · 54.9 KB
/
hplip-printer-app.c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// HPLIP (hpcups) Printer Application based on PAPPL and libpappl-retrofit
//
// Copyright © 2020-2021 by Till Kamppeter.
// Copyright © 2020 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers...
//
#define _GNU_SOURCE
#include <pappl-retrofit.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
//
// Constants...
//
// Name and version
#define SYSTEM_NAME "HPLIP Printer Application"
#define SYSTEM_PACKAGE_NAME "hplip-printer-app"
#ifndef SYSTEM_VERSION_STR
# define SYSTEM_VERSION_STR "1.0"
#endif
#ifndef SYSTEM_VERSION_ARR_0
# define SYSTEM_VERSION_ARR_0 1
#endif
#ifndef SYSTEM_VERSION_ARR_1
# define SYSTEM_VERSION_ARR_1 0
#endif
#ifndef SYSTEM_VERSION_ARR_2
# define SYSTEM_VERSION_ARR_2 0
#endif
#ifndef SYSTEM_VERSION_ARR_3
# define SYSTEM_VERSION_ARR_3 0
#endif
#define SYSTEM_WEB_IF_FOOTER "Copyright © 2021 by Till Kamppeter. Provided under the terms of the <a href=\"https://www.apache.org/licenses/LICENSE-2.0\">Apache License 2.0</a>."
// Test page
#define TESTPAGE "testpage.pdf"
// System architecture
#if defined(__x86_64__) || defined(_M_X64)
#define ARCH "x86_64"
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
#define ARCH "x86_32"
#elif defined(__aarch64__) || defined(_M_ARM64) || defined(__ARM_ARCH_ISA_A64)
#define ARCH "arm64"
#elif defined(__arm__) || defined(_M_ARM)
#define ARCH "arm32"
#else
// "grep"ping the binary should not match this
#define ARCH "UNSUPPORTED_ARCH"
#endif
// Plugin download URLs
#define PLUGIN_CONF_URL "http://hplip.sf.net/plugin.conf"
#define PLUGIN_ALT_LOCATION "https://developers.hp.com/sites/default/files"
//
// Types...
//
typedef enum hplip_plugin_status_e
{
HPLIP_PLUGIN_NOT_INSTALLED = 0,
HPLIP_PLUGIN_OUTDATED,
HPLIP_PLUGIN_INSTALLED
} hplip_plugin_status_t;
//
// Functions...
//
//
// 'get_config_value()' - Return the value of a given variable/key in
// a given section of a config file
//
char *
get_config_value(FILE *fp,
const char *section,
const char *key)
{
char line[1024];
char *value = NULL;
int in_section = 0;
if (!key || !key[0])
return (NULL);
rewind(fp);
while (fgets(line, sizeof(line), fp))
{
while (line[strlen(line) - 1] == '\n' ||
line[strlen(line) - 1] == '\r') // Remove newline
line[strlen(line) - 1] = '\0';
if (line[0] == '[')
{
if (section && !strncasecmp(line + 1, section, strlen(section)) &&
line[strlen(section) + 1] == ']')
in_section = 1;
else
in_section = 0;
}
else if ((!section || in_section) &&
!strncasecmp(line, key, strlen(key)))
{
value = line + strlen(key);
while (*value && isspace(*value)) value ++;
if (*value == '=')
{
value ++;
while (*value && isspace(*value)) value ++;
if (*value)
{
value = strdup(value);
break;
}
else
value = NULL;
}
else
value = NULL;
}
}
return (value);
}
//
// 'set_config_value()' - Set a new value for a given variable/key in
// a given section of a config file. Create the
// section/key if not yet present.
//
int
set_config_value(char **filebuf,
const char *section,
const char *key,
const char *value)
{
char line[1024];
int section_found = 0,
line_changed = 0,
file_changed = 0,
done = 0;
char *filebufptr, *buf, *bufptr, *lineptr, *lineendptr;
size_t size_needed,
bytes_written;
if (!filebuf || !key || !key[0])
return (0);
size_needed = (*filebuf ? strlen(*filebuf) : 0) +
(section ? strlen(section) : 0) +
strlen(key) +
(value ? strlen(value) : 0) + 8;
// Create a buffer for the whole file plus the new entry
buf = (char *)calloc(size_needed, sizeof(char));
bufptr = buf;
// Read the lines of the file to find the point where to apply the change
// and put the lines including the change into the buffer
if (*filebuf)
{
filebufptr = *filebuf;
while (*filebufptr)
{
// Read one line from input buffer
for (line[0] = '\0', lineptr = line;
*filebufptr && *filebufptr != '\n';
filebufptr++, lineptr ++)
*lineptr = *filebufptr;
*lineptr = '\n';
lineptr ++;
*lineptr = '\0';
if (*filebufptr) filebufptr ++;
line_changed = 0;
if (strlen(line) > 0 && !done)
{
// We have not yet set the requested value, still searching the
// right place in the file ...
if (line[0] == '[')
{
// New section
if (section && !strncasecmp(line + 1, section, strlen(section)) &&
line[strlen(section) + 1] == ']')
// Start of requested section
section_found = 1;
else if (section_found && !done && value)
{
// Requested section has ended and new setting not yet written,
// Create new line at end of section
bytes_written = sprintf(bufptr, "%s = %s\n", key, value);
bufptr += bytes_written;
file_changed = 1;
done = 1;
}
}
else if ((!section || section_found) &&
!strncasecmp(line, key, strlen(key)))
{
// Line begins with the name of the key we want to change, continue
// parsing
lineptr = line + strlen(key);
while (*lineptr && isspace(*lineptr)) lineptr ++;
if (*lineptr == '=' || *lineptr == '\n' || *lineptr == '\r' ||
*lineptr == '\0')
{
// The line actually sets our key, right after the key name is
// an '=' ot the line end
if (value) // value == NULL removes the key in the section
{
// We have a valiue, so we change the key's value to hours
if (*lineptr == '=')
{
// Find start of value in line
lineptr ++;
while (*lineptr && isspace(*lineptr)) lineptr ++;
}
// Find end of value in line
lineendptr = lineptr;
while (*lineendptr && *lineendptr != '\n' && *lineendptr != '\r')
lineendptr ++;
if (strlen(value) != lineendptr - lineptr ||
strncmp(value, lineptr, lineendptr - lineptr))
{
// Value differs from the current one, only then write the
// line with the value replaced by hours
sprintf(bufptr, "%s", line);
bufptr += (lineptr - line);
bytes_written = sprintf(bufptr, "%s%s", value, lineendptr);
bufptr += bytes_written;
line_changed = 1;
file_changed = 1;
}
}
else
{
// Value is NULL, meaning that we want to remove the key, so
// mark the line as changed without writing it
line_changed = 1;
file_changed = 1;
}
done = 1;
}
}
}
if (!line_changed)
{
// No change needed on original line, write it as it is
bytes_written = sprintf(bufptr, "%s", line);
bufptr += bytes_written;
}
}
}
if (!done && value)
{
// Requested section or requested key in section not found, create
// the section and the line in it
if (section && !section_found)
{
// Write section line
bytes_written = sprintf(bufptr, "[%s]\n", section);
bufptr += bytes_written;
}
// Write key=value line
bytes_written = sprintf(bufptr, "%s = %s\n", key, value);
bufptr += bytes_written;
file_changed = 1;
}
*bufptr = '\0';
if (file_changed)
{
// File has changed, replace the input buffer by the output buffer
if (*filebuf)
free(*filebuf);
*filebuf = buf;
}
else
free(buf);
return (file_changed);
}
//
// 'hplip_version()' - Read out the HPLIP version from /etc/hp/hplip.conf
//
char *
hplip_version(pappl_system_t *system)
{
char buf[1024];
FILE *fp;
char *version = NULL;
snprintf(buf, sizeof(buf), "%s/%s", HPLIP_CONF_DIR, "hplip.conf");
if ((fp = fopen(buf, "r")) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to open HPLIP configuration file %s", buf);
return (NULL);
}
version = get_config_value(fp, "hplip", "version");
fclose(fp);
return (version);
}
//
// 'hplip_plugin_status()' - Read out the status of the installed p;lugin
// from /var/lib/hp/hplip.state
//
hplip_plugin_status_t
hplip_plugin_status(pappl_system_t *system)
{
char buf[1024];
FILE *fp;
char *plugin_version,
*installed_status,
*version;
hplip_plugin_status_t status = HPLIP_PLUGIN_NOT_INSTALLED;
snprintf(buf, sizeof(buf), "%s/%s", HPLIP_PLUGIN_STATE_DIR, "hplip.state");
if ((fp = fopen(buf, "r")) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to open HPLIP plugin status file %s", buf);
return (status);
}
// HACK FOR TESTING: If empty hplip.state is created, install the plugin
// also if it was not installed before
fseek(fp, 0L, SEEK_END);
if (ftell(fp) == 0)
{
fclose(fp);
return (HPLIP_PLUGIN_OUTDATED);
}
plugin_version = get_config_value(fp, "plugin", "version");
installed_status = get_config_value(fp, "plugin", "installed");
if (installed_status && atoi(installed_status) != 0 &&
plugin_version && plugin_version[0] &&
(version = hplip_version(system)) != NULL)
{
if (!strcasecmp(plugin_version, version))
status = HPLIP_PLUGIN_INSTALLED;
else
status = HPLIP_PLUGIN_OUTDATED;
free(version);
}
if (plugin_version)
free(plugin_version);
if (installed_status)
free(installed_status);
fclose(fp);
return (status);
}
//
// 'hplip_download_file() - Download a file from a given URL to a local
// temporary file
//
char*
hplip_download_file(pappl_system_t *system, const char *url)
{
int fd, status;
char tempfile[1024] = "";
CURL *curl;
FILE *fp = NULL;
CURLcode ret;
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Downloading %s", url);
// Setup curl
curl = curl_easy_init();
if (curl)
{
// Create a temporary file
fd = cupsTempFd(tempfile, sizeof(tempfile));
if (fd < 0 || !tempfile[0])
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to create temporary file");
return (NULL);
}
// Download the file
fp = fdopen(fd, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 10L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
ret = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
close(fd);
}
else
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to start curl");
return (NULL);
}
// Check for errors
if ((int)ret == 0)
return(strdup(tempfile));
else
{
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Download status: %d", ret);
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to download file %s", url);
unlink(tempfile);
}
return (NULL);
}
//
// 'hplip_run_command_line()' - Run a command line and log its screen output,
// both stdout and stderr. Return the exit code
// of the command.
//
int
hplip_run_command_line(pappl_system_t *system, const char *command)
{
FILE *fp;
char buf[1024];
if ((fp = popen(command, "r")) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to start \"gpg\" utility fo plugin signature verification. Command: %s",
command);
return (-1);
}
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Executing command: %s", command);
while (fgets(buf, sizeof(buf), fp) != NULL)
{
buf[strlen(buf) - 1] = '\0'; // Remove newline
papplLog(system, PAPPL_LOGLEVEL_DEBUG, " %s", buf);
}
return (pclose(fp));
}
//
// 'hplip_get_uncompress_dir() - Determine (and create) the directory
// where the plugin file should be
// uncompressed after download
//
char *
hplip_get_uncompress_dir(pappl_system_t *system, int create)
{
#ifdef HPLIP_PLUGIN_ALT_DIR
(void)system;
(void)create;
return strdup(HPLIP_PLUGIN_ALT_DIR);
#else
char buf[1024],
*home;
if ((home = getenv("HOME")) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"No HOME environment variable defined.");
return (NULL);
}
snprintf(buf, sizeof(buf), "%s/.hplip", home);
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Directory to uncompress the plugin in: %s", buf);
if (create)
{
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Creating directory for uncompressed plugin %s", buf);
if (mkdir(buf, S_IRWXU) == -1)
{
if (errno != EEXIST)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Error creating directory %s: %s", buf, strerror(errno));
return (NULL);
}
}
}
return (strdup(buf));
#endif // HPLIP_PLUGIN_ALT_DIR
}
//
// 'hplip_remove_uncompress_dir() - Remove the uncompressed plugin
// file when we do not need it any more
//
int
hplip_remove_uncompress_dir(pappl_system_t *system, const char *name)
{
char *uncompress_dir;
char buf[1024];
DIR *d;
struct dirent *entry;
char *filename;
int len;
// Find the directory where the uncompressed plugin is located
if ((uncompress_dir = hplip_get_uncompress_dir(system, 0)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not determine the directory with the uncompressed plugin file.");
return (0);
}
// Open the directory with the files of the uncompressed plugin
len = snprintf(buf, sizeof(buf), "%s/%s", uncompress_dir, name);
if ((d = opendir(buf)) == NULL && errno != ENOENT)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not open the directory %s: %s", buf, strerror(errno));
free(uncompress_dir);
return (0);
}
// Remove the files of the plugin (only regular files and symlinks, no
// sub-directories)
if (d)
{
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Removing uncompressed plugin directory %s", buf);
while (entry = readdir(d))
{
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
snprintf(buf + len, sizeof(buf) - len, "/%s", entry->d_name);
if (unlink(buf) != 0)
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not remove file %s: %s", buf, strerror(errno));
}
closedir(d);
// Remove the emptied directory
buf[len] = '\0';
if (rmdir(buf) != 0)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not remove directory %s: %s", buf, strerror(errno));
free(uncompress_dir);
return (0);
}
}
#ifndef HPLIP_PLUGIN_ALT_DIR
// Try to remove the directory in which we had uncompressed the
// plugin, but ignore errors (for example if it contains something
// else, then we most probably did not create it in the first place)
rmdir(uncompress_dir);
if (errno == 0)
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Removed directory for uncompressed plugin %s", uncompress_dir);
#endif // !HPLIP_PLUGIN_ALT_DIR
free(uncompress_dir);
return (1);
}
//
// 'hplip_download_plugin()' - Download the plugin from the HP's official
// locations, validate, and uncompress it
//
char *
hplip_download_plugin(pappl_system_t *system)
{
int i;
char *plugin_conf = NULL,
*plugin_file = NULL,
*signature_file = NULL,
*version = NULL,
*url = NULL,
*size_str = NULL,
*checksum = NULL,
*uncompress_dir = NULL,
*ret = NULL;
FILE *fp;
size_t plugin_size;
char buf[1024];
struct stat st;
int fd;
int bytes;
SHA_CTX ctx;
unsigned char hash[SHA_DIGEST_LENGTH];
int status;
// Get plugin index file from HP
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Getting plugin index from HP ...");
if ((plugin_conf = hplip_download_file(system, PLUGIN_CONF_URL)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to download plugin index");
goto out;
}
// HPLIP version which we have installed, equals section name in the plugin
// index
if ((version = hplip_version(system)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to determine version of HPLIP");
goto out;
}
// Open downloaded plugin index
if ((fp = fopen(plugin_conf, "r")) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to open downloaded plugin index file");
goto out;
}
// Read needed data items: URL, size, checksum
if ((url = get_config_value(fp, version, "url")) == NULL || !url[0])
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not find plugin URL in index file. Is HPLIP %s already released?",
version);
goto out;
}
if ((size_str = get_config_value(fp, version, "size")) == NULL ||
!size_str[0] || (plugin_size = atoi(size_str)) <= 0)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not find plugin size in index file.");
goto out;
}
if ((checksum = get_config_value(fp, version, "checksum")) == NULL ||
!checksum[0])
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not find plugin checksum in index file.");
goto out;
}
fclose(fp);
// Download the plugin file
buf[0] = '\0';
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Getting plugin file for HPLIP %s from official location ...",
version);
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Trying URL: %s", url);
if ((plugin_file = hplip_download_file(system, url)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Unable to download plugin file, trying backup server");
snprintf(buf, sizeof(buf) - 5, "%s/hplip-%s-plugin.run",
PLUGIN_ALT_LOCATION, version);
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Trying URL: %s", buf);
if ((plugin_file = hplip_download_file(system, buf)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to download plugin file.");
goto out;
}
}
// Download the plugin signature file
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Getting plugin signature file from the same location as the plugin file ...");
if (buf[0])
strcpy(buf + strlen(buf), ".asc");
else
snprintf(buf, sizeof(buf), "%s.asc", url);
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Using URL: %s", buf);
if ((signature_file = hplip_download_file(system, buf)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to download plugin signature file.");
goto out;
}
// Determine size of the downloaded plugin
stat(plugin_file, &st);
if (st.st_size != plugin_size)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Downloaded plugin file is not of expected size. File has %ld bytes but expected are %ld bytes.",
st.st_size, plugin_size);
goto out;
}
else
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Downloaded file size OK (%ld).", st.st_size);
// Verify sha1sum of the plugin file against the checksum from the index file
SHA1_Init(&ctx);
fd = open(plugin_file, O_RDONLY);
while ((bytes = read(fd, buf, sizeof(buf))) > 0)
SHA1_Update(&ctx, buf, bytes);
close(fd);
SHA1_Final(hash, &ctx);
for (i = 0; i < SHA_DIGEST_LENGTH; i ++)
snprintf(buf + 2 * i, 3, "%.2x", hash[i]);
buf[2 * SHA_DIGEST_LENGTH] = '\0';
if (strcasecmp(buf, checksum))
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Checksum of the plugin file (%s) does not match checksum of the plugin index (%s).",
buf, checksum);
goto out;
}
else
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Downloaded file checksum OK (%s).", buf);
// Check GPG signature
// We do not load HP's public key here (which is needed ofr verification), as
// HP's original command does not work.
// Command as of HPLIP 3.21.8:
// gpg --homedir ~ --no-permission-warning --keyserver pgp.mit.edu --recv-keys 0x4ABA2F66DBD5A95894910E0673D770CDA59047B9
// So the actual command depends on the source code in use and how it got
// patched. For Debian/Ubuntu it is (key got added to the package):
// gpg --homedir ~ --no-permission-warning --import /usr/share/hplip/signing-key.asc
// Please add a suitable command to the start-up script for this Printer Application
snprintf(buf, sizeof(buf),
"gpg --homedir ~ --no-permission-warning --verify %s %s 2>&1",
signature_file, plugin_file);
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Verifying plugin's signature.");
if ((status = hplip_run_command_line(system, buf)) != 0)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Plugin signature verification failed.");
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"NOTE: If this failure is due to a missing public key, we do not load the");
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"public key here as this step can vary with the used HPLIP source code.");
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"The command used in the original source code of HPLIP (3.21.8)");
papplLog(system, PAPPL_LOGLEVEL_ERROR,
" gpg --homedir ~ --no-permission-warning --keyserver pgp.mit.edu --recv-keys 0x4ABA2F66DBD5A95894910E0673D770CDA59047B9");
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"does not work.");
goto out;
}
// Get the directory where to uncompress the plugin
if ((uncompress_dir = hplip_get_uncompress_dir(system, 1)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Could not find/create the directory for uncompressing the plugin.");
goto out;
}
// Make sure we have the directory name "plugin_tmp" free for uncompressing
// our plugin
if (!hplip_remove_uncompress_dir(system, "plugin_tmp"))
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Old uncompressed plugin file is in the way: %s/plugin_tmp",
uncompress_dir);
goto out;
}
// Uncompress the plugin
snprintf(buf, sizeof(buf),
"cd %s 2>&1 && mkdir plugin_tmp 2>&1 && cd plugin_tmp 2>&1 && sh %s --tar xf --no-same-owner 2>&1 && cd .. 2>&1 && chmod -R go+rX plugin_tmp 2>&1",
uncompress_dir, plugin_file);
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Uncompressing the plugin file.");
if ((status = hplip_run_command_line(system, buf)) != 0)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to uncompress the plugin");
goto out;
}
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Downloaded, verified, and uncompressed the plugin successfully. Ready to install in %s/plugin_tmp",
uncompress_dir);
// Output the uncompress location
ret = uncompress_dir;
out:
// Clean up
if (plugin_conf)
{
unlink(plugin_conf);
free(plugin_conf);
}
if (signature_file)
{
unlink(signature_file);
free(signature_file);
}
if (version)
free(version);
if (url)
free(url);
if (size_str)
free(size_str);
if (checksum)
free(checksum);
if (plugin_file)
{
unlink(plugin_file);
free(plugin_file);
}
if (ret == NULL && uncompress_dir)
{
hplip_remove_uncompress_dir(system, "plugin_tmp");
free(uncompress_dir);
}
return (ret);
}
#ifdef SNAP
//
// 'hplip_register_plugin() - Register the plugin installation or
// removal in the hplip,state file (in the
// Snap only).
//
int
hplip_register_plugin(pappl_system_t *system, const char *installed,
const char *eula, const char *version)
{
int ret = 0;
char buf[1024];
char *filebuf = NULL;
int size_needed;
FILE *fp;
// Register installation or removal of plugin in hplip.state
// Open current file, if present
snprintf(buf, sizeof(buf), "%s/%s", HPLIP_PLUGIN_STATE_DIR, "hplip.state");
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Registering plugin installation status in %s", buf);
if ((fp = fopen(buf, "r")) == NULL && errno != ENOENT)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to open HPLIP plugin status file %s: %s",
buf, strerror(errno));
goto out;
}
if (fp)
{
// Load complete file into a buffer (if we have a state file)
fseek(fp, 0L, SEEK_END);
size_needed = ftell(fp);
filebuf = (char *)calloc(size_needed + 1, sizeof(char));
rewind(fp);
if (fread(filebuf, 1, size_needed, fp) != size_needed)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to read HPLIP plugin status file %s: %s",
buf, strerror(errno));
fclose(fp);
goto out;
}
fclose(fp);
filebuf[size_needed] = '\0';
}
// Modify the values in the buffer
if (set_config_value(&filebuf, "plugin", "installed", installed) +
set_config_value(&filebuf, "plugin", "eula", eula) +
set_config_value(&filebuf, "plugin", "version", version) > 0)
{
// File has changed, rewrite it
// Remove the old file
if (unlink(buf) != 0 && errno != ENOENT)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to delete old HPLIP plugin status file %s: %s",
buf, strerror(errno));
free(filebuf);
goto out;
}
// Write new file
if ((fp = fopen(buf, "w")) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to open HPLIP plugin status file %s: %s",
buf, strerror(errno));
free(filebuf);
goto out;
}
if (fwrite(filebuf, 1, strlen(filebuf), fp) != strlen(filebuf))
{
papplLog(system, PAPPL_LOGLEVEL_ERROR,
"Unable to write to HPLIP plugin status file %s: %s",
buf, strerror(errno));
free(filebuf);
fclose(fp);
goto out;
}
fclose(fp);
}
// Done
papplLog(system, PAPPL_LOGLEVEL_DEBUG,
"Registered plugin installation status successfully.");
ret = 1;
out:
return (ret);
}
#endif // SNAP
//
// 'hplip_install_plugin() - Install the downloaded plugin, after the
// license got accepted in the web
// interface, or right after uncompressing
// during an update. Use HP's Python script