1#!/bin/gawk
2#
3# Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15#    derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28BEGIN {
29  r_uint = "(0|[1-9][0-9]*)"
30  regexp = "^getx?uid" suffix "\\(\\)[[:space:]]+= " r_uint "$"
31  expected = "getuid"
32  fail = 0
33}
34
35regexp == "" {
36  fail = 1
37  next
38}
39
40{
41  if (match($0, regexp, a)) {
42    if (expected == "getuid") {
43      uid = a[1]
44      expected = "setuid"
45      regexp = "^setuid" suffix "\\(" uid "\\)[[:space:]]+= 0$"
46    } else if (expected == "setuid") {
47      expected = "getresuid"
48      regexp = "^getresuid" suffix "\\(\\[" uid "\\], \\[" uid "\\], \\[" uid "\\]\\)[[:space:]]+= 0$"
49    } else if (expected == "getresuid") {
50      expected = "setreuid"
51      regexp = "^setreuid" suffix "\\(-1, -1\\)[[:space:]]+= 0$"
52    } else if (expected == "setreuid") {
53      expected = "setresuid"
54      regexp = "^setresuid" suffix "\\(" uid ", -1, -1\\)[[:space:]]+= 0$"
55    } else if (expected == "setresuid") {
56      expected = "fchown"
57      regexp = "^fchown" suffix "\\(1, -1, -1\\)[[:space:]]+= 0$"
58    } else if (expected == "fchown") {
59      expected = "1st getgroups"
60      regexp = "^getgroups" suffix "\\(0, NULL\\)[[:space:]]+= " r_uint "$"
61    } else if (expected == "1st getgroups") {
62      ngroups = a[1]
63      if (ngroups == "0")
64        list=""
65      else if (ngroups == "1")
66        list=r_uint
67      else
68        list=r_uint "(, " r_uint "){" (ngroups - 1) "}"
69      expected = "2nd getgroups"
70      regexp = "^getgroups" suffix "\\(" ngroups ", \\[" list "\\]\\)[[:space:]]+= " ngroups "$"
71    } else if (expected == "2nd getgroups") {
72      expected = "the last line"
73      regexp = "^\\+\\+\\+ exited with 0 \\+\\+\\+$"
74    } else if (expected == "the last line") {
75      expected = "nothing"
76      regexp = ""
77    }
78  }
79}
80
81END {
82  if (fail) {
83    print "Unexpected output after exit"
84    exit 1
85  }
86  if (regexp == "")
87    exit 0
88  print "error: " expected " doesn't match"
89  exit 1
90}
91