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