added support for browsing the list of workgroups at the top level in
[jra/samba/.git] / source3 / smbwrapper / smbsh.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    SMB wrapper functions - frontend
5    Copyright (C) Andrew Tridgell 1998
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 static void smbsh_usage(void)
25 {
26         printf("smbsh [options]\n\n");
27         printf(" -W workgroup\n");
28         printf(" -U username\n");
29         printf(" -P prefix\n");
30         printf(" -R resolve order\n");
31         printf(" -d debug level\n");
32         printf(" -l logfile\n");
33         printf(" -L libdir\n");
34         exit(0);
35 }
36
37 int main(int argc, char *argv[])
38 {
39         char *p, *u;
40         char *libd = BINDIR;    
41         pstring line, wd;
42         int opt;
43         extern char *optarg;
44         extern int optind;
45         extern FILE *dbf;
46
47         dbf = stdout;
48         charset_initialise();
49         smbw_setup_shared();
50
51         while ((opt = getopt(argc, argv, "W:U:R:d:P:l:hL:")) != EOF) {
52                 switch (opt) {
53                 case 'L':
54                         libd = optarg;
55                         break;
56                 case 'W':
57                         smbw_setshared("WORKGROUP", optarg);
58                         break;
59                 case 'l':
60                         smbw_setshared("LOGFILE", optarg);
61                         break;
62                 case 'P':
63                         smbw_setshared("PREFIX", optarg);
64                         break;
65                 case 'd':
66                         smbw_setshared("DEBUG", optarg);
67                         break;
68                 case 'U':
69                         p = strchr(optarg,'%');
70                         if (p) {
71                                 *p=0;
72                                 smbw_setshared("PASSWORD",p+1);
73                         }
74                         smbw_setshared("USER", optarg);
75                         break;
76                 case 'R':
77                         smbw_setshared("RESOLVE_ORDER",optarg);
78                         break;
79
80                 case 'h':
81                 default:
82                         smbsh_usage();
83                 }
84         }
85
86
87         if (!smbw_getshared("USER")) {
88                 printf("Username: ");
89                 u = fgets_slash(line, sizeof(line)-1, stdin);
90                 smbw_setshared("USER", u);
91         }
92
93         if (!smbw_getshared("PASSWORD")) {
94                 p = getpass("Password: ");
95                 smbw_setshared("PASSWORD", p);
96         }
97
98         smbw_setenv("PS1", "smbsh$ ");
99
100         sys_getwd(wd);
101
102         slprintf(line,sizeof(line)-1,"PWD_%d", (int)getpid());
103
104         smbw_setshared(line, wd);
105
106         slprintf(line,sizeof(line)-1,"%s/smbwrapper.so", libd);
107         smbw_setenv("LD_PRELOAD", line);
108
109         slprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so", libd);
110
111         if (file_exist(line, NULL)) {
112                 slprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so:DEFAULT", libd);
113                 smbw_setenv("_RLD_LIST", line);
114                 slprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
115                 smbw_setenv("_RLDN32_LIST", line);
116         } else {
117                 slprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
118                 smbw_setenv("_RLD_LIST", line);
119         }
120
121         {
122         char *shellpath = getenv("SHELL");
123                 if(shellpath)
124                         execl(shellpath,"smbsh",NULL);
125                 else
126                         execl("/bin/sh","smbsh",NULL);
127         }
128         printf("launch failed!\n");
129         return 1;
130 }