(create_and_write_cookie): try to return better (and correct) errors.
Based on a patch from Love <lha@e.kth.se> git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@6154 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -50,6 +50,12 @@ int xauthfile_size = sizeof(xauthfile);
|
|||||||
u_char cookie[16];
|
u_char cookie[16];
|
||||||
size_t cookie_len = sizeof(cookie);
|
size_t cookie_len = sizeof(cookie);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copy data from `fd1' to `fd2', {en,de}crypting with cfb64
|
||||||
|
* with `mode' and state stored in `iv', `schedule', and `num'.
|
||||||
|
* Return -1 if error, 0 if eof, else 1
|
||||||
|
*/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_enccopy (int fd1, int fd2, int mode, des_cblock *iv,
|
do_enccopy (int fd1, int fd2, int mode, des_cblock *iv,
|
||||||
des_key_schedule schedule, int *num)
|
des_key_schedule schedule, int *num)
|
||||||
@@ -82,8 +88,8 @@ do_enccopy (int fd1, int fd2, int mode, des_cblock *iv,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
copy_encrypted (int fd1, int fd2, des_cblock *iv,
|
copy_encrypted (int fd1, int fd2,
|
||||||
des_key_schedule schedule)
|
des_cblock *iv, des_key_schedule schedule)
|
||||||
{
|
{
|
||||||
des_cblock iv1, iv2;
|
des_cblock iv1, iv2;
|
||||||
int num1 = 0, num2 = 0;
|
int num1 = 0, num2 = 0;
|
||||||
@@ -223,6 +229,14 @@ try_pipe (struct x_socket *s, int dpy, const char *pattern)
|
|||||||
}
|
}
|
||||||
#endif /* MAY_HAVE_X11_PIPES */
|
#endif /* MAY_HAVE_X11_PIPES */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Try to create a TCP socket in `s' corresponding to display `dpy'.
|
||||||
|
*
|
||||||
|
* 0 if all is OK
|
||||||
|
* -1 if bind failed badly
|
||||||
|
* 1 if dpy is already used
|
||||||
|
*/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
try_tcp (struct x_socket *s, int dpy)
|
try_tcp (struct x_socket *s, int dpy)
|
||||||
{
|
{
|
||||||
@@ -279,6 +293,10 @@ NULL
|
|||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create the directory corresponding to `path' or fail.
|
||||||
|
*/
|
||||||
|
|
||||||
static void
|
static void
|
||||||
try_mkdir (const char *path)
|
try_mkdir (const char *path)
|
||||||
{
|
{
|
||||||
@@ -298,6 +316,14 @@ try_mkdir (const char *path)
|
|||||||
free (dir);
|
free (dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Allocate a display, returning the number of sockets in `number' and
|
||||||
|
* all the corresponding sockets in `sockets'. If `tcp_socket' is
|
||||||
|
* true, also allcoaet a TCP socket.
|
||||||
|
*
|
||||||
|
* The return value is the display allocated or -1 if an error occurred.
|
||||||
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
get_xsockets (int *number, struct x_socket **sockets, int tcp_socket)
|
get_xsockets (int *number, struct x_socket **sockets, int tcp_socket)
|
||||||
{
|
{
|
||||||
@@ -382,7 +408,8 @@ get_xsockets (int *number, struct x_socket **sockets, int tcp_socket)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
* Change owner on the `n' sockets in `sockets' to `uid', `gid'.
|
||||||
|
* Return 0 is succesful or -1 if an error occurred.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -398,7 +425,8 @@ chown_xsockets (int n, struct x_socket *sockets, uid_t uid, gid_t gid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
* Connect to local display `dnr' with local transport.
|
||||||
|
* Return a file descriptor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -421,11 +449,18 @@ connect_local_xsocket (unsigned dnr)
|
|||||||
err (1, "connecting to local display %u", dnr);
|
err (1, "connecting to local display %u", dnr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create a cookie file with a random cookie for the localhost. The
|
||||||
|
* file name will be stored in `xauthfile' (but not larger than
|
||||||
|
* `xauthfile_size'), and the cookie returned in `cookie', `cookie_sz'.
|
||||||
|
* Return 0 if succesful, or errno.
|
||||||
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
create_and_write_cookie (char *xauthfile,
|
create_and_write_cookie (char *xauthfile,
|
||||||
size_t size,
|
size_t xauthfile_size,
|
||||||
u_char *cookie,
|
u_char *cookie,
|
||||||
size_t sz)
|
size_t cookie_sz)
|
||||||
{
|
{
|
||||||
Xauth auth;
|
Xauth auth;
|
||||||
char tmp[64];
|
char tmp[64];
|
||||||
@@ -433,7 +468,7 @@ create_and_write_cookie (char *xauthfile,
|
|||||||
FILE *f;
|
FILE *f;
|
||||||
char hostname[MaxHostNameLen];
|
char hostname[MaxHostNameLen];
|
||||||
struct in_addr loopback;
|
struct in_addr loopback;
|
||||||
struct hostent *h;
|
int saved_errno;
|
||||||
|
|
||||||
gethostname (hostname, sizeof(hostname));
|
gethostname (hostname, sizeof(hostname));
|
||||||
loopback.s_addr = htonl(INADDR_LOOPBACK);
|
loopback.s_addr = htonl(INADDR_LOOPBACK);
|
||||||
@@ -446,30 +481,27 @@ create_and_write_cookie (char *xauthfile,
|
|||||||
auth.number = tmp;
|
auth.number = tmp;
|
||||||
auth.name = COOKIE_TYPE;
|
auth.name = COOKIE_TYPE;
|
||||||
auth.name_length = strlen(auth.name);
|
auth.name_length = strlen(auth.name);
|
||||||
auth.data_length = sz;
|
auth.data_length = cookie_sz;
|
||||||
auth.data = (char*)cookie;
|
auth.data = (char*)cookie;
|
||||||
des_rand_data (cookie, sz);
|
des_rand_data (cookie, cookie_sz);
|
||||||
|
|
||||||
strcpy_truncate(xauthfile, "/tmp/AXXXXXX", size);
|
strcpy_truncate(xauthfile, "/tmp/AXXXXXX", xauthfile_size);
|
||||||
fd = mkstemp(xauthfile);
|
fd = mkstemp(xauthfile);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
|
saved_errno = errno;
|
||||||
syslog(LOG_ERR, "create_and_write_cookie: mkstemp: %m");
|
syslog(LOG_ERR, "create_and_write_cookie: mkstemp: %m");
|
||||||
return 1;
|
return saved_errno;
|
||||||
}
|
}
|
||||||
f = fdopen(fd, "r+");
|
f = fdopen(fd, "r+");
|
||||||
if(f == NULL){
|
if(f == NULL){
|
||||||
|
saved_errno = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
return 1;
|
return errno;
|
||||||
}
|
}
|
||||||
if(XauWriteAuth(f, &auth) == 0) {
|
if(XauWriteAuth(f, &auth) == 0) {
|
||||||
fclose(f);
|
saved_errno = errno;
|
||||||
return 1;
|
fclose(f);
|
||||||
}
|
return saved_errno;
|
||||||
|
|
||||||
h = gethostbyname (hostname);
|
|
||||||
if (h == NULL) {
|
|
||||||
fclose (f);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -487,12 +519,13 @@ create_and_write_cookie (char *xauthfile,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (XauWriteAuth(f, &auth) == 0) {
|
if (XauWriteAuth(f, &auth) == 0) {
|
||||||
fclose (f);
|
saved_errno = errno;
|
||||||
return 1;
|
fclose (f);
|
||||||
|
return saved_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fclose(f))
|
if(fclose(f))
|
||||||
return 1;
|
return errno;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user