2 # Generates samba network traffic
4 # Copyright (C) Catalyst IT Ltd. 2017
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from __future__
import print_function
26 sys
.path
.insert(0, "bin/python")
28 from samba
.emulate
import traffic
29 import samba
.getopt
as options
32 def print_err(*args
, **kwargs
):
33 print(*args
, file=sys
.stderr
, **kwargs
)
38 desc
= ("Generates network traffic 'conversations' based on <summary-file>"
39 " (which should the output file produced by either traffic_learner"
40 " or traffic_summary.pl). This traffic is sent to <dns-hostname>,"
41 " which is the full DNS hostname of the DC being tested.")
43 parser
= optparse
.OptionParser(
44 "%prog [--help|options] <summary-file> <dns-hostname>",
47 parser
.add_option('--dns-rate', type='float', default
=0,
48 help='fire extra DNS packets at this rate')
49 parser
.add_option('-B', '--badpassword-frequency',
50 type='float', default
=0.0,
51 help='frequency of connections with bad passwords')
52 parser
.add_option('-K', '--prefer-kerberos',
54 help='prefer kerberos when authenticating test users')
55 parser
.add_option('-I', '--instance-id', type='int', default
=0,
56 help='Instance number, when running multiple instances')
57 parser
.add_option('-t', '--timing-data',
58 help=('write individual message timing data here '
60 parser
.add_option('--preserve-tempdir', default
=False, action
="store_true",
61 help='do not delete temporary files')
62 parser
.add_option('-F', '--fixed-password',
63 type='string', default
=None,
64 help=('Password used for the test users created. '
66 parser
.add_option('-c', '--clean-up',
68 help='Clean up the generated groups and user accounts')
70 model_group
= optparse
.OptionGroup(parser
, 'Traffic Model Options',
71 'These options alter the traffic '
72 'generated when the summary-file is a '
73 'traffic-model (produced by '
75 model_group
.add_option('-S', '--scale-traffic', type='float', default
=1.0,
76 help='Increase the number of conversations by '
78 model_group
.add_option('-D', '--duration', type='float', default
=None,
79 help=('Run model for this long (approx). '
80 'Default 60s for models'))
81 model_group
.add_option('-r', '--replay-rate', type='float', default
=1.0,
82 help='Replay the traffic faster by this factor')
83 model_group
.add_option('--traffic-summary',
84 help=('Generate a traffic summary file and write '
85 'it here (- for stdout)'))
86 parser
.add_option_group(model_group
)
88 user_gen_group
= optparse
.OptionGroup(parser
, 'Generate User Options',
89 "Add extra user/groups on the DC to "
90 "increase the DB size. These extra "
91 "users aren't used for traffic "
93 user_gen_group
.add_option('-G', '--generate-users-only',
95 help='Generate the users, but do not replay '
97 user_gen_group
.add_option('-n', '--number-of-users', type='int', default
=0,
98 help='Total number of test users to create')
99 user_gen_group
.add_option('--number-of-groups', type='int', default
=0,
100 help='Create this many groups')
101 user_gen_group
.add_option('--average-groups-per-user',
102 type='int', default
=0,
103 help='Assign the test users to this '
104 'many groups on average')
105 user_gen_group
.add_option('--group-memberships', type='int', default
=0,
106 help='Total memberships to assign across all '
107 'test users and all groups')
108 parser
.add_option_group(user_gen_group
)
110 sambaopts
= options
.SambaOptions(parser
)
111 parser
.add_option_group(sambaopts
)
112 parser
.add_option_group(options
.VersionOptions(parser
))
113 credopts
= options
.CredentialsOptions(parser
)
114 parser
.add_option_group(credopts
)
116 # the --no-password credential doesn't make sense for this tool
117 if parser
.has_option('-N'):
118 parser
.remove_option('-N')
120 opts
, args
= parser
.parse_args()
122 # First ensure we have reasonable arguments
134 print_err("Removing user and machine accounts")
135 lp
= sambaopts
.get_loadparm()
136 creds
= credopts
.get_credentials(lp
)
137 ldb
= traffic
.openLdb(host
, creds
, lp
)
138 traffic
.clean_up_accounts(ldb
, opts
.instance_id
)
142 if not os
.path
.exists(summary
):
143 print_err("Summary file %s doesn't exist" % summary
)
145 # the summary-file can be ommitted for --generate-users-only and
146 # --cleanup-up, but it should be specified in all other cases
147 elif not opts
.generate_users_only
:
148 print_err("No summary-file specified to replay traffic from")
151 if not opts
.fixed_password
:
152 print_err(("Please use --fixed-password to specify a password"
153 " for the users created as part of this test"))
156 lp
= sambaopts
.get_loadparm()
157 creds
= credopts
.get_credentials(lp
)
159 domain
= opts
.workgroup
161 lp
.set("workgroup", domain
)
163 domain
= lp
.get("workgroup")
164 if domain
== "WORKGROUP":
165 print_err(("NETBIOS domain does not appear to be "
166 "specified, use the --workgroup option"))
169 if not opts
.realm
and not lp
.get('realm'):
170 print_err("Realm not specified, use the --realm option")
173 if opts
.generate_users_only
and not (opts
.number_of_users
or
174 opts
.number_of_groups
):
175 print_err(("Please specify the number of users and/or groups "
179 if opts
.group_memberships
and opts
.average_groups_per_user
:
180 print_err(("--group-memberships and --average-groups-per-user"
181 " are incompatible options - use one or the other"))
184 if not opts
.number_of_groups
and opts
.average_groups_per_user
:
185 print_err(("--average-groups-per-user requires "
186 "--number-of-groups"))
189 if not opts
.number_of_groups
and opts
.group_memberships
:
190 print_err("--group-memberships requires --number-of-groups")
193 if opts
.timing_data
not in ('-', None):
195 open(opts
.timing_data
, 'w').close()
197 print_err(("the supplied timing data destination "
198 "(%s) is not writable" % opts
.timing_data
))
202 if opts
.traffic_summary
not in ('-', None):
204 open(opts
.traffic_summary
, 'w').close()
206 print_err(("the supplied traffic summary destination "
207 "(%s) is not writable" % opts
.traffic_summary
))
211 traffic
.DEBUG_LEVEL
= opts
.debuglevel
213 duration
= opts
.duration
217 # ingest the model or traffic summary
220 conversations
, interval
, duration
, dns_counts
= \
221 traffic
.ingest_summaries([summary
])
223 print_err(("Using conversations from the traffic summary "
226 # honour the specified duration if it's different to the
228 if opts
.duration
is not None:
229 duration
= opts
.duration
231 except ValueError as e
:
232 if not e
.message
.startswith('need more than'):
235 model
= traffic
.TrafficModel()
240 print_err(("Could not parse %s. The summary file "
241 "should be the output from either the "
242 "traffic_summary.pl or "
243 "traffic_learner scripts."
247 print_err(("Using the specified model file to "
248 "generate conversations"))
250 conversations
= model
.generate_conversations(opts
.scale_traffic
,
257 if opts
.debuglevel
> 5:
258 for c
in conversations
:
264 if opts
.number_of_users
and opts
.number_of_users
< len(conversations
):
265 print_err(("--number-of-users (%d) is less than the "
266 "number of conversations to replay (%d)"
267 % (opts
.number_of_users
, len(conversations
))))
270 number_of_users
= max(opts
.number_of_users
, len(conversations
))
271 max_memberships
= number_of_users
* opts
.number_of_groups
273 if not opts
.group_memberships
and opts
.average_groups_per_user
:
274 opts
.group_memberships
= opts
.average_groups_per_user
* number_of_users
275 print_err(("Using %d group-memberships based on %u average "
276 "memberships for %d users"
277 % (opts
.group_memberships
,
278 opts
.average_groups_per_user
, number_of_users
)))
280 if opts
.group_memberships
> max_memberships
:
281 print_err(("The group memberships specified (%d) exceeds "
282 "the total users (%d) * total groups (%d)"
283 % (opts
.group_memberships
, number_of_users
,
284 opts
.number_of_groups
)))
288 ldb
= traffic
.openLdb(host
, creds
, lp
)
290 print_err(("\nInitial LDAP connection failed! Did you supply "
291 "a DNS host name and the correct credentials?"))
294 if opts
.generate_users_only
:
295 traffic
.generate_users_and_groups(ldb
,
298 opts
.number_of_users
,
299 opts
.number_of_groups
,
300 opts
.group_memberships
)
303 tempdir
= tempfile
.mkdtemp(prefix
="samba_tg_")
304 print_err("Using temp dir %s" % tempdir
)
306 traffic
.generate_users_and_groups(ldb
,
310 opts
.number_of_groups
,
311 opts
.group_memberships
)
313 accounts
= traffic
.generate_replay_accounts(ldb
,
318 statsdir
= traffic
.mk_masked_dir(tempdir
, 'stats')
320 if opts
.traffic_summary
:
321 if opts
.traffic_summary
== '-':
322 summary_dest
= sys
.stdout
324 summary_dest
= open(opts
.traffic_summary
, 'w')
326 print_err("Writing traffic summary")
328 for c
in conversations
:
329 summaries
+= c
.replay_as_summary_lines()
332 for (time
, line
) in summaries
:
333 print(line
, file=summary_dest
)
337 traffic
.replay(conversations
, host
,
341 dns_rate
=opts
.dns_rate
,
343 badpassword_frequency
=opts
.badpassword_frequency
,
344 prefer_kerberos
=opts
.prefer_kerberos
,
347 base_dn
=ldb
.domain_dn(),
348 ou
=traffic
.ou_name(ldb
, opts
.instance_id
),
350 domain_sid
=ldb
.get_domain_sid())
352 if opts
.timing_data
== '-':
353 timing_dest
= sys
.stdout
354 elif opts
.timing_data
is None:
357 timing_dest
= open(opts
.timing_data
, 'w')
359 print_err("Generating statistics")
360 traffic
.generate_stats(statsdir
, timing_dest
)
362 if not opts
.preserve_tempdir
:
363 print_err("Removing temporary directory")
364 shutil
.rmtree(tempdir
)