xref: /titanic_41/usr/src/cmd/krb5/kadmin/kclient/ksmb.c (revision e71ca95ca6de23d33b54cb55cefdef30bc7c969b)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <locale.h>
33 #include <netdb.h>
34 #include <limits.h>
35 #include <smbsrv/libsmbns.h>
36 
37 #define	QUOTE(x)	#x
38 #define	VAL2STR(x)	QUOTE(x)
39 
40 char *whoami = NULL;
41 
42 static void usage();
43 
44 static
45 void
46 usage()
47 {
48 	fprintf(stderr,
49 	    gettext("Usage: %s [ -d fqdn ] [ -s server ]\n"), whoami);
50 	fprintf(stderr,
51 	    gettext("\t-d\tThe fully qualified domain of the client\n"));
52 	fprintf(stderr, gettext("\t-s\tThe domain controller to join\n"));
53 	fprintf(stderr,
54 	    gettext("\tstdin is used to read in the password or \n"));
55 	fprintf(stderr, gettext("\tthe password is prompted for.\n"));
56 
57 	exit(1);
58 }
59 
60 int
61 main(int argc, char **argv)
62 {
63 	char c, fqdn[MAXHOSTNAMELEN], server[MAXHOSTNAMELEN];
64 	char *newpw;
65 	int ret = 0;
66 
67 	(void) setlocale(LC_ALL, "");
68 
69 #if !defined(TEXT_DOMAIN)
70 #define	TEXT_DOMAIN "SYS_TEST"
71 #endif /* TEXT_DOMAIN */
72 
73 	(void) textdomain(TEXT_DOMAIN);
74 
75 	whoami = argv[0];
76 
77 	while ((c = getopt(argc, argv, "d:s:")) != -1) {
78 		switch (c) {
79 		case 'd':
80 			(void) strncpy(fqdn, optarg, sizeof (fqdn));
81 			break;
82 		case 's':
83 			(void) strncpy(server, optarg, sizeof (server));
84 			break;
85 		default:
86 			usage();
87 			break;
88 		}
89 	}
90 
91 	if (argc != optind)
92 		usage();
93 
94 	if (!isatty(fileno(stdin))) {
95 		char buf[PASS_MAX + 1];
96 
97 		if (scanf("%" VAL2STR(PASS_MAX) "s", &buf) != 1) {
98 			fprintf(stderr,
99 			    gettext("Couldn't read new password\n"));
100 			exit(1);
101 		}
102 
103 		newpw = strdup(buf);
104 		if (newpw == NULL) {
105 			fprintf(stderr, gettext("Couldn't allocate memory\n"));
106 			exit(1);
107 		}
108 	} else {
109 		newpw = getpassphrase(gettext("Enter new password: "));
110 		if (newpw == NULL) {
111 			fprintf(stderr,
112 			    gettext("Couldn't read new password\n"));
113 			exit(1);
114 		}
115 
116 		newpw = strdup(newpw);
117 		if (newpw == NULL) {
118 			fprintf(stderr, gettext("Couldn't allocate memory\n"));
119 			exit(1);
120 		}
121 	}
122 
123 	/*
124 	 * Set the SMF properties for smb for later use.
125 	 */
126 	ret = smb_setdomainprops(fqdn, server, newpw);
127 
128 	free(newpw);
129 
130 	return (ret);
131 }
132