summaryrefslogtreecommitdiffstats
path: root/tools/asn2deb
blob: 926d34e2e5993decfe7cfd2ef1e1328e1129909d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3

# asn2deb - quick hack by W. Borgert <debacle@debian.org> to create
# Debian GNU/Linux packages from ASN.1 files for Wireshark.
# Copyright 2004, W. Borgert

# ASN.1 module for Wireshark, use of snacc type table:
# Copyright 2003, Matthijs Melchior <matthijs.melchior@xs4all.nl>
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.com>
# Copyright 1998 Gerald Combs

# SPDX-License-Identifier: GPL-2.0-or-later

import getopt, os, string, sys, time

scriptinfo = """asn2deb version 2004-02-17
Copyright 2004, W. Borgert
Free software, released under the terms of the GPL."""

options = {'asn':      None,
           'dbopts':   "",
           'email':    "invalid@invalid.invalid",
           'help':     0,
           'name':     "No Name",
           'preserve': 0,
           'version':  0}

def create_file(filename, content, mode = None):
    """Create a file with given content."""
    global options
    if options['preserve'] and os.path.isfile(filename):
        return
    f = open(filename, 'w')
    f.write(content)
    f.close()
    if mode:
        os.chmod(filename, mode)

def create_files(version, deb, email, asn, name, iso, rfc):
    """Create all files for the .deb build process."""
    base = asn.lower()[:-5]

    if not os.path.isdir("packaging/debian"):
        os.mkdir("packaging/debian")

    create_file("packaging/debian/rules", """#!/usr/bin/make -f

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

PREFIX=`pwd`/packaging/debian/wireshark-asn1-%s

binary-post-install/wireshark-asn1-%s::
	rm -f $(PREFIX)/usr/lib/wireshark/plugins/%s/*.a
""" % (base, base, version), 0o755)

    create_file("packaging/debian/control", """Source: wireshark-asn1-%s
Section: net
Priority: optional
Maintainer: %s <%s>
Standards-Version: 3.6.1.0
Build-Depends: snacc, autotools-dev, debhelper, cdbs

Package: wireshark-asn1-%s
Architecture: all
Depends: wireshark (= %s)
Description: ASN.1/BER dissector for %s
 This package provides a type table for decoding BER (Basic Encoding
 Rules) data over TCP or UDP, described by an ASN.1 (Abstract Syntax
 Notation 1) file '%s.asn1'.
""" % (base, name, email, base, deb, base, base))

    create_file("packaging/debian/changelog",
            """wireshark-asn1-%s (0.0.1-1) unstable; urgency=low

  * Automatically created package.

 -- %s <%s>  %s
""" % (base, name, email, rfc + "\n    (" + iso + ")"))

    create_file("packaging/debian/copyright",
            """This package has been created automatically be asn2deb on
%s for Debian GNU/Linux.

Wireshark: https://www.wireshark.com/

Copyright:

GPL, as evidenced by existence of GPL license file \"COPYING\".
(the GNU GPL may be viewed on Debian systems in
/usr/share/common-licenses/GPL)
""" % (iso))

def get_wrs_version():
    """Detect version of wireshark-dev package."""
    deb = os.popen(
        "dpkg-query -W --showformat='${Version}' wireshark-dev").read()
    debv = string.find(deb, "-")
    if debv == -1: debv = len(deb)
    version = deb[string.find(deb, ":")+1:debv]
    return version, deb

def get_time():
    """Detect current time and return ISO and RFC time string."""
    currenttime = time.gmtime()
    return time.strftime("%Y-%m-%d %H:%M:%S +0000", currenttime), \
           time.strftime("%a, %d %b %Y %H:%M:%S +0000", currenttime)

def main():
    global options
    process_opts(sys.argv)
    iso, rfc = get_time()
    version, deb = get_wrs_version()
    create_files(version, deb,
                 options['email'], options['asn'], options['name'],
                 iso, rfc)
    os.system("dpkg-buildpackage " + options['dbopts'])

def process_opts(argv):
    """Process command line options."""
    global options
    try:
        opts, args = getopt.getopt(argv[1:], "a:d:e:hn:pv",
                                   ["asn=",
                                    "dbopts=",
                                    "email=",
                                    "help",
                                    "name=",
                                    "preserve",
                                    "version"])
    except getopt.GetoptError:
        usage(argv[0])
        sys.exit(1)
    for o, a in opts:
        if o in ("-a", "--asn"):
            options['asn'] = a
        if o in ("-d", "--dbopts"):
            options['dbopts'] = a
        if o in ("-e", "--email"):
            options['email'] = a
        if o in ("-h", "--help"):
            options['help'] = 1
        if o in ("-n", "--name"):
            options['name'] = a
        if o in ("-p", "--preserve"):
            options['preserve'] = 1
        if o in ("-v", "--version"):
            options['version'] = 1
    if options['help']:
        usage(argv[0])
        sys.exit(0)
    if options['version']:
        print(scriptinfo)
        sys.exit(0)
    if not options['asn']:
        print("mandatory ASN.1 file parameter missing")
        sys.exit(1)
    if not os.access(options['asn'], os.R_OK):
        print("ASN.1 file not accessible")
        sys.exit(1)

def usage(name):
    """Print usage help."""
    print("Usage: " + name + " <parameters>\n" + \
          "Parameters are\n" + \
          "  --asn      -a asn1file, ASN.1 file to use (mandatory)\n" + \
          "  --dbopts   -d opts,     options for dpkg-buildpackage\n" + \
          "  --email    -e address,  use e-mail address\n" + \
          "  --help     -h,          print help and exit\n" + \
          "  --name     -n name,     use user name\n" + \
          "  --preserve -p,          do not overwrite files\n" + \
          "  --version  -v,          print version and exit\n" + \
          "Example:\n" + \
          name + " -e me@foo.net -a bar.asn1 -n \"My Name\" " + \
          "-d \"-rfakeroot -uc -us\"")
if __name__ == '__main__':
    main()