1 /*
2   Copyright 2020 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 <zones.h>
26 #include <string_lib.h>
27 
28 #ifdef HAVE_ZONE_H
29 # include <zone.h>
30 #endif
31 
32 #ifndef __MINGW32__
33 
IsGlobalZone()34 bool IsGlobalZone()
35 {
36 #ifdef HAVE_GETZONEID
37     zoneid_t zid;
38     char zone[ZONENAME_MAX];
39 
40     zid = getzoneid();
41     getzonenamebyid(zid, zone, ZONENAME_MAX);
42 
43     if (strcmp(zone, "global") == 0)
44     {
45         return true;
46     }
47 #endif
48 
49     return false;
50 }
51 
ForeignZone(char * s)52 bool ForeignZone(char *s)
53 {
54 // We want to keep the banner
55 
56     if (strstr(s, "PID"))
57     {
58         return false;
59     }
60 
61 # ifdef HAVE_GETZONEID
62     zoneid_t zid;
63     char *sp, zone[ZONENAME_MAX];
64 
65     zid = getzoneid();
66     getzonenamebyid(zid, zone, ZONENAME_MAX);
67 
68     if (strcmp(zone, "global") == 0)
69     {
70         if (StringStartsWith(s, "global") && isspace(s[6]))
71         {
72 
73             for (sp = s + strlen(s) - 1; isspace(*sp); sp--)
74             {
75                 *sp = '\0';
76             }
77 
78             return false;
79         }
80         else
81         {
82             return true;
83         }
84     }
85 # endif
86     return false;
87 }
88 
89 #ifdef HAVE_GETZONEID
90 #define ZONE_ONLY
91 #else
92 #define ZONE_ONLY ARG_UNUSED
93 #endif
CurrentZoneName(ZONE_ONLY char * s)94 int CurrentZoneName(ZONE_ONLY char *s)
95 {
96 # ifdef HAVE_GETZONEID
97     zoneid_t zid = getzoneid();
98 
99     if (zid >= 0)
100     {
101         return getzonenamebyid(zid, s, ZONENAME_MAX);
102     }
103 # endif
104     return -1;
105 }
106 #endif // !__MINGW32__
107