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
180
181
182
183
184
185
|
# Copyright (C) 2010, 2011 SPARTA, Inc. dba Cobham Analytic Solutions
# Copyright (C) 2012 SPARTA, Inc. a Parsons Company
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
__version__ = '$Id$'
from django import forms
from rpki import resource_set
from rpki.gui.app import models
from rpki.exceptions import BadIPResource
class AddConfForm(forms.Form):
handle = forms.CharField(required=True,
help_text='your handle for your rpki instance')
run_rpkid = forms.BooleanField(required=False, initial=True,
label='Run rpkid?',
help_text='do you want to run your own instance of rpkid?')
rpkid_server_host = forms.CharField(initial='rpkid.example.org',
label='rpkid hostname',
help_text='publicly visible hostname for your rpkid instance')
rpkid_server_port = forms.IntegerField(initial=4404,
label='rpkid port')
run_pubd = forms.BooleanField(required=False, initial=False,
label='Run pubd?',
help_text='do you want to run your own instance of pubd?')
pubd_server_host = forms.CharField(initial='pubd.example.org',
label='pubd hostname',
help_text='publicly visible hostname for your pubd instance')
pubd_server_port = forms.IntegerField(initial=4402, label='pubd port')
pubd_contact_info = forms.CharField(initial='repo-man@rpki.example.org',
label='Pubd contact',
help_text='email address for the operator of your pubd instance')
class GhostbusterRequestForm(forms.ModelForm):
"""
Generate a ModelForm with the subset of parents for the current
resource handle.
"""
# override default form field
parent = forms.ModelChoiceField(queryset=None, required=False,
help_text='Specify specific parent, or none for all parents')
# override full_name. it is required in the db schema, but we allow the
# user to skip it and default from family+given name
full_name = forms.CharField(max_length=40, required=False,
help_text='automatically generated from family and given names if left blank')
def __init__(self, issuer, *args, **kwargs):
super(GhostbusterRequestForm, self).__init__(*args, **kwargs)
self.fields['parent'].queryset = models.Parent.objects.filter(issuer=issuer)
class Meta:
model = models.GhostbusterRequest
exclude = ('issuer', 'vcard')
def clean(self):
family_name = self.cleaned_data.get('family_name')
given_name = self.cleaned_data.get('given_name')
if not all([family_name, given_name]):
raise forms.ValidationError, 'Family and Given names must be specified'
email = self.cleaned_data.get('email_address')
postal = self.cleaned_data.get('postal_address')
telephone = self.cleaned_data.get('telephone')
if not any([email, postal, telephone]):
raise forms.ValidationError, 'One of telephone, email or postal address must be specified'
# if the full name is not specified, default to given+family
fn = self.cleaned_data.get('full_name')
if not fn:
self.cleaned_data['full_name'] = '%s %s' % (given_name, family_name)
return self.cleaned_data
class ImportForm(forms.Form):
"""Form used for uploading parent/child identity xml files."""
handle = forms.CharField(required=False,
widget=forms.TextInput(attrs={'class': 'xlarge'}),
help_text='Optional. Your name for this entity, or blank to accept name in XML')
xml = forms.FileField(label='XML file',
widget=forms.FileInput(attrs={'class': 'input-file'}))
class ImportRepositoryForm(forms.Form):
handle = forms.CharField(max_length=30, required=False,
label='Parent Handle',
help_text='Optional. Must be specified if you use a different name for this parent')
xml = forms.FileField(label='XML file',
widget=forms.FileInput(attrs={'class': 'input-file'}))
class ImportClientForm(forms.Form):
"""Form used for importing publication client requests."""
xml = forms.FileField(label='XML file',
widget=forms.FileInput(attrs={'class': 'input-file'}))
def ChildWizardForm(parent, *args, **kwargs):
class wrapped(forms.Form):
handle = forms.CharField(max_length=30, help_text='handle for new child')
#create_user = forms.BooleanField(help_text='create a new user account for this handle?')
#password = forms.CharField(widget=forms.PasswordInput, help_text='password for new user', required=False)
#password2 = forms.CharField(widget=forms.PasswordInput, help_text='repeat password', required=False)
def clean_handle(self):
if parent.children.filter(handle=self.cleaned_data['handle']):
raise forms.ValidationError, 'a child with that handle already exists'
return self.cleaned_data['handle']
return wrapped(*args, **kwargs)
class ROARequest(forms.Form):
"""Form for entering a ROA request.
Handles both IPv4 and IPv6."""
asn = forms.IntegerField(label='AS')
prefix = forms.CharField(max_length=50)
max_prefixlen = forms.CharField(required=False,
label='Max Prefix Length')
confirmed = forms.BooleanField(widget=forms.HiddenInput, required=False)
def _as_resource_range(self):
prefix = self.cleaned_data.get('prefix')
try:
r = resource_set.resource_range_ipv4.parse_str(prefix)
except BadIPResource:
r = resource_set.resource_range_ipv6.parse_str(prefix)
return r
def clean_asn(self):
value = self.cleaned_data.get('asn')
if value < 0:
raise forms.ValidationError, 'AS must be a positive value or 0'
return value
def clean_prefix(self):
try:
r = self._as_resource_range()
except:
raise forms.ValidationError, 'invalid IP address'
return str(r)
def clean_max_prefixlen(self):
v = self.cleaned_data.get('max_prefixlen')
if v:
if v[0] == '/':
v = v[1:] # allow user to specify /24
if int(v) < 0:
raise forms.ValidationError, \
'max prefix length must be positive or 0'
return v
def clean(self):
if 'prefix' in self.cleaned_data:
r = self._as_resource_range()
max_prefixlen = self.cleaned_data.get('max_prefixlen')
max_prefixlen = int(max_prefixlen) if max_prefixlen else r.prefixlen()
if max_prefixlen < r.prefixlen():
raise forms.ValidationError, \
'max prefix length must be greater than or equal to the prefix length'
if max_prefixlen > r.datum_type.bits:
raise forms.ValidationError, \
'max prefix length (%d) is out of range for IP version (%d)' % (max_prefixlen, r.datum_type.bits)
self.cleaned_data['max_prefixlen'] = str(max_prefixlen)
return self.cleaned_data
# vim:sw=4 ts=8 expandtab
|