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
27 sys.path.insert(0, "bin/python")
29 from samba import gensec, get_debug_level
30 from samba.emulate import traffic
31 import samba.getopt as options
32 from samba.logger import get_samba_logger
33 from samba.samdb import SamDB
34 from samba.auth import system_session
37 def print_err(*args, **kwargs):
38 print(*args, file=sys.stderr, **kwargs)
43 desc = ("Generates network traffic 'conversations' based on <summary-file>"
44 " (which should be the output file produced by either traffic_learner"
45 " or traffic_summary.pl). This traffic is sent to <dns-hostname>,"
46 " which is the full DNS hostname of the DC being tested.")
48 parser = optparse.OptionParser(
49 "%prog [--help|options] <summary-file> <dns-hostname>",
52 parser.add_option('--dns-rate', type='float', default=0,
53 help='fire extra DNS packets at this rate')
54 parser.add_option('-B', '--badpassword-frequency',
55 type='float', default=0.0,
56 help='frequency of connections with bad passwords')
57 parser.add_option('-K', '--prefer-kerberos',
59 help='prefer kerberos when authenticating test users')
60 parser.add_option('-I', '--instance-id', type='int', default=0,
61 help='Instance number, when running multiple instances')
62 parser.add_option('-t', '--timing-data',
63 help=('write individual message timing data here '
65 parser.add_option('--preserve-tempdir', default=False, action="store_true",
66 help='do not delete temporary files')
67 parser.add_option('-F', '--fixed-password',
68 type='string', default=None,
69 help=('Password used for the test users created. '
71 parser.add_option('-c', '--clean-up',
73 help='Clean up the generated groups and user accounts')
74 parser.add_option('--random-seed', type='int', default=0,
75 help='Use to keep randomness consistent across multiple runs')
77 model_group = optparse.OptionGroup(parser, 'Traffic Model Options',
78 'These options alter the traffic '
79 'generated when the summary-file is a '
80 'traffic-model (produced by '
82 model_group.add_option('-S', '--scale-traffic', type='float', default=1.0,
83 help='Increase the number of conversations by '
85 model_group.add_option('-D', '--duration', type='float', default=None,
86 help=('Run model for this long (approx). '
87 'Default 60s for models'))
88 model_group.add_option('-r', '--replay-rate', type='float', default=1.0,
89 help='Replay the traffic faster by this factor')
90 model_group.add_option('--traffic-summary',
91 help=('Generate a traffic summary file and write '
92 'it here (- for stdout)'))
93 parser.add_option_group(model_group)
95 user_gen_group = optparse.OptionGroup(parser, 'Generate User Options',
96 "Add extra user/groups on the DC to "
97 "increase the DB size. These extra "
98 "users aren't used for traffic "
100 user_gen_group.add_option('-G', '--generate-users-only',
102 help='Generate the users, but do not replay '
104 user_gen_group.add_option('-n', '--number-of-users', type='int', default=0,
105 help='Total number of test users to create')
106 user_gen_group.add_option('--number-of-groups', type='int', default=0,
107 help='Create this many groups')
108 user_gen_group.add_option('--average-groups-per-user',
109 type='int', default=0,
110 help='Assign the test users to this '
111 'many groups on average')
112 user_gen_group.add_option('--group-memberships', type='int', default=0,
113 help='Total memberships to assign across all '
114 'test users and all groups')
115 parser.add_option_group(user_gen_group)
117 sambaopts = options.SambaOptions(parser)
118 parser.add_option_group(sambaopts)
119 parser.add_option_group(options.VersionOptions(parser))
120 credopts = options.CredentialsOptions(parser)
121 parser.add_option_group(credopts)
123 # the --no-password credential doesn't make sense for this tool
124 if parser.has_option('-N'):
125 parser.remove_option('-N')
127 opts, args = parser.parse_args()
129 # First ensure we have reasonable arguments
140 lp = sambaopts.get_loadparm()
141 debuglevel = get_debug_level()
142 logger = get_samba_logger(name=__name__,
143 verbose=debuglevel > 3,
144 quiet=debuglevel < 1)
146 traffic.DEBUG_LEVEL = debuglevel
147 # pass log level down to traffic module to make sure level is controlled
148 traffic.LOGGER.setLevel(logger.getEffectiveLevel())
151 logger.info("Removing user and machine accounts")
152 lp = sambaopts.get_loadparm()
153 creds = credopts.get_credentials(lp)
154 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
155 ldb = traffic.openLdb(host, creds, lp)
156 traffic.clean_up_accounts(ldb, opts.instance_id)
160 if not os.path.exists(summary):
161 logger.error("Summary file %s doesn't exist" % summary)
163 # the summary-file can be ommitted for --generate-users-only and
164 # --cleanup-up, but it should be specified in all other cases
165 elif not opts.generate_users_only:
166 logger.error("No summary-file specified to replay traffic from")
169 if not opts.fixed_password:
170 logger.error(("Please use --fixed-password to specify a password"
171 " for the users created as part of this test"))
175 random.seed(opts.random_seed)
177 creds = credopts.get_credentials(lp)
178 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
180 domain = creds.get_domain()
182 lp.set("workgroup", domain)
184 domain = lp.get("workgroup")
185 if domain == "WORKGROUP":
186 logger.error(("NETBIOS domain does not appear to be "
187 "specified, use the --workgroup option"))
190 if not opts.realm and not lp.get('realm'):
191 logger.error("Realm not specified, use the --realm option")
194 if opts.generate_users_only and not (opts.number_of_users or
195 opts.number_of_groups):
196 logger.error(("Please specify the number of users and/or groups "
200 if opts.group_memberships and opts.average_groups_per_user:
201 logger.error(("--group-memberships and --average-groups-per-user"
202 " are incompatible options - use one or the other"))
205 if not opts.number_of_groups and opts.average_groups_per_user:
206 logger.error(("--average-groups-per-user requires "
207 "--number-of-groups"))
210 if opts.number_of_groups and opts.average_groups_per_user:
211 if opts.number_of_groups < opts.average_groups_per_user:
212 logger.error(("--average-groups-per-user can not be more than "
213 "--number-of-groups"))
216 if not opts.number_of_groups and opts.group_memberships:
217 logger.error("--group-memberships requires --number-of-groups")
220 if opts.timing_data not in ('-', None):
222 open(opts.timing_data, 'w').close()
224 # exception info will be added to log automatically
225 logger.exception(("the supplied timing data destination "
226 "(%s) is not writable" % opts.timing_data))
229 if opts.traffic_summary not in ('-', None):
231 open(opts.traffic_summary, 'w').close()
233 # exception info will be added to log automatically
234 logger.exception(("the supplied traffic summary destination "
235 "(%s) is not writable" % opts.traffic_summary))
238 duration = opts.duration
242 # ingest the model or traffic summary
245 conversations, interval, duration, dns_counts = \
246 traffic.ingest_summaries([summary])
248 logger.info(("Using conversations from the traffic summary "
251 # honour the specified duration if it's different to the
253 if opts.duration is not None:
254 duration = opts.duration
256 except ValueError as e:
257 if not str(e).startswith('need more than'):
260 model = traffic.TrafficModel()
265 logger.error(("Could not parse %s. The summary file "
266 "should be the output from either the "
267 "traffic_summary.pl or "
268 "traffic_learner scripts.") % summary)
271 logger.info(("Using the specified model file to "
272 "generate conversations"))
274 conversations = model.generate_conversations(opts.scale_traffic,
282 for c in conversations:
284 print(" ", p, file=sys.stderr)
286 print('=' * 72, file=sys.stderr)
288 if opts.number_of_users and opts.number_of_users < len(conversations):
289 logger.error(("--number-of-users (%d) is less than the "
290 "number of conversations to replay (%d)"
291 % (opts.number_of_users, len(conversations))))
294 number_of_users = max(opts.number_of_users, len(conversations))
295 max_memberships = number_of_users * opts.number_of_groups
297 if not opts.group_memberships and opts.average_groups_per_user:
298 opts.group_memberships = opts.average_groups_per_user * number_of_users
299 logger.info(("Using %d group-memberships based on %u average "
300 "memberships for %d users"
301 % (opts.group_memberships,
302 opts.average_groups_per_user, number_of_users)))
304 if opts.group_memberships > max_memberships:
305 logger.error(("The group memberships specified (%d) exceeds "
306 "the total users (%d) * total groups (%d)"
307 % (opts.group_memberships, number_of_users,
308 opts.number_of_groups)))
311 # Get an LDB connection.
313 # if we're only adding users, then it's OK to pass a sam.ldb filepath
314 # as the host, which creates the users much faster. In all other cases
315 # we should be connecting to a remote DC
316 if opts.generate_users_only and os.path.isfile(host):
317 ldb = SamDB(url="ldb://{0}".format(host),
318 session_info=system_session(), lp=lp)
320 ldb = traffic.openLdb(host, creds, lp)
322 logger.error(("\nInitial LDAP connection failed! Did you supply "
323 "a DNS host name and the correct credentials?"))
326 if opts.generate_users_only:
327 traffic.generate_users_and_groups(ldb,
330 opts.number_of_users,
331 opts.number_of_groups,
332 opts.group_memberships)
335 tempdir = tempfile.mkdtemp(prefix="samba_tg_")
336 logger.info("Using temp dir %s" % tempdir)
338 traffic.generate_users_and_groups(ldb,
342 opts.number_of_groups,
343 opts.group_memberships)
345 accounts = traffic.generate_replay_accounts(ldb,
350 statsdir = traffic.mk_masked_dir(tempdir, 'stats')
352 if opts.traffic_summary:
353 if opts.traffic_summary == '-':
354 summary_dest = sys.stdout
356 summary_dest = open(opts.traffic_summary, 'w')
358 logger.info("Writing traffic summary")
360 for c in conversations:
361 summaries += c.replay_as_summary_lines()
364 for (time, line) in summaries:
365 print(line, file=summary_dest)
369 traffic.replay(conversations, host,
373 dns_rate=opts.dns_rate,
375 badpassword_frequency=opts.badpassword_frequency,
376 prefer_kerberos=opts.prefer_kerberos,
379 base_dn=ldb.domain_dn(),
380 ou=traffic.ou_name(ldb, opts.instance_id),
382 domain_sid=ldb.get_domain_sid())
384 if opts.timing_data == '-':
385 timing_dest = sys.stdout
386 elif opts.timing_data is None:
389 timing_dest = open(opts.timing_data, 'w')
391 logger.info("Generating statistics")
392 traffic.generate_stats(statsdir, timing_dest)
394 if not opts.preserve_tempdir:
395 logger.info("Removing temporary directory")
396 shutil.rmtree(tempdir)