1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <cf3.defs.h>
26 
27 #include <keyring.h>
28 #include <dir.h>
29 #include <file_lib.h>
30 #include <known_dirs.h>
31 
32 /***************************************************************/
33 
HostKeyAddressUnknown(const char * value)34 bool HostKeyAddressUnknown(const char *value)
35 {
36     if (strcmp(value, "location unknown") == 0)
37     {
38         return true;
39     }
40 
41 // Is there some other non-ip string left over?
42 
43     if (!((strchr(value, '.')) || (strchr(value, ':'))))
44     {
45         return false;
46     }
47 
48     return false;
49 }
50 
51 /***************************************************************/
52 
RemovePublicKey(const char * id)53 int RemovePublicKey(const char *id)
54 {
55     Dir *dirh = NULL;
56     int removed = 0;
57     char keysdir[CF_BUFSIZE];
58     const struct dirent *dirp;
59     char suffix[CF_BUFSIZE];
60 
61     snprintf(keysdir, CF_BUFSIZE, "%s/ppkeys", GetWorkDir());
62 
63     MapName(keysdir);
64 
65     if ((dirh = DirOpen(keysdir)) == NULL)
66     {
67         if (errno == ENOENT)
68         {
69             return 0;
70         }
71         else
72         {
73             Log(LOG_LEVEL_ERR, "Unable to open keys directory at '%s'. (opendir: %s)", keysdir, GetErrorStr());
74             return -1;
75         }
76     }
77 
78     snprintf(suffix, CF_BUFSIZE, "-%s.pub", id);
79 
80     while ((dirp = DirRead(dirh)) != NULL)
81     {
82         char *c = strstr(dirp->d_name, suffix);
83 
84         if (c && c[strlen(suffix)] == '\0')     /* dirp->d_name ends with suffix */
85         {
86             char keyfilename[sizeof(keysdir) + sizeof(dirp->d_name)];
87             // both sizeof's include NUL, so one of those is for the '/'
88 
89             snprintf(keyfilename, sizeof(keyfilename), "%s/%s", keysdir, dirp->d_name);
90             MapName(keyfilename);
91 
92             if (unlink(keyfilename) < 0)
93             {
94                 if (errno != ENOENT)
95                 {
96                     Log(LOG_LEVEL_ERR, "Unable to remove key file '%s'. (unlink: %s)", dirp->d_name, GetErrorStr());
97                     DirClose(dirh);
98                     return -1;
99                 }
100             }
101             else
102             {
103                 removed++;
104             }
105         }
106     }
107 
108     if (errno)
109     {
110         Log(LOG_LEVEL_ERR, "Unable to enumerate files in keys directory. (ReadDir: %s)", GetErrorStr());
111         DirClose(dirh);
112         return -1;
113     }
114 
115     DirClose(dirh);
116     return removed;
117 }
118 /***************************************************************/
119