port latest changes from SAMBA_3_0 tree
[Samba.git] / source3 / lib / popt_common.c
blobaf1cbcfe80caff856d5a0f76fb3dc1991089237b
1 /*
2 Unix SMB/CIFS implementation.
3 Common popt routines
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 /* Handle command line options:
26 * -d,--debuglevel
27 * -s,--configfile
28 * -O,--socket-options
29 * -V,--version
30 * -l,--log-base
31 * -n,--netbios-name
32 * -W,--workgroup
33 * -i,--scope
36 extern pstring user_socket_options;
37 extern BOOL AllowDebugChange;
39 struct user_auth_info cmdline_auth_info;
41 static void popt_common_callback(poptContext con,
42 enum poptCallbackReason reason,
43 const struct poptOption *opt,
44 const char *arg, const void *data)
46 pstring logfile;
47 const char *pname;
49 /* Find out basename of current program */
50 pname = strrchr_m(poptGetInvocationName(con),'/');
52 if (!pname)
53 pname = poptGetInvocationName(con);
54 else
55 pname++;
57 if (reason == POPT_CALLBACK_REASON_PRE) {
58 pstr_sprintf(logfile, "%s/log.%s", dyn_LOGFILEBASE, pname);
59 lp_set_logfile(logfile);
60 return;
63 switch(opt->val) {
64 case 'd':
65 if (arg) {
66 debug_parse_levels(arg);
67 AllowDebugChange = False;
69 break;
71 case 'V':
72 printf( "Version %s\n", VERSION );
73 exit(0);
74 break;
76 case 'O':
77 if (arg) {
78 pstrcpy(user_socket_options,arg);
80 break;
82 case 's':
83 if (arg) {
84 pstrcpy(dyn_CONFIGFILE, arg);
86 break;
88 case 'n':
89 if (arg) {
90 set_global_myname(arg);
92 break;
94 case 'l':
95 if (arg) {
96 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
97 lp_set_logfile(logfile);
99 break;
101 case 'i':
102 if (arg) {
103 set_global_scope(arg);
105 break;
107 case 'W':
108 if (arg) {
109 set_global_myworkgroup(arg);
111 break;
115 struct poptOption popt_common_connection[] = {
116 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
117 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
118 "SOCKETOPTIONS" },
119 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
120 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
121 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
122 POPT_TABLEEND
125 struct poptOption popt_common_samba[] = {
126 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
127 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
128 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
129 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
130 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
131 POPT_TABLEEND
134 struct poptOption popt_common_version[] = {
135 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
136 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
137 POPT_TABLEEND
142 /****************************************************************************
143 * get a password from a a file or file descriptor
144 * exit on failure
145 * ****************************************************************************/
146 static void get_password_file(struct user_auth_info *a)
148 int fd = -1;
149 char *p;
150 BOOL close_it = False;
151 pstring spec;
152 char pass[128];
154 if ((p = getenv("PASSWD_FD")) != NULL) {
155 pstrcpy(spec, "descriptor ");
156 pstrcat(spec, p);
157 sscanf(p, "%d", &fd);
158 close_it = False;
159 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
160 fd = sys_open(p, O_RDONLY, 0);
161 pstrcpy(spec, p);
162 if (fd < 0) {
163 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
164 spec, strerror(errno));
165 exit(1);
167 close_it = True;
170 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
171 p && p - pass < sizeof(pass);) {
172 switch (read(fd, p, 1)) {
173 case 1:
174 if (*p != '\n' && *p != '\0') {
175 *++p = '\0'; /* advance p, and null-terminate pass */
176 break;
178 case 0:
179 if (p - pass) {
180 *p = '\0'; /* null-terminate it, just in case... */
181 p = NULL; /* then force the loop condition to become false */
182 break;
183 } else {
184 fprintf(stderr, "Error reading password from file %s: %s\n",
185 spec, "empty password\n");
186 exit(1);
189 default:
190 fprintf(stderr, "Error reading password from file %s: %s\n",
191 spec, strerror(errno));
192 exit(1);
195 pstrcpy(a->password, pass);
196 if (close_it)
197 close(fd);
200 static void get_credentials_file(const char *file, struct user_auth_info *info)
202 XFILE *auth;
203 fstring buf;
204 uint16 len = 0;
205 char *ptr, *val, *param;
207 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
209 /* fail if we can't open the credentials file */
210 d_printf("ERROR: Unable to open credentials file!\n");
211 exit(-1);
214 while (!x_feof(auth))
216 /* get a line from the file */
217 if (!x_fgets(buf, sizeof(buf), auth))
218 continue;
219 len = strlen(buf);
221 if ((len) && (buf[len-1]=='\n'))
223 buf[len-1] = '\0';
224 len--;
226 if (len == 0)
227 continue;
229 /* break up the line into parameter & value.
230 * will need to eat a little whitespace possibly */
231 param = buf;
232 if (!(ptr = strchr_m (buf, '=')))
233 continue;
235 val = ptr+1;
236 *ptr = '\0';
238 /* eat leading white space */
239 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
240 val++;
242 if (strwicmp("password", param) == 0)
244 pstrcpy(info->password, val);
245 info->got_pass = True;
247 else if (strwicmp("username", param) == 0)
248 pstrcpy(info->username, val);
249 else if (strwicmp("domain", param) == 0)
250 set_global_myworkgroup(val);
251 memset(buf, 0, sizeof(buf));
253 x_fclose(auth);
256 /* Handle command line options:
257 * -U,--user
258 * -A,--authentication-file
259 * -k,--use-kerberos
260 * -N,--no-pass
261 * -S,--signing
265 static void popt_common_credentials_callback(poptContext con,
266 enum poptCallbackReason reason,
267 const struct poptOption *opt,
268 const char *arg, const void *data)
270 char *p;
272 if (reason == POPT_CALLBACK_REASON_PRE) {
273 cmdline_auth_info.use_kerberos = False;
274 cmdline_auth_info.got_pass = False;
275 cmdline_auth_info.signing_state = Undefined;
276 pstrcpy(cmdline_auth_info.username, "GUEST");
278 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
280 if (getenv("USER")) {
281 pstrcpy(cmdline_auth_info.username,getenv("USER"));
283 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
284 *p = 0;
285 pstrcpy(cmdline_auth_info.password,p+1);
286 cmdline_auth_info.got_pass = True;
287 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
291 if (getenv("PASSWD")) {
292 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
293 cmdline_auth_info.got_pass = True;
296 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
297 get_password_file(&cmdline_auth_info);
298 cmdline_auth_info.got_pass = True;
301 return;
304 switch(opt->val) {
305 case 'U':
307 char *lp;
309 pstrcpy(cmdline_auth_info.username,arg);
310 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
311 *lp = 0;
312 pstrcpy(cmdline_auth_info.password,lp+1);
313 cmdline_auth_info.got_pass = True;
314 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
317 break;
319 case 'A':
320 get_credentials_file(arg, &cmdline_auth_info);
321 break;
323 case 'k':
324 #ifndef HAVE_KRB5
325 d_printf("No kerberos support compiled in\n");
326 exit(1);
327 #else
328 cmdline_auth_info.use_kerberos = True;
329 cmdline_auth_info.got_pass = True;
330 #endif
331 break;
333 case 'S':
335 cmdline_auth_info.signing_state = -1;
336 if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
337 cmdline_auth_info.signing_state = False;
338 else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true"))
339 cmdline_auth_info.signing_state = True;
340 else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
341 cmdline_auth_info.signing_state = Required;
342 else {
343 fprintf(stderr, "Unknown signing option %s\n", arg );
344 exit(1);
347 break;
353 struct poptOption popt_common_credentials[] = {
354 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
355 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
356 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
357 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
358 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
359 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
360 POPT_TABLEEND