1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2013-2018 Todd C. Miller <Todd.Miller@sudo.ws>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22  */
23 
24 #include <config.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 
33 #include "sudoers.h"
34 #include "sudo_digest.h"
35 
36 unsigned char *
sudo_filedigest(int fd,const char * file,int digest_type,size_t * digest_len)37 sudo_filedigest(int fd, const char *file, int digest_type, size_t *digest_len)
38 {
39     unsigned char *file_digest = NULL;
40     unsigned char buf[32 * 1024];
41     struct sudo_digest *dig = NULL;
42     FILE *fp = NULL;
43     size_t nread;
44     int fd2;
45     debug_decl(sudo_filedigest, SUDOERS_DEBUG_UTIL);
46 
47     *digest_len = sudo_digest_getlen(digest_type);
48     if (*digest_len == (size_t)-1) {
49 	sudo_warnx(U_("unsupported digest type %d for %s"), digest_type, file);
50 	goto bad;
51     }
52 
53     if ((dig = sudo_digest_alloc(digest_type)) == NULL) {
54 	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
55 	goto bad;
56     }
57 
58     if ((fd2 = dup(fd)) == -1) {
59 	sudo_debug_printf(SUDO_DEBUG_INFO, "unable to dup %s: %s",
60 	    file, strerror(errno));
61 	goto bad;
62     }
63     if ((fp = fdopen(fd2, "r")) == NULL) {
64 	sudo_debug_printf(SUDO_DEBUG_INFO, "unable to fdopen %s: %s",
65 	    file, strerror(errno));
66 	close(fd2);
67 	goto bad;
68     }
69     if ((file_digest = malloc(*digest_len)) == NULL) {
70 	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
71 	goto bad;
72     }
73 
74     while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0) {
75 	sudo_digest_update(dig, buf, nread);
76     }
77     if (ferror(fp)) {
78 	sudo_warnx(U_("%s: read error"), file);
79 	goto bad;
80     }
81     sudo_digest_final(dig, file_digest);
82     sudo_digest_free(dig);
83     fclose(fp);
84 
85     debug_return_ptr(file_digest);
86 bad:
87     sudo_digest_free(dig);
88     free(file_digest);
89     if (fp != NULL)
90 	fclose(fp);
91     debug_return_ptr(NULL);
92 }
93