root/examples/libsmbclient/smbwrapper/smbsh.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. smbsh_usage
  2. main

   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, see <http://www.gnu.org/licenses/>.
  20 */
  21 
  22 #include <sys/types.h>
  23 #include <sys/stat.h>
  24 #include <stdio.h>
  25 #include <stdlib.h>
  26 #include <unistd.h>
  27 #include <limits.h>
  28 #include <string.h>
  29 #include <libsmbclient.h>
  30 #include "bsd-strlfunc.h"
  31 
  32 #ifndef FALSE
  33 # define        FALSE   (0)
  34 # define        TRUE    (! FALSE)
  35 #endif
  36 
  37 static void smbsh_usage(void)
     /* [<][>][^][v][top][bottom][index][help] */
  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[])
     /* [<][>][^][v][top][bottom][index][help] */
  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                                 smbw_strlcat(pre, " ", sizeof(pre));
  71                         smbw_strlcat(pre, optarg, sizeof(pre));
  72                         break;
  73                         
  74                 case 'a':       /* append library after smbwrapper.so */
  75                         smbw_strlcat(post, " ", sizeof(post));
  76                         smbw_strlcat(post, optarg, sizeof(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         smbw_strlcpy(line, pre, PATH_MAX - strlen(line));
 125         smbw_strlcat(line, " ", sizeof(line));
 126         smbw_strlcat(line, libd, sizeof(line));
 127         smbw_strlcat(line, "/smbwrapper.so", sizeof(line));
 128         smbw_strlcat(line, post, sizeof(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,
 136                          "%s/smbwrapper.32.so:DEFAULT", libd);
 137                 setenv("_RLD_LIST", line, TRUE);
 138                 snprintf(line, sizeof(line)-1,
 139                          "%s/smbwrapper.so:DEFAULT", libd);
 140                 setenv("_RLDN32_LIST", line, TRUE);
 141         } else {
 142                 snprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd);
 143                 setenv("_RLD_LIST", line, TRUE);
 144         }
 145 
 146         if (optind < argc) {
 147                 execvp(argv[optind], &argv[optind]);
 148         } else {
 149                 char *shellpath = getenv("SHELL");
 150 
 151                 setenv("PS1", "smbsh$ ", TRUE);
 152 
 153                 if(shellpath) {
 154                         execl(shellpath,"smbsh", NULL);
 155                 } else {
 156                         setenv("SHELL", "/bin/sh", TRUE);
 157                         execl("/bin/sh", "smbsh", NULL);
 158                 }
 159         }
 160         printf("launch failed!\n");
 161         return 1;
 162 }       

/* [<][>][^][v][top][bottom][index][help] */