7b33de766fd89e8d493781ae208e2873152c552d
[samba.git] / examples / libsmbclient / 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    Copyright (C) Derrell Lipman 2003-2005
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 <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <libsmbclient.h>
31
32 #ifndef FALSE
33 # define        FALSE   (0)
34 # define        TRUE    (! FALSE)
35 #endif
36
37 static void smbsh_usage(void)
38 {
39         printf("smbsh [options] [command [args] ...]\n\n");
40         printf(" -p prepend library name\n");
41         printf(" -a append library name\n");
42         printf(" -n");
43         printf(" -W workgroup\n");
44         printf(" -U username\n");
45         printf(" -P prefix\n");
46         printf(" -R resolve order\n");
47         printf(" -d debug level\n");
48         printf(" -l logfile\n");
49         printf(" -L libdir\n");
50         exit(0);
51 }
52
53 int main(int argc, char *argv[])
54 {
55         char *p, *u;
56         char *libd = ".";
57         char line[PATH_MAX], pre[PATH_MAX], post[PATH_MAX];
58         int opt;
59         int no_ask = 0;
60         struct stat statbuf;
61         extern char *optarg;
62         extern int optind;
63
64         *pre = *post = '\0';
65
66         while ((opt = getopt(argc, argv, "p:a:d:nL:W:U:h")) != -1) {
67                 switch (opt) {
68                 case 'p':       /* prepend library before smbwrapper.so */
69                         if (*pre != '\0')
70                                 strncat(pre, " ", PATH_MAX - strlen(pre));
71                         strncat(pre, optarg, PATH_MAX - strlen(pre));
72                         break;
73                         
74                 case 'a':       /* append library after smbwrapper.so */
75                         strncat(post, " ", PATH_MAX - strlen(post));
76                         strncat(post, optarg, PATH_MAX - strlen(post));
77                         break;
78                         
79                 case 'd':
80                         setenv("DEBUG", optarg, TRUE);
81                         break;
82
83                 case 'n':       /* don't ask for username/password */
84                         no_ask++;
85                         break;
86
87                 case 'L':
88                         libd = optarg;
89                         break;
90
91                 case 'W':
92                         setenv("WORKGROUP", optarg, TRUE);
93                         break;
94
95                 case 'U':
96                         p = strchr(optarg,'%');
97                         if (p) {
98                                 *p=0;
99                                 setenv("PASSWORD", p+1, TRUE);
100                         }
101                         setenv("USER", optarg, TRUE);
102                         break;
103
104                 case 'h':
105                 default:
106                         smbsh_usage();
107                 }
108         }
109
110
111         if (! no_ask) {
112                 if (!getenv("USER")) {
113                         printf("Username: ");
114                         u = fgets(line, sizeof(line)-1, stdin);
115                         setenv("USER", u, TRUE);
116                 }
117                 
118                 if (!getenv("PASSWORD")) {
119                         p = getpass("Password: ");
120                         setenv("PASSWORD", p, TRUE);
121                 }
122         }
123
124         strncpy(line, pre, PATH_MAX - strlen(line));
125         strncat(line, " ", PATH_MAX - strlen(line));
126         strncat(line, libd, PATH_MAX - strlen(line));
127         strncat(line, "/smbwrapper.so", PATH_MAX - strlen(line));
128         strncat(line, post, PATH_MAX - strlen(line));
129         setenv("LD_PRELOAD", line, TRUE);
130         setenv("LD_BIND_NOW", "true", TRUE);
131
132         snprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so", libd);
133
134         if (stat(line, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
135                 snprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so:DEFAULT", libd);
136                 setenv("_RLD_LIST", line, TRUE);
137                 snprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
138                 setenv("_RLDN32_LIST", line, TRUE);
139         } else {
140                 snprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
141                 setenv("_RLD_LIST", line, TRUE);
142         }
143
144         if (optind < argc) {
145                 execvp(argv[optind], &argv[optind]);
146         } else {
147                 char *shellpath = getenv("SHELL");
148
149                 setenv("PS1", "smbsh$ ", TRUE);
150
151                 if(shellpath) {
152                         execl(shellpath,"smbsh", NULL);
153                 } else {
154                         setenv("SHELL", "/bin/sh", TRUE);
155                         execl("/bin/sh", "smbsh", NULL);
156                 }
157         }
158         printf("launch failed!\n");
159         return 1;
160 }