Plug leaks in selinux context handling in fsm
[platform/upstream/rpm.git] / lib / fsm.h
1 #ifndef H_FSM
2 #define H_FSM
3
4 /** \ingroup payload
5  * \file lib/fsm.h
6  * File state machine to handle a payload within an rpm package.
7  */
8
9 #include <rpm/rpmfi.h>
10
11 extern int _fsm_debug;
12
13 /**
14  */
15 #define FSM_VERBOSE     0x8000
16 #define FSM_INTERNAL    0x4000
17 #define FSM_SYSCALL     0x2000
18 #define FSM_DEAD        0x1000
19
20 #define _fv(_a)         ((_a) | FSM_VERBOSE)
21 #define _fi(_a)         ((_a) | FSM_INTERNAL)
22 #define _fs(_a)         ((_a) | (FSM_INTERNAL | FSM_SYSCALL))
23 #define _fd(_a)         ((_a) | (FSM_INTERNAL | FSM_DEAD))
24
25 typedef enum fileStage_e {
26     FSM_UNKNOWN =   0,
27     FSM_INIT    =  _fd(1),
28     FSM_PRE     =  _fd(2),
29     FSM_PROCESS =  _fv(3),
30     FSM_POST    =  _fd(4),
31     FSM_UNDO    =  5,
32     FSM_FINI    =  6,
33
34     FSM_PKGINSTALL      = _fd(7),
35     FSM_PKGERASE        = _fd(8),
36     FSM_PKGBUILD        = _fd(9),
37     FSM_PKGCOMMIT       = _fd(10),
38     FSM_PKGUNDO         = _fd(11),
39
40     FSM_CREATE  =  _fd(17),
41     FSM_MAP     =  _fd(18),
42     FSM_MKDIRS  =  _fi(19),
43     FSM_RMDIRS  =  _fi(20),
44     FSM_MKLINKS =  _fi(21),
45     FSM_NOTIFY  =  _fd(22),
46     FSM_DESTROY =  _fd(23),
47     FSM_VERIFY  =  _fd(24),
48     FSM_COMMIT  =  _fd(25),
49
50     FSM_UNLINK  =  _fs(33),
51     FSM_RENAME  =  _fs(34),
52     FSM_MKDIR   =  _fs(35),
53     FSM_RMDIR   =  _fs(36),
54     FSM_LSETFCON=  _fs(39),
55     FSM_CHOWN   =  _fs(40),
56     FSM_LCHOWN  =  _fs(41),
57     FSM_CHMOD   =  _fs(42),
58     FSM_UTIME   =  _fs(43),
59     FSM_SYMLINK =  _fs(44),
60     FSM_LINK    =  _fs(45),
61     FSM_MKFIFO  =  _fs(46),
62     FSM_MKNOD   =  _fs(47),
63     FSM_LSTAT   =  _fs(48),
64     FSM_STAT    =  _fs(49),
65     FSM_READLINK=  _fs(50),
66     FSM_CHROOT  =  _fs(51),
67
68     FSM_NEXT    =  _fd(65),
69     FSM_EAT     =  _fd(66),
70     FSM_POS     =  _fd(67),
71     FSM_PAD     =  _fd(68),
72     FSM_TRAILER =  _fd(69),
73     FSM_HREAD   =  _fd(70),
74     FSM_HWRITE  =  _fd(71),
75     FSM_DREAD   =  _fs(72),
76     FSM_DWRITE  =  _fs(73),
77
78     FSM_ROPEN   =  _fs(129),
79     FSM_READ    =  _fs(130),
80     FSM_RCLOSE  =  _fs(131),
81     FSM_WOPEN   =  _fs(132),
82     FSM_WRITE   =  _fs(133),
83     FSM_WCLOSE  =  _fs(134)
84 } fileStage;
85 #undef  _fv
86 #undef  _fi
87 #undef  _fs
88 #undef  _fd
89
90 /** \ingroup payload
91  */
92 typedef enum cpioMapFlags_e {
93     CPIO_MAP_PATH       = (1 << 0),
94     CPIO_MAP_MODE       = (1 << 1),
95     CPIO_MAP_UID        = (1 << 2),
96     CPIO_MAP_GID        = (1 << 3),
97     CPIO_FOLLOW_SYMLINKS= (1 << 4), /*!< only for building. */
98     CPIO_MAP_ABSOLUTE   = (1 << 5),
99     CPIO_MAP_ADDDOT     = (1 << 6),
100     CPIO_ALL_HARDLINKS  = (1 << 7), /*!< fail if hardlinks are missing. */
101     CPIO_MAP_TYPE       = (1 << 8),  /*!< only for building. */
102     CPIO_SBIT_CHECK     = (1 << 9)
103 } cpioMapFlags;
104
105 typedef struct fsmIterator_s * FSMI_t;
106 typedef struct fsm_s * FSM_t;
107
108 typedef struct hardLink_s * hardLink_t;
109
110 /** \ingroup payload
111  * File name and stat information.
112  */
113 struct fsm_s {
114     const char * path;          /*!< Current file name. */
115     const char * opath;         /*!< Original file name. */
116     FD_t cfd;                   /*!< Payload file handle. */
117     FD_t rfd;                   /*!<  read: File handle. */
118     char * rdbuf;               /*!<  read: Buffer. */
119     char * rdb;                 /*!<  read: Buffer allocated. */
120     size_t rdsize;              /*!<  read: Buffer allocated size. */
121     size_t rdlen;               /*!<  read: Number of bytes requested.*/
122     size_t rdnb;                /*!<  read: Number of bytes returned. */
123     FD_t wfd;                   /*!< write: File handle. */
124     char * wrbuf;               /*!< write: Buffer. */
125     char * wrb;                 /*!< write: Buffer allocated. */
126     size_t wrsize;              /*!< write: Buffer allocated size. */
127     size_t wrlen;               /*!< write: Number of bytes requested.*/
128     size_t wrnb;                /*!< write: Number of bytes returned. */
129     FSMI_t iter;                /*!< File iterator. */
130     int ix;                     /*!< Current file iterator index. */
131     hardLink_t links;           /*!< Pending hard linked file(s). */
132     hardLink_t li;              /*!< Current hard linked file(s). */
133     rpm_loff_t * archiveSize;   /*!< Pointer to archive size. */
134     char ** failedFile;         /*!< First file name that failed. */
135     const char * subdir;        /*!< Current file sub-directory. */
136     char subbuf[64];    /* XXX eliminate */
137     const char * osuffix;       /*!< Old, preserved, file suffix. */
138     const char * nsuffix;       /*!< New, created, file suffix. */
139     const char * suffix;        /*!< Current file suffix. */
140     char sufbuf[64];    /* XXX eliminate */
141     short * dnlx;               /*!< Last dirpath verified indexes. */
142     char * ldn;                 /*!< Last dirpath verified. */
143     int ldnlen;                 /*!< Last dirpath current length. */
144     int ldnalloc;               /*!< Last dirpath allocated length. */
145     int postpone;               /*!< Skip remaining stages? */
146     int diskchecked;            /*!< Has stat(2) been performed? */
147     int exists;                 /*!< Does current file exist on disk? */
148     int mkdirsdone;             /*!< Have "orphan" dirs been created? */
149     int astriplen;              /*!< Length of buildroot prefix. */
150     int rc;                     /*!< External file stage return code. */
151     int commit;                 /*!< Commit synchronously? */
152     cpioMapFlags mapFlags;      /*!< Bit(s) to control mapping. */
153     const char * dirName;       /*!< File directory name. */
154     const char * baseName;      /*!< File base name. */
155     const char * digest;        /*!< Binary checksum (NULL disables). */
156     security_context_t fcontext;/*!< File security context (NULL disables). */
157     pgpHashAlgo digestalgo;     /*!< File checksum algorithm */
158     
159     unsigned fflags;            /*!< File flags. */
160     rpmFileAction action;       /*!< File disposition. */
161     fileStage goal;             /*!< Package state machine goal. */
162     fileStage stage;            /*!< External file stage. */
163     fileStage nstage;           /*!< Next file stage. */
164     struct stat sb;             /*!< Current file stat(2) info. */
165     struct stat osb;            /*!< Original file stat(2) info. */
166 };
167
168 #ifdef __cplusplus
169 extern "C" {
170 #endif
171
172 /**
173  * Create file state machine instance.
174  * @return              file state machine
175  */
176 FSM_t newFSM(void);
177
178 /**
179  * Destroy file state machine instance.
180  * @param fsm           file state machine
181  * @return              always NULL
182  */
183 FSM_t freeFSM(FSM_t fsm);
184
185 /**
186  * Load external data into file state machine.
187  * @param fsm           file state machine
188  * @param goal
189  * @param ts            transaction set
190  * @param fi            transaction element file info
191  * @param cfd
192  * @retval archiveSize  pointer to archive size
193  * @retval failedFile   pointer to first file name that failed (malloced)
194  * @return              0 on success
195  */
196 int fsmSetup(FSM_t fsm, fileStage goal,
197                 const rpmts ts,
198                 const rpmfi fi,
199                 FD_t cfd,
200                 rpm_loff_t * archiveSize,
201                 char ** failedFile);
202
203 /**
204  * Clean file state machine.
205  * @param fsm           file state machine
206  * @return              0 on success
207  */
208 int fsmTeardown(FSM_t fsm);
209
210 /**
211  * File state machine driver.
212  * @param fsm           file state machine
213  * @param nstage                next stage
214  * @return              0 on success
215  */
216 int fsmNext(FSM_t fsm, fileStage nstage);
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif  /* H_FSM */