OK. Smbpasswd -j is DEAD.
[tprouty/samba.git] / source / utils / net_ads.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Version 3.0
4    net ads commands
5    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
20 */
21
22 #include "includes.h"
23
24 #ifdef HAVE_ADS
25
26 int net_ads_usage(int argc, const char **argv)
27 {
28         d_printf(
29 "\nnet ads join"\
30 "\n\tjoins the local machine to a ADS realm\n"\
31 "\nnet ads leave"\
32 "\n\tremoves the local machine from a ADS realm\n"\
33 "\nnet ads user"\
34 "\n\tlist users in the realm\n"\
35 "\nnet ads group"\
36 "\n\tlist groups in the realm\n"\
37 "\nnet ads status"\
38 "\n\tdump the machine account details to stdout\n"
39                 );
40         return -1;
41 }
42
43 static ADS_STRUCT *ads_startup(void)
44 {
45         ADS_STRUCT *ads;
46         int rc;
47         ads = ads_init(NULL, NULL, NULL, NULL);
48
49         rc = ads_connect(ads);
50         if (rc) {
51                 d_printf("ads_connect: %s\n", ads_errstr(rc));
52                 return NULL;
53         }
54         return ads;
55 }
56
57 static int net_ads_user(int argc, const char **argv)
58 {
59         ADS_STRUCT *ads;
60         int rc;
61         void *res;
62         const char *attrs[] = {"sAMAccountName", "name", "objectSid", NULL};
63
64         if (!(ads = ads_startup())) return -1;
65         rc = ads_search(ads, &res, "(objectclass=user)", attrs);
66         if (rc) {
67                 d_printf("ads_search: %s\n", ads_errstr(rc));
68                 return -1;
69         }
70
71         if (ads_count_replies(ads, res) == 0) {
72                 d_printf("No users found\n");
73                 return -1;
74         }
75
76         ads_dump(ads, res);
77         ads_destroy(&ads);
78         return 0;
79 }
80
81 static int net_ads_group(int argc, const char **argv)
82 {
83         ADS_STRUCT *ads;
84         int rc;
85         void *res;
86         const char *attrs[] = {"sAMAccountName", "name", "objectSid", NULL};
87
88         if (!(ads = ads_startup())) return -1;
89         rc = ads_search(ads, &res, "(objectclass=group)", attrs);
90         if (rc) {
91                 d_printf("ads_search: %s\n", ads_errstr(rc));
92                 return -1;
93         }
94
95         if (ads_count_replies(ads, res) == 0) {
96                 d_printf("No groups found\n");
97                 return -1;
98         }
99
100         ads_dump(ads, res);
101         return 0;
102 }
103
104 static int net_ads_status(int argc, const char **argv)
105 {
106         ADS_STRUCT *ads;
107         int rc;
108         extern pstring global_myname;
109         void *res;
110
111         if (!(ads = ads_startup())) return -1;
112
113         rc = ads_find_machine_acct(ads, &res, global_myname);
114         if (rc) {
115                 d_printf("ads_find_machine_acct: %s\n", ads_errstr(rc));
116                 return -1;
117         }
118
119         if (ads_count_replies(ads, res) == 0) {
120                 d_printf("No machine account for '%s' found\n", global_myname);
121                 return -1;
122         }
123
124         ads_dump(ads, res);
125
126         return 0;
127 }
128
129 static int net_ads_leave(int argc, const char **argv)
130 {
131         ADS_STRUCT *ads;
132         int rc;
133         extern pstring global_myname;
134
135         if (!(ads = ads_startup())) return -1;
136
137         if (!secrets_init()) {
138                 DEBUG(1,("Failed to initialise secrets database\n"));
139                 return -1;
140         }
141
142         rc = ads_leave_realm(ads, global_myname);
143         if (rc) {
144             d_printf("Failed to delete host '%s' from the '%s' realm.\n", 
145                      global_myname, ads->realm);
146             return -1;
147         }
148
149         d_printf("Removed '%s' from realm '%s'\n", global_myname, ads->realm);
150
151         return 0;
152 }
153
154 static int net_ads_join(int argc, const char **argv)
155 {
156         ADS_STRUCT *ads;
157         int rc;
158         char *password;
159         char *tmp_password;
160         extern pstring global_myname;
161         NTSTATUS status;
162
163         if (!secrets_init()) {
164                 DEBUG(1,("Failed to initialise secrets database\n"));
165                 return -1;
166         }
167         
168         
169         tmp_password = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
170         password = strdup(tmp_password);
171
172         if (!(ads = ads_startup())) return -1;
173
174         rc = ads_join_realm(ads, global_myname);
175         if (rc) {
176                 d_printf("ads_join_realm: %s\n", ads_errstr(rc));
177                 return -1;
178         }
179
180         status = ads_set_machine_password(ads, global_myname, password);
181         if (!NT_STATUS_IS_OK(status)) {
182                 d_printf("ads_set_machine_password: %s\n", get_nt_error_msg(status));
183                 return -1;
184         }
185
186         if (!secrets_store_machine_password(password)) {
187                 DEBUG(1,("Failed to save machine password\n"));
188                 return -1;
189         }
190
191         d_printf("Joined '%s' to realm '%s'\n", global_myname, ads->realm);
192
193         free(password);
194
195         return 0;
196 }
197
198 int net_ads(int argc, const char **argv)
199 {
200         struct functable func[] = {
201                 {"JOIN", net_ads_join},
202                 {"LEAVE", net_ads_leave},
203                 {"STATUS", net_ads_status},
204                 {"USER", net_ads_user},
205                 {"GROUP", net_ads_group},
206                 {NULL, NULL}
207         };
208         
209         return net_run_function(argc, argv, func, net_ads_usage);
210 }
211
212 #else
213
214 int net_ads_usage(int argc, const char **argv)
215 {
216         d_printf("ADS support not compiled in\n");
217         return -1;
218 }
219
220 int net_ads(int argc, const char **argv)
221 {
222         return net_ads_usage(argc, argv);
223 }
224
225 #endif