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 <mod_methods.h>
26 
27 #include <syntax.h>
28 #include <policy.h>
29 #include <string_lib.h>
30 #include <fncall.h>
31 #include <rlist.h>
32 #include <class.h>
33 
34 static const char *const POLICY_ERROR_METHODS_BUNDLE_ARITY =
35     "Conflicting arity in calling bundle %s, expected %d arguments, %d given";
36 
37 static const ConstraintSyntax CF_METHOD_BODIES[] =
38 {
39     ConstraintSyntaxNewBool("inherit", "If true this causes the sub-bundle to inherit the private classes of its parent", SYNTAX_STATUS_NORMAL),
40     ConstraintSyntaxNewBundle("usebundle", "Specify the name of a bundle to run as a parameterized method", SYNTAX_STATUS_NORMAL),
41     ConstraintSyntaxNewString("useresult", CF_IDRANGE, "Specify the name of a local variable to contain any result/return value from the child", SYNTAX_STATUS_NORMAL),
42     ConstraintSyntaxNewNull()
43 };
44 
MethodsParseTreeCheck(const Promise * pp,Seq * errors)45 static bool MethodsParseTreeCheck(const Promise *pp, Seq *errors)
46 {
47     bool success = true;
48 
49     for (size_t i = 0; i < SeqLength(pp->conlist); i++)
50     {
51         const Constraint *cp = SeqAt(pp->conlist, i);
52 
53         // ensure: if call and callee are resolved, then they have matching arity
54         if (StringEqual(cp->lval, "usebundle"))
55         {
56             if (cp->rval.type == RVAL_TYPE_FNCALL)
57             {
58                 // HACK: exploiting the fact that class-references and call-references are similar
59                 FnCall *call = RvalFnCallValue(cp->rval);
60                 ClassRef ref = ClassRefParse(call->name);
61                 if (!ClassRefIsQualified(ref))
62                 {
63                     ClassRefQualify(&ref, PromiseGetNamespace(pp));
64                 }
65 
66                 const Bundle *callee = PolicyGetBundle(PolicyFromPromise(pp), ref.ns, "agent", ref.name);
67                 if (!callee)
68                 {
69                     callee = PolicyGetBundle(PolicyFromPromise(pp), ref.ns, "common", ref.name);
70                 }
71 
72                 ClassRefDestroy(ref);
73 
74                 if (callee)
75                 {
76                     if (RlistLen(call->args) != RlistLen(callee->args))
77                     {
78                         SeqAppend(errors, PolicyErrorNew(POLICY_ELEMENT_TYPE_CONSTRAINT, cp,
79                                                          POLICY_ERROR_METHODS_BUNDLE_ARITY,
80                                                          call->name, RlistLen(callee->args), RlistLen(call->args)));
81                         success = false;
82                     }
83                 }
84             }
85         }
86     }
87     return success;
88 }
89 
90 const PromiseTypeSyntax CF_METHOD_PROMISE_TYPES[] =
91 {
92     PromiseTypeSyntaxNew("agent", "methods", CF_METHOD_BODIES, &MethodsParseTreeCheck, SYNTAX_STATUS_NORMAL),
93     PromiseTypeSyntaxNewNull()
94 };
95