Changeset 740 for vendor/current/source3/lib/util.c
- Timestamp:
- Nov 14, 2012, 12:59:34 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/lib/util.c
r597 r740 12 12 the Free Software Foundation; either version 3 of the License, or 13 13 (at your option) any later version. 14 14 15 15 This program is distributed in the hope that it will be useful, 16 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 18 GNU General Public License for more details. 19 19 20 20 You should have received a copy of the GNU General Public License 21 21 along with this program. If not, see <http://www.gnu.org/licenses/>. … … 23 23 24 24 #include "includes.h" 25 #include "system/passwd.h" 26 #include "system/filesys.h" 27 #include "util_tdb.h" 28 #include "ctdbd_conn.h" 29 #include "../lib/util/util_pw.h" 30 #include "messages.h" 25 31 26 32 extern char *global_clobber_region_function; … … 74 80 ***********************************************************************/ 75 81 76 static char *smb_myname;77 static char *smb_myworkgroup;78 82 static char *smb_scope; 79 83 static int smb_num_netbios_names; 80 84 static char **smb_my_netbios_names; 81 82 /***********************************************************************83 Allocate and set myname. Ensure upper case.84 ***********************************************************************/85 86 bool set_global_myname(const char *myname)87 {88 SAFE_FREE(smb_myname);89 smb_myname = SMB_STRDUP(myname);90 if (!smb_myname)91 return False;92 strupper_m(smb_myname);93 return True;94 }95 96 const char *global_myname(void)97 {98 return smb_myname;99 }100 101 /***********************************************************************102 Allocate and set myworkgroup. Ensure upper case.103 ***********************************************************************/104 105 bool set_global_myworkgroup(const char *myworkgroup)106 {107 SAFE_FREE(smb_myworkgroup);108 smb_myworkgroup = SMB_STRDUP(myworkgroup);109 if (!smb_myworkgroup)110 return False;111 strupper_m(smb_myworkgroup);112 return True;113 }114 115 const char *lp_workgroup(void)116 {117 return smb_myworkgroup;118 }119 85 120 86 /*********************************************************************** … … 185 151 void gfree_names(void) 186 152 { 187 SAFE_FREE( smb_myname ); 188 SAFE_FREE( smb_myworkgroup ); 153 gfree_netbios_names(); 189 154 SAFE_FREE( smb_scope ); 190 155 free_netbios_names_array(); … … 262 227 if (global_myname() == NULL || *global_myname() == '\0') { 263 228 if (!set_global_myname(myhostname())) { 264 DEBUG( 0, ( "init_ structs: malloc fail.\n" ) );229 DEBUG( 0, ( "init_names: malloc fail.\n" ) ); 265 230 return False; 266 231 } … … 268 233 269 234 if (!set_netbios_aliases(lp_netbios_aliases())) { 270 DEBUG( 0, ( "init_ structs: malloc fail.\n" ) );235 DEBUG( 0, ( "init_names: malloc fail.\n" ) ); 271 236 return False; 272 237 } … … 281 246 282 247 return( True ); 283 }284 285 /**************************************************************************n286 Code to cope with username/password auth options from the commandline.287 Used mainly in client tools.288 ****************************************************************************/289 290 struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)291 {292 struct user_auth_info *result;293 294 result = TALLOC_ZERO_P(mem_ctx, struct user_auth_info);295 if (result == NULL) {296 return NULL;297 }298 299 result->signing_state = Undefined;300 return result;301 }302 303 const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)304 {305 if (!auth_info->username) {306 return "";307 }308 return auth_info->username;309 }310 311 void set_cmdline_auth_info_username(struct user_auth_info *auth_info,312 const char *username)313 {314 TALLOC_FREE(auth_info->username);315 auth_info->username = talloc_strdup(auth_info, username);316 if (!auth_info->username) {317 exit(ENOMEM);318 }319 }320 321 const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)322 {323 if (!auth_info->domain) {324 return "";325 }326 return auth_info->domain;327 }328 329 void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,330 const char *domain)331 {332 TALLOC_FREE(auth_info->domain);333 auth_info->domain = talloc_strdup(auth_info, domain);334 if (!auth_info->domain) {335 exit(ENOMEM);336 }337 }338 339 const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)340 {341 if (!auth_info->password) {342 return "";343 }344 return auth_info->password;345 }346 347 void set_cmdline_auth_info_password(struct user_auth_info *auth_info,348 const char *password)349 {350 TALLOC_FREE(auth_info->password);351 if (password == NULL) {352 password = "";353 }354 auth_info->password = talloc_strdup(auth_info, password);355 if (!auth_info->password) {356 exit(ENOMEM);357 }358 auth_info->got_pass = true;359 }360 361 bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,362 const char *arg)363 {364 auth_info->signing_state = -1;365 if (strequal(arg, "off") || strequal(arg, "no") ||366 strequal(arg, "false")) {367 auth_info->signing_state = false;368 } else if (strequal(arg, "on") || strequal(arg, "yes") ||369 strequal(arg, "true") || strequal(arg, "auto")) {370 auth_info->signing_state = true;371 } else if (strequal(arg, "force") || strequal(arg, "required") ||372 strequal(arg, "forced")) {373 auth_info->signing_state = Required;374 } else {375 return false;376 }377 return true;378 }379 380 int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)381 {382 return auth_info->signing_state;383 }384 385 void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)386 {387 auth_info->use_ccache = b;388 }389 390 bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)391 {392 return auth_info->use_ccache;393 }394 395 void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,396 bool b)397 {398 auth_info->use_kerberos = b;399 }400 401 bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)402 {403 return auth_info->use_kerberos;404 }405 406 void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,407 bool b)408 {409 auth_info->fallback_after_kerberos = b;410 }411 412 bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)413 {414 return auth_info->fallback_after_kerberos;415 }416 417 /* This should only be used by lib/popt_common.c JRA */418 void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)419 {420 auth_info->use_kerberos = true;421 auth_info->got_pass = true;422 }423 424 /* This should only be used by lib/popt_common.c JRA */425 void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)426 {427 auth_info->smb_encrypt = true;428 }429 430 void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)431 {432 auth_info->use_machine_account = true;433 }434 435 bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)436 {437 return auth_info->got_pass;438 }439 440 bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)441 {442 return auth_info->smb_encrypt;443 }444 445 bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)446 {447 return auth_info->use_machine_account;448 }449 450 struct user_auth_info *get_cmdline_auth_info_copy(TALLOC_CTX *mem_ctx,451 const struct user_auth_info *src)452 {453 struct user_auth_info *result;454 455 result = user_auth_info_init(mem_ctx);456 if (result == NULL) {457 return NULL;458 }459 460 *result = *src;461 462 result->username = talloc_strdup(463 result, get_cmdline_auth_info_username(src));464 result->password = talloc_strdup(465 result, get_cmdline_auth_info_password(src));466 if ((result->username == NULL) || (result->password == NULL)) {467 TALLOC_FREE(result);468 return NULL;469 }470 471 return result;472 }473 474 bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)475 {476 char *pass = NULL;477 char *account = NULL;478 479 if (!get_cmdline_auth_info_use_machine_account(auth_info)) {480 return false;481 }482 483 if (!secrets_init()) {484 d_printf("ERROR: Unable to open secrets database\n");485 return false;486 }487 488 if (asprintf(&account, "%s$@%s", global_myname(), lp_realm()) < 0) {489 return false;490 }491 492 pass = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);493 if (!pass) {494 d_printf("ERROR: Unable to fetch machine password for "495 "%s in domain %s\n",496 account, lp_workgroup());497 SAFE_FREE(account);498 return false;499 }500 501 set_cmdline_auth_info_username(auth_info, account);502 set_cmdline_auth_info_password(auth_info, pass);503 504 SAFE_FREE(account);505 SAFE_FREE(pass);506 507 return true;508 }509 510 /****************************************************************************511 Ensure we have a password if one not given.512 ****************************************************************************/513 514 void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)515 {516 char *label = NULL;517 char *pass;518 TALLOC_CTX *frame;519 520 if (get_cmdline_auth_info_got_pass(auth_info) ||521 get_cmdline_auth_info_use_kerberos(auth_info)) {522 /* Already got one... */523 return;524 }525 526 frame = talloc_stackframe();527 label = talloc_asprintf(frame, "Enter %s's password: ",528 get_cmdline_auth_info_username(auth_info));529 pass = getpass(label);530 if (pass) {531 set_cmdline_auth_info_password(auth_info, pass);532 }533 TALLOC_FREE(frame);534 248 } 535 249 … … 596 310 attrstr[0] = 0; 597 311 598 if (mode & aVOLID) fstrcat(attrstr,"V");599 if (mode & aDIR) fstrcat(attrstr,"D");600 if (mode & aARCH) fstrcat(attrstr,"A");601 if (mode & aHIDDEN) fstrcat(attrstr,"H");602 if (mode & aSYSTEM) fstrcat(attrstr,"S");603 if (mode & aRONLY) fstrcat(attrstr,"R");312 if (mode & FILE_ATTRIBUTE_VOLUME) fstrcat(attrstr,"V"); 313 if (mode & FILE_ATTRIBUTE_DIRECTORY) fstrcat(attrstr,"D"); 314 if (mode & FILE_ATTRIBUTE_ARCHIVE) fstrcat(attrstr,"A"); 315 if (mode & FILE_ATTRIBUTE_HIDDEN) fstrcat(attrstr,"H"); 316 if (mode & FILE_ATTRIBUTE_SYSTEM) fstrcat(attrstr,"S"); 317 if (mode & FILE_ATTRIBUTE_READONLY) fstrcat(attrstr,"R"); 604 318 605 319 return talloc_strdup(talloc_tos(), attrstr); … … 617 331 if (!DEBUGLVL(5)) 618 332 return; 619 333 620 334 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n", 621 335 smb_len(buf), … … 636 350 DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i, 637 351 SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i))); 638 352 639 353 bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct))); 640 354 … … 863 577 } 864 578 865 /*******************************************************************866 Sleep for a specified number of milliseconds.867 ********************************************************************/868 869 void smb_msleep(unsigned int t)870 {871 #if defined(HAVE_NANOSLEEP)872 struct timespec tval;873 int ret;874 875 tval.tv_sec = t/1000;876 tval.tv_nsec = 1000000*(t%1000);877 878 do {879 errno = 0;880 ret = nanosleep(&tval, &tval);881 } while (ret < 0 && errno == EINTR && (tval.tv_sec > 0 || tval.tv_nsec > 0));882 #else883 unsigned int tdiff=0;884 struct timeval tval,t1,t2;885 fd_set fds;886 887 GetTimeOfDay(&t1);888 t2 = t1;889 890 while (tdiff < t) {891 tval.tv_sec = (t-tdiff)/1000;892 tval.tv_usec = 1000*((t-tdiff)%1000);893 894 /* Never wait for more than 1 sec. */895 if (tval.tv_sec > 1) {896 tval.tv_sec = 1;897 tval.tv_usec = 0;898 }899 900 FD_ZERO(&fds);901 errno = 0;902 sys_select_intr(0,&fds,NULL,NULL,&tval);903 904 GetTimeOfDay(&t2);905 if (t2.tv_sec < t1.tv_sec) {906 /* Someone adjusted time... */907 t1 = t2;908 }909 910 tdiff = TvalDiff(&t1,&t2);911 }912 #endif913 }914 579 915 580 NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx, 916 struct event_context *ev_ctx, 917 bool parent_longlived) 581 struct event_context *ev_ctx, 582 struct server_id id, 583 bool parent_longlived) 918 584 { 919 585 NTSTATUS status = NT_STATUS_OK; … … 932 598 } 933 599 934 if (ev_ctx ) {935 event_context_reinit(ev_ctx);600 if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) { 601 smb_panic(__location__ ": Failed to re-initialise event context"); 936 602 } 937 603 … … 941 607 * fork 942 608 */ 943 status = messaging_reinit(msg_ctx );609 status = messaging_reinit(msg_ctx, id); 944 610 if (!NT_STATUS_IS_OK(status)) { 945 611 DEBUG(0,("messaging_reinit() failed: %s\n", … … 949 615 done: 950 616 return status; 951 }952 953 /****************************************************************************954 Put up a yes/no prompt.955 ****************************************************************************/956 957 bool yesno(const char *p)958 {959 char ans[20];960 printf("%s",p);961 962 if (!fgets(ans,sizeof(ans)-1,stdin))963 return(False);964 965 if (*ans == 'y' || *ans == 'Y')966 return(True);967 968 return(False);969 617 } 970 618 … … 1208 856 if (strequal(str,"CORE+")) 1209 857 return(PROTOCOL_COREPLUS); 1210 858 1211 859 DEBUG(0,("Unrecognised protocol level %s\n",str)); 1212 860 1213 861 return(def); 1214 862 } … … 1353 1001 1354 1002 #ifdef CLUSTER_SUPPORT 1355 return ctdbd_process_exists(messaging_ctdbd_connection(), pid.vnn,1356 pid. pid);1003 return ctdbd_process_exists(messaging_ctdbd_connection(), 1004 pid.vnn, pid.pid); 1357 1005 #else 1358 1006 return False; … … 1411 1059 uid_t u; 1412 1060 1413 pass = Get_Pwnam_alloc(talloc_ autofree_context(), name);1061 pass = Get_Pwnam_alloc(talloc_tos(), name); 1414 1062 if (pass) { 1415 1063 u = pass->pw_uid; … … 1571 1219 DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 1572 1220 (unsigned long)backtrace_size)); 1573 1221 1574 1222 if (backtrace_strings) { 1575 1223 int i; … … 1635 1283 if (!p) 1636 1284 return(NULL); 1637 1285 1638 1286 ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p); 1639 1287 if (!ptr) … … 1709 1357 if possible. 1710 1358 ********************************************************************/ 1711 1359 1712 1360 void set_namearray(name_compare_entry **ppname_array, const char *namelist) 1713 1361 { … … 1785 1433 i++; 1786 1434 } 1787 1435 1788 1436 (*ppname_array)[i].name = NULL; 1789 1437 … … 1845 1493 *pcount = lock.l_len; 1846 1494 *ppid = lock.l_pid; 1847 1495 1848 1496 DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n", 1849 1497 fd, (int)lock.l_type, (unsigned int)lock.l_pid)); … … 1963 1611 remote_arch_str = "CIFSFS"; 1964 1612 break; 1613 case RA_OSX: 1614 remote_arch_str = "OSX"; 1615 break; 1965 1616 default: 1966 1617 ra_type = RA_UNKNOWN; … … 2002 1653 { 2003 1654 TDB_DATA key = string_tdb_data(s); 2004 return jenkins_hash(&key);1655 return tdb_jenkins_hash(&key); 2005 1656 } 2006 1657 … … 2146 1797 static char *ret; 2147 1798 if (ret == NULL) { 2148 /* This is cached forever so 2149 * use talloc_autofree_context() ctx. */ 2150 ret = get_myname(talloc_autofree_context()); 1799 ret = get_myname(NULL); 2151 1800 } 2152 1801 return ret; … … 2295 1944 char *p; 2296 1945 ptrdiff_t len; 2297 1946 2298 1947 p = strrchr_m(dir, '/'); /* Find final '/', if any */ 2299 1948 … … 2371 2020 bool mask_match(const char *string, const char *pattern, bool is_case_sensitive) 2372 2021 { 2373 if ( strcmp(string,"..") == 0)2022 if (ISDOTDOT(string)) 2374 2023 string = "."; 2375 if ( strcmp(pattern,".") == 0)2024 if (ISDOT(pattern)) 2376 2025 return False; 2377 2026 2378 2027 return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0; 2379 2028 } … … 2387 2036 bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive) 2388 2037 { 2389 if ( strcmp(string,"..") == 0)2038 if (ISDOTDOT(string)) 2390 2039 string = "."; 2391 if ( strcmp(pattern,".") == 0)2040 if (ISDOT(pattern)) 2392 2041 return False; 2393 2042 2394 2043 return ms_fnmatch(pattern, string, True, is_case_sensitive) == 0; 2395 2044 } … … 2660 2309 } 2661 2310 2311 static uint64_t my_unique_id = 0; 2312 2313 void set_my_unique_id(uint64_t unique_id) 2314 { 2315 my_unique_id = unique_id; 2316 } 2317 2662 2318 struct server_id pid_to_procid(pid_t pid) 2663 2319 { 2664 2320 struct server_id result; 2665 2321 result.pid = pid; 2666 #ifdef CLUSTER_SUPPORT 2322 result.unique_id = my_unique_id; 2667 2323 result.vnn = my_vnn; 2668 #endif2669 2324 return result; 2670 2325 } … … 2673 2328 { 2674 2329 return pid_to_procid(sys_getpid()); 2675 }2676 2677 struct server_id server_id_self(void)2678 {2679 return procid_self();2680 2330 } 2681 2331 … … 2684 2334 if (p1->pid != p2->pid) 2685 2335 return False; 2686 #ifdef CLUSTER_SUPPORT2687 2336 if (p1->vnn != p2->vnn) 2688 2337 return False; 2689 #endif2690 2338 return True; 2691 2339 } … … 2701 2349 if (pid->pid != sys_getpid()) 2702 2350 return False; 2703 #ifdef CLUSTER_SUPPORT2704 2351 if (pid->vnn != my_vnn) 2705 2352 return False; 2706 #endif2707 2353 return True; 2708 2354 } … … 2712 2358 struct server_id result; 2713 2359 int pid; 2714 #ifdef CLUSTER_SUPPORT2715 2360 unsigned int vnn; 2716 2361 if (sscanf(pid_string, "%u:%d", &vnn, &pid) == 2) { … … 2726 2371 result.pid = -1; 2727 2372 } 2728 #else2729 if (sscanf(pid_string, "%d", &pid) != 1) {2730 result.pid = -1;2731 } else {2732 result.pid = pid;2733 }2734 #endif2735 2373 /* Assigning to result.pid may have overflowed 2736 2374 Map negative pid to -1: i.e. error */ … … 2738 2376 result.pid = -1; 2739 2377 } 2378 result.unique_id = 0; 2740 2379 return result; 2741 2380 } … … 2743 2382 char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid) 2744 2383 { 2745 #ifdef CLUSTER_SUPPORT2746 2384 if (pid->vnn == NONCLUSTER_VNN) { 2747 2385 return talloc_asprintf(mem_ctx, … … 2755 2393 (int)pid->pid); 2756 2394 } 2757 #else2758 return talloc_asprintf(mem_ctx,2759 "%d",2760 (int)pid->pid);2761 #endif2762 2395 } 2763 2396 … … 2774 2407 bool procid_is_local(const struct server_id *pid) 2775 2408 { 2776 #ifdef CLUSTER_SUPPORT2777 2409 return pid->vnn == my_vnn; 2778 #else2779 return True;2780 #endif2781 }2782 2783 int this_is_smp(void)2784 {2785 #if defined(HAVE_SYSCONF)2786 2787 #if defined(SYSCONF_SC_NPROC_ONLN)2788 return (sysconf(_SC_NPROC_ONLN) > 1) ? 1 : 0;2789 #elif defined(SYSCONF_SC_NPROCESSORS_ONLN)2790 return (sysconf(_SC_NPROCESSORS_ONLN) > 1) ? 1 : 0;2791 #else2792 return 0;2793 #endif2794 2795 #else2796 return 0;2797 #endif2798 2410 } 2799 2411 … … 3029 2641 #endif 3030 2642 3031 bool is_valid_policy_hnd(const struct policy_handle *hnd)3032 {3033 struct policy_handle tmp;3034 ZERO_STRUCT(tmp);3035 return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);3036 }3037 3038 bool policy_hnd_equal(const struct policy_handle *hnd1,3039 const struct policy_handle *hnd2)3040 {3041 if (!hnd1 || !hnd2) {3042 return false;3043 }3044 3045 return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);3046 }3047 3048 2643 /**************************************************************** 3049 2644 strip off leading '\\' from a hostname … … 3076 2671 return ret; 3077 2672 } 2673 2674 bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result) 2675 { 2676 if (!NT_STATUS_IS_OK(err1)) { 2677 *result = err1; 2678 return true; 2679 } 2680 if (!NT_STATUS_IS_OK(err2)) { 2681 *result = err2; 2682 return true; 2683 } 2684 return false; 2685 } 2686 2687 int timeval_to_msec(struct timeval t) 2688 { 2689 return t.tv_sec * 1000 + (t.tv_usec+999) / 1000; 2690 } 2691 2692 /******************************************************************* 2693 Check a given DOS pathname is valid for a share. 2694 ********************************************************************/ 2695 2696 char *valid_share_pathname(TALLOC_CTX *ctx, const char *dos_pathname) 2697 { 2698 char *ptr = NULL; 2699 2700 if (!dos_pathname) { 2701 return NULL; 2702 } 2703 2704 ptr = talloc_strdup(ctx, dos_pathname); 2705 if (!ptr) { 2706 return NULL; 2707 } 2708 /* Convert any '\' paths to '/' */ 2709 unix_format(ptr); 2710 ptr = unix_clean_name(ctx, ptr); 2711 if (!ptr) { 2712 return NULL; 2713 } 2714 2715 /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */ 2716 if (strlen(ptr) > 2 && ptr[1] == ':' && ptr[0] != '/') 2717 ptr += 2; 2718 2719 /* Only absolute paths allowed. */ 2720 if (*ptr != '/') 2721 return NULL; 2722 2723 return ptr; 2724 }
Note:
See TracChangeset
for help on using the changeset viewer.