File: common.c

package info (click to toggle)
xtell 2.9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 188 kB
  • ctags: 73
  • sloc: ansic: 982; sh: 117; makefile: 78; python: 22
file content (84 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * ================================================================
 * Please view:
 *
 * 	README for program information.
 * 	COPYING for distribution information.
 *
 * 	based on ident2 by Michael Bacarella (defile@nyct.net)
 *
 * ================================================================ 
 */

#include "xtelld.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
              
/* ------------------------------------------------------------------
 * get_request	:	a fgets for file descriptors 
 * ------------------------------------------------------------------
 */
short int get_request(int d, char buffer[], unsigned short int len)
{
    unsigned short i;
    char ch;

    memset(buffer, 0, len);
    for (i = 0; i < len; i++) {
	if (read(d, &ch, 1) != 1)
	    return -1;
	else if (ch == '\n')
	    break;
	else if (ch == '\r') {
	    read(d, &ch, 1);	/* it better be \n */
	    break;
	} else
	    buffer[i] = ch;
    }
    buffer[i] = '\0';
    return i;
}

int logFILE(const char *fromluser)
{
    int filedes;
    char *path;
    struct passwd *passs;
    struct stat s;

    if ((passs = getpwnam(fromluser)) != NULL) {
	path = malloc(strlen(passs->pw_dir) + sizeof(LOGFILE) + 1);
	if (path == NULL)
	    return -1;
	strcpy(path, passs->pw_dir);
	strcat(path, "/");
	strcat(path, LOGFILE);
#ifdef O_NOFOLLOW        
	if ((filedes = open(path, O_WRONLY | O_APPEND | O_NOFOLLOW)) != -1) {
#else
	if ((filedes = open(path, O_WRONLY | O_APPEND)) != -1) {
#endif
           if (fstat(filedes, &s) == 0) {
	        if (S_ISREG(s.st_mode)) {
		    free(path);
		    return filedes;
		}
	    }
	}
	free(path);
    }
    return -1;
}


/* ------------------------------------------------------------------
 * killsock:
 *	violently kills a socket
 * ------------------------------------------------------------------
 */
void killsock(int s)
{
    shutdown(s, 2);
    close(s);
}