xref: /freebsd/usr.bin/users/users.cc (revision 5e3934b1)
1e8df2232SEd Schouten /*-
233aa643fSPietro Cerutti  * Copyright (c) 2014 Pietro Cerutti <gahr@FreeBSD.org>
333aa643fSPietro Cerutti  * All rights reserved.
433aa643fSPietro Cerutti  *
533aa643fSPietro Cerutti  * Redistribution and use in source and binary forms, with or without
633aa643fSPietro Cerutti  * modification, are permitted provided that the following conditions
733aa643fSPietro Cerutti  * are met:
833aa643fSPietro Cerutti  * 1. Redistributions of source code must retain the above copyright
933aa643fSPietro Cerutti  *    notice, this list of conditions and the following disclaimer.
1033aa643fSPietro Cerutti  * 2. Redistributions in binary form must reproduce the above copyright
1133aa643fSPietro Cerutti  *    notice, this list of conditions and the following disclaimer in the
1233aa643fSPietro Cerutti  *    documentation and/or other materials provided with the distribution.
13fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1433aa643fSPietro Cerutti  *    may be used to endorse or promote products derived from this software
1533aa643fSPietro Cerutti  *    without specific prior written permission.
1633aa643fSPietro Cerutti  *
1733aa643fSPietro Cerutti  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1833aa643fSPietro Cerutti  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1933aa643fSPietro Cerutti  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2033aa643fSPietro Cerutti  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2133aa643fSPietro Cerutti  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2233aa643fSPietro Cerutti  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2333aa643fSPietro Cerutti  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2433aa643fSPietro Cerutti  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2533aa643fSPietro Cerutti  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2633aa643fSPietro Cerutti  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2733aa643fSPietro Cerutti  * SUCH DAMAGE.
2833aa643fSPietro Cerutti  */
2933aa643fSPietro Cerutti 
302c7c36f9SAllan Jude #include <sys/capsicum.h>
312c7c36f9SAllan Jude 
32bec6dc30SEitan Adler #include <capsicum_helpers.h>
3347312af6SAllan Jude #include <err.h>
3447312af6SAllan Jude #include <errno.h>
3533aa643fSPietro Cerutti #include <utmpx.h>
3633aa643fSPietro Cerutti 
3733aa643fSPietro Cerutti #include <algorithm>
3833aa643fSPietro Cerutti #include <iostream>
3933aa643fSPietro Cerutti #include <iterator>
40975f9124SEd Schouten #include <set>
41e8df2232SEd Schouten #include <string>
4233aa643fSPietro Cerutti using namespace std;
4333aa643fSPietro Cerutti 
4433aa643fSPietro Cerutti int
main(int argc,char **)4533aa643fSPietro Cerutti main(int argc, char **)
4633aa643fSPietro Cerutti {
4733aa643fSPietro Cerutti 	struct utmpx *ut;
48975f9124SEd Schouten 	set<string> names;
4933aa643fSPietro Cerutti 
5033aa643fSPietro Cerutti 	if (argc > 1) {
5133aa643fSPietro Cerutti 		cerr << "usage: users" << endl;
5233aa643fSPietro Cerutti 		return (1);
5333aa643fSPietro Cerutti 	}
5433aa643fSPietro Cerutti 
5533aa643fSPietro Cerutti 	setutxent();
5647312af6SAllan Jude 
57bec6dc30SEitan Adler 	if (caph_enter())
5847312af6SAllan Jude 		err(1, "Failed to enter capability mode.");
5947312af6SAllan Jude 
60975f9124SEd Schouten 	while ((ut = getutxent()) != NULL)
61975f9124SEd Schouten 		if (ut->ut_type == USER_PROCESS)
62975f9124SEd Schouten 			names.insert(ut->ut_user);
6333aa643fSPietro Cerutti 	endutxent();
6433aa643fSPietro Cerutti 
65975f9124SEd Schouten 	if (!names.empty()) {
66e8df2232SEd Schouten 		set<string>::iterator last = names.end();
67975f9124SEd Schouten 		--last;
68975f9124SEd Schouten 		copy(names.begin(), last, ostream_iterator<string>(cout, " "));
69975f9124SEd Schouten 		cout << *last << endl;
7033aa643fSPietro Cerutti 	}
7133aa643fSPietro Cerutti }
72