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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/******************************************************************************
(c) 2000-2003 Christine Caulfield christine.caulfield@googlemail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
******************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/utsname.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "utils.h"
void add_string(unsigned char *packet, int *ptr,
const unsigned char *string)
{
int len = strlen((char *)string);
packet[(*ptr)++] = len;
strcpy((char *)packet + *ptr, (char *)string);
*ptr += len;
}
void get_string(unsigned char *packet, int *ptr,
unsigned char *string)
{
int len = packet[(*ptr)++];
strncpy((char*)string, (char*)packet + *ptr, len);
string[len] = '\0';
*ptr += len;
}
#ifdef VERBOSE_DEBUG
void chrissie_debuglog(char *fmt, ...)
{
static time_t starttime = time(NULL);
va_list ap;
fprintf(stderr, "%5ld: ", time(NULL)-starttime);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
}
#endif
#ifndef HAVE_OPENPTY
int chrissie_openpty(int *master, int *slave, char *a, char *b, char *d)
{
char ptyname[] = "/dev/ptyCP";
char c;
char *line=NULL;
int i;
int pty=-1, t;
int gotpty=0;
struct stat stb;
for (c='p'; c <= 'z'; c++)
{
line = ptyname;
line[strlen("/dev/pty")] = c;
line[strlen("/dev/ptyC")] = '0';
if (stat(line,&stb) < 0)
break;
for (i=0; i < 16; i++)
{
line[strlen("/dev/ptyC")]= "0123456789abcdef"[i];
if ( (pty=open(line,O_RDWR)) > 0)
{
gotpty = 1;
break;
}
}
if (gotpty) break;
}
if (!gotpty)
{
debuglog(("No ptys available for connection"));
return -1;
}
line[strlen("/dev/")] = 't';
if ( (t=open(line,O_RDWR)) < 0)
{
debuglog(("Error connecting to physical terminal: %m"));
return -1;
}
*master = pty;
*slave = t;
return 0;
}
#endif
/* Expand \ sequences in /etc/issue.net file and add CRs to
LFs */
int expand_issue(const char *original, int len, char *newstring, int maxlen,
const char *servicename)
{
int i,j;
char scratch[132];
time_t t;
struct tm *tm;
struct utsname un;
uname(&un);
j=0;
for (i=0; i<len; i++)
{
if (j >= maxlen) break;
if (original[i] == '\n')
newstring[j++] = '\r';;
if (original[i] == '\\' || original[i] == '%')
{
i++;
switch (original[i])
{
case '\\':
case '%':
newstring[j++] = original[i];
break;
case 'b':
strcpy(newstring+j, "9600");
j+=4;
break;
case 'd':
t = time(NULL);
tm = localtime(&t);
strftime(scratch, sizeof(scratch), "%a %b %d %Y", tm);
strcpy(newstring+j, scratch);
j+=strlen(scratch);
break;
case 's':
strcpy(newstring+j, un.sysname);
j+=strlen(un.sysname);
break;
case 'l':
strcpy(newstring+j, servicename);
j+=strlen(servicename);
break;
case 'm':
strcpy(newstring+j, un.machine);
j+=strlen(un.machine);
break;
case 'n':
strcpy(newstring+j, un.nodename);
j+=strlen(un.nodename);
break;
#ifdef __GNU_SOURCE
case 'o':
strcpy(newstring+j, un.domainname);
j+=strlen(un.domainname);
break;
#endif
case 'r':
strcpy(newstring+j, un.release);
j+=strlen(un.release);
break;
case 't':
t = time(NULL);
tm = localtime(&t);
strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
strcpy(newstring+j, scratch);
j+=strlen(scratch);
break;
/* No, I am not doing number of users logged in... */
case 'v':
strcpy(newstring+j, un.version);
j+=strlen(un.version);
break;
}
}
else
{
newstring[j++] = original[i];
}
}
return j;
}
|