first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[nivanova/samba-autobuild/.git] / source / 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         exit(0);
34 }
35
36 int main(int argc, char *argv[])
37 {
38         char *p, *u;
39         char *libd = BINDIR;    
40         pstring line, wd;
41         int opt;
42         extern char *optarg;
43         extern int optind;
44
45         smbw_setup_shared();
46
47         while ((opt = getopt(argc, argv, "W:U:R:d:P:l:h")) != EOF) {
48                 switch (opt) {
49                 case 'W':
50                         smbw_setshared("WORKGROUP", optarg);
51                         break;
52                 case 'l':
53                         smbw_setshared("LOGFILE", optarg);
54                         break;
55                 case 'P':
56                         smbw_setshared("PREFIX", optarg);
57                         break;
58                 case 'd':
59                         smbw_setshared("DEBUG", optarg);
60                         break;
61                 case 'U':
62                         p = strchr(optarg,'%');
63                         if (p) {
64                                 *p=0;
65                                 smbw_setshared("PASSWORD",p+1);
66                         }
67                         smbw_setshared("USER", optarg);
68                         break;
69                 case 'R':
70                         smbw_setshared("RESOLVE_ORDER",optarg);
71                         break;
72
73                 case 'h':
74                 default:
75                         smbsh_usage();
76                 }
77         }
78
79         charset_initialise();
80
81         if (!smbw_getshared("USER")) {
82                 printf("Username: ");
83                 u = fgets_slash(line, sizeof(line)-1, stdin);
84                 smbw_setshared("USER", u);
85         }
86
87         if (!smbw_getshared("PASSWORD")) {
88                 p = getpass("Password: ");
89                 smbw_setshared("PASSWORD", p);
90         }
91
92         smbw_setenv("PS1", "smbsh$ ");
93
94         sys_getwd(wd);
95
96         slprintf(line,sizeof(line)-1,"PWD_%d", (int)getpid());
97
98         smbw_setshared(line, wd);
99
100         slprintf(line,sizeof(line)-1,"%s/smbwrapper.so", libd);
101         smbw_setenv("LD_PRELOAD", line);
102
103         slprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so", libd);
104
105         if (file_exist(line, NULL)) {
106                 slprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so:DEFAULT", libd);
107                 smbw_setenv("_RLD_LIST", line);
108                 slprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
109                 smbw_setenv("_RLDN32_LIST", line);
110         } else {
111                 slprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
112                 smbw_setenv("_RLD_LIST", line);
113         }
114
115         {
116         char *shellpath = getenv("SHELL");
117                 if(shellpath)
118                         execl(shellpath,"smbsh",NULL);
119                 else
120                         execl("/bin/sh","smbsh",NULL);
121         }
122         printf("launch failed!\n");
123         return 1;
124 }