2 Unix SMB/CIFS implementation.
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.
25 /* Handle command line options:
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)
49 /* Find out basename of current program */
50 pname = strrchr_m(poptGetInvocationName(con),'/');
53 pname = poptGetInvocationName(con);
57 if (reason == POPT_CALLBACK_REASON_PRE) {
58 pstr_sprintf(logfile, "%s/log.%s", dyn_LOGFILEBASE, pname);
59 lp_set_cmdline("log file", logfile);
65 lp_set_cmdline("log level", arg);
69 printf( "Version %s\n", SAMBA_VERSION );
75 pstrcpy(dyn_CONFIGFILE, arg);
81 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
82 lp_set_cmdline("log file", logfile);
87 lp_set_cmdline("workgroup", arg);
91 lp_set_cmdline("netbios name", arg);
95 lp_set_cmdline("netbios scope", arg);
99 lp_set_cmdline("max protocol", arg);
104 struct poptOption popt_common_connection[] = {
105 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
106 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
108 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
109 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
110 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
111 { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
115 struct poptOption popt_common_samba[] = {
116 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
117 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
118 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
119 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
120 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
124 struct poptOption popt_common_version[] = {
125 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
126 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
132 /****************************************************************************
133 * get a password from a a file or file descriptor
135 * ****************************************************************************/
136 static void get_password_file(struct user_auth_info *a)
140 BOOL close_it = False;
144 if ((p = getenv("PASSWD_FD")) != NULL) {
145 pstrcpy(spec, "descriptor ");
147 sscanf(p, "%d", &fd);
149 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
150 fd = sys_open(p, O_RDONLY, 0);
153 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
154 spec, strerror(errno));
160 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
161 p && p - pass < sizeof(pass);) {
162 switch (read(fd, p, 1)) {
164 if (*p != '\n' && *p != '\0') {
165 *++p = '\0'; /* advance p, and null-terminate pass */
170 *p = '\0'; /* null-terminate it, just in case... */
171 p = NULL; /* then force the loop condition to become false */
174 fprintf(stderr, "Error reading password from file %s: %s\n",
175 spec, "empty password\n");
180 fprintf(stderr, "Error reading password from file %s: %s\n",
181 spec, strerror(errno));
185 pstrcpy(a->password, pass);
190 static void get_credentials_file(const char *file, struct user_auth_info *info)
195 char *ptr, *val, *param;
197 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
199 /* fail if we can't open the credentials file */
200 d_printf("ERROR: Unable to open credentials file!\n");
204 while (!x_feof(auth))
206 /* get a line from the file */
207 if (!x_fgets(buf, sizeof(buf), auth))
211 if ((len) && (buf[len-1]=='\n'))
219 /* break up the line into parameter & value.
220 * will need to eat a little whitespace possibly */
222 if (!(ptr = strchr_m (buf, '=')))
228 /* eat leading white space */
229 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
232 if (strwicmp("password", param) == 0)
234 pstrcpy(info->password, val);
235 info->got_pass = True;
237 else if (strwicmp("username", param) == 0)
238 pstrcpy(info->username, val);
240 else if (strwicmp("domain", param) == 0)
241 set_global_myworkgroup(val);
243 memset(buf, 0, sizeof(buf));
248 /* Handle command line options:
250 * -A,--authentication-file
256 static void popt_common_credentials_callback(poptContext con,
257 enum poptCallbackReason reason,
258 const struct poptOption *opt,
259 const char *arg, const void *data)
263 if (reason == POPT_CALLBACK_REASON_PRE) {
264 cmdline_auth_info.use_kerberos = False;
265 cmdline_auth_info.got_pass = False;
266 pstrcpy(cmdline_auth_info.username, "GUEST");
268 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
270 if (getenv("USER")) {
271 pstrcpy(cmdline_auth_info.username,getenv("USER"));
273 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
275 pstrcpy(cmdline_auth_info.password,p+1);
276 cmdline_auth_info.got_pass = True;
277 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
281 if (getenv("PASSWD")) {
282 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
283 cmdline_auth_info.got_pass = True;
286 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
287 get_password_file(&cmdline_auth_info);
288 cmdline_auth_info.got_pass = True;
299 pstrcpy(cmdline_auth_info.username,arg);
300 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
302 pstrcpy(cmdline_auth_info.password,lp+1);
303 cmdline_auth_info.got_pass = True;
304 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
310 get_credentials_file(arg, &cmdline_auth_info);
315 d_printf("No kerberos support compiled in\n");
318 cmdline_auth_info.use_kerberos = True;
319 cmdline_auth_info.got_pass = True;
327 struct poptOption popt_common_credentials[] = {
328 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
329 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
330 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, True, "Don't ask for a password" },
331 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, True, "Use kerberos (active directory) authentication" },
332 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },