Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / libsmbclient / smbwrapper / smbsh.c
blob23b1ac26c796ad4f181697df29d8be79dbed1ce5
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
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.
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.
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.
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"
33 #ifndef FALSE
34 # define FALSE (0)
35 # define TRUE (! FALSE)
36 #endif
38 static void smbsh_usage(void)
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);
54 int main(int argc, char *argv[])
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;
65 *pre = *post = '\0';
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;
75 case 'a': /* append library after smbwrapper.so */
76 smbw_strlcat(post, " ", sizeof(post));
77 smbw_strlcat(post, optarg, sizeof(post));
78 break;
80 case 'd':
81 setenv("DEBUG", optarg, TRUE);
82 break;
84 case 'n': /* don't ask for username/password */
85 no_ask++;
86 break;
88 case 'L':
89 libd = optarg;
90 break;
92 case 'W':
93 setenv("WORKGROUP", optarg, TRUE);
94 break;
96 case 'U':
97 p = strchr(optarg,'%');
98 if (p) {
99 *p=0;
100 setenv("PASSWORD", p+1, TRUE);
102 setenv("USER", optarg, TRUE);
103 break;
105 case 'h':
106 default:
107 smbsh_usage();
112 if (! no_ask) {
113 if (!getenv("USER")) {
114 printf("Username: ");
115 u = fgets(line, sizeof(line)-1, stdin);
116 setenv("USER", u, TRUE);
119 if (!getenv("PASSWORD")) {
120 p = getpass("Password: ");
121 setenv("PASSWORD", p, TRUE);
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);
133 snprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so", libd);
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);
147 if (optind < argc) {
148 execvp(argv[optind], &argv[optind]);
149 } else {
150 char *shellpath = getenv("SHELL");
152 setenv("PS1", "smbsh$ ", TRUE);
154 if(shellpath) {
155 execl(shellpath,"smbsh", NULL);
156 } else {
157 setenv("SHELL", "/bin/sh", TRUE);
158 execl("/bin/sh", "smbsh", NULL);
161 printf("launch failed!\n");
162 return 1;