~ubuntu-branches/ubuntu/quantal/splitvt/quantal

« back to all changes in this revision

Viewing changes to .pc/old.changes.patch/utmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Mònica Ramírez Arceda
  • Date: 2011-02-13 00:21:26 UTC
  • Revision ID: james.westby@ubuntu.com-20110213002126-dl9ggx3zwvpu0awj
Tags: 1.6.6-8
* Adopt the package. (Closes: #489450)
* Switch to quilt format: add old.changes.patch.
* Tidy up debian/copyright: organize it in sections, add copyright
  notice, add GPL version and add myself as a Debian mantainer.
* Manpage:
  - Change - by \- to use minus sign instead of hyphen in manpage 
    (fix.man.minus.sign.patch).
  - Add .TB macro (fix.man.macro.not.defined.patch).
* Override lintian warning (setgid-binary): splitvt needs write 
  access to /var/run/utmp file.
* Add debian/install, debian/docs, debian/examples and
  debian/splitvt.manpages in order to minimize debian/rules.
* Update debian/rules to use overrides of dh_*.
* Delete Homepage field in debian/control and debian/watch: 
  upstream site doesn't exist any more.
* Bump to Standards-Version 3.9.1. No changes required.
* Add Vcs-Git, Vcs-Browser fields in debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*  utmp.c    Shareware Copyright by Sam Lantinga       10/6/93 */
 
3
 
 
4
#include        <sys/types.h>
 
5
#include        <sys/stat.h>
 
6
#include        <fcntl.h>
 
7
#include        <utmp.h>
 
8
#include        <stdio.h>
 
9
#include        <string.h>
 
10
 
 
11
#ifdef DEBUG_UTMP
 
12
#undef  UTMP_FILE
 
13
#define UTMP_FILE  "/tmp/utmp"
 
14
#else
 
15
#ifndef UTMP_FILE
 
16
#define UTMP_FILE  "/etc/utmp"
 
17
#endif /* UTMP_FILE */
 
18
#endif /* DEBUG_UTMP */
 
19
 
 
20
 
 
21
/* Remove us from the utmp file, saving our entry to replace later */
 
22
 
 
23
static struct utmp saved_utmp;
 
24
static int utmp_saved=0;
 
25
static char saved_tty[128];
 
26
 
 
27
int remove_me()
 
28
{
 
29
        struct utmp ut;
 
30
        char *tty;
 
31
        time_t now;
 
32
 
 
33
        if ( ! isatty(0) )
 
34
                return(-1);
 
35
 
 
36
        tty=(char *)ttyname(0);
 
37
        if ( tty == NULL || strlen(tty)+1 > sizeof(saved_tty) )
 
38
                return(-1);
 
39
 
 
40
        /* Retrieve our utmp record */
 
41
        (void) time(&now);
 
42
        if ( get_utmp(tty, &ut) == 0 ) {
 
43
                /* Save the utmp entry and tty pathname */
 
44
                utmp_saved=1;
 
45
                d_copy((char *)&ut, (char *)&saved_utmp, sizeof(ut));
 
46
                strcpy(saved_tty, tty);
 
47
 
 
48
                /* Clean out the entry and return */
 
49
                ut.ut_name[0]='\0';
 
50
                ut.ut_time=now;
 
51
#ifdef USER_PROCESS
 
52
                ut.ut_type = DEAD_PROCESS;
 
53
#endif
 
54
#ifdef HAVE_UTHOST
 
55
                ut.ut_host[0]='\0';
 
56
#endif
 
57
                return(set_utmp(tty, &ut));
 
58
        }
 
59
        /* Nothing to clean out, good. */
 
60
        return(0);
 
61
}
 
62
 
 
63
 
 
64
int replace_me()
 
65
{
 
66
        if ( utmp_saved )
 
67
                return(set_utmp(saved_tty, &saved_utmp));
 
68
        return(0);
 
69
}
 
70
        
 
71
int get_utmp(tty, save)
 
72
char *tty;
 
73
struct utmp *save;
 
74
{
 
75
        int fd;
 
76
        char *ttyptr;
 
77
        struct utmp ut;
 
78
 
 
79
        /* See if we can open the utmp file */
 
80
        if ( (fd=open(UTMP_FILE, O_RDWR)) < 0 )
 
81
                return(-1);
 
82
 
 
83
        /* Get the ttyxy form of the tty pathname if possible. */
 
84
        if ( *tty == '/' ) {
 
85
                for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
 
86
                        if ( *ttyptr == '/' )
 
87
                                break;
 
88
                }
 
89
                if ( *ttyptr == '/' )
 
90
                        ++ttyptr;
 
91
        } else
 
92
                ttyptr=tty;
 
93
 
 
94
        while (read(fd,(char *) &ut, sizeof(ut)) == sizeof(ut)) {
 
95
                if (strncmp(ttyptr, ut.ut_line, sizeof(ut.ut_line)) == 0) {
 
96
                        /* Break out; we've found our entry! */
 
97
                        if ( save )
 
98
                                d_copy((char *)&ut, save, sizeof(ut));
 
99
                        close(fd);
 
100
                        return(0);
 
101
                }
 
102
        }
 
103
        /* We never found an entry for our tty */
 
104
        close(fd);
 
105
        return(-1);
 
106
}
 
107
 
 
108
int set_utmp(tty, save)
 
109
char *tty;
 
110
struct utmp *save;
 
111
{
 
112
        int fd, found=0;
 
113
        char *ttyptr;
 
114
        struct utmp ut;
 
115
 
 
116
        /* See if we can open the utmp file */
 
117
        if ( (fd=open(UTMP_FILE, O_RDWR)) < 0 )
 
118
                return(-1);
 
119
 
 
120
        /* Get the ttyxy form of the tty pathname if possible. */
 
121
        if ( *tty == '/' ) {
 
122
                for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
 
123
                        if ( *ttyptr == '/' )
 
124
                                break;
 
125
                }
 
126
                if ( *ttyptr == '/' )
 
127
                        ++ttyptr;
 
128
        } else
 
129
                ttyptr=tty;
 
130
 
 
131
        while (read(fd,(char *) &ut, sizeof(ut)) == sizeof(ut)) {
 
132
                if (strncmp(ttyptr, ut.ut_line, sizeof(ut.ut_line)) == 0) {
 
133
                        found=1;
 
134
                        lseek(fd, -(long)sizeof(struct utmp), 1);
 
135
                        break;
 
136
                }
 
137
        }
 
138
 
 
139
        /* Add a new entry to the utmp file if we can't find our entry */
 
140
        if ( ! found )
 
141
        { /* Reopen to avoid a race with other end-of-utmp entries. */
 
142
                (void) close(fd);
 
143
                if ( (fd=open(UTMP_FILE, (O_RDWR|O_APPEND))) < 0 )
 
144
                        return -1;
 
145
        }
 
146
 
 
147
        if (write(fd, (char *)save, sizeof(*save)) != sizeof(*save)) {
 
148
                (void) close(fd);
 
149
                return -1;
 
150
        }
 
151
        return(close(fd));
 
152
}
 
153
 
 
154
        
 
155
/* Set up a utmp entry and tty for a user */
 
156
 
 
157
int addutmp(user, uid, tty)
 
158
char *user;             /* The user to add to the utmp file */
 
159
int uid;                /* The uid corresponding to user */
 
160
char *tty;              /* /dev/ttyxx */
 
161
{
 
162
        struct stat sb;
 
163
        struct utmp ut;
 
164
        char *ttyptr;
 
165
 
 
166
        /* Retrieve any existing utmp entry */
 
167
        d_zero((char *)&ut, sizeof(ut));
 
168
        (void) get_utmp(tty, &ut);
 
169
 
 
170
        /* Get the ttyxy form of the tty pathname if possible. */
 
171
        if ( *tty == '/' ) {
 
172
                for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
 
173
                        if ( *ttyptr == '/' )
 
174
                                break;
 
175
                }
 
176
                if ( *ttyptr == '/' )
 
177
                        ++ttyptr;
 
178
        } else
 
179
                ttyptr=tty;
 
180
 
 
181
        /* Customize the utmp entry */
 
182
        strncpy(ut.ut_name, user, sizeof(ut.ut_name)-1);
 
183
        ut.ut_name[sizeof(ut.ut_name)-1]='\0';
 
184
        strncpy(ut.ut_line, ttyptr, sizeof(ut.ut_line)-1);
 
185
        ut.ut_line[sizeof(ut.ut_line)-1]='\0';
 
186
#ifdef USER_PROCESS
 
187
        ut.ut_type=USER_PROCESS;
 
188
        ut.ut_pid=getpid();
 
189
#endif
 
190
#if defined(HAVE_UTHOST)
 
191
        /* remove_me() should be called before this function */
 
192
        if ( utmp_saved ) {
 
193
                strncpy(ut.ut_host, saved_utmp.ut_host, sizeof(ut.ut_host)-1);
 
194
                ut.ut_host[sizeof(ut.ut_host)-1]='\0';
 
195
        }
 
196
#endif
 
197
        (void) time(&ut.ut_time);
 
198
 
 
199
#if !defined(SOLARIS) && !defined(IRIX)
 
200
        /* Solaris and Irix machines do this automatically */
 
201
        /* Change the ownership and mode of the tty */
 
202
        if ( stat(tty, &sb) == 0 ) {
 
203
                (void) chmod(tty, 0620);  /* crw--w---- */
 
204
                (void) chown(tty, uid, sb.st_gid);
 
205
        }
 
206
#endif
 
207
        return(set_utmp(tty, &ut));
 
208
}
 
209
        
 
210
 
 
211
/* End a utmp entry and tty for a user and a tty */
 
212
 
 
213
int delutmp(user, tty)
 
214
char *user;
 
215
char *tty;              /* /dev/ttyxx */
 
216
{
 
217
        struct stat sb;
 
218
        struct utmp ut;
 
219
        int retval=0;
 
220
 
 
221
        /* Retrieve any existing utmp entry */
 
222
        d_zero((char *)&ut, sizeof(ut));
 
223
        if ( get_utmp(tty, &ut) == 0 ) {
 
224
                /* Clear the utmp entry */
 
225
                ut.ut_name[0]='\0';
 
226
#ifdef USER_PROCESS
 
227
                ut.ut_type=DEAD_PROCESS;
 
228
#endif
 
229
#if defined(HAVE_UTHOST)
 
230
                ut.ut_host[0]='\0';
 
231
#endif
 
232
                (void) time(&ut.ut_time);
 
233
                retval=set_utmp(tty, &ut);
 
234
        }
 
235
 
 
236
#if !defined(SOLARIS) && !defined(IRIX)
 
237
        /* Solaris and Irix machines do this automatically */
 
238
        /* Reset the owner and mode of the tty */
 
239
        if ( stat(tty, &sb) == 0 ) {
 
240
                (void) chmod(tty, 0666);        /* crw-rw-rw- */
 
241
                (void) chown(tty, 0, sb.st_gid);
 
242
        }
 
243
#endif
 
244
        return(retval);
 
245
}
 
246
 
 
247