trying to get HEAD building again. If you want the code
[samba.git] / source3 / lib / popt_common.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Common popt routines
4
5    Copyright (C) Tim Potter 2001,2002
6    Copyright (C) Jelmer Vernooij 2002,2003
7
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
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
34  */
35
36 extern pstring user_socket_options;
37 extern BOOL AllowDebugChange;
38
39 struct user_auth_info cmdline_auth_info;
40
41 static void popt_common_callback(poptContext con, 
42                            enum poptCallbackReason reason,
43                            const struct poptOption *opt,
44                            const char *arg, const void *data)
45 {
46         pstring logfile;
47         const char *pname;
48         
49         /* Find out basename of current program */
50         pname = strrchr_m(poptGetInvocationName(con),'/');
51
52         if (!pname)
53                 pname = poptGetInvocationName(con);
54         else 
55                 pname++;
56
57         if (reason == POPT_CALLBACK_REASON_PRE) {
58                 pstr_sprintf(logfile, "%s/log.%s", dyn_LOGFILEBASE, pname);
59                 lp_set_logfile(logfile);
60                 return;
61         }
62
63         switch(opt->val) {
64         case 'd':
65                 if (arg) {
66                         debug_parse_levels(arg);
67                         AllowDebugChange = False;
68                 }
69                 break;
70
71         case 'V':
72                 printf( "Version %s\n", VERSION );
73                 exit(0);
74                 break;
75
76         case 'O':
77                 if (arg) {
78                         pstrcpy(user_socket_options,arg);
79                 }
80                 break;
81
82         case 's':
83                 if (arg) {
84                         pstrcpy(dyn_CONFIGFILE, arg);
85                 }
86                 break;
87
88         case 'n':
89                 if (arg) {
90                         set_global_myname(arg);
91                 }
92                 break;
93
94         case 'l':
95                 if (arg) {
96                         pstr_sprintf(logfile, "%s/log.%s", arg, pname);
97                         lp_set_logfile(logfile);
98                 }
99                 break;
100
101         case 'i':
102                 if (arg) {
103                           set_global_scope(arg);
104                 }
105                 break;
106
107         case 'W':
108                 if (arg) {
109                         set_global_myworkgroup(arg);
110                 }
111                 break;
112         }
113 }
114
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
123 };
124
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
132 };
133
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
138 };
139
140
141
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)
147 {
148         int fd = -1;
149         char *p;
150         BOOL close_it = False;
151         pstring spec;
152         char pass[128];
153
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);
166                 }
167                 close_it = True;
168         }
169
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;
177                         }
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);
187                         }
188
189                 default:
190                         fprintf(stderr, "Error reading password from file %s: %s\n",
191                                         spec, strerror(errno));
192                         exit(1);
193                 }
194         }
195         pstrcpy(a->password, pass);
196         if (close_it)
197                 close(fd);
198 }
199
200 static void get_credentials_file(const char *file, struct user_auth_info *info) 
201 {
202         XFILE *auth;
203         fstring buf;
204         uint16 len = 0;
205         char *ptr, *val, *param;
206
207         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
208         {
209                 /* fail if we can't open the credentials file */
210                 d_printf("ERROR: Unable to open credentials file!\n");
211                 exit(-1);
212         }
213
214         while (!x_feof(auth))
215         {
216                 /* get a line from the file */
217                 if (!x_fgets(buf, sizeof(buf), auth))
218                         continue;
219                 len = strlen(buf);
220
221                 if ((len) && (buf[len-1]=='\n'))
222                 {
223                         buf[len-1] = '\0';
224                         len--;
225                 }
226                 if (len == 0)
227                         continue;
228
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;
234
235                 val = ptr+1;
236                 *ptr = '\0';
237
238                 /* eat leading white space */
239                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
240                         val++;
241
242                 if (strwicmp("password", param) == 0)
243                 {
244                         pstrcpy(info->password, val);
245                         info->got_pass = True;
246                 }
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));
252         }
253         x_fclose(auth);
254 }
255
256 /* Handle command line options:
257  *              -U,--user
258  *              -A,--authentication-file
259  *              -k,--use-kerberos
260  *              -N,--no-pass
261  */
262
263
264 static void popt_common_credentials_callback(poptContext con, 
265                                                                                          enum poptCallbackReason reason,
266                                                                                          const struct poptOption *opt,
267                                                                                          const char *arg, const void *data)
268 {
269         char *p;
270
271         if (reason == POPT_CALLBACK_REASON_PRE) {
272                 cmdline_auth_info.use_kerberos = False;
273                 cmdline_auth_info.got_pass = False;
274                 pstrcpy(cmdline_auth_info.username, "GUEST");   
275
276                 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
277
278                 if (getenv("USER")) {
279                         pstrcpy(cmdline_auth_info.username,getenv("USER"));
280
281                         if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
282                                 *p = 0;
283                                 pstrcpy(cmdline_auth_info.password,p+1);
284                                 cmdline_auth_info.got_pass = True;
285                                 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
286                         }
287                 }
288
289                 if (getenv("PASSWD")) {
290                         pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
291                         cmdline_auth_info.got_pass = True;
292                 }
293
294                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
295                         get_password_file(&cmdline_auth_info);
296                         cmdline_auth_info.got_pass = True;
297                 }
298
299                 return;
300         }
301
302         switch(opt->val) {
303         case 'U':
304                 {
305                         char *lp;
306
307                         pstrcpy(cmdline_auth_info.username,arg);
308                         if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
309                                 *lp = 0;
310                                 pstrcpy(cmdline_auth_info.password,lp+1);
311                                 cmdline_auth_info.got_pass = True;
312                                 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
313                         }
314                 }
315                 break;
316
317         case 'A':
318                 get_credentials_file(arg, &cmdline_auth_info);
319                 break;
320
321         case 'k':
322 #ifndef HAVE_KRB5
323                 d_printf("No kerberos support compiled in\n");
324                 exit(1);
325 #else
326                 cmdline_auth_info.use_kerberos = True;
327                 cmdline_auth_info.got_pass = True;
328 #endif
329                 break;
330         }
331 }
332
333
334
335 struct poptOption popt_common_credentials[] = {
336         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
337         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
338         { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
339         { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
340         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
341         POPT_TABLEEND
342 };