xref: /illumos-gate/usr/src/cmd/krb5/kadmin/kclient/ksmb.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <strings.h>
30 #include <locale.h>
31 #include <netdb.h>
32 #include <limits.h>
33 #include <smbsrv/libsmbns.h>
34 
35 #define	QUOTE(x)	#x
36 #define	VAL2STR(x)	QUOTE(x)
37 
38 char *whoami = NULL;
39 
40 static void usage();
41 
42 static
43 void
44 usage()
45 {
46 	fprintf(stderr,
47 	    gettext("Usage: %s [ -d fqdn ] [ -s server ]\n"), whoami);
48 	fprintf(stderr,
49 	    gettext("\t-d\tThe fully qualified domain of the client\n"));
50 	fprintf(stderr, gettext("\t-s\tThe domain controller to join\n"));
51 	fprintf(stderr,
52 	    gettext("\tstdin is used to read in the password or \n"));
53 	fprintf(stderr, gettext("\tthe password is prompted for.\n"));
54 
55 	exit(1);
56 }
57 
58 int
59 main(int argc, char **argv)
60 {
61 	char c, fqdn[MAXHOSTNAMELEN], server[MAXHOSTNAMELEN];
62 	char *newpw;
63 	int ret = 0;
64 
65 	(void) setlocale(LC_ALL, "");
66 
67 #if !defined(TEXT_DOMAIN)
68 #define	TEXT_DOMAIN "SYS_TEST"
69 #endif /* TEXT_DOMAIN */
70 
71 	(void) textdomain(TEXT_DOMAIN);
72 
73 	whoami = argv[0];
74 
75 	while ((c = getopt(argc, argv, "d:s:")) != -1) {
76 		switch (c) {
77 		case 'd':
78 			(void) strncpy(fqdn, optarg, sizeof (fqdn));
79 			break;
80 		case 's':
81 			(void) strncpy(server, optarg, sizeof (server));
82 			break;
83 		default:
84 			usage();
85 			break;
86 		}
87 	}
88 
89 	if (argc != optind)
90 		usage();
91 
92 	if (!isatty(fileno(stdin))) {
93 		char buf[PASS_MAX + 1];
94 
95 		if (scanf("%" VAL2STR(PASS_MAX) "s", &buf) != 1) {
96 			fprintf(stderr,
97 			    gettext("Couldn't read new password\n"));
98 			exit(1);
99 		}
100 
101 		newpw = strdup(buf);
102 		if (newpw == NULL) {
103 			fprintf(stderr, gettext("Couldn't allocate memory\n"));
104 			exit(1);
105 		}
106 	} else {
107 		newpw = getpassphrase(gettext("Enter new password: "));
108 		if (newpw == NULL) {
109 			fprintf(stderr,
110 			    gettext("Couldn't read new password\n"));
111 			exit(1);
112 		}
113 
114 		newpw = strdup(newpw);
115 		if (newpw == NULL) {
116 			fprintf(stderr, gettext("Couldn't allocate memory\n"));
117 			exit(1);
118 		}
119 	}
120 
121 	/*
122 	 * Set the SMF properties for smb for later use.
123 	 */
124 	ret = smb_setdomainprops(fqdn, server, newpw);
125 
126 	free(newpw);
127 
128 	return (ret);
129 }
130