1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
10
* Sun Industry Standards Source License Version 1.2
11
* =================================================
12
* The contents of this file are subject to the Sun Industry Standards
13
* Source License Version 1.2 (the "License"); You may not use this file
14
* except in compliance with the License. You may obtain a copy of the
15
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
17
* Software provided under this License is provided on an "AS IS" basis,
18
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
19
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
20
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
21
* See the License for the specific provisions governing your rights and
22
* obligations concerning the Software.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
42
#include <sys/types.h>
43
#include <sys/socket.h>
44
#include <arpa/inet.h>
45
#include <sys/statvfs.h>
46
#include <interix/interix.h>
47
#include <interix/security.h>
51
#include "basis_types.h"
52
#include "../../../utilbin/sge_passwd.h"
53
#include "msg_wingrid.h"
54
#include "config_file.h"
55
#include "sge_bootstrap.h"
58
static bool wl_enable_windomacc = false;
60
/* ============================================================================
62
* ==========================================================================*/
63
/****** wingrid/wl_copy_string_to_buffer() ***********************************
65
* wl_copy_string_to_buffer() -- Copies one string to a position in a
66
* buffer, sets buffer position and
67
* remaining size of buffer.
70
* static int wl_copy_string_to_buffer(const char *string, char *buf,
71
* int *bufpos, int *bufremain)
74
* Copies a string into a buffer at given position. Checks before copying
75
* if the string will fit into the buffer. After copying, sets the index
76
* of the next free buffer element and the size of the remaining free
77
* buffer, so a subsequent call to this function will copy the next
78
* string to the next free location in the buffer.
81
* const char *string - The string that is to copy into the buffer.
82
* char *buf - Pointer to beginning of the buffer.
83
* int *bufpos - Pointer to the Index of the position in the buffer,
84
* where the string should be copied to.
85
* int *bufremain - Pointer to the number of remaining free bytes in
86
* the buffer before copying.
89
* int *bufpos - Pointer to the Index of the next free position
91
* int *bufremain - Pointer to the number of remaining free bytes in
92
* the buffer after copying.
95
* int - 0 if OK, 1 if the buffer is too small.
98
* MT-NOTE: wl_copy_string_to_buffer() is MT-safe
99
******************************************************************************/
100
static int wl_copy_string_to_buffer(const char *string, char *buf,
101
int *bufpos, int *bufremain)
105
stringlen = strlen(string)+1; /* Include trailing '\0' */
106
if(stringlen > *bufremain) {
109
memcpy(&buf[*bufpos], (unsigned char*)string, stringlen);
110
*bufpos += stringlen;
111
*bufremain -= stringlen;
116
/****** wingrid/wl_getpwuid_ex_r() *******************************************
118
* wl_getpwuid_ex_r() -- MT safe version of extended Interix version of
122
* int wl_getpwuid_ex_r(uid_t uid, struct passwd *pwd, char *buffer,
123
* size_t bufsize, struct passwd **result, uint flags)
126
* MT safe function to retrieve passwd information for a specific UID.
127
* Uses Interix' extended version of getpwuid() that allows to retrieve
128
* full qualified Windows domain user names.
129
* E.g.: "BOFUR+Administrator" instead of "Administrator"
132
* uid_t uid - The UID of the passwd
133
* struct passwd *pwd - Pointer of the passwd struct that is to be filled.
134
* char *buffer - Buffer providing memory for the strings in passwd.
135
* size_t bufsize - Size of the buffer.
136
* uint flags - 0: short name is retrieved
137
* PW_FULLNAME: FQDN name is retrieved
140
* struct passwd **result - Points to pwd after function call,
144
* int - 0 if OK, 1 if the buffer is too small.
147
* MT-NOTE: wl_getpwuid_ex_r() is not MT-safe
148
* It is safe it getpwuid_ex() is not called from any other place.
151
* getpwuid_r(), getpwuid_ex()
152
******************************************************************************/
153
int wl_getpwuid_ex_r(uid_t uid, struct passwd *pwd, char *buffer,
154
size_t bufsize, struct passwd **result, uint flags)
156
static pthread_mutex_t getpwuid_ex_mutex = PTHREAD_MUTEX_INITIALIZER;
160
struct passwd *pwd_ex;
162
pthread_mutex_lock(&getpwuid_ex_mutex);
164
pwd_ex = getpwuid_ex(uid, flags);
165
pwd->pw_uid = pwd_ex->pw_uid;
166
pwd->pw_gid = pwd_ex->pw_gid;
167
pwd->pw_change = pwd_ex->pw_change;
168
pwd->pw_expire = pwd_ex->pw_expire;
173
pwd->pw_name = &buffer[bufpos];
174
ret = wl_copy_string_to_buffer(pwd_ex->pw_name, buffer, &bufpos, &bufremain);
176
pwd->pw_dir = &buffer[bufpos];
177
ret = wl_copy_string_to_buffer(pwd_ex->pw_dir, buffer, &bufpos, &bufremain);
180
pwd->pw_shell = &buffer[bufpos];
181
ret = wl_copy_string_to_buffer(pwd_ex->pw_shell, buffer, &bufpos, &bufremain);
184
pwd->pw_passwd = &buffer[bufpos];
185
ret = wl_copy_string_to_buffer(pwd_ex->pw_passwd, buffer, &bufpos, &bufremain);
188
pwd->pw_gecos = &buffer[bufpos];
189
ret = wl_copy_string_to_buffer(pwd_ex->pw_gecos, buffer, &bufpos, &bufremain);
193
pthread_mutex_unlock(&getpwuid_ex_mutex);
197
/****** wingrid/wl_handle_ls_results() **************************************
199
* wl_handle_ls_results() -- Parse result of external load sensor
202
* bool wl_handle_ls_results(const char *name, const char *value,
203
* const char *host, char *error_string)
206
* Parses the result of an externel loadsensor. Returns true if the
207
* loadsensors reported load, returns false if the loadsensor
211
* bool - true if load is OK, false if load is an error report.
214
* MT-NOTE: wl_handle_ls_results() is MT safe
215
******************************************************************************/
216
/* returns true if name/value/host is a loadvalue */
217
/* error_string should have a size of at least 4 * MAX_STRING_SIZE */
218
bool wl_handle_ls_results(const char *name,
225
if (strcmp(host, "_sge_pseudo_host") == 0) {
226
if (strncmp(name, "error", 5) == 0) {
227
if (error_string != NULL) {
228
sprintf(error_string, "loadsensor report: %s", value);
237
/* ============================================================================
239
* ==========================================================================*/
240
/****** wingrid/wl_get_superuser_id() ***************************************
242
* wl_get_superuser_id() -- return UID of local Administrator
245
* uid_t wl_get_superuser_id()
248
* Return UID of local Administrator.
249
* It is under Windows, unlike Unix, not always 0.
250
* Only the local Administrator is comparable to root, the Domain
251
* Administrator has only limited abilities.
254
* uid_t - the UID of the local Administrator.
257
* MT-NOTE: wl_get_superuser_id() is MT safe
258
******************************************************************************/
259
uid_t wl_get_superuser_id()
264
gid_t wl_get_superuser_gid()
269
/****** wingrid/wl_get_superuser_name() **************************************
271
* wl_get_superuser_name() -- return name of local Administrator
274
* int wl_get_superuser_name(char *buf, int bufsize)
277
* Return name of local Administrator.
278
* The name depends on the language of Windows and could be set manually
279
* by the Admnistrator.
282
* bufsize - sife of buffer that is to receive superuser name.
285
* char *buf - Pointer to a buffer that is to receive superuser name.
289
* 1: buffer to small for superuser name
292
* MT-NOTE: wl_get_superuser_name() is MT safe
293
******************************************************************************/
294
int wl_get_superuser_name(char *buf, int bufsize)
296
struct passwd pwd, *pwdresult;
299
uid_t uid = wl_get_superuser_id();
301
ret = wl_getpwuid_ex_r(uid, &pwd, buffer, sizeof(buffer), &pwdresult, 0);
303
strlcpy(buf, pwd.pw_name, bufsize);
308
/****** wingrid/wl_is_user_id_superuser()*************************************
310
* wl_is_user_id_superuser() -- check if UID is superuser
311
* (= local Administrator)
314
* bool wl_is_user_id_superuser(int uid)
317
* Check if UID is superuser (=local Administrator)
320
* uid - the UID that is to be checked.
323
* bool - true: UID is superuser, false: UID is not superuser
326
* MT-NOTE: wl_is_user_id_superuser() is MT safe
327
******************************************************************************/
328
bool wl_is_user_id_superuser(int uid)
330
/* Only the local Administrator is the 'real' superuser! */
331
return (uid==wl_get_superuser_id());
334
/* ============================================================================
336
* ==========================================================================*/
337
/****** wingrid/wl_setuser() **********************************************
339
* wl_setuser() -- change effective and real uid and gid of process
342
* int wl_setuser(int uid, int gid)
345
* Change effective and real uid and gid of process
348
* int uid - UID that the process should be changed to.
349
* int gid - GID that the process should be changed to.
352
* int - 0 if uid and gid changed correctly, else errno is set and:
353
* 1 can't read passwd information
357
* MT-NOTE: wl_setuser() is MT safe
358
******************************************************************************/
359
int wl_setuser(int uid, int gid, const char *pass, char* err_str)
364
struct passwd pwd, *ppwd=NULL;
366
/* Do this only for domain users */
369
* Get username of UID
370
* (Use not MT safe getpwuid_ex() for full qualified name)
372
ret = wl_getpwuid_ex_r(uid, &pwd, buf, sizeof(buf), &ppwd, 0);
374
sprintf(err_str, MSG_WIN_CANT_GET_PASSWD_INFO, uid);
377
* Read password from file
379
if(pwd.pw_name==NULL ||
380
(pwd.pw_name!=NULL && pwd.pw_name[0]=='\0')) {
381
sprintf(err_str, MSG_WIN_CANT_GET_PASSWD_INFO, uid);
384
ret = setuser(pwd.pw_name, pass, SU_CHECK);
386
sprintf(err_str, MSG_WIN_CHECK_SETUSER_FAILED, pwd.pw_name,
387
strerror(errno)?strerror(errno):"<NULL>", errno);
391
ret = setuser(pwd.pw_name, pass, SU_COMPLETE);
396
} while(ret != 0 && try < 5);
399
sprintf(err_str, MSG_WIN_SETUSER_FAILED, pwd.pw_name,
400
strerror(errno)?strerror(errno):"<NULL>", errno);
410
sprintf(err_str, MSG_WIN_CANT_SETGID_IN_SETUSER, gid,
411
strerror(errno)?strerror(errno):"<NULL>", errno);
415
sprintf(err_str, MSG_WIN_CANT_SETUID_IN_SETUSER, uid,
416
strerror(errno)?strerror(errno):"<NULL>", errno);
423
bool wl_use_sgepasswd()
425
return wl_enable_windomacc;
428
void wl_set_use_sgepasswd(bool use_it)
430
wl_enable_windomacc = use_it;
433
/* ============================================================================
434
* User Name modifications
435
* ==========================================================================*/
436
/****** wingrid/wl_strip_hostname() ******************************************
438
* wl_strip_hostname() -- strip hostname from full qualified username
441
* char *wl_strip_hostname(char *user_name)
444
* Strips the hostname from the full qualified username, i.e.
445
* returns "username" for "HOST+username".
448
* char *user_name - Pointer to a dynamically allocated buffer that
449
* contains the full qualified username. This buffer
450
* gets freed in this function.
453
* char * - Pointer to a dynamically allocated buffer that contains
454
* the stripped user name. This buffer must be freed after usage.
457
* MT-NOTE: wl_strip_hostname() is MT-safe.
460
******************************************************************************/
461
char *wl_strip_hostname(char *user_name)
464
char *ret = strdup(user_name);
467
token = strtok_r(user_name, "+", &lasts);
469
token = strtok_r(NULL, " ", &lasts);
478
/****** wingrid/wl_build_fq_local_name() *************************************
480
* wl_build_fq_local_name() -- build full qualified local user name
483
* int wl_build_fq_local_name(const char *user, char *fq_name)
486
* Builds a full qualified Interix user name from the given short
487
* user name. A full qualified username is of the form
488
* DOMAIN+USER, where DOMAIN is in this case the name of the local host.
491
* const char *user - short name of the user
494
* char *fq_name - buffer that is to receive the full qualified name.
495
* It's size must be 1024.
498
* int - 0 if full qualified name was successfully build
499
* 1 if the user name was not provided in short format
502
* MT-NOTE: wl_build_fq_local_name() is MT-safe.
505
******************************************************************************/
506
int wl_build_fq_local_name(const char *user, char *fq_name)
512
* do only if user name is not already full qualified
514
if(user != NULL && fq_name != NULL && strstr(user, "+")==NULL) {
515
gethostname(hostname, 128);
516
snprintf(fq_name, 1023, "%s+%s", hostname, user);
522
/* ============================================================================
523
* REPLACEMENTS of buggy libc functions
524
* ==========================================================================*/
525
/* ----------------------------------------------------------------------------
527
* --------------------------------------------------------------------------*/
528
/****** wingrid/wl_stat() / wingrid/wl_statvfs()***************************
530
* wl_stat() / wl_statvfs() - obtain info about files
533
* int wl_stat(const char *path, struct stat *buf)
534
* int wl_statvfs(const char *path, struct statvfs *buf)
537
* Obtain information about the file pointed to by path.
538
* This functions are replacements for the libc functions of Interix
539
* that don't work on network drives.
540
* Limitation of this functions: The process must have the permissions
541
* to open the file for reading.
544
* const char *path - path of the file to obtain info about.
547
* struct stat *buf - Information bout the file.
548
* struct statvfs *buf -
551
* int - 0 on successful completion, otherwise -1 and errno is set.
554
* MT-NOTE: wl_stat() and wl_statvfs are MT safe.
555
******************************************************************************/
556
int wl_stat(const char *path, struct stat *buf)
558
/* stat() doesn't work on NFS drives, have to use fstat() */
562
if((fd = open(path, O_RDONLY))!=-1) {
563
ret = fstat(fd, buf);
569
int wl_statvfs(const char *path, struct statvfs *buf)
571
/* statvfs() doesn't work on NFS drives, have to use fstatvfs() */
575
if((fd = open(path, O_RDONLY))!=-1) {
576
ret = fstatvfs(fd, buf);
582
/* ----------------------------------------------------------------------------
584
* --------------------------------------------------------------------------*/
585
/****** wingrid/wl_xdrmem_create() ******************************************
587
* wl_xdrmem_create() -- Initialize XDR stream
590
* void wl_xdrmem_create(XDR *xdrs, const caddr_t addr,
591
* const uint_t size, const enum xdr_op op)
594
* Initializes XDR stream for usage.
597
* XDR *xdrs - XDR stream
598
* const caddr_t addr - Buffer
599
* const uint_t size - Size of buffer
600
* const enum xdr_op op - Direction of XDR stream (encode or decode)
603
* XDR *xdrs - Initialized XDR stream
606
* MT-NOTE: wl_xdrmem_create() is MT safe
607
******************************************************************************/
608
void wl_xdrmem_create(XDR *xdrs, const caddr_t addr,
609
const uint_t size, const enum xdr_op op)
614
xdrs->x_public = addr;
615
xdrs->x_private = NULL;
621
/****** wingrid/wl_xdr_destroy() ********************************************
623
* wl_xdr_destroy() -- Destroy XDR stream
626
* void wl_xdr_destroy(XDR *xdrs)
629
* Destroys XDR stream. Doesn't free any buffers!
632
* XDR *xdrs - XDR stream
635
* MT-NOTE: wl_xdr_destroy() is MT safe
636
******************************************************************************/
637
void wl_xdr_destroy(XDR *xdrs)
641
/****** wingrid/wl_xdr_double() *********************************************
643
* wl_xdr_double() -- Convert a double
646
* bool_t wl_xdr_double(XDR *xdrs, double *dp)
649
* Encodes or decodes a variable of type double.
652
* XDR *xdrs - If decoding: Stream that contains the encoded value
653
* double *dp - If encoding: Pointer to the variable that is to encode
656
* XDR *xdrs - If encoding: Stream that contains the encoded value
657
* double *dp - If decoding: Pointer to the variable that receives value
660
* MT-NOTE: wl_xdr_double() is MT safe
661
******************************************************************************/
662
bool_t wl_xdr_double(XDR *xdrs, double *dp)
666
if(xdrs==NULL || dp==NULL) {
670
if(xdrs->x_op==XDR_ENCODE)
673
((char*)xdrs->x_public)[i+xdrs->x_handy] = ((char*)dp)[7-i];
677
else if(xdrs->x_op==XDR_DECODE)
680
((char*)dp)[7-i] = ((char*)xdrs->x_public)[i+xdrs->x_handy];
687
/****** wingrid/wl_xdr_getpos() *********************************************
689
* wl_xdr_getpos() -- Get current position in stream
692
* uint_t wl_xdr_getpos(const XDR *xdrs)
695
* Returns position in XDR stream.
698
* const XDR *xdrs - Stream on which position is to be returned.
701
* uint_t - Current position on stream.
704
* MT-NOTE: wl_xdr_getpos() is MT safe
705
******************************************************************************/
706
uint_t wl_xdr_getpos(const XDR *xdrs)
708
return xdrs->x_handy;