root/source3/lib/popt_common.c

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

DEFINITIONS

This source file includes following definitions.
  1. set_logfile
  2. popt_common_callback
  3. popt_dynconfig_callback
  4. get_password_file
  5. get_credentials_file
  6. popt_common_credentials_callback
  7. popt_common_set_auth_info

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    Common popt routines
   4 
   5    Copyright (C) Tim Potter 2001,2002
   6    Copyright (C) Jelmer Vernooij 2002,2003
   7    Copyright (C) James Peach 2006
   8 
   9    This program is free software; you can redistribute it and/or modify
  10    it under the terms of the GNU General Public License as published by
  11    the Free Software Foundation; either version 3 of the License, or
  12    (at your option) any later version.
  13    
  14    This program is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18    
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21 */
  22 
  23 #include "includes.h"
  24 
  25 /* Handle command line options:
  26  *              -d,--debuglevel 
  27  *              -s,--configfile 
  28  *              -O,--socket-options 
  29  *              -V,--version
  30  *              -l,--log-base
  31  *              -n,--netbios-name
  32  *              -W,--workgroup
  33  *              -i,--scope
  34  */
  35 
  36 extern bool AllowDebugChange;
  37 extern bool override_logfile;
  38 
  39 static void set_logfile(poptContext con, const char * arg)
     /* [<][>][^][v][top][bottom][index][help] */
  40 {
  41 
  42         char *lfile = NULL;
  43         const char *pname;
  44 
  45         /* Find out basename of current program */
  46         pname = strrchr_m(poptGetInvocationName(con),'/');
  47 
  48         if (!pname)
  49                 pname = poptGetInvocationName(con);
  50         else
  51                 pname++;
  52 
  53         if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
  54                 return;
  55         }
  56         lp_set_logfile(lfile);
  57         SAFE_FREE(lfile);
  58 }
  59 
  60 static bool PrintSambaVersionString;
  61 
  62 static void popt_common_callback(poptContext con,
     /* [<][>][^][v][top][bottom][index][help] */
  63                            enum poptCallbackReason reason,
  64                            const struct poptOption *opt,
  65                            const char *arg, const void *data)
  66 {
  67 
  68         if (reason == POPT_CALLBACK_REASON_PRE) {
  69                 set_logfile(con, get_dyn_LOGFILEBASE());
  70                 return;
  71         }
  72 
  73         if (reason == POPT_CALLBACK_REASON_POST) {
  74 
  75                 if (PrintSambaVersionString) {
  76                         printf( "Version %s\n", samba_version_string());
  77                         exit(0);
  78                 }
  79 
  80                 if (is_default_dyn_CONFIGFILE()) {
  81                         if(getenv("SMB_CONF_PATH")) {
  82                                 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
  83                         }
  84                 }
  85 
  86                 /* Further 'every Samba program must do this' hooks here. */
  87                 return;
  88         }
  89 
  90         switch(opt->val) {
  91         case 'd':
  92                 if (arg) {
  93                         debug_parse_levels(arg);
  94                         AllowDebugChange = False;
  95                 }
  96                 break;
  97 
  98         case 'V':
  99                 PrintSambaVersionString = True;
 100                 break;
 101 
 102         case 'O':
 103                 if (arg) {
 104                         lp_do_parameter(-1, "socket options", arg);
 105                 }
 106                 break;
 107 
 108         case 's':
 109                 if (arg) {
 110                         set_dyn_CONFIGFILE(arg);
 111                 }
 112                 break;
 113 
 114         case 'n':
 115                 if (arg) {
 116                         set_global_myname(arg);
 117                 }
 118                 break;
 119 
 120         case 'l':
 121                 if (arg) {
 122                         set_logfile(con, arg);
 123                         override_logfile = True;
 124                         set_dyn_LOGFILEBASE(arg);
 125                 }
 126                 break;
 127 
 128         case 'i':
 129                 if (arg) {
 130                           set_global_scope(arg);
 131                 }
 132                 break;
 133 
 134         case 'W':
 135                 if (arg) {
 136                         set_global_myworkgroup(arg);
 137                 }
 138                 break;
 139         }
 140 }
 141 
 142 struct poptOption popt_common_connection[] = {
 143         { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
 144         { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
 145           "SOCKETOPTIONS" },
 146         { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
 147         { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
 148         { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
 149 
 150         POPT_TABLEEND
 151 };
 152 
 153 struct poptOption popt_common_samba[] = {
 154         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
 155         { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
 156         { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
 157         { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
 158         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
 159         POPT_TABLEEND
 160 };
 161 
 162 struct poptOption popt_common_configfile[] = {
 163         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
 164         { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
 165         POPT_TABLEEND
 166 };
 167 
 168 struct poptOption popt_common_version[] = {
 169         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
 170         { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
 171         POPT_TABLEEND
 172 };
 173 
 174 struct poptOption popt_common_debuglevel[] = {
 175         { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
 176         { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
 177         POPT_TABLEEND
 178 };
 179 
 180 
 181 /* Handle command line options:
 182  *              --sbindir
 183  *              --bindir
 184  *              --swatdir
 185  *              --lmhostsfile
 186  *              --libdir
 187  *              --modulesdir
 188  *              --shlibext
 189  *              --lockdir
 190  *              --statedir
 191  *              --cachedir
 192  *              --piddir
 193  *              --smb-passwd-file
 194  *              --private-dir
 195  */
 196 
 197 enum dyn_item{
 198         DYN_SBINDIR = 1,
 199         DYN_BINDIR,
 200         DYN_SWATDIR,
 201         DYN_LMHOSTSFILE,
 202         DYN_LIBDIR,
 203         DYN_MODULESDIR,
 204         DYN_SHLIBEXT,
 205         DYN_LOCKDIR,
 206         DYN_STATEDIR,
 207         DYN_CACHEDIR,
 208         DYN_PIDDIR,
 209         DYN_SMB_PASSWD_FILE,
 210         DYN_PRIVATE_DIR,
 211 };
 212 
 213 
 214 static void popt_dynconfig_callback(poptContext con,
     /* [<][>][^][v][top][bottom][index][help] */
 215                            enum poptCallbackReason reason,
 216                            const struct poptOption *opt,
 217                            const char *arg, const void *data)
 218 {
 219 
 220         switch (opt->val) {
 221         case DYN_SBINDIR:
 222                 if (arg) {
 223                         set_dyn_SBINDIR(arg);
 224                 }
 225                 break;
 226 
 227         case DYN_BINDIR:
 228                 if (arg) {
 229                         set_dyn_BINDIR(arg);
 230                 }
 231                 break;
 232 
 233         case DYN_SWATDIR:
 234                 if (arg) {
 235                         set_dyn_SWATDIR(arg);
 236                 }
 237                 break;
 238 
 239         case DYN_LMHOSTSFILE:
 240                 if (arg) {
 241                         set_dyn_LMHOSTSFILE(arg);
 242                 }
 243                 break;
 244 
 245         case DYN_LIBDIR:
 246                 if (arg) {
 247                         set_dyn_LIBDIR(arg);
 248                 }
 249                 break;
 250 
 251         case DYN_MODULESDIR:
 252                 if (arg) {
 253                         set_dyn_MODULESDIR(arg);
 254                 }
 255                 break;
 256 
 257         case DYN_SHLIBEXT:
 258                 if (arg) {
 259                         set_dyn_SHLIBEXT(arg);
 260                 }
 261                 break;
 262 
 263         case DYN_LOCKDIR:
 264                 if (arg) {
 265                         set_dyn_LOCKDIR(arg);
 266                 }
 267                 break;
 268 
 269         case DYN_STATEDIR:
 270                 if (arg) {
 271                         set_dyn_STATEDIR(arg);
 272                 }
 273                 break;
 274 
 275         case DYN_CACHEDIR:
 276                 if (arg) {
 277                         set_dyn_CACHEDIR(arg);
 278                 }
 279                 break;
 280 
 281         case DYN_PIDDIR:
 282                 if (arg) {
 283                         set_dyn_PIDDIR(arg);
 284                 }
 285                 break;
 286 
 287         case DYN_SMB_PASSWD_FILE:
 288                 if (arg) {
 289                         set_dyn_SMB_PASSWD_FILE(arg);
 290                 }
 291                 break;
 292 
 293         case DYN_PRIVATE_DIR:
 294                 if (arg) {
 295                         set_dyn_PRIVATE_DIR(arg);
 296                 }
 297                 break;
 298 
 299         }
 300 }
 301 
 302 const struct poptOption popt_common_dynconfig[] = {
 303 
 304         { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
 305 
 306         { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
 307             "Path to sbin directory", "SBINDIR" },
 308         { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
 309             "Path to bin directory", "BINDIR" },
 310         { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
 311             "Path to SWAT installation directory", "SWATDIR" },
 312         { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
 313             "Path to lmhosts file", "LMHOSTSFILE" },
 314         { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
 315             "Path to shared library directory", "LIBDIR" },
 316         { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
 317             "Path to shared modules directory", "MODULESDIR" },
 318         { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
 319             "Shared library extension", "SHLIBEXT" },
 320         { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
 321             "Path to lock file directory", "LOCKDIR" },
 322         { "statedir", '\0' , POPT_ARG_STRING, NULL, DYN_STATEDIR,
 323             "Path to persistent state file directory", "STATEDIR" },
 324         { "cachedir", '\0' , POPT_ARG_STRING, NULL, DYN_CACHEDIR,
 325             "Path to temporary cache file directory", "CACHEDIR" },
 326         { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
 327             "Path to PID file directory", "PIDDIR" },
 328         { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
 329             "Path to smbpasswd file", "SMB_PASSWD_FILE" },
 330         { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
 331             "Path to private data directory", "PRIVATE_DIR" },
 332 
 333         POPT_TABLEEND
 334 };
 335 
 336 /****************************************************************************
 337  * get a password from a a file or file descriptor
 338  * exit on failure
 339  * ****************************************************************************/
 340 
 341 static void get_password_file(struct user_auth_info *auth_info)
     /* [<][>][^][v][top][bottom][index][help] */
 342 {
 343         int fd = -1;
 344         char *p;
 345         bool close_it = False;
 346         char *spec = NULL;
 347         char pass[128];
 348 
 349         if ((p = getenv("PASSWD_FD")) != NULL) {
 350                 if (asprintf(&spec, "descriptor %s", p) < 0) {
 351                         return;
 352                 }
 353                 sscanf(p, "%d", &fd);
 354                 close_it = false;
 355         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
 356                 fd = sys_open(p, O_RDONLY, 0);
 357                 spec = SMB_STRDUP(p);
 358                 if (fd < 0) {
 359                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
 360                                         spec, strerror(errno));
 361                         exit(1);
 362                 }
 363                 close_it = True;
 364         }
 365 
 366         if (fd < 0) {
 367                 fprintf(stderr, "fd = %d, < 0\n", fd);
 368                 exit(1);
 369         }
 370 
 371         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
 372                 p && p - pass < sizeof(pass);) {
 373                 switch (read(fd, p, 1)) {
 374                 case 1:
 375                         if (*p != '\n' && *p != '\0') {
 376                                 *++p = '\0'; /* advance p, and null-terminate pass */
 377                                 break;
 378                         }
 379                 case 0:
 380                         if (p - pass) {
 381                                 *p = '\0'; /* null-terminate it, just in case... */
 382                                 p = NULL; /* then force the loop condition to become false */
 383                                 break;
 384                         } else {
 385                                 fprintf(stderr, "Error reading password from file %s: %s\n",
 386                                                 spec, "empty password\n");
 387                                 SAFE_FREE(spec);
 388                                 exit(1);
 389                         }
 390 
 391                 default:
 392                         fprintf(stderr, "Error reading password from file %s: %s\n",
 393                                         spec, strerror(errno));
 394                         SAFE_FREE(spec);
 395                         exit(1);
 396                 }
 397         }
 398         SAFE_FREE(spec);
 399 
 400         set_cmdline_auth_info_password(auth_info, pass);
 401         if (close_it) {
 402                 close(fd);
 403         }
 404 }
 405 
 406 static void get_credentials_file(struct user_auth_info *auth_info,
     /* [<][>][^][v][top][bottom][index][help] */
 407                                  const char *file)
 408 {
 409         XFILE *auth;
 410         fstring buf;
 411         uint16 len = 0;
 412         char *ptr, *val, *param;
 413 
 414         if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
 415         {
 416                 /* fail if we can't open the credentials file */
 417                 d_printf("ERROR: Unable to open credentials file!\n");
 418                 exit(-1);
 419         }
 420 
 421         while (!x_feof(auth))
 422         {
 423                 /* get a line from the file */
 424                 if (!x_fgets(buf, sizeof(buf), auth))
 425                         continue;
 426                 len = strlen(buf);
 427 
 428                 if ((len) && (buf[len-1]=='\n'))
 429                 {
 430                         buf[len-1] = '\0';
 431                         len--;
 432                 }
 433                 if (len == 0)
 434                         continue;
 435 
 436                 /* break up the line into parameter & value.
 437                  * will need to eat a little whitespace possibly */
 438                 param = buf;
 439                 if (!(ptr = strchr_m (buf, '=')))
 440                         continue;
 441 
 442                 val = ptr+1;
 443                 *ptr = '\0';
 444 
 445                 /* eat leading white space */
 446                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
 447                         val++;
 448 
 449                 if (strwicmp("password", param) == 0) {
 450                         set_cmdline_auth_info_password(auth_info, val);
 451                 } else if (strwicmp("username", param) == 0) {
 452                         set_cmdline_auth_info_username(auth_info, val);
 453                 } else if (strwicmp("domain", param) == 0) {
 454                         set_global_myworkgroup(val);
 455                 }
 456                 memset(buf, 0, sizeof(buf));
 457         }
 458         x_fclose(auth);
 459 }
 460 
 461 /* Handle command line options:
 462  *              -U,--user
 463  *              -A,--authentication-file
 464  *              -k,--use-kerberos
 465  *              -N,--no-pass
 466  *              -S,--signing
 467  *              -P --machine-pass
 468  *              -e --encrypt
 469  */
 470 
 471 
 472 static void popt_common_credentials_callback(poptContext con,
     /* [<][>][^][v][top][bottom][index][help] */
 473                                         enum poptCallbackReason reason,
 474                                         const struct poptOption *opt,
 475                                         const char *arg, const void *data)
 476 {
 477         struct user_auth_info *auth_info = talloc_get_type_abort(
 478                 *((const char **)data), struct user_auth_info);
 479         char *p;
 480 
 481         if (reason == POPT_CALLBACK_REASON_PRE) {
 482                 set_cmdline_auth_info_username(auth_info, "GUEST");
 483 
 484                 if (getenv("LOGNAME")) {
 485                         set_cmdline_auth_info_username(auth_info,
 486                                                        getenv("LOGNAME"));
 487                 }
 488 
 489                 if (getenv("USER")) {
 490                         char *puser = SMB_STRDUP(getenv("USER"));
 491                         if (!puser) {
 492                                 exit(ENOMEM);
 493                         }
 494                         set_cmdline_auth_info_username(auth_info, puser);
 495 
 496                         if ((p = strchr_m(puser,'%'))) {
 497                                 size_t len;
 498                                 *p = 0;
 499                                 len = strlen(p+1);
 500                                 set_cmdline_auth_info_password(auth_info, p+1);
 501                                 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
 502                         }
 503                         SAFE_FREE(puser);
 504                 }
 505 
 506                 if (getenv("PASSWD")) {
 507                         set_cmdline_auth_info_password(auth_info,
 508                                                        getenv("PASSWD"));
 509                 }
 510 
 511                 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
 512                         get_password_file(auth_info);
 513                 }
 514 
 515                 return;
 516         }
 517 
 518         switch(opt->val) {
 519         case 'U':
 520                 {
 521                         char *lp;
 522                         char *puser = SMB_STRDUP(arg);
 523 
 524                         if ((lp=strchr_m(puser,'%'))) {
 525                                 size_t len;
 526                                 *lp = 0;
 527                                 set_cmdline_auth_info_username(auth_info,
 528                                                                puser);
 529                                 set_cmdline_auth_info_password(auth_info,
 530                                                                lp+1);
 531                                 len = strlen(lp+1);
 532                                 memset(strchr_m(arg,'%')+1,'X',len);
 533                         } else {
 534                                 set_cmdline_auth_info_username(auth_info,
 535                                                                puser);
 536                         }
 537                         SAFE_FREE(puser);
 538                 }
 539                 break;
 540 
 541         case 'A':
 542                 get_credentials_file(auth_info, arg);
 543                 break;
 544 
 545         case 'k':
 546 #ifndef HAVE_KRB5
 547                 d_printf("No kerberos support compiled in\n");
 548                 exit(1);
 549 #else
 550                 set_cmdline_auth_info_use_krb5_ticket(auth_info);
 551 #endif
 552                 break;
 553 
 554         case 'S':
 555                 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
 556                         fprintf(stderr, "Unknown signing option %s\n", arg );
 557                         exit(1);
 558                 }
 559                 break;
 560         case 'P':
 561                 set_cmdline_auth_info_use_machine_account(auth_info);
 562                 break;
 563         case 'N':
 564                 set_cmdline_auth_info_password(auth_info, "");
 565                 break;
 566         case 'e':
 567                 set_cmdline_auth_info_smb_encrypt(auth_info);
 568                 break;
 569 
 570         }
 571 }
 572 
 573 static struct user_auth_info *global_auth_info;
 574 
 575 void popt_common_set_auth_info(struct user_auth_info *auth_info)
     /* [<][>][^][v][top][bottom][index][help] */
 576 {
 577         global_auth_info = auth_info;
 578 }
 579 
 580 struct poptOption popt_common_credentials[] = {
 581         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
 582           (void *)popt_common_credentials_callback, 0,
 583           (const char *)&global_auth_info },
 584         { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
 585         { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
 586         { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
 587         { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
 588         { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
 589         {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
 590         {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
 591         POPT_TABLEEND
 592 };

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